Thierry Fournier | e726b14 | 2016-02-11 17:57:57 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Lua unsafe core engine |
| 3 | * |
| 4 | * Copyright 2015-2016 Thierry Fournier <tfournier@arpalert.org> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | */ |
| 12 | |
Bertrand Jacquin | f4c12d4 | 2021-01-21 21:14:07 +0000 | [diff] [blame] | 13 | #define _GNU_SOURCE |
| 14 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 15 | #include <ctype.h> |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 16 | #include <setjmp.h> |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 17 | |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 18 | #include <lauxlib.h> |
| 19 | #include <lua.h> |
| 20 | #include <lualib.h> |
| 21 | |
Thierry FOURNIER | 463119c | 2015-03-10 00:35:36 +0100 | [diff] [blame] | 22 | #if !defined(LUA_VERSION_NUM) || LUA_VERSION_NUM < 503 |
| 23 | #error "Requires Lua 5.3 or later." |
Cyril Bonté | dc0306e | 2015-03-02 00:08:40 +0100 | [diff] [blame] | 24 | #endif |
| 25 | |
Willy Tarreau | 8d2b777 | 2020-05-27 10:58:19 +0200 | [diff] [blame] | 26 | #include <import/ebpttree.h> |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 27 | |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 28 | #include <haproxy/api.h> |
| 29 | #include <haproxy/applet.h> |
Willy Tarreau | dfd3de8 | 2020-06-04 23:46:14 +0200 | [diff] [blame] | 30 | #include <haproxy/arg.h> |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 31 | #include <haproxy/auth.h> |
Willy Tarreau | 6be7849 | 2020-06-05 00:00:29 +0200 | [diff] [blame] | 32 | #include <haproxy/cfgparse.h> |
Willy Tarreau | f1d32c4 | 2020-06-04 21:07:02 +0200 | [diff] [blame] | 33 | #include <haproxy/channel.h> |
Willy Tarreau | 83487a8 | 2020-06-04 20:19:54 +0200 | [diff] [blame] | 34 | #include <haproxy/cli.h> |
Willy Tarreau | 7ea393d | 2020-06-04 18:02:10 +0200 | [diff] [blame] | 35 | #include <haproxy/connection.h> |
Willy Tarreau | 5413a87 | 2020-06-02 19:33:08 +0200 | [diff] [blame] | 36 | #include <haproxy/h1.h> |
Willy Tarreau | 8641605 | 2020-06-04 09:20:54 +0200 | [diff] [blame] | 37 | #include <haproxy/hlua.h> |
Willy Tarreau | 8c79400 | 2020-06-04 10:05:25 +0200 | [diff] [blame] | 38 | #include <haproxy/hlua_fcn.h> |
Willy Tarreau | c2b1ff0 | 2020-06-04 21:21:03 +0200 | [diff] [blame] | 39 | #include <haproxy/http_ana.h> |
Willy Tarreau | 126ba3a | 2020-06-04 18:26:43 +0200 | [diff] [blame] | 40 | #include <haproxy/http_fetch.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 41 | #include <haproxy/http_htx.h> |
Willy Tarreau | c761f84 | 2020-06-04 11:40:28 +0200 | [diff] [blame] | 42 | #include <haproxy/http_rules.h> |
Willy Tarreau | 36979d9 | 2020-06-05 17:27:29 +0200 | [diff] [blame] | 43 | #include <haproxy/log.h> |
Willy Tarreau | 2cd5809 | 2020-06-04 15:10:43 +0200 | [diff] [blame] | 44 | #include <haproxy/map.h> |
Willy Tarreau | 8efbdfb | 2020-06-04 11:29:21 +0200 | [diff] [blame] | 45 | #include <haproxy/obj_type.h> |
Willy Tarreau | 225a90a | 2020-06-04 15:06:28 +0200 | [diff] [blame] | 46 | #include <haproxy/pattern.h> |
Willy Tarreau | 469509b | 2020-06-04 15:13:30 +0200 | [diff] [blame] | 47 | #include <haproxy/payload.h> |
Willy Tarreau | 3d6ee40 | 2021-05-08 20:28:07 +0200 | [diff] [blame] | 48 | #include <haproxy/proxy.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 49 | #include <haproxy/regex.h> |
Willy Tarreau | e6ce10b | 2020-06-04 15:33:47 +0200 | [diff] [blame] | 50 | #include <haproxy/sample.h> |
Willy Tarreau | 198e92a | 2021-03-05 10:23:32 +0100 | [diff] [blame] | 51 | #include <haproxy/server.h> |
Willy Tarreau | 48d25b3 | 2020-06-04 18:58:52 +0200 | [diff] [blame] | 52 | #include <haproxy/session.h> |
Willy Tarreau | 2eec9b5 | 2020-06-04 19:58:55 +0200 | [diff] [blame] | 53 | #include <haproxy/stats-t.h> |
Willy Tarreau | dfd3de8 | 2020-06-04 23:46:14 +0200 | [diff] [blame] | 54 | #include <haproxy/stream.h> |
Willy Tarreau | 5e539c9 | 2020-06-04 20:45:39 +0200 | [diff] [blame] | 55 | #include <haproxy/stream_interface.h> |
Willy Tarreau | cea0e1b | 2020-06-04 17:25:40 +0200 | [diff] [blame] | 56 | #include <haproxy/task.h> |
Willy Tarreau | 8b550af | 2020-06-04 17:42:48 +0200 | [diff] [blame] | 57 | #include <haproxy/tcp_rules.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 58 | #include <haproxy/thread.h> |
Willy Tarreau | 48fbcae | 2020-06-03 18:09:46 +0200 | [diff] [blame] | 59 | #include <haproxy/tools.h> |
Willy Tarreau | a171892 | 2020-06-04 16:25:31 +0200 | [diff] [blame] | 60 | #include <haproxy/vars.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 61 | #include <haproxy/xref.h> |
| 62 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 63 | |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 64 | /* Lua uses longjmp to perform yield or throwing errors. This |
| 65 | * macro is used only for identifying the function that can |
| 66 | * not return because a longjmp is executed. |
| 67 | * __LJMP marks a prototype of hlua file that can use longjmp. |
| 68 | * WILL_LJMP() marks an lua function that will use longjmp. |
| 69 | * MAY_LJMP() marks an lua function that may use longjmp. |
| 70 | */ |
| 71 | #define __LJMP |
Willy Tarreau | 4e7cc33 | 2018-10-20 17:45:48 +0200 | [diff] [blame] | 72 | #define WILL_LJMP(func) do { func; my_unreachable(); } while(0) |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 73 | #define MAY_LJMP(func) func |
| 74 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 75 | /* This couple of function executes securely some Lua calls outside of |
| 76 | * the lua runtime environment. Each Lua call can return a longjmp |
| 77 | * if it encounter a memory error. |
| 78 | * |
| 79 | * Lua documentation extract: |
| 80 | * |
| 81 | * If an error happens outside any protected environment, Lua calls |
| 82 | * a panic function (see lua_atpanic) and then calls abort, thus |
| 83 | * exiting the host application. Your panic function can avoid this |
| 84 | * exit by never returning (e.g., doing a long jump to your own |
| 85 | * recovery point outside Lua). |
| 86 | * |
| 87 | * The panic function runs as if it were a message handler (see |
| 88 | * §2.3); in particular, the error message is at the top of the |
| 89 | * stack. However, there is no guarantee about stack space. To push |
| 90 | * anything on the stack, the panic function must first check the |
| 91 | * available space (see §4.2). |
| 92 | * |
| 93 | * We must check all the Lua entry point. This includes: |
| 94 | * - The include/proto/hlua.h exported functions |
| 95 | * - the task wrapper function |
| 96 | * - The action wrapper function |
| 97 | * - The converters wrapper function |
| 98 | * - The sample-fetch wrapper functions |
| 99 | * |
Ilya Shipitsin | 46a030c | 2020-07-05 16:36:08 +0500 | [diff] [blame] | 100 | * It is tolerated that the initialisation function returns an abort. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 101 | * Before each Lua abort, an error message is written on stderr. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 102 | * |
| 103 | * The macro SET_SAFE_LJMP initialise the longjmp. The Macro |
| 104 | * RESET_SAFE_LJMP reset the longjmp. These function must be macro |
| 105 | * because they must be exists in the program stack when the longjmp |
| 106 | * is called. |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 107 | * |
| 108 | * Note that the Lua processing is not really thread safe. It provides |
| 109 | * heavy system which consists to add our own lock function in the Lua |
| 110 | * code and recompile the library. This system will probably not accepted |
| 111 | * by maintainers of various distribs. |
| 112 | * |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 113 | * Our main execution point of the Lua is the function lua_resume(). A |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 114 | * quick looking on the Lua sources displays a lua_lock() a the start |
| 115 | * of function and a lua_unlock() at the end of the function. So I |
| 116 | * conclude that the Lua thread safe mode just perform a mutex around |
| 117 | * all execution. So I prefer to do this in the HAProxy code, it will be |
| 118 | * easier for distro maintainers. |
| 119 | * |
| 120 | * Note that the HAProxy lua functions rounded by the macro SET_SAFE_LJMP |
| 121 | * and RESET_SAFE_LJMP manipulates the Lua stack, so it will be careful |
| 122 | * to set mutex around these functions. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 123 | */ |
Willy Tarreau | 86abe44 | 2018-11-25 20:12:18 +0100 | [diff] [blame] | 124 | __decl_spinlock(hlua_global_lock); |
Thierry FOURNIER | ffbad79 | 2017-07-12 11:39:04 +0200 | [diff] [blame] | 125 | THREAD_LOCAL jmp_buf safe_ljmp_env; |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 126 | static int hlua_panic_safe(lua_State *L) { return 0; } |
Willy Tarreau | 69c66e3 | 2021-07-14 19:41:25 +0200 | [diff] [blame] | 127 | static int hlua_panic_ljmp(lua_State *L) { WILL_LJMP(longjmp(safe_ljmp_env, 1)); return 0; } |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 128 | |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 129 | /* This is the chained list of struct hlua_function referenced |
| 130 | * for haproxy action, sample-fetches, converters, cli and |
| 131 | * applet bindings. It is used for a post-initialisation control. |
| 132 | */ |
| 133 | static struct list referenced_functions = LIST_HEAD_INIT(referenced_functions); |
| 134 | |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 135 | /* This variable is used only during initialization to identify the Lua state |
| 136 | * currently being initialized. 0 is the common lua state, 1 to n are the Lua |
| 137 | * states dedicated to each thread (in this case hlua_state_id==tid+1). |
| 138 | */ |
| 139 | static int hlua_state_id; |
| 140 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 141 | /* This is a NULL-terminated list of lua file which are referenced to load per thread */ |
| 142 | static char **per_thread_load = NULL; |
| 143 | |
| 144 | lua_State *hlua_init_state(int thread_id); |
| 145 | |
Willy Tarreau | 3eb2e47 | 2021-08-20 15:47:25 +0200 | [diff] [blame] | 146 | /* This function takes the Lua global lock. Keep this function's visibility |
| 147 | * global so that it can appear in stack dumps and performance profiles! |
| 148 | */ |
Aurelien DARRAGON | e00bf5d | 2023-03-21 13:22:33 +0100 | [diff] [blame] | 149 | static inline void lua_take_global_lock() |
Willy Tarreau | 3eb2e47 | 2021-08-20 15:47:25 +0200 | [diff] [blame] | 150 | { |
| 151 | HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock); |
| 152 | } |
| 153 | |
| 154 | static inline void lua_drop_global_lock() |
| 155 | { |
| 156 | HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); |
| 157 | } |
| 158 | |
Aurelien DARRAGON | 5483bde | 2023-03-21 14:02:16 +0100 | [diff] [blame] | 159 | /* lua lock helpers: only lock when required |
| 160 | * |
| 161 | * state_id == 0: we're operating on the main lua stack (shared between |
| 162 | * os threads), so we need to acquire the main lock |
| 163 | * |
| 164 | * If the thread already owns the lock (_hlua_locked != 0), skip the lock |
| 165 | * attempt. This could happen if we run under protected lua environment. |
| 166 | * Not doing this could result in deadlocks because of nested locking |
| 167 | * attempts from the same thread |
| 168 | */ |
| 169 | static THREAD_LOCAL int _hlua_locked = 0; |
Aurelien DARRAGON | e00bf5d | 2023-03-21 13:22:33 +0100 | [diff] [blame] | 170 | static inline void hlua_lock(struct hlua *hlua) |
| 171 | { |
Aurelien DARRAGON | 5483bde | 2023-03-21 14:02:16 +0100 | [diff] [blame] | 172 | if (hlua->state_id != 0) |
| 173 | return; |
| 174 | if (!_hlua_locked) |
Aurelien DARRAGON | e00bf5d | 2023-03-21 13:22:33 +0100 | [diff] [blame] | 175 | lua_take_global_lock(); |
Aurelien DARRAGON | 5483bde | 2023-03-21 14:02:16 +0100 | [diff] [blame] | 176 | _hlua_locked += 1; |
Aurelien DARRAGON | e00bf5d | 2023-03-21 13:22:33 +0100 | [diff] [blame] | 177 | } |
| 178 | static inline void hlua_unlock(struct hlua *hlua) |
| 179 | { |
Aurelien DARRAGON | 5483bde | 2023-03-21 14:02:16 +0100 | [diff] [blame] | 180 | if (hlua->state_id != 0) |
| 181 | return; |
| 182 | BUG_ON(_hlua_locked <= 0); |
| 183 | _hlua_locked--; |
| 184 | /* drop the lock once the lock count reaches 0 */ |
| 185 | if (!_hlua_locked) |
Aurelien DARRAGON | e00bf5d | 2023-03-21 13:22:33 +0100 | [diff] [blame] | 186 | lua_drop_global_lock(); |
| 187 | } |
| 188 | |
Aurelien DARRAGON | a374161 | 2024-03-01 15:55:17 +0100 | [diff] [blame] | 189 | /* below is an helper function to retrieve string on on Lua stack at <index> |
| 190 | * in a safe way (function may not LJMP). It can be useful to retrieve errors |
| 191 | * at the top of the stack from an unprotected environment. |
| 192 | * |
| 193 | * The returned string will is only valid as long as the value at <index> is |
| 194 | * not removed from the stack. |
| 195 | * |
| 196 | * It is assumed that the calling function is allowed to manipulate <L> |
| 197 | */ |
| 198 | __LJMP static int _hlua_tostring_safe(lua_State *L) |
| 199 | { |
| 200 | const char **str = lua_touserdata(L, 1); |
| 201 | const char *cur_str = MAY_LJMP(lua_tostring(L, 2)); |
| 202 | |
| 203 | if (cur_str) |
| 204 | *str = cur_str; |
| 205 | return 0; |
| 206 | } |
| 207 | static const char *hlua_tostring_safe(lua_State *L, int index) |
| 208 | { |
| 209 | const char *str = NULL; |
| 210 | |
| 211 | if (!lua_checkstack(L, 4)) |
| 212 | return NULL; |
| 213 | |
| 214 | /* before any stack modification, save the targeted value on the top of |
| 215 | * the stack: this will allow us to use relative index to target it. |
| 216 | */ |
| 217 | lua_pushvalue(L, index); |
| 218 | |
| 219 | /* push our custom _hlua_tostring_safe() function on the stack, then push |
| 220 | * our own string pointer and targeted value (at <index>) as argument |
| 221 | */ |
| 222 | lua_pushcfunction(L, _hlua_tostring_safe); |
| 223 | lua_pushlightuserdata(L, &str); // 1st func argument = string pointer |
| 224 | lua_pushvalue(L, -3); // 2nd func argument = targeted value |
| 225 | |
| 226 | lua_remove(L, -4); // remove <index> copy as we're done using it |
| 227 | |
| 228 | /* call our custom function with proper arguments using pcall() to catch |
| 229 | * exceptions (if any) |
| 230 | */ |
| 231 | switch (lua_pcall(L, 2, 0, 0)) { |
| 232 | case LUA_OK: |
| 233 | break; |
| 234 | default: |
| 235 | /* error was caught */ |
| 236 | return NULL; |
| 237 | } |
| 238 | return str; |
| 239 | } |
| 240 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 241 | #define SET_SAFE_LJMP_L(__L, __HLUA) \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 242 | ({ \ |
| 243 | int ret; \ |
Aurelien DARRAGON | e00bf5d | 2023-03-21 13:22:33 +0100 | [diff] [blame] | 244 | hlua_lock(__HLUA); \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 245 | if (setjmp(safe_ljmp_env) != 0) { \ |
| 246 | lua_atpanic(__L, hlua_panic_safe); \ |
| 247 | ret = 0; \ |
Aurelien DARRAGON | e00bf5d | 2023-03-21 13:22:33 +0100 | [diff] [blame] | 248 | hlua_unlock(__HLUA); \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 249 | } else { \ |
| 250 | lua_atpanic(__L, hlua_panic_ljmp); \ |
| 251 | ret = 1; \ |
| 252 | } \ |
| 253 | ret; \ |
| 254 | }) |
| 255 | |
| 256 | /* If we are the last function catching Lua errors, we |
| 257 | * must reset the panic function. |
| 258 | */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 259 | #define RESET_SAFE_LJMP_L(__L, __HLUA) \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 260 | do { \ |
| 261 | lua_atpanic(__L, hlua_panic_safe); \ |
Aurelien DARRAGON | e00bf5d | 2023-03-21 13:22:33 +0100 | [diff] [blame] | 262 | hlua_unlock(__HLUA); \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 263 | } while(0) |
| 264 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 265 | #define SET_SAFE_LJMP(__HLUA) \ |
| 266 | SET_SAFE_LJMP_L((__HLUA)->T, __HLUA) |
| 267 | |
| 268 | #define RESET_SAFE_LJMP(__HLUA) \ |
| 269 | RESET_SAFE_LJMP_L((__HLUA)->T, __HLUA) |
| 270 | |
| 271 | #define SET_SAFE_LJMP_PARENT(__HLUA) \ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 272 | SET_SAFE_LJMP_L(hlua_states[(__HLUA)->state_id], __HLUA) |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 273 | |
| 274 | #define RESET_SAFE_LJMP_PARENT(__HLUA) \ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 275 | RESET_SAFE_LJMP_L(hlua_states[(__HLUA)->state_id], __HLUA) |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 276 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 277 | /* Applet status flags */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 278 | #define APPLET_DONE 0x01 /* applet processing is done. */ |
Christopher Faulet | 18c2e8d | 2019-03-01 12:02:08 +0100 | [diff] [blame] | 279 | /* unused: 0x02 */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 280 | #define APPLET_HDR_SENT 0x04 /* Response header sent. */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 281 | /* unused: 0x08, 0x10 */ |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 282 | #define APPLET_HTTP11 0x20 /* Last chunk sent. */ |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 283 | #define APPLET_RSP_SENT 0x40 /* The response was fully sent */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 284 | |
Thierry Fournier | afc63e2 | 2020-11-28 17:06:51 +0100 | [diff] [blame] | 285 | /* The main Lua execution context. The 0 index is the |
| 286 | * common state shared by all threads. |
| 287 | */ |
Willy Tarreau | 186f376 | 2020-12-04 11:48:12 +0100 | [diff] [blame] | 288 | static lua_State *hlua_states[MAX_THREADS + 1]; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 289 | |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 290 | /* This is the memory pool containing struct lua for applets |
| 291 | * (including cli). |
| 292 | */ |
Willy Tarreau | 8ceae72 | 2018-11-26 11:58:30 +0100 | [diff] [blame] | 293 | DECLARE_STATIC_POOL(pool_head_hlua, "hlua", sizeof(struct hlua)); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 294 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 295 | /* Used for Socket connection. */ |
Amaury Denoyelle | 239fdbf | 2021-03-24 10:22:03 +0100 | [diff] [blame] | 296 | static struct proxy *socket_proxy; |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 297 | static struct server *socket_tcp; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 298 | #ifdef USE_OPENSSL |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 299 | static struct server *socket_ssl; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 300 | #endif |
| 301 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 302 | /* List head of the function called at the initialisation time. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 303 | struct list hlua_init_functions[MAX_THREADS + 1]; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 304 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 305 | /* The following variables contains the reference of the different |
| 306 | * Lua classes. These references are useful for identify metadata |
| 307 | * associated with an object. |
| 308 | */ |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 309 | static int class_txn_ref; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 310 | static int class_socket_ref; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 311 | static int class_channel_ref; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 312 | static int class_fetches_ref; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 313 | static int class_converters_ref; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 314 | static int class_http_ref; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 315 | static int class_map_ref; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 316 | static int class_applet_tcp_ref; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 317 | static int class_applet_http_ref; |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 318 | static int class_txn_reply_ref; |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 319 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 320 | /* Global Lua execution timeout. By default Lua, execution linked |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 321 | * with stream (actions, sample-fetches and converters) have a |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 322 | * short timeout. Lua linked with tasks doesn't have a timeout |
| 323 | * because a task may remain alive during all the haproxy execution. |
| 324 | */ |
| 325 | static unsigned int hlua_timeout_session = 4000; /* session timeout. */ |
| 326 | static unsigned int hlua_timeout_task = TICK_ETERNITY; /* task timeout. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 327 | static unsigned int hlua_timeout_applet = 4000; /* applet timeout. */ |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 328 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 329 | /* Interrupts the Lua processing each "hlua_nb_instruction" instructions. |
| 330 | * it is used for preventing infinite loops. |
| 331 | * |
| 332 | * I test the scheer with an infinite loop containing one incrementation |
| 333 | * and one test. I run this loop between 10 seconds, I raise a ceil of |
| 334 | * 710M loops from one interrupt each 9000 instructions, so I fix the value |
| 335 | * to one interrupt each 10 000 instructions. |
| 336 | * |
| 337 | * configured | Number of |
| 338 | * instructions | loops executed |
| 339 | * between two | in milions |
| 340 | * forced yields | |
| 341 | * ---------------+--------------- |
| 342 | * 10 | 160 |
| 343 | * 500 | 670 |
| 344 | * 1000 | 680 |
| 345 | * 5000 | 700 |
| 346 | * 7000 | 700 |
| 347 | * 8000 | 700 |
| 348 | * 9000 | 710 <- ceil |
| 349 | * 10000 | 710 |
| 350 | * 100000 | 710 |
| 351 | * 1000000 | 710 |
| 352 | * |
| 353 | */ |
| 354 | static unsigned int hlua_nb_instruction = 10000; |
| 355 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 356 | /* Descriptor for the memory allocation state. The limit is pre-initialised to |
| 357 | * 0 until it is replaced by "tune.lua.maxmem" during the config parsing, or it |
| 358 | * is replaced with ~0 during post_init after everything was loaded. This way |
| 359 | * it is guaranteed that if limit is ~0 the boot is complete and that if it's |
| 360 | * zero it's not yet limited and proper accounting is required. |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 361 | */ |
| 362 | struct hlua_mem_allocator { |
| 363 | size_t allocated; |
| 364 | size_t limit; |
| 365 | }; |
| 366 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 367 | static struct hlua_mem_allocator hlua_global_allocator THREAD_ALIGNED(64); |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 368 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 369 | /* These functions converts types between HAProxy internal args or |
| 370 | * sample and LUA types. Another function permits to check if the |
| 371 | * LUA stack contains arguments according with an required ARG_T |
| 372 | * format. |
| 373 | */ |
| 374 | static int hlua_arg2lua(lua_State *L, const struct arg *arg); |
| 375 | static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 376 | __LJMP static int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp, |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 377 | uint64_t mask, struct proxy *p); |
Willy Tarreau | 5eadada | 2015-03-10 17:28:54 +0100 | [diff] [blame] | 378 | static int hlua_smp2lua(lua_State *L, struct sample *smp); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 379 | static int hlua_smp2lua_str(lua_State *L, struct sample *smp); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 380 | static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp); |
| 381 | |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 382 | __LJMP static int hlua_http_get_headers(lua_State *L, struct http_msg *msg); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 383 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 384 | struct prepend_path { |
| 385 | struct list l; |
| 386 | char *type; |
| 387 | char *path; |
| 388 | }; |
| 389 | |
| 390 | static struct list prepend_path_list = LIST_HEAD_INIT(prepend_path_list); |
| 391 | |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 392 | #define SEND_ERR(__be, __fmt, __args...) \ |
| 393 | do { \ |
| 394 | send_log(__be, LOG_ERR, __fmt, ## __args); \ |
| 395 | if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) \ |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 396 | ha_alert(__fmt, ## __args); \ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 397 | } while (0) |
| 398 | |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 399 | static inline struct hlua_function *new_hlua_function() |
| 400 | { |
| 401 | struct hlua_function *fcn; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 402 | int i; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 403 | |
| 404 | fcn = calloc(1, sizeof(*fcn)); |
| 405 | if (!fcn) |
| 406 | return NULL; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 407 | LIST_APPEND(&referenced_functions, &fcn->l); |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 408 | for (i = 0; i < MAX_THREADS + 1; i++) |
| 409 | fcn->function_ref[i] = -1; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 410 | return fcn; |
| 411 | } |
| 412 | |
Christopher Faulet | dda4444 | 2021-04-12 14:05:43 +0200 | [diff] [blame] | 413 | static inline void release_hlua_function(struct hlua_function *fcn) |
| 414 | { |
| 415 | if (!fcn) |
| 416 | return; |
| 417 | if (fcn->name) |
| 418 | ha_free(&fcn->name); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 419 | LIST_DELETE(&fcn->l); |
Christopher Faulet | dda4444 | 2021-04-12 14:05:43 +0200 | [diff] [blame] | 420 | ha_free(&fcn); |
| 421 | } |
| 422 | |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 423 | /* If the common state is set, the stack id is 0, otherwise it is the tid + 1 */ |
| 424 | static inline int fcn_ref_to_stack_id(struct hlua_function *fcn) |
| 425 | { |
| 426 | if (fcn->function_ref[0] == -1) |
| 427 | return tid + 1; |
| 428 | return 0; |
| 429 | } |
| 430 | |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 431 | /* Used to check an Lua function type in the stack. It creates and |
| 432 | * returns a reference of the function. This function throws an |
Aurelien DARRAGON | 5ce1152 | 2023-03-02 17:50:49 +0100 | [diff] [blame] | 433 | * error if the argument is not a "function". |
| 434 | * When no longer used, the ref must be released with hlua_unref() |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 435 | */ |
| 436 | __LJMP unsigned int hlua_checkfunction(lua_State *L, int argno) |
| 437 | { |
| 438 | if (!lua_isfunction(L, argno)) { |
Thierry FOURNIER | fd1e955 | 2018-02-23 18:41:18 +0100 | [diff] [blame] | 439 | const char *msg = lua_pushfstring(L, "function expected, got %s", luaL_typename(L, argno)); |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 440 | WILL_LJMP(luaL_argerror(L, argno, msg)); |
| 441 | } |
| 442 | lua_pushvalue(L, argno); |
| 443 | return luaL_ref(L, LUA_REGISTRYINDEX); |
| 444 | } |
| 445 | |
Christopher Faulet | ddc9032 | 2020-02-25 10:20:04 +0100 | [diff] [blame] | 446 | /* Used to check an Lua table type in the stack. It creates and |
| 447 | * returns a reference of the table. This function throws an |
Aurelien DARRAGON | 5ce1152 | 2023-03-02 17:50:49 +0100 | [diff] [blame] | 448 | * error if the argument is not a "table". |
| 449 | * When no longer used, the ref must be released with hlua_unref() |
Christopher Faulet | ddc9032 | 2020-02-25 10:20:04 +0100 | [diff] [blame] | 450 | */ |
| 451 | __LJMP unsigned int hlua_checktable(lua_State *L, int argno) |
| 452 | { |
| 453 | if (!lua_istable(L, argno)) { |
| 454 | const char *msg = lua_pushfstring(L, "table expected, got %s", luaL_typename(L, argno)); |
| 455 | WILL_LJMP(luaL_argerror(L, argno, msg)); |
| 456 | } |
| 457 | lua_pushvalue(L, argno); |
| 458 | return luaL_ref(L, LUA_REGISTRYINDEX); |
| 459 | } |
| 460 | |
Aurelien DARRAGON | 5ce1152 | 2023-03-02 17:50:49 +0100 | [diff] [blame] | 461 | /* Get a reference to the object that is at the top of the stack |
| 462 | * The referenced object will be popped from the stack |
| 463 | * |
| 464 | * The function returns the reference to the object which must |
| 465 | * be cleared using hlua_unref() when no longer used |
| 466 | */ |
| 467 | __LJMP int hlua_ref(lua_State *L) |
| 468 | { |
| 469 | return MAY_LJMP(luaL_ref(L, LUA_REGISTRYINDEX)); |
| 470 | } |
| 471 | |
| 472 | /* Pushes a reference previously created using luaL_ref(L, LUA_REGISTRYINDEX) |
| 473 | * on <L> stack |
| 474 | * (ie: hlua_checkfunction(), hlua_checktable() or hlua_ref()) |
| 475 | * |
| 476 | * When the reference is no longer used, it should be released by calling |
| 477 | * hlua_unref() |
| 478 | * |
| 479 | * <L> can be from any co-routine as long as it belongs to the same lua |
| 480 | * parent state that the one used to get the reference. |
| 481 | */ |
| 482 | void hlua_pushref(lua_State *L, int ref) |
| 483 | { |
| 484 | lua_rawgeti(L, LUA_REGISTRYINDEX, ref); |
| 485 | } |
| 486 | |
| 487 | /* Releases a reference previously created using luaL_ref(L, LUA_REGISTRYINDEX) |
| 488 | * (ie: hlua_checkfunction(), hlua_checktable() or hlua_ref()) |
| 489 | * |
| 490 | * This will allow the reference to be reused and the referred object |
| 491 | * to be garbage collected. |
| 492 | * |
| 493 | * <L> can be from any co-routine as long as it belongs to the same lua |
| 494 | * parent state that the one used to get the reference. |
| 495 | */ |
| 496 | void hlua_unref(lua_State *L, int ref) |
| 497 | { |
| 498 | luaL_unref(L, LUA_REGISTRYINDEX, ref); |
| 499 | } |
| 500 | |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 501 | __LJMP const char *hlua_traceback(lua_State *L, const char* sep) |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 502 | { |
| 503 | lua_Debug ar; |
| 504 | int level = 0; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 505 | struct buffer *msg = get_trash_chunk(); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 506 | |
| 507 | while (lua_getstack(L, level++, &ar)) { |
| 508 | |
| 509 | /* Add separator */ |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 510 | if (b_data(msg)) |
| 511 | chunk_appendf(msg, "%s", sep); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 512 | |
| 513 | /* Fill fields: |
| 514 | * 'S': fills in the fields source, short_src, linedefined, lastlinedefined, and what; |
| 515 | * 'l': fills in the field currentline; |
| 516 | * 'n': fills in the field name and namewhat; |
| 517 | * 't': fills in the field istailcall; |
| 518 | */ |
| 519 | lua_getinfo(L, "Slnt", &ar); |
| 520 | |
| 521 | /* Append code localisation */ |
| 522 | if (ar.currentline > 0) |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 523 | chunk_appendf(msg, "%s:%d: ", ar.short_src, ar.currentline); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 524 | else |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 525 | chunk_appendf(msg, "%s: ", ar.short_src); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 526 | |
| 527 | /* |
| 528 | * Get function name |
| 529 | * |
| 530 | * if namewhat is no empty, name is defined. |
| 531 | * what contains "Lua" for Lua function, "C" for C function, |
| 532 | * or "main" for main code. |
| 533 | */ |
| 534 | if (*ar.namewhat != '\0' && ar.name != NULL) /* is there a name from code? */ |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 535 | chunk_appendf(msg, "in %s '%s'", ar.namewhat, ar.name); /* use it */ |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 536 | |
| 537 | else if (*ar.what == 'm') /* "main", the code is not executed in a function */ |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 538 | chunk_appendf(msg, "in main chunk"); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 539 | |
| 540 | else if (*ar.what != 'C') /* for Lua functions, use <file:line> */ |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 541 | chunk_appendf(msg, "in function line %d", ar.linedefined); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 542 | |
| 543 | else /* nothing left... */ |
| 544 | chunk_appendf(msg, "?"); |
| 545 | |
| 546 | |
| 547 | /* Display tailed call */ |
| 548 | if (ar.istailcall) |
| 549 | chunk_appendf(msg, " ..."); |
| 550 | } |
| 551 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 552 | return msg->area; |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 553 | } |
| 554 | |
| 555 | |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 556 | /* This function check the number of arguments available in the |
| 557 | * stack. If the number of arguments available is not the same |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 558 | * then <nb> an error is thrown. |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 559 | */ |
| 560 | __LJMP static inline void check_args(lua_State *L, int nb, char *fcn) |
| 561 | { |
| 562 | if (lua_gettop(L) == nb) |
| 563 | return; |
| 564 | WILL_LJMP(luaL_error(L, "'%s' needs %d arguments", fcn, nb)); |
| 565 | } |
| 566 | |
Mark Lakes | 22154b4 | 2018-01-29 14:38:40 -0800 | [diff] [blame] | 567 | /* This function pushes an error string prefixed by the file name |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 568 | * and the line number where the error is encountered. |
| 569 | */ |
| 570 | static int hlua_pusherror(lua_State *L, const char *fmt, ...) |
| 571 | { |
| 572 | va_list argp; |
| 573 | va_start(argp, fmt); |
| 574 | luaL_where(L, 1); |
| 575 | lua_pushvfstring(L, fmt, argp); |
| 576 | va_end(argp); |
| 577 | lua_concat(L, 2); |
| 578 | return 1; |
| 579 | } |
| 580 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 581 | /* This functions is used with sample fetch and converters. It |
| 582 | * converts the HAProxy configuration argument in a lua stack |
| 583 | * values. |
| 584 | * |
| 585 | * It takes an array of "arg", and each entry of the array is |
| 586 | * converted and pushed in the LUA stack. |
| 587 | */ |
| 588 | static int hlua_arg2lua(lua_State *L, const struct arg *arg) |
| 589 | { |
| 590 | switch (arg->type) { |
| 591 | case ARGT_SINT: |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 592 | case ARGT_TIME: |
| 593 | case ARGT_SIZE: |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 594 | lua_pushinteger(L, arg->data.sint); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 595 | break; |
| 596 | |
| 597 | case ARGT_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 598 | lua_pushlstring(L, arg->data.str.area, arg->data.str.data); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 599 | break; |
| 600 | |
| 601 | case ARGT_IPV4: |
| 602 | case ARGT_IPV6: |
| 603 | case ARGT_MSK4: |
| 604 | case ARGT_MSK6: |
| 605 | case ARGT_FE: |
| 606 | case ARGT_BE: |
| 607 | case ARGT_TAB: |
| 608 | case ARGT_SRV: |
| 609 | case ARGT_USR: |
| 610 | case ARGT_MAP: |
| 611 | default: |
| 612 | lua_pushnil(L); |
| 613 | break; |
| 614 | } |
| 615 | return 1; |
| 616 | } |
| 617 | |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 618 | /* This function take one entry in an LUA stack at the index "ud", |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 619 | * and try to convert it in an HAProxy argument entry. This is useful |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 620 | * with sample fetch wrappers. The input arguments are given to the |
| 621 | * lua wrapper and converted as arg list by the function. |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 622 | */ |
| 623 | static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg) |
| 624 | { |
| 625 | switch (lua_type(L, ud)) { |
| 626 | |
| 627 | case LUA_TNUMBER: |
| 628 | case LUA_TBOOLEAN: |
| 629 | arg->type = ARGT_SINT; |
| 630 | arg->data.sint = lua_tointeger(L, ud); |
| 631 | break; |
| 632 | |
| 633 | case LUA_TSTRING: |
| 634 | arg->type = ARGT_STR; |
Tim Duesterhus | 2e89dec | 2019-09-29 23:03:08 +0200 | [diff] [blame] | 635 | arg->data.str.area = (char *)lua_tolstring(L, ud, &arg->data.str.data); |
Tim Duesterhus | 29d2e8a | 2019-09-29 23:03:07 +0200 | [diff] [blame] | 636 | /* We don't know the actual size of the underlying allocation, so be conservative. */ |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 637 | arg->data.str.size = arg->data.str.data+1; /* count the terminating null byte */ |
Tim Duesterhus | 29d2e8a | 2019-09-29 23:03:07 +0200 | [diff] [blame] | 638 | arg->data.str.head = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 639 | break; |
| 640 | |
| 641 | case LUA_TUSERDATA: |
| 642 | case LUA_TNIL: |
| 643 | case LUA_TTABLE: |
| 644 | case LUA_TFUNCTION: |
| 645 | case LUA_TTHREAD: |
| 646 | case LUA_TLIGHTUSERDATA: |
| 647 | arg->type = ARGT_SINT; |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 648 | arg->data.sint = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 649 | break; |
| 650 | } |
| 651 | return 1; |
| 652 | } |
| 653 | |
| 654 | /* the following functions are used to convert a struct sample |
| 655 | * in Lua type. This useful to convert the return of the |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 656 | * fetches or converters. |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 657 | */ |
Willy Tarreau | 5eadada | 2015-03-10 17:28:54 +0100 | [diff] [blame] | 658 | static int hlua_smp2lua(lua_State *L, struct sample *smp) |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 659 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 660 | switch (smp->data.type) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 661 | case SMP_T_SINT: |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 662 | case SMP_T_BOOL: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 663 | lua_pushinteger(L, smp->data.u.sint); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 664 | break; |
| 665 | |
| 666 | case SMP_T_BIN: |
| 667 | case SMP_T_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 668 | lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 669 | break; |
| 670 | |
| 671 | case SMP_T_METH: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 672 | switch (smp->data.u.meth.meth) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 673 | case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break; |
| 674 | case HTTP_METH_GET: lua_pushstring(L, "GET"); break; |
| 675 | case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break; |
| 676 | case HTTP_METH_POST: lua_pushstring(L, "POST"); break; |
| 677 | case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break; |
| 678 | case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break; |
| 679 | case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break; |
| 680 | case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break; |
| 681 | case HTTP_METH_OTHER: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 682 | lua_pushlstring(L, smp->data.u.meth.str.area, smp->data.u.meth.str.data); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 683 | break; |
| 684 | default: |
| 685 | lua_pushnil(L); |
| 686 | break; |
| 687 | } |
| 688 | break; |
| 689 | |
| 690 | case SMP_T_IPV4: |
| 691 | case SMP_T_IPV6: |
| 692 | case SMP_T_ADDR: /* This type is never used to qualify a sample. */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 693 | if (sample_casts[smp->data.type][SMP_T_STR] && |
| 694 | sample_casts[smp->data.type][SMP_T_STR](smp)) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 695 | lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data); |
Willy Tarreau | 5eadada | 2015-03-10 17:28:54 +0100 | [diff] [blame] | 696 | else |
| 697 | lua_pushnil(L); |
| 698 | break; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 699 | default: |
| 700 | lua_pushnil(L); |
| 701 | break; |
| 702 | } |
| 703 | return 1; |
| 704 | } |
| 705 | |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 706 | /* the following functions are used to convert a struct sample |
| 707 | * in Lua strings. This is useful to convert the return of the |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 708 | * fetches or converters. |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 709 | */ |
| 710 | static int hlua_smp2lua_str(lua_State *L, struct sample *smp) |
| 711 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 712 | switch (smp->data.type) { |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 713 | |
| 714 | case SMP_T_BIN: |
| 715 | case SMP_T_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 716 | lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 717 | break; |
| 718 | |
| 719 | case SMP_T_METH: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 720 | switch (smp->data.u.meth.meth) { |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 721 | case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break; |
| 722 | case HTTP_METH_GET: lua_pushstring(L, "GET"); break; |
| 723 | case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break; |
| 724 | case HTTP_METH_POST: lua_pushstring(L, "POST"); break; |
| 725 | case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break; |
| 726 | case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break; |
| 727 | case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break; |
| 728 | case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break; |
| 729 | case HTTP_METH_OTHER: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 730 | lua_pushlstring(L, smp->data.u.meth.str.area, smp->data.u.meth.str.data); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 731 | break; |
| 732 | default: |
| 733 | lua_pushstring(L, ""); |
| 734 | break; |
| 735 | } |
| 736 | break; |
| 737 | |
| 738 | case SMP_T_SINT: |
| 739 | case SMP_T_BOOL: |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 740 | case SMP_T_IPV4: |
| 741 | case SMP_T_IPV6: |
| 742 | case SMP_T_ADDR: /* This type is never used to qualify a sample. */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 743 | if (sample_casts[smp->data.type][SMP_T_STR] && |
| 744 | sample_casts[smp->data.type][SMP_T_STR](smp)) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 745 | lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 746 | else |
| 747 | lua_pushstring(L, ""); |
| 748 | break; |
| 749 | default: |
| 750 | lua_pushstring(L, ""); |
| 751 | break; |
| 752 | } |
| 753 | return 1; |
| 754 | } |
| 755 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 756 | /* the following functions are used to convert an Lua type in a |
| 757 | * struct sample. This is useful to provide data from a converter |
| 758 | * to the LUA code. |
| 759 | */ |
| 760 | static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp) |
| 761 | { |
| 762 | switch (lua_type(L, ud)) { |
| 763 | |
| 764 | case LUA_TNUMBER: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 765 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 766 | smp->data.u.sint = lua_tointeger(L, ud); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 767 | break; |
| 768 | |
| 769 | |
| 770 | case LUA_TBOOLEAN: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 771 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 772 | smp->data.u.sint = lua_toboolean(L, ud); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 773 | break; |
| 774 | |
| 775 | case LUA_TSTRING: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 776 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 777 | smp->flags |= SMP_F_CONST; |
Tim Duesterhus | 2e89dec | 2019-09-29 23:03:08 +0200 | [diff] [blame] | 778 | smp->data.u.str.area = (char *)lua_tolstring(L, ud, &smp->data.u.str.data); |
Tim Duesterhus | 29d2e8a | 2019-09-29 23:03:07 +0200 | [diff] [blame] | 779 | /* We don't know the actual size of the underlying allocation, so be conservative. */ |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 780 | smp->data.u.str.size = smp->data.u.str.data+1; /* count the terminating null byte */ |
Tim Duesterhus | 29d2e8a | 2019-09-29 23:03:07 +0200 | [diff] [blame] | 781 | smp->data.u.str.head = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 782 | break; |
| 783 | |
| 784 | case LUA_TUSERDATA: |
| 785 | case LUA_TNIL: |
| 786 | case LUA_TTABLE: |
| 787 | case LUA_TFUNCTION: |
| 788 | case LUA_TTHREAD: |
| 789 | case LUA_TLIGHTUSERDATA: |
Thierry FOURNIER | 93405e1 | 2015-08-26 14:19:03 +0200 | [diff] [blame] | 790 | case LUA_TNONE: |
| 791 | default: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 792 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 793 | smp->data.u.sint = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 794 | break; |
| 795 | } |
| 796 | return 1; |
| 797 | } |
| 798 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 799 | /* This function check the "argp" built by another conversion function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 800 | * is in accord with the expected argp defined by the "mask". The function |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 801 | * returns true or false. It can be adjust the types if there compatibles. |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 802 | * |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 803 | * This function assumes that the argp argument contains ARGM_NBARGS + 1 |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 804 | * entries. |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 805 | */ |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 806 | __LJMP int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp, |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 807 | uint64_t mask, struct proxy *p) |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 808 | { |
| 809 | int min_arg; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 810 | int i, idx; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 811 | struct proxy *px; |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 812 | struct userlist *ul; |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 813 | struct my_regex *reg; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 814 | const char *msg = NULL; |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 815 | char *sname, *pname, *err = NULL; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 816 | |
| 817 | idx = 0; |
| 818 | min_arg = ARGM(mask); |
| 819 | mask >>= ARGM_BITS; |
| 820 | |
| 821 | while (1) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 822 | struct buffer tmp = BUF_NULL; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 823 | |
| 824 | /* Check oversize. */ |
| 825 | if (idx >= ARGM_NBARGS && argp[idx].type != ARGT_STOP) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 826 | msg = "Malformed argument mask"; |
| 827 | goto error; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 828 | } |
| 829 | |
| 830 | /* Check for mandatory arguments. */ |
| 831 | if (argp[idx].type == ARGT_STOP) { |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 832 | if (idx < min_arg) { |
| 833 | |
| 834 | /* If miss other argument than the first one, we return an error. */ |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 835 | if (idx > 0) { |
| 836 | msg = "Mandatory argument expected"; |
| 837 | goto error; |
| 838 | } |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 839 | |
| 840 | /* If first argument have a certain type, some default values |
| 841 | * may be used. See the function smp_resolve_args(). |
| 842 | */ |
| 843 | switch (mask & ARGT_MASK) { |
| 844 | |
| 845 | case ARGT_FE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 846 | if (!(p->cap & PR_CAP_FE)) { |
| 847 | msg = "Mandatory argument expected"; |
| 848 | goto error; |
| 849 | } |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 850 | argp[idx].data.prx = p; |
| 851 | argp[idx].type = ARGT_FE; |
| 852 | argp[idx+1].type = ARGT_STOP; |
| 853 | break; |
| 854 | |
| 855 | case ARGT_BE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 856 | if (!(p->cap & PR_CAP_BE)) { |
| 857 | msg = "Mandatory argument expected"; |
| 858 | goto error; |
| 859 | } |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 860 | argp[idx].data.prx = p; |
| 861 | argp[idx].type = ARGT_BE; |
| 862 | argp[idx+1].type = ARGT_STOP; |
| 863 | break; |
| 864 | |
| 865 | case ARGT_TAB: |
Olivier Houchard | a265827 | 2022-09-13 00:35:53 +0200 | [diff] [blame] | 866 | if (!p->table) { |
| 867 | msg = "Mandatory argument expected"; |
| 868 | goto error; |
| 869 | } |
| 870 | argp[idx].data.t = p->table; |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 871 | argp[idx].type = ARGT_TAB; |
| 872 | argp[idx+1].type = ARGT_STOP; |
| 873 | break; |
| 874 | |
| 875 | default: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 876 | msg = "Mandatory argument expected"; |
| 877 | goto error; |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 878 | break; |
| 879 | } |
| 880 | } |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 881 | break; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 882 | } |
| 883 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 884 | /* Check for exceed the number of required argument. */ |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 885 | if ((mask & ARGT_MASK) == ARGT_STOP && |
| 886 | argp[idx].type != ARGT_STOP) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 887 | msg = "Last argument expected"; |
| 888 | goto error; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 889 | } |
| 890 | |
| 891 | if ((mask & ARGT_MASK) == ARGT_STOP && |
| 892 | argp[idx].type == ARGT_STOP) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 893 | break; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 894 | } |
| 895 | |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 896 | /* Convert some argument types. All string in argp[] are for not |
| 897 | * duplicated yet. |
| 898 | */ |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 899 | switch (mask & ARGT_MASK) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 900 | case ARGT_SINT: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 901 | if (argp[idx].type != ARGT_SINT) { |
| 902 | msg = "integer expected"; |
| 903 | goto error; |
| 904 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 905 | argp[idx].type = ARGT_SINT; |
| 906 | break; |
| 907 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 908 | case ARGT_TIME: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 909 | if (argp[idx].type != ARGT_SINT) { |
| 910 | msg = "integer expected"; |
| 911 | goto error; |
| 912 | } |
Thierry FOURNIER | 29176f3 | 2015-07-07 00:41:29 +0200 | [diff] [blame] | 913 | argp[idx].type = ARGT_TIME; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 914 | break; |
| 915 | |
| 916 | case ARGT_SIZE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 917 | if (argp[idx].type != ARGT_SINT) { |
| 918 | msg = "integer expected"; |
| 919 | goto error; |
| 920 | } |
Thierry FOURNIER | 29176f3 | 2015-07-07 00:41:29 +0200 | [diff] [blame] | 921 | argp[idx].type = ARGT_SIZE; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 922 | break; |
| 923 | |
| 924 | case ARGT_FE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 925 | if (argp[idx].type != ARGT_STR) { |
| 926 | msg = "string expected"; |
| 927 | goto error; |
| 928 | } |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 929 | argp[idx].data.prx = proxy_fe_by_name(argp[idx].data.str.area); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 930 | if (!argp[idx].data.prx) { |
| 931 | msg = "frontend doesn't exist"; |
| 932 | goto error; |
| 933 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 934 | argp[idx].type = ARGT_FE; |
| 935 | break; |
| 936 | |
| 937 | case ARGT_BE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 938 | if (argp[idx].type != ARGT_STR) { |
| 939 | msg = "string expected"; |
| 940 | goto error; |
| 941 | } |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 942 | argp[idx].data.prx = proxy_be_by_name(argp[idx].data.str.area); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 943 | if (!argp[idx].data.prx) { |
| 944 | msg = "backend doesn't exist"; |
| 945 | goto error; |
| 946 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 947 | argp[idx].type = ARGT_BE; |
| 948 | break; |
| 949 | |
| 950 | case ARGT_TAB: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 951 | if (argp[idx].type != ARGT_STR) { |
| 952 | msg = "string expected"; |
| 953 | goto error; |
| 954 | } |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 955 | argp[idx].data.t = stktable_find_by_name(argp[idx].data.str.area); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 956 | if (!argp[idx].data.t) { |
| 957 | msg = "table doesn't exist"; |
| 958 | goto error; |
| 959 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 960 | argp[idx].type = ARGT_TAB; |
| 961 | break; |
| 962 | |
| 963 | case ARGT_SRV: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 964 | if (argp[idx].type != ARGT_STR) { |
| 965 | msg = "string expected"; |
| 966 | goto error; |
| 967 | } |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 968 | sname = strrchr(argp[idx].data.str.area, '/'); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 969 | if (sname) { |
| 970 | *sname++ = '\0'; |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 971 | pname = argp[idx].data.str.area; |
Willy Tarreau | 9e0bb10 | 2015-05-26 11:24:42 +0200 | [diff] [blame] | 972 | px = proxy_be_by_name(pname); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 973 | if (!px) { |
| 974 | msg = "backend doesn't exist"; |
| 975 | goto error; |
| 976 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 977 | } |
| 978 | else { |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 979 | sname = argp[idx].data.str.area; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 980 | px = p; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 981 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 982 | argp[idx].data.srv = findserver(px, sname); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 983 | if (!argp[idx].data.srv) { |
| 984 | msg = "server doesn't exist"; |
| 985 | goto error; |
| 986 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 987 | argp[idx].type = ARGT_SRV; |
| 988 | break; |
| 989 | |
| 990 | case ARGT_IPV4: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 991 | if (argp[idx].type != ARGT_STR) { |
| 992 | msg = "string expected"; |
| 993 | goto error; |
| 994 | } |
| 995 | if (inet_pton(AF_INET, argp[idx].data.str.area, &argp[idx].data.ipv4)) { |
| 996 | msg = "invalid IPv4 address"; |
| 997 | goto error; |
| 998 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 999 | argp[idx].type = ARGT_IPV4; |
| 1000 | break; |
| 1001 | |
| 1002 | case ARGT_MSK4: |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 1003 | if (argp[idx].type == ARGT_SINT) |
| 1004 | len2mask4(argp[idx].data.sint, &argp[idx].data.ipv4); |
| 1005 | else if (argp[idx].type == ARGT_STR) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1006 | if (!str2mask(argp[idx].data.str.area, &argp[idx].data.ipv4)) { |
| 1007 | msg = "invalid IPv4 mask"; |
| 1008 | goto error; |
| 1009 | } |
| 1010 | } |
| 1011 | else { |
| 1012 | msg = "integer or string expected"; |
| 1013 | goto error; |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 1014 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 1015 | argp[idx].type = ARGT_MSK4; |
| 1016 | break; |
| 1017 | |
| 1018 | case ARGT_IPV6: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1019 | if (argp[idx].type != ARGT_STR) { |
| 1020 | msg = "string expected"; |
| 1021 | goto error; |
| 1022 | } |
| 1023 | if (inet_pton(AF_INET6, argp[idx].data.str.area, &argp[idx].data.ipv6)) { |
| 1024 | msg = "invalid IPv6 address"; |
| 1025 | goto error; |
| 1026 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 1027 | argp[idx].type = ARGT_IPV6; |
| 1028 | break; |
| 1029 | |
| 1030 | case ARGT_MSK6: |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 1031 | if (argp[idx].type == ARGT_SINT) |
| 1032 | len2mask6(argp[idx].data.sint, &argp[idx].data.ipv6); |
| 1033 | else if (argp[idx].type == ARGT_STR) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1034 | if (!str2mask6(argp[idx].data.str.area, &argp[idx].data.ipv6)) { |
| 1035 | msg = "invalid IPv6 mask"; |
| 1036 | goto error; |
| 1037 | } |
| 1038 | } |
| 1039 | else { |
| 1040 | msg = "integer or string expected"; |
| 1041 | goto error; |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 1042 | } |
Tim Duesterhus | b814da6 | 2018-01-25 16:24:50 +0100 | [diff] [blame] | 1043 | argp[idx].type = ARGT_MSK6; |
| 1044 | break; |
| 1045 | |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 1046 | case ARGT_REG: |
| 1047 | if (argp[idx].type != ARGT_STR) { |
| 1048 | msg = "string expected"; |
| 1049 | goto error; |
| 1050 | } |
| 1051 | reg = regex_comp(argp[idx].data.str.area, !(argp[idx].type_flags & ARGF_REG_ICASE), 1, &err); |
| 1052 | if (!reg) { |
| 1053 | msg = lua_pushfstring(L, "error compiling regex '%s' : '%s'", |
| 1054 | argp[idx].data.str.area, err); |
| 1055 | free(err); |
| 1056 | goto error; |
| 1057 | } |
| 1058 | argp[idx].type = ARGT_REG; |
| 1059 | argp[idx].data.reg = reg; |
| 1060 | break; |
| 1061 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 1062 | case ARGT_USR: |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 1063 | if (argp[idx].type != ARGT_STR) { |
| 1064 | msg = "string expected"; |
| 1065 | goto error; |
| 1066 | } |
| 1067 | if (p->uri_auth && p->uri_auth->userlist && |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 1068 | strcmp(p->uri_auth->userlist->name, argp[idx].data.str.area) == 0) |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 1069 | ul = p->uri_auth->userlist; |
| 1070 | else |
| 1071 | ul = auth_find_userlist(argp[idx].data.str.area); |
| 1072 | |
| 1073 | if (!ul) { |
| 1074 | msg = lua_pushfstring(L, "unable to find userlist '%s'", argp[idx].data.str.area); |
| 1075 | goto error; |
| 1076 | } |
| 1077 | argp[idx].type = ARGT_USR; |
| 1078 | argp[idx].data.usr = ul; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1079 | break; |
| 1080 | |
| 1081 | case ARGT_STR: |
| 1082 | if (!chunk_dup(&tmp, &argp[idx].data.str)) { |
| 1083 | msg = "unable to duplicate string arg"; |
| 1084 | goto error; |
| 1085 | } |
| 1086 | argp[idx].data.str = tmp; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 1087 | break; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1088 | |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 1089 | case ARGT_MAP: |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 1090 | msg = "type not yet supported"; |
| 1091 | goto error; |
| 1092 | break; |
| 1093 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 1094 | } |
| 1095 | |
| 1096 | /* Check for type of argument. */ |
| 1097 | if ((mask & ARGT_MASK) != argp[idx].type) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1098 | msg = lua_pushfstring(L, "'%s' expected, got '%s'", |
| 1099 | arg_type_names[(mask & ARGT_MASK)], |
| 1100 | arg_type_names[argp[idx].type & ARGT_MASK]); |
| 1101 | goto error; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 1102 | } |
| 1103 | |
| 1104 | /* Next argument. */ |
| 1105 | mask >>= ARGT_BITS; |
| 1106 | idx++; |
| 1107 | } |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1108 | return 0; |
| 1109 | |
| 1110 | error: |
Olivier Houchard | da25dac | 2022-09-13 00:31:17 +0200 | [diff] [blame] | 1111 | argp[idx].type = ARGT_STOP; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1112 | for (i = 0; i < idx; i++) { |
| 1113 | if (argp[i].type == ARGT_STR) |
| 1114 | chunk_destroy(&argp[i].data.str); |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 1115 | else if (argp[i].type == ARGT_REG) |
| 1116 | regex_free(argp[i].data.reg); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1117 | } |
| 1118 | WILL_LJMP(luaL_argerror(L, first + idx, msg)); |
| 1119 | return 0; /* Never reached */ |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 1120 | } |
| 1121 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1122 | /* |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 1123 | * The following functions are used to make correspondence between the the |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1124 | * executed lua pointer and the "struct hlua *" that contain the context. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1125 | * |
| 1126 | * - hlua_gethlua : return the hlua context associated with an lua_State. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1127 | * - hlua_sethlua : create the association between hlua context and lua_state. |
| 1128 | */ |
| 1129 | static inline struct hlua *hlua_gethlua(lua_State *L) |
| 1130 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 1131 | struct hlua **hlua = lua_getextraspace(L); |
| 1132 | return *hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1133 | } |
| 1134 | static inline void hlua_sethlua(struct hlua *hlua) |
| 1135 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 1136 | struct hlua **hlua_store = lua_getextraspace(hlua->T); |
| 1137 | *hlua_store = hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1138 | } |
| 1139 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1140 | /* This function is used to send logs. It try to send on screen (stderr) |
| 1141 | * and on the default syslog server. |
| 1142 | */ |
| 1143 | static inline void hlua_sendlog(struct proxy *px, int level, const char *msg) |
| 1144 | { |
| 1145 | struct tm tm; |
| 1146 | char *p; |
| 1147 | |
| 1148 | /* Cleanup the log message. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1149 | p = trash.area; |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1150 | for (; *msg != '\0'; msg++, p++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1151 | if (p >= trash.area + trash.size - 1) { |
Thierry FOURNIER | ccf0063 | 2015-09-16 12:47:03 +0200 | [diff] [blame] | 1152 | /* Break the message if exceed the buffer size. */ |
| 1153 | *(p-4) = ' '; |
| 1154 | *(p-3) = '.'; |
| 1155 | *(p-2) = '.'; |
| 1156 | *(p-1) = '.'; |
| 1157 | break; |
| 1158 | } |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 1159 | if (isprint((unsigned char)*msg)) |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1160 | *p = *msg; |
| 1161 | else |
| 1162 | *p = '.'; |
| 1163 | } |
| 1164 | *p = '\0'; |
| 1165 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1166 | send_log(px, level, "%s\n", trash.area); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1167 | if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) { |
Christopher Faulet | f98d821 | 2020-10-02 18:13:52 +0200 | [diff] [blame] | 1168 | if (level == LOG_DEBUG && !(global.mode & MODE_DEBUG)) |
| 1169 | return; |
| 1170 | |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 1171 | get_localtime(date.tv_sec, &tm); |
| 1172 | fprintf(stderr, "[%s] %03d/%02d%02d%02d (%d) : %s\n", |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1173 | log_levels[level], tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec, |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1174 | (int)getpid(), trash.area); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1175 | fflush(stderr); |
| 1176 | } |
| 1177 | } |
| 1178 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1179 | /* This function just ensure that the yield will be always |
| 1180 | * returned with a timeout and permit to set some flags |
| 1181 | */ |
Aurelien DARRAGON | f5d3b31 | 2023-07-13 10:18:04 +0200 | [diff] [blame] | 1182 | __LJMP void hlua_yieldk(lua_State *L, int nresults, lua_KContext ctx, |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1183 | lua_KFunction k, int timeout, unsigned int flags) |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1184 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 1185 | struct hlua *hlua; |
| 1186 | |
| 1187 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 1188 | hlua = hlua_gethlua(L); |
| 1189 | if (!hlua) { |
| 1190 | return; |
| 1191 | } |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1192 | |
| 1193 | /* Set the wake timeout. If timeout is required, we set |
| 1194 | * the expiration time. |
| 1195 | */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1196 | hlua->wake_time = timeout; |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1197 | |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 1198 | hlua->flags |= flags; |
| 1199 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1200 | /* Process the yield. */ |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 1201 | MAY_LJMP(lua_yieldk(L, nresults, ctx, k)); |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1202 | } |
| 1203 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1204 | /* This function initialises the Lua environment stored in the stream. |
| 1205 | * It must be called at the start of the stream. This function creates |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1206 | * an LUA coroutine. It can not be use to crete the main LUA context. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 1207 | * |
| 1208 | * This function is particular. it initialises a new Lua thread. If the |
| 1209 | * initialisation fails (example: out of memory error), the lua function |
| 1210 | * throws an error (longjmp). |
| 1211 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1212 | * This function manipulates two Lua stacks: the main and the thread. Only |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 1213 | * the main stack can fail. The thread is not manipulated. This function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1214 | * MUST NOT manipulate the created thread stack state, because it is not |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1215 | * protected against errors thrown by the thread stack. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1216 | */ |
Aurelien DARRAGON | 5483bde | 2023-03-21 14:02:16 +0100 | [diff] [blame] | 1217 | int hlua_ctx_init(struct hlua *lua, int state_id, struct task *task) |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1218 | { |
| 1219 | lua->Mref = LUA_REFNIL; |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1220 | lua->flags = 0; |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 1221 | lua->gc_count = 0; |
Christopher Faulet | bc275a9 | 2020-02-26 14:55:16 +0100 | [diff] [blame] | 1222 | lua->wake_time = TICK_ETERNITY; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1223 | lua->state_id = state_id; |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 1224 | LIST_INIT(&lua->com); |
Aurelien DARRAGON | 5483bde | 2023-03-21 14:02:16 +0100 | [diff] [blame] | 1225 | if (!SET_SAFE_LJMP_PARENT(lua)) { |
| 1226 | lua->Tref = LUA_REFNIL; |
| 1227 | return 0; |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1228 | } |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1229 | lua->T = lua_newthread(hlua_states[state_id]); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1230 | if (!lua->T) { |
| 1231 | lua->Tref = LUA_REFNIL; |
Aurelien DARRAGON | 5483bde | 2023-03-21 14:02:16 +0100 | [diff] [blame] | 1232 | RESET_SAFE_LJMP_PARENT(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1233 | return 0; |
| 1234 | } |
| 1235 | hlua_sethlua(lua); |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1236 | lua->Tref = luaL_ref(hlua_states[state_id], LUA_REGISTRYINDEX); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1237 | lua->task = task; |
Aurelien DARRAGON | 5483bde | 2023-03-21 14:02:16 +0100 | [diff] [blame] | 1238 | RESET_SAFE_LJMP_PARENT(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1239 | return 1; |
| 1240 | } |
| 1241 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1242 | /* Used to destroy the Lua coroutine when the attached stream or task |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1243 | * is destroyed. The destroy also the memory context. The struct "lua" |
Aurelien DARRAGON | d5c8a10 | 2023-03-01 16:45:50 +0100 | [diff] [blame] | 1244 | * will be freed. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1245 | */ |
| 1246 | void hlua_ctx_destroy(struct hlua *lua) |
| 1247 | { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 1248 | if (!lua) |
Thierry FOURNIER | a718b29 | 2015-03-04 16:48:34 +0100 | [diff] [blame] | 1249 | return; |
| 1250 | |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 1251 | if (!lua->T) |
| 1252 | goto end; |
| 1253 | |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 1254 | /* Purge all the pending signals. */ |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1255 | notification_purge(&lua->com); |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 1256 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1257 | if (!SET_SAFE_LJMP(lua)) |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 1258 | return; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1259 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1260 | RESET_SAFE_LJMP(lua); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 1261 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1262 | if (!SET_SAFE_LJMP_PARENT(lua)) |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 1263 | return; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1264 | luaL_unref(hlua_states[lua->state_id], LUA_REGISTRYINDEX, lua->Tref); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1265 | RESET_SAFE_LJMP_PARENT(lua); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 1266 | /* Forces a garbage collecting process. If the Lua program is finished |
| 1267 | * without error, we run the GC on the thread pointer. Its freed all |
| 1268 | * the unused memory. |
| 1269 | * If the thread is finnish with an error or is currently yielded, |
| 1270 | * it seems that the GC applied on the thread doesn't clean anything, |
| 1271 | * so e run the GC on the main thread. |
| 1272 | * NOTE: maybe this action locks all the Lua threads untiml the en of |
| 1273 | * the garbage collection. |
| 1274 | */ |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 1275 | if (lua->gc_count) { |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1276 | if (!SET_SAFE_LJMP_PARENT(lua)) |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 1277 | return; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1278 | lua_gc(hlua_states[lua->state_id], LUA_GCCOLLECT, 0); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1279 | RESET_SAFE_LJMP_PARENT(lua); |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 1280 | } |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 1281 | |
Thierry FOURNIER | a7b536b | 2015-09-21 22:50:24 +0200 | [diff] [blame] | 1282 | lua->T = NULL; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 1283 | |
| 1284 | end: |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 1285 | pool_free(pool_head_hlua, lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1286 | } |
| 1287 | |
| 1288 | /* This function is used to restore the Lua context when a coroutine |
| 1289 | * fails. This function copy the common memory between old coroutine |
| 1290 | * and the new coroutine. The old coroutine is destroyed, and its |
| 1291 | * replaced by the new coroutine. |
| 1292 | * If the flag "keep_msg" is set, the last entry of the old is assumed |
| 1293 | * as string error message and it is copied in the new stack. |
| 1294 | */ |
| 1295 | static int hlua_ctx_renew(struct hlua *lua, int keep_msg) |
| 1296 | { |
| 1297 | lua_State *T; |
| 1298 | int new_ref; |
| 1299 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1300 | /* New Lua coroutine. */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1301 | T = lua_newthread(hlua_states[lua->state_id]); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1302 | if (!T) |
| 1303 | return 0; |
| 1304 | |
| 1305 | /* Copy last error message. */ |
| 1306 | if (keep_msg) |
| 1307 | lua_xmove(lua->T, T, 1); |
| 1308 | |
| 1309 | /* Copy data between the coroutines. */ |
| 1310 | lua_rawgeti(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 1311 | lua_xmove(lua->T, T, 1); |
Ilya Shipitsin | 46a030c | 2020-07-05 16:36:08 +0500 | [diff] [blame] | 1312 | new_ref = luaL_ref(T, LUA_REGISTRYINDEX); /* Value popped. */ |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1313 | |
| 1314 | /* Destroy old data. */ |
| 1315 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 1316 | |
| 1317 | /* The thread is garbage collected by Lua. */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1318 | luaL_unref(hlua_states[lua->state_id], LUA_REGISTRYINDEX, lua->Tref); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1319 | |
| 1320 | /* Fill the struct with the new coroutine values. */ |
| 1321 | lua->Mref = new_ref; |
| 1322 | lua->T = T; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1323 | lua->Tref = luaL_ref(hlua_states[lua->state_id], LUA_REGISTRYINDEX); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1324 | |
| 1325 | /* Set context. */ |
| 1326 | hlua_sethlua(lua); |
| 1327 | |
| 1328 | return 1; |
| 1329 | } |
| 1330 | |
Aurelien DARRAGON | be7b7ab | 2024-03-12 17:05:54 +0100 | [diff] [blame] | 1331 | /* Helper function to get the lua ctx for a given stream and state_id */ |
| 1332 | static inline struct hlua *hlua_stream_ctx_get(struct stream *s, int state_id) |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 1333 | { |
Aurelien DARRAGON | be7b7ab | 2024-03-12 17:05:54 +0100 | [diff] [blame] | 1334 | /* state_id == 0 -> global runtime ctx |
| 1335 | * state_id != 0 -> per-thread runtime ctx |
| 1336 | */ |
| 1337 | return s->hlua[!!state_id]; |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 1338 | } |
| 1339 | |
Aurelien DARRAGON | be7b7ab | 2024-03-12 17:05:54 +0100 | [diff] [blame] | 1340 | /* Helper function to prepare the lua ctx for a given stream and state id |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 1341 | * |
Aurelien DARRAGON | be7b7ab | 2024-03-12 17:05:54 +0100 | [diff] [blame] | 1342 | * It uses the global or per-thread ctx depending on the expected |
| 1343 | * <state_id>. |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 1344 | * |
| 1345 | * Returns hlua ctx on success and NULL on failure |
| 1346 | */ |
| 1347 | static struct hlua *hlua_stream_ctx_prepare(struct stream *s, int state_id) |
| 1348 | { |
| 1349 | /* In the execution wrappers linked with a stream, the |
| 1350 | * Lua context can be not initialized. This behavior |
| 1351 | * permits to save performances because a systematic |
| 1352 | * Lua initialization cause 5% performances loss. |
| 1353 | */ |
Aurelien DARRAGON | be7b7ab | 2024-03-12 17:05:54 +0100 | [diff] [blame] | 1354 | if (!s->hlua[!!state_id]) { |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 1355 | struct hlua *hlua; |
| 1356 | |
| 1357 | hlua = pool_alloc(pool_head_hlua); |
| 1358 | if (!hlua) |
| 1359 | return NULL; |
| 1360 | HLUA_INIT(hlua); |
| 1361 | if (!hlua_ctx_init(hlua, state_id, s->task)) { |
| 1362 | pool_free(pool_head_hlua, hlua); |
| 1363 | return NULL; |
| 1364 | } |
Aurelien DARRAGON | be7b7ab | 2024-03-12 17:05:54 +0100 | [diff] [blame] | 1365 | s->hlua[!!state_id] = hlua; |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 1366 | } |
Aurelien DARRAGON | be7b7ab | 2024-03-12 17:05:54 +0100 | [diff] [blame] | 1367 | return s->hlua[!!state_id]; |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 1368 | } |
| 1369 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1370 | void hlua_hook(lua_State *L, lua_Debug *ar) |
| 1371 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 1372 | struct hlua *hlua; |
| 1373 | |
| 1374 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 1375 | hlua = hlua_gethlua(L); |
| 1376 | if (!hlua) |
| 1377 | return; |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1378 | |
| 1379 | /* Lua cannot yield when its returning from a function, |
| 1380 | * so, we can fix the interrupt hook to 1 instruction, |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1381 | * expecting that the function is finished. |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1382 | */ |
| 1383 | if (lua_gethookmask(L) & LUA_MASKRET) { |
| 1384 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, 1); |
| 1385 | return; |
| 1386 | } |
| 1387 | |
| 1388 | /* restore the interrupt condition. */ |
| 1389 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 1390 | |
| 1391 | /* If we interrupt the Lua processing in yieldable state, we yield. |
| 1392 | * If the state is not yieldable, trying yield causes an error. |
| 1393 | */ |
| 1394 | if (lua_isyieldable(L)) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 1395 | MAY_LJMP(hlua_yieldk(L, 0, 0, NULL, TICK_ETERNITY, HLUA_CTRLYIELD)); |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1396 | |
Thierry FOURNIER | a85cfb1 | 2015-03-13 14:50:06 +0100 | [diff] [blame] | 1397 | /* If we cannot yield, update the clock and check the timeout. */ |
| 1398 | tv_update_date(0, 1); |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1399 | hlua->run_time += now_ms - hlua->start_time; |
| 1400 | if (hlua->max_time && hlua->run_time >= hlua->max_time) { |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1401 | lua_pushfstring(L, "execution timeout"); |
| 1402 | WILL_LJMP(lua_error(L)); |
| 1403 | } |
| 1404 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1405 | /* Update the start time. */ |
| 1406 | hlua->start_time = now_ms; |
| 1407 | |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1408 | /* Try to interrupt the process at the end of the current |
| 1409 | * unyieldable function. |
| 1410 | */ |
| 1411 | lua_sethook(hlua->T, hlua_hook, LUA_MASKRET|LUA_MASKCOUNT, hlua_nb_instruction); |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1412 | } |
| 1413 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1414 | /* This function start or resumes the Lua stack execution. If the flag |
| 1415 | * "yield_allowed" if no set and the LUA stack execution returns a yield |
| 1416 | * The function return an error. |
| 1417 | * |
| 1418 | * The function can returns 4 values: |
| 1419 | * - HLUA_E_OK : The execution is terminated without any errors. |
| 1420 | * - HLUA_E_AGAIN : The execution must continue at the next associated |
| 1421 | * task wakeup. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1422 | * - HLUA_E_ERRMSG : An error has occurred, an error message is set in |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1423 | * the top of the stack. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1424 | * - HLUA_E_ERR : An error has occurred without error message. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1425 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1426 | * If an error occurred, the stack is renewed and it is ready to run new |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1427 | * LUA code. |
| 1428 | */ |
| 1429 | static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed) |
| 1430 | { |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1431 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
| 1432 | int nres; |
| 1433 | #endif |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1434 | int ret; |
| 1435 | const char *msg; |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1436 | const char *trace; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1437 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1438 | /* Initialise run time counter. */ |
| 1439 | if (!HLUA_IS_RUNNING(lua)) |
| 1440 | lua->run_time = 0; |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1441 | |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1442 | /* Lock the whole Lua execution. This lock must be before the |
| 1443 | * label "resume_execution". |
| 1444 | */ |
Aurelien DARRAGON | e00bf5d | 2023-03-21 13:22:33 +0100 | [diff] [blame] | 1445 | hlua_lock(lua); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1446 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1447 | resume_execution: |
| 1448 | |
| 1449 | /* This hook interrupts the Lua processing each 'hlua_nb_instruction' |
| 1450 | * instructions. it is used for preventing infinite loops. |
| 1451 | */ |
| 1452 | lua_sethook(lua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 1453 | |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1454 | /* Remove all flags except the running flags. */ |
Thierry FOURNIER | 2f3867f | 2015-09-28 01:02:01 +0200 | [diff] [blame] | 1455 | HLUA_SET_RUN(lua); |
| 1456 | HLUA_CLR_CTRLYIELD(lua); |
| 1457 | HLUA_CLR_WAKERESWR(lua); |
| 1458 | HLUA_CLR_WAKEREQWR(lua); |
Christopher Faulet | a6e792f | 2021-08-04 17:58:21 +0200 | [diff] [blame] | 1459 | HLUA_CLR_NOYIELD(lua); |
| 1460 | if (!yield_allowed) |
| 1461 | HLUA_SET_NOYIELD(lua); |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1462 | |
Christopher Faulet | bc275a9 | 2020-02-26 14:55:16 +0100 | [diff] [blame] | 1463 | /* Update the start time and reset wake_time. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1464 | lua->start_time = now_ms; |
Christopher Faulet | bc275a9 | 2020-02-26 14:55:16 +0100 | [diff] [blame] | 1465 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1466 | |
Aurelien DARRAGON | 5d35196 | 2024-03-12 00:22:44 +0100 | [diff] [blame] | 1467 | HLUA_SET_BUSY(lua); |
| 1468 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1469 | /* Call the function. */ |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1470 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1471 | ret = lua_resume(lua->T, hlua_states[lua->state_id], lua->nargs, &nres); |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1472 | #else |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1473 | ret = lua_resume(lua->T, hlua_states[lua->state_id], lua->nargs); |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1474 | #endif |
Aurelien DARRAGON | fff2796 | 2023-09-08 14:00:27 +0200 | [diff] [blame] | 1475 | |
Aurelien DARRAGON | 5d35196 | 2024-03-12 00:22:44 +0100 | [diff] [blame] | 1476 | HLUA_CLR_BUSY(lua); |
| 1477 | |
Aurelien DARRAGON | fff2796 | 2023-09-08 14:00:27 +0200 | [diff] [blame] | 1478 | /* reset nargs because those possibly passed to the lua_resume() call |
| 1479 | * were already consumed, and since we may call lua_resume() again |
| 1480 | * after a successful yield, we don't want to pass stale nargs hint |
| 1481 | * to the Lua API. As such, nargs should be set explicitly before each |
| 1482 | * lua_resume() (or hlua_ctx_resume()) invocation if needed. |
| 1483 | */ |
| 1484 | lua->nargs = 0; |
| 1485 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1486 | switch (ret) { |
| 1487 | |
| 1488 | case LUA_OK: |
| 1489 | ret = HLUA_E_OK; |
| 1490 | break; |
| 1491 | |
| 1492 | case LUA_YIELD: |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1493 | /* Check if the execution timeout is expired. It it is the case, we |
| 1494 | * break the Lua execution. |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1495 | */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1496 | tv_update_date(0, 1); |
| 1497 | lua->run_time += now_ms - lua->start_time; |
| 1498 | if (lua->max_time && lua->run_time > lua->max_time) { |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1499 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1500 | ret = HLUA_E_ETMOUT; |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1501 | break; |
| 1502 | } |
| 1503 | /* Process the forced yield. if the general yield is not allowed or |
| 1504 | * if no task were associated this the current Lua execution |
| 1505 | * coroutine, we resume the execution. Else we want to return in the |
| 1506 | * scheduler and we want to be waked up again, to continue the |
| 1507 | * current Lua execution. So we schedule our own task. |
| 1508 | */ |
| 1509 | if (HLUA_IS_CTRLYIELDING(lua)) { |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1510 | if (!yield_allowed || !lua->task) |
| 1511 | goto resume_execution; |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1512 | task_wakeup(lua->task, TASK_WOKEN_MSG); |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1513 | } |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1514 | if (!yield_allowed) { |
| 1515 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1516 | ret = HLUA_E_YIELD; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1517 | break; |
| 1518 | } |
| 1519 | ret = HLUA_E_AGAIN; |
| 1520 | break; |
| 1521 | |
| 1522 | case LUA_ERRRUN: |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1523 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1524 | /* Special exit case. The traditional exit is returned as an error |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1525 | * because the errors ares the only one mean to return immediately |
| 1526 | * from and lua execution. |
| 1527 | */ |
| 1528 | if (lua->flags & HLUA_EXIT) { |
| 1529 | ret = HLUA_E_OK; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 1530 | hlua_ctx_renew(lua, 1); |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1531 | break; |
| 1532 | } |
| 1533 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1534 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1535 | if (!lua_checkstack(lua->T, 1)) { |
| 1536 | ret = HLUA_E_ERR; |
| 1537 | break; |
| 1538 | } |
Aurelien DARRAGON | a374161 | 2024-03-01 15:55:17 +0100 | [diff] [blame] | 1539 | msg = hlua_tostring_safe(lua->T, -1); |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 1540 | trace = hlua_traceback(lua->T, ", "); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1541 | if (msg) |
Thierry Fournier | 46278ff | 2020-11-29 11:48:12 +0100 | [diff] [blame] | 1542 | lua_pushfstring(lua->T, "[state-id %d] runtime error: %s from %s", lua->state_id, msg, trace); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1543 | else |
Thierry Fournier | 46278ff | 2020-11-29 11:48:12 +0100 | [diff] [blame] | 1544 | lua_pushfstring(lua->T, "[state-id %d] unknown runtime error from %s", lua->state_id, trace); |
Aurelien DARRAGON | adac932 | 2024-03-01 19:54:16 +0100 | [diff] [blame] | 1545 | |
| 1546 | /* Move the error msg at the top and then empty the stack except last msg */ |
| 1547 | lua_insert(lua->T, -lua_gettop(lua->T)); |
| 1548 | lua_settop(lua->T, 1); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1549 | ret = HLUA_E_ERRMSG; |
| 1550 | break; |
| 1551 | |
| 1552 | case LUA_ERRMEM: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1553 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1554 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1555 | ret = HLUA_E_NOMEM; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1556 | break; |
| 1557 | |
| 1558 | case LUA_ERRERR: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1559 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1560 | if (!lua_checkstack(lua->T, 1)) { |
| 1561 | ret = HLUA_E_ERR; |
| 1562 | break; |
| 1563 | } |
Aurelien DARRAGON | a374161 | 2024-03-01 15:55:17 +0100 | [diff] [blame] | 1564 | msg = hlua_tostring_safe(lua->T, -1); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1565 | if (msg) |
Thierry Fournier | 46278ff | 2020-11-29 11:48:12 +0100 | [diff] [blame] | 1566 | lua_pushfstring(lua->T, "[state-id %d] message handler error: %s", lua->state_id, msg); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1567 | else |
Thierry Fournier | 46278ff | 2020-11-29 11:48:12 +0100 | [diff] [blame] | 1568 | lua_pushfstring(lua->T, "[state-id %d] message handler error", lua->state_id); |
Aurelien DARRAGON | adac932 | 2024-03-01 19:54:16 +0100 | [diff] [blame] | 1569 | |
| 1570 | /* Move the error msg at the top and then empty the stack except last msg */ |
| 1571 | lua_insert(lua->T, -lua_gettop(lua->T)); |
| 1572 | lua_settop(lua->T, 1); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1573 | ret = HLUA_E_ERRMSG; |
| 1574 | break; |
| 1575 | |
| 1576 | default: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1577 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1578 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1579 | ret = HLUA_E_ERR; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1580 | break; |
| 1581 | } |
| 1582 | |
| 1583 | switch (ret) { |
| 1584 | case HLUA_E_AGAIN: |
| 1585 | break; |
| 1586 | |
| 1587 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1588 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1589 | hlua_ctx_renew(lua, 1); |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1590 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1591 | break; |
| 1592 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1593 | case HLUA_E_ETMOUT: |
| 1594 | case HLUA_E_NOMEM: |
| 1595 | case HLUA_E_YIELD: |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1596 | case HLUA_E_ERR: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1597 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1598 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1599 | hlua_ctx_renew(lua, 0); |
| 1600 | break; |
| 1601 | |
| 1602 | case HLUA_E_OK: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1603 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1604 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1605 | break; |
| 1606 | } |
| 1607 | |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1608 | /* This is the main exit point, remove the Lua lock. */ |
Aurelien DARRAGON | e00bf5d | 2023-03-21 13:22:33 +0100 | [diff] [blame] | 1609 | hlua_unlock(lua); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1610 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1611 | return ret; |
| 1612 | } |
| 1613 | |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1614 | /* This function exit the current code. */ |
| 1615 | __LJMP static int hlua_done(lua_State *L) |
| 1616 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 1617 | struct hlua *hlua; |
| 1618 | |
| 1619 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 1620 | hlua = hlua_gethlua(L); |
| 1621 | if (!hlua) |
| 1622 | return 0; |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1623 | |
| 1624 | hlua->flags |= HLUA_EXIT; |
| 1625 | WILL_LJMP(lua_error(L)); |
| 1626 | |
| 1627 | return 0; |
| 1628 | } |
| 1629 | |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1630 | /* This function is an LUA binding. It provides a function |
| 1631 | * for deleting ACL from a referenced ACL file. |
| 1632 | */ |
| 1633 | __LJMP static int hlua_del_acl(lua_State *L) |
| 1634 | { |
| 1635 | const char *name; |
| 1636 | const char *key; |
| 1637 | struct pat_ref *ref; |
| 1638 | |
| 1639 | MAY_LJMP(check_args(L, 2, "del_acl")); |
| 1640 | |
| 1641 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1642 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1643 | |
| 1644 | ref = pat_ref_lookup(name); |
| 1645 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1646 | WILL_LJMP(luaL_error(L, "'del_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1647 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1648 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1649 | pat_ref_delete(ref, key); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1650 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1651 | return 0; |
| 1652 | } |
| 1653 | |
| 1654 | /* This function is an LUA binding. It provides a function |
| 1655 | * for deleting map entry from a referenced map file. |
| 1656 | */ |
| 1657 | static int hlua_del_map(lua_State *L) |
| 1658 | { |
| 1659 | const char *name; |
| 1660 | const char *key; |
| 1661 | struct pat_ref *ref; |
| 1662 | |
| 1663 | MAY_LJMP(check_args(L, 2, "del_map")); |
| 1664 | |
| 1665 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1666 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1667 | |
| 1668 | ref = pat_ref_lookup(name); |
| 1669 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1670 | WILL_LJMP(luaL_error(L, "'del_map': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1671 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1672 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1673 | pat_ref_delete(ref, key); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1674 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1675 | return 0; |
| 1676 | } |
| 1677 | |
| 1678 | /* This function is an LUA binding. It provides a function |
| 1679 | * for adding ACL pattern from a referenced ACL file. |
| 1680 | */ |
| 1681 | static int hlua_add_acl(lua_State *L) |
| 1682 | { |
| 1683 | const char *name; |
| 1684 | const char *key; |
| 1685 | struct pat_ref *ref; |
| 1686 | |
| 1687 | MAY_LJMP(check_args(L, 2, "add_acl")); |
| 1688 | |
| 1689 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1690 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1691 | |
| 1692 | ref = pat_ref_lookup(name); |
| 1693 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1694 | WILL_LJMP(luaL_error(L, "'add_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1695 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1696 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1697 | if (pat_ref_find_elt(ref, key) == NULL) |
| 1698 | pat_ref_add(ref, key, NULL, NULL); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1699 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1700 | return 0; |
| 1701 | } |
| 1702 | |
| 1703 | /* This function is an LUA binding. It provides a function |
| 1704 | * for setting map pattern and sample from a referenced map |
| 1705 | * file. |
| 1706 | */ |
| 1707 | static int hlua_set_map(lua_State *L) |
| 1708 | { |
| 1709 | const char *name; |
| 1710 | const char *key; |
| 1711 | const char *value; |
| 1712 | struct pat_ref *ref; |
| 1713 | |
| 1714 | MAY_LJMP(check_args(L, 3, "set_map")); |
| 1715 | |
| 1716 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1717 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1718 | value = MAY_LJMP(luaL_checkstring(L, 3)); |
| 1719 | |
| 1720 | ref = pat_ref_lookup(name); |
| 1721 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1722 | WILL_LJMP(luaL_error(L, "'set_map': unknown map file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1723 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1724 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1725 | if (pat_ref_find_elt(ref, key) != NULL) |
| 1726 | pat_ref_set(ref, key, value, NULL); |
| 1727 | else |
| 1728 | pat_ref_add(ref, key, value, NULL); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1729 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1730 | return 0; |
| 1731 | } |
| 1732 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1733 | /* A class is a lot of memory that contain data. This data can be a table, |
| 1734 | * an integer or user data. This data is associated with a metatable. This |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 1735 | * metatable have an original version registered in the global context with |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1736 | * the name of the object (_G[<name>] = <metable> ). |
| 1737 | * |
| 1738 | * A metable is a table that modify the standard behavior of a standard |
| 1739 | * access to the associated data. The entries of this new metatable are |
| 1740 | * defined as is: |
| 1741 | * |
| 1742 | * http://lua-users.org/wiki/MetatableEvents |
| 1743 | * |
| 1744 | * __index |
| 1745 | * |
| 1746 | * we access an absent field in a table, the result is nil. This is |
| 1747 | * true, but it is not the whole truth. Actually, such access triggers |
| 1748 | * the interpreter to look for an __index metamethod: If there is no |
| 1749 | * such method, as usually happens, then the access results in nil; |
| 1750 | * otherwise, the metamethod will provide the result. |
| 1751 | * |
| 1752 | * Control 'prototype' inheritance. When accessing "myTable[key]" and |
| 1753 | * the key does not appear in the table, but the metatable has an __index |
| 1754 | * property: |
| 1755 | * |
| 1756 | * - if the value is a function, the function is called, passing in the |
| 1757 | * table and the key; the return value of that function is returned as |
| 1758 | * the result. |
| 1759 | * |
| 1760 | * - if the value is another table, the value of the key in that table is |
| 1761 | * asked for and returned (and if it doesn't exist in that table, but that |
| 1762 | * table's metatable has an __index property, then it continues on up) |
| 1763 | * |
| 1764 | * - Use "rawget(myTable,key)" to skip this metamethod. |
| 1765 | * |
| 1766 | * http://www.lua.org/pil/13.4.1.html |
| 1767 | * |
| 1768 | * __newindex |
| 1769 | * |
| 1770 | * Like __index, but control property assignment. |
| 1771 | * |
| 1772 | * __mode - Control weak references. A string value with one or both |
| 1773 | * of the characters 'k' and 'v' which specifies that the the |
| 1774 | * keys and/or values in the table are weak references. |
| 1775 | * |
| 1776 | * __call - Treat a table like a function. When a table is followed by |
| 1777 | * parenthesis such as "myTable( 'foo' )" and the metatable has |
| 1778 | * a __call key pointing to a function, that function is invoked |
| 1779 | * (passing any specified arguments) and the return value is |
| 1780 | * returned. |
| 1781 | * |
| 1782 | * __metatable - Hide the metatable. When "getmetatable( myTable )" is |
| 1783 | * called, if the metatable for myTable has a __metatable |
| 1784 | * key, the value of that key is returned instead of the |
| 1785 | * actual metatable. |
| 1786 | * |
| 1787 | * __tostring - Control string representation. When the builtin |
| 1788 | * "tostring( myTable )" function is called, if the metatable |
| 1789 | * for myTable has a __tostring property set to a function, |
| 1790 | * that function is invoked (passing myTable to it) and the |
| 1791 | * return value is used as the string representation. |
| 1792 | * |
| 1793 | * __len - Control table length. When the table length is requested using |
| 1794 | * the length operator ( '#' ), if the metatable for myTable has |
| 1795 | * a __len key pointing to a function, that function is invoked |
| 1796 | * (passing myTable to it) and the return value used as the value |
| 1797 | * of "#myTable". |
| 1798 | * |
| 1799 | * __gc - Userdata finalizer code. When userdata is set to be garbage |
| 1800 | * collected, if the metatable has a __gc field pointing to a |
| 1801 | * function, that function is first invoked, passing the userdata |
| 1802 | * to it. The __gc metamethod is not called for tables. |
| 1803 | * (See http://lua-users.org/lists/lua-l/2006-11/msg00508.html) |
| 1804 | * |
| 1805 | * Special metamethods for redefining standard operators: |
| 1806 | * http://www.lua.org/pil/13.1.html |
| 1807 | * |
| 1808 | * __add "+" |
| 1809 | * __sub "-" |
| 1810 | * __mul "*" |
| 1811 | * __div "/" |
| 1812 | * __unm "!" |
| 1813 | * __pow "^" |
| 1814 | * __concat ".." |
| 1815 | * |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1816 | * Special methods for redefining standard relations |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1817 | * http://www.lua.org/pil/13.2.html |
| 1818 | * |
| 1819 | * __eq "==" |
| 1820 | * __lt "<" |
| 1821 | * __le "<=" |
| 1822 | */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1823 | |
| 1824 | /* |
| 1825 | * |
| 1826 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1827 | * Class Map |
| 1828 | * |
| 1829 | * |
| 1830 | */ |
| 1831 | |
| 1832 | /* Returns a struct hlua_map if the stack entry "ud" is |
| 1833 | * a class session, otherwise it throws an error. |
| 1834 | */ |
| 1835 | __LJMP static struct map_descriptor *hlua_checkmap(lua_State *L, int ud) |
| 1836 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1837 | return MAY_LJMP(hlua_checkudata(L, ud, class_map_ref)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1838 | } |
| 1839 | |
| 1840 | /* This function is the map constructor. It don't need |
| 1841 | * the class Map object. It creates and return a new Map |
| 1842 | * object. It must be called only during "body" or "init" |
| 1843 | * context because it process some filesystem accesses. |
| 1844 | */ |
| 1845 | __LJMP static int hlua_map_new(struct lua_State *L) |
| 1846 | { |
| 1847 | const char *fn; |
| 1848 | int match = PAT_MATCH_STR; |
| 1849 | struct sample_conv conv; |
| 1850 | const char *file = ""; |
| 1851 | int line = 0; |
| 1852 | lua_Debug ar; |
| 1853 | char *err = NULL; |
| 1854 | struct arg args[2]; |
| 1855 | |
| 1856 | if (lua_gettop(L) < 1 || lua_gettop(L) > 2) |
| 1857 | WILL_LJMP(luaL_error(L, "'new' needs at least 1 argument.")); |
| 1858 | |
| 1859 | fn = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1860 | |
| 1861 | if (lua_gettop(L) >= 2) { |
| 1862 | match = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 1863 | if (match < 0 || match >= PAT_MATCH_NUM) |
| 1864 | WILL_LJMP(luaL_error(L, "'new' needs a valid match method.")); |
| 1865 | } |
| 1866 | |
| 1867 | /* Get Lua filename and line number. */ |
| 1868 | if (lua_getstack(L, 1, &ar)) { /* check function at level */ |
| 1869 | lua_getinfo(L, "Sl", &ar); /* get info about it */ |
| 1870 | if (ar.currentline > 0) { /* is there info? */ |
| 1871 | file = ar.short_src; |
| 1872 | line = ar.currentline; |
| 1873 | } |
| 1874 | } |
| 1875 | |
| 1876 | /* fill fake sample_conv struct. */ |
| 1877 | conv.kw = ""; /* unused. */ |
| 1878 | conv.process = NULL; /* unused. */ |
| 1879 | conv.arg_mask = 0; /* unused. */ |
| 1880 | conv.val_args = NULL; /* unused. */ |
| 1881 | conv.out_type = SMP_T_STR; |
| 1882 | conv.private = (void *)(long)match; |
| 1883 | switch (match) { |
| 1884 | case PAT_MATCH_STR: conv.in_type = SMP_T_STR; break; |
| 1885 | case PAT_MATCH_BEG: conv.in_type = SMP_T_STR; break; |
| 1886 | case PAT_MATCH_SUB: conv.in_type = SMP_T_STR; break; |
| 1887 | case PAT_MATCH_DIR: conv.in_type = SMP_T_STR; break; |
| 1888 | case PAT_MATCH_DOM: conv.in_type = SMP_T_STR; break; |
| 1889 | case PAT_MATCH_END: conv.in_type = SMP_T_STR; break; |
| 1890 | case PAT_MATCH_REG: conv.in_type = SMP_T_STR; break; |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1891 | case PAT_MATCH_INT: conv.in_type = SMP_T_SINT; break; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1892 | case PAT_MATCH_IP: conv.in_type = SMP_T_ADDR; break; |
| 1893 | default: |
| 1894 | WILL_LJMP(luaL_error(L, "'new' doesn't support this match mode.")); |
| 1895 | } |
| 1896 | |
| 1897 | /* fill fake args. */ |
| 1898 | args[0].type = ARGT_STR; |
Christopher Faulet | 73292e9 | 2020-08-06 08:40:09 +0200 | [diff] [blame] | 1899 | args[0].data.str.area = strdup(fn); |
| 1900 | args[0].data.str.data = strlen(fn); |
| 1901 | args[0].data.str.size = args[0].data.str.data+1; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1902 | args[1].type = ARGT_STOP; |
| 1903 | |
| 1904 | /* load the map. */ |
| 1905 | if (!sample_load_map(args, &conv, file, line, &err)) { |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 1906 | /* error case: we can't use luaL_error because we must |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1907 | * free the err variable. |
| 1908 | */ |
| 1909 | luaL_where(L, 1); |
| 1910 | lua_pushfstring(L, "'new': %s.", err); |
| 1911 | lua_concat(L, 2); |
| 1912 | free(err); |
Christopher Faulet | 6ad7df4 | 2020-08-07 11:45:18 +0200 | [diff] [blame] | 1913 | chunk_destroy(&args[0].data.str); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1914 | WILL_LJMP(lua_error(L)); |
| 1915 | } |
| 1916 | |
| 1917 | /* create the lua object. */ |
| 1918 | lua_newtable(L); |
| 1919 | lua_pushlightuserdata(L, args[0].data.map); |
| 1920 | lua_rawseti(L, -2, 0); |
| 1921 | |
| 1922 | /* Pop a class Map metatable and affect it to the userdata. */ |
| 1923 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_map_ref); |
| 1924 | lua_setmetatable(L, -2); |
| 1925 | |
| 1926 | |
| 1927 | return 1; |
| 1928 | } |
| 1929 | |
| 1930 | __LJMP static inline int _hlua_map_lookup(struct lua_State *L, int str) |
| 1931 | { |
| 1932 | struct map_descriptor *desc; |
| 1933 | struct pattern *pat; |
| 1934 | struct sample smp; |
| 1935 | |
| 1936 | MAY_LJMP(check_args(L, 2, "lookup")); |
| 1937 | desc = MAY_LJMP(hlua_checkmap(L, 1)); |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1938 | if (desc->pat.expect_type == SMP_T_SINT) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1939 | smp.data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1940 | smp.data.u.sint = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1941 | } |
| 1942 | else { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1943 | smp.data.type = SMP_T_STR; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1944 | smp.flags = SMP_F_CONST; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1945 | 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] | 1946 | smp.data.u.str.size = smp.data.u.str.data + 1; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1947 | } |
| 1948 | |
| 1949 | pat = pattern_exec_match(&desc->pat, &smp, 1); |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1950 | if (!pat || !pat->data) { |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1951 | if (str) |
| 1952 | lua_pushstring(L, ""); |
| 1953 | else |
| 1954 | lua_pushnil(L); |
| 1955 | return 1; |
| 1956 | } |
| 1957 | |
| 1958 | /* 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] | 1959 | 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] | 1960 | return 1; |
| 1961 | } |
| 1962 | |
| 1963 | __LJMP static int hlua_map_lookup(struct lua_State *L) |
| 1964 | { |
| 1965 | return _hlua_map_lookup(L, 0); |
| 1966 | } |
| 1967 | |
| 1968 | __LJMP static int hlua_map_slookup(struct lua_State *L) |
| 1969 | { |
| 1970 | return _hlua_map_lookup(L, 1); |
| 1971 | } |
| 1972 | |
| 1973 | /* |
| 1974 | * |
| 1975 | * |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1976 | * Class Socket |
| 1977 | * |
| 1978 | * |
| 1979 | */ |
| 1980 | |
| 1981 | __LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud) |
| 1982 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1983 | return MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1984 | } |
| 1985 | |
| 1986 | /* 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] | 1987 | * connection. It is used for notify space available to send or data |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1988 | * received. |
| 1989 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1990 | static void hlua_socket_handler(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1991 | { |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1992 | struct stream_interface *si = appctx->owner; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1993 | |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1994 | if (appctx->ctx.hlua_cosocket.die) { |
| 1995 | si_shutw(si); |
| 1996 | si_shutr(si); |
| 1997 | si_ic(si)->flags |= CF_READ_NULL; |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1998 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1999 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 2000 | stream_shutdown(si_strm(si), SF_ERR_KILLED); |
| 2001 | } |
| 2002 | |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 2003 | /* If we can't write, wakeup the pending write signals. */ |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 2004 | if (channel_output_closed(si_ic(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 2005 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 2006 | |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 2007 | /* If we can't read, wakeup the pending read signals. */ |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 2008 | if (channel_input_closed(si_oc(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 2009 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 2010 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2011 | /* if the connection is not established, inform the stream that we want |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 2012 | * to be notified whenever the connection completes. |
| 2013 | */ |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2014 | if (si_opposite(si)->state < SI_ST_EST) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 2015 | si_cant_get(si); |
Willy Tarreau | 12c2423 | 2018-12-06 15:29:50 +0100 | [diff] [blame] | 2016 | si_rx_conn_blk(si); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 2017 | si_rx_endp_more(si); |
Willy Tarreau | d4da196 | 2015-04-20 01:31:23 +0200 | [diff] [blame] | 2018 | return; |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 2019 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2020 | |
| 2021 | /* This function is called after the connect. */ |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2022 | appctx->ctx.hlua_cosocket.connected = 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2023 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2024 | /* 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] | 2025 | if (channel_may_recv(si_ic(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 2026 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2027 | |
| 2028 | /* Wake the tasks which wants to read if the buffer contains data. */ |
Christopher Faulet | 174144c | 2024-02-16 15:33:44 +0100 | [diff] [blame] | 2029 | if (!channel_is_empty(si_oc(si))) { |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 2030 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
Christopher Faulet | 174144c | 2024-02-16 15:33:44 +0100 | [diff] [blame] | 2031 | si_cant_get(si); |
| 2032 | } |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 2033 | |
| 2034 | /* Some data were injected in the buffer, notify the stream |
| 2035 | * interface. |
| 2036 | */ |
| 2037 | if (!channel_is_empty(si_ic(si))) |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 2038 | si_update(si); |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 2039 | |
| 2040 | /* If write notifications are registered, we considers we want |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 2041 | * to write, so we clear the blocking flag. |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 2042 | */ |
| 2043 | if (notification_registered(&appctx->ctx.hlua_cosocket.wake_on_write)) |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 2044 | si_rx_endp_more(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2045 | } |
| 2046 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2047 | /* This function is called when the "struct stream" is destroyed. |
| 2048 | * Remove the link from the object to this stream. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2049 | * Wake all the pending signals. |
| 2050 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 2051 | static void hlua_socket_release(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2052 | { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2053 | struct xref *peer; |
| 2054 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2055 | /* Remove my link in the original object. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2056 | peer = xref_get_peer_and_lock(&appctx->ctx.hlua_cosocket.xref); |
| 2057 | if (peer) |
| 2058 | xref_disconnect(&appctx->ctx.hlua_cosocket.xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2059 | |
| 2060 | /* Wake all the task waiting for me. */ |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 2061 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 2062 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2063 | } |
| 2064 | |
| 2065 | /* If the garbage collectio of the object is launch, nobody |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2066 | * uses this object. If the stream does not exists, just quit. |
| 2067 | * Send the shutdown signal to the stream. In some cases, |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2068 | * pending signal can rest in the read and write lists. destroy |
| 2069 | * it. |
| 2070 | */ |
| 2071 | __LJMP static int hlua_socket_gc(lua_State *L) |
| 2072 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2073 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2074 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2075 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2076 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2077 | MAY_LJMP(check_args(L, 1, "__gc")); |
| 2078 | |
| 2079 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2080 | peer = xref_get_peer_and_lock(&socket->xref); |
| 2081 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2082 | return 0; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2083 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2084 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2085 | /* Set the flag which destroy the session. */ |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 2086 | appctx->ctx.hlua_cosocket.die = 1; |
| 2087 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2088 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2089 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2090 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2091 | return 0; |
| 2092 | } |
| 2093 | |
| 2094 | /* The close function send shutdown signal and break the |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2095 | * links between the stream and the object. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2096 | */ |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 2097 | __LJMP static int hlua_socket_close_helper(lua_State *L) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2098 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2099 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2100 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2101 | struct xref *peer; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2102 | struct hlua *hlua; |
| 2103 | |
| 2104 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 2105 | hlua = hlua_gethlua(L); |
| 2106 | if (!hlua) |
| 2107 | return 0; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2108 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2109 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2110 | |
| 2111 | /* Check if we run on the same thread than the xreator thread. |
| 2112 | * We cannot access to the socket if the thread is different. |
| 2113 | */ |
| 2114 | if (socket->tid != tid) |
| 2115 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2116 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2117 | peer = xref_get_peer_and_lock(&socket->xref); |
| 2118 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2119 | return 0; |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 2120 | |
| 2121 | hlua->gc_count--; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2122 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2123 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2124 | /* Set the flag which destroy the session. */ |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 2125 | appctx->ctx.hlua_cosocket.die = 1; |
| 2126 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2127 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2128 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2129 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2130 | return 0; |
| 2131 | } |
| 2132 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 2133 | /* The close function calls close_helper. |
| 2134 | */ |
| 2135 | __LJMP static int hlua_socket_close(lua_State *L) |
| 2136 | { |
| 2137 | MAY_LJMP(check_args(L, 1, "close")); |
| 2138 | return hlua_socket_close_helper(L); |
| 2139 | } |
| 2140 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2141 | /* This Lua function assumes that the stack contain three parameters. |
| 2142 | * 1 - USERDATA containing a struct socket |
| 2143 | * 2 - INTEGER with values of the macro defined below |
| 2144 | * If the integer is -1, we must read at most one line. |
| 2145 | * If the integer is -2, we ust read all the data until the |
| 2146 | * end of the stream. |
| 2147 | * If the integer is positive value, we must read a number of |
| 2148 | * bytes corresponding to this value. |
| 2149 | */ |
| 2150 | #define HLSR_READ_LINE (-1) |
| 2151 | #define HLSR_READ_ALL (-2) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2152 | __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] | 2153 | { |
| 2154 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2155 | int wanted = lua_tointeger(L, 2); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2156 | struct hlua *hlua; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2157 | struct appctx *appctx; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2158 | size_t len; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2159 | int nblk; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 2160 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2161 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 2162 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2163 | size_t len2; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 2164 | int skip_at_end = 0; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 2165 | struct channel *oc; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2166 | struct stream_interface *si; |
| 2167 | struct stream *s; |
| 2168 | struct xref *peer; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 2169 | int missing_bytes; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2170 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2171 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 2172 | hlua = hlua_gethlua(L); |
| 2173 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2174 | /* Check if this lua stack is schedulable. */ |
| 2175 | if (!hlua || !hlua->task) |
| 2176 | WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in " |
| 2177 | "'frontend', 'backend' or 'task'")); |
| 2178 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2179 | /* Check if we run on the same thread than the xreator thread. |
| 2180 | * We cannot access to the socket if the thread is different. |
| 2181 | */ |
| 2182 | if (socket->tid != tid) |
| 2183 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2184 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2185 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2186 | peer = xref_get_peer_and_lock(&socket->xref); |
| 2187 | if (!peer) |
| 2188 | goto no_peer; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2189 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2190 | si = appctx->owner; |
| 2191 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2192 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2193 | oc = &s->res; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2194 | if (wanted == HLSR_READ_LINE) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2195 | /* Read line. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2196 | nblk = co_getline_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2197 | if (nblk < 0) /* Connection close. */ |
| 2198 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2199 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2200 | goto connection_empty; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 2201 | |
| 2202 | /* remove final \r\n. */ |
| 2203 | if (nblk == 1) { |
| 2204 | if (blk1[len1-1] == '\n') { |
| 2205 | len1--; |
| 2206 | skip_at_end++; |
| 2207 | if (blk1[len1-1] == '\r') { |
| 2208 | len1--; |
| 2209 | skip_at_end++; |
| 2210 | } |
| 2211 | } |
| 2212 | } |
| 2213 | else { |
| 2214 | if (blk2[len2-1] == '\n') { |
| 2215 | len2--; |
| 2216 | skip_at_end++; |
| 2217 | if (blk2[len2-1] == '\r') { |
| 2218 | len2--; |
| 2219 | skip_at_end++; |
| 2220 | } |
| 2221 | } |
| 2222 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2223 | } |
| 2224 | |
| 2225 | else if (wanted == HLSR_READ_ALL) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2226 | /* Read all the available data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2227 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2228 | if (nblk < 0) /* Connection close. */ |
| 2229 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2230 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2231 | goto connection_empty; |
| 2232 | } |
| 2233 | |
| 2234 | else { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2235 | /* Read a block of data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2236 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2237 | if (nblk < 0) /* Connection close. */ |
| 2238 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2239 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2240 | goto connection_empty; |
| 2241 | |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 2242 | missing_bytes = wanted - socket->b.n; |
| 2243 | if (len1 > missing_bytes) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2244 | nblk = 1; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 2245 | len1 = missing_bytes; |
| 2246 | } if (nblk == 2 && len1 + len2 > missing_bytes) |
| 2247 | len2 = missing_bytes - len1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2248 | } |
| 2249 | |
| 2250 | len = len1; |
| 2251 | |
| 2252 | luaL_addlstring(&socket->b, blk1, len1); |
| 2253 | if (nblk == 2) { |
| 2254 | len += len2; |
| 2255 | luaL_addlstring(&socket->b, blk2, len2); |
| 2256 | } |
| 2257 | |
| 2258 | /* Consume data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2259 | co_skip(oc, len + skip_at_end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2260 | |
| 2261 | /* Don't wait anything. */ |
Christopher Faulet | 174144c | 2024-02-16 15:33:44 +0100 | [diff] [blame] | 2262 | si_want_get(si); |
Thierry FOURNIER | 7e4ee47 | 2018-05-25 15:03:50 +0200 | [diff] [blame] | 2263 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2264 | |
| 2265 | /* If the pattern reclaim to read all the data |
| 2266 | * in the connection, got out. |
| 2267 | */ |
| 2268 | if (wanted == HLSR_READ_ALL) |
| 2269 | goto connection_empty; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 2270 | else if (wanted >= 0 && socket->b.n < wanted) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2271 | goto connection_empty; |
| 2272 | |
| 2273 | /* Return result. */ |
| 2274 | luaL_pushresult(&socket->b); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2275 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2276 | return 1; |
| 2277 | |
| 2278 | connection_closed: |
| 2279 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2280 | xref_unlock(&socket->xref, peer); |
| 2281 | |
| 2282 | no_peer: |
| 2283 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2284 | /* If the buffer containds data. */ |
| 2285 | if (socket->b.n > 0) { |
| 2286 | luaL_pushresult(&socket->b); |
| 2287 | return 1; |
| 2288 | } |
| 2289 | lua_pushnil(L); |
| 2290 | lua_pushstring(L, "connection closed."); |
| 2291 | return 2; |
| 2292 | |
| 2293 | connection_empty: |
| 2294 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2295 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_read, hlua->task)) { |
| 2296 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2297 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2298 | } |
| 2299 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2300 | 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] | 2301 | return 0; |
| 2302 | } |
| 2303 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2304 | /* This Lua function gets two parameters. The first one can be string |
| 2305 | * or a number. If the string is "*l", the user requires one line. If |
| 2306 | * 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] | 2307 | * If the value is a number, the user require a number of bytes equal |
| 2308 | * to the value. The default value is "*l" (a line). |
| 2309 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2310 | * This parameter with a variable type is converted in integer. This |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2311 | * integer takes this values: |
| 2312 | * -1 : read a line |
| 2313 | * -2 : read all the stream |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2314 | * >0 : amount of bytes. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2315 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2316 | * The second parameter is optional. It contains a string that must be |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2317 | * concatenated with the read data. |
| 2318 | */ |
| 2319 | __LJMP static int hlua_socket_receive(struct lua_State *L) |
| 2320 | { |
| 2321 | int wanted = HLSR_READ_LINE; |
| 2322 | const char *pattern; |
Christopher Faulet | c31b200 | 2021-05-03 10:11:13 +0200 | [diff] [blame] | 2323 | int lastarg, type; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2324 | char *error; |
| 2325 | size_t len; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2326 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2327 | |
| 2328 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 2329 | WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments.")); |
| 2330 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2331 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2332 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2333 | /* Check if we run on the same thread than the xreator thread. |
| 2334 | * We cannot access to the socket if the thread is different. |
| 2335 | */ |
| 2336 | if (socket->tid != tid) |
| 2337 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2338 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2339 | /* check for pattern. */ |
| 2340 | if (lua_gettop(L) >= 2) { |
| 2341 | type = lua_type(L, 2); |
| 2342 | if (type == LUA_TSTRING) { |
| 2343 | pattern = lua_tostring(L, 2); |
| 2344 | if (strcmp(pattern, "*a") == 0) |
| 2345 | wanted = HLSR_READ_ALL; |
| 2346 | else if (strcmp(pattern, "*l") == 0) |
| 2347 | wanted = HLSR_READ_LINE; |
| 2348 | else { |
| 2349 | wanted = strtoll(pattern, &error, 10); |
| 2350 | if (*error != '\0') |
| 2351 | WILL_LJMP(luaL_error(L, "Unsupported pattern.")); |
| 2352 | } |
| 2353 | } |
| 2354 | else if (type == LUA_TNUMBER) { |
| 2355 | wanted = lua_tointeger(L, 2); |
| 2356 | if (wanted < 0) |
| 2357 | WILL_LJMP(luaL_error(L, "Unsupported size.")); |
| 2358 | } |
| 2359 | } |
| 2360 | |
| 2361 | /* Set pattern. */ |
| 2362 | lua_pushinteger(L, wanted); |
Tim Duesterhus | c6e377e | 2018-01-04 19:32:13 +0100 | [diff] [blame] | 2363 | |
| 2364 | /* Check if we would replace the top by itself. */ |
| 2365 | if (lua_gettop(L) != 2) |
| 2366 | lua_replace(L, 2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2367 | |
Christopher Faulet | c31b200 | 2021-05-03 10:11:13 +0200 | [diff] [blame] | 2368 | /* Save index of the top of the stack because since buffers are used, it |
| 2369 | * may change |
| 2370 | */ |
| 2371 | lastarg = lua_gettop(L); |
| 2372 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2373 | /* init buffer, and fill it with prefix. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2374 | luaL_buffinit(L, &socket->b); |
| 2375 | |
| 2376 | /* Check prefix. */ |
Christopher Faulet | c31b200 | 2021-05-03 10:11:13 +0200 | [diff] [blame] | 2377 | if (lastarg >= 3) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2378 | if (lua_type(L, 3) != LUA_TSTRING) |
| 2379 | WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix")); |
| 2380 | pattern = lua_tolstring(L, 3, &len); |
| 2381 | luaL_addlstring(&socket->b, pattern, len); |
| 2382 | } |
| 2383 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2384 | return __LJMP(hlua_socket_receive_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2385 | } |
| 2386 | |
| 2387 | /* Write the Lua input string in the output buffer. |
Mark Lakes | 22154b4 | 2018-01-29 14:38:40 -0800 | [diff] [blame] | 2388 | * This function returns a yield if no space is available. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2389 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2390 | 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] | 2391 | { |
| 2392 | struct hlua_socket *socket; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2393 | struct hlua *hlua; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2394 | struct appctx *appctx; |
| 2395 | size_t buf_len; |
| 2396 | const char *buf; |
| 2397 | int len; |
| 2398 | int send_len; |
| 2399 | int sent; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2400 | struct xref *peer; |
| 2401 | struct stream_interface *si; |
| 2402 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2403 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2404 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 2405 | hlua = hlua_gethlua(L); |
| 2406 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2407 | /* Check if this lua stack is schedulable. */ |
| 2408 | if (!hlua || !hlua->task) |
| 2409 | WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in " |
| 2410 | "'frontend', 'backend' or 'task'")); |
| 2411 | |
| 2412 | /* Get object */ |
| 2413 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2414 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2415 | sent = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2416 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2417 | /* Check if we run on the same thread than the xreator thread. |
| 2418 | * We cannot access to the socket if the thread is different. |
| 2419 | */ |
| 2420 | if (socket->tid != tid) |
| 2421 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2422 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2423 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2424 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2425 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2426 | lua_pushinteger(L, -1); |
| 2427 | return 1; |
| 2428 | } |
| 2429 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2430 | si = appctx->owner; |
| 2431 | s = si_strm(si); |
| 2432 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2433 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2434 | if (channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2435 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2436 | lua_pushinteger(L, -1); |
| 2437 | return 1; |
| 2438 | } |
| 2439 | |
| 2440 | /* Update the input buffer data. */ |
| 2441 | buf += sent; |
| 2442 | send_len = buf_len - sent; |
| 2443 | |
| 2444 | /* All the data are sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2445 | if (sent >= buf_len) { |
| 2446 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2447 | return 1; /* Implicitly return the length sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2448 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2449 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2450 | /* Check if the buffer is available because HAProxy doesn't allocate |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 2451 | * the request buffer if its not required. |
| 2452 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2453 | if (s->req.buf.size == 0) { |
Willy Tarreau | 581abd3 | 2018-10-25 10:21:41 +0200 | [diff] [blame] | 2454 | if (!si_alloc_ibuf(si, &appctx->buffer_wait)) |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2455 | goto hlua_socket_write_yield_return; |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 2456 | } |
| 2457 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2458 | /* Check for available space. */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2459 | len = b_room(&s->req.buf); |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2460 | if (len <= 0) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2461 | goto hlua_socket_write_yield_return; |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2462 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2463 | |
| 2464 | /* send data */ |
| 2465 | if (len < send_len) |
| 2466 | send_len = len; |
Thierry FOURNIER | 66b8919 | 2018-05-27 01:14:47 +0200 | [diff] [blame] | 2467 | len = ci_putblk(&s->req, buf, send_len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2468 | |
| 2469 | /* "Not enough space" (-1), "Buffer too little to contain |
| 2470 | * the data" (-2) are not expected because the available length |
| 2471 | * is tested. |
| 2472 | * Other unknown error are also not expected. |
| 2473 | */ |
| 2474 | if (len <= 0) { |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2475 | if (len == -1) |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2476 | s->req.flags |= CF_WAKE_WRITE; |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2477 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 2478 | MAY_LJMP(hlua_socket_close_helper(L)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2479 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2480 | lua_pushinteger(L, -1); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2481 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2482 | return 1; |
| 2483 | } |
| 2484 | |
| 2485 | /* update buffers. */ |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 2486 | appctx_wakeup(appctx); |
Willy Tarreau | de70fa1 | 2015-09-26 11:25:05 +0200 | [diff] [blame] | 2487 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2488 | s->req.rex = TICK_ETERNITY; |
| 2489 | s->res.wex = TICK_ETERNITY; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2490 | |
| 2491 | /* Update length sent. */ |
| 2492 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2493 | lua_pushinteger(L, sent + len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2494 | |
| 2495 | /* All the data buffer is sent ? */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2496 | if (sent + len >= buf_len) { |
| 2497 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2498 | return 1; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2499 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2500 | |
| 2501 | hlua_socket_write_yield_return: |
Thierry FOURNIER | ba42fcd | 2018-05-27 00:59:48 +0200 | [diff] [blame] | 2502 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2503 | xref_unlock(&socket->xref, peer); |
| 2504 | WILL_LJMP(luaL_error(L, "out of memory")); |
| 2505 | } |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2506 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2507 | 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] | 2508 | return 0; |
| 2509 | } |
| 2510 | |
| 2511 | /* This function initiate the send of data. It just check the input |
| 2512 | * parameters and push an integer in the Lua stack that contain the |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2513 | * 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] | 2514 | * "hlua_socket_write_yield" that can yield. |
| 2515 | * |
| 2516 | * The Lua function gets between 3 and 4 parameters. The first one is |
| 2517 | * the associated object. The second is a string buffer. The third is |
| 2518 | * a facultative integer that represents where is the buffer position |
| 2519 | * of the start of the data that can send. The first byte is the |
| 2520 | * position "1". The default value is "1". The fourth argument is a |
| 2521 | * facultative integer that represents where is the buffer position |
| 2522 | * of the end of the data that can send. The default is the last byte. |
| 2523 | */ |
| 2524 | static int hlua_socket_send(struct lua_State *L) |
| 2525 | { |
| 2526 | int i; |
| 2527 | int j; |
| 2528 | const char *buf; |
| 2529 | size_t buf_len; |
| 2530 | |
| 2531 | /* Check number of arguments. */ |
| 2532 | if (lua_gettop(L) < 2 || lua_gettop(L) > 4) |
| 2533 | WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments")); |
| 2534 | |
| 2535 | /* Get the string. */ |
| 2536 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
| 2537 | |
| 2538 | /* Get and check j. */ |
| 2539 | if (lua_gettop(L) == 4) { |
| 2540 | j = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 2541 | if (j < 0) |
| 2542 | j = buf_len + j + 1; |
| 2543 | if (j > buf_len) |
| 2544 | j = buf_len + 1; |
| 2545 | lua_pop(L, 1); |
| 2546 | } |
| 2547 | else |
| 2548 | j = buf_len; |
| 2549 | |
| 2550 | /* Get and check i. */ |
| 2551 | if (lua_gettop(L) == 3) { |
| 2552 | i = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2553 | if (i < 0) |
| 2554 | i = buf_len + i + 1; |
| 2555 | if (i > buf_len) |
| 2556 | i = buf_len + 1; |
| 2557 | lua_pop(L, 1); |
| 2558 | } else |
| 2559 | i = 1; |
| 2560 | |
| 2561 | /* Check bth i and j. */ |
| 2562 | if (i > j) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2563 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2564 | return 1; |
| 2565 | } |
| 2566 | if (i == 0 && j == 0) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2567 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2568 | return 1; |
| 2569 | } |
| 2570 | if (i == 0) |
| 2571 | i = 1; |
| 2572 | if (j == 0) |
| 2573 | j = 1; |
| 2574 | |
| 2575 | /* Pop the string. */ |
| 2576 | lua_pop(L, 1); |
| 2577 | |
| 2578 | /* Update the buffer length. */ |
| 2579 | buf += i - 1; |
| 2580 | buf_len = j - i + 1; |
| 2581 | lua_pushlstring(L, buf, buf_len); |
| 2582 | |
| 2583 | /* This unsigned is used to remember the amount of sent data. */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2584 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2585 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2586 | return MAY_LJMP(hlua_socket_write_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2587 | } |
| 2588 | |
Willy Tarreau | 22b0a68 | 2015-06-17 19:43:49 +0200 | [diff] [blame] | 2589 | #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] | 2590 | __LJMP static inline int hlua_socket_info(struct lua_State *L, struct sockaddr_storage *addr) |
| 2591 | { |
| 2592 | static char buffer[SOCKET_INFO_MAX_LEN]; |
| 2593 | int ret; |
| 2594 | int len; |
| 2595 | char *p; |
| 2596 | |
| 2597 | ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1); |
| 2598 | if (ret <= 0) { |
| 2599 | lua_pushnil(L); |
| 2600 | return 1; |
| 2601 | } |
| 2602 | |
| 2603 | if (ret == AF_UNIX) { |
| 2604 | lua_pushstring(L, buffer+1); |
| 2605 | return 1; |
| 2606 | } |
| 2607 | else if (ret == AF_INET6) { |
| 2608 | buffer[0] = '['; |
| 2609 | len = strlen(buffer); |
| 2610 | buffer[len] = ']'; |
| 2611 | len++; |
| 2612 | buffer[len] = ':'; |
| 2613 | len++; |
| 2614 | p = buffer; |
| 2615 | } |
| 2616 | else if (ret == AF_INET) { |
| 2617 | p = buffer + 1; |
| 2618 | len = strlen(p); |
| 2619 | p[len] = ':'; |
| 2620 | len++; |
| 2621 | } |
| 2622 | else { |
| 2623 | lua_pushnil(L); |
| 2624 | return 1; |
| 2625 | } |
| 2626 | |
| 2627 | if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) { |
| 2628 | lua_pushnil(L); |
| 2629 | return 1; |
| 2630 | } |
| 2631 | |
| 2632 | lua_pushstring(L, p); |
| 2633 | return 1; |
| 2634 | } |
| 2635 | |
| 2636 | /* Returns information about the peer of the connection. */ |
| 2637 | __LJMP static int hlua_socket_getpeername(struct lua_State *L) |
| 2638 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2639 | struct hlua_socket *socket; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2640 | struct xref *peer; |
| 2641 | struct appctx *appctx; |
| 2642 | struct stream_interface *si; |
| 2643 | struct stream *s; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2644 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2645 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2646 | MAY_LJMP(check_args(L, 1, "getpeername")); |
| 2647 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2648 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2649 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2650 | /* Check if we run on the same thread than the xreator thread. |
| 2651 | * We cannot access to the socket if the thread is different. |
| 2652 | */ |
| 2653 | if (socket->tid != tid) |
| 2654 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2655 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2656 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2657 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2658 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2659 | lua_pushnil(L); |
| 2660 | return 1; |
| 2661 | } |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2662 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2663 | si = appctx->owner; |
| 2664 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2665 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2666 | if (!s->target_addr) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2667 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2668 | lua_pushnil(L); |
| 2669 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2670 | } |
| 2671 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2672 | ret = MAY_LJMP(hlua_socket_info(L, s->target_addr)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2673 | xref_unlock(&socket->xref, peer); |
| 2674 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2675 | } |
| 2676 | |
| 2677 | /* Returns information about my connection side. */ |
| 2678 | static int hlua_socket_getsockname(struct lua_State *L) |
| 2679 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2680 | struct hlua_socket *socket; |
| 2681 | struct connection *conn; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2682 | struct appctx *appctx; |
| 2683 | struct xref *peer; |
| 2684 | struct stream_interface *si; |
| 2685 | struct stream *s; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2686 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2687 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2688 | MAY_LJMP(check_args(L, 1, "getsockname")); |
| 2689 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2690 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2691 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2692 | /* Check if we run on the same thread than the xreator thread. |
| 2693 | * We cannot access to the socket if the thread is different. |
| 2694 | */ |
| 2695 | if (socket->tid != tid) |
| 2696 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2697 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2698 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2699 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2700 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2701 | lua_pushnil(L); |
| 2702 | return 1; |
| 2703 | } |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2704 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2705 | si = appctx->owner; |
| 2706 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2707 | |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 2708 | conn = cs_conn(objt_cs(s->si[1].end)); |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2709 | if (!conn || !conn_get_src(conn)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2710 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2711 | lua_pushnil(L); |
| 2712 | return 1; |
| 2713 | } |
| 2714 | |
Willy Tarreau | 9da9a6f | 2019-07-17 14:49:44 +0200 | [diff] [blame] | 2715 | ret = hlua_socket_info(L, conn->src); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2716 | xref_unlock(&socket->xref, peer); |
| 2717 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2718 | } |
| 2719 | |
| 2720 | /* This struct define the applet. */ |
Willy Tarreau | 3057645 | 2015-04-13 13:50:30 +0200 | [diff] [blame] | 2721 | static struct applet update_applet = { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2722 | .obj_type = OBJ_TYPE_APPLET, |
| 2723 | .name = "<LUA_TCP>", |
| 2724 | .fct = hlua_socket_handler, |
| 2725 | .release = hlua_socket_release, |
| 2726 | }; |
| 2727 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2728 | __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] | 2729 | { |
| 2730 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2731 | struct hlua *hlua; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2732 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2733 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2734 | struct stream_interface *si; |
| 2735 | struct stream *s; |
| 2736 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2737 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 2738 | hlua = hlua_gethlua(L); |
| 2739 | if (!hlua) |
| 2740 | return 0; |
| 2741 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2742 | /* Check if we run on the same thread than the xreator thread. |
| 2743 | * We cannot access to the socket if the thread is different. |
| 2744 | */ |
| 2745 | if (socket->tid != tid) |
| 2746 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2747 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2748 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2749 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2750 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2751 | lua_pushnil(L); |
| 2752 | lua_pushstring(L, "Can't connect"); |
| 2753 | return 2; |
| 2754 | } |
| 2755 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2756 | si = appctx->owner; |
| 2757 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2758 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2759 | /* Check if we run on the same thread than the xreator thread. |
| 2760 | * We cannot access to the socket if the thread is different. |
| 2761 | */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2762 | if (socket->tid != tid) { |
| 2763 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2764 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2765 | } |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2766 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2767 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2768 | if (!hlua || channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2769 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2770 | lua_pushnil(L); |
| 2771 | lua_pushstring(L, "Can't connect"); |
| 2772 | return 2; |
| 2773 | } |
| 2774 | |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 2775 | appctx = __objt_appctx(s->si[0].end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2776 | |
| 2777 | /* Check for connection established. */ |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2778 | if (appctx->ctx.hlua_cosocket.connected) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2779 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2780 | lua_pushinteger(L, 1); |
| 2781 | return 1; |
| 2782 | } |
| 2783 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2784 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2785 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2786 | WILL_LJMP(luaL_error(L, "out of memory error")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2787 | } |
| 2788 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2789 | 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] | 2790 | return 0; |
| 2791 | } |
| 2792 | |
| 2793 | /* This function fail or initite the connection. */ |
| 2794 | __LJMP static int hlua_socket_connect(struct lua_State *L) |
| 2795 | { |
| 2796 | struct hlua_socket *socket; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2797 | int port = -1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2798 | const char *ip; |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2799 | struct hlua *hlua; |
| 2800 | struct appctx *appctx; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2801 | int low, high; |
| 2802 | struct sockaddr_storage *addr; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2803 | struct xref *peer; |
| 2804 | struct stream_interface *si; |
| 2805 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2806 | |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2807 | if (lua_gettop(L) < 2) |
| 2808 | WILL_LJMP(luaL_error(L, "connect: need at least 2 arguments")); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2809 | |
| 2810 | /* Get args. */ |
| 2811 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2812 | |
| 2813 | /* Check if we run on the same thread than the xreator thread. |
| 2814 | * We cannot access to the socket if the thread is different. |
| 2815 | */ |
| 2816 | if (socket->tid != tid) |
| 2817 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2818 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2819 | ip = MAY_LJMP(luaL_checkstring(L, 2)); |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 2820 | if (lua_gettop(L) >= 3) { |
| 2821 | luaL_Buffer b; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2822 | port = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2823 | |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 2824 | /* Force the ip to end with a colon, to support IPv6 addresses |
| 2825 | * that are not enclosed within square brackets. |
| 2826 | */ |
| 2827 | if (port > 0) { |
| 2828 | luaL_buffinit(L, &b); |
| 2829 | luaL_addstring(&b, ip); |
| 2830 | luaL_addchar(&b, ':'); |
| 2831 | luaL_pushresult(&b); |
| 2832 | ip = lua_tolstring(L, lua_gettop(L), NULL); |
| 2833 | } |
| 2834 | } |
| 2835 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2836 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2837 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2838 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2839 | lua_pushnil(L); |
| 2840 | return 1; |
| 2841 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2842 | |
| 2843 | /* Parse ip address. */ |
Willy Tarreau | 5fc9328 | 2020-09-16 18:25:03 +0200 | [diff] [blame] | 2844 | 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] | 2845 | if (!addr) { |
| 2846 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2847 | WILL_LJMP(luaL_error(L, "connect: cannot parse destination address '%s'", ip)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2848 | } |
Willy Tarreau | 9da9a6f | 2019-07-17 14:49:44 +0200 | [diff] [blame] | 2849 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2850 | /* Set port. */ |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2851 | if (low == 0) { |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2852 | if (addr->ss_family == AF_INET) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2853 | if (port == -1) { |
| 2854 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2855 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2856 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2857 | ((struct sockaddr_in *)addr)->sin_port = htons(port); |
| 2858 | } else if (addr->ss_family == AF_INET6) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2859 | if (port == -1) { |
| 2860 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2861 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2862 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2863 | ((struct sockaddr_in6 *)addr)->sin6_port = htons(port); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2864 | } |
| 2865 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2866 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2867 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2868 | si = appctx->owner; |
| 2869 | s = si_strm(si); |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2870 | |
Willy Tarreau | 9b7587a | 2020-10-15 07:32:10 +0200 | [diff] [blame] | 2871 | if (!sockaddr_alloc(&s->target_addr, addr, sizeof(*addr))) { |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2872 | xref_unlock(&socket->xref, peer); |
| 2873 | WILL_LJMP(luaL_error(L, "connect: internal error")); |
| 2874 | } |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2875 | s->flags |= SF_ADDR_SET; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2876 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2877 | /* Get hlua struct, or NULL if we execute from main lua state */ |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2878 | hlua = hlua_gethlua(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2879 | if (!hlua) |
| 2880 | return 0; |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2881 | |
| 2882 | /* inform the stream that we want to be notified whenever the |
| 2883 | * connection completes. |
| 2884 | */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 2885 | si_cant_get(&s->si[0]); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 2886 | si_rx_endp_more(&s->si[0]); |
Thierry FOURNIER | 8c8fbbe | 2015-09-26 17:02:35 +0200 | [diff] [blame] | 2887 | appctx_wakeup(appctx); |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2888 | |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 2889 | hlua->gc_count++; |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 2890 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2891 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2892 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2893 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2894 | } |
| 2895 | xref_unlock(&socket->xref, peer); |
PiBa-NL | 706d5ee | 2018-05-05 23:51:42 +0200 | [diff] [blame] | 2896 | |
| 2897 | task_wakeup(s->task, TASK_WOKEN_INIT); |
| 2898 | /* Return yield waiting for connection. */ |
| 2899 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2900 | 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] | 2901 | |
| 2902 | return 0; |
| 2903 | } |
| 2904 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2905 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2906 | __LJMP static int hlua_socket_connect_ssl(struct lua_State *L) |
| 2907 | { |
| 2908 | struct hlua_socket *socket; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2909 | struct xref *peer; |
| 2910 | struct appctx *appctx; |
| 2911 | struct stream_interface *si; |
| 2912 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2913 | |
| 2914 | MAY_LJMP(check_args(L, 3, "connect_ssl")); |
| 2915 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2916 | |
| 2917 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2918 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2919 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2920 | lua_pushnil(L); |
| 2921 | return 1; |
| 2922 | } |
| 2923 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2924 | si = appctx->owner; |
| 2925 | s = si_strm(si); |
| 2926 | |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 2927 | s->target = &socket_ssl->obj_type; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2928 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2929 | return MAY_LJMP(hlua_socket_connect(L)); |
| 2930 | } |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2931 | #endif |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2932 | |
| 2933 | __LJMP static int hlua_socket_setoption(struct lua_State *L) |
| 2934 | { |
| 2935 | return 0; |
| 2936 | } |
| 2937 | |
| 2938 | __LJMP static int hlua_socket_settimeout(struct lua_State *L) |
| 2939 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2940 | struct hlua_socket *socket; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2941 | int tmout; |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2942 | double dtmout; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2943 | struct xref *peer; |
| 2944 | struct appctx *appctx; |
| 2945 | struct stream_interface *si; |
| 2946 | struct stream *s; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2947 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2948 | MAY_LJMP(check_args(L, 2, "settimeout")); |
| 2949 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2950 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2951 | |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2952 | /* convert the timeout to millis */ |
| 2953 | dtmout = MAY_LJMP(luaL_checknumber(L, 2)) * 1000; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2954 | |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 2955 | /* Check for negative values */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2956 | if (dtmout < 0) |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 2957 | WILL_LJMP(luaL_error(L, "settimeout: cannot set negatives values")); |
| 2958 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2959 | if (dtmout > INT_MAX) /* overflow check */ |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2960 | 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] | 2961 | |
| 2962 | tmout = MS_TO_TICKS((int)dtmout); |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2963 | if (tmout == 0) |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 2964 | tmout++; /* very small timeouts are adjusted to a minimum of 1ms */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2965 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2966 | /* Check if we run on the same thread than the xreator thread. |
| 2967 | * We cannot access to the socket if the thread is different. |
| 2968 | */ |
| 2969 | if (socket->tid != tid) |
| 2970 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2971 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2972 | /* check for connection break. If some data were read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2973 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2974 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2975 | hlua_pusherror(L, "socket: not yet initialised, you can't set timeouts."); |
| 2976 | WILL_LJMP(lua_error(L)); |
| 2977 | return 0; |
| 2978 | } |
| 2979 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2980 | si = appctx->owner; |
| 2981 | s = si_strm(si); |
| 2982 | |
Cyril Bonté | 7bb6345 | 2018-08-17 23:51:02 +0200 | [diff] [blame] | 2983 | s->sess->fe->timeout.connect = tmout; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2984 | s->req.rto = tmout; |
| 2985 | s->req.wto = tmout; |
| 2986 | s->res.rto = tmout; |
| 2987 | s->res.wto = tmout; |
Cyril Bonté | 7bb6345 | 2018-08-17 23:51:02 +0200 | [diff] [blame] | 2988 | s->req.rex = tick_add_ifset(now_ms, tmout); |
| 2989 | s->req.wex = tick_add_ifset(now_ms, tmout); |
| 2990 | s->res.rex = tick_add_ifset(now_ms, tmout); |
| 2991 | s->res.wex = tick_add_ifset(now_ms, tmout); |
| 2992 | |
| 2993 | s->task->expire = tick_add_ifset(now_ms, tmout); |
| 2994 | task_queue(s->task); |
| 2995 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2996 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2997 | |
Thierry Fournier | e9636f1 | 2018-03-08 09:54:32 +0100 | [diff] [blame] | 2998 | lua_pushinteger(L, 1); |
Tim Duesterhus | 119a5f1 | 2018-01-06 19:16:25 +0100 | [diff] [blame] | 2999 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3000 | } |
| 3001 | |
| 3002 | __LJMP static int hlua_socket_new(lua_State *L) |
| 3003 | { |
| 3004 | struct hlua_socket *socket; |
| 3005 | struct appctx *appctx; |
Willy Tarreau | 15b5e14 | 2015-04-04 14:38:25 +0200 | [diff] [blame] | 3006 | struct session *sess; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 3007 | struct stream *strm; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3008 | |
| 3009 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 3010 | if (!lua_checkstack(L, 3)) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3011 | hlua_pusherror(L, "socket: full stack"); |
| 3012 | goto out_fail_conf; |
| 3013 | } |
| 3014 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 3015 | /* Create the object: obj[0] = userdata. */ |
| 3016 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3017 | socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket))); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 3018 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3019 | memset(socket, 0, sizeof(*socket)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 3020 | socket->tid = tid; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3021 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3022 | /* Check if the various memory pools are initialized. */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 3023 | if (!pool_head_stream || !pool_head_buffer) { |
Thierry FOURNIER | 4a6170c | 2015-03-09 17:07:10 +0100 | [diff] [blame] | 3024 | hlua_pusherror(L, "socket: uninitialized pools."); |
| 3025 | goto out_fail_conf; |
| 3026 | } |
| 3027 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3028 | /* Pop a class stream metatable and affect it to the userdata. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3029 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref); |
| 3030 | lua_setmetatable(L, -2); |
| 3031 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 3032 | /* Create the applet context */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 3033 | appctx = appctx_new(&update_applet, tid_bit); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 3034 | if (!appctx) { |
| 3035 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | feb7640 | 2015-04-03 14:10:06 +0200 | [diff] [blame] | 3036 | goto out_fail_conf; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 3037 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3038 | |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 3039 | appctx->ctx.hlua_cosocket.connected = 0; |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 3040 | appctx->ctx.hlua_cosocket.die = 0; |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 3041 | LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_write); |
| 3042 | LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_read); |
Willy Tarreau | b2bf833 | 2015-04-04 15:58:58 +0200 | [diff] [blame] | 3043 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 3044 | /* Now create a session, task and stream for this applet */ |
Amaury Denoyelle | 239fdbf | 2021-03-24 10:22:03 +0100 | [diff] [blame] | 3045 | sess = session_new(socket_proxy, NULL, &appctx->obj_type); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 3046 | if (!sess) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3047 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 3048 | goto out_fail_sess; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3049 | } |
| 3050 | |
Christopher Faulet | 26256f8 | 2020-09-14 11:40:13 +0200 | [diff] [blame] | 3051 | strm = stream_new(sess, &appctx->obj_type, &BUF_NULL); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 3052 | if (!strm) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3053 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 3054 | goto out_fail_stream; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3055 | } |
| 3056 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 3057 | /* Initialise cross reference between stream and Lua socket object. */ |
| 3058 | xref_create(&socket->xref, &appctx->ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3059 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3060 | /* Configure "right" stream interface. this "si" is used to connect |
| 3061 | * and retrieve data from the server. The connection is initialized |
| 3062 | * with the "struct server". |
| 3063 | */ |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 3064 | si_set_state(&strm->si[1], SI_ST_ASS); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3065 | |
| 3066 | /* Force destination server. */ |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 3067 | strm->flags |= SF_DIRECT | SF_ASSIGNED | SF_BE_ASSIGNED; |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 3068 | strm->target = &socket_tcp->obj_type; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3069 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3070 | return 1; |
| 3071 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 3072 | out_fail_stream: |
Willy Tarreau | 11c3624 | 2015-04-04 15:54:03 +0200 | [diff] [blame] | 3073 | session_free(sess); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 3074 | out_fail_sess: |
| 3075 | appctx_free(appctx); |
| 3076 | out_fail_conf: |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3077 | WILL_LJMP(lua_error(L)); |
| 3078 | return 0; |
| 3079 | } |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 3080 | |
| 3081 | /* |
| 3082 | * |
| 3083 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3084 | * Class Channel |
| 3085 | * |
| 3086 | * |
| 3087 | */ |
| 3088 | |
| 3089 | /* Returns the struct hlua_channel join to the class channel in the |
| 3090 | * stack entry "ud" or throws an argument error. |
| 3091 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3092 | __LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3093 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3094 | return MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3095 | } |
| 3096 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3097 | /* Pushes the channel onto the top of the stack. If the stask does not have a |
| 3098 | * free slots, the function fails and returns 0; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3099 | */ |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 3100 | static int hlua_channel_new(lua_State *L, struct channel *channel) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3101 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3102 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 3103 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3104 | return 0; |
| 3105 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 3106 | lua_newtable(L); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3107 | lua_pushlightuserdata(L, channel); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 3108 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3109 | |
| 3110 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 3111 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref); |
| 3112 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3113 | return 1; |
| 3114 | } |
| 3115 | |
| 3116 | /* Duplicate all the data present in the input channel and put it |
| 3117 | * in a string LUA variables. Returns -1 and push a nil value in |
| 3118 | * the stack if the channel is closed and all the data are consumed, |
| 3119 | * returns 0 if no data are available, otherwise it returns the length |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 3120 | * of the built string. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3121 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3122 | static inline int _hlua_channel_dup(struct channel *chn, lua_State *L) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3123 | { |
| 3124 | char *blk1; |
| 3125 | char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3126 | size_t len1; |
| 3127 | size_t len2; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3128 | int ret; |
| 3129 | luaL_Buffer b; |
| 3130 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3131 | ret = ci_getblk_nc(chn, &blk1, &len1, &blk2, &len2); |
Christopher Faulet | 055310f | 2021-08-05 11:58:37 +0200 | [diff] [blame] | 3132 | if (unlikely(ret <= 0)) { |
| 3133 | if (ret < 0 || HLUA_CANT_YIELD(hlua_gethlua(L))) { |
| 3134 | lua_pushnil(L); |
| 3135 | return -1; |
| 3136 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3137 | return 0; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3138 | } |
| 3139 | |
| 3140 | luaL_buffinit(L, &b); |
| 3141 | luaL_addlstring(&b, blk1, len1); |
| 3142 | if (unlikely(ret == 2)) |
| 3143 | luaL_addlstring(&b, blk2, len2); |
| 3144 | luaL_pushresult(&b); |
| 3145 | |
| 3146 | if (unlikely(ret == 2)) |
| 3147 | return len1 + len2; |
| 3148 | return len1; |
| 3149 | } |
| 3150 | |
| 3151 | /* "_hlua_channel_dup" wrapper. If no data are available, it returns |
| 3152 | * a yield. This function keep the data in the buffer. |
| 3153 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3154 | __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] | 3155 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3156 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3157 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3158 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3159 | |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 3160 | if (IS_HTX_STRM(chn_strm(chn))) { |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3161 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3162 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3163 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3164 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3165 | if (_hlua_channel_dup(chn, L) == 0) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3166 | 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] | 3167 | return 1; |
| 3168 | } |
| 3169 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3170 | /* Check arguments for the function "hlua_channel_dup_yield". */ |
| 3171 | __LJMP static int hlua_channel_dup(lua_State *L) |
| 3172 | { |
| 3173 | MAY_LJMP(check_args(L, 1, "dup")); |
| 3174 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3175 | return MAY_LJMP(hlua_channel_dup_yield(L, 0, 0)); |
| 3176 | } |
| 3177 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3178 | /* "_hlua_channel_dup" wrapper. If no data are available, it returns |
| 3179 | * a yield. This function consumes the data in the buffer. It returns |
| 3180 | * a string containing the data or a nil pointer if no data are available |
| 3181 | * and the channel is closed. |
| 3182 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3183 | __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] | 3184 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3185 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3186 | int ret; |
| 3187 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3188 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3189 | |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 3190 | if (IS_HTX_STRM(chn_strm(chn))) { |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3191 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3192 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3193 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3194 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3195 | ret = _hlua_channel_dup(chn, L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3196 | if (unlikely(ret == 0)) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3197 | 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] | 3198 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3199 | if (unlikely(ret == -1)) |
| 3200 | return 1; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3201 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3202 | b_sub(&chn->buf, ret); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3203 | return 1; |
| 3204 | } |
| 3205 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3206 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3207 | __LJMP static int hlua_channel_get(lua_State *L) |
| 3208 | { |
| 3209 | MAY_LJMP(check_args(L, 1, "get")); |
| 3210 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3211 | return MAY_LJMP(hlua_channel_get_yield(L, 0, 0)); |
| 3212 | } |
| 3213 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3214 | /* This functions consumes and returns one line. If the channel is closed, |
| 3215 | * 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] | 3216 | * 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] | 3217 | * value. |
| 3218 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3219 | __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] | 3220 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3221 | char *blk1; |
| 3222 | char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3223 | size_t len1; |
| 3224 | size_t len2; |
| 3225 | size_t len; |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3226 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3227 | int ret; |
| 3228 | luaL_Buffer b; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3229 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3230 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3231 | |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 3232 | if (IS_HTX_STRM(chn_strm(chn))) { |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3233 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3234 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3235 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3236 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3237 | ret = ci_getline_nc(chn, &blk1, &len1, &blk2, &len2); |
Christopher Faulet | 055310f | 2021-08-05 11:58:37 +0200 | [diff] [blame] | 3238 | if (ret == 0) { |
| 3239 | if (HLUA_CANT_YIELD(hlua_gethlua(L))) { |
| 3240 | _hlua_channel_dup(chn, L); |
| 3241 | return 1; |
| 3242 | } |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3243 | 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] | 3244 | } |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3245 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3246 | if (ret == -1) { |
| 3247 | lua_pushnil(L); |
| 3248 | return 1; |
| 3249 | } |
| 3250 | |
| 3251 | luaL_buffinit(L, &b); |
| 3252 | luaL_addlstring(&b, blk1, len1); |
| 3253 | len = len1; |
| 3254 | if (unlikely(ret == 2)) { |
| 3255 | luaL_addlstring(&b, blk2, len2); |
| 3256 | len += len2; |
| 3257 | } |
| 3258 | luaL_pushresult(&b); |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3259 | 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] | 3260 | return 1; |
| 3261 | } |
| 3262 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3263 | /* Check arguments for the function "hlua_channel_getline_yield". */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3264 | __LJMP static int hlua_channel_getline(lua_State *L) |
| 3265 | { |
| 3266 | MAY_LJMP(check_args(L, 1, "getline")); |
| 3267 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3268 | return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0)); |
| 3269 | } |
| 3270 | |
Christopher Faulet | 3b7bc3e | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3271 | /* This function takes a string as argument, and append it at the input side of |
| 3272 | * channel. If data cannot be copied, because there is not enough space to do so |
| 3273 | * or because the channel is closed, it returns -1. Otherwise, it returns the |
| 3274 | * amount of data written (the string length). This function does not yield. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3275 | */ |
Christopher Faulet | 3b7bc3e | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3276 | __LJMP static int hlua_channel_append(lua_State *L) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3277 | { |
Christopher Faulet | 3b7bc3e | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3278 | struct channel *chn; |
| 3279 | const char *str; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3280 | size_t len; |
Christopher Faulet | 3b7bc3e | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3281 | |
| 3282 | MAY_LJMP(check_args(L, 2, "append")); |
| 3283 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3284 | str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3285 | |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 3286 | if (IS_HTX_STRM(chn_strm(chn))) { |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3287 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3288 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3289 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3290 | |
Christopher Faulet | 3b7bc3e | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3291 | if (len > c_room(chn) || ci_putblk(chn, str, len) < 0) |
| 3292 | lua_pushinteger(L, -1); |
| 3293 | else |
| 3294 | lua_pushinteger(L, len); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3295 | return 1; |
| 3296 | } |
| 3297 | |
Christopher Faulet | 3b7bc3e | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3298 | /* The function replaces input data from the channel by the string passed as |
| 3299 | * argument. Before clearing the buffer, we take care the copy will be |
| 3300 | * possible. It returns the amount of data written (the string length) on |
| 3301 | * 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] | 3302 | */ |
| 3303 | __LJMP static int hlua_channel_set(lua_State *L) |
| 3304 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3305 | struct channel *chn; |
Christopher Faulet | 3b7bc3e | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3306 | const char *str; |
| 3307 | size_t len; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3308 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3309 | MAY_LJMP(check_args(L, 2, "set")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3310 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 3b7bc3e | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3311 | str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3312 | |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 3313 | if (IS_HTX_STRM(chn_strm(chn))) { |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3314 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3315 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3316 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3317 | |
Christopher Faulet | 3b7bc3e | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3318 | /* Be sure we can copied the string once input data will be removed. */ |
| 3319 | if (len > ci_data(chn) || channel_input_closed(chn)) |
| 3320 | lua_pushinteger(L, -1); |
| 3321 | else { |
| 3322 | b_set_data(&chn->buf, co_data(chn)); |
| 3323 | if (ci_putblk(chn, str, len) < 0) |
| 3324 | lua_pushinteger(L, -1); |
| 3325 | else |
| 3326 | lua_pushinteger(L, len); |
| 3327 | } |
| 3328 | return -1; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3329 | } |
| 3330 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3331 | /* Append data in the output side of the buffer. This data is immediately |
| 3332 | * sent. The function returns the amount of data written. If the buffer |
| 3333 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3334 | * if the channel is closed. |
| 3335 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3336 | __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] | 3337 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3338 | struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3339 | size_t len; |
| 3340 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3341 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3342 | int max; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 3343 | struct hlua *hlua; |
| 3344 | |
| 3345 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 3346 | hlua = hlua_gethlua(L); |
| 3347 | if (!hlua) { |
| 3348 | lua_pushnil(L); |
| 3349 | return 1; |
| 3350 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3351 | |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 3352 | if (IS_HTX_STRM(chn_strm(chn))) { |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3353 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3354 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3355 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3356 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3357 | if (unlikely(channel_output_closed(chn))) { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3358 | lua_pushinteger(L, -1); |
| 3359 | return 1; |
| 3360 | } |
| 3361 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3362 | /* Check if the buffer is available because HAProxy doesn't allocate |
Thierry FOURNIER | 3e3a608 | 2015-03-05 17:06:12 +0100 | [diff] [blame] | 3363 | * the request buffer if its not required. |
| 3364 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3365 | if (chn->buf.size == 0) { |
Christopher Faulet | 055310f | 2021-08-05 11:58:37 +0200 | [diff] [blame] | 3366 | if (HLUA_CANT_YIELD(hlua_gethlua(L))) |
| 3367 | return 1; |
Willy Tarreau | 4b962a4 | 2018-11-15 11:03:21 +0100 | [diff] [blame] | 3368 | si_rx_buff_blk(chn_prod(chn)); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3369 | 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] | 3370 | } |
| 3371 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3372 | /* The written data will be immediately sent, so we can check |
| 3373 | * the available space without taking in account the reserve. |
| 3374 | * The reserve is guaranteed for the processing of incoming |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3375 | * data, because the buffer will be flushed. |
| 3376 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3377 | max = b_room(&chn->buf); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3378 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3379 | /* If there is no space available, and the output buffer is empty. |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3380 | * in this case, we cannot add more data, so we cannot yield, |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3381 | * we return the amount of copied data. |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3382 | */ |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3383 | if (max == 0 && co_data(chn) == 0) |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3384 | return 1; |
| 3385 | |
| 3386 | /* Adjust the real required length. */ |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3387 | if (max > len - l) |
| 3388 | max = len - l; |
| 3389 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3390 | /* The buffer available size may be not contiguous. This test |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3391 | * detects a non contiguous buffer and realign it. |
| 3392 | */ |
Willy Tarreau | 3f67999 | 2018-06-15 15:06:42 +0200 | [diff] [blame] | 3393 | if (ci_space_for_replace(chn) < max) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3394 | channel_slow_realign(chn, trash.area); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3395 | |
| 3396 | /* Copy input data in the buffer. */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3397 | 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] | 3398 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3399 | /* buffer replace considers that the input part is filled. |
| 3400 | * so, I must forward these new data in the output part. |
| 3401 | */ |
Willy Tarreau | bcbd393 | 2018-06-06 07:13:22 +0200 | [diff] [blame] | 3402 | c_adv(chn, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3403 | |
| 3404 | l += max; |
| 3405 | lua_pop(L, 1); |
| 3406 | lua_pushinteger(L, l); |
| 3407 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3408 | if (l < len) { |
Christopher Faulet | 055310f | 2021-08-05 11:58:37 +0200 | [diff] [blame] | 3409 | /* If there is no space available, and the output buffer is empty. |
| 3410 | * in this case, we cannot add more data, so we cannot yield, |
| 3411 | * we return the amount of copied data. |
| 3412 | */ |
| 3413 | max = b_room(&chn->buf); |
| 3414 | if ((max == 0 && co_data(chn) == 0) || HLUA_CANT_YIELD(hlua_gethlua(L))) |
| 3415 | return 1; |
| 3416 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3417 | /* If we are waiting for space in the response buffer, we |
| 3418 | * must set the flag WAKERESWR. This flag required the task |
| 3419 | * wake up if any activity is detected on the response buffer. |
| 3420 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3421 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3422 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3423 | else |
| 3424 | HLUA_SET_WAKEREQWR(hlua); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3425 | 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] | 3426 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3427 | |
| 3428 | return 1; |
| 3429 | } |
| 3430 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3431 | /* Just a wrapper of "_hlua_channel_send". This wrapper permits |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3432 | * yield the LUA process, and resume it without checking the |
| 3433 | * input arguments. |
| 3434 | */ |
| 3435 | __LJMP static int hlua_channel_send(lua_State *L) |
| 3436 | { |
| 3437 | MAY_LJMP(check_args(L, 2, "send")); |
| 3438 | lua_pushinteger(L, 0); |
| 3439 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3440 | return MAY_LJMP(hlua_channel_send_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3441 | } |
| 3442 | |
| 3443 | /* This function forward and amount of butes. The data pass from |
| 3444 | * the input side of the buffer to the output side, and can be |
| 3445 | * forwarded. This function never fails. |
| 3446 | * |
| 3447 | * The Lua function takes an amount of bytes to be forwarded in |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3448 | * input. It returns the number of bytes forwarded. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3449 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3450 | __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] | 3451 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3452 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3453 | int len; |
| 3454 | int l; |
| 3455 | int max; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 3456 | struct hlua *hlua; |
| 3457 | |
| 3458 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 3459 | hlua = hlua_gethlua(L); |
| 3460 | if (!hlua) |
| 3461 | return 1; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3462 | |
| 3463 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3464 | |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 3465 | if (IS_HTX_STRM(chn_strm(chn))) { |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3466 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3467 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3468 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3469 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3470 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3471 | l = MAY_LJMP(luaL_checkinteger(L, -1)); |
| 3472 | |
| 3473 | max = len - l; |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3474 | if (max > ci_data(chn)) |
| 3475 | max = ci_data(chn); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3476 | channel_forward(chn, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3477 | l += max; |
| 3478 | |
| 3479 | lua_pop(L, 1); |
| 3480 | lua_pushinteger(L, l); |
| 3481 | |
| 3482 | /* Check if it miss bytes to forward. */ |
| 3483 | if (l < len) { |
| 3484 | /* The the input channel or the output channel are closed, we |
| 3485 | * must return the amount of data forwarded. |
| 3486 | */ |
Christopher Faulet | 055310f | 2021-08-05 11:58:37 +0200 | [diff] [blame] | 3487 | 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] | 3488 | return 1; |
| 3489 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3490 | /* If we are waiting for space data in the response buffer, we |
| 3491 | * must set the flag WAKERESWR. This flag required the task |
| 3492 | * wake up if any activity is detected on the response buffer. |
| 3493 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3494 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3495 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3496 | else |
| 3497 | HLUA_SET_WAKEREQWR(hlua); |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3498 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3499 | /* Otherwise, we can yield waiting for new data in the inpout side. */ |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3500 | 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] | 3501 | } |
| 3502 | |
| 3503 | return 1; |
| 3504 | } |
| 3505 | |
| 3506 | /* Just check the input and prepare the stack for the previous |
| 3507 | * function "hlua_channel_forward_yield" |
| 3508 | */ |
| 3509 | __LJMP static int hlua_channel_forward(lua_State *L) |
| 3510 | { |
| 3511 | MAY_LJMP(check_args(L, 2, "forward")); |
| 3512 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3513 | MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3514 | |
| 3515 | lua_pushinteger(L, 0); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3516 | return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3517 | } |
| 3518 | |
| 3519 | /* Just returns the number of bytes available in the input |
| 3520 | * side of the buffer. This function never fails. |
| 3521 | */ |
| 3522 | __LJMP static int hlua_channel_get_in_len(lua_State *L) |
| 3523 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3524 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3525 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3526 | MAY_LJMP(check_args(L, 1, "get_in_len")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3527 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a3ceac1 | 2018-12-14 13:39:09 +0100 | [diff] [blame] | 3528 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3529 | struct htx *htx = htxbuf(&chn->buf); |
| 3530 | lua_pushinteger(L, htx->data - co_data(chn)); |
| 3531 | } |
| 3532 | else |
| 3533 | lua_pushinteger(L, ci_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3534 | return 1; |
| 3535 | } |
| 3536 | |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3537 | /* Returns true if the channel is full. */ |
| 3538 | __LJMP static int hlua_channel_is_full(lua_State *L) |
| 3539 | { |
| 3540 | struct channel *chn; |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3541 | |
| 3542 | MAY_LJMP(check_args(L, 1, "is_full")); |
| 3543 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 0ec740e | 2020-02-26 11:59:19 +0100 | [diff] [blame] | 3544 | /* ignore the reserve, we are not on a producer side (ie in an |
| 3545 | * applet). |
| 3546 | */ |
| 3547 | lua_pushboolean(L, channel_full(chn, 0)); |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3548 | return 1; |
| 3549 | } |
| 3550 | |
Christopher Faulet | 2ac9ba2 | 2020-02-25 10:15:50 +0100 | [diff] [blame] | 3551 | /* Returns true if the channel is the response channel. */ |
| 3552 | __LJMP static int hlua_channel_is_resp(lua_State *L) |
| 3553 | { |
| 3554 | struct channel *chn; |
| 3555 | |
| 3556 | MAY_LJMP(check_args(L, 1, "is_resp")); |
| 3557 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3558 | |
| 3559 | lua_pushboolean(L, !!(chn->flags & CF_ISRESP)); |
| 3560 | return 1; |
| 3561 | } |
| 3562 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3563 | /* Just returns the number of bytes available in the output |
| 3564 | * side of the buffer. This function never fails. |
| 3565 | */ |
| 3566 | __LJMP static int hlua_channel_get_out_len(lua_State *L) |
| 3567 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3568 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3569 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3570 | MAY_LJMP(check_args(L, 1, "get_out_len")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3571 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3572 | lua_pushinteger(L, co_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3573 | return 1; |
| 3574 | } |
| 3575 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3576 | /* |
| 3577 | * |
| 3578 | * |
| 3579 | * Class Fetches |
| 3580 | * |
| 3581 | * |
| 3582 | */ |
| 3583 | |
| 3584 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3585 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3586 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3587 | __LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud) |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3588 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3589 | return MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3590 | } |
| 3591 | |
| 3592 | /* This function creates and push in the stack a fetch object according |
| 3593 | * with a current TXN. |
| 3594 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3595 | 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] | 3596 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3597 | struct hlua_smp *hsmp; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3598 | |
| 3599 | /* Check stack size. */ |
| 3600 | if (!lua_checkstack(L, 3)) |
| 3601 | return 0; |
| 3602 | |
| 3603 | /* Create the object: obj[0] = userdata. |
| 3604 | * Note that the base of the Fetches object is the |
| 3605 | * transaction object. |
| 3606 | */ |
| 3607 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3608 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3609 | lua_rawseti(L, -2, 0); |
| 3610 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3611 | hsmp->s = txn->s; |
| 3612 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 3613 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3614 | hsmp->flags = flags; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3615 | |
| 3616 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 3617 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref); |
| 3618 | lua_setmetatable(L, -2); |
| 3619 | |
| 3620 | return 1; |
| 3621 | } |
| 3622 | |
| 3623 | /* This function is an LUA binding. It is called with each sample-fetch. |
| 3624 | * It uses closure argument to store the associated sample-fetch. It |
| 3625 | * returns only one argument or throws an error. An error is thrown |
| 3626 | * only if an error is encountered during the argument parsing. If |
| 3627 | * the "sample-fetch" function fails, nil is returned. |
| 3628 | */ |
| 3629 | __LJMP static int hlua_run_sample_fetch(lua_State *L) |
| 3630 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3631 | struct hlua_smp *hsmp; |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 3632 | struct sample_fetch *f; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 3633 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3634 | int i; |
| 3635 | struct sample smp; |
| 3636 | |
| 3637 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3638 | f = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3639 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3640 | /* Get traditional arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3641 | hsmp = MAY_LJMP(hlua_checkfetches(L, 1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3642 | |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 3643 | /* Check execution authorization. */ |
| 3644 | if (f->use & SMP_USE_HTTP_ANY && |
| 3645 | !(hsmp->flags & HLUA_F_MAY_USE_HTTP)) { |
| 3646 | lua_pushfstring(L, "the sample-fetch '%s' needs an HTTP parser which " |
| 3647 | "is not available in Lua services", f->kw); |
| 3648 | WILL_LJMP(lua_error(L)); |
| 3649 | } |
| 3650 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3651 | /* Get extra arguments. */ |
| 3652 | for (i = 0; i < lua_gettop(L) - 1; i++) { |
| 3653 | if (i >= ARGM_NBARGS) |
| 3654 | break; |
| 3655 | hlua_lua2arg(L, i + 2, &args[i]); |
| 3656 | } |
| 3657 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3658 | args[i].data.str.area = NULL; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3659 | |
| 3660 | /* Check arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3661 | 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] | 3662 | |
| 3663 | /* Run the special args checker. */ |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 3664 | if (f->val_args && !f->val_args(args, NULL)) { |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3665 | lua_pushfstring(L, "error in arguments"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3666 | goto error; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3667 | } |
| 3668 | |
| 3669 | /* Initialise the sample. */ |
| 3670 | memset(&smp, 0, sizeof(smp)); |
| 3671 | |
| 3672 | /* Run the sample fetch process. */ |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 3673 | 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] | 3674 | if (!f->process(args, &smp, f->kw, f->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3675 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3676 | lua_pushstring(L, ""); |
| 3677 | else |
| 3678 | lua_pushnil(L); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3679 | goto end; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3680 | } |
| 3681 | |
| 3682 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3683 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3684 | hlua_smp2lua_str(L, &smp); |
| 3685 | else |
| 3686 | hlua_smp2lua(L, &smp); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3687 | |
| 3688 | end: |
| 3689 | for (i = 0; args[i].type != ARGT_STOP; i++) { |
| 3690 | if (args[i].type == ARGT_STR) |
| 3691 | chunk_destroy(&args[i].data.str); |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 3692 | else if (args[i].type == ARGT_REG) |
| 3693 | regex_free(args[i].data.reg); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3694 | } |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3695 | return 1; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3696 | |
| 3697 | error: |
| 3698 | for (i = 0; args[i].type != ARGT_STOP; i++) { |
| 3699 | if (args[i].type == ARGT_STR) |
| 3700 | chunk_destroy(&args[i].data.str); |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 3701 | else if (args[i].type == ARGT_REG) |
| 3702 | regex_free(args[i].data.reg); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3703 | } |
| 3704 | WILL_LJMP(lua_error(L)); |
| 3705 | return 0; /* Never reached */ |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3706 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3707 | |
| 3708 | /* |
| 3709 | * |
| 3710 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3711 | * Class Converters |
| 3712 | * |
| 3713 | * |
| 3714 | */ |
| 3715 | |
| 3716 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3717 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3718 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3719 | __LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3720 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3721 | return MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3722 | } |
| 3723 | |
| 3724 | /* This function creates and push in the stack a Converters object |
| 3725 | * according with a current TXN. |
| 3726 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3727 | 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] | 3728 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3729 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3730 | |
| 3731 | /* Check stack size. */ |
| 3732 | if (!lua_checkstack(L, 3)) |
| 3733 | return 0; |
| 3734 | |
| 3735 | /* Create the object: obj[0] = userdata. |
| 3736 | * Note that the base of the Converters object is the |
| 3737 | * same than the TXN object. |
| 3738 | */ |
| 3739 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3740 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3741 | lua_rawseti(L, -2, 0); |
| 3742 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3743 | hsmp->s = txn->s; |
| 3744 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 3745 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3746 | hsmp->flags = flags; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3747 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3748 | /* Pop a class stream metatable and affect it to the table. */ |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3749 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref); |
| 3750 | lua_setmetatable(L, -2); |
| 3751 | |
| 3752 | return 1; |
| 3753 | } |
| 3754 | |
| 3755 | /* This function is an LUA binding. It is called with each converter. |
| 3756 | * It uses closure argument to store the associated converter. It |
| 3757 | * returns only one argument or throws an error. An error is thrown |
| 3758 | * only if an error is encountered during the argument parsing. If |
| 3759 | * the converter function function fails, nil is returned. |
| 3760 | */ |
| 3761 | __LJMP static int hlua_run_sample_conv(lua_State *L) |
| 3762 | { |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3763 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3764 | struct sample_conv *conv; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 3765 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3766 | int i; |
| 3767 | struct sample smp; |
| 3768 | |
| 3769 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3770 | conv = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3771 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3772 | /* Get traditional arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3773 | hsmp = MAY_LJMP(hlua_checkconverters(L, 1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3774 | |
| 3775 | /* Get extra arguments. */ |
| 3776 | for (i = 0; i < lua_gettop(L) - 2; i++) { |
| 3777 | if (i >= ARGM_NBARGS) |
| 3778 | break; |
| 3779 | hlua_lua2arg(L, i + 3, &args[i]); |
| 3780 | } |
| 3781 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3782 | args[i].data.str.area = NULL; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3783 | |
| 3784 | /* Check arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3785 | 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] | 3786 | |
| 3787 | /* Run the special args checker. */ |
| 3788 | if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) { |
| 3789 | hlua_pusherror(L, "error in arguments"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3790 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3791 | } |
| 3792 | |
| 3793 | /* Initialise the sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 3794 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3795 | if (!hlua_lua2smp(L, 2, &smp)) { |
| 3796 | hlua_pusherror(L, "error in the input argument"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3797 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3798 | } |
| 3799 | |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 3800 | smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR); |
| 3801 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3802 | /* Apply expected cast. */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3803 | if (!sample_casts[smp.data.type][conv->in_type]) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3804 | hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'", |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3805 | smp_to_type[smp.data.type], smp_to_type[conv->in_type]); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3806 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3807 | } |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3808 | if (sample_casts[smp.data.type][conv->in_type] != c_none && |
| 3809 | !sample_casts[smp.data.type][conv->in_type](&smp)) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3810 | hlua_pusherror(L, "error during the input argument casting"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3811 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3812 | } |
| 3813 | |
| 3814 | /* Run the sample conversion process. */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 3815 | if (!conv->process(args, &smp, conv->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3816 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3817 | lua_pushstring(L, ""); |
| 3818 | else |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 3819 | lua_pushnil(L); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3820 | goto end; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3821 | } |
| 3822 | |
| 3823 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3824 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3825 | hlua_smp2lua_str(L, &smp); |
| 3826 | else |
| 3827 | hlua_smp2lua(L, &smp); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3828 | end: |
| 3829 | for (i = 0; args[i].type != ARGT_STOP; i++) { |
| 3830 | if (args[i].type == ARGT_STR) |
| 3831 | chunk_destroy(&args[i].data.str); |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 3832 | else if (args[i].type == ARGT_REG) |
| 3833 | regex_free(args[i].data.reg); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3834 | } |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 3835 | return 1; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3836 | |
| 3837 | error: |
| 3838 | for (i = 0; args[i].type != ARGT_STOP; i++) { |
| 3839 | if (args[i].type == ARGT_STR) |
| 3840 | chunk_destroy(&args[i].data.str); |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 3841 | else if (args[i].type == ARGT_REG) |
| 3842 | regex_free(args[i].data.reg); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3843 | } |
| 3844 | WILL_LJMP(lua_error(L)); |
| 3845 | return 0; /* Never reached */ |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3846 | } |
| 3847 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3848 | /* |
| 3849 | * |
| 3850 | * |
| 3851 | * Class AppletTCP |
| 3852 | * |
| 3853 | * |
| 3854 | */ |
| 3855 | |
| 3856 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 3857 | * a class stream, otherwise it throws an error. |
| 3858 | */ |
| 3859 | __LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud) |
| 3860 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3861 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3862 | } |
| 3863 | |
| 3864 | /* This function creates and push in the stack an Applet object |
| 3865 | * according with a current TXN. |
| 3866 | */ |
| 3867 | static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx) |
| 3868 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3869 | struct hlua_appctx *luactx; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3870 | struct stream_interface *si = ctx->owner; |
| 3871 | struct stream *s = si_strm(si); |
| 3872 | struct proxy *p = s->be; |
| 3873 | |
| 3874 | /* Check stack size. */ |
| 3875 | if (!lua_checkstack(L, 3)) |
| 3876 | return 0; |
| 3877 | |
| 3878 | /* Create the object: obj[0] = userdata. |
| 3879 | * Note that the base of the Converters object is the |
| 3880 | * same than the TXN object. |
| 3881 | */ |
| 3882 | lua_newtable(L); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3883 | luactx = lua_newuserdata(L, sizeof(*luactx)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3884 | lua_rawseti(L, -2, 0); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3885 | luactx->appctx = ctx; |
| 3886 | luactx->htxn.s = s; |
| 3887 | luactx->htxn.p = p; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3888 | |
| 3889 | /* Create the "f" field that contains a list of fetches. */ |
| 3890 | lua_pushstring(L, "f"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3891 | if (!hlua_fetches_new(L, &luactx->htxn, 0)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3892 | return 0; |
| 3893 | lua_settable(L, -3); |
| 3894 | |
| 3895 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 3896 | lua_pushstring(L, "sf"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3897 | if (!hlua_fetches_new(L, &luactx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3898 | return 0; |
| 3899 | lua_settable(L, -3); |
| 3900 | |
| 3901 | /* Create the "c" field that contains a list of converters. */ |
| 3902 | lua_pushstring(L, "c"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3903 | if (!hlua_converters_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 "sc" field that contains a list of stringsafe converters. */ |
| 3908 | lua_pushstring(L, "sc"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3909 | if (!hlua_converters_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 | /* Pop a class stream metatable and affect it to the table. */ |
| 3914 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref); |
| 3915 | lua_setmetatable(L, -2); |
| 3916 | |
| 3917 | return 1; |
| 3918 | } |
| 3919 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3920 | __LJMP static int hlua_applet_tcp_set_var(lua_State *L) |
| 3921 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3922 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3923 | struct stream *s; |
| 3924 | const char *name; |
| 3925 | size_t len; |
| 3926 | struct sample smp; |
| 3927 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 3928 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 3929 | 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] | 3930 | |
| 3931 | /* It is useles to retrieve the stream, but this function |
| 3932 | * runs only in a stream context. |
| 3933 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3934 | luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3935 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3936 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3937 | |
| 3938 | /* Converts the third argument in a sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 3939 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3940 | hlua_lua2smp(L, 3, &smp); |
| 3941 | |
Aurelien DARRAGON | d39fb74 | 2023-05-17 16:06:11 +0200 | [diff] [blame] | 3942 | /* Store the sample in a variable. We don't need to dup the smp, vars API |
| 3943 | * already takes care of duplicating dynamic var data. |
| 3944 | */ |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3945 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 3946 | |
| 3947 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 3948 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 3949 | else |
| 3950 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 3951 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 3952 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3953 | } |
| 3954 | |
| 3955 | __LJMP static int hlua_applet_tcp_unset_var(lua_State *L) |
| 3956 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3957 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3958 | struct stream *s; |
| 3959 | const char *name; |
| 3960 | size_t len; |
| 3961 | struct sample smp; |
| 3962 | |
| 3963 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 3964 | |
| 3965 | /* It is useles to retrieve the stream, but this function |
| 3966 | * runs only in a stream context. |
| 3967 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3968 | luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3969 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3970 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3971 | |
| 3972 | /* Unset the variable. */ |
| 3973 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 3974 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 3975 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3976 | } |
| 3977 | |
| 3978 | __LJMP static int hlua_applet_tcp_get_var(lua_State *L) |
| 3979 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3980 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3981 | struct stream *s; |
| 3982 | const char *name; |
| 3983 | size_t len; |
| 3984 | struct sample smp; |
| 3985 | |
| 3986 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 3987 | |
| 3988 | /* It is useles to retrieve the stream, but this function |
| 3989 | * runs only in a stream context. |
| 3990 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3991 | luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3992 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3993 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3994 | |
| 3995 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 3996 | if (!vars_get_by_name(name, len, &smp)) { |
| 3997 | lua_pushnil(L); |
| 3998 | return 1; |
| 3999 | } |
| 4000 | |
| 4001 | return hlua_smp2lua(L, &smp); |
| 4002 | } |
| 4003 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4004 | __LJMP static int hlua_applet_tcp_set_priv(lua_State *L) |
| 4005 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4006 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 4007 | struct stream *s = luactx->htxn.s; |
Aurelien DARRAGON | be7b7ab | 2024-03-12 17:05:54 +0100 | [diff] [blame] | 4008 | struct hlua *hlua = hlua_stream_ctx_get(s, luactx->appctx->ctx.hlua_apptcp.hlua->state_id); |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4009 | |
| 4010 | /* 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] | 4011 | if (!hlua) |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 4012 | return 0; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4013 | |
| 4014 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 4015 | |
| 4016 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 4017 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4018 | |
| 4019 | /* Get and store new value. */ |
| 4020 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 4021 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 4022 | |
| 4023 | return 0; |
| 4024 | } |
| 4025 | |
| 4026 | __LJMP static int hlua_applet_tcp_get_priv(lua_State *L) |
| 4027 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4028 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 4029 | struct stream *s = luactx->htxn.s; |
Aurelien DARRAGON | be7b7ab | 2024-03-12 17:05:54 +0100 | [diff] [blame] | 4030 | struct hlua *hlua = hlua_stream_ctx_get(s, luactx->appctx->ctx.hlua_apptcp.hlua->state_id); |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4031 | |
| 4032 | /* 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] | 4033 | if (!hlua) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 4034 | lua_pushnil(L); |
| 4035 | return 1; |
| 4036 | } |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4037 | |
| 4038 | /* Push configuration index in the stack. */ |
| 4039 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 4040 | |
| 4041 | return 1; |
| 4042 | } |
| 4043 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4044 | /* If expected data not yet available, it returns a yield. This function |
| 4045 | * consumes the data in the buffer. It returns a string containing the |
| 4046 | * data. This string can be empty. |
| 4047 | */ |
| 4048 | __LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx) |
| 4049 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4050 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 4051 | struct stream_interface *si = luactx->appctx->owner; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4052 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4053 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4054 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4055 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4056 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4057 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4058 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4059 | ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4060 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4061 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4062 | if (ret == 0) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 4063 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4064 | 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] | 4065 | } |
| 4066 | |
| 4067 | /* End of data: commit the total strings and return. */ |
| 4068 | if (ret < 0) { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4069 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4070 | return 1; |
| 4071 | } |
| 4072 | |
| 4073 | /* Ensure that the block 2 length is usable. */ |
| 4074 | if (ret == 1) |
| 4075 | len2 = 0; |
| 4076 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 4077 | /* don't check the max length read and don't check. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4078 | luaL_addlstring(&luactx->b, blk1, len1); |
| 4079 | luaL_addlstring(&luactx->b, blk2, len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4080 | |
| 4081 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4082 | co_skip(si_oc(si), len1 + len2); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4083 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4084 | return 1; |
| 4085 | } |
| 4086 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4087 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4088 | __LJMP static int hlua_applet_tcp_getline(lua_State *L) |
| 4089 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4090 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4091 | |
| 4092 | /* Initialise the string catenation. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4093 | luaL_buffinit(L, &luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4094 | |
| 4095 | return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0)); |
| 4096 | } |
| 4097 | |
| 4098 | /* If expected data not yet available, it returns a yield. This function |
| 4099 | * consumes the data in the buffer. It returns a string containing the |
| 4100 | * data. This string can be empty. |
| 4101 | */ |
| 4102 | __LJMP static int hlua_applet_tcp_recv_yield(lua_State *L, int status, lua_KContext ctx) |
| 4103 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4104 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 4105 | struct stream_interface *si = luactx->appctx->owner; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4106 | size_t len = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4107 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4108 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4109 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4110 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4111 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4112 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4113 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4114 | ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4115 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4116 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4117 | if (ret == 0) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 4118 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4119 | 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] | 4120 | } |
| 4121 | |
| 4122 | /* End of data: commit the total strings and return. */ |
| 4123 | if (ret < 0) { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4124 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4125 | return 1; |
| 4126 | } |
| 4127 | |
| 4128 | /* Ensure that the block 2 length is usable. */ |
| 4129 | if (ret == 1) |
| 4130 | len2 = 0; |
| 4131 | |
| 4132 | if (len == -1) { |
| 4133 | |
| 4134 | /* If len == -1, catenate all the data avalaile and |
| 4135 | * yield because we want to get all the data until |
| 4136 | * the end of data stream. |
| 4137 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4138 | luaL_addlstring(&luactx->b, blk1, len1); |
| 4139 | luaL_addlstring(&luactx->b, blk2, len2); |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4140 | co_skip(si_oc(si), len1 + len2); |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 4141 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4142 | 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] | 4143 | |
| 4144 | } else { |
| 4145 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4146 | /* Copy the first block caping to the length required. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4147 | if (len1 > len) |
| 4148 | len1 = len; |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4149 | luaL_addlstring(&luactx->b, blk1, len1); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4150 | len -= len1; |
| 4151 | |
| 4152 | /* Copy the second block. */ |
| 4153 | if (len2 > len) |
| 4154 | len2 = len; |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4155 | luaL_addlstring(&luactx->b, blk2, len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4156 | len -= len2; |
| 4157 | |
| 4158 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4159 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4160 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4161 | /* If there is no other data available, yield waiting for new data. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4162 | if (len > 0) { |
| 4163 | lua_pushinteger(L, len); |
| 4164 | lua_replace(L, 2); |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 4165 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4166 | 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] | 4167 | } |
| 4168 | |
| 4169 | /* return the result. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4170 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4171 | return 1; |
| 4172 | } |
| 4173 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4174 | /* we never execute this */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4175 | hlua_pusherror(L, "Lua: internal error"); |
| 4176 | WILL_LJMP(lua_error(L)); |
| 4177 | return 0; |
| 4178 | } |
| 4179 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4180 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4181 | __LJMP static int hlua_applet_tcp_recv(lua_State *L) |
| 4182 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4183 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4184 | int len = -1; |
| 4185 | |
| 4186 | if (lua_gettop(L) > 2) |
| 4187 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 4188 | if (lua_gettop(L) >= 2) { |
| 4189 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4190 | lua_pop(L, 1); |
| 4191 | } |
| 4192 | |
| 4193 | /* Confirm or set the required length */ |
| 4194 | lua_pushinteger(L, len); |
| 4195 | |
| 4196 | /* Initialise the string catenation. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4197 | luaL_buffinit(L, &luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4198 | |
| 4199 | return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0)); |
| 4200 | } |
| 4201 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4202 | /* Append data in the output side of the buffer. This data is immediately |
| 4203 | * sent. The function returns the amount of data written. If the buffer |
| 4204 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4205 | * if the channel is closed. |
| 4206 | */ |
| 4207 | __LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx) |
| 4208 | { |
| 4209 | size_t len; |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4210 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4211 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4212 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4213 | struct stream_interface *si = luactx->appctx->owner; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4214 | struct channel *chn = si_ic(si); |
| 4215 | int max; |
| 4216 | |
| 4217 | /* Get the max amount of data which can write as input in the channel. */ |
| 4218 | max = channel_recv_max(chn); |
| 4219 | if (max > (len - l)) |
| 4220 | max = len - l; |
| 4221 | |
| 4222 | /* Copy data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4223 | ci_putblk(chn, str + l, max); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4224 | |
| 4225 | /* update counters. */ |
| 4226 | l += max; |
| 4227 | lua_pop(L, 1); |
| 4228 | lua_pushinteger(L, l); |
| 4229 | |
| 4230 | /* If some data is not send, declares the situation to the |
| 4231 | * applet, and returns a yield. |
| 4232 | */ |
| 4233 | if (l < len) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 4234 | si_rx_room_blk(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4235 | 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] | 4236 | } |
| 4237 | |
| 4238 | return 1; |
| 4239 | } |
| 4240 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4241 | /* Just a wrapper of "hlua_applet_tcp_send_yield". This wrapper permits |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4242 | * yield the LUA process, and resume it without checking the |
| 4243 | * input arguments. |
| 4244 | */ |
| 4245 | __LJMP static int hlua_applet_tcp_send(lua_State *L) |
| 4246 | { |
| 4247 | MAY_LJMP(check_args(L, 2, "send")); |
| 4248 | lua_pushinteger(L, 0); |
| 4249 | |
| 4250 | return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0)); |
| 4251 | } |
| 4252 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4253 | /* |
| 4254 | * |
| 4255 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4256 | * Class AppletHTTP |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4257 | * |
| 4258 | * |
| 4259 | */ |
| 4260 | |
| 4261 | /* Returns a struct hlua_txn if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4262 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4263 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4264 | __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] | 4265 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4266 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_http_ref)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4267 | } |
| 4268 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4269 | /* This function creates and push in the stack an Applet object |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4270 | * according with a current TXN. |
| 4271 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4272 | static int hlua_applet_http_new(lua_State *L, struct appctx *ctx) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4273 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4274 | struct hlua_appctx *luactx; |
Thierry FOURNIER | 841475e | 2015-12-11 17:10:09 +0100 | [diff] [blame] | 4275 | struct hlua_txn htxn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4276 | struct stream_interface *si = ctx->owner; |
| 4277 | struct stream *s = si_strm(si); |
| 4278 | struct proxy *px = s->be; |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4279 | struct htx *htx; |
| 4280 | struct htx_blk *blk; |
| 4281 | struct htx_sl *sl; |
| 4282 | struct ist path; |
| 4283 | unsigned long long len = 0; |
| 4284 | int32_t pos; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4285 | |
| 4286 | /* Check stack size. */ |
| 4287 | if (!lua_checkstack(L, 3)) |
| 4288 | return 0; |
| 4289 | |
| 4290 | /* Create the object: obj[0] = userdata. |
| 4291 | * Note that the base of the Converters object is the |
| 4292 | * same than the TXN object. |
| 4293 | */ |
| 4294 | lua_newtable(L); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4295 | luactx = lua_newuserdata(L, sizeof(*luactx)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4296 | lua_rawseti(L, -2, 0); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4297 | luactx->appctx = ctx; |
| 4298 | luactx->appctx->ctx.hlua_apphttp.status = 200; /* Default status code returned. */ |
| 4299 | luactx->appctx->ctx.hlua_apphttp.reason = NULL; /* Use default reason based on status */ |
| 4300 | luactx->htxn.s = s; |
| 4301 | luactx->htxn.p = px; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4302 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4303 | /* Create the "f" field that contains a list of fetches. */ |
| 4304 | lua_pushstring(L, "f"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4305 | if (!hlua_fetches_new(L, &luactx->htxn, 0)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4306 | return 0; |
| 4307 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4308 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4309 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 4310 | lua_pushstring(L, "sf"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4311 | if (!hlua_fetches_new(L, &luactx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4312 | return 0; |
| 4313 | lua_settable(L, -3); |
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 "c" field that contains a list of converters. */ |
| 4316 | lua_pushstring(L, "c"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4317 | if (!hlua_converters_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 "sc" field that contains a list of stringsafe converters. */ |
| 4322 | lua_pushstring(L, "sc"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4323 | if (!hlua_converters_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); |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4326 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4327 | htx = htxbuf(&s->req.buf); |
| 4328 | blk = htx_get_first_blk(htx); |
Christopher Faulet | ea00973 | 2019-11-18 15:50:25 +0100 | [diff] [blame] | 4329 | BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4330 | sl = htx_get_blk_ptr(htx, blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4331 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4332 | /* Stores the request method. */ |
| 4333 | lua_pushstring(L, "method"); |
| 4334 | lua_pushlstring(L, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); |
| 4335 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4336 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4337 | /* Stores the http version. */ |
| 4338 | lua_pushstring(L, "version"); |
| 4339 | lua_pushlstring(L, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); |
| 4340 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4341 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4342 | /* creates an array of headers. hlua_http_get_headers() crates and push |
| 4343 | * the array on the top of the stack. |
| 4344 | */ |
| 4345 | lua_pushstring(L, "headers"); |
| 4346 | htxn.s = s; |
| 4347 | htxn.p = px; |
| 4348 | htxn.dir = SMP_OPT_DIR_REQ; |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 4349 | if (!hlua_http_get_headers(L, &htxn.s->txn->req)) |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4350 | return 0; |
| 4351 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4352 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4353 | path = http_get_path(htx_sl_req_uri(sl)); |
Tim Duesterhus | ed52637 | 2020-03-05 17:56:33 +0100 | [diff] [blame] | 4354 | if (isttest(path)) { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4355 | char *p, *q, *end; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4356 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4357 | p = path.ptr; |
| 4358 | end = path.ptr + path.len; |
| 4359 | q = p; |
| 4360 | while (q < end && *q != '?') |
| 4361 | q++; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4362 | |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 4363 | /* Stores the request path. */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4364 | lua_pushstring(L, "path"); |
| 4365 | lua_pushlstring(L, p, q - p); |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 4366 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4367 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4368 | /* Stores the query string. */ |
| 4369 | lua_pushstring(L, "qs"); |
| 4370 | if (*q == '?') |
| 4371 | q++; |
| 4372 | lua_pushlstring(L, q, end - q); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4373 | lua_settable(L, -3); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4374 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4375 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4376 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 4377 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 4378 | enum htx_blk_type type = htx_get_blk_type(blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4379 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 4380 | if (type == HTX_BLK_TLR || type == HTX_BLK_EOT) |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4381 | break; |
| 4382 | if (type == HTX_BLK_DATA) |
| 4383 | len += htx_get_blksz(blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4384 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4385 | if (htx->extra != ULLONG_MAX) |
| 4386 | len += htx->extra; |
| 4387 | |
| 4388 | /* Stores the request path. */ |
| 4389 | lua_pushstring(L, "length"); |
| 4390 | lua_pushinteger(L, len); |
| 4391 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4392 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4393 | /* Create an empty array of HTTP request headers. */ |
| 4394 | lua_pushstring(L, "response"); |
| 4395 | lua_newtable(L); |
| 4396 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4397 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4398 | /* Pop a class stream metatable and affect it to the table. */ |
| 4399 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref); |
| 4400 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4401 | |
| 4402 | return 1; |
| 4403 | } |
| 4404 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4405 | __LJMP static int hlua_applet_http_set_var(lua_State *L) |
| 4406 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4407 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4408 | struct stream *s; |
| 4409 | const char *name; |
| 4410 | size_t len; |
| 4411 | struct sample smp; |
| 4412 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 4413 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 4414 | 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] | 4415 | |
| 4416 | /* It is useles to retrieve the stream, but this function |
| 4417 | * runs only in a stream context. |
| 4418 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4419 | luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4420 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4421 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4422 | |
| 4423 | /* Converts the third argument in a sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 4424 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4425 | hlua_lua2smp(L, 3, &smp); |
| 4426 | |
Aurelien DARRAGON | d39fb74 | 2023-05-17 16:06:11 +0200 | [diff] [blame] | 4427 | /* Store the sample in a variable. We don't need to dup the smp, vars API |
| 4428 | * already takes care of duplicating dynamic var data. |
| 4429 | */ |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4430 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 4431 | |
| 4432 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 4433 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 4434 | else |
| 4435 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 4436 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 4437 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4438 | } |
| 4439 | |
| 4440 | __LJMP static int hlua_applet_http_unset_var(lua_State *L) |
| 4441 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4442 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4443 | struct stream *s; |
| 4444 | const char *name; |
| 4445 | size_t len; |
| 4446 | struct sample smp; |
| 4447 | |
| 4448 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 4449 | |
| 4450 | /* It is useles to retrieve the stream, but this function |
| 4451 | * runs only in a stream context. |
| 4452 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4453 | luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4454 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4455 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4456 | |
| 4457 | /* Unset the variable. */ |
| 4458 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 4459 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 4460 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4461 | } |
| 4462 | |
| 4463 | __LJMP static int hlua_applet_http_get_var(lua_State *L) |
| 4464 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4465 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4466 | struct stream *s; |
| 4467 | const char *name; |
| 4468 | size_t len; |
| 4469 | struct sample smp; |
| 4470 | |
| 4471 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 4472 | |
| 4473 | /* It is useles to retrieve the stream, but this function |
| 4474 | * runs only in a stream context. |
| 4475 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4476 | luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4477 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4478 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4479 | |
| 4480 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 4481 | if (!vars_get_by_name(name, len, &smp)) { |
| 4482 | lua_pushnil(L); |
| 4483 | return 1; |
| 4484 | } |
| 4485 | |
| 4486 | return hlua_smp2lua(L, &smp); |
| 4487 | } |
| 4488 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4489 | __LJMP static int hlua_applet_http_set_priv(lua_State *L) |
| 4490 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4491 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4492 | struct stream *s = luactx->htxn.s; |
Aurelien DARRAGON | be7b7ab | 2024-03-12 17:05:54 +0100 | [diff] [blame] | 4493 | struct hlua *hlua = hlua_stream_ctx_get(s, luactx->appctx->ctx.hlua_apphttp.hlua->state_id); |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4494 | |
| 4495 | /* 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] | 4496 | if (!hlua) |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 4497 | return 0; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4498 | |
| 4499 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 4500 | |
| 4501 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 4502 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4503 | |
| 4504 | /* Get and store new value. */ |
| 4505 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 4506 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 4507 | |
| 4508 | return 0; |
| 4509 | } |
| 4510 | |
| 4511 | __LJMP static int hlua_applet_http_get_priv(lua_State *L) |
| 4512 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4513 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4514 | struct stream *s = luactx->htxn.s; |
Aurelien DARRAGON | be7b7ab | 2024-03-12 17:05:54 +0100 | [diff] [blame] | 4515 | struct hlua *hlua = hlua_stream_ctx_get(s, luactx->appctx->ctx.hlua_apphttp.hlua->state_id); |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4516 | |
| 4517 | /* 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] | 4518 | if (!hlua) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 4519 | lua_pushnil(L); |
| 4520 | return 1; |
| 4521 | } |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4522 | |
| 4523 | /* Push configuration index in the stack. */ |
| 4524 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 4525 | |
| 4526 | return 1; |
| 4527 | } |
| 4528 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4529 | /* If expected data not yet available, it returns a yield. This function |
| 4530 | * consumes the data in the buffer. It returns a string containing the |
| 4531 | * data. This string can be empty. |
| 4532 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4533 | __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] | 4534 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4535 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4536 | struct stream_interface *si = luactx->appctx->owner; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4537 | struct channel *req = si_oc(si); |
| 4538 | struct htx *htx; |
| 4539 | struct htx_blk *blk; |
| 4540 | size_t count; |
| 4541 | int stop = 0; |
| 4542 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4543 | htx = htx_from_buf(&req->buf); |
| 4544 | count = co_data(req); |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 4545 | blk = htx_get_first_blk(htx); |
Christopher Faulet | cc26b13 | 2018-12-18 21:20:57 +0100 | [diff] [blame] | 4546 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4547 | while (count && !stop && blk) { |
| 4548 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4549 | uint32_t sz = htx_get_blksz(blk); |
| 4550 | struct ist v; |
| 4551 | uint32_t vlen; |
| 4552 | char *nl; |
| 4553 | |
| 4554 | vlen = sz; |
| 4555 | if (vlen > count) { |
| 4556 | if (type != HTX_BLK_DATA) |
| 4557 | break; |
| 4558 | vlen = count; |
| 4559 | } |
| 4560 | |
| 4561 | switch (type) { |
| 4562 | case HTX_BLK_UNUSED: |
| 4563 | break; |
| 4564 | |
| 4565 | case HTX_BLK_DATA: |
| 4566 | v = htx_get_blk_value(htx, blk); |
| 4567 | v.len = vlen; |
| 4568 | nl = istchr(v, '\n'); |
| 4569 | if (nl != NULL) { |
| 4570 | stop = 1; |
| 4571 | vlen = nl - v.ptr + 1; |
| 4572 | } |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4573 | luaL_addlstring(&luactx->b, v.ptr, vlen); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4574 | break; |
| 4575 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4576 | case HTX_BLK_TLR: |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 4577 | case HTX_BLK_EOT: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4578 | stop = 1; |
| 4579 | break; |
| 4580 | |
| 4581 | default: |
| 4582 | break; |
| 4583 | } |
| 4584 | |
| 4585 | co_set_data(req, co_data(req) - vlen); |
| 4586 | count -= vlen; |
| 4587 | if (sz == vlen) |
| 4588 | blk = htx_remove_blk(htx, blk); |
| 4589 | else { |
| 4590 | htx_cut_data_blk(htx, blk, vlen); |
| 4591 | break; |
| 4592 | } |
| 4593 | } |
| 4594 | |
Christopher Faulet | eccb31c | 2021-04-02 14:24:56 +0200 | [diff] [blame] | 4595 | /* The message was fully consumed and no more data are expected |
| 4596 | * (EOM flag set). |
| 4597 | */ |
Christopher Faulet | a5a25b1 | 2022-08-29 15:37:16 +0200 | [diff] [blame] | 4598 | if (htx_is_empty(htx) && (req->flags & CF_EOI)) |
Christopher Faulet | eccb31c | 2021-04-02 14:24:56 +0200 | [diff] [blame] | 4599 | stop = 1; |
| 4600 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4601 | htx_to_buf(htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4602 | if (!stop) { |
| 4603 | si_cant_get(si); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4604 | 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] | 4605 | } |
| 4606 | |
| 4607 | /* return the result. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4608 | luaL_pushresult(&luactx->b); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4609 | return 1; |
| 4610 | } |
| 4611 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4612 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4613 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4614 | __LJMP static int hlua_applet_http_getline(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4615 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4616 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4617 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4618 | /* Initialise the string catenation. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4619 | luaL_buffinit(L, &luactx->b); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4620 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4621 | return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4622 | } |
| 4623 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4624 | /* If expected data not yet available, it returns a yield. This function |
| 4625 | * consumes the data in the buffer. It returns a string containing the |
| 4626 | * data. This string can be empty. |
| 4627 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4628 | __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] | 4629 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4630 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4631 | struct stream_interface *si = luactx->appctx->owner; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4632 | struct channel *req = si_oc(si); |
| 4633 | struct htx *htx; |
| 4634 | struct htx_blk *blk; |
| 4635 | size_t count; |
| 4636 | int len; |
| 4637 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4638 | htx = htx_from_buf(&req->buf); |
| 4639 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4640 | count = co_data(req); |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 4641 | blk = htx_get_head_blk(htx); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4642 | while (count && len && blk) { |
| 4643 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4644 | uint32_t sz = htx_get_blksz(blk); |
| 4645 | struct ist v; |
| 4646 | uint32_t vlen; |
| 4647 | |
| 4648 | vlen = sz; |
| 4649 | if (len > 0 && vlen > len) |
| 4650 | vlen = len; |
| 4651 | if (vlen > count) { |
| 4652 | if (type != HTX_BLK_DATA) |
| 4653 | break; |
| 4654 | vlen = count; |
| 4655 | } |
| 4656 | |
| 4657 | switch (type) { |
| 4658 | case HTX_BLK_UNUSED: |
| 4659 | break; |
| 4660 | |
| 4661 | case HTX_BLK_DATA: |
| 4662 | v = htx_get_blk_value(htx, blk); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4663 | luaL_addlstring(&luactx->b, v.ptr, vlen); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4664 | break; |
| 4665 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4666 | case HTX_BLK_TLR: |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 4667 | case HTX_BLK_EOT: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4668 | len = 0; |
| 4669 | break; |
| 4670 | |
| 4671 | default: |
| 4672 | break; |
| 4673 | } |
| 4674 | |
| 4675 | co_set_data(req, co_data(req) - vlen); |
| 4676 | count -= vlen; |
| 4677 | if (len > 0) |
| 4678 | len -= vlen; |
| 4679 | if (sz == vlen) |
| 4680 | blk = htx_remove_blk(htx, blk); |
| 4681 | else { |
| 4682 | htx_cut_data_blk(htx, blk, vlen); |
| 4683 | break; |
| 4684 | } |
| 4685 | } |
| 4686 | |
Christopher Faulet | eccb31c | 2021-04-02 14:24:56 +0200 | [diff] [blame] | 4687 | /* The message was fully consumed and no more data are expected |
| 4688 | * (EOM flag set). |
| 4689 | */ |
Christopher Faulet | a5a25b1 | 2022-08-29 15:37:16 +0200 | [diff] [blame] | 4690 | if (htx_is_empty(htx) && (req->flags & CF_EOI)) |
Christopher Faulet | eccb31c | 2021-04-02 14:24:56 +0200 | [diff] [blame] | 4691 | len = 0; |
| 4692 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4693 | htx_to_buf(htx, &req->buf); |
| 4694 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4695 | /* If we are no other data available, yield waiting for new data. */ |
| 4696 | if (len) { |
| 4697 | if (len > 0) { |
| 4698 | lua_pushinteger(L, len); |
| 4699 | lua_replace(L, 2); |
| 4700 | } |
| 4701 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4702 | 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] | 4703 | } |
| 4704 | |
| 4705 | /* return the result. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4706 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4707 | return 1; |
| 4708 | } |
| 4709 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4710 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4711 | __LJMP static int hlua_applet_http_recv(lua_State *L) |
| 4712 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4713 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4714 | int len = -1; |
| 4715 | |
| 4716 | /* Check arguments. */ |
| 4717 | if (lua_gettop(L) > 2) |
| 4718 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 4719 | if (lua_gettop(L) >= 2) { |
| 4720 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4721 | lua_pop(L, 1); |
| 4722 | } |
| 4723 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4724 | lua_pushinteger(L, len); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4725 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4726 | /* Initialise the string catenation. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4727 | luaL_buffinit(L, &luactx->b); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4728 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4729 | return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4730 | } |
| 4731 | |
| 4732 | /* Append data in the output side of the buffer. This data is immediately |
| 4733 | * sent. The function returns the amount of data written. If the buffer |
| 4734 | * cannot contain the data, the function yields. The function returns -1 |
| 4735 | * if the channel is closed. |
| 4736 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4737 | __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] | 4738 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4739 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4740 | struct stream_interface *si = luactx->appctx->owner; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4741 | struct channel *res = si_ic(si); |
| 4742 | struct htx *htx = htx_from_buf(&res->buf); |
| 4743 | const char *data; |
| 4744 | size_t len; |
| 4745 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 4746 | int max; |
| 4747 | |
Christopher Faulet | 9060fc0 | 2019-07-03 11:39:30 +0200 | [diff] [blame] | 4748 | max = htx_get_max_blksz(htx, channel_htx_recv_max(res, htx)); |
Christopher Faulet | 4b0e9b2 | 2019-01-09 12:16:58 +0100 | [diff] [blame] | 4749 | if (!max) |
| 4750 | goto snd_yield; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4751 | |
| 4752 | data = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4753 | |
| 4754 | /* Get the max amount of data which can write as input in the channel. */ |
| 4755 | if (max > (len - l)) |
| 4756 | max = len - l; |
| 4757 | |
| 4758 | /* Copy data. */ |
Willy Tarreau | 0a7ef02 | 2019-05-28 10:30:11 +0200 | [diff] [blame] | 4759 | max = htx_add_data(htx, ist2(data + l, max)); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 4760 | channel_add_input(res, max); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4761 | |
| 4762 | /* update counters. */ |
| 4763 | l += max; |
| 4764 | lua_pop(L, 1); |
| 4765 | lua_pushinteger(L, l); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4766 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4767 | /* If some data is not send, declares the situation to the |
| 4768 | * applet, and returns a yield. |
| 4769 | */ |
| 4770 | if (l < len) { |
Christopher Faulet | 4b0e9b2 | 2019-01-09 12:16:58 +0100 | [diff] [blame] | 4771 | snd_yield: |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4772 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4773 | si_rx_room_blk(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4774 | 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] | 4775 | } |
| 4776 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4777 | htx_to_buf(htx, &res->buf); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4778 | return 1; |
| 4779 | } |
| 4780 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4781 | /* Just a wrapper of "hlua_applet_send_yield". This wrapper permits |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4782 | * yield the LUA process, and resume it without checking the |
| 4783 | * input arguments. |
| 4784 | */ |
| 4785 | __LJMP static int hlua_applet_http_send(lua_State *L) |
| 4786 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4787 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4788 | |
| 4789 | /* We want to send some data. Headers must be sent. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4790 | if (!(luactx->appctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4791 | hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data."); |
| 4792 | WILL_LJMP(lua_error(L)); |
| 4793 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4794 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4795 | /* This integer is used for followinf the amount of data sent. */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4796 | lua_pushinteger(L, 0); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4797 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4798 | return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4799 | } |
| 4800 | |
| 4801 | __LJMP static int hlua_applet_http_addheader(lua_State *L) |
| 4802 | { |
| 4803 | const char *name; |
| 4804 | int ret; |
| 4805 | |
| 4806 | MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4807 | name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 4808 | MAY_LJMP(luaL_checkstring(L, 3)); |
| 4809 | |
| 4810 | /* Push in the stack the "response" entry. */ |
| 4811 | ret = lua_getfield(L, 1, "response"); |
| 4812 | if (ret != LUA_TTABLE) { |
| 4813 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] " |
| 4814 | "is expected as an array. %s found", lua_typename(L, ret)); |
| 4815 | WILL_LJMP(lua_error(L)); |
| 4816 | } |
| 4817 | |
| 4818 | /* check if the header is already registered if it is not |
| 4819 | * the case, register it. |
| 4820 | */ |
| 4821 | ret = lua_getfield(L, -1, name); |
| 4822 | if (ret == LUA_TNIL) { |
| 4823 | |
| 4824 | /* Entry not found. */ |
| 4825 | lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */ |
| 4826 | |
| 4827 | /* Insert the new header name in the array in the top of the stack. |
| 4828 | * It left the new array in the top of the stack. |
| 4829 | */ |
| 4830 | lua_newtable(L); |
| 4831 | lua_pushvalue(L, 2); |
| 4832 | lua_pushvalue(L, -2); |
| 4833 | lua_settable(L, -4); |
| 4834 | |
| 4835 | } else if (ret != LUA_TTABLE) { |
| 4836 | |
| 4837 | /* corruption error. */ |
| 4838 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] " |
| 4839 | "is expected as an array. %s found", name, lua_typename(L, ret)); |
| 4840 | WILL_LJMP(lua_error(L)); |
| 4841 | } |
| 4842 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 4843 | /* Now the top of thestack is an array of values. We push |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4844 | * the header value as new entry. |
| 4845 | */ |
| 4846 | lua_pushvalue(L, 3); |
| 4847 | ret = lua_rawlen(L, -2); |
| 4848 | lua_rawseti(L, -2, ret + 1); |
| 4849 | lua_pushboolean(L, 1); |
| 4850 | return 1; |
| 4851 | } |
| 4852 | |
| 4853 | __LJMP static int hlua_applet_http_status(lua_State *L) |
| 4854 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4855 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4856 | int status = MAY_LJMP(luaL_checkinteger(L, 2)); |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4857 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4858 | |
| 4859 | if (status < 100 || status > 599) { |
| 4860 | lua_pushboolean(L, 0); |
| 4861 | return 1; |
| 4862 | } |
| 4863 | |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4864 | luactx->appctx->ctx.hlua_apphttp.status = status; |
| 4865 | luactx->appctx->ctx.hlua_apphttp.reason = reason; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4866 | lua_pushboolean(L, 1); |
| 4867 | return 1; |
| 4868 | } |
| 4869 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4870 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4871 | __LJMP static int hlua_applet_http_send_response(lua_State *L) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4872 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4873 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4874 | struct stream_interface *si = luactx->appctx->owner; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4875 | struct channel *res = si_ic(si); |
| 4876 | struct htx *htx; |
| 4877 | struct htx_sl *sl; |
| 4878 | struct h1m h1m; |
| 4879 | const char *status, *reason; |
| 4880 | const char *name, *value; |
| 4881 | size_t nlen, vlen; |
| 4882 | unsigned int flags; |
| 4883 | |
| 4884 | /* Send the message at once. */ |
| 4885 | htx = htx_from_buf(&res->buf); |
| 4886 | h1m_init_res(&h1m); |
| 4887 | |
| 4888 | /* Use the same http version than the request. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4889 | status = ultoa_r(luactx->appctx->ctx.hlua_apphttp.status, trash.area, trash.size); |
| 4890 | reason = luactx->appctx->ctx.hlua_apphttp.reason; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4891 | if (reason == NULL) |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4892 | reason = http_get_reason(luactx->appctx->ctx.hlua_apphttp.status); |
| 4893 | if (luactx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4894 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11); |
| 4895 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist(status), ist(reason)); |
| 4896 | } |
| 4897 | else { |
| 4898 | flags = HTX_SL_F_IS_RESP; |
| 4899 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), ist(status), ist(reason)); |
| 4900 | } |
| 4901 | if (!sl) { |
| 4902 | hlua_pusherror(L, "Lua applet http '%s': Failed to create response.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4903 | luactx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4904 | WILL_LJMP(lua_error(L)); |
| 4905 | } |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4906 | sl->info.res.status = luactx->appctx->ctx.hlua_apphttp.status; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4907 | |
| 4908 | /* Get the array associated to the field "response" in the object AppletHTTP. */ |
| 4909 | lua_pushvalue(L, 0); |
| 4910 | if (lua_getfield(L, 1, "response") != LUA_TTABLE) { |
| 4911 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4912 | luactx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4913 | WILL_LJMP(lua_error(L)); |
| 4914 | } |
| 4915 | |
| 4916 | /* Browse the list of headers. */ |
| 4917 | lua_pushnil(L); |
| 4918 | while(lua_next(L, -2) != 0) { |
| 4919 | /* We expect a string as -2. */ |
| 4920 | if (lua_type(L, -2) != LUA_TSTRING) { |
| 4921 | 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] | 4922 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4923 | lua_typename(L, lua_type(L, -2))); |
| 4924 | WILL_LJMP(lua_error(L)); |
| 4925 | } |
| 4926 | name = lua_tolstring(L, -2, &nlen); |
| 4927 | |
| 4928 | /* We expect an array as -1. */ |
| 4929 | if (lua_type(L, -1) != LUA_TTABLE) { |
| 4930 | 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] | 4931 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4932 | name, |
| 4933 | lua_typename(L, lua_type(L, -1))); |
| 4934 | WILL_LJMP(lua_error(L)); |
| 4935 | } |
| 4936 | |
| 4937 | /* Browse the table who is on the top of the stack. */ |
| 4938 | lua_pushnil(L); |
| 4939 | while(lua_next(L, -2) != 0) { |
| 4940 | int id; |
| 4941 | |
| 4942 | /* We expect a number as -2. */ |
| 4943 | if (lua_type(L, -2) != LUA_TNUMBER) { |
| 4944 | 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] | 4945 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4946 | name, |
| 4947 | lua_typename(L, lua_type(L, -2))); |
| 4948 | WILL_LJMP(lua_error(L)); |
| 4949 | } |
| 4950 | id = lua_tointeger(L, -2); |
| 4951 | |
| 4952 | /* We expect a string as -2. */ |
| 4953 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 4954 | 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] | 4955 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4956 | name, id, |
| 4957 | lua_typename(L, lua_type(L, -1))); |
| 4958 | WILL_LJMP(lua_error(L)); |
| 4959 | } |
| 4960 | value = lua_tolstring(L, -1, &vlen); |
| 4961 | |
| 4962 | /* Simple Protocol checks. */ |
| 4963 | if (isteqi(ist2(name, nlen), ist("transfer-encoding"))) |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 4964 | h1_parse_xfer_enc_header(&h1m, ist2(value, vlen)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4965 | else if (isteqi(ist2(name, nlen), ist("content-length"))) { |
| 4966 | struct ist v = ist2(value, vlen); |
| 4967 | int ret; |
| 4968 | |
| 4969 | ret = h1_parse_cont_len_header(&h1m, &v); |
| 4970 | if (ret < 0) { |
| 4971 | hlua_pusherror(L, "Lua applet http '%s': Invalid '%s' header.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4972 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4973 | name); |
| 4974 | WILL_LJMP(lua_error(L)); |
| 4975 | } |
| 4976 | else if (ret == 0) |
| 4977 | goto next; /* Skip it */ |
| 4978 | } |
| 4979 | |
| 4980 | /* Add a new header */ |
| 4981 | if (!htx_add_header(htx, ist2(name, nlen), ist2(value, vlen))) { |
| 4982 | 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] | 4983 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4984 | name); |
| 4985 | WILL_LJMP(lua_error(L)); |
| 4986 | } |
| 4987 | next: |
| 4988 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 4989 | lua_pop(L, 1); |
| 4990 | } |
| 4991 | |
| 4992 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 4993 | lua_pop(L, 1); |
| 4994 | } |
| 4995 | |
| 4996 | if (h1m.flags & H1_MF_CHNK) |
| 4997 | h1m.flags &= ~H1_MF_CLEN; |
| 4998 | if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK)) |
| 4999 | h1m.flags |= H1_MF_XFER_LEN; |
| 5000 | |
| 5001 | /* Uset HTX start-line flags */ |
| 5002 | if (h1m.flags & H1_MF_XFER_ENC) |
| 5003 | flags |= HTX_SL_F_XFER_ENC; |
| 5004 | if (h1m.flags & H1_MF_XFER_LEN) { |
| 5005 | flags |= HTX_SL_F_XFER_LEN; |
| 5006 | if (h1m.flags & H1_MF_CHNK) |
| 5007 | flags |= HTX_SL_F_CHNK; |
| 5008 | else if (h1m.flags & H1_MF_CLEN) |
| 5009 | flags |= HTX_SL_F_CLEN; |
| 5010 | if (h1m.body_len == 0) |
| 5011 | flags |= HTX_SL_F_BODYLESS; |
| 5012 | } |
| 5013 | sl->flags |= flags; |
| 5014 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 5015 | /* 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] | 5016 | * and the status code implies the presence of a message body, we must |
| 5017 | * announce a transfer encoding chunked. This is required by haproxy |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 5018 | * for the keepalive compliance. If the applet announces a transfer-encoding |
| 5019 | * chunked itself, don't do anything. |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5020 | */ |
| 5021 | 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] | 5022 | luactx->appctx->ctx.hlua_apphttp.status >= 200 && |
| 5023 | luactx->appctx->ctx.hlua_apphttp.status != 204 && |
| 5024 | luactx->appctx->ctx.hlua_apphttp.status != 304) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5025 | /* Add a new header */ |
| 5026 | sl->flags |= (HTX_SL_F_XFER_ENC|H1_MF_CHNK|H1_MF_XFER_LEN); |
| 5027 | if (!htx_add_header(htx, ist("transfer-encoding"), ist("chunked"))) { |
| 5028 | 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] | 5029 | luactx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5030 | WILL_LJMP(lua_error(L)); |
| 5031 | } |
| 5032 | } |
| 5033 | |
| 5034 | /* Finalize headers. */ |
| 5035 | if (!htx_add_endof(htx, HTX_BLK_EOH)) { |
| 5036 | hlua_pusherror(L, "Lua applet http '%s': Failed create the response.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5037 | luactx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5038 | WILL_LJMP(lua_error(L)); |
| 5039 | } |
| 5040 | |
| 5041 | if (htx_used_space(htx) > b_size(&res->buf) - global.tune.maxrewrite) { |
| 5042 | b_reset(&res->buf); |
| 5043 | hlua_pusherror(L, "Lua: 'start_response': response header block too big"); |
| 5044 | WILL_LJMP(lua_error(L)); |
| 5045 | } |
| 5046 | |
| 5047 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 5048 | channel_add_input(res, htx->data); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5049 | |
| 5050 | /* Headers sent, set the flag. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5051 | luactx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5052 | return 0; |
| 5053 | |
| 5054 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5055 | /* We will build the status line and the headers of the HTTP response. |
| 5056 | * We will try send at once if its not possible, we give back the hand |
| 5057 | * waiting for more room. |
| 5058 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5059 | __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] | 5060 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5061 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 5062 | struct stream_interface *si = luactx->appctx->owner; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5063 | struct channel *res = si_ic(si); |
| 5064 | |
| 5065 | if (co_data(res)) { |
| 5066 | si_rx_room_blk(si); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5067 | 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] | 5068 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5069 | return MAY_LJMP(hlua_applet_http_send_response(L)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5070 | } |
| 5071 | |
| 5072 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5073 | __LJMP static int hlua_applet_http_start_response(lua_State *L) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5074 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5075 | return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5076 | } |
| 5077 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5078 | /* |
| 5079 | * |
| 5080 | * |
| 5081 | * Class HTTP |
| 5082 | * |
| 5083 | * |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5084 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5085 | |
| 5086 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 5087 | * a class stream, otherwise it throws an error. |
| 5088 | */ |
| 5089 | __LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5090 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5091 | return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref)); |
| 5092 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5093 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5094 | /* This function creates and push in the stack a HTTP object |
| 5095 | * according with a current TXN. |
| 5096 | */ |
| 5097 | static int hlua_http_new(lua_State *L, struct hlua_txn *txn) |
| 5098 | { |
| 5099 | struct hlua_txn *htxn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5100 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5101 | /* Check stack size. */ |
| 5102 | if (!lua_checkstack(L, 3)) |
| 5103 | return 0; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5104 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5105 | /* Create the object: obj[0] = userdata. |
| 5106 | * Note that the base of the Converters object is the |
| 5107 | * same than the TXN object. |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5108 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5109 | lua_newtable(L); |
| 5110 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
| 5111 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5112 | |
| 5113 | htxn->s = txn->s; |
| 5114 | htxn->p = txn->p; |
Christopher Faulet | 256b69a | 2019-05-23 11:14:21 +0200 | [diff] [blame] | 5115 | htxn->dir = txn->dir; |
| 5116 | htxn->flags = txn->flags; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5117 | |
| 5118 | /* Pop a class stream metatable and affect it to the table. */ |
| 5119 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref); |
| 5120 | lua_setmetatable(L, -2); |
| 5121 | |
| 5122 | return 1; |
| 5123 | } |
| 5124 | |
| 5125 | /* This function creates ans returns an array of HTTP headers. |
| 5126 | * This function does not fails. It is used as wrapper with the |
| 5127 | * 2 following functions. |
| 5128 | */ |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 5129 | __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] | 5130 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5131 | struct htx *htx; |
| 5132 | int32_t pos; |
| 5133 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5134 | /* Create the table. */ |
| 5135 | lua_newtable(L); |
| 5136 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5137 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5138 | htx = htxbuf(&msg->chn->buf); |
| 5139 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 5140 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 5141 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 5142 | struct ist n, v; |
| 5143 | int len; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5144 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5145 | if (type == HTX_BLK_HDR) { |
| 5146 | n = htx_get_blk_name(htx,blk); |
| 5147 | v = htx_get_blk_value(htx, blk); |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5148 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5149 | else if (type == HTX_BLK_EOH) |
| 5150 | break; |
| 5151 | else |
| 5152 | continue; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5153 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5154 | /* Check for existing entry: |
| 5155 | * assume that the table is on the top of the stack, and |
| 5156 | * push the key in the stack, the function lua_gettable() |
| 5157 | * perform the lookup. |
| 5158 | */ |
| 5159 | lua_pushlstring(L, n.ptr, n.len); |
| 5160 | lua_gettable(L, -2); |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5161 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5162 | switch (lua_type(L, -1)) { |
| 5163 | case LUA_TNIL: |
| 5164 | /* Table not found, create it. */ |
| 5165 | lua_pop(L, 1); /* remove the nil value. */ |
| 5166 | lua_pushlstring(L, n.ptr, n.len); /* push the header name as key. */ |
| 5167 | lua_newtable(L); /* create and push empty table. */ |
| 5168 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 5169 | lua_rawseti(L, -2, 0); /* index header value (pop it). */ |
| 5170 | 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] | 5171 | break; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5172 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5173 | case LUA_TTABLE: |
| 5174 | /* Entry found: push the value in the table. */ |
| 5175 | len = lua_rawlen(L, -1); |
| 5176 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 5177 | lua_rawseti(L, -2, len+1); /* index header value (pop it). */ |
| 5178 | lua_pop(L, 1); /* remove the table (it is stored in the main table). */ |
| 5179 | break; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5180 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5181 | default: |
| 5182 | /* Other cases are errors. */ |
| 5183 | hlua_pusherror(L, "internal error during the parsing of headers."); |
| 5184 | WILL_LJMP(lua_error(L)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5185 | } |
| 5186 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5187 | return 1; |
| 5188 | } |
| 5189 | |
| 5190 | __LJMP static int hlua_http_req_get_headers(lua_State *L) |
| 5191 | { |
| 5192 | struct hlua_txn *htxn; |
| 5193 | |
| 5194 | MAY_LJMP(check_args(L, 1, "req_get_headers")); |
| 5195 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5196 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5197 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5198 | WILL_LJMP(lua_error(L)); |
| 5199 | |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 5200 | return hlua_http_get_headers(L, &htxn->s->txn->req); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5201 | } |
| 5202 | |
| 5203 | __LJMP static int hlua_http_res_get_headers(lua_State *L) |
| 5204 | { |
| 5205 | struct hlua_txn *htxn; |
| 5206 | |
| 5207 | MAY_LJMP(check_args(L, 1, "res_get_headers")); |
| 5208 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5209 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5210 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5211 | WILL_LJMP(lua_error(L)); |
| 5212 | |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 5213 | return hlua_http_get_headers(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5214 | } |
| 5215 | |
| 5216 | /* This function replace full header, or just a value in |
| 5217 | * the request or in the response. It is a wrapper fir the |
| 5218 | * 4 following functions. |
| 5219 | */ |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5220 | __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] | 5221 | { |
| 5222 | size_t name_len; |
| 5223 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5224 | const char *reg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 5225 | const char *value = MAY_LJMP(luaL_checkstring(L, 4)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5226 | struct htx *htx; |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 5227 | struct my_regex *re; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5228 | |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 5229 | if (!(re = regex_comp(reg, 1, 1, NULL))) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5230 | WILL_LJMP(luaL_argerror(L, 3, "invalid regex")); |
| 5231 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5232 | htx = htxbuf(&msg->chn->buf); |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5233 | 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] | 5234 | regex_free(re); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5235 | return 0; |
| 5236 | } |
| 5237 | |
| 5238 | __LJMP static int hlua_http_req_rep_hdr(lua_State *L) |
| 5239 | { |
| 5240 | struct hlua_txn *htxn; |
| 5241 | |
| 5242 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 5243 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5244 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5245 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5246 | WILL_LJMP(lua_error(L)); |
| 5247 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5248 | 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] | 5249 | } |
| 5250 | |
| 5251 | __LJMP static int hlua_http_res_rep_hdr(lua_State *L) |
| 5252 | { |
| 5253 | struct hlua_txn *htxn; |
| 5254 | |
| 5255 | MAY_LJMP(check_args(L, 4, "res_rep_hdr")); |
| 5256 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5257 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5258 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5259 | WILL_LJMP(lua_error(L)); |
| 5260 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5261 | 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] | 5262 | } |
| 5263 | |
| 5264 | __LJMP static int hlua_http_req_rep_val(lua_State *L) |
| 5265 | { |
| 5266 | struct hlua_txn *htxn; |
| 5267 | |
| 5268 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 5269 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5270 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5271 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5272 | WILL_LJMP(lua_error(L)); |
| 5273 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5274 | 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] | 5275 | } |
| 5276 | |
| 5277 | __LJMP static int hlua_http_res_rep_val(lua_State *L) |
| 5278 | { |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5279 | struct hlua_txn *htxn; |
| 5280 | |
| 5281 | MAY_LJMP(check_args(L, 4, "res_rep_val")); |
| 5282 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5283 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5284 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5285 | WILL_LJMP(lua_error(L)); |
| 5286 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5287 | 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] | 5288 | } |
| 5289 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5290 | /* This function deletes all the occurrences of an header. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5291 | * It is a wrapper for the 2 following functions. |
| 5292 | */ |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5293 | __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] | 5294 | { |
| 5295 | size_t len; |
| 5296 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5297 | struct htx *htx = htxbuf(&msg->chn->buf); |
| 5298 | struct http_hdr_ctx ctx; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5299 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5300 | ctx.blk = NULL; |
| 5301 | while (http_find_header(htx, ist2(name, len), &ctx, 1)) |
| 5302 | http_remove_header(htx, &ctx); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5303 | return 0; |
| 5304 | } |
| 5305 | |
| 5306 | __LJMP static int hlua_http_req_del_hdr(lua_State *L) |
| 5307 | { |
| 5308 | struct hlua_txn *htxn; |
| 5309 | |
| 5310 | MAY_LJMP(check_args(L, 2, "req_del_hdr")); |
| 5311 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5312 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5313 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5314 | WILL_LJMP(lua_error(L)); |
| 5315 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5316 | return hlua_http_del_hdr(L, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5317 | } |
| 5318 | |
| 5319 | __LJMP static int hlua_http_res_del_hdr(lua_State *L) |
| 5320 | { |
| 5321 | struct hlua_txn *htxn; |
| 5322 | |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5323 | MAY_LJMP(check_args(L, 2, "res_del_hdr")); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5324 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5325 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5326 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5327 | WILL_LJMP(lua_error(L)); |
| 5328 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5329 | return hlua_http_del_hdr(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5330 | } |
| 5331 | |
| 5332 | /* This function adds an header. It is a wrapper used by |
| 5333 | * the 2 following functions. |
| 5334 | */ |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5335 | __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] | 5336 | { |
| 5337 | size_t name_len; |
| 5338 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5339 | size_t value_len; |
| 5340 | const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5341 | struct htx *htx = htxbuf(&msg->chn->buf); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5342 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5343 | lua_pushboolean(L, http_add_header(htx, ist2(name, name_len), |
| 5344 | ist2(value, value_len))); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5345 | return 0; |
| 5346 | } |
| 5347 | |
| 5348 | __LJMP static int hlua_http_req_add_hdr(lua_State *L) |
| 5349 | { |
| 5350 | struct hlua_txn *htxn; |
| 5351 | |
| 5352 | MAY_LJMP(check_args(L, 3, "req_add_hdr")); |
| 5353 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5354 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5355 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5356 | WILL_LJMP(lua_error(L)); |
| 5357 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5358 | return hlua_http_add_hdr(L, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5359 | } |
| 5360 | |
| 5361 | __LJMP static int hlua_http_res_add_hdr(lua_State *L) |
| 5362 | { |
| 5363 | struct hlua_txn *htxn; |
| 5364 | |
| 5365 | MAY_LJMP(check_args(L, 3, "res_add_hdr")); |
| 5366 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5367 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5368 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5369 | WILL_LJMP(lua_error(L)); |
| 5370 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5371 | return hlua_http_add_hdr(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5372 | } |
| 5373 | |
| 5374 | static int hlua_http_req_set_hdr(lua_State *L) |
| 5375 | { |
| 5376 | struct hlua_txn *htxn; |
| 5377 | |
| 5378 | MAY_LJMP(check_args(L, 3, "req_set_hdr")); |
| 5379 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5380 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5381 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5382 | WILL_LJMP(lua_error(L)); |
| 5383 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5384 | hlua_http_del_hdr(L, &htxn->s->txn->req); |
| 5385 | return hlua_http_add_hdr(L, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5386 | } |
| 5387 | |
| 5388 | static int hlua_http_res_set_hdr(lua_State *L) |
| 5389 | { |
| 5390 | struct hlua_txn *htxn; |
| 5391 | |
| 5392 | MAY_LJMP(check_args(L, 3, "res_set_hdr")); |
| 5393 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5394 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5395 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5396 | WILL_LJMP(lua_error(L)); |
| 5397 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5398 | hlua_http_del_hdr(L, &htxn->s->txn->rsp); |
| 5399 | return hlua_http_add_hdr(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5400 | } |
| 5401 | |
| 5402 | /* This function set the method. */ |
| 5403 | static int hlua_http_req_set_meth(lua_State *L) |
| 5404 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5405 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5406 | size_t name_len; |
| 5407 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5408 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5409 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5410 | WILL_LJMP(lua_error(L)); |
| 5411 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5412 | 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] | 5413 | return 1; |
| 5414 | } |
| 5415 | |
| 5416 | /* This function set the method. */ |
| 5417 | static int hlua_http_req_set_path(lua_State *L) |
| 5418 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5419 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5420 | size_t name_len; |
| 5421 | 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] | 5422 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5423 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5424 | WILL_LJMP(lua_error(L)); |
| 5425 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5426 | 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] | 5427 | return 1; |
| 5428 | } |
| 5429 | |
| 5430 | /* This function set the query-string. */ |
| 5431 | static int hlua_http_req_set_query(lua_State *L) |
| 5432 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5433 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5434 | size_t name_len; |
| 5435 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5436 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5437 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5438 | WILL_LJMP(lua_error(L)); |
| 5439 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5440 | /* Check length. */ |
| 5441 | if (name_len > trash.size - 1) { |
| 5442 | lua_pushboolean(L, 0); |
| 5443 | return 1; |
| 5444 | } |
| 5445 | |
| 5446 | /* Add the mark question as prefix. */ |
| 5447 | chunk_reset(&trash); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5448 | trash.area[trash.data++] = '?'; |
| 5449 | memcpy(trash.area + trash.data, name, name_len); |
| 5450 | trash.data += name_len; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5451 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5452 | lua_pushboolean(L, |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5453 | 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] | 5454 | return 1; |
| 5455 | } |
| 5456 | |
| 5457 | /* This function set the uri. */ |
| 5458 | static int hlua_http_req_set_uri(lua_State *L) |
| 5459 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5460 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5461 | size_t name_len; |
| 5462 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5463 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5464 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5465 | WILL_LJMP(lua_error(L)); |
| 5466 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5467 | 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] | 5468 | return 1; |
| 5469 | } |
| 5470 | |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5471 | /* This function set the response code & optionally reason. */ |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5472 | static int hlua_http_res_set_status(lua_State *L) |
| 5473 | { |
| 5474 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5475 | unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2)); |
Christopher Faulet | 96bff76 | 2019-12-17 13:46:18 +0100 | [diff] [blame] | 5476 | const char *str = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
| 5477 | const struct ist reason = ist2(str, (str ? strlen(str) : 0)); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5478 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5479 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5480 | WILL_LJMP(lua_error(L)); |
| 5481 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5482 | http_res_set_status(code, reason, htxn->s); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5483 | return 0; |
| 5484 | } |
| 5485 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5486 | /* |
| 5487 | * |
| 5488 | * |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5489 | * Class TXN |
| 5490 | * |
| 5491 | * |
| 5492 | */ |
| 5493 | |
| 5494 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5495 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5496 | */ |
| 5497 | __LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud) |
| 5498 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 5499 | return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref)); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5500 | } |
| 5501 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5502 | __LJMP static int hlua_set_var(lua_State *L) |
| 5503 | { |
| 5504 | struct hlua_txn *htxn; |
| 5505 | const char *name; |
| 5506 | size_t len; |
| 5507 | struct sample smp; |
| 5508 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 5509 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 5510 | 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] | 5511 | |
| 5512 | /* It is useles to retrieve the stream, but this function |
| 5513 | * runs only in a stream context. |
| 5514 | */ |
| 5515 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5516 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5517 | |
| 5518 | /* Converts the third argument in a sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 5519 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5520 | hlua_lua2smp(L, 3, &smp); |
| 5521 | |
Aurelien DARRAGON | d39fb74 | 2023-05-17 16:06:11 +0200 | [diff] [blame] | 5522 | /* Store the sample in a variable. We don't need to dup the smp, vars API |
| 5523 | * already takes care of duplicating dynamic var data. |
| 5524 | */ |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 5525 | 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] | 5526 | |
| 5527 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 5528 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 5529 | else |
| 5530 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 5531 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 5532 | return 1; |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5533 | } |
| 5534 | |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 5535 | __LJMP static int hlua_unset_var(lua_State *L) |
| 5536 | { |
| 5537 | struct hlua_txn *htxn; |
| 5538 | const char *name; |
| 5539 | size_t len; |
| 5540 | struct sample smp; |
| 5541 | |
| 5542 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 5543 | |
| 5544 | /* It is useles to retrieve the stream, but this function |
| 5545 | * runs only in a stream context. |
| 5546 | */ |
| 5547 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5548 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5549 | |
| 5550 | /* Unset the variable. */ |
| 5551 | 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] | 5552 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 5553 | return 1; |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 5554 | } |
| 5555 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5556 | __LJMP static int hlua_get_var(lua_State *L) |
| 5557 | { |
| 5558 | struct hlua_txn *htxn; |
| 5559 | const char *name; |
| 5560 | size_t len; |
| 5561 | struct sample smp; |
| 5562 | |
| 5563 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 5564 | |
| 5565 | /* It is useles to retrieve the stream, but this function |
| 5566 | * runs only in a stream context. |
| 5567 | */ |
| 5568 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5569 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5570 | |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 5571 | 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] | 5572 | if (!vars_get_by_name(name, len, &smp)) { |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5573 | lua_pushnil(L); |
| 5574 | return 1; |
| 5575 | } |
| 5576 | |
| 5577 | return hlua_smp2lua(L, &smp); |
| 5578 | } |
| 5579 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5580 | __LJMP static int hlua_set_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5581 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5582 | struct hlua *hlua; |
| 5583 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5584 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 5585 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5586 | /* It is useles to retrieve the stream, but this function |
| 5587 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5588 | */ |
| 5589 | MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 5590 | |
| 5591 | /* Get hlua struct, or NULL if we execute from main lua state */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5592 | hlua = hlua_gethlua(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 5593 | if (!hlua) |
| 5594 | return 0; |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5595 | |
| 5596 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 5597 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5598 | |
| 5599 | /* Get and store new value. */ |
| 5600 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 5601 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 5602 | |
| 5603 | return 0; |
| 5604 | } |
| 5605 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5606 | __LJMP static int hlua_get_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5607 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5608 | struct hlua *hlua; |
| 5609 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5610 | MAY_LJMP(check_args(L, 1, "get_priv")); |
| 5611 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5612 | /* It is useles to retrieve the stream, but this function |
| 5613 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5614 | */ |
| 5615 | MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 5616 | |
| 5617 | /* Get hlua struct, or NULL if we execute from main lua state */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5618 | hlua = hlua_gethlua(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 5619 | if (!hlua) { |
| 5620 | lua_pushnil(L); |
| 5621 | return 1; |
| 5622 | } |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5623 | |
| 5624 | /* Push configuration index in the stack. */ |
| 5625 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 5626 | |
| 5627 | return 1; |
| 5628 | } |
| 5629 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5630 | /* Create stack entry containing a class TXN. This function |
| 5631 | * return 0 if the stack does not contains free slots, |
| 5632 | * otherwise it returns 1. |
| 5633 | */ |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5634 | 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] | 5635 | { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5636 | struct hlua_txn *htxn; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5637 | |
| 5638 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5639 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5640 | return 0; |
| 5641 | |
| 5642 | /* NOTE: The allocation never fails. The failure |
| 5643 | * throw an error, and the function never returns. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5644 | * if the throw is not available, the process is aborted. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5645 | */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5646 | /* Create the object: obj[0] = userdata. */ |
| 5647 | lua_newtable(L); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5648 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5649 | lua_rawseti(L, -2, 0); |
| 5650 | |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5651 | htxn->s = s; |
| 5652 | htxn->p = p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 5653 | htxn->dir = dir; |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5654 | htxn->flags = flags; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5655 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 5656 | /* Create the "f" field that contains a list of fetches. */ |
| 5657 | lua_pushstring(L, "f"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 5658 | if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5659 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5660 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5661 | |
| 5662 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 5663 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 5664 | 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] | 5665 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5666 | lua_rawset(L, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 5667 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5668 | /* Create the "c" field that contains a list of converters. */ |
| 5669 | lua_pushstring(L, "c"); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5670 | if (!hlua_converters_new(L, htxn, 0)) |
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 "sc" field that contains a list of stringsafe converters. */ |
| 5675 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 5676 | if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +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 | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5679 | |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5680 | /* Create the "req" field that contains the request channel object. */ |
| 5681 | lua_pushstring(L, "req"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 5682 | if (!hlua_channel_new(L, &s->req)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +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 | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5685 | |
| 5686 | /* Create the "res" field that contains the response channel object. */ |
| 5687 | lua_pushstring(L, "res"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 5688 | if (!hlua_channel_new(L, &s->res)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +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 | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5691 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5692 | /* Creates the HTTP object is the current proxy allows http. */ |
| 5693 | lua_pushstring(L, "http"); |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 5694 | if (IS_HTX_STRM(s)) { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5695 | if (!hlua_http_new(L, htxn)) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5696 | return 0; |
| 5697 | } |
| 5698 | else |
| 5699 | lua_pushnil(L); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5700 | lua_rawset(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5701 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5702 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 5703 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref); |
| 5704 | lua_setmetatable(L, -2); |
| 5705 | |
| 5706 | return 1; |
| 5707 | } |
| 5708 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 5709 | __LJMP static int hlua_txn_deflog(lua_State *L) |
| 5710 | { |
| 5711 | const char *msg; |
| 5712 | struct hlua_txn *htxn; |
| 5713 | |
| 5714 | MAY_LJMP(check_args(L, 2, "deflog")); |
| 5715 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5716 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5717 | |
| 5718 | hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg); |
| 5719 | return 0; |
| 5720 | } |
| 5721 | |
| 5722 | __LJMP static int hlua_txn_log(lua_State *L) |
| 5723 | { |
| 5724 | int level; |
| 5725 | const char *msg; |
| 5726 | struct hlua_txn *htxn; |
| 5727 | |
| 5728 | MAY_LJMP(check_args(L, 3, "log")); |
| 5729 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5730 | level = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5731 | msg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 5732 | |
| 5733 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 5734 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 5735 | |
| 5736 | hlua_sendlog(htxn->s->be, level, msg); |
| 5737 | return 0; |
| 5738 | } |
| 5739 | |
| 5740 | __LJMP static int hlua_txn_log_debug(lua_State *L) |
| 5741 | { |
| 5742 | const char *msg; |
| 5743 | struct hlua_txn *htxn; |
| 5744 | |
| 5745 | MAY_LJMP(check_args(L, 2, "Debug")); |
| 5746 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5747 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5748 | hlua_sendlog(htxn->s->be, LOG_DEBUG, msg); |
| 5749 | return 0; |
| 5750 | } |
| 5751 | |
| 5752 | __LJMP static int hlua_txn_log_info(lua_State *L) |
| 5753 | { |
| 5754 | const char *msg; |
| 5755 | struct hlua_txn *htxn; |
| 5756 | |
| 5757 | MAY_LJMP(check_args(L, 2, "Info")); |
| 5758 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5759 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5760 | hlua_sendlog(htxn->s->be, LOG_INFO, msg); |
| 5761 | return 0; |
| 5762 | } |
| 5763 | |
| 5764 | __LJMP static int hlua_txn_log_warning(lua_State *L) |
| 5765 | { |
| 5766 | const char *msg; |
| 5767 | struct hlua_txn *htxn; |
| 5768 | |
| 5769 | MAY_LJMP(check_args(L, 2, "Warning")); |
| 5770 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5771 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5772 | hlua_sendlog(htxn->s->be, LOG_WARNING, msg); |
| 5773 | return 0; |
| 5774 | } |
| 5775 | |
| 5776 | __LJMP static int hlua_txn_log_alert(lua_State *L) |
| 5777 | { |
| 5778 | const char *msg; |
| 5779 | struct hlua_txn *htxn; |
| 5780 | |
| 5781 | MAY_LJMP(check_args(L, 2, "Alert")); |
| 5782 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5783 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5784 | hlua_sendlog(htxn->s->be, LOG_ALERT, msg); |
| 5785 | return 0; |
| 5786 | } |
| 5787 | |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5788 | __LJMP static int hlua_txn_set_loglevel(lua_State *L) |
| 5789 | { |
| 5790 | struct hlua_txn *htxn; |
| 5791 | int ll; |
| 5792 | |
| 5793 | MAY_LJMP(check_args(L, 2, "set_loglevel")); |
| 5794 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5795 | ll = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5796 | |
Christopher Faulet | 8263e94 | 2024-02-29 15:41:17 +0100 | [diff] [blame] | 5797 | if (ll < -1 || ll > NB_LOG_LEVELS) |
| 5798 | WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be one of the following value:" |
| 5799 | " core.silent(-1), core.emerg(0), core.alert(1), core.crit(2), core.error(3)," |
| 5800 | " 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] | 5801 | |
Christopher Faulet | 8263e94 | 2024-02-29 15:41:17 +0100 | [diff] [blame] | 5802 | htxn->s->logs.level = (ll == -1) ? ll : ll + 1; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5803 | return 0; |
| 5804 | } |
| 5805 | |
| 5806 | __LJMP static int hlua_txn_set_tos(lua_State *L) |
| 5807 | { |
| 5808 | struct hlua_txn *htxn; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5809 | int tos; |
| 5810 | |
| 5811 | MAY_LJMP(check_args(L, 2, "set_tos")); |
| 5812 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5813 | tos = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5814 | |
Willy Tarreau | 1a18b54 | 2018-12-11 16:37:42 +0100 | [diff] [blame] | 5815 | conn_set_tos(objt_conn(htxn->s->sess->origin), tos); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5816 | return 0; |
| 5817 | } |
| 5818 | |
| 5819 | __LJMP static int hlua_txn_set_mark(lua_State *L) |
| 5820 | { |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5821 | struct hlua_txn *htxn; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5822 | int mark; |
| 5823 | |
| 5824 | MAY_LJMP(check_args(L, 2, "set_mark")); |
| 5825 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5826 | mark = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5827 | |
Lukas Tribus | 579e3e3 | 2019-08-11 18:03:45 +0200 | [diff] [blame] | 5828 | conn_set_mark(objt_conn(htxn->s->sess->origin), mark); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5829 | return 0; |
| 5830 | } |
| 5831 | |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 5832 | __LJMP static int hlua_txn_set_priority_class(lua_State *L) |
| 5833 | { |
| 5834 | struct hlua_txn *htxn; |
| 5835 | |
| 5836 | MAY_LJMP(check_args(L, 2, "set_priority_class")); |
| 5837 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5838 | htxn->s->priority_class = queue_limit_class(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 5839 | return 0; |
| 5840 | } |
| 5841 | |
| 5842 | __LJMP static int hlua_txn_set_priority_offset(lua_State *L) |
| 5843 | { |
| 5844 | struct hlua_txn *htxn; |
| 5845 | |
| 5846 | MAY_LJMP(check_args(L, 2, "set_priority_offset")); |
| 5847 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5848 | htxn->s->priority_offset = queue_limit_offset(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 5849 | return 0; |
| 5850 | } |
| 5851 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5852 | /* 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] | 5853 | * 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] | 5854 | * message and terminate the transaction. It returns 1 on success and 0 on |
| 5855 | * error. The Reply must be on top of the stack. |
| 5856 | */ |
| 5857 | __LJMP static int hlua_txn_forward_reply(lua_State *L, struct stream *s) |
| 5858 | { |
| 5859 | struct htx *htx; |
| 5860 | struct htx_sl *sl; |
| 5861 | struct h1m h1m; |
| 5862 | const char *status, *reason, *body; |
| 5863 | size_t status_len, reason_len, body_len; |
| 5864 | int ret, code, flags; |
| 5865 | |
| 5866 | code = 200; |
| 5867 | status = "200"; |
| 5868 | status_len = 3; |
| 5869 | ret = lua_getfield(L, -1, "status"); |
| 5870 | if (ret == LUA_TNUMBER) { |
| 5871 | code = lua_tointeger(L, -1); |
| 5872 | status = lua_tolstring(L, -1, &status_len); |
| 5873 | } |
| 5874 | lua_pop(L, 1); |
| 5875 | |
| 5876 | reason = http_get_reason(code); |
| 5877 | reason_len = strlen(reason); |
| 5878 | ret = lua_getfield(L, -1, "reason"); |
| 5879 | if (ret == LUA_TSTRING) |
| 5880 | reason = lua_tolstring(L, -1, &reason_len); |
| 5881 | lua_pop(L, 1); |
| 5882 | |
| 5883 | body = NULL; |
| 5884 | body_len = 0; |
| 5885 | ret = lua_getfield(L, -1, "body"); |
| 5886 | if (ret == LUA_TSTRING) |
| 5887 | body = lua_tolstring(L, -1, &body_len); |
| 5888 | lua_pop(L, 1); |
| 5889 | |
| 5890 | /* Prepare the response before inserting the headers */ |
| 5891 | h1m_init_res(&h1m); |
| 5892 | htx = htx_from_buf(&s->res.buf); |
| 5893 | channel_htx_truncate(&s->res, htx); |
| 5894 | if (s->txn->req.flags & HTTP_MSGF_VER_11) { |
| 5895 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11); |
| 5896 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), |
| 5897 | ist2(status, status_len), ist2(reason, reason_len)); |
| 5898 | } |
| 5899 | else { |
| 5900 | flags = HTX_SL_F_IS_RESP; |
| 5901 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), |
| 5902 | ist2(status, status_len), ist2(reason, reason_len)); |
| 5903 | } |
| 5904 | if (!sl) |
| 5905 | goto fail; |
| 5906 | sl->info.res.status = code; |
| 5907 | |
| 5908 | /* Push in the stack the "headers" entry. */ |
| 5909 | ret = lua_getfield(L, -1, "headers"); |
| 5910 | if (ret != LUA_TTABLE) |
| 5911 | goto skip_headers; |
| 5912 | |
| 5913 | lua_pushnil(L); |
| 5914 | while (lua_next(L, -2) != 0) { |
| 5915 | struct ist name, value; |
| 5916 | const char *n, *v; |
| 5917 | size_t nlen, vlen; |
| 5918 | |
| 5919 | if (!lua_isstring(L, -2) || !lua_istable(L, -1)) { |
| 5920 | /* Skip element if the key is not a string or if the value is not a table */ |
| 5921 | goto next_hdr; |
| 5922 | } |
| 5923 | |
| 5924 | n = lua_tolstring(L, -2, &nlen); |
| 5925 | name = ist2(n, nlen); |
| 5926 | if (isteqi(name, ist("content-length"))) { |
| 5927 | /* Always skip content-length header. It will be added |
| 5928 | * later with the correct len |
| 5929 | */ |
| 5930 | goto next_hdr; |
| 5931 | } |
| 5932 | |
| 5933 | /* Loop on header's values */ |
| 5934 | lua_pushnil(L); |
| 5935 | while (lua_next(L, -2)) { |
| 5936 | if (!lua_isstring(L, -1)) { |
| 5937 | /* Skip the value if it is not a string */ |
| 5938 | goto next_value; |
| 5939 | } |
| 5940 | |
| 5941 | v = lua_tolstring(L, -1, &vlen); |
| 5942 | value = ist2(v, vlen); |
| 5943 | |
| 5944 | if (isteqi(name, ist("transfer-encoding"))) |
| 5945 | h1_parse_xfer_enc_header(&h1m, value); |
| 5946 | if (!htx_add_header(htx, ist2(n, nlen), ist2(v, vlen))) |
| 5947 | goto fail; |
| 5948 | |
| 5949 | next_value: |
| 5950 | lua_pop(L, 1); |
| 5951 | } |
| 5952 | |
| 5953 | next_hdr: |
| 5954 | lua_pop(L, 1); |
| 5955 | } |
| 5956 | skip_headers: |
| 5957 | lua_pop(L, 1); |
| 5958 | |
| 5959 | /* Update h1m flags: CLEN is set if CHNK is not present */ |
| 5960 | if (!(h1m.flags & H1_MF_CHNK)) { |
| 5961 | const char *clen = ultoa(body_len); |
| 5962 | |
| 5963 | h1m.flags |= H1_MF_CLEN; |
| 5964 | if (!htx_add_header(htx, ist("content-length"), ist(clen))) |
| 5965 | goto fail; |
| 5966 | } |
| 5967 | if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK)) |
| 5968 | h1m.flags |= H1_MF_XFER_LEN; |
| 5969 | |
| 5970 | /* Update HTX start-line flags */ |
| 5971 | if (h1m.flags & H1_MF_XFER_ENC) |
| 5972 | flags |= HTX_SL_F_XFER_ENC; |
| 5973 | if (h1m.flags & H1_MF_XFER_LEN) { |
| 5974 | flags |= HTX_SL_F_XFER_LEN; |
| 5975 | if (h1m.flags & H1_MF_CHNK) |
| 5976 | flags |= HTX_SL_F_CHNK; |
| 5977 | else if (h1m.flags & H1_MF_CLEN) |
| 5978 | flags |= HTX_SL_F_CLEN; |
| 5979 | if (h1m.body_len == 0) |
| 5980 | flags |= HTX_SL_F_BODYLESS; |
| 5981 | } |
| 5982 | sl->flags |= flags; |
| 5983 | |
| 5984 | |
| 5985 | if (!htx_add_endof(htx, HTX_BLK_EOH) || |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 5986 | (body_len && !htx_add_data_atonce(htx, ist2(body, body_len)))) |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5987 | goto fail; |
| 5988 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 5989 | htx->flags |= HTX_FL_EOM; |
| 5990 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5991 | /* Now, forward the response and terminate the transaction */ |
| 5992 | s->txn->status = code; |
| 5993 | htx_to_buf(htx, &s->res.buf); |
| 5994 | if (!http_forward_proxy_resp(s, 1)) |
| 5995 | goto fail; |
| 5996 | |
| 5997 | return 1; |
| 5998 | |
| 5999 | fail: |
| 6000 | channel_htx_truncate(&s->res, htx); |
| 6001 | return 0; |
| 6002 | } |
| 6003 | |
| 6004 | /* Terminate a transaction if called from a lua action. For TCP streams, |
| 6005 | * processing is just aborted. Nothing is returned to the client and all |
| 6006 | * arguments are ignored. For HTTP streams, if a reply is passed as argument, it |
| 6007 | * is forwarded to the client before terminating the transaction. On success, |
| 6008 | * the function exits with ACT_RET_DONE code. If an error occurred, it exits |
| 6009 | * with ACT_RET_ERR code. If this function is not called from a lua action, it |
| 6010 | * just exits without any processing. |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 6011 | */ |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 6012 | __LJMP static int hlua_txn_done(lua_State *L) |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 6013 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 6014 | struct hlua_txn *htxn; |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 6015 | struct stream *s; |
| 6016 | int finst; |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 6017 | |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 6018 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 6019 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 6020 | /* If the flags NOTERM is set, we cannot terminate the session, so we |
| 6021 | * just end the execution of the current lua code. */ |
| 6022 | if (htxn->flags & HLUA_TXN_NOTERM) |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 6023 | WILL_LJMP(hlua_done(L)); |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 6024 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 6025 | s = htxn->s; |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 6026 | if (!IS_HTX_STRM(htxn->s)) { |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 6027 | struct channel *req = &s->req; |
| 6028 | struct channel *res = &s->res; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 6029 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 6030 | channel_auto_read(req); |
| 6031 | channel_abort(req); |
| 6032 | channel_auto_close(req); |
| 6033 | channel_erase(req); |
| 6034 | |
| 6035 | res->wex = tick_add_ifset(now_ms, res->wto); |
| 6036 | channel_auto_read(res); |
| 6037 | channel_auto_close(res); |
| 6038 | channel_shutr_now(res); |
| 6039 | |
| 6040 | finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_D); |
| 6041 | goto done; |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 6042 | } |
Thierry FOURNIER | 10ec214 | 2015-08-24 17:23:45 +0200 | [diff] [blame] | 6043 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 6044 | if (lua_gettop(L) == 1 || !lua_istable(L, 2)) { |
| 6045 | /* No reply or invalid reply */ |
| 6046 | s->txn->status = 0; |
| 6047 | http_reply_and_close(s, 0, NULL); |
| 6048 | } |
| 6049 | else { |
| 6050 | /* Remove extra args to have the reply on top of the stack */ |
| 6051 | if (lua_gettop(L) > 2) |
| 6052 | lua_pop(L, lua_gettop(L) - 2); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 6053 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 6054 | if (!hlua_txn_forward_reply(L, s)) { |
| 6055 | if (!(s->flags & SF_ERR_MASK)) |
| 6056 | s->flags |= SF_ERR_PRXCOND; |
| 6057 | lua_pushinteger(L, ACT_RET_ERR); |
| 6058 | WILL_LJMP(hlua_done(L)); |
| 6059 | return 0; /* Never reached */ |
| 6060 | } |
Christopher Faulet | 4d0e263 | 2019-07-16 10:52:40 +0200 | [diff] [blame] | 6061 | } |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 6062 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 6063 | finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_H); |
| 6064 | if (htxn->dir == SMP_OPT_DIR_REQ) { |
| 6065 | /* let's log the request time */ |
| 6066 | s->logs.tv_request = now; |
| 6067 | 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] | 6068 | _HA_ATOMIC_INC(&s->sess->fe->fe_counters.intercepted_req); |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 6069 | } |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 6070 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 6071 | done: |
| 6072 | if (!(s->flags & SF_ERR_MASK)) |
| 6073 | s->flags |= SF_ERR_LOCAL; |
| 6074 | if (!(s->flags & SF_FINST_MASK)) |
| 6075 | s->flags |= finst; |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 6076 | |
Christopher Faulet | 4ad7310 | 2020-03-05 11:07:31 +0100 | [diff] [blame] | 6077 | lua_pushinteger(L, ACT_RET_ABRT); |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 6078 | WILL_LJMP(hlua_done(L)); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 6079 | return 0; |
| 6080 | } |
| 6081 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 6082 | /* |
| 6083 | * |
| 6084 | * |
| 6085 | * Class REPLY |
| 6086 | * |
| 6087 | * |
| 6088 | */ |
| 6089 | |
| 6090 | /* Pushes the TXN reply onto the top of the stack. If the stask does not have a |
| 6091 | * free slots, the function fails and returns 0; |
| 6092 | */ |
| 6093 | static int hlua_txn_reply_new(lua_State *L) |
| 6094 | { |
| 6095 | struct hlua_txn *htxn; |
| 6096 | const char *reason, *body = NULL; |
| 6097 | int ret, status; |
| 6098 | |
| 6099 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 6100 | if (!IS_HTX_STRM(htxn->s)) { |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 6101 | hlua_pusherror(L, "txn object is not an HTTP transaction."); |
| 6102 | WILL_LJMP(lua_error(L)); |
| 6103 | } |
| 6104 | |
| 6105 | /* Default value */ |
| 6106 | status = 200; |
| 6107 | reason = http_get_reason(status); |
| 6108 | |
| 6109 | if (lua_istable(L, 2)) { |
| 6110 | /* load status and reason from the table argument at index 2 */ |
| 6111 | ret = lua_getfield(L, 2, "status"); |
| 6112 | if (ret == LUA_TNIL) |
| 6113 | goto reason; |
| 6114 | else if (ret != LUA_TNUMBER) { |
| 6115 | /* invalid status: ignore the reason */ |
| 6116 | goto body; |
| 6117 | } |
| 6118 | status = lua_tointeger(L, -1); |
| 6119 | |
| 6120 | reason: |
| 6121 | lua_pop(L, 1); /* restore the stack: remove status */ |
| 6122 | ret = lua_getfield(L, 2, "reason"); |
| 6123 | if (ret == LUA_TSTRING) |
| 6124 | reason = lua_tostring(L, -1); |
| 6125 | |
| 6126 | body: |
| 6127 | lua_pop(L, 1); /* restore the stack: remove invalid status or reason */ |
| 6128 | ret = lua_getfield(L, 2, "body"); |
| 6129 | if (ret == LUA_TSTRING) |
| 6130 | body = lua_tostring(L, -1); |
| 6131 | lua_pop(L, 1); /* restore the stack: remove body */ |
| 6132 | } |
| 6133 | |
| 6134 | /* Create the Reply table */ |
| 6135 | lua_newtable(L); |
| 6136 | |
| 6137 | /* Add status element */ |
| 6138 | lua_pushstring(L, "status"); |
| 6139 | lua_pushinteger(L, status); |
| 6140 | lua_settable(L, -3); |
| 6141 | |
| 6142 | /* Add reason element */ |
| 6143 | reason = http_get_reason(status); |
| 6144 | lua_pushstring(L, "reason"); |
| 6145 | lua_pushstring(L, reason); |
| 6146 | lua_settable(L, -3); |
| 6147 | |
| 6148 | /* Add body element, nil if undefined */ |
| 6149 | lua_pushstring(L, "body"); |
| 6150 | if (body) |
| 6151 | lua_pushstring(L, body); |
| 6152 | else |
| 6153 | lua_pushnil(L); |
| 6154 | lua_settable(L, -3); |
| 6155 | |
| 6156 | /* Add headers element */ |
| 6157 | lua_pushstring(L, "headers"); |
| 6158 | lua_newtable(L); |
| 6159 | |
| 6160 | /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */ |
| 6161 | if (lua_istable(L, 2)) { |
| 6162 | /* load headers from the table argument at index 2. If it is a table, copy it. */ |
| 6163 | ret = lua_getfield(L, 2, "headers"); |
| 6164 | if (ret == LUA_TTABLE) { |
| 6165 | /* stack: [ ... <headers:table>, <table> ] */ |
| 6166 | lua_pushnil(L); |
| 6167 | while (lua_next(L, -2) != 0) { |
| 6168 | /* stack: [ ... <headers:table>, <table>, k, v] */ |
| 6169 | if (!lua_isstring(L, -1) && !lua_istable(L, -1)) { |
| 6170 | /* invalid value type, skip it */ |
| 6171 | lua_pop(L, 1); |
| 6172 | continue; |
| 6173 | } |
| 6174 | |
| 6175 | |
| 6176 | /* Duplicate the key and swap it with the value. */ |
| 6177 | lua_pushvalue(L, -2); |
| 6178 | lua_insert(L, -2); |
| 6179 | /* stack: [ ... <headers:table>, <table>, k, k, v ] */ |
| 6180 | |
| 6181 | lua_newtable(L); |
| 6182 | lua_insert(L, -2); |
| 6183 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, v ] */ |
| 6184 | |
| 6185 | if (lua_isstring(L, -1)) { |
| 6186 | /* push the value in the inner table */ |
| 6187 | lua_rawseti(L, -2, 1); |
| 6188 | } |
| 6189 | else { /* table */ |
| 6190 | lua_pushnil(L); |
| 6191 | while (lua_next(L, -2) != 0) { |
| 6192 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2, v2 ] */ |
| 6193 | if (!lua_isstring(L, -1)) { |
| 6194 | /* invalid value type, skip it*/ |
| 6195 | lua_pop(L, 1); |
| 6196 | continue; |
| 6197 | } |
| 6198 | /* push the value in the inner table */ |
| 6199 | lua_rawseti(L, -4, lua_rawlen(L, -4) + 1); |
| 6200 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2 ] */ |
| 6201 | } |
| 6202 | lua_pop(L, 1); |
| 6203 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table> ] */ |
| 6204 | } |
| 6205 | |
| 6206 | /* push (k,v) on the stack in the headers table: |
| 6207 | * stack: [ ... <headers:table>, <table>, k, k, v ] |
| 6208 | */ |
| 6209 | lua_settable(L, -5); |
| 6210 | /* stack: [ ... <headers:table>, <table>, k ] */ |
| 6211 | } |
| 6212 | } |
| 6213 | lua_pop(L, 1); |
| 6214 | } |
| 6215 | /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */ |
| 6216 | lua_settable(L, -3); |
| 6217 | /* stack: [ txn, <Arg:table>, <Reply:table> ] */ |
| 6218 | |
| 6219 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 6220 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_reply_ref); |
| 6221 | lua_setmetatable(L, -2); |
| 6222 | return 1; |
| 6223 | } |
| 6224 | |
| 6225 | /* Set the reply status code, and optionally the reason. If no reason is |
| 6226 | * provided, the default one corresponding to the status code is used. |
| 6227 | */ |
| 6228 | __LJMP static int hlua_txn_reply_set_status(lua_State *L) |
| 6229 | { |
| 6230 | int status = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 6231 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
| 6232 | |
| 6233 | /* First argument (self) must be a table */ |
| 6234 | luaL_checktype(L, 1, LUA_TTABLE); |
| 6235 | |
| 6236 | if (status < 100 || status > 599) { |
| 6237 | lua_pushboolean(L, 0); |
| 6238 | return 1; |
| 6239 | } |
| 6240 | if (!reason) |
| 6241 | reason = http_get_reason(status); |
| 6242 | |
| 6243 | lua_pushinteger(L, status); |
| 6244 | lua_setfield(L, 1, "status"); |
| 6245 | |
| 6246 | lua_pushstring(L, reason); |
| 6247 | lua_setfield(L, 1, "reason"); |
| 6248 | |
| 6249 | lua_pushboolean(L, 1); |
| 6250 | return 1; |
| 6251 | } |
| 6252 | |
| 6253 | /* Add a header into the reply object. Each header name is associated to an |
| 6254 | * array of values in the "headers" table. If the header name is not found, a |
| 6255 | * new entry is created. |
| 6256 | */ |
| 6257 | __LJMP static int hlua_txn_reply_add_header(lua_State *L) |
| 6258 | { |
| 6259 | const char *name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 6260 | const char *value = MAY_LJMP(luaL_checkstring(L, 3)); |
| 6261 | int ret; |
| 6262 | |
| 6263 | /* First argument (self) must be a table */ |
| 6264 | luaL_checktype(L, 1, LUA_TTABLE); |
| 6265 | |
| 6266 | /* Push in the stack the "headers" entry. */ |
| 6267 | ret = lua_getfield(L, 1, "headers"); |
| 6268 | if (ret != LUA_TTABLE) { |
| 6269 | hlua_pusherror(L, "Reply['headers'] is expected to a an array. %s found", lua_typename(L, ret)); |
| 6270 | WILL_LJMP(lua_error(L)); |
| 6271 | } |
| 6272 | |
| 6273 | /* check if the header is already registered. If not, register it. */ |
| 6274 | ret = lua_getfield(L, -1, name); |
| 6275 | if (ret == LUA_TNIL) { |
| 6276 | /* Entry not found. */ |
| 6277 | lua_pop(L, 1); /* remove the nil. The "headers" table is the top of the stack. */ |
| 6278 | |
| 6279 | /* Insert the new header name in the array in the top of the stack. |
| 6280 | * It left the new array in the top of the stack. |
| 6281 | */ |
| 6282 | lua_newtable(L); |
| 6283 | lua_pushstring(L, name); |
| 6284 | lua_pushvalue(L, -2); |
| 6285 | lua_settable(L, -4); |
| 6286 | } |
| 6287 | else if (ret != LUA_TTABLE) { |
| 6288 | hlua_pusherror(L, "Reply['headers']['%s'] is expected to be an array. %s found", name, lua_typename(L, ret)); |
| 6289 | WILL_LJMP(lua_error(L)); |
| 6290 | } |
| 6291 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 6292 | /* Now the top of thestack is an array of values. We push |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 6293 | * the header value as new entry. |
| 6294 | */ |
| 6295 | lua_pushstring(L, value); |
| 6296 | ret = lua_rawlen(L, -2); |
| 6297 | lua_rawseti(L, -2, ret + 1); |
| 6298 | |
| 6299 | lua_pushboolean(L, 1); |
| 6300 | return 1; |
| 6301 | } |
| 6302 | |
| 6303 | /* Remove all occurrences of a given header name. */ |
| 6304 | __LJMP static int hlua_txn_reply_del_header(lua_State *L) |
| 6305 | { |
| 6306 | const char *name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 6307 | int ret; |
| 6308 | |
| 6309 | /* First argument (self) must be a table */ |
| 6310 | luaL_checktype(L, 1, LUA_TTABLE); |
| 6311 | |
| 6312 | /* Push in the stack the "headers" entry. */ |
| 6313 | ret = lua_getfield(L, 1, "headers"); |
| 6314 | if (ret != LUA_TTABLE) { |
| 6315 | hlua_pusherror(L, "Reply['headers'] is expected to be an array. %s found", lua_typename(L, ret)); |
| 6316 | WILL_LJMP(lua_error(L)); |
| 6317 | } |
| 6318 | |
| 6319 | lua_pushstring(L, name); |
| 6320 | lua_pushnil(L); |
| 6321 | lua_settable(L, -3); |
| 6322 | |
| 6323 | lua_pushboolean(L, 1); |
| 6324 | return 1; |
| 6325 | } |
| 6326 | |
| 6327 | /* Set the reply's body. Overwrite any existing entry. */ |
| 6328 | __LJMP static int hlua_txn_reply_set_body(lua_State *L) |
| 6329 | { |
| 6330 | const char *payload = MAY_LJMP(luaL_checkstring(L, 2)); |
| 6331 | |
| 6332 | /* First argument (self) must be a table */ |
| 6333 | luaL_checktype(L, 1, LUA_TTABLE); |
| 6334 | |
| 6335 | lua_pushstring(L, payload); |
| 6336 | lua_setfield(L, 1, "body"); |
| 6337 | |
| 6338 | lua_pushboolean(L, 1); |
| 6339 | return 1; |
| 6340 | } |
| 6341 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 6342 | __LJMP static int hlua_log(lua_State *L) |
| 6343 | { |
| 6344 | int level; |
| 6345 | const char *msg; |
| 6346 | |
| 6347 | MAY_LJMP(check_args(L, 2, "log")); |
| 6348 | level = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 6349 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 6350 | |
| 6351 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 6352 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 6353 | |
| 6354 | hlua_sendlog(NULL, level, msg); |
| 6355 | return 0; |
| 6356 | } |
| 6357 | |
| 6358 | __LJMP static int hlua_log_debug(lua_State *L) |
| 6359 | { |
| 6360 | const char *msg; |
| 6361 | |
| 6362 | MAY_LJMP(check_args(L, 1, "debug")); |
| 6363 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6364 | hlua_sendlog(NULL, LOG_DEBUG, msg); |
| 6365 | return 0; |
| 6366 | } |
| 6367 | |
| 6368 | __LJMP static int hlua_log_info(lua_State *L) |
| 6369 | { |
| 6370 | const char *msg; |
| 6371 | |
| 6372 | MAY_LJMP(check_args(L, 1, "info")); |
| 6373 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6374 | hlua_sendlog(NULL, LOG_INFO, msg); |
| 6375 | return 0; |
| 6376 | } |
| 6377 | |
| 6378 | __LJMP static int hlua_log_warning(lua_State *L) |
| 6379 | { |
| 6380 | const char *msg; |
| 6381 | |
| 6382 | MAY_LJMP(check_args(L, 1, "warning")); |
| 6383 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6384 | hlua_sendlog(NULL, LOG_WARNING, msg); |
| 6385 | return 0; |
| 6386 | } |
| 6387 | |
| 6388 | __LJMP static int hlua_log_alert(lua_State *L) |
| 6389 | { |
| 6390 | const char *msg; |
| 6391 | |
| 6392 | MAY_LJMP(check_args(L, 1, "alert")); |
| 6393 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6394 | hlua_sendlog(NULL, LOG_ALERT, msg); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 6395 | return 0; |
| 6396 | } |
| 6397 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 6398 | __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] | 6399 | { |
| 6400 | int wakeup_ms = lua_tointeger(L, -1); |
Willy Tarreau | 9cc2e4c | 2021-09-30 16:12:31 +0200 | [diff] [blame] | 6401 | if (!tick_is_expired(wakeup_ms, now_ms)) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 6402 | 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] | 6403 | return 0; |
| 6404 | } |
| 6405 | |
| 6406 | __LJMP static int hlua_sleep(lua_State *L) |
| 6407 | { |
| 6408 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6409 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6410 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6411 | MAY_LJMP(check_args(L, 1, "sleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6412 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 6413 | delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6414 | wakeup_ms = tick_add(now_ms, delay); |
| 6415 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6416 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 6417 | 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] | 6418 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6419 | } |
| 6420 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6421 | __LJMP static int hlua_msleep(lua_State *L) |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6422 | { |
| 6423 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6424 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6425 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6426 | MAY_LJMP(check_args(L, 1, "msleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6427 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 6428 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6429 | wakeup_ms = tick_add(now_ms, delay); |
| 6430 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6431 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 6432 | 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] | 6433 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6434 | } |
| 6435 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 6436 | /* This functionis an LUA binding. it permits to give back |
| 6437 | * the hand at the HAProxy scheduler. It is used when the |
| 6438 | * LUA processing consumes a lot of time. |
| 6439 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 6440 | __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] | 6441 | { |
| 6442 | return 0; |
| 6443 | } |
| 6444 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 6445 | __LJMP static int hlua_yield(lua_State *L) |
| 6446 | { |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 6447 | 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] | 6448 | return 0; |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 6449 | } |
| 6450 | |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6451 | /* This function change the nice of the currently executed |
| 6452 | * task. It is used set low or high priority at the current |
| 6453 | * task. |
| 6454 | */ |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 6455 | __LJMP static int hlua_set_nice(lua_State *L) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6456 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6457 | struct hlua *hlua; |
| 6458 | int nice; |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6459 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6460 | MAY_LJMP(check_args(L, 1, "set_nice")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6461 | nice = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6462 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 6463 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 6464 | hlua = hlua_gethlua(L); |
| 6465 | |
| 6466 | /* If the task is not set, I'm in a start mode. */ |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6467 | if (!hlua || !hlua->task) |
| 6468 | return 0; |
| 6469 | |
| 6470 | if (nice < -1024) |
| 6471 | nice = -1024; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6472 | else if (nice > 1024) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6473 | nice = 1024; |
| 6474 | |
| 6475 | hlua->task->nice = nice; |
| 6476 | return 0; |
| 6477 | } |
| 6478 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6479 | /* 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] | 6480 | * HAProxy task subsystem when the task is awaked. The LUA runtime can |
| 6481 | * return an E_AGAIN signal, the emmiter of this signal must set a |
| 6482 | * signal to wake the task. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6483 | * |
| 6484 | * Task wrapper are longjmp safe because the only one Lua code |
| 6485 | * executed is the safe hlua_ctx_resume(); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6486 | */ |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 6487 | 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] | 6488 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 6489 | struct hlua *hlua = context; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6490 | enum hlua_exec status; |
| 6491 | |
Christopher Faulet | 5bc9972 | 2018-04-25 10:34:45 +0200 | [diff] [blame] | 6492 | if (task->thread_mask == MAX_THREADS_MASK) |
| 6493 | task_set_affinity(task, tid_bit); |
| 6494 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6495 | /* If it is the first call to the task, we must initialize the |
| 6496 | * execution timeouts. |
| 6497 | */ |
| 6498 | if (!HLUA_IS_RUNNING(hlua)) |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 6499 | hlua->max_time = hlua_timeout_task; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6500 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6501 | /* Execute the Lua code. */ |
| 6502 | status = hlua_ctx_resume(hlua, 1); |
| 6503 | |
| 6504 | switch (status) { |
| 6505 | /* finished or yield */ |
| 6506 | case HLUA_E_OK: |
| 6507 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 6508 | task_destroy(task); |
Tim Duesterhus | cd235c6 | 2018-04-24 13:56:01 +0200 | [diff] [blame] | 6509 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6510 | break; |
| 6511 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6512 | case HLUA_E_AGAIN: /* co process or timeout wake me later. */ |
Thierry FOURNIER | cb14688 | 2017-12-10 17:10:57 +0100 | [diff] [blame] | 6513 | notification_gc(&hlua->com); |
PiBa-NL | fe971b3 | 2018-05-02 22:27:14 +0200 | [diff] [blame] | 6514 | task->expire = hlua->wake_time; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6515 | break; |
| 6516 | |
| 6517 | /* finished with error. */ |
| 6518 | case HLUA_E_ERRMSG: |
Aurelien DARRAGON | 6ab7ec9 | 2024-03-01 21:17:51 +0100 | [diff] [blame] | 6519 | hlua_lock(hlua); |
Aurelien DARRAGON | a374161 | 2024-03-01 15:55:17 +0100 | [diff] [blame] | 6520 | 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] | 6521 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 6522 | task_destroy(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 6523 | task = NULL; |
Aurelien DARRAGON | 6ab7ec9 | 2024-03-01 21:17:51 +0100 | [diff] [blame] | 6524 | hlua_unlock(hlua); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6525 | break; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6526 | case HLUA_E_ERR: |
| 6527 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6528 | SEND_ERR(NULL, "Lua task: unknown error.\n"); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6529 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 6530 | task_destroy(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 6531 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6532 | break; |
| 6533 | } |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 6534 | return task; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6535 | } |
| 6536 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6537 | /* This function is an LUA binding that register LUA function to be |
| 6538 | * executed after the HAProxy configuration parsing and before the |
| 6539 | * HAProxy scheduler starts. This function expect only one LUA |
| 6540 | * argument that is a function. This function returns nothing, but |
| 6541 | * throws if an error is encountered. |
| 6542 | */ |
| 6543 | __LJMP static int hlua_register_init(lua_State *L) |
| 6544 | { |
| 6545 | struct hlua_init_function *init; |
| 6546 | int ref; |
| 6547 | |
| 6548 | MAY_LJMP(check_args(L, 1, "register_init")); |
| 6549 | |
Aurelien DARRAGON | 8985ab8 | 2023-02-24 09:43:54 +0100 | [diff] [blame] | 6550 | if (hlua_gethlua(L)) { |
| 6551 | /* runtime processing */ |
| 6552 | WILL_LJMP(luaL_error(L, "register_init: not available outside of body context")); |
| 6553 | } |
| 6554 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6555 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 6556 | |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6557 | init = calloc(1, sizeof(*init)); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6558 | if (!init) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6559 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6560 | |
| 6561 | init->function_ref = ref; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 6562 | LIST_APPEND(&hlua_init_functions[hlua_state_id], &init->l); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6563 | return 0; |
| 6564 | } |
| 6565 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6566 | /* This functio is an LUA binding. It permits to register a task |
| 6567 | * executed in parallel of the main HAroxy activity. The task is |
| 6568 | * created and it is set in the HAProxy scheduler. It can be called |
| 6569 | * from the "init" section, "post init" or during the runtime. |
| 6570 | * |
| 6571 | * Lua prototype: |
| 6572 | * |
| 6573 | * <none> core.register_task(<function>) |
| 6574 | */ |
| 6575 | static int hlua_register_task(lua_State *L) |
| 6576 | { |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 6577 | struct hlua *hlua = NULL; |
| 6578 | struct task *task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6579 | int ref; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 6580 | int state_id; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6581 | |
| 6582 | MAY_LJMP(check_args(L, 1, "register_task")); |
| 6583 | |
| 6584 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 6585 | |
Thierry Fournier | 75fc029 | 2020-11-28 13:18:56 +0100 | [diff] [blame] | 6586 | /* Get the reference state. If the reference is NULL, L is the master |
| 6587 | * state, otherwise hlua->T is. |
| 6588 | */ |
| 6589 | hlua = hlua_gethlua(L); |
| 6590 | if (hlua) |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 6591 | /* we are in runtime processing */ |
| 6592 | state_id = hlua->state_id; |
Thierry Fournier | 75fc029 | 2020-11-28 13:18:56 +0100 | [diff] [blame] | 6593 | else |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 6594 | /* we are in initialization mode */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 6595 | state_id = hlua_state_id; |
Thierry Fournier | 75fc029 | 2020-11-28 13:18:56 +0100 | [diff] [blame] | 6596 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6597 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6598 | if (!hlua) |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 6599 | goto alloc_error; |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 6600 | HLUA_INIT(hlua); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6601 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 6602 | /* We are in the common lua state, execute the task anywhere, |
| 6603 | * otherwise, inherit the current thread identifier |
| 6604 | */ |
| 6605 | if (state_id == 0) |
| 6606 | task = task_new(MAX_THREADS_MASK); |
| 6607 | else |
| 6608 | task = task_new(tid_bit); |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 6609 | if (!task) |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 6610 | goto alloc_error; |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 6611 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6612 | task->context = hlua; |
| 6613 | task->process = hlua_process_task; |
| 6614 | |
Aurelien DARRAGON | 5483bde | 2023-03-21 14:02:16 +0100 | [diff] [blame] | 6615 | if (!hlua_ctx_init(hlua, state_id, task)) |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 6616 | goto alloc_error; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6617 | |
| 6618 | /* Restore the function in the stack. */ |
| 6619 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref); |
Aurelien DARRAGON | b6aade3 | 2023-03-13 14:09:21 +0100 | [diff] [blame] | 6620 | /* function ref not needed anymore since it was pushed to the substack */ |
| 6621 | hlua_unref(L, ref); |
| 6622 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6623 | hlua->nargs = 0; |
| 6624 | |
| 6625 | /* Schedule task. */ |
Willy Tarreau | 790810f | 2021-09-30 16:17:37 +0200 | [diff] [blame] | 6626 | task_wakeup(task, TASK_WOKEN_INIT); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6627 | |
| 6628 | return 0; |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 6629 | |
| 6630 | alloc_error: |
| 6631 | task_destroy(task); |
| 6632 | hlua_ctx_destroy(hlua); |
| 6633 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 6634 | return 0; /* Never reached */ |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6635 | } |
| 6636 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6637 | /* Wrapper called by HAProxy to execute an LUA converter. This wrapper |
| 6638 | * doesn't allow "yield" functions because the HAProxy engine cannot |
| 6639 | * resume converters. |
| 6640 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6641 | 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] | 6642 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 6643 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6644 | struct stream *stream = smp->strm; |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 6645 | struct hlua *hlua = NULL; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6646 | const char *error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6647 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6648 | if (!stream) |
| 6649 | return 0; |
| 6650 | |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 6651 | 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] | 6652 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 6653 | return 0; |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6654 | } |
| 6655 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6656 | /* If it is the first run, initialize the data for the call. */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 6657 | if (!HLUA_IS_RUNNING(hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6658 | |
| 6659 | /* The following Lua calls can fail. */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 6660 | if (!SET_SAFE_LJMP(hlua)) { |
| 6661 | hlua_lock(hlua); |
| 6662 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 6663 | error = hlua_tostring_safe(hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6664 | else |
| 6665 | error = "critical error"; |
| 6666 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error); |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 6667 | hlua_unlock(hlua); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6668 | return 0; |
| 6669 | } |
| 6670 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6671 | /* Check stack available size. */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 6672 | if (!lua_checkstack(hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6673 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 6674 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6675 | return 0; |
| 6676 | } |
| 6677 | |
| 6678 | /* Restore the function in the stack. */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 6679 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, fcn->function_ref[hlua->state_id]); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6680 | |
| 6681 | /* convert input sample and pust-it in the stack. */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 6682 | if (!lua_checkstack(hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6683 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 6684 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6685 | return 0; |
| 6686 | } |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 6687 | hlua_smp2lua(hlua->T, smp); |
| 6688 | hlua->nargs = 1; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6689 | |
| 6690 | /* push keywords in the stack. */ |
| 6691 | if (arg_p) { |
| 6692 | for (; arg_p->type != ARGT_STOP; arg_p++) { |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 6693 | if (!lua_checkstack(hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6694 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 6695 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6696 | return 0; |
| 6697 | } |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 6698 | hlua_arg2lua(hlua->T, arg_p); |
| 6699 | hlua->nargs++; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6700 | } |
| 6701 | } |
| 6702 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6703 | /* We must initialize the execution timeouts. */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 6704 | hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6705 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6706 | /* At this point the execution is safe. */ |
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 | } |
| 6709 | |
| 6710 | /* Execute the function. */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 6711 | switch (hlua_ctx_resume(hlua, 0)) { |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6712 | /* finished. */ |
| 6713 | case HLUA_E_OK: |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 6714 | hlua_lock(hlua); |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 6715 | /* If the stack is empty, the function fails. */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 6716 | if (lua_gettop(hlua->T) <= 0) { |
| 6717 | hlua_unlock(hlua); |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 6718 | return 0; |
Aurelien DARRAGON | 6ab7ec9 | 2024-03-01 21:17:51 +0100 | [diff] [blame] | 6719 | } |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 6720 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6721 | /* Convert the returned value in sample. */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 6722 | hlua_lua2smp(hlua->T, -1, smp); |
Aurelien DARRAGON | d39fb74 | 2023-05-17 16:06:11 +0200 | [diff] [blame] | 6723 | /* dup the smp before popping the related lua value and |
| 6724 | * returning it to haproxy |
| 6725 | */ |
| 6726 | smp_dup(smp); |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 6727 | lua_pop(hlua->T, 1); |
| 6728 | hlua_unlock(hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6729 | return 1; |
| 6730 | |
| 6731 | /* yield. */ |
| 6732 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6733 | 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] | 6734 | return 0; |
| 6735 | |
| 6736 | /* finished with error. */ |
| 6737 | case HLUA_E_ERRMSG: |
| 6738 | /* Display log. */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 6739 | hlua_lock(hlua); |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6740 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 6741 | fcn->name, hlua_tostring_safe(hlua->T, -1)); |
| 6742 | lua_pop(hlua->T, 1); |
| 6743 | hlua_unlock(hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6744 | return 0; |
| 6745 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6746 | case HLUA_E_ETMOUT: |
| 6747 | SEND_ERR(stream->be, "Lua converter '%s': execution timeout.\n", fcn->name); |
| 6748 | return 0; |
| 6749 | |
| 6750 | case HLUA_E_NOMEM: |
| 6751 | SEND_ERR(stream->be, "Lua converter '%s': out of memory error.\n", fcn->name); |
| 6752 | return 0; |
| 6753 | |
| 6754 | case HLUA_E_YIELD: |
| 6755 | SEND_ERR(stream->be, "Lua converter '%s': yield functions like core.tcp() or core.sleep() are not allowed.\n", fcn->name); |
| 6756 | return 0; |
| 6757 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6758 | case HLUA_E_ERR: |
| 6759 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6760 | 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] | 6761 | /* fall through */ |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6762 | |
| 6763 | default: |
| 6764 | return 0; |
| 6765 | } |
| 6766 | } |
| 6767 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6768 | /* Wrapper called by HAProxy to execute a sample-fetch. this wrapper |
| 6769 | * doesn't allow "yield" functions because the HAProxy engine cannot |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6770 | * resume sample-fetches. This function will be called by the sample |
| 6771 | * fetch engine to call lua-based fetch operations. |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6772 | */ |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6773 | static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp, |
| 6774 | const char *kw, void *private) |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6775 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 6776 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6777 | struct stream *stream = smp->strm; |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 6778 | struct hlua *hlua = NULL; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6779 | const char *error; |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6780 | unsigned int hflags = HLUA_TXN_NOTERM; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6781 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6782 | if (!stream) |
| 6783 | return 0; |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 6784 | |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 6785 | 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] | 6786 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 6787 | return 0; |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6788 | } |
| 6789 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6790 | /* If it is the first run, initialize the data for the call. */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 6791 | if (!HLUA_IS_RUNNING(hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6792 | |
| 6793 | /* The following Lua calls can fail. */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 6794 | if (!SET_SAFE_LJMP(hlua)) { |
| 6795 | hlua_lock(hlua); |
| 6796 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 6797 | error = hlua_tostring_safe(hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6798 | else |
| 6799 | error = "critical error"; |
| 6800 | 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] | 6801 | hlua_unlock(hlua); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6802 | return 0; |
| 6803 | } |
| 6804 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6805 | /* Check stack available size. */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 6806 | if (!lua_checkstack(hlua->T, 2)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6807 | 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] | 6808 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6809 | return 0; |
| 6810 | } |
| 6811 | |
| 6812 | /* Restore the function in the stack. */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 6813 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, fcn->function_ref[hlua->state_id]); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6814 | |
| 6815 | /* push arguments in the stack. */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 6816 | 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] | 6817 | 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] | 6818 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6819 | return 0; |
| 6820 | } |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 6821 | hlua->nargs = 1; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6822 | |
| 6823 | /* push keywords in the stack. */ |
| 6824 | for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) { |
| 6825 | /* Check stack available size. */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 6826 | if (!lua_checkstack(hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6827 | 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] | 6828 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6829 | return 0; |
| 6830 | } |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 6831 | hlua_arg2lua(hlua->T, arg_p); |
| 6832 | hlua->nargs++; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6833 | } |
| 6834 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6835 | /* We must initialize the execution timeouts. */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 6836 | hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6837 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6838 | /* At this point the execution is safe. */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 6839 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6840 | } |
| 6841 | |
| 6842 | /* Execute the function. */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 6843 | switch (hlua_ctx_resume(hlua, 0)) { |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6844 | /* finished. */ |
| 6845 | case HLUA_E_OK: |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 6846 | hlua_lock(hlua); |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 6847 | /* If the stack is empty, the function fails. */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 6848 | if (lua_gettop(hlua->T) <= 0) { |
| 6849 | hlua_unlock(hlua); |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 6850 | return 0; |
Aurelien DARRAGON | 6ab7ec9 | 2024-03-01 21:17:51 +0100 | [diff] [blame] | 6851 | } |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 6852 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6853 | /* Convert the returned value in sample. */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 6854 | hlua_lua2smp(hlua->T, -1, smp); |
Aurelien DARRAGON | d39fb74 | 2023-05-17 16:06:11 +0200 | [diff] [blame] | 6855 | /* dup the smp before popping the related lua value and |
| 6856 | * returning it to haproxy |
| 6857 | */ |
| 6858 | smp_dup(smp); |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 6859 | lua_pop(hlua->T, 1); |
| 6860 | hlua_unlock(hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6861 | |
| 6862 | /* Set the end of execution flag. */ |
| 6863 | smp->flags &= ~SMP_F_MAY_CHANGE; |
| 6864 | return 1; |
| 6865 | |
| 6866 | /* yield. */ |
| 6867 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6868 | 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] | 6869 | return 0; |
| 6870 | |
| 6871 | /* finished with error. */ |
| 6872 | case HLUA_E_ERRMSG: |
| 6873 | /* Display log. */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 6874 | hlua_lock(hlua); |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6875 | SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 6876 | fcn->name, hlua_tostring_safe(hlua->T, -1)); |
| 6877 | lua_pop(hlua->T, 1); |
| 6878 | hlua_unlock(hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6879 | return 0; |
| 6880 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6881 | case HLUA_E_ETMOUT: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6882 | SEND_ERR(smp->px, "Lua sample-fetch '%s': execution timeout.\n", fcn->name); |
| 6883 | return 0; |
| 6884 | |
| 6885 | case HLUA_E_NOMEM: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6886 | SEND_ERR(smp->px, "Lua sample-fetch '%s': out of memory error.\n", fcn->name); |
| 6887 | return 0; |
| 6888 | |
| 6889 | case HLUA_E_YIELD: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6890 | SEND_ERR(smp->px, "Lua sample-fetch '%s': yield not allowed.\n", fcn->name); |
| 6891 | return 0; |
| 6892 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6893 | case HLUA_E_ERR: |
| 6894 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6895 | 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] | 6896 | /* fall through */ |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6897 | |
| 6898 | default: |
| 6899 | return 0; |
| 6900 | } |
| 6901 | } |
| 6902 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6903 | /* This function is an LUA binding used for registering |
| 6904 | * "sample-conv" functions. It expects a converter name used |
| 6905 | * in the haproxy configuration file, and an LUA function. |
| 6906 | */ |
| 6907 | __LJMP static int hlua_register_converters(lua_State *L) |
| 6908 | { |
| 6909 | struct sample_conv_kw_list *sck; |
| 6910 | const char *name; |
| 6911 | int ref; |
| 6912 | int len; |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 6913 | struct hlua_function *fcn = NULL; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 6914 | struct sample_conv *sc; |
| 6915 | struct buffer *trash; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6916 | |
| 6917 | MAY_LJMP(check_args(L, 2, "register_converters")); |
| 6918 | |
Aurelien DARRAGON | 8985ab8 | 2023-02-24 09:43:54 +0100 | [diff] [blame] | 6919 | if (hlua_gethlua(L)) { |
| 6920 | /* runtime processing */ |
| 6921 | WILL_LJMP(luaL_error(L, "register_converters: not available outside of body context")); |
| 6922 | } |
| 6923 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6924 | /* First argument : converter name. */ |
| 6925 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6926 | |
| 6927 | /* Second argument : lua function. */ |
| 6928 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 6929 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 6930 | /* Check if the converter is already registered */ |
| 6931 | trash = get_trash_chunk(); |
| 6932 | chunk_printf(trash, "lua.%s", name); |
| 6933 | sc = find_sample_conv(trash->area, trash->data); |
| 6934 | if (sc != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 6935 | fcn = sc->private; |
| 6936 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 6937 | ha_warning("Trying to register converter 'lua.%s' more than once. " |
| 6938 | "This will become a hard error in version 2.5.\n", name); |
| 6939 | } |
| 6940 | fcn->function_ref[hlua_state_id] = ref; |
| 6941 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 6942 | } |
| 6943 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6944 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6945 | sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6946 | if (!sck) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 6947 | goto alloc_error; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 6948 | fcn = new_hlua_function(); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6949 | if (!fcn) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 6950 | goto alloc_error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6951 | |
| 6952 | /* Fill fcn. */ |
| 6953 | fcn->name = strdup(name); |
| 6954 | if (!fcn->name) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 6955 | goto alloc_error; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 6956 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6957 | |
| 6958 | /* List head */ |
| 6959 | sck->list.n = sck->list.p = NULL; |
| 6960 | |
| 6961 | /* converter keyword. */ |
| 6962 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6963 | sck->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6964 | if (!sck->kw[0].kw) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 6965 | goto alloc_error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6966 | |
| 6967 | snprintf((char *)sck->kw[0].kw, len, "lua.%s", name); |
| 6968 | sck->kw[0].process = hlua_sample_conv_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 6969 | 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] | 6970 | sck->kw[0].val_args = NULL; |
| 6971 | sck->kw[0].in_type = SMP_T_STR; |
| 6972 | sck->kw[0].out_type = SMP_T_STR; |
| 6973 | sck->kw[0].private = fcn; |
| 6974 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6975 | /* Register this new converter */ |
| 6976 | sample_register_convs(sck); |
| 6977 | |
| 6978 | return 0; |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 6979 | |
| 6980 | alloc_error: |
| 6981 | release_hlua_function(fcn); |
| 6982 | ha_free(&sck); |
| 6983 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 6984 | return 0; /* Never reached */ |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6985 | } |
| 6986 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6987 | /* This function is an LUA binding used for registering |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6988 | * "sample-fetch" functions. It expects a converter name used |
| 6989 | * in the haproxy configuration file, and an LUA function. |
| 6990 | */ |
| 6991 | __LJMP static int hlua_register_fetches(lua_State *L) |
| 6992 | { |
| 6993 | const char *name; |
| 6994 | int ref; |
| 6995 | int len; |
| 6996 | struct sample_fetch_kw_list *sfk; |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 6997 | struct hlua_function *fcn = NULL; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 6998 | struct sample_fetch *sf; |
| 6999 | struct buffer *trash; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 7000 | |
| 7001 | MAY_LJMP(check_args(L, 2, "register_fetches")); |
| 7002 | |
Aurelien DARRAGON | 8985ab8 | 2023-02-24 09:43:54 +0100 | [diff] [blame] | 7003 | if (hlua_gethlua(L)) { |
| 7004 | /* runtime processing */ |
| 7005 | WILL_LJMP(luaL_error(L, "register_fetches: not available outside of body context")); |
| 7006 | } |
| 7007 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 7008 | /* First argument : sample-fetch name. */ |
| 7009 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 7010 | |
| 7011 | /* Second argument : lua function. */ |
| 7012 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 7013 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 7014 | /* Check if the sample-fetch is already registered */ |
| 7015 | trash = get_trash_chunk(); |
| 7016 | chunk_printf(trash, "lua.%s", name); |
| 7017 | sf = find_sample_fetch(trash->area, trash->data); |
| 7018 | if (sf != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 7019 | fcn = sf->private; |
| 7020 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 7021 | ha_warning("Trying to register sample-fetch 'lua.%s' more than once. " |
| 7022 | "This will become a hard error in version 2.5.\n", name); |
| 7023 | } |
| 7024 | fcn->function_ref[hlua_state_id] = ref; |
| 7025 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 7026 | } |
| 7027 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 7028 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7029 | sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 7030 | if (!sfk) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 7031 | goto alloc_error; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 7032 | fcn = new_hlua_function(); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 7033 | if (!fcn) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 7034 | goto alloc_error; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 7035 | |
| 7036 | /* Fill fcn. */ |
| 7037 | fcn->name = strdup(name); |
| 7038 | if (!fcn->name) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 7039 | goto alloc_error; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 7040 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 7041 | |
| 7042 | /* List head */ |
| 7043 | sfk->list.n = sfk->list.p = NULL; |
| 7044 | |
| 7045 | /* sample-fetch keyword. */ |
| 7046 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7047 | sfk->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 7048 | if (!sfk->kw[0].kw) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 7049 | goto alloc_error; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 7050 | |
| 7051 | snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name); |
| 7052 | sfk->kw[0].process = hlua_sample_fetch_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 7053 | 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] | 7054 | sfk->kw[0].val_args = NULL; |
| 7055 | sfk->kw[0].out_type = SMP_T_STR; |
| 7056 | sfk->kw[0].use = SMP_USE_HTTP_ANY; |
| 7057 | sfk->kw[0].val = 0; |
| 7058 | sfk->kw[0].private = fcn; |
| 7059 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 7060 | /* Register this new fetch. */ |
| 7061 | sample_register_fetches(sfk); |
| 7062 | |
| 7063 | return 0; |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 7064 | |
| 7065 | alloc_error: |
| 7066 | release_hlua_function(fcn); |
| 7067 | ha_free(&sfk); |
| 7068 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 7069 | return 0; /* Never reached */ |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 7070 | } |
| 7071 | |
Christopher Faulet | 501465d | 2020-02-26 14:54:16 +0100 | [diff] [blame] | 7072 | /* This function is a lua binding to set the wake_time. |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 7073 | */ |
Christopher Faulet | 501465d | 2020-02-26 14:54:16 +0100 | [diff] [blame] | 7074 | __LJMP static int hlua_set_wake_time(lua_State *L) |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 7075 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 7076 | struct hlua *hlua; |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 7077 | unsigned int delay; |
| 7078 | unsigned int wakeup_ms; |
| 7079 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 7080 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 7081 | hlua = hlua_gethlua(L); |
| 7082 | if (!hlua) { |
| 7083 | return 0; |
| 7084 | } |
| 7085 | |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 7086 | MAY_LJMP(check_args(L, 1, "wake_time")); |
| 7087 | |
| 7088 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 7089 | wakeup_ms = tick_add(now_ms, delay); |
| 7090 | hlua->wake_time = wakeup_ms; |
| 7091 | return 0; |
| 7092 | } |
| 7093 | |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7094 | /* This function is a wrapper to execute each LUA function declared as an action |
| 7095 | * wrapper during the initialisation period. This function may return any |
| 7096 | * ACT_RET_* value. On error ACT_RET_CONT is returned and the action is |
| 7097 | * ignored. If the lua action yields, ACT_RET_YIELD is returned. On success, the |
| 7098 | * return value is the first element on the stack. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7099 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7100 | 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] | 7101 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7102 | { |
| 7103 | char **arg; |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 7104 | unsigned int hflags = 0; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7105 | int dir, act_ret = ACT_RET_CONT; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7106 | const char *error; |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 7107 | struct hlua *hlua = NULL; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7108 | |
| 7109 | switch (rule->from) { |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 7110 | case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break; |
| 7111 | case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break; |
| 7112 | case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break; |
| 7113 | case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7114 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 7115 | SEND_ERR(px, "Lua: internal error while execute action.\n"); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7116 | goto end; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7117 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7118 | |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 7119 | 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] | 7120 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
| 7121 | rule->arg.hlua_rule->fcn->name); |
| 7122 | goto end; |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 7123 | } |
| 7124 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7125 | /* If it is the first run, initialize the data for the call. */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 7126 | if (!HLUA_IS_RUNNING(hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7127 | |
| 7128 | /* The following Lua calls can fail. */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 7129 | if (!SET_SAFE_LJMP(hlua)) { |
| 7130 | hlua_lock(hlua); |
| 7131 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 7132 | error = hlua_tostring_safe(hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7133 | else |
| 7134 | error = "critical error"; |
| 7135 | SEND_ERR(px, "Lua function '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7136 | rule->arg.hlua_rule->fcn->name, error); |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 7137 | hlua_unlock(hlua); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7138 | goto end; |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7139 | } |
| 7140 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7141 | /* Check stack available size. */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 7142 | if (!lua_checkstack(hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 7143 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7144 | rule->arg.hlua_rule->fcn->name); |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 7145 | RESET_SAFE_LJMP(hlua); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7146 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7147 | } |
| 7148 | |
| 7149 | /* Restore the function in the stack. */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 7150 | 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] | 7151 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7152 | /* Create and and push object stream in the stack. */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 7153 | if (!hlua_txn_new(hlua->T, s, px, dir, hflags)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 7154 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7155 | rule->arg.hlua_rule->fcn->name); |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 7156 | RESET_SAFE_LJMP(hlua); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7157 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7158 | } |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 7159 | hlua->nargs = 1; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7160 | |
| 7161 | /* push keywords in the stack. */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7162 | for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) { |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 7163 | if (!lua_checkstack(hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 7164 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7165 | rule->arg.hlua_rule->fcn->name); |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 7166 | RESET_SAFE_LJMP(hlua); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7167 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7168 | } |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 7169 | lua_pushstring(hlua->T, *arg); |
| 7170 | hlua->nargs++; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7171 | } |
| 7172 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7173 | /* Now the execution is safe. */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 7174 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7175 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 7176 | /* We must initialize the execution timeouts. */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 7177 | hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7178 | } |
| 7179 | |
| 7180 | /* Execute the function. */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 7181 | switch (hlua_ctx_resume(hlua, !(flags & ACT_OPT_FINAL))) { |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7182 | /* finished. */ |
| 7183 | case HLUA_E_OK: |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7184 | /* Catch the return value */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 7185 | hlua_lock(hlua); |
| 7186 | if (lua_gettop(hlua->T) > 0) |
| 7187 | act_ret = lua_tointeger(hlua->T, -1); |
| 7188 | hlua_unlock(hlua); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7189 | |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 7190 | /* Set timeout in the required channel. */ |
Christopher Faulet | 498c483 | 2020-07-28 11:59:58 +0200 | [diff] [blame] | 7191 | if (act_ret == ACT_RET_YIELD) { |
| 7192 | if (flags & ACT_OPT_FINAL) |
| 7193 | goto err_yield; |
| 7194 | |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 7195 | if (dir == SMP_OPT_DIR_REQ) |
| 7196 | 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] | 7197 | hlua->wake_time); |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 7198 | else |
| 7199 | 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] | 7200 | hlua->wake_time); |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 7201 | } |
| 7202 | goto end; |
| 7203 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7204 | /* yield. */ |
| 7205 | case HLUA_E_AGAIN: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 7206 | /* Set timeout in the required channel. */ |
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 | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 7213 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7214 | /* Some actions can be wake up when a "write" event |
| 7215 | * is detected on a response channel. This is useful |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7216 | * only for actions targeted on the requests. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7217 | */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 7218 | if (HLUA_IS_WAKERESWR(hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 7219 | s->res.flags |= CF_WAKE_WRITE; |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 7220 | if (HLUA_IS_WAKEREQWR(hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 7221 | s->req.flags |= CF_WAKE_WRITE; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7222 | act_ret = ACT_RET_YIELD; |
| 7223 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7224 | |
| 7225 | /* finished with error. */ |
| 7226 | case HLUA_E_ERRMSG: |
| 7227 | /* Display log. */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 7228 | hlua_lock(hlua); |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 7229 | SEND_ERR(px, "Lua function '%s': %s.\n", |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 7230 | rule->arg.hlua_rule->fcn->name, hlua_tostring_safe(hlua->T, -1)); |
| 7231 | lua_pop(hlua->T, 1); |
| 7232 | hlua_unlock(hlua); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7233 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7234 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7235 | case HLUA_E_ETMOUT: |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7236 | 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] | 7237 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7238 | |
| 7239 | case HLUA_E_NOMEM: |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7240 | 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] | 7241 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7242 | |
| 7243 | case HLUA_E_YIELD: |
Christopher Faulet | 498c483 | 2020-07-28 11:59:58 +0200 | [diff] [blame] | 7244 | err_yield: |
| 7245 | act_ret = ACT_RET_CONT; |
Aurelien DARRAGON | 602c4af | 2023-08-31 21:45:21 +0200 | [diff] [blame] | 7246 | SEND_ERR(px, "Lua function '%s': yield not allowed.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7247 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7248 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7249 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7250 | case HLUA_E_ERR: |
| 7251 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 7252 | SEND_ERR(px, "Lua function '%s' return an unknown error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7253 | rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7254 | |
| 7255 | default: |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7256 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7257 | } |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7258 | |
| 7259 | end: |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 7260 | if (act_ret != ACT_RET_YIELD && hlua) |
| 7261 | hlua->wake_time = TICK_ETERNITY; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7262 | return act_ret; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7263 | } |
| 7264 | |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 7265 | 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] | 7266 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 7267 | struct appctx *ctx = context; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7268 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7269 | appctx_wakeup(ctx); |
Willy Tarreau | d958741 | 2017-08-23 16:07:33 +0200 | [diff] [blame] | 7270 | t->expire = TICK_ETERNITY; |
Willy Tarreau | d1aa41f | 2017-07-21 16:41:56 +0200 | [diff] [blame] | 7271 | return t; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7272 | } |
| 7273 | |
| 7274 | static int hlua_applet_tcp_init(struct appctx *ctx, struct proxy *px, struct stream *strm) |
| 7275 | { |
| 7276 | struct stream_interface *si = ctx->owner; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7277 | struct hlua *hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7278 | struct task *task; |
| 7279 | char **arg; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7280 | const char *error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7281 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7282 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7283 | if (!hlua) { |
| 7284 | SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7285 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7286 | return 0; |
| 7287 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7288 | HLUA_INIT(hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7289 | ctx->ctx.hlua_apptcp.hlua = hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7290 | ctx->ctx.hlua_apptcp.flags = 0; |
| 7291 | |
| 7292 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 7293 | task = task_new(tid_bit); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7294 | if (!task) { |
| 7295 | SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7296 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7297 | return 0; |
| 7298 | } |
| 7299 | task->nice = 0; |
| 7300 | task->context = ctx; |
| 7301 | task->process = hlua_applet_wakeup; |
| 7302 | ctx->ctx.hlua_apptcp.task = task; |
| 7303 | |
| 7304 | /* In the execution wrappers linked with a stream, the |
| 7305 | * Lua context can be not initialized. This behavior |
| 7306 | * permits to save performances because a systematic |
| 7307 | * Lua initialization cause 5% performances loss. |
| 7308 | */ |
Aurelien DARRAGON | 5483bde | 2023-03-21 14:02:16 +0100 | [diff] [blame] | 7309 | 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] | 7310 | 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] | 7311 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7312 | return 0; |
| 7313 | } |
| 7314 | |
| 7315 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 7316 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7317 | |
| 7318 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7319 | if (!SET_SAFE_LJMP(hlua)) { |
Aurelien DARRAGON | d4df562 | 2024-03-04 11:17:58 +0100 | [diff] [blame] | 7320 | hlua_lock(hlua); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7321 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
Aurelien DARRAGON | a374161 | 2024-03-01 15:55:17 +0100 | [diff] [blame] | 7322 | error = hlua_tostring_safe(hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7323 | else |
| 7324 | error = "critical error"; |
| 7325 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7326 | ctx->rule->arg.hlua_rule->fcn->name, error); |
Aurelien DARRAGON | d4df562 | 2024-03-04 11:17:58 +0100 | [diff] [blame] | 7327 | hlua_unlock(hlua); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7328 | return 0; |
| 7329 | } |
| 7330 | |
| 7331 | /* Check stack available size. */ |
| 7332 | if (!lua_checkstack(hlua->T, 1)) { |
| 7333 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7334 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7335 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7336 | return 0; |
| 7337 | } |
| 7338 | |
| 7339 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 7340 | 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] | 7341 | |
| 7342 | /* Create and and push object stream in the stack. */ |
| 7343 | if (!hlua_applet_tcp_new(hlua->T, ctx)) { |
| 7344 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7345 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7346 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7347 | return 0; |
| 7348 | } |
| 7349 | hlua->nargs = 1; |
| 7350 | |
| 7351 | /* push keywords in the stack. */ |
| 7352 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 7353 | if (!lua_checkstack(hlua->T, 1)) { |
| 7354 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7355 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7356 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7357 | return 0; |
| 7358 | } |
| 7359 | lua_pushstring(hlua->T, *arg); |
| 7360 | hlua->nargs++; |
| 7361 | } |
| 7362 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7363 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7364 | |
| 7365 | /* Wakeup the applet ASAP. */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 7366 | si_cant_get(si); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 7367 | si_rx_endp_more(si); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7368 | |
| 7369 | return 1; |
| 7370 | } |
| 7371 | |
Willy Tarreau | 60409db | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 7372 | void hlua_applet_tcp_fct(struct appctx *ctx) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7373 | { |
| 7374 | struct stream_interface *si = ctx->owner; |
| 7375 | struct stream *strm = si_strm(si); |
| 7376 | struct channel *res = si_ic(si); |
| 7377 | struct act_rule *rule = ctx->rule; |
| 7378 | struct proxy *px = strm->be; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7379 | struct hlua *hlua = ctx->ctx.hlua_apptcp.hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7380 | |
| 7381 | /* The applet execution is already done. */ |
Olivier Houchard | 594c8c5 | 2018-08-28 14:41:31 +0200 | [diff] [blame] | 7382 | if (ctx->ctx.hlua_apptcp.flags & APPLET_DONE) { |
| 7383 | /* eat the whole request */ |
| 7384 | co_skip(si_oc(si), co_data(si_oc(si))); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7385 | return; |
Olivier Houchard | 594c8c5 | 2018-08-28 14:41:31 +0200 | [diff] [blame] | 7386 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7387 | |
| 7388 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 7389 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 7390 | return; |
| 7391 | |
| 7392 | /* Execute the function. */ |
| 7393 | switch (hlua_ctx_resume(hlua, 1)) { |
| 7394 | /* finished. */ |
| 7395 | case HLUA_E_OK: |
| 7396 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 7397 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7398 | /* eat the whole request */ |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 7399 | co_skip(si_oc(si), co_data(si_oc(si))); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7400 | res->flags |= CF_READ_NULL; |
| 7401 | si_shutr(si); |
| 7402 | return; |
| 7403 | |
| 7404 | /* yield. */ |
| 7405 | case HLUA_E_AGAIN: |
Thierry Fournier | 0164f20 | 2016-02-20 17:47:43 +0100 | [diff] [blame] | 7406 | if (hlua->wake_time != TICK_ETERNITY) |
| 7407 | task_schedule(ctx->ctx.hlua_apptcp.task, hlua->wake_time); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7408 | return; |
| 7409 | |
| 7410 | /* finished with error. */ |
| 7411 | case HLUA_E_ERRMSG: |
| 7412 | /* Display log. */ |
Aurelien DARRAGON | 6ab7ec9 | 2024-03-01 21:17:51 +0100 | [diff] [blame] | 7413 | hlua_lock(hlua); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7414 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
Aurelien DARRAGON | a374161 | 2024-03-01 15:55:17 +0100 | [diff] [blame] | 7415 | rule->arg.hlua_rule->fcn->name, hlua_tostring_safe(hlua->T, -1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7416 | lua_pop(hlua->T, 1); |
Aurelien DARRAGON | 6ab7ec9 | 2024-03-01 21:17:51 +0100 | [diff] [blame] | 7417 | hlua_unlock(hlua); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7418 | goto error; |
| 7419 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7420 | case HLUA_E_ETMOUT: |
| 7421 | SEND_ERR(px, "Lua applet tcp '%s': execution timeout.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7422 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7423 | goto error; |
| 7424 | |
| 7425 | case HLUA_E_NOMEM: |
| 7426 | SEND_ERR(px, "Lua applet tcp '%s': out of memory error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7427 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7428 | goto error; |
| 7429 | |
| 7430 | case HLUA_E_YIELD: /* unexpected */ |
| 7431 | SEND_ERR(px, "Lua applet tcp '%s': yield not allowed.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7432 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7433 | goto error; |
| 7434 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7435 | case HLUA_E_ERR: |
| 7436 | /* Display log. */ |
| 7437 | SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7438 | rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7439 | goto error; |
| 7440 | |
| 7441 | default: |
| 7442 | goto error; |
| 7443 | } |
| 7444 | |
| 7445 | error: |
| 7446 | |
| 7447 | /* For all other cases, just close the stream. */ |
| 7448 | si_shutw(si); |
| 7449 | si_shutr(si); |
| 7450 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 7451 | } |
| 7452 | |
| 7453 | static void hlua_applet_tcp_release(struct appctx *ctx) |
| 7454 | { |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 7455 | task_destroy(ctx->ctx.hlua_apptcp.task); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7456 | ctx->ctx.hlua_apptcp.task = NULL; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7457 | hlua_ctx_destroy(ctx->ctx.hlua_apptcp.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7458 | ctx->ctx.hlua_apptcp.hlua = NULL; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7459 | } |
| 7460 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7461 | /* The function returns 1 if the initialisation is complete, 0 if |
| 7462 | * an errors occurs and -1 if more data are required for initializing |
| 7463 | * the applet. |
| 7464 | */ |
| 7465 | static int hlua_applet_http_init(struct appctx *ctx, struct proxy *px, struct stream *strm) |
| 7466 | { |
| 7467 | struct stream_interface *si = ctx->owner; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7468 | struct http_txn *txn; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7469 | struct hlua *hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7470 | char **arg; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7471 | struct task *task; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7472 | const char *error; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7473 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7474 | txn = strm->txn; |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7475 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7476 | if (!hlua) { |
| 7477 | SEND_ERR(px, "Lua applet http '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7478 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7479 | return 0; |
| 7480 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7481 | HLUA_INIT(hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7482 | ctx->ctx.hlua_apphttp.hlua = hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7483 | ctx->ctx.hlua_apphttp.left_bytes = -1; |
| 7484 | ctx->ctx.hlua_apphttp.flags = 0; |
| 7485 | |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 7486 | if (txn->req.flags & HTTP_MSGF_VER_11) |
| 7487 | ctx->ctx.hlua_apphttp.flags |= APPLET_HTTP11; |
| 7488 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7489 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 7490 | task = task_new(tid_bit); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7491 | if (!task) { |
| 7492 | SEND_ERR(px, "Lua applet http '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7493 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7494 | return 0; |
| 7495 | } |
| 7496 | task->nice = 0; |
| 7497 | task->context = ctx; |
| 7498 | task->process = hlua_applet_wakeup; |
| 7499 | ctx->ctx.hlua_apphttp.task = task; |
| 7500 | |
| 7501 | /* In the execution wrappers linked with a stream, the |
| 7502 | * Lua context can be not initialized. This behavior |
| 7503 | * permits to save performances because a systematic |
| 7504 | * Lua initialization cause 5% performances loss. |
| 7505 | */ |
Aurelien DARRAGON | 5483bde | 2023-03-21 14:02:16 +0100 | [diff] [blame] | 7506 | 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] | 7507 | 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] | 7508 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7509 | return 0; |
| 7510 | } |
| 7511 | |
| 7512 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 7513 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7514 | |
| 7515 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7516 | if (!SET_SAFE_LJMP(hlua)) { |
Aurelien DARRAGON | d4df562 | 2024-03-04 11:17:58 +0100 | [diff] [blame] | 7517 | hlua_lock(hlua); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7518 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
Aurelien DARRAGON | a374161 | 2024-03-01 15:55:17 +0100 | [diff] [blame] | 7519 | error = hlua_tostring_safe(hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7520 | else |
| 7521 | error = "critical error"; |
| 7522 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7523 | ctx->rule->arg.hlua_rule->fcn->name, error); |
Aurelien DARRAGON | d4df562 | 2024-03-04 11:17:58 +0100 | [diff] [blame] | 7524 | hlua_unlock(hlua); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7525 | return 0; |
| 7526 | } |
| 7527 | |
| 7528 | /* Check stack available size. */ |
| 7529 | if (!lua_checkstack(hlua->T, 1)) { |
| 7530 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7531 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7532 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7533 | return 0; |
| 7534 | } |
| 7535 | |
| 7536 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 7537 | 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] | 7538 | |
| 7539 | /* Create and and push object stream in the stack. */ |
| 7540 | if (!hlua_applet_http_new(hlua->T, ctx)) { |
| 7541 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7542 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7543 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7544 | return 0; |
| 7545 | } |
| 7546 | hlua->nargs = 1; |
| 7547 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7548 | /* push keywords in the stack. */ |
| 7549 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 7550 | if (!lua_checkstack(hlua->T, 1)) { |
| 7551 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7552 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7553 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7554 | return 0; |
| 7555 | } |
| 7556 | lua_pushstring(hlua->T, *arg); |
| 7557 | hlua->nargs++; |
| 7558 | } |
| 7559 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7560 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7561 | |
| 7562 | /* Wakeup the applet when data is ready for read. */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 7563 | si_cant_get(si); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7564 | |
| 7565 | return 1; |
| 7566 | } |
| 7567 | |
Willy Tarreau | 60409db | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 7568 | void hlua_applet_http_fct(struct appctx *ctx) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7569 | { |
| 7570 | struct stream_interface *si = ctx->owner; |
| 7571 | struct stream *strm = si_strm(si); |
| 7572 | struct channel *req = si_oc(si); |
| 7573 | struct channel *res = si_ic(si); |
| 7574 | struct act_rule *rule = ctx->rule; |
| 7575 | struct proxy *px = strm->be; |
| 7576 | struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua; |
| 7577 | struct htx *req_htx, *res_htx; |
| 7578 | |
| 7579 | res_htx = htx_from_buf(&res->buf); |
| 7580 | |
| 7581 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 7582 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 7583 | goto out; |
| 7584 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 7585 | /* Check if the input buffer is available. */ |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7586 | if (!b_size(&res->buf)) { |
| 7587 | si_rx_room_blk(si); |
| 7588 | goto out; |
| 7589 | } |
| 7590 | /* check that the output is not closed */ |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7591 | if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR)) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7592 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 7593 | |
| 7594 | /* Set the currently running flag. */ |
| 7595 | if (!HLUA_IS_RUNNING(hlua) && |
| 7596 | !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
Christopher Faulet | bd878d2 | 2021-04-28 10:50:21 +0200 | [diff] [blame] | 7597 | if (!co_data(req)) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7598 | si_cant_get(si); |
| 7599 | goto out; |
| 7600 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7601 | } |
| 7602 | |
| 7603 | /* Executes The applet if it is not done. */ |
| 7604 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
| 7605 | |
| 7606 | /* Execute the function. */ |
| 7607 | switch (hlua_ctx_resume(hlua, 1)) { |
| 7608 | /* finished. */ |
| 7609 | case HLUA_E_OK: |
| 7610 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 7611 | break; |
| 7612 | |
| 7613 | /* yield. */ |
| 7614 | case HLUA_E_AGAIN: |
| 7615 | if (hlua->wake_time != TICK_ETERNITY) |
| 7616 | task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time); |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7617 | goto out; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7618 | |
| 7619 | /* finished with error. */ |
| 7620 | case HLUA_E_ERRMSG: |
| 7621 | /* Display log. */ |
Aurelien DARRAGON | 6ab7ec9 | 2024-03-01 21:17:51 +0100 | [diff] [blame] | 7622 | hlua_lock(hlua); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7623 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
Aurelien DARRAGON | a374161 | 2024-03-01 15:55:17 +0100 | [diff] [blame] | 7624 | rule->arg.hlua_rule->fcn->name, hlua_tostring_safe(hlua->T, -1)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7625 | lua_pop(hlua->T, 1); |
Aurelien DARRAGON | 6ab7ec9 | 2024-03-01 21:17:51 +0100 | [diff] [blame] | 7626 | hlua_unlock(hlua); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7627 | goto error; |
| 7628 | |
| 7629 | case HLUA_E_ETMOUT: |
| 7630 | SEND_ERR(px, "Lua applet http '%s': execution timeout.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7631 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7632 | goto error; |
| 7633 | |
| 7634 | case HLUA_E_NOMEM: |
| 7635 | SEND_ERR(px, "Lua applet http '%s': out of memory error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7636 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7637 | goto error; |
| 7638 | |
| 7639 | case HLUA_E_YIELD: /* unexpected */ |
| 7640 | SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7641 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7642 | goto error; |
| 7643 | |
| 7644 | case HLUA_E_ERR: |
| 7645 | /* Display log. */ |
| 7646 | SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7647 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7648 | goto error; |
| 7649 | |
| 7650 | default: |
| 7651 | goto error; |
| 7652 | } |
| 7653 | } |
| 7654 | |
| 7655 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7656 | if (ctx->ctx.hlua_apphttp.flags & APPLET_RSP_SENT) |
| 7657 | goto done; |
| 7658 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7659 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) |
| 7660 | goto error; |
| 7661 | |
Christopher Faulet | 868b3b1 | 2022-04-07 10:07:18 +0200 | [diff] [blame] | 7662 | /* no more data are expected. If the response buffer is empty |
| 7663 | * for a chunked message, be sure to add something (EOT block in |
| 7664 | * this case) to have something to send. It is important to be |
| 7665 | * sure the EOM flags will be handled by the endpoint. |
| 7666 | */ |
| 7667 | if (htx_is_empty(res_htx) && (strm->txn->rsp.flags & (HTTP_MSGF_XFER_LEN|HTTP_MSGF_CNT_LEN)) == HTTP_MSGF_XFER_LEN) { |
| 7668 | if (!htx_add_endof(res_htx, HTX_BLK_EOT)) { |
| 7669 | si_rx_room_blk(si); |
| 7670 | goto out; |
| 7671 | } |
| 7672 | channel_add_input(res, 1); |
| 7673 | } |
| 7674 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 7675 | res_htx->flags |= HTX_FL_EOM; |
Christopher Faulet | 79c0fc7 | 2022-03-07 15:50:54 +0100 | [diff] [blame] | 7676 | res->flags |= CF_EOI; |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7677 | strm->txn->status = ctx->ctx.hlua_apphttp.status; |
| 7678 | ctx->ctx.hlua_apphttp.flags |= APPLET_RSP_SENT; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7679 | } |
| 7680 | |
| 7681 | done: |
| 7682 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7683 | if (!(res->flags & CF_SHUTR)) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7684 | res->flags |= CF_READ_NULL; |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7685 | si_shutr(si); |
| 7686 | } |
| 7687 | |
| 7688 | /* eat the whole request */ |
| 7689 | if (co_data(req)) { |
| 7690 | req_htx = htx_from_buf(&req->buf); |
| 7691 | co_htx_skip(req, req_htx, co_data(req)); |
| 7692 | htx_to_buf(req_htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7693 | } |
| 7694 | } |
| 7695 | |
| 7696 | out: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7697 | htx_to_buf(res_htx, &res->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7698 | return; |
| 7699 | |
| 7700 | error: |
| 7701 | |
| 7702 | /* If we are in HTTP mode, and we are not send any |
| 7703 | * data, return a 500 server error in best effort: |
| 7704 | * if there is no room available in the buffer, |
| 7705 | * just close the connection. |
| 7706 | */ |
| 7707 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) { |
Christopher Faulet | f734638 | 2019-07-17 22:02:08 +0200 | [diff] [blame] | 7708 | struct buffer *err = &http_err_chunks[HTTP_ERR_500]; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7709 | |
| 7710 | channel_erase(res); |
| 7711 | res->buf.data = b_data(err); |
| 7712 | memcpy(res->buf.area, b_head(err), b_data(err)); |
| 7713 | res_htx = htx_from_buf(&res->buf); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 7714 | channel_add_input(res, res_htx->data); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7715 | } |
| 7716 | if (!(strm->flags & SF_ERR_MASK)) |
| 7717 | strm->flags |= SF_ERR_RESOURCE; |
| 7718 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 7719 | goto done; |
| 7720 | } |
| 7721 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7722 | static void hlua_applet_http_release(struct appctx *ctx) |
| 7723 | { |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 7724 | task_destroy(ctx->ctx.hlua_apphttp.task); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7725 | ctx->ctx.hlua_apphttp.task = NULL; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7726 | hlua_ctx_destroy(ctx->ctx.hlua_apphttp.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7727 | ctx->ctx.hlua_apphttp.hlua = NULL; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7728 | } |
| 7729 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7730 | /* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 7731 | * success case, else return ACT_RET_PRS_ERR. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7732 | * |
| 7733 | * This function can fail with an abort() due to an Lua critical error. |
| 7734 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 7735 | * tolerated. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7736 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7737 | static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px, |
| 7738 | struct act_rule *rule, char **err) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7739 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7740 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7741 | int i; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7742 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7743 | /* Memory for the rule. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7744 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7745 | if (!rule->arg.hlua_rule) { |
| 7746 | memprintf(err, "out of memory error"); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 7747 | goto error; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7748 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7749 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7750 | /* Memory for arguments. */ |
Tim Duesterhus | e52b6e5 | 2020-09-12 20:26:43 +0200 | [diff] [blame] | 7751 | rule->arg.hlua_rule->args = calloc(fcn->nargs + 1, |
| 7752 | sizeof(*rule->arg.hlua_rule->args)); |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7753 | if (!rule->arg.hlua_rule->args) { |
| 7754 | memprintf(err, "out of memory error"); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 7755 | goto error; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7756 | } |
| 7757 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7758 | /* Reference the Lua function and store the reference. */ |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7759 | rule->arg.hlua_rule->fcn = fcn; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7760 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7761 | /* Expect some arguments */ |
| 7762 | for (i = 0; i < fcn->nargs; i++) { |
Thierry FOURNIER | 1725c2e | 2019-01-06 19:38:49 +0100 | [diff] [blame] | 7763 | if (*args[*cur_arg] == '\0') { |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7764 | memprintf(err, "expect %d arguments", fcn->nargs); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 7765 | goto error; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7766 | } |
Thierry FOURNIER | 1725c2e | 2019-01-06 19:38:49 +0100 | [diff] [blame] | 7767 | rule->arg.hlua_rule->args[i] = strdup(args[*cur_arg]); |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7768 | if (!rule->arg.hlua_rule->args[i]) { |
| 7769 | memprintf(err, "out of memory error"); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 7770 | goto error; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7771 | } |
| 7772 | (*cur_arg)++; |
| 7773 | } |
| 7774 | rule->arg.hlua_rule->args[i] = NULL; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7775 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 7776 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7777 | rule->action_ptr = hlua_action; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 7778 | return ACT_RET_PRS_OK; |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 7779 | |
| 7780 | error: |
| 7781 | if (rule->arg.hlua_rule) { |
| 7782 | if (rule->arg.hlua_rule->args) { |
| 7783 | for (i = 0; i < fcn->nargs; i++) |
| 7784 | ha_free(&rule->arg.hlua_rule->args[i]); |
| 7785 | ha_free(&rule->arg.hlua_rule->args); |
| 7786 | } |
| 7787 | ha_free(&rule->arg.hlua_rule); |
| 7788 | } |
| 7789 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7790 | } |
| 7791 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7792 | static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px, |
| 7793 | struct act_rule *rule, char **err) |
| 7794 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7795 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7796 | |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 7797 | /* HTTP applets are forbidden in tcp-request rules. |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 7798 | * HTTP applet request requires everything initialized by |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 7799 | * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER). |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 7800 | * The applet will be immediately initialized, but its before |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 7801 | * the call of this analyzer. |
| 7802 | */ |
| 7803 | if (rule->from != ACT_F_HTTP_REQ) { |
| 7804 | memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets"); |
| 7805 | return ACT_RET_PRS_ERR; |
| 7806 | } |
| 7807 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7808 | /* Memory for the rule. */ |
| 7809 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 7810 | if (!rule->arg.hlua_rule) { |
| 7811 | memprintf(err, "out of memory error"); |
| 7812 | return ACT_RET_PRS_ERR; |
| 7813 | } |
| 7814 | |
| 7815 | /* Reference the Lua function and store the reference. */ |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7816 | rule->arg.hlua_rule->fcn = fcn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7817 | |
| 7818 | /* TODO: later accept arguments. */ |
| 7819 | rule->arg.hlua_rule->args = NULL; |
| 7820 | |
| 7821 | /* Add applet pointer in the rule. */ |
| 7822 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 7823 | rule->applet.name = fcn->name; |
| 7824 | rule->applet.init = hlua_applet_http_init; |
| 7825 | rule->applet.fct = hlua_applet_http_fct; |
| 7826 | rule->applet.release = hlua_applet_http_release; |
| 7827 | rule->applet.timeout = hlua_timeout_applet; |
| 7828 | |
| 7829 | return ACT_RET_PRS_OK; |
| 7830 | } |
| 7831 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7832 | /* This function is an LUA binding used for registering |
| 7833 | * "sample-conv" functions. It expects a converter name used |
| 7834 | * in the haproxy configuration file, and an LUA function. |
| 7835 | */ |
| 7836 | __LJMP static int hlua_register_action(lua_State *L) |
| 7837 | { |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 7838 | struct action_kw_list *akl = NULL; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7839 | const char *name; |
| 7840 | int ref; |
| 7841 | int len; |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 7842 | struct hlua_function *fcn = NULL; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7843 | int nargs; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 7844 | struct buffer *trash; |
| 7845 | struct action_kw *akw; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7846 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7847 | /* Initialise the number of expected arguments at 0. */ |
| 7848 | nargs = 0; |
| 7849 | |
| 7850 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 7851 | 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] | 7852 | |
Aurelien DARRAGON | 8985ab8 | 2023-02-24 09:43:54 +0100 | [diff] [blame] | 7853 | if (hlua_gethlua(L)) { |
| 7854 | /* runtime processing */ |
| 7855 | WILL_LJMP(luaL_error(L, "register_action: not available outside of body context")); |
| 7856 | } |
| 7857 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7858 | /* First argument : converter name. */ |
| 7859 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 7860 | |
| 7861 | /* Second argument : environment. */ |
| 7862 | if (lua_type(L, 2) != LUA_TTABLE) |
| 7863 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 7864 | |
| 7865 | /* Third argument : lua function. */ |
| 7866 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 7867 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7868 | /* 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] | 7869 | if (lua_gettop(L) >= 4) |
| 7870 | nargs = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 7871 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7872 | /* browse the second argument as an array. */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7873 | lua_pushnil(L); |
| 7874 | while (lua_next(L, 2) != 0) { |
| 7875 | if (lua_type(L, -1) != LUA_TSTRING) |
| 7876 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 7877 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 7878 | /* Check if action exists */ |
| 7879 | trash = get_trash_chunk(); |
| 7880 | chunk_printf(trash, "lua.%s", name); |
| 7881 | if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) { |
| 7882 | akw = tcp_req_cont_action(trash->area); |
| 7883 | } else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) { |
| 7884 | akw = tcp_res_cont_action(trash->area); |
| 7885 | } else if (strcmp(lua_tostring(L, -1), "http-req") == 0) { |
| 7886 | akw = action_http_req_custom(trash->area); |
| 7887 | } else if (strcmp(lua_tostring(L, -1), "http-res") == 0) { |
| 7888 | akw = action_http_res_custom(trash->area); |
| 7889 | } else { |
| 7890 | akw = NULL; |
| 7891 | } |
| 7892 | if (akw != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 7893 | fcn = akw->private; |
| 7894 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 7895 | ha_warning("Trying to register action 'lua.%s' more than once. " |
| 7896 | "This will become a hard error in version 2.5.\n", name); |
| 7897 | } |
| 7898 | fcn->function_ref[hlua_state_id] = ref; |
| 7899 | |
| 7900 | /* pop the environment string. */ |
| 7901 | lua_pop(L, 1); |
| 7902 | continue; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 7903 | } |
| 7904 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7905 | /* Check required environment. Only accepted "http" or "tcp". */ |
| 7906 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7907 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7908 | if (!akl) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 7909 | goto alloc_error;; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 7910 | fcn = new_hlua_function(); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7911 | if (!fcn) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 7912 | goto alloc_error; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7913 | |
| 7914 | /* Fill fcn. */ |
| 7915 | fcn->name = strdup(name); |
| 7916 | if (!fcn->name) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 7917 | goto alloc_error; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 7918 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7919 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 7920 | /* Set the expected number of arguments. */ |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7921 | fcn->nargs = nargs; |
| 7922 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7923 | /* List head */ |
| 7924 | akl->list.n = akl->list.p = NULL; |
| 7925 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7926 | /* action keyword. */ |
| 7927 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7928 | akl->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7929 | if (!akl->kw[0].kw) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 7930 | goto alloc_error; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7931 | |
| 7932 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 7933 | |
Amaury Denoyelle | e4a617c | 2021-05-06 15:33:09 +0200 | [diff] [blame] | 7934 | akl->kw[0].flags = 0; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7935 | akl->kw[0].private = fcn; |
| 7936 | akl->kw[0].parse = action_register_lua; |
| 7937 | |
| 7938 | /* select the action registering point. */ |
| 7939 | if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) |
| 7940 | tcp_req_cont_keywords_register(akl); |
| 7941 | else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) |
| 7942 | tcp_res_cont_keywords_register(akl); |
| 7943 | else if (strcmp(lua_tostring(L, -1), "http-req") == 0) |
| 7944 | http_req_keywords_register(akl); |
| 7945 | else if (strcmp(lua_tostring(L, -1), "http-res") == 0) |
| 7946 | http_res_keywords_register(akl); |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 7947 | else { |
| 7948 | release_hlua_function(fcn); |
| 7949 | if (akl) |
| 7950 | ha_free((char **)&(akl->kw[0].kw)); |
| 7951 | ha_free(&akl); |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7952 | WILL_LJMP(luaL_error(L, "Lua action environment '%s' is unknown. " |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7953 | "'tcp-req', 'tcp-res', 'http-req' or 'http-res' " |
| 7954 | "are expected.", lua_tostring(L, -1))); |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 7955 | } |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7956 | |
| 7957 | /* pop the environment string. */ |
| 7958 | lua_pop(L, 1); |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 7959 | |
| 7960 | /* reset for next loop */ |
| 7961 | akl = NULL; |
| 7962 | fcn = NULL; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7963 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7964 | return ACT_RET_PRS_OK; |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 7965 | |
| 7966 | alloc_error: |
| 7967 | release_hlua_function(fcn); |
| 7968 | ha_free(&akl); |
| 7969 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 7970 | return 0; /* Never reached */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7971 | } |
| 7972 | |
| 7973 | static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px, |
| 7974 | struct act_rule *rule, char **err) |
| 7975 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7976 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7977 | |
Christopher Faulet | 280f85b | 2019-07-15 15:02:04 +0200 | [diff] [blame] | 7978 | if (px->mode == PR_MODE_HTTP) { |
| 7979 | memprintf(err, "Lua TCP services cannot be used on HTTP proxies"); |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 7980 | return ACT_RET_PRS_ERR; |
| 7981 | } |
| 7982 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7983 | /* Memory for the rule. */ |
| 7984 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 7985 | if (!rule->arg.hlua_rule) { |
| 7986 | memprintf(err, "out of memory error"); |
| 7987 | return ACT_RET_PRS_ERR; |
| 7988 | } |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7989 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7990 | /* Reference the Lua function and store the reference. */ |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7991 | rule->arg.hlua_rule->fcn = fcn; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7992 | |
| 7993 | /* TODO: later accept arguments. */ |
| 7994 | rule->arg.hlua_rule->args = NULL; |
| 7995 | |
| 7996 | /* Add applet pointer in the rule. */ |
| 7997 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 7998 | rule->applet.name = fcn->name; |
| 7999 | rule->applet.init = hlua_applet_tcp_init; |
| 8000 | rule->applet.fct = hlua_applet_tcp_fct; |
| 8001 | rule->applet.release = hlua_applet_tcp_release; |
| 8002 | rule->applet.timeout = hlua_timeout_applet; |
| 8003 | |
| 8004 | return 0; |
| 8005 | } |
| 8006 | |
| 8007 | /* This function is an LUA binding used for registering |
| 8008 | * "sample-conv" functions. It expects a converter name used |
| 8009 | * in the haproxy configuration file, and an LUA function. |
| 8010 | */ |
| 8011 | __LJMP static int hlua_register_service(lua_State *L) |
| 8012 | { |
| 8013 | struct action_kw_list *akl; |
| 8014 | const char *name; |
| 8015 | const char *env; |
| 8016 | int ref; |
| 8017 | int len; |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 8018 | struct hlua_function *fcn = NULL; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8019 | struct buffer *trash; |
| 8020 | struct action_kw *akw; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8021 | |
| 8022 | MAY_LJMP(check_args(L, 3, "register_service")); |
| 8023 | |
Aurelien DARRAGON | 8985ab8 | 2023-02-24 09:43:54 +0100 | [diff] [blame] | 8024 | if (hlua_gethlua(L)) { |
| 8025 | /* runtime processing */ |
| 8026 | WILL_LJMP(luaL_error(L, "register_service: not available outside of body context")); |
| 8027 | } |
| 8028 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8029 | /* First argument : converter name. */ |
| 8030 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 8031 | |
| 8032 | /* Second argument : environment. */ |
| 8033 | env = MAY_LJMP(luaL_checkstring(L, 2)); |
| 8034 | |
| 8035 | /* Third argument : lua function. */ |
| 8036 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 8037 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8038 | /* Check for service already registered */ |
| 8039 | trash = get_trash_chunk(); |
| 8040 | chunk_printf(trash, "lua.%s", name); |
| 8041 | akw = service_find(trash->area); |
| 8042 | if (akw != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8043 | fcn = akw->private; |
| 8044 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 8045 | ha_warning("Trying to register service 'lua.%s' more than once. " |
| 8046 | "This will become a hard error in version 2.5.\n", name); |
| 8047 | } |
| 8048 | fcn->function_ref[hlua_state_id] = ref; |
| 8049 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8050 | } |
| 8051 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8052 | /* Allocate and fill the sample fetch keyword struct. */ |
| 8053 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
| 8054 | if (!akl) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 8055 | goto alloc_error; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 8056 | fcn = new_hlua_function(); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8057 | if (!fcn) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 8058 | goto alloc_error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8059 | |
| 8060 | /* Fill fcn. */ |
| 8061 | len = strlen("<lua.>") + strlen(name) + 1; |
| 8062 | fcn->name = calloc(1, len); |
| 8063 | if (!fcn->name) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 8064 | goto alloc_error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8065 | snprintf((char *)fcn->name, len, "<lua.%s>", name); |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8066 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8067 | |
| 8068 | /* List head */ |
| 8069 | akl->list.n = akl->list.p = NULL; |
| 8070 | |
| 8071 | /* converter keyword. */ |
| 8072 | len = strlen("lua.") + strlen(name) + 1; |
| 8073 | akl->kw[0].kw = calloc(1, len); |
| 8074 | if (!akl->kw[0].kw) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 8075 | goto alloc_error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8076 | |
| 8077 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 8078 | |
Thierry FOURNIER / OZON.IO | 02564fd | 2016-11-12 11:07:05 +0100 | [diff] [blame] | 8079 | /* Check required environment. Only accepted "http" or "tcp". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8080 | if (strcmp(env, "tcp") == 0) |
| 8081 | akl->kw[0].parse = action_register_service_tcp; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8082 | else if (strcmp(env, "http") == 0) |
| 8083 | akl->kw[0].parse = action_register_service_http; |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 8084 | else { |
| 8085 | release_hlua_function(fcn); |
| 8086 | if (akl) |
| 8087 | ha_free((char **)&(akl->kw[0].kw)); |
| 8088 | ha_free(&akl); |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8089 | WILL_LJMP(luaL_error(L, "Lua service environment '%s' is unknown. " |
Eric Salama | fe7456f | 2017-12-21 14:30:07 +0100 | [diff] [blame] | 8090 | "'tcp' or 'http' are expected.", env)); |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 8091 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8092 | |
Amaury Denoyelle | e4a617c | 2021-05-06 15:33:09 +0200 | [diff] [blame] | 8093 | akl->kw[0].flags = 0; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8094 | akl->kw[0].private = fcn; |
| 8095 | |
| 8096 | /* End of array. */ |
| 8097 | memset(&akl->kw[1], 0, sizeof(*akl->kw)); |
| 8098 | |
| 8099 | /* Register this new converter */ |
| 8100 | service_keywords_register(akl); |
| 8101 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 8102 | return 0; |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 8103 | |
| 8104 | alloc_error: |
| 8105 | release_hlua_function(fcn); |
| 8106 | ha_free(&akl); |
| 8107 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 8108 | return 0; /* Never reached */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 8109 | } |
| 8110 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8111 | /* This function initialises Lua cli handler. It copies the |
| 8112 | * arguments in the Lua stack and create channel IO objects. |
| 8113 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 8114 | 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] | 8115 | { |
| 8116 | struct hlua *hlua; |
| 8117 | struct hlua_function *fcn; |
| 8118 | int i; |
| 8119 | const char *error; |
| 8120 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8121 | fcn = private; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 8122 | appctx->ctx.hlua_cli.fcn = private; |
| 8123 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 8124 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 8125 | if (!hlua) { |
| 8126 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 8127 | return 1; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 8128 | } |
| 8129 | HLUA_INIT(hlua); |
| 8130 | appctx->ctx.hlua_cli.hlua = hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8131 | |
| 8132 | /* Create task used by signal to wakeup applets. |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8133 | * 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] | 8134 | * applet_http. It is absolutely compatible. |
| 8135 | */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 8136 | appctx->ctx.hlua_cli.task = task_new(tid_bit); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8137 | if (!appctx->ctx.hlua_cli.task) { |
Thierry FOURNIER | ffbf569 | 2016-12-16 11:14:06 +0100 | [diff] [blame] | 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 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8140 | } |
| 8141 | appctx->ctx.hlua_cli.task->nice = 0; |
| 8142 | appctx->ctx.hlua_cli.task->context = appctx; |
| 8143 | appctx->ctx.hlua_cli.task->process = hlua_applet_wakeup; |
| 8144 | |
| 8145 | /* Initialises the Lua context */ |
Aurelien DARRAGON | 5483bde | 2023-03-21 14:02:16 +0100 | [diff] [blame] | 8146 | 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] | 8147 | 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] | 8148 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8149 | } |
| 8150 | |
| 8151 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8152 | if (!SET_SAFE_LJMP(hlua)) { |
Aurelien DARRAGON | d4df562 | 2024-03-04 11:17:58 +0100 | [diff] [blame] | 8153 | hlua_lock(hlua); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8154 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
Aurelien DARRAGON | a374161 | 2024-03-01 15:55:17 +0100 | [diff] [blame] | 8155 | error = hlua_tostring_safe(hlua->T, -1); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8156 | else |
| 8157 | error = "critical error"; |
| 8158 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error); |
Aurelien DARRAGON | d4df562 | 2024-03-04 11:17:58 +0100 | [diff] [blame] | 8159 | hlua_unlock(hlua); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8160 | goto error; |
| 8161 | } |
| 8162 | |
| 8163 | /* Check stack available size. */ |
| 8164 | if (!lua_checkstack(hlua->T, 2)) { |
| 8165 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 8166 | goto error; |
| 8167 | } |
| 8168 | |
| 8169 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8170 | 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] | 8171 | |
| 8172 | /* Once the arguments parsed, the CLI is like an AppletTCP, |
| 8173 | * so push AppletTCP in the stack. |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8174 | */ |
| 8175 | if (!hlua_applet_tcp_new(hlua->T, appctx)) { |
| 8176 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 8177 | goto error; |
| 8178 | } |
| 8179 | hlua->nargs = 1; |
| 8180 | |
| 8181 | /* push keywords in the stack. */ |
| 8182 | for (i = 0; *args[i]; i++) { |
| 8183 | /* Check stack available size. */ |
| 8184 | if (!lua_checkstack(hlua->T, 1)) { |
| 8185 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 8186 | goto error; |
| 8187 | } |
| 8188 | lua_pushstring(hlua->T, args[i]); |
| 8189 | hlua->nargs++; |
| 8190 | } |
| 8191 | |
| 8192 | /* We must initialize the execution timeouts. */ |
| 8193 | hlua->max_time = hlua_timeout_session; |
| 8194 | |
| 8195 | /* At this point the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8196 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8197 | |
| 8198 | /* It's ok */ |
| 8199 | return 0; |
| 8200 | |
| 8201 | /* It's not ok. */ |
| 8202 | error: |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8203 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8204 | hlua_ctx_destroy(hlua); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 8205 | appctx->ctx.hlua_cli.hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8206 | return 1; |
| 8207 | } |
| 8208 | |
| 8209 | static int hlua_cli_io_handler_fct(struct appctx *appctx) |
| 8210 | { |
| 8211 | struct hlua *hlua; |
| 8212 | struct stream_interface *si; |
| 8213 | struct hlua_function *fcn; |
| 8214 | |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 8215 | hlua = appctx->ctx.hlua_cli.hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8216 | si = appctx->owner; |
Willy Tarreau | 8ae4f75 | 2016-12-14 15:41:45 +0100 | [diff] [blame] | 8217 | fcn = appctx->ctx.hlua_cli.fcn; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8218 | |
| 8219 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 8220 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 8221 | return 1; |
| 8222 | |
| 8223 | /* Execute the function. */ |
| 8224 | switch (hlua_ctx_resume(hlua, 1)) { |
| 8225 | |
| 8226 | /* finished. */ |
| 8227 | case HLUA_E_OK: |
| 8228 | return 1; |
| 8229 | |
| 8230 | /* yield. */ |
| 8231 | case HLUA_E_AGAIN: |
| 8232 | /* We want write. */ |
| 8233 | if (HLUA_IS_WAKERESWR(hlua)) |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 8234 | si_rx_room_blk(si); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8235 | /* Set the timeout. */ |
| 8236 | if (hlua->wake_time != TICK_ETERNITY) |
| 8237 | task_schedule(hlua->task, hlua->wake_time); |
| 8238 | return 0; |
| 8239 | |
| 8240 | /* finished with error. */ |
| 8241 | case HLUA_E_ERRMSG: |
| 8242 | /* Display log. */ |
Aurelien DARRAGON | 6ab7ec9 | 2024-03-01 21:17:51 +0100 | [diff] [blame] | 8243 | hlua_lock(hlua); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8244 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", |
Aurelien DARRAGON | a374161 | 2024-03-01 15:55:17 +0100 | [diff] [blame] | 8245 | fcn->name, hlua_tostring_safe(hlua->T, -1)); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8246 | lua_pop(hlua->T, 1); |
Aurelien DARRAGON | 6ab7ec9 | 2024-03-01 21:17:51 +0100 | [diff] [blame] | 8247 | hlua_unlock(hlua); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8248 | return 1; |
| 8249 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 8250 | case HLUA_E_ETMOUT: |
| 8251 | SEND_ERR(NULL, "Lua converter '%s': execution timeout.\n", |
| 8252 | fcn->name); |
| 8253 | return 1; |
| 8254 | |
| 8255 | case HLUA_E_NOMEM: |
| 8256 | SEND_ERR(NULL, "Lua converter '%s': out of memory error.\n", |
| 8257 | fcn->name); |
| 8258 | return 1; |
| 8259 | |
| 8260 | case HLUA_E_YIELD: /* unexpected */ |
| 8261 | SEND_ERR(NULL, "Lua converter '%s': yield not allowed.\n", |
| 8262 | fcn->name); |
| 8263 | return 1; |
| 8264 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8265 | case HLUA_E_ERR: |
| 8266 | /* Display log. */ |
| 8267 | SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n", |
| 8268 | fcn->name); |
| 8269 | return 1; |
| 8270 | |
| 8271 | default: |
| 8272 | return 1; |
| 8273 | } |
| 8274 | |
| 8275 | return 1; |
| 8276 | } |
| 8277 | |
| 8278 | static void hlua_cli_io_release_fct(struct appctx *appctx) |
| 8279 | { |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 8280 | hlua_ctx_destroy(appctx->ctx.hlua_cli.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 8281 | appctx->ctx.hlua_cli.hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8282 | } |
| 8283 | |
| 8284 | /* This function is an LUA binding used for registering |
| 8285 | * new keywords in the cli. It expects a list of keywords |
| 8286 | * which are the "path". It is limited to 5 keywords. A |
| 8287 | * description of the command, a function to be executed |
| 8288 | * for the parsing and a function for io handlers. |
| 8289 | */ |
| 8290 | __LJMP static int hlua_register_cli(lua_State *L) |
| 8291 | { |
| 8292 | struct cli_kw_list *cli_kws; |
| 8293 | const char *message; |
| 8294 | int ref_io; |
| 8295 | int len; |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 8296 | struct hlua_function *fcn = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8297 | int index; |
| 8298 | int i; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8299 | struct buffer *trash; |
| 8300 | const char *kw[5]; |
| 8301 | struct cli_kw *cli_kw; |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 8302 | const char *errmsg; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8303 | |
| 8304 | MAY_LJMP(check_args(L, 3, "register_cli")); |
| 8305 | |
Aurelien DARRAGON | 8985ab8 | 2023-02-24 09:43:54 +0100 | [diff] [blame] | 8306 | if (hlua_gethlua(L)) { |
| 8307 | /* runtime processing */ |
| 8308 | WILL_LJMP(luaL_error(L, "register_cli: not available outside of body context")); |
| 8309 | } |
| 8310 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8311 | /* First argument : an array of maximum 5 keywords. */ |
| 8312 | if (!lua_istable(L, 1)) |
| 8313 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table")); |
| 8314 | |
| 8315 | /* Second argument : string with contextual message. */ |
| 8316 | message = MAY_LJMP(luaL_checkstring(L, 2)); |
| 8317 | |
| 8318 | /* Third and fourth argument : lua function. */ |
| 8319 | ref_io = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 8320 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8321 | /* Check for CLI service already registered */ |
| 8322 | trash = get_trash_chunk(); |
| 8323 | index = 0; |
| 8324 | lua_pushnil(L); |
| 8325 | memset(kw, 0, sizeof(kw)); |
| 8326 | while (lua_next(L, 1) != 0) { |
| 8327 | if (index >= CLI_PREFIX_KW_NB) |
| 8328 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries")); |
| 8329 | if (lua_type(L, -1) != LUA_TSTRING) |
| 8330 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings")); |
| 8331 | kw[index] = lua_tostring(L, -1); |
| 8332 | if (index == 0) |
| 8333 | chunk_printf(trash, "%s", kw[index]); |
| 8334 | else |
| 8335 | chunk_appendf(trash, " %s", kw[index]); |
| 8336 | index++; |
| 8337 | lua_pop(L, 1); |
| 8338 | } |
| 8339 | cli_kw = cli_find_kw_exact((char **)kw); |
| 8340 | if (cli_kw != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8341 | fcn = cli_kw->private; |
| 8342 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 8343 | ha_warning("Trying to register CLI keyword 'lua.%s' more than once. " |
| 8344 | "This will become a hard error in version 2.5.\n", trash->area); |
| 8345 | } |
| 8346 | fcn->function_ref[hlua_state_id] = ref_io; |
| 8347 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8348 | } |
| 8349 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8350 | /* Allocate and fill the sample fetch keyword struct. */ |
| 8351 | cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 8352 | if (!cli_kws) { |
| 8353 | errmsg = "Lua out of memory error."; |
| 8354 | goto error; |
| 8355 | } |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 8356 | fcn = new_hlua_function(); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 8357 | if (!fcn) { |
| 8358 | errmsg = "Lua out of memory error."; |
| 8359 | goto error; |
| 8360 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8361 | |
| 8362 | /* Fill path. */ |
| 8363 | index = 0; |
| 8364 | lua_pushnil(L); |
| 8365 | while(lua_next(L, 1) != 0) { |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 8366 | if (index >= 5) { |
| 8367 | errmsg = "1st argument must be a table with a maximum of 5 entries"; |
| 8368 | goto error; |
| 8369 | } |
| 8370 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 8371 | errmsg = "1st argument must be a table filled with strings"; |
| 8372 | goto error; |
| 8373 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8374 | cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1)); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 8375 | if (!cli_kws->kw[0].str_kw[index]) { |
| 8376 | errmsg = "Lua out of memory error."; |
| 8377 | goto error; |
| 8378 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8379 | index++; |
| 8380 | lua_pop(L, 1); |
| 8381 | } |
| 8382 | |
| 8383 | /* Copy help message. */ |
| 8384 | cli_kws->kw[0].usage = strdup(message); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 8385 | if (!cli_kws->kw[0].usage) { |
| 8386 | errmsg = "Lua out of memory error."; |
| 8387 | goto error; |
| 8388 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8389 | |
| 8390 | /* Fill fcn io handler. */ |
| 8391 | len = strlen("<lua.cli>") + 1; |
| 8392 | for (i = 0; i < index; i++) |
| 8393 | len += strlen(cli_kws->kw[0].str_kw[i]) + 1; |
| 8394 | fcn->name = calloc(1, len); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 8395 | if (!fcn->name) { |
| 8396 | errmsg = "Lua out of memory error."; |
| 8397 | goto error; |
| 8398 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8399 | strncat((char *)fcn->name, "<lua.cli", len); |
| 8400 | for (i = 0; i < index; i++) { |
| 8401 | strncat((char *)fcn->name, ".", len); |
| 8402 | strncat((char *)fcn->name, cli_kws->kw[0].str_kw[i], len); |
| 8403 | } |
| 8404 | strncat((char *)fcn->name, ">", len); |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8405 | fcn->function_ref[hlua_state_id] = ref_io; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8406 | |
| 8407 | /* Fill last entries. */ |
| 8408 | cli_kws->kw[0].private = fcn; |
| 8409 | cli_kws->kw[0].parse = hlua_cli_parse_fct; |
| 8410 | cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct; |
| 8411 | cli_kws->kw[0].io_release = hlua_cli_io_release_fct; |
| 8412 | |
| 8413 | /* Register this new converter */ |
| 8414 | cli_register_kw(cli_kws); |
| 8415 | |
| 8416 | return 0; |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 8417 | |
| 8418 | error: |
| 8419 | release_hlua_function(fcn); |
| 8420 | if (cli_kws) { |
| 8421 | for (i = 0; i < index; i++) |
| 8422 | ha_free((char **)&(cli_kws->kw[0].str_kw[i])); |
| 8423 | ha_free((char **)&(cli_kws->kw[0].usage)); |
| 8424 | } |
| 8425 | ha_free(&cli_kws); |
| 8426 | WILL_LJMP(luaL_error(L, errmsg)); |
| 8427 | return 0; /* Never reached */ |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8428 | } |
| 8429 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8430 | 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] | 8431 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8432 | char **err, unsigned int *timeout) |
| 8433 | { |
| 8434 | const char *error; |
| 8435 | |
| 8436 | error = parse_time_err(args[1], timeout, TIME_UNIT_MS); |
Willy Tarreau | 9faebe3 | 2019-06-07 19:00:37 +0200 | [diff] [blame] | 8437 | if (error == PARSE_TIME_OVER) { |
| 8438 | memprintf(err, "timer overflow in argument <%s> to <%s> (maximum value is 2147483647 ms or ~24.8 days)", |
| 8439 | args[1], args[0]); |
| 8440 | return -1; |
| 8441 | } |
| 8442 | else if (error == PARSE_TIME_UNDER) { |
| 8443 | memprintf(err, "timer underflow in argument <%s> to <%s> (minimum non-null value is 1 ms)", |
| 8444 | args[1], args[0]); |
| 8445 | return -1; |
| 8446 | } |
| 8447 | else if (error) { |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8448 | memprintf(err, "%s: invalid timeout", args[0]); |
| 8449 | return -1; |
| 8450 | } |
| 8451 | return 0; |
| 8452 | } |
| 8453 | |
| 8454 | 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] | 8455 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8456 | char **err) |
| 8457 | { |
| 8458 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 8459 | file, line, err, &hlua_timeout_session); |
| 8460 | } |
| 8461 | |
| 8462 | 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] | 8463 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8464 | char **err) |
| 8465 | { |
| 8466 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 8467 | file, line, err, &hlua_timeout_task); |
| 8468 | } |
| 8469 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8470 | 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] | 8471 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8472 | char **err) |
| 8473 | { |
| 8474 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 8475 | file, line, err, &hlua_timeout_applet); |
| 8476 | } |
| 8477 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 8478 | 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] | 8479 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 8480 | char **err) |
| 8481 | { |
| 8482 | char *error; |
| 8483 | |
| 8484 | hlua_nb_instruction = strtoll(args[1], &error, 10); |
| 8485 | if (*error != '\0') { |
| 8486 | memprintf(err, "%s: invalid number", args[0]); |
| 8487 | return -1; |
| 8488 | } |
| 8489 | return 0; |
| 8490 | } |
| 8491 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8492 | 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] | 8493 | const struct proxy *defpx, const char *file, int line, |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8494 | char **err) |
| 8495 | { |
| 8496 | char *error; |
| 8497 | |
| 8498 | if (*(args[1]) == 0) { |
| 8499 | memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]); |
| 8500 | return -1; |
| 8501 | } |
| 8502 | hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L; |
| 8503 | if (*error != '\0') { |
| 8504 | memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error); |
| 8505 | return -1; |
| 8506 | } |
| 8507 | return 0; |
| 8508 | } |
| 8509 | |
| 8510 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8511 | /* This function is called by the main configuration key "lua-load". It loads and |
| 8512 | * execute an lua file during the parsing of the HAProxy configuration file. It is |
| 8513 | * the main lua entry point. |
| 8514 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 8515 | * This function runs with the HAProxy keywords API. It returns -1 if an error |
| 8516 | * occurs, otherwise it returns 0. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8517 | * |
| 8518 | * In some error case, LUA set an error message in top of the stack. This function |
| 8519 | * 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] | 8520 | * |
| 8521 | * This function can fail with an abort() due to an Lua critical error. |
| 8522 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 8523 | * tolerated. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8524 | */ |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8525 | static int hlua_load_state(char *filename, lua_State *L, char **err) |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8526 | { |
| 8527 | int error; |
| 8528 | |
| 8529 | /* Just load and compile the file. */ |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8530 | error = luaL_loadfile(L, filename); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8531 | if (error) { |
Aurelien DARRAGON | a374161 | 2024-03-01 15:55:17 +0100 | [diff] [blame] | 8532 | 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] | 8533 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8534 | return -1; |
| 8535 | } |
| 8536 | |
| 8537 | /* If no syntax error where detected, execute the code. */ |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8538 | error = lua_pcall(L, 0, LUA_MULTRET, 0); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8539 | switch (error) { |
| 8540 | case LUA_OK: |
| 8541 | break; |
| 8542 | case LUA_ERRRUN: |
Aurelien DARRAGON | a374161 | 2024-03-01 15:55:17 +0100 | [diff] [blame] | 8543 | memprintf(err, "Lua runtime error: %s\n", hlua_tostring_safe(L, -1)); |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8544 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8545 | return -1; |
| 8546 | case LUA_ERRMEM: |
Thierry Fournier | de6145f | 2020-11-29 00:55:53 +0100 | [diff] [blame] | 8547 | memprintf(err, "Lua out of memory error\n"); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8548 | return -1; |
| 8549 | case LUA_ERRERR: |
Aurelien DARRAGON | a374161 | 2024-03-01 15:55:17 +0100 | [diff] [blame] | 8550 | 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] | 8551 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8552 | return -1; |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 8553 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM <= 503 |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8554 | case LUA_ERRGCMM: |
Aurelien DARRAGON | a374161 | 2024-03-01 15:55:17 +0100 | [diff] [blame] | 8555 | 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] | 8556 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8557 | return -1; |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 8558 | #endif |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8559 | default: |
Aurelien DARRAGON | a374161 | 2024-03-01 15:55:17 +0100 | [diff] [blame] | 8560 | memprintf(err, "Lua unknown error: %s\n", hlua_tostring_safe(L, -1)); |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8561 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8562 | return -1; |
| 8563 | } |
| 8564 | |
| 8565 | return 0; |
| 8566 | } |
| 8567 | |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8568 | static int hlua_load(char **args, int section_type, struct proxy *curpx, |
Willy Tarreau | 0182516 | 2021-03-09 09:53:46 +0100 | [diff] [blame] | 8569 | const struct proxy *defpx, const char *file, int line, |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8570 | char **err) |
| 8571 | { |
| 8572 | if (*(args[1]) == 0) { |
| 8573 | memprintf(err, "'%s' expects a file name as parameter.\n", args[0]); |
| 8574 | return -1; |
| 8575 | } |
| 8576 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8577 | /* loading for global state */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8578 | hlua_state_id = 0; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8579 | ha_set_tid(0); |
Thierry Fournier | afc63e2 | 2020-11-28 17:06:51 +0100 | [diff] [blame] | 8580 | return hlua_load_state(args[1], hlua_states[0], err); |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8581 | } |
| 8582 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8583 | 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] | 8584 | const struct proxy *defpx, const char *file, int line, |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8585 | char **err) |
| 8586 | { |
| 8587 | int len; |
| 8588 | |
| 8589 | if (*(args[1]) == 0) { |
| 8590 | memprintf(err, "'%s' expects a file as parameter.\n", args[0]); |
| 8591 | return -1; |
| 8592 | } |
| 8593 | |
| 8594 | if (per_thread_load == NULL) { |
| 8595 | /* allocate the first entry large enough to store the final NULL */ |
| 8596 | per_thread_load = calloc(1, sizeof(*per_thread_load)); |
| 8597 | if (per_thread_load == NULL) { |
| 8598 | memprintf(err, "out of memory error"); |
| 8599 | return -1; |
| 8600 | } |
| 8601 | } |
| 8602 | |
| 8603 | /* count used entries */ |
| 8604 | for (len = 0; per_thread_load[len] != NULL; len++) |
| 8605 | ; |
| 8606 | |
| 8607 | per_thread_load = realloc(per_thread_load, (len + 2) * sizeof(*per_thread_load)); |
| 8608 | if (per_thread_load == NULL) { |
| 8609 | memprintf(err, "out of memory error"); |
| 8610 | return -1; |
| 8611 | } |
| 8612 | |
| 8613 | per_thread_load[len] = strdup(args[1]); |
| 8614 | per_thread_load[len + 1] = NULL; |
| 8615 | |
| 8616 | if (per_thread_load[len] == NULL) { |
| 8617 | memprintf(err, "out of memory error"); |
| 8618 | return -1; |
| 8619 | } |
| 8620 | |
| 8621 | /* loading for thread 1 only */ |
| 8622 | hlua_state_id = 1; |
| 8623 | ha_set_tid(0); |
| 8624 | return hlua_load_state(args[1], hlua_states[1], err); |
| 8625 | } |
| 8626 | |
Tim Duesterhus | c9fc9f2 | 2020-01-12 13:55:39 +0100 | [diff] [blame] | 8627 | /* Prepend the given <path> followed by a semicolon to the `package.<type>` variable |
| 8628 | * in the given <ctx>. |
| 8629 | */ |
Thierry Fournier | 3fb9e51 | 2020-11-28 10:13:12 +0100 | [diff] [blame] | 8630 | static int hlua_prepend_path(lua_State *L, char *type, char *path) |
Tim Duesterhus | c9fc9f2 | 2020-01-12 13:55:39 +0100 | [diff] [blame] | 8631 | { |
Thierry Fournier | 3fb9e51 | 2020-11-28 10:13:12 +0100 | [diff] [blame] | 8632 | lua_getglobal(L, "package"); /* push package variable */ |
| 8633 | lua_pushstring(L, path); /* push given path */ |
| 8634 | lua_pushstring(L, ";"); /* push semicolon */ |
| 8635 | lua_getfield(L, -3, type); /* push old path */ |
| 8636 | lua_concat(L, 3); /* concatenate to new path */ |
| 8637 | lua_setfield(L, -2, type); /* store new path */ |
| 8638 | lua_pop(L, 1); /* pop package variable */ |
Tim Duesterhus | c9fc9f2 | 2020-01-12 13:55:39 +0100 | [diff] [blame] | 8639 | |
| 8640 | return 0; |
| 8641 | } |
| 8642 | |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8643 | 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] | 8644 | const struct proxy *defpx, const char *file, int line, |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8645 | char **err) |
| 8646 | { |
| 8647 | char *path; |
| 8648 | char *type = "path"; |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 8649 | struct prepend_path *p = NULL; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8650 | |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8651 | if (too_many_args(2, args, err, NULL)) { |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 8652 | goto err; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8653 | } |
| 8654 | |
| 8655 | if (!(*args[1])) { |
| 8656 | memprintf(err, "'%s' expects to receive a <path> as argument", args[0]); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 8657 | goto err; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8658 | } |
| 8659 | path = args[1]; |
| 8660 | |
| 8661 | if (*args[2]) { |
| 8662 | if (strcmp(args[2], "path") != 0 && strcmp(args[2], "cpath") != 0) { |
| 8663 | 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] | 8664 | goto err; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8665 | } |
| 8666 | type = args[2]; |
| 8667 | } |
| 8668 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8669 | p = calloc(1, sizeof(*p)); |
| 8670 | if (p == NULL) { |
Tim Duesterhus | f89d43a | 2021-01-03 20:04:37 +0100 | [diff] [blame] | 8671 | memprintf(err, "memory allocation failed"); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 8672 | goto err; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8673 | } |
| 8674 | p->path = strdup(path); |
| 8675 | if (p->path == NULL) { |
Tim Duesterhus | f89d43a | 2021-01-03 20:04:37 +0100 | [diff] [blame] | 8676 | memprintf(err, "memory allocation failed"); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 8677 | goto err2; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8678 | } |
| 8679 | p->type = strdup(type); |
| 8680 | if (p->type == NULL) { |
Tim Duesterhus | f89d43a | 2021-01-03 20:04:37 +0100 | [diff] [blame] | 8681 | memprintf(err, "memory allocation failed"); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 8682 | goto err2; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8683 | } |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 8684 | LIST_APPEND(&prepend_path_list, &p->l); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8685 | |
| 8686 | hlua_prepend_path(hlua_states[0], type, path); |
| 8687 | hlua_prepend_path(hlua_states[1], type, path); |
| 8688 | return 0; |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 8689 | |
| 8690 | err2: |
| 8691 | free(p->type); |
| 8692 | free(p->path); |
| 8693 | err: |
| 8694 | free(p); |
| 8695 | return -1; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8696 | } |
| 8697 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8698 | /* configuration keywords declaration */ |
| 8699 | static struct cfg_kw_list cfg_kws = {{ },{ |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8700 | { CFG_GLOBAL, "lua-prepend-path", hlua_config_prepend_path }, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8701 | { CFG_GLOBAL, "lua-load", hlua_load }, |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8702 | { CFG_GLOBAL, "lua-load-per-thread", hlua_load_per_thread }, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8703 | { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout }, |
| 8704 | { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout }, |
Thierry FOURNIER | 56da101 | 2015-10-01 08:42:31 +0200 | [diff] [blame] | 8705 | { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout }, |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 8706 | { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield }, |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8707 | { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem }, |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8708 | { 0, NULL, NULL }, |
| 8709 | }}; |
| 8710 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 8711 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |
| 8712 | |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 8713 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8714 | /* This function can fail with an abort() due to an Lua critical error. |
| 8715 | * We are in the initialisation process of HAProxy, this abort() is |
| 8716 | * tolerated. |
| 8717 | */ |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8718 | int hlua_post_init_state(lua_State *L) |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8719 | { |
| 8720 | struct hlua_init_function *init; |
| 8721 | const char *msg; |
| 8722 | enum hlua_exec ret; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8723 | const char *error; |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8724 | const char *kind; |
| 8725 | const char *trace; |
| 8726 | int return_status = 1; |
| 8727 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
| 8728 | int nres; |
| 8729 | #endif |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8730 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 8731 | /* disable memory limit checks if limit is not set */ |
| 8732 | if (!hlua_global_allocator.limit) |
| 8733 | hlua_global_allocator.limit = ~hlua_global_allocator.limit; |
| 8734 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 8735 | /* Call post initialisation function in safe environment. */ |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 8736 | if (setjmp(safe_ljmp_env) != 0) { |
| 8737 | lua_atpanic(L, hlua_panic_safe); |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8738 | if (lua_type(L, -1) == LUA_TSTRING) |
Aurelien DARRAGON | a374161 | 2024-03-01 15:55:17 +0100 | [diff] [blame] | 8739 | error = hlua_tostring_safe(L, -1); |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 8740 | else |
| 8741 | error = "critical error"; |
| 8742 | fprintf(stderr, "Lua post-init: %s.\n", error); |
| 8743 | exit(1); |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 8744 | } else { |
| 8745 | lua_atpanic(L, hlua_panic_ljmp); |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 8746 | } |
Frédéric Lécaille | 54f2bcf | 2018-08-29 13:46:24 +0200 | [diff] [blame] | 8747 | |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8748 | hlua_fcn_post_init(L); |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 8749 | |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8750 | list_for_each_entry(init, &hlua_init_functions[hlua_state_id], l) { |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8751 | lua_rawgeti(L, LUA_REGISTRYINDEX, init->function_ref); |
Aurelien DARRAGON | 93591b4 | 2023-03-20 16:29:55 +0100 | [diff] [blame] | 8752 | /* function ref should be released right away since it was pushed |
| 8753 | * on the stack and will not be used anymore |
| 8754 | */ |
| 8755 | hlua_unref(L, init->function_ref); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8756 | |
| 8757 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
Aurelien DARRAGON | 2dec950 | 2023-09-08 19:29:08 +0200 | [diff] [blame] | 8758 | ret = lua_resume(L, NULL, 0, &nres); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8759 | #else |
Aurelien DARRAGON | 2dec950 | 2023-09-08 19:29:08 +0200 | [diff] [blame] | 8760 | ret = lua_resume(L, NULL, 0); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8761 | #endif |
| 8762 | kind = NULL; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8763 | switch (ret) { |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8764 | |
| 8765 | case LUA_OK: |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8766 | lua_pop(L, -1); |
Thierry Fournier | 13d08b7 | 2020-11-28 11:02:58 +0100 | [diff] [blame] | 8767 | break; |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8768 | |
| 8769 | case LUA_ERRERR: |
| 8770 | kind = "message handler error"; |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 8771 | /* Fall through */ |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8772 | case LUA_ERRRUN: |
| 8773 | if (!kind) |
| 8774 | kind = "runtime error"; |
Aurelien DARRAGON | a374161 | 2024-03-01 15:55:17 +0100 | [diff] [blame] | 8775 | msg = hlua_tostring_safe(L, -1); |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 8776 | trace = hlua_traceback(L, ", "); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8777 | if (msg) |
| 8778 | ha_alert("Lua init: %s: '%s' from %s\n", kind, msg, trace); |
| 8779 | else |
| 8780 | ha_alert("Lua init: unknown %s from %s\n", kind, trace); |
Aurelien DARRAGON | adac932 | 2024-03-01 19:54:16 +0100 | [diff] [blame] | 8781 | |
| 8782 | lua_settop(L, 0); /* Empty the stack. */ |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8783 | return_status = 0; |
| 8784 | break; |
| 8785 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8786 | default: |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8787 | /* Unknown error */ |
| 8788 | kind = "Unknown error"; |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 8789 | /* Fall through */ |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8790 | case LUA_YIELD: |
| 8791 | /* yield is not configured at this step, this state doesn't happen */ |
| 8792 | if (!kind) |
| 8793 | kind = "yield not allowed"; |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 8794 | /* Fall through */ |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8795 | case LUA_ERRMEM: |
| 8796 | if (!kind) |
| 8797 | kind = "out of memory error"; |
Aurelien DARRAGON | 93f5d8d | 2023-08-09 10:11:49 +0200 | [diff] [blame] | 8798 | lua_settop(L, 0); /* Empty the stack. */ |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 8799 | trace = hlua_traceback(L, ", "); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8800 | ha_alert("Lua init: %s: %s\n", kind, trace); |
| 8801 | return_status = 0; |
| 8802 | break; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8803 | } |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8804 | if (!return_status) |
| 8805 | break; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8806 | } |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 8807 | |
| 8808 | lua_atpanic(L, hlua_panic_safe); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8809 | return return_status; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8810 | } |
| 8811 | |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8812 | int hlua_post_init() |
| 8813 | { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8814 | int ret; |
| 8815 | int i; |
| 8816 | int errors; |
| 8817 | char *err = NULL; |
| 8818 | struct hlua_function *fcn; |
| 8819 | |
Willy Tarreau | 42afc59 | 2021-08-30 09:35:18 +0200 | [diff] [blame] | 8820 | #if defined(USE_OPENSSL) |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8821 | /* Initialize SSL server. */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 8822 | if (socket_ssl->xprt->prepare_srv) { |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8823 | int saved_used_backed = global.ssl_used_backend; |
| 8824 | // don't affect maxconn automatic computation |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 8825 | socket_ssl->xprt->prepare_srv(socket_ssl); |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8826 | global.ssl_used_backend = saved_used_backed; |
| 8827 | } |
| 8828 | #endif |
| 8829 | |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8830 | /* Perform post init of common thread */ |
| 8831 | hlua_state_id = 0; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8832 | ha_set_tid(0); |
| 8833 | ret = hlua_post_init_state(hlua_states[hlua_state_id]); |
| 8834 | if (ret == 0) |
| 8835 | return 0; |
| 8836 | |
| 8837 | /* init remaining lua states and load files */ |
| 8838 | for (hlua_state_id = 2; hlua_state_id < global.nbthread + 1; hlua_state_id++) { |
| 8839 | |
| 8840 | /* set thread context */ |
| 8841 | ha_set_tid(hlua_state_id - 1); |
| 8842 | |
| 8843 | /* Init lua state */ |
| 8844 | hlua_states[hlua_state_id] = hlua_init_state(hlua_state_id); |
| 8845 | |
| 8846 | /* Load lua files */ |
| 8847 | for (i = 0; per_thread_load && per_thread_load[i]; i++) { |
| 8848 | ret = hlua_load_state(per_thread_load[i], hlua_states[hlua_state_id], &err); |
| 8849 | if (ret != 0) { |
| 8850 | ha_alert("Lua init: %s\n", err); |
| 8851 | return 0; |
| 8852 | } |
| 8853 | } |
| 8854 | } |
| 8855 | |
| 8856 | /* Reset thread context */ |
| 8857 | ha_set_tid(0); |
| 8858 | |
| 8859 | /* Execute post init for all states */ |
| 8860 | for (hlua_state_id = 1; hlua_state_id < global.nbthread + 1; hlua_state_id++) { |
| 8861 | |
| 8862 | /* set thread context */ |
| 8863 | ha_set_tid(hlua_state_id - 1); |
| 8864 | |
| 8865 | /* run post init */ |
| 8866 | ret = hlua_post_init_state(hlua_states[hlua_state_id]); |
| 8867 | if (ret == 0) |
| 8868 | return 0; |
| 8869 | } |
| 8870 | |
| 8871 | /* Reset thread context */ |
| 8872 | ha_set_tid(0); |
| 8873 | |
| 8874 | /* control functions registering. Each function must have: |
| 8875 | * - only the function_ref[0] set positive and all other to -1 |
| 8876 | * - only the function_ref[0] set to -1 and all other positive |
| 8877 | * This ensure a same reference is not used both in shared |
| 8878 | * lua state and thread dedicated lua state. Note: is the case |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 8879 | * reach, the shared state is priority, but the bug will be |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8880 | * complicated to found for the end user. |
| 8881 | */ |
| 8882 | errors = 0; |
| 8883 | list_for_each_entry(fcn, &referenced_functions, l) { |
| 8884 | ret = 0; |
| 8885 | for (i = 1; i < global.nbthread + 1; i++) { |
| 8886 | if (fcn->function_ref[i] == -1) |
| 8887 | ret--; |
| 8888 | else |
| 8889 | ret++; |
| 8890 | } |
| 8891 | if (abs(ret) != global.nbthread) { |
| 8892 | ha_alert("Lua function '%s' is not referenced in all thread. " |
| 8893 | "Expect function in all thread or in none thread.\n", fcn->name); |
| 8894 | errors++; |
| 8895 | continue; |
| 8896 | } |
| 8897 | |
| 8898 | if ((fcn->function_ref[0] == -1) == (ret < 0)) { |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 8899 | ha_alert("Lua function '%s' is referenced both ins shared Lua context (through lua-load) " |
| 8900 | "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] | 8901 | "exclusive.\n", fcn->name); |
| 8902 | errors++; |
| 8903 | } |
| 8904 | } |
| 8905 | |
| 8906 | if (errors > 0) |
| 8907 | return 0; |
| 8908 | |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 8909 | /* 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] | 8910 | * -1 in order to have probably a segfault if someone use it |
| 8911 | */ |
| 8912 | hlua_state_id = -1; |
| 8913 | |
| 8914 | return 1; |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8915 | } |
| 8916 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8917 | /* The memory allocator used by the Lua stack. <ud> is a pointer to the |
| 8918 | * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize> |
| 8919 | * 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] | 8920 | * allocation. <nsize> is the requested new size. A new allocation is |
| 8921 | * indicated by <ptr> being NULL. A free is indicated by <nsize> being |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 8922 | * zero. This one verifies that the limits are respected but is optimized |
| 8923 | * 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] | 8924 | * |
| 8925 | * Warning: while this API ressembles glibc's realloc() a lot, glibc surpasses |
| 8926 | * POSIX by making realloc(ptr,0) an effective free(), but others do not do |
| 8927 | * that and will simply allocate zero as if it were the result of malloc(0), |
| 8928 | * so mapping this onto realloc() will lead to memory leaks on non-glibc |
| 8929 | * systems. |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8930 | */ |
| 8931 | static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize) |
| 8932 | { |
| 8933 | struct hlua_mem_allocator *zone = ud; |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 8934 | size_t limit, old, new; |
| 8935 | |
Tim Duesterhus | 2258652 | 2021-01-08 10:35:33 +0100 | [diff] [blame] | 8936 | if (unlikely(!ptr && !nsize)) |
| 8937 | return NULL; |
| 8938 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 8939 | /* a limit of ~0 means unlimited and boot complete, so there's no need |
| 8940 | * for accounting anymore. |
| 8941 | */ |
Willy Tarreau | e00ad05 | 2021-10-22 16:00:02 +0200 | [diff] [blame] | 8942 | if (likely(~zone->limit == 0)) { |
| 8943 | if (!nsize) |
| 8944 | ha_free(&ptr); |
| 8945 | else |
| 8946 | ptr = realloc(ptr, nsize); |
| 8947 | return ptr; |
| 8948 | } |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8949 | |
Willy Tarreau | d36c7fa | 2020-12-02 12:26:29 +0100 | [diff] [blame] | 8950 | if (!ptr) |
| 8951 | osize = 0; |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8952 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 8953 | /* enforce strict limits across all threads */ |
| 8954 | limit = zone->limit; |
| 8955 | old = _HA_ATOMIC_LOAD(&zone->allocated); |
| 8956 | do { |
| 8957 | new = old + nsize - osize; |
| 8958 | if (unlikely(nsize && limit && new > limit)) |
| 8959 | return NULL; |
| 8960 | } while (!_HA_ATOMIC_CAS(&zone->allocated, &old, new)); |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8961 | |
Willy Tarreau | e00ad05 | 2021-10-22 16:00:02 +0200 | [diff] [blame] | 8962 | if (!nsize) |
| 8963 | ha_free(&ptr); |
| 8964 | else |
| 8965 | ptr = realloc(ptr, nsize); |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 8966 | |
| 8967 | if (unlikely(!ptr && nsize)) // failed |
| 8968 | _HA_ATOMIC_SUB(&zone->allocated, nsize - osize); |
| 8969 | |
| 8970 | __ha_barrier_atomic_store(); |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8971 | return ptr; |
| 8972 | } |
| 8973 | |
Thierry Fournier | ecb83c2 | 2020-11-28 15:49:44 +0100 | [diff] [blame] | 8974 | /* 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] | 8975 | * We are in the initialisation process of HAProxy, this abort() is |
| 8976 | * tolerated. |
| 8977 | */ |
Thierry Fournier | ecb83c2 | 2020-11-28 15:49:44 +0100 | [diff] [blame] | 8978 | lua_State *hlua_init_state(int thread_num) |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 8979 | { |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8980 | int i; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8981 | int idx; |
| 8982 | struct sample_fetch *sf; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8983 | struct sample_conv *sc; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8984 | char *p; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8985 | const char *error_msg; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 8986 | void **context; |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8987 | lua_State *L; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8988 | struct prepend_path *pp; |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8989 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 8990 | /* Init main lua stack. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8991 | L = lua_newstate(hlua_alloc, &hlua_global_allocator); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 8992 | |
firexinghe | bdd336c | 2023-07-13 11:03:34 +0800 | [diff] [blame] | 8993 | if (!L) { |
| 8994 | fprintf(stderr, |
| 8995 | "Lua init: critical error: lua_newstate() returned NULL." |
| 8996 | " This may possibly be caused by a memory allocation error.\n"); |
| 8997 | exit(1); |
| 8998 | } |
| 8999 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 9000 | /* Initialise Lua context to NULL */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9001 | context = lua_getextraspace(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 9002 | *context = NULL; |
| 9003 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 9004 | /* From this point, until the end of the initialisation function, |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 9005 | * the Lua function can fail with an abort. We are in the initialisation |
| 9006 | * process of HAProxy, this abort() is tolerated. |
| 9007 | */ |
| 9008 | |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 9009 | /* Call post initialisation function in safe environment. */ |
| 9010 | if (setjmp(safe_ljmp_env) != 0) { |
| 9011 | lua_atpanic(L, hlua_panic_safe); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9012 | if (lua_type(L, -1) == LUA_TSTRING) |
Aurelien DARRAGON | a374161 | 2024-03-01 15:55:17 +0100 | [diff] [blame] | 9013 | error_msg = hlua_tostring_safe(L, -1); |
Thierry Fournier | 2f05cc6 | 2020-11-28 16:08:02 +0100 | [diff] [blame] | 9014 | else |
| 9015 | error_msg = "critical error"; |
| 9016 | fprintf(stderr, "Lua init: %s.\n", error_msg); |
| 9017 | exit(1); |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 9018 | } else { |
| 9019 | lua_atpanic(L, hlua_panic_ljmp); |
Thierry Fournier | 2f05cc6 | 2020-11-28 16:08:02 +0100 | [diff] [blame] | 9020 | } |
| 9021 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 9022 | /* Initialise lua. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9023 | luaL_openlibs(L); |
Tim Duesterhus | 541fe1e | 2020-01-12 13:55:41 +0100 | [diff] [blame] | 9024 | #define HLUA_PREPEND_PATH_TOSTRING1(x) #x |
| 9025 | #define HLUA_PREPEND_PATH_TOSTRING(x) HLUA_PREPEND_PATH_TOSTRING1(x) |
| 9026 | #ifdef HLUA_PREPEND_PATH |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9027 | hlua_prepend_path(L, "path", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_PATH)); |
Tim Duesterhus | 541fe1e | 2020-01-12 13:55:41 +0100 | [diff] [blame] | 9028 | #endif |
| 9029 | #ifdef HLUA_PREPEND_CPATH |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9030 | hlua_prepend_path(L, "cpath", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_CPATH)); |
Tim Duesterhus | 541fe1e | 2020-01-12 13:55:41 +0100 | [diff] [blame] | 9031 | #endif |
| 9032 | #undef HLUA_PREPEND_PATH_TOSTRING |
| 9033 | #undef HLUA_PREPEND_PATH_TOSTRING1 |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 9034 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 9035 | /* Apply configured prepend path */ |
| 9036 | list_for_each_entry(pp, &prepend_path_list, l) |
| 9037 | hlua_prepend_path(L, pp->type, pp->path); |
| 9038 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 9039 | /* |
| 9040 | * |
| 9041 | * Create "core" object. |
| 9042 | * |
| 9043 | */ |
| 9044 | |
Thierry FOURNIER | a2d8c65 | 2015-03-11 17:29:39 +0100 | [diff] [blame] | 9045 | /* This table entry is the object "core" base. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9046 | lua_newtable(L); |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 9047 | |
Thierry Fournier | ecb83c2 | 2020-11-28 15:49:44 +0100 | [diff] [blame] | 9048 | /* set the thread id */ |
| 9049 | hlua_class_const_int(L, "thread", thread_num); |
| 9050 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 9051 | /* Push the loglevel constants. */ |
Christopher Faulet | 8263e94 | 2024-02-29 15:41:17 +0100 | [diff] [blame] | 9052 | hlua_class_const_int(L, "silent", -1); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 9053 | for (i = 0; i < NB_LOG_LEVELS; i++) |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9054 | hlua_class_const_int(L, log_levels[i], i); |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 9055 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 9056 | /* Register special functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9057 | hlua_class_function(L, "register_init", hlua_register_init); |
| 9058 | hlua_class_function(L, "register_task", hlua_register_task); |
| 9059 | hlua_class_function(L, "register_fetches", hlua_register_fetches); |
| 9060 | hlua_class_function(L, "register_converters", hlua_register_converters); |
| 9061 | hlua_class_function(L, "register_action", hlua_register_action); |
| 9062 | hlua_class_function(L, "register_service", hlua_register_service); |
| 9063 | hlua_class_function(L, "register_cli", hlua_register_cli); |
| 9064 | hlua_class_function(L, "yield", hlua_yield); |
| 9065 | hlua_class_function(L, "set_nice", hlua_set_nice); |
| 9066 | hlua_class_function(L, "sleep", hlua_sleep); |
| 9067 | hlua_class_function(L, "msleep", hlua_msleep); |
| 9068 | hlua_class_function(L, "add_acl", hlua_add_acl); |
| 9069 | hlua_class_function(L, "del_acl", hlua_del_acl); |
| 9070 | hlua_class_function(L, "set_map", hlua_set_map); |
| 9071 | hlua_class_function(L, "del_map", hlua_del_map); |
| 9072 | hlua_class_function(L, "tcp", hlua_socket_new); |
| 9073 | hlua_class_function(L, "log", hlua_log); |
| 9074 | hlua_class_function(L, "Debug", hlua_log_debug); |
| 9075 | hlua_class_function(L, "Info", hlua_log_info); |
| 9076 | hlua_class_function(L, "Warning", hlua_log_warning); |
| 9077 | hlua_class_function(L, "Alert", hlua_log_alert); |
| 9078 | hlua_class_function(L, "done", hlua_done); |
| 9079 | hlua_fcn_reg_core_fcn(L); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 9080 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9081 | lua_setglobal(L, "core"); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 9082 | |
| 9083 | /* |
| 9084 | * |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 9085 | * Create "act" object. |
| 9086 | * |
| 9087 | */ |
| 9088 | |
| 9089 | /* This table entry is the object "act" base. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9090 | lua_newtable(L); |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 9091 | |
| 9092 | /* push action return constants */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9093 | hlua_class_const_int(L, "CONTINUE", ACT_RET_CONT); |
| 9094 | hlua_class_const_int(L, "STOP", ACT_RET_STOP); |
| 9095 | hlua_class_const_int(L, "YIELD", ACT_RET_YIELD); |
| 9096 | hlua_class_const_int(L, "ERROR", ACT_RET_ERR); |
| 9097 | hlua_class_const_int(L, "DONE", ACT_RET_DONE); |
| 9098 | hlua_class_const_int(L, "DENY", ACT_RET_DENY); |
| 9099 | hlua_class_const_int(L, "ABORT", ACT_RET_ABRT); |
| 9100 | hlua_class_const_int(L, "INVALID", ACT_RET_INV); |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 9101 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9102 | hlua_class_function(L, "wake_time", hlua_set_wake_time); |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 9103 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9104 | lua_setglobal(L, "act"); |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 9105 | |
| 9106 | /* |
| 9107 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 9108 | * Register class Map |
| 9109 | * |
| 9110 | */ |
| 9111 | |
| 9112 | /* This table entry is the object "Map" base. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9113 | lua_newtable(L); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 9114 | |
| 9115 | /* register pattern types. */ |
| 9116 | for (i=0; i<PAT_MATCH_NUM; i++) |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9117 | hlua_class_const_int(L, pat_match_names[i], i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 9118 | for (i=0; i<PAT_MATCH_NUM; i++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 9119 | snprintf(trash.area, trash.size, "_%s", pat_match_names[i]); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9120 | hlua_class_const_int(L, trash.area, i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 9121 | } |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 9122 | |
| 9123 | /* register constructor. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9124 | hlua_class_function(L, "new", hlua_map_new); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 9125 | |
| 9126 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9127 | lua_newtable(L); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 9128 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 9129 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9130 | lua_pushstring(L, "__index"); |
| 9131 | lua_newtable(L); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 9132 | |
| 9133 | /* Register . */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9134 | hlua_class_function(L, "lookup", hlua_map_lookup); |
| 9135 | hlua_class_function(L, "slookup", hlua_map_slookup); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 9136 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9137 | lua_rawset(L, -3); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 9138 | |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 9139 | /* Register previous table in the registry with reference and named entry. |
| 9140 | * The function hlua_register_metatable() pops the stack, so we |
| 9141 | * previously create a copy of the table. |
| 9142 | */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9143 | lua_pushvalue(L, -1); /* Copy the -1 entry and push it on the stack. */ |
| 9144 | class_map_ref = hlua_register_metatable(L, CLASS_MAP); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 9145 | |
| 9146 | /* Assign the metatable to the mai Map object. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9147 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 9148 | |
| 9149 | /* Set a name to the table. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9150 | lua_setglobal(L, "Map"); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 9151 | |
| 9152 | /* |
| 9153 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 9154 | * Register class Channel |
| 9155 | * |
| 9156 | */ |
| 9157 | |
| 9158 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9159 | lua_newtable(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 9160 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 9161 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9162 | lua_pushstring(L, "__index"); |
| 9163 | lua_newtable(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 9164 | |
| 9165 | /* Register . */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9166 | hlua_class_function(L, "get", hlua_channel_get); |
| 9167 | hlua_class_function(L, "dup", hlua_channel_dup); |
| 9168 | hlua_class_function(L, "getline", hlua_channel_getline); |
| 9169 | hlua_class_function(L, "set", hlua_channel_set); |
| 9170 | hlua_class_function(L, "append", hlua_channel_append); |
| 9171 | hlua_class_function(L, "send", hlua_channel_send); |
| 9172 | hlua_class_function(L, "forward", hlua_channel_forward); |
| 9173 | hlua_class_function(L, "get_in_len", hlua_channel_get_in_len); |
| 9174 | hlua_class_function(L, "get_out_len", hlua_channel_get_out_len); |
| 9175 | hlua_class_function(L, "is_full", hlua_channel_is_full); |
| 9176 | hlua_class_function(L, "is_resp", hlua_channel_is_resp); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 9177 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9178 | lua_rawset(L, -3); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 9179 | |
| 9180 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9181 | class_channel_ref = hlua_register_metatable(L, CLASS_CHANNEL); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 9182 | |
| 9183 | /* |
| 9184 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 9185 | * Register class Fetches |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 9186 | * |
| 9187 | */ |
| 9188 | |
| 9189 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9190 | lua_newtable(L); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 9191 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 9192 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9193 | lua_pushstring(L, "__index"); |
| 9194 | lua_newtable(L); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 9195 | |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 9196 | /* Browse existing fetches and create the associated |
| 9197 | * object method. |
| 9198 | */ |
| 9199 | sf = NULL; |
| 9200 | while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) { |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 9201 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 9202 | * by an underscore. |
| 9203 | */ |
Willy Tarreau | fa92f36 | 2021-08-26 16:48:53 +0200 | [diff] [blame] | 9204 | strlcpy2(trash.area, sf->kw, trash.size); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 9205 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 9206 | if (*p == '.' || *p == '-' || *p == '+') |
| 9207 | *p = '_'; |
| 9208 | |
| 9209 | /* Register the function. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9210 | lua_pushstring(L, trash.area); |
| 9211 | lua_pushlightuserdata(L, sf); |
| 9212 | lua_pushcclosure(L, hlua_run_sample_fetch, 1); |
| 9213 | lua_rawset(L, -3); |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 9214 | } |
| 9215 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9216 | lua_rawset(L, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 9217 | |
| 9218 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9219 | class_fetches_ref = hlua_register_metatable(L, CLASS_FETCHES); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 9220 | |
| 9221 | /* |
| 9222 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 9223 | * Register class Converters |
| 9224 | * |
| 9225 | */ |
| 9226 | |
| 9227 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9228 | lua_newtable(L); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 9229 | |
| 9230 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9231 | lua_pushstring(L, "__index"); |
| 9232 | lua_newtable(L); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 9233 | |
| 9234 | /* Browse existing converters and create the associated |
| 9235 | * object method. |
| 9236 | */ |
| 9237 | sc = NULL; |
| 9238 | while ((sc = sample_conv_getnext(sc, &idx)) != NULL) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 9239 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 9240 | * by an underscore. |
| 9241 | */ |
Willy Tarreau | fa92f36 | 2021-08-26 16:48:53 +0200 | [diff] [blame] | 9242 | strlcpy2(trash.area, sc->kw, trash.size); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 9243 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 9244 | if (*p == '.' || *p == '-' || *p == '+') |
| 9245 | *p = '_'; |
| 9246 | |
| 9247 | /* Register the function. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9248 | lua_pushstring(L, trash.area); |
| 9249 | lua_pushlightuserdata(L, sc); |
| 9250 | lua_pushcclosure(L, hlua_run_sample_conv, 1); |
| 9251 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 9252 | } |
| 9253 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9254 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 9255 | |
| 9256 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9257 | class_converters_ref = hlua_register_metatable(L, CLASS_CONVERTERS); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 9258 | |
| 9259 | /* |
| 9260 | * |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 9261 | * Register class HTTP |
| 9262 | * |
| 9263 | */ |
| 9264 | |
| 9265 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9266 | lua_newtable(L); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 9267 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 9268 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9269 | lua_pushstring(L, "__index"); |
| 9270 | lua_newtable(L); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 9271 | |
| 9272 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9273 | hlua_class_function(L, "req_get_headers",hlua_http_req_get_headers); |
| 9274 | hlua_class_function(L, "req_del_header", hlua_http_req_del_hdr); |
| 9275 | hlua_class_function(L, "req_rep_header", hlua_http_req_rep_hdr); |
| 9276 | hlua_class_function(L, "req_rep_value", hlua_http_req_rep_val); |
| 9277 | hlua_class_function(L, "req_add_header", hlua_http_req_add_hdr); |
| 9278 | hlua_class_function(L, "req_set_header", hlua_http_req_set_hdr); |
| 9279 | hlua_class_function(L, "req_set_method", hlua_http_req_set_meth); |
| 9280 | hlua_class_function(L, "req_set_path", hlua_http_req_set_path); |
| 9281 | hlua_class_function(L, "req_set_query", hlua_http_req_set_query); |
| 9282 | hlua_class_function(L, "req_set_uri", hlua_http_req_set_uri); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 9283 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9284 | hlua_class_function(L, "res_get_headers",hlua_http_res_get_headers); |
| 9285 | hlua_class_function(L, "res_del_header", hlua_http_res_del_hdr); |
| 9286 | hlua_class_function(L, "res_rep_header", hlua_http_res_rep_hdr); |
| 9287 | hlua_class_function(L, "res_rep_value", hlua_http_res_rep_val); |
| 9288 | hlua_class_function(L, "res_add_header", hlua_http_res_add_hdr); |
| 9289 | hlua_class_function(L, "res_set_header", hlua_http_res_set_hdr); |
| 9290 | hlua_class_function(L, "res_set_status", hlua_http_res_set_status); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 9291 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9292 | lua_rawset(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 9293 | |
| 9294 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9295 | class_http_ref = hlua_register_metatable(L, CLASS_HTTP); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 9296 | |
| 9297 | /* |
| 9298 | * |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9299 | * Register class AppletTCP |
| 9300 | * |
| 9301 | */ |
| 9302 | |
| 9303 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9304 | lua_newtable(L); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9305 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 9306 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9307 | lua_pushstring(L, "__index"); |
| 9308 | lua_newtable(L); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9309 | |
| 9310 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9311 | hlua_class_function(L, "getline", hlua_applet_tcp_getline); |
| 9312 | hlua_class_function(L, "receive", hlua_applet_tcp_recv); |
| 9313 | hlua_class_function(L, "send", hlua_applet_tcp_send); |
| 9314 | hlua_class_function(L, "set_priv", hlua_applet_tcp_set_priv); |
| 9315 | hlua_class_function(L, "get_priv", hlua_applet_tcp_get_priv); |
| 9316 | hlua_class_function(L, "set_var", hlua_applet_tcp_set_var); |
| 9317 | hlua_class_function(L, "unset_var", hlua_applet_tcp_unset_var); |
| 9318 | hlua_class_function(L, "get_var", hlua_applet_tcp_get_var); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9319 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9320 | lua_settable(L, -3); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9321 | |
| 9322 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9323 | class_applet_tcp_ref = hlua_register_metatable(L, CLASS_APPLET_TCP); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9324 | |
| 9325 | /* |
| 9326 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9327 | * Register class AppletHTTP |
| 9328 | * |
| 9329 | */ |
| 9330 | |
| 9331 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9332 | lua_newtable(L); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9333 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 9334 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9335 | lua_pushstring(L, "__index"); |
| 9336 | lua_newtable(L); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9337 | |
| 9338 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9339 | hlua_class_function(L, "set_priv", hlua_applet_http_set_priv); |
| 9340 | hlua_class_function(L, "get_priv", hlua_applet_http_get_priv); |
| 9341 | hlua_class_function(L, "set_var", hlua_applet_http_set_var); |
| 9342 | hlua_class_function(L, "unset_var", hlua_applet_http_unset_var); |
| 9343 | hlua_class_function(L, "get_var", hlua_applet_http_get_var); |
| 9344 | hlua_class_function(L, "getline", hlua_applet_http_getline); |
| 9345 | hlua_class_function(L, "receive", hlua_applet_http_recv); |
| 9346 | hlua_class_function(L, "send", hlua_applet_http_send); |
| 9347 | hlua_class_function(L, "add_header", hlua_applet_http_addheader); |
| 9348 | hlua_class_function(L, "set_status", hlua_applet_http_status); |
| 9349 | hlua_class_function(L, "start_response", hlua_applet_http_start_response); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9350 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9351 | lua_settable(L, -3); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9352 | |
| 9353 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9354 | class_applet_http_ref = hlua_register_metatable(L, CLASS_APPLET_HTTP); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9355 | |
| 9356 | /* |
| 9357 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 9358 | * Register class TXN |
| 9359 | * |
| 9360 | */ |
| 9361 | |
| 9362 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9363 | lua_newtable(L); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 9364 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 9365 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9366 | lua_pushstring(L, "__index"); |
| 9367 | lua_newtable(L); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 9368 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 9369 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9370 | hlua_class_function(L, "set_priv", hlua_set_priv); |
| 9371 | hlua_class_function(L, "get_priv", hlua_get_priv); |
| 9372 | hlua_class_function(L, "set_var", hlua_set_var); |
| 9373 | hlua_class_function(L, "unset_var", hlua_unset_var); |
| 9374 | hlua_class_function(L, "get_var", hlua_get_var); |
| 9375 | hlua_class_function(L, "done", hlua_txn_done); |
| 9376 | hlua_class_function(L, "reply", hlua_txn_reply_new); |
| 9377 | hlua_class_function(L, "set_loglevel", hlua_txn_set_loglevel); |
| 9378 | hlua_class_function(L, "set_tos", hlua_txn_set_tos); |
| 9379 | hlua_class_function(L, "set_mark", hlua_txn_set_mark); |
| 9380 | hlua_class_function(L, "set_priority_class", hlua_txn_set_priority_class); |
| 9381 | hlua_class_function(L, "set_priority_offset", hlua_txn_set_priority_offset); |
| 9382 | hlua_class_function(L, "deflog", hlua_txn_deflog); |
| 9383 | hlua_class_function(L, "log", hlua_txn_log); |
| 9384 | hlua_class_function(L, "Debug", hlua_txn_log_debug); |
| 9385 | hlua_class_function(L, "Info", hlua_txn_log_info); |
| 9386 | hlua_class_function(L, "Warning", hlua_txn_log_warning); |
| 9387 | hlua_class_function(L, "Alert", hlua_txn_log_alert); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 9388 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9389 | lua_rawset(L, -3); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 9390 | |
| 9391 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9392 | class_txn_ref = hlua_register_metatable(L, CLASS_TXN); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9393 | |
| 9394 | /* |
| 9395 | * |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 9396 | * Register class reply |
| 9397 | * |
| 9398 | */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9399 | lua_newtable(L); |
| 9400 | lua_pushstring(L, "__index"); |
| 9401 | lua_newtable(L); |
| 9402 | hlua_class_function(L, "set_status", hlua_txn_reply_set_status); |
| 9403 | hlua_class_function(L, "add_header", hlua_txn_reply_add_header); |
| 9404 | hlua_class_function(L, "del_header", hlua_txn_reply_del_header); |
| 9405 | hlua_class_function(L, "set_body", hlua_txn_reply_set_body); |
| 9406 | lua_settable(L, -3); /* Sets the __index entry. */ |
| 9407 | class_txn_reply_ref = luaL_ref(L, LUA_REGISTRYINDEX); |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 9408 | |
| 9409 | |
| 9410 | /* |
| 9411 | * |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9412 | * Register class Socket |
| 9413 | * |
| 9414 | */ |
| 9415 | |
| 9416 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9417 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9418 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 9419 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9420 | lua_pushstring(L, "__index"); |
| 9421 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9422 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 9423 | #ifdef USE_OPENSSL |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9424 | hlua_class_function(L, "connect_ssl", hlua_socket_connect_ssl); |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 9425 | #endif |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9426 | hlua_class_function(L, "connect", hlua_socket_connect); |
| 9427 | hlua_class_function(L, "send", hlua_socket_send); |
| 9428 | hlua_class_function(L, "receive", hlua_socket_receive); |
| 9429 | hlua_class_function(L, "close", hlua_socket_close); |
| 9430 | hlua_class_function(L, "getpeername", hlua_socket_getpeername); |
| 9431 | hlua_class_function(L, "getsockname", hlua_socket_getsockname); |
| 9432 | hlua_class_function(L, "setoption", hlua_socket_setoption); |
| 9433 | hlua_class_function(L, "settimeout", hlua_socket_settimeout); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9434 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9435 | 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] | 9436 | |
| 9437 | /* Register the garbage collector entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9438 | lua_pushstring(L, "__gc"); |
| 9439 | lua_pushcclosure(L, hlua_socket_gc, 0); |
| 9440 | 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] | 9441 | |
| 9442 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9443 | class_socket_ref = hlua_register_metatable(L, CLASS_SOCKET); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9444 | |
Thierry Fournier | aafc777 | 2020-12-04 11:47:47 +0100 | [diff] [blame] | 9445 | lua_atpanic(L, hlua_panic_safe); |
| 9446 | |
| 9447 | return L; |
| 9448 | } |
| 9449 | |
| 9450 | void hlua_init(void) { |
| 9451 | int i; |
Amaury Denoyelle | 239fdbf | 2021-03-24 10:22:03 +0100 | [diff] [blame] | 9452 | char *errmsg; |
Thierry Fournier | aafc777 | 2020-12-04 11:47:47 +0100 | [diff] [blame] | 9453 | #ifdef USE_OPENSSL |
| 9454 | struct srv_kw *kw; |
| 9455 | int tmp_error; |
| 9456 | char *error; |
| 9457 | char *args[] = { /* SSL client configuration. */ |
| 9458 | "ssl", |
| 9459 | "verify", |
| 9460 | "none", |
| 9461 | NULL |
| 9462 | }; |
| 9463 | #endif |
| 9464 | |
| 9465 | /* Init post init function list head */ |
| 9466 | for (i = 0; i < MAX_THREADS + 1; i++) |
| 9467 | LIST_INIT(&hlua_init_functions[i]); |
| 9468 | |
| 9469 | /* Init state for common/shared lua parts */ |
| 9470 | hlua_state_id = 0; |
| 9471 | ha_set_tid(0); |
| 9472 | hlua_states[0] = hlua_init_state(0); |
| 9473 | |
| 9474 | /* Init state 1 for thread 0. We have at least one thread. */ |
| 9475 | hlua_state_id = 1; |
| 9476 | ha_set_tid(0); |
| 9477 | hlua_states[1] = hlua_init_state(1); |
| 9478 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9479 | /* Proxy and server configuration initialisation. */ |
Amaury Denoyelle | 239fdbf | 2021-03-24 10:22:03 +0100 | [diff] [blame] | 9480 | socket_proxy = alloc_new_proxy("LUA-SOCKET", PR_CAP_FE|PR_CAP_BE|PR_CAP_LUA, &errmsg); |
| 9481 | if (!socket_proxy) { |
| 9482 | fprintf(stderr, "Lua init: %s\n", errmsg); |
| 9483 | exit(1); |
| 9484 | } |
| 9485 | proxy_preset_defaults(socket_proxy); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9486 | |
| 9487 | /* Init TCP server: unchanged parameters */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 9488 | socket_tcp = new_server(socket_proxy); |
| 9489 | if (!socket_tcp) { |
| 9490 | fprintf(stderr, "Lua init: failed to allocate tcp server socket\n"); |
| 9491 | exit(1); |
| 9492 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9493 | |
| 9494 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9495 | /* Init TCP server: unchanged parameters */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 9496 | socket_ssl = new_server(socket_proxy); |
| 9497 | if (!socket_ssl) { |
| 9498 | fprintf(stderr, "Lua init: failed to allocate ssl server socket\n"); |
| 9499 | exit(1); |
| 9500 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9501 | |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 9502 | socket_ssl->use_ssl = 1; |
| 9503 | socket_ssl->xprt = xprt_get(XPRT_SSL); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9504 | |
Bertrand Jacquin | 80839ff | 2021-01-21 19:14:46 +0000 | [diff] [blame] | 9505 | for (i = 0; args[i] != NULL; i++) { |
| 9506 | 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] | 9507 | /* |
| 9508 | * |
| 9509 | * If the keyword is not known, we can search in the registered |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 9510 | * server keywords. This is useful to configure special SSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9511 | * features like client certificates and ssl_verify. |
| 9512 | * |
| 9513 | */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 9514 | tmp_error = kw->parse(args, &i, socket_proxy, socket_ssl, &error); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9515 | if (tmp_error != 0) { |
| 9516 | fprintf(stderr, "INTERNAL ERROR: %s\n", error); |
| 9517 | abort(); /* This must be never arrives because the command line |
| 9518 | not editable by the user. */ |
| 9519 | } |
Bertrand Jacquin | 80839ff | 2021-01-21 19:14:46 +0000 | [diff] [blame] | 9520 | i += kw->skip; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9521 | } |
| 9522 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9523 | #endif |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 9524 | |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 9525 | } |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 9526 | |
Tim Duesterhus | d0c0ca2 | 2020-07-04 11:53:26 +0200 | [diff] [blame] | 9527 | static void hlua_deinit() |
| 9528 | { |
Willy Tarreau | 186f376 | 2020-12-04 11:48:12 +0100 | [diff] [blame] | 9529 | int thr; |
| 9530 | |
| 9531 | for (thr = 0; thr < MAX_THREADS+1; thr++) { |
| 9532 | if (hlua_states[thr]) |
| 9533 | lua_close(hlua_states[thr]); |
| 9534 | } |
Willy Tarreau | 430bf4a | 2021-03-04 09:45:32 +0100 | [diff] [blame] | 9535 | |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 9536 | free_server(socket_tcp); |
Willy Tarreau | 430bf4a | 2021-03-04 09:45:32 +0100 | [diff] [blame] | 9537 | |
Willy Tarreau | 0f143af | 2021-03-05 10:41:48 +0100 | [diff] [blame] | 9538 | #ifdef USE_OPENSSL |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 9539 | free_server(socket_ssl); |
Willy Tarreau | 0f143af | 2021-03-05 10:41:48 +0100 | [diff] [blame] | 9540 | #endif |
Amaury Denoyelle | 239fdbf | 2021-03-24 10:22:03 +0100 | [diff] [blame] | 9541 | |
| 9542 | free_proxy(socket_proxy); |
Tim Duesterhus | d0c0ca2 | 2020-07-04 11:53:26 +0200 | [diff] [blame] | 9543 | } |
| 9544 | |
| 9545 | REGISTER_POST_DEINIT(hlua_deinit); |
| 9546 | |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 9547 | static void hlua_register_build_options(void) |
| 9548 | { |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 9549 | char *ptr = NULL; |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 9550 | |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 9551 | memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE); |
| 9552 | hap_register_build_opts(ptr, 1); |
| 9553 | } |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 9554 | |
| 9555 | INITCALL0(STG_REGISTER, hlua_register_build_options); |