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 | |
Aurelien DARRAGON | 159136c | 2024-06-03 23:18:24 +0200 | [diff] [blame^] | 241 | /* below is an helper function similar to lua_pushvfstring() to push a |
| 242 | * formatted string on Lua stack but in a safe way (function may not LJMP). |
| 243 | * It can be useful to push allocated strings (ie: error messages) on the |
| 244 | * stack and ensure proper cleanup. |
| 245 | * |
| 246 | * Returns a pointer to the internal copy of the string on success and NULL |
| 247 | * on error. |
| 248 | * |
| 249 | * It is assumed that the calling function is allowed to manipulate <L> |
| 250 | */ |
| 251 | __LJMP static int _hlua_pushvfstring_safe(lua_State *L) |
| 252 | { |
| 253 | const char **dst = lua_touserdata(L, 1); |
| 254 | const char *fmt = lua_touserdata(L, 2); |
| 255 | va_list *argp = lua_touserdata(L, 3); |
| 256 | |
| 257 | *dst = lua_pushvfstring(L, fmt, *argp); |
| 258 | return 1; |
| 259 | } |
| 260 | static const char *hlua_pushvfstring_safe(lua_State *L, const char *fmt, va_list argp) |
| 261 | { |
| 262 | const char *dst = NULL; |
| 263 | va_list cpy_argp; /* required if argp is implemented as array type */ |
| 264 | |
| 265 | if (!lua_checkstack(L, 4)) |
| 266 | return NULL; |
| 267 | |
| 268 | va_copy(cpy_argp, argp); |
| 269 | |
| 270 | /* push our custom _hlua_pushvfstring_safe() function on the stack, then |
| 271 | * push our destination string pointer, fmt and arg list |
| 272 | */ |
| 273 | lua_pushcfunction(L, _hlua_pushvfstring_safe); |
| 274 | lua_pushlightuserdata(L, &dst); // 1st func argument = dst string pointer |
| 275 | lua_pushlightuserdata(L, (void *)fmt); // 2nd func argument = fmt |
| 276 | lua_pushlightuserdata(L, &cpy_argp); // 3rd func argument = arg list |
| 277 | |
| 278 | /* call our custom function with proper arguments using pcall() to catch |
| 279 | * exceptions (if any) |
| 280 | */ |
| 281 | switch (lua_pcall(L, 3, 1, 0)) { |
| 282 | case LUA_OK: |
| 283 | break; |
| 284 | default: |
| 285 | /* error was caught */ |
| 286 | dst = NULL; |
| 287 | } |
| 288 | va_end(cpy_argp); |
| 289 | |
| 290 | return dst; |
| 291 | } |
| 292 | |
| 293 | static const char *hlua_pushfstring_safe(lua_State *L, const char *fmt, ...) |
| 294 | { |
| 295 | va_list argp; |
| 296 | const char *dst; |
| 297 | |
| 298 | va_start(argp, fmt); |
| 299 | dst = hlua_pushvfstring_safe(L, fmt, argp); |
| 300 | va_end(argp); |
| 301 | |
| 302 | return dst; |
| 303 | } |
| 304 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 305 | #define SET_SAFE_LJMP_L(__L, __HLUA) \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 306 | ({ \ |
| 307 | int ret; \ |
Aurelien DARRAGON | e00bf5d | 2023-03-21 13:22:33 +0100 | [diff] [blame] | 308 | hlua_lock(__HLUA); \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 309 | if (setjmp(safe_ljmp_env) != 0) { \ |
| 310 | lua_atpanic(__L, hlua_panic_safe); \ |
| 311 | ret = 0; \ |
Aurelien DARRAGON | e00bf5d | 2023-03-21 13:22:33 +0100 | [diff] [blame] | 312 | hlua_unlock(__HLUA); \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 313 | } else { \ |
| 314 | lua_atpanic(__L, hlua_panic_ljmp); \ |
| 315 | ret = 1; \ |
| 316 | } \ |
| 317 | ret; \ |
| 318 | }) |
| 319 | |
| 320 | /* If we are the last function catching Lua errors, we |
| 321 | * must reset the panic function. |
| 322 | */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 323 | #define RESET_SAFE_LJMP_L(__L, __HLUA) \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 324 | do { \ |
| 325 | lua_atpanic(__L, hlua_panic_safe); \ |
Aurelien DARRAGON | e00bf5d | 2023-03-21 13:22:33 +0100 | [diff] [blame] | 326 | hlua_unlock(__HLUA); \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 327 | } while(0) |
| 328 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 329 | #define SET_SAFE_LJMP(__HLUA) \ |
| 330 | SET_SAFE_LJMP_L((__HLUA)->T, __HLUA) |
| 331 | |
| 332 | #define RESET_SAFE_LJMP(__HLUA) \ |
| 333 | RESET_SAFE_LJMP_L((__HLUA)->T, __HLUA) |
| 334 | |
| 335 | #define SET_SAFE_LJMP_PARENT(__HLUA) \ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 336 | SET_SAFE_LJMP_L(hlua_states[(__HLUA)->state_id], __HLUA) |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 337 | |
| 338 | #define RESET_SAFE_LJMP_PARENT(__HLUA) \ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 339 | RESET_SAFE_LJMP_L(hlua_states[(__HLUA)->state_id], __HLUA) |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 340 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 341 | /* Applet status flags */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 342 | #define APPLET_DONE 0x01 /* applet processing is done. */ |
Christopher Faulet | 18c2e8d | 2019-03-01 12:02:08 +0100 | [diff] [blame] | 343 | /* unused: 0x02 */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 344 | #define APPLET_HDR_SENT 0x04 /* Response header sent. */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 345 | /* unused: 0x08, 0x10 */ |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 346 | #define APPLET_HTTP11 0x20 /* Last chunk sent. */ |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 347 | #define APPLET_RSP_SENT 0x40 /* The response was fully sent */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 348 | |
Thierry Fournier | afc63e2 | 2020-11-28 17:06:51 +0100 | [diff] [blame] | 349 | /* The main Lua execution context. The 0 index is the |
| 350 | * common state shared by all threads. |
| 351 | */ |
Willy Tarreau | 186f376 | 2020-12-04 11:48:12 +0100 | [diff] [blame] | 352 | static lua_State *hlua_states[MAX_THREADS + 1]; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 353 | |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 354 | /* This is the memory pool containing struct lua for applets |
| 355 | * (including cli). |
| 356 | */ |
Willy Tarreau | 8ceae72 | 2018-11-26 11:58:30 +0100 | [diff] [blame] | 357 | DECLARE_STATIC_POOL(pool_head_hlua, "hlua", sizeof(struct hlua)); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 358 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 359 | /* Used for Socket connection. */ |
Amaury Denoyelle | 239fdbf | 2021-03-24 10:22:03 +0100 | [diff] [blame] | 360 | static struct proxy *socket_proxy; |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 361 | static struct server *socket_tcp; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 362 | #ifdef USE_OPENSSL |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 363 | static struct server *socket_ssl; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 364 | #endif |
| 365 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 366 | /* List head of the function called at the initialisation time. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 367 | struct list hlua_init_functions[MAX_THREADS + 1]; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 368 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 369 | /* The following variables contains the reference of the different |
| 370 | * Lua classes. These references are useful for identify metadata |
| 371 | * associated with an object. |
| 372 | */ |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 373 | static int class_txn_ref; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 374 | static int class_socket_ref; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 375 | static int class_channel_ref; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 376 | static int class_fetches_ref; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 377 | static int class_converters_ref; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 378 | static int class_http_ref; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 379 | static int class_map_ref; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 380 | static int class_applet_tcp_ref; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 381 | static int class_applet_http_ref; |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 382 | static int class_txn_reply_ref; |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 383 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 384 | /* Global Lua execution timeout. By default Lua, execution linked |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 385 | * with stream (actions, sample-fetches and converters) have a |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 386 | * short timeout. Lua linked with tasks doesn't have a timeout |
| 387 | * because a task may remain alive during all the haproxy execution. |
| 388 | */ |
| 389 | static unsigned int hlua_timeout_session = 4000; /* session timeout. */ |
| 390 | static unsigned int hlua_timeout_task = TICK_ETERNITY; /* task timeout. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 391 | static unsigned int hlua_timeout_applet = 4000; /* applet timeout. */ |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 392 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 393 | /* Interrupts the Lua processing each "hlua_nb_instruction" instructions. |
| 394 | * it is used for preventing infinite loops. |
| 395 | * |
| 396 | * I test the scheer with an infinite loop containing one incrementation |
| 397 | * and one test. I run this loop between 10 seconds, I raise a ceil of |
| 398 | * 710M loops from one interrupt each 9000 instructions, so I fix the value |
| 399 | * to one interrupt each 10 000 instructions. |
| 400 | * |
| 401 | * configured | Number of |
| 402 | * instructions | loops executed |
| 403 | * between two | in milions |
| 404 | * forced yields | |
| 405 | * ---------------+--------------- |
| 406 | * 10 | 160 |
| 407 | * 500 | 670 |
| 408 | * 1000 | 680 |
| 409 | * 5000 | 700 |
| 410 | * 7000 | 700 |
| 411 | * 8000 | 700 |
| 412 | * 9000 | 710 <- ceil |
| 413 | * 10000 | 710 |
| 414 | * 100000 | 710 |
| 415 | * 1000000 | 710 |
| 416 | * |
| 417 | */ |
| 418 | static unsigned int hlua_nb_instruction = 10000; |
| 419 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 420 | /* Descriptor for the memory allocation state. The limit is pre-initialised to |
| 421 | * 0 until it is replaced by "tune.lua.maxmem" during the config parsing, or it |
| 422 | * is replaced with ~0 during post_init after everything was loaded. This way |
| 423 | * it is guaranteed that if limit is ~0 the boot is complete and that if it's |
| 424 | * zero it's not yet limited and proper accounting is required. |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 425 | */ |
| 426 | struct hlua_mem_allocator { |
| 427 | size_t allocated; |
| 428 | size_t limit; |
| 429 | }; |
| 430 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 431 | static struct hlua_mem_allocator hlua_global_allocator THREAD_ALIGNED(64); |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 432 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 433 | /* These functions converts types between HAProxy internal args or |
| 434 | * sample and LUA types. Another function permits to check if the |
| 435 | * LUA stack contains arguments according with an required ARG_T |
| 436 | * format. |
| 437 | */ |
| 438 | static int hlua_arg2lua(lua_State *L, const struct arg *arg); |
| 439 | static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 440 | __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] | 441 | uint64_t mask, struct proxy *p); |
Willy Tarreau | 5eadada | 2015-03-10 17:28:54 +0100 | [diff] [blame] | 442 | static int hlua_smp2lua(lua_State *L, struct sample *smp); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 443 | static int hlua_smp2lua_str(lua_State *L, struct sample *smp); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 444 | static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp); |
| 445 | |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 446 | __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] | 447 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 448 | struct prepend_path { |
| 449 | struct list l; |
| 450 | char *type; |
| 451 | char *path; |
| 452 | }; |
| 453 | |
| 454 | static struct list prepend_path_list = LIST_HEAD_INIT(prepend_path_list); |
| 455 | |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 456 | #define SEND_ERR(__be, __fmt, __args...) \ |
| 457 | do { \ |
| 458 | send_log(__be, LOG_ERR, __fmt, ## __args); \ |
| 459 | if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) \ |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 460 | ha_alert(__fmt, ## __args); \ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 461 | } while (0) |
| 462 | |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 463 | static inline struct hlua_function *new_hlua_function() |
| 464 | { |
| 465 | struct hlua_function *fcn; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 466 | int i; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 467 | |
| 468 | fcn = calloc(1, sizeof(*fcn)); |
| 469 | if (!fcn) |
| 470 | return NULL; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 471 | LIST_APPEND(&referenced_functions, &fcn->l); |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 472 | for (i = 0; i < MAX_THREADS + 1; i++) |
| 473 | fcn->function_ref[i] = -1; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 474 | return fcn; |
| 475 | } |
| 476 | |
Christopher Faulet | dda4444 | 2021-04-12 14:05:43 +0200 | [diff] [blame] | 477 | static inline void release_hlua_function(struct hlua_function *fcn) |
| 478 | { |
| 479 | if (!fcn) |
| 480 | return; |
| 481 | if (fcn->name) |
| 482 | ha_free(&fcn->name); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 483 | LIST_DELETE(&fcn->l); |
Christopher Faulet | dda4444 | 2021-04-12 14:05:43 +0200 | [diff] [blame] | 484 | ha_free(&fcn); |
| 485 | } |
| 486 | |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 487 | /* If the common state is set, the stack id is 0, otherwise it is the tid + 1 */ |
| 488 | static inline int fcn_ref_to_stack_id(struct hlua_function *fcn) |
| 489 | { |
| 490 | if (fcn->function_ref[0] == -1) |
| 491 | return tid + 1; |
| 492 | return 0; |
| 493 | } |
| 494 | |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 495 | /* Used to check an Lua function type in the stack. It creates and |
| 496 | * returns a reference of the function. This function throws an |
Aurelien DARRAGON | 5ce1152 | 2023-03-02 17:50:49 +0100 | [diff] [blame] | 497 | * error if the argument is not a "function". |
| 498 | * When no longer used, the ref must be released with hlua_unref() |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 499 | */ |
| 500 | __LJMP unsigned int hlua_checkfunction(lua_State *L, int argno) |
| 501 | { |
| 502 | if (!lua_isfunction(L, argno)) { |
Thierry FOURNIER | fd1e955 | 2018-02-23 18:41:18 +0100 | [diff] [blame] | 503 | 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] | 504 | WILL_LJMP(luaL_argerror(L, argno, msg)); |
| 505 | } |
| 506 | lua_pushvalue(L, argno); |
| 507 | return luaL_ref(L, LUA_REGISTRYINDEX); |
| 508 | } |
| 509 | |
Christopher Faulet | ddc9032 | 2020-02-25 10:20:04 +0100 | [diff] [blame] | 510 | /* Used to check an Lua table type in the stack. It creates and |
| 511 | * returns a reference of the table. This function throws an |
Aurelien DARRAGON | 5ce1152 | 2023-03-02 17:50:49 +0100 | [diff] [blame] | 512 | * error if the argument is not a "table". |
| 513 | * When no longer used, the ref must be released with hlua_unref() |
Christopher Faulet | ddc9032 | 2020-02-25 10:20:04 +0100 | [diff] [blame] | 514 | */ |
| 515 | __LJMP unsigned int hlua_checktable(lua_State *L, int argno) |
| 516 | { |
| 517 | if (!lua_istable(L, argno)) { |
| 518 | const char *msg = lua_pushfstring(L, "table expected, got %s", luaL_typename(L, argno)); |
| 519 | WILL_LJMP(luaL_argerror(L, argno, msg)); |
| 520 | } |
| 521 | lua_pushvalue(L, argno); |
| 522 | return luaL_ref(L, LUA_REGISTRYINDEX); |
| 523 | } |
| 524 | |
Aurelien DARRAGON | 5ce1152 | 2023-03-02 17:50:49 +0100 | [diff] [blame] | 525 | /* Get a reference to the object that is at the top of the stack |
| 526 | * The referenced object will be popped from the stack |
| 527 | * |
| 528 | * The function returns the reference to the object which must |
| 529 | * be cleared using hlua_unref() when no longer used |
| 530 | */ |
| 531 | __LJMP int hlua_ref(lua_State *L) |
| 532 | { |
| 533 | return MAY_LJMP(luaL_ref(L, LUA_REGISTRYINDEX)); |
| 534 | } |
| 535 | |
| 536 | /* Pushes a reference previously created using luaL_ref(L, LUA_REGISTRYINDEX) |
| 537 | * on <L> stack |
| 538 | * (ie: hlua_checkfunction(), hlua_checktable() or hlua_ref()) |
| 539 | * |
| 540 | * When the reference is no longer used, it should be released by calling |
| 541 | * hlua_unref() |
| 542 | * |
| 543 | * <L> can be from any co-routine as long as it belongs to the same lua |
| 544 | * parent state that the one used to get the reference. |
| 545 | */ |
| 546 | void hlua_pushref(lua_State *L, int ref) |
| 547 | { |
| 548 | lua_rawgeti(L, LUA_REGISTRYINDEX, ref); |
| 549 | } |
| 550 | |
| 551 | /* Releases a reference previously created using luaL_ref(L, LUA_REGISTRYINDEX) |
| 552 | * (ie: hlua_checkfunction(), hlua_checktable() or hlua_ref()) |
| 553 | * |
| 554 | * This will allow the reference to be reused and the referred object |
| 555 | * to be garbage collected. |
| 556 | * |
| 557 | * <L> can be from any co-routine as long as it belongs to the same lua |
| 558 | * parent state that the one used to get the reference. |
| 559 | */ |
| 560 | void hlua_unref(lua_State *L, int ref) |
| 561 | { |
| 562 | luaL_unref(L, LUA_REGISTRYINDEX, ref); |
| 563 | } |
| 564 | |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 565 | __LJMP const char *hlua_traceback(lua_State *L, const char* sep) |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 566 | { |
| 567 | lua_Debug ar; |
| 568 | int level = 0; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 569 | struct buffer *msg = get_trash_chunk(); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 570 | |
| 571 | while (lua_getstack(L, level++, &ar)) { |
| 572 | |
| 573 | /* Add separator */ |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 574 | if (b_data(msg)) |
| 575 | chunk_appendf(msg, "%s", sep); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 576 | |
| 577 | /* Fill fields: |
| 578 | * 'S': fills in the fields source, short_src, linedefined, lastlinedefined, and what; |
| 579 | * 'l': fills in the field currentline; |
| 580 | * 'n': fills in the field name and namewhat; |
| 581 | * 't': fills in the field istailcall; |
| 582 | */ |
| 583 | lua_getinfo(L, "Slnt", &ar); |
| 584 | |
| 585 | /* Append code localisation */ |
| 586 | if (ar.currentline > 0) |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 587 | chunk_appendf(msg, "%s:%d: ", ar.short_src, ar.currentline); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 588 | else |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 589 | chunk_appendf(msg, "%s: ", ar.short_src); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 590 | |
| 591 | /* |
| 592 | * Get function name |
| 593 | * |
| 594 | * if namewhat is no empty, name is defined. |
| 595 | * what contains "Lua" for Lua function, "C" for C function, |
| 596 | * or "main" for main code. |
| 597 | */ |
| 598 | 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] | 599 | chunk_appendf(msg, "in %s '%s'", ar.namewhat, ar.name); /* use it */ |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 600 | |
| 601 | 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] | 602 | chunk_appendf(msg, "in main chunk"); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 603 | |
| 604 | else if (*ar.what != 'C') /* for Lua functions, use <file:line> */ |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 605 | chunk_appendf(msg, "in function line %d", ar.linedefined); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 606 | |
| 607 | else /* nothing left... */ |
| 608 | chunk_appendf(msg, "?"); |
| 609 | |
| 610 | |
| 611 | /* Display tailed call */ |
| 612 | if (ar.istailcall) |
| 613 | chunk_appendf(msg, " ..."); |
| 614 | } |
| 615 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 616 | return msg->area; |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 617 | } |
| 618 | |
| 619 | |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 620 | /* This function check the number of arguments available in the |
| 621 | * stack. If the number of arguments available is not the same |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 622 | * then <nb> an error is thrown. |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 623 | */ |
| 624 | __LJMP static inline void check_args(lua_State *L, int nb, char *fcn) |
| 625 | { |
| 626 | if (lua_gettop(L) == nb) |
| 627 | return; |
| 628 | WILL_LJMP(luaL_error(L, "'%s' needs %d arguments", fcn, nb)); |
| 629 | } |
| 630 | |
Mark Lakes | 22154b4 | 2018-01-29 14:38:40 -0800 | [diff] [blame] | 631 | /* This function pushes an error string prefixed by the file name |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 632 | * and the line number where the error is encountered. |
| 633 | */ |
| 634 | static int hlua_pusherror(lua_State *L, const char *fmt, ...) |
| 635 | { |
| 636 | va_list argp; |
| 637 | va_start(argp, fmt); |
| 638 | luaL_where(L, 1); |
| 639 | lua_pushvfstring(L, fmt, argp); |
| 640 | va_end(argp); |
| 641 | lua_concat(L, 2); |
| 642 | return 1; |
| 643 | } |
| 644 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 645 | /* This functions is used with sample fetch and converters. It |
| 646 | * converts the HAProxy configuration argument in a lua stack |
| 647 | * values. |
| 648 | * |
| 649 | * It takes an array of "arg", and each entry of the array is |
| 650 | * converted and pushed in the LUA stack. |
| 651 | */ |
| 652 | static int hlua_arg2lua(lua_State *L, const struct arg *arg) |
| 653 | { |
| 654 | switch (arg->type) { |
| 655 | case ARGT_SINT: |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 656 | case ARGT_TIME: |
| 657 | case ARGT_SIZE: |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 658 | lua_pushinteger(L, arg->data.sint); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 659 | break; |
| 660 | |
| 661 | case ARGT_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 662 | lua_pushlstring(L, arg->data.str.area, arg->data.str.data); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 663 | break; |
| 664 | |
| 665 | case ARGT_IPV4: |
| 666 | case ARGT_IPV6: |
| 667 | case ARGT_MSK4: |
| 668 | case ARGT_MSK6: |
| 669 | case ARGT_FE: |
| 670 | case ARGT_BE: |
| 671 | case ARGT_TAB: |
| 672 | case ARGT_SRV: |
| 673 | case ARGT_USR: |
| 674 | case ARGT_MAP: |
| 675 | default: |
| 676 | lua_pushnil(L); |
| 677 | break; |
| 678 | } |
| 679 | return 1; |
| 680 | } |
| 681 | |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 682 | /* 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] | 683 | * 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] | 684 | * with sample fetch wrappers. The input arguments are given to the |
| 685 | * lua wrapper and converted as arg list by the function. |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 686 | */ |
| 687 | static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg) |
| 688 | { |
| 689 | switch (lua_type(L, ud)) { |
| 690 | |
| 691 | case LUA_TNUMBER: |
| 692 | case LUA_TBOOLEAN: |
| 693 | arg->type = ARGT_SINT; |
| 694 | arg->data.sint = lua_tointeger(L, ud); |
| 695 | break; |
| 696 | |
| 697 | case LUA_TSTRING: |
| 698 | arg->type = ARGT_STR; |
Tim Duesterhus | 2e89dec | 2019-09-29 23:03:08 +0200 | [diff] [blame] | 699 | 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] | 700 | /* 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] | 701 | 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] | 702 | arg->data.str.head = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 703 | break; |
| 704 | |
| 705 | case LUA_TUSERDATA: |
| 706 | case LUA_TNIL: |
| 707 | case LUA_TTABLE: |
| 708 | case LUA_TFUNCTION: |
| 709 | case LUA_TTHREAD: |
| 710 | case LUA_TLIGHTUSERDATA: |
| 711 | arg->type = ARGT_SINT; |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 712 | arg->data.sint = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 713 | break; |
| 714 | } |
| 715 | return 1; |
| 716 | } |
| 717 | |
| 718 | /* the following functions are used to convert a struct sample |
| 719 | * in Lua type. This useful to convert the return of the |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 720 | * fetches or converters. |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 721 | */ |
Willy Tarreau | 5eadada | 2015-03-10 17:28:54 +0100 | [diff] [blame] | 722 | static int hlua_smp2lua(lua_State *L, struct sample *smp) |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 723 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 724 | switch (smp->data.type) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 725 | case SMP_T_SINT: |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 726 | case SMP_T_BOOL: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 727 | lua_pushinteger(L, smp->data.u.sint); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 728 | break; |
| 729 | |
| 730 | case SMP_T_BIN: |
| 731 | case SMP_T_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 732 | 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] | 733 | break; |
| 734 | |
| 735 | case SMP_T_METH: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 736 | switch (smp->data.u.meth.meth) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 737 | case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break; |
| 738 | case HTTP_METH_GET: lua_pushstring(L, "GET"); break; |
| 739 | case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break; |
| 740 | case HTTP_METH_POST: lua_pushstring(L, "POST"); break; |
| 741 | case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break; |
| 742 | case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break; |
| 743 | case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break; |
| 744 | case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break; |
| 745 | case HTTP_METH_OTHER: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 746 | 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] | 747 | break; |
| 748 | default: |
| 749 | lua_pushnil(L); |
| 750 | break; |
| 751 | } |
| 752 | break; |
| 753 | |
| 754 | case SMP_T_IPV4: |
| 755 | case SMP_T_IPV6: |
| 756 | 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] | 757 | if (sample_casts[smp->data.type][SMP_T_STR] && |
| 758 | sample_casts[smp->data.type][SMP_T_STR](smp)) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 759 | 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] | 760 | else |
| 761 | lua_pushnil(L); |
| 762 | break; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 763 | default: |
| 764 | lua_pushnil(L); |
| 765 | break; |
| 766 | } |
| 767 | return 1; |
| 768 | } |
| 769 | |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 770 | /* the following functions are used to convert a struct sample |
| 771 | * in Lua strings. This is useful to convert the return of the |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 772 | * fetches or converters. |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 773 | */ |
| 774 | static int hlua_smp2lua_str(lua_State *L, struct sample *smp) |
| 775 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 776 | switch (smp->data.type) { |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 777 | |
| 778 | case SMP_T_BIN: |
| 779 | case SMP_T_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 780 | 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] | 781 | break; |
| 782 | |
| 783 | case SMP_T_METH: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 784 | switch (smp->data.u.meth.meth) { |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 785 | case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break; |
| 786 | case HTTP_METH_GET: lua_pushstring(L, "GET"); break; |
| 787 | case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break; |
| 788 | case HTTP_METH_POST: lua_pushstring(L, "POST"); break; |
| 789 | case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break; |
| 790 | case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break; |
| 791 | case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break; |
| 792 | case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break; |
| 793 | case HTTP_METH_OTHER: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 794 | 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] | 795 | break; |
| 796 | default: |
| 797 | lua_pushstring(L, ""); |
| 798 | break; |
| 799 | } |
| 800 | break; |
| 801 | |
| 802 | case SMP_T_SINT: |
| 803 | case SMP_T_BOOL: |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 804 | case SMP_T_IPV4: |
| 805 | case SMP_T_IPV6: |
| 806 | 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] | 807 | if (sample_casts[smp->data.type][SMP_T_STR] && |
| 808 | sample_casts[smp->data.type][SMP_T_STR](smp)) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 809 | 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] | 810 | else |
| 811 | lua_pushstring(L, ""); |
| 812 | break; |
| 813 | default: |
| 814 | lua_pushstring(L, ""); |
| 815 | break; |
| 816 | } |
| 817 | return 1; |
| 818 | } |
| 819 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 820 | /* the following functions are used to convert an Lua type in a |
| 821 | * struct sample. This is useful to provide data from a converter |
| 822 | * to the LUA code. |
| 823 | */ |
| 824 | static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp) |
| 825 | { |
| 826 | switch (lua_type(L, ud)) { |
| 827 | |
| 828 | case LUA_TNUMBER: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 829 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 830 | smp->data.u.sint = lua_tointeger(L, ud); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 831 | break; |
| 832 | |
| 833 | |
| 834 | case LUA_TBOOLEAN: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 835 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 836 | smp->data.u.sint = lua_toboolean(L, ud); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 837 | break; |
| 838 | |
| 839 | case LUA_TSTRING: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 840 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 841 | smp->flags |= SMP_F_CONST; |
Tim Duesterhus | 2e89dec | 2019-09-29 23:03:08 +0200 | [diff] [blame] | 842 | 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] | 843 | /* 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] | 844 | 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] | 845 | smp->data.u.str.head = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 846 | break; |
| 847 | |
| 848 | case LUA_TUSERDATA: |
| 849 | case LUA_TNIL: |
| 850 | case LUA_TTABLE: |
| 851 | case LUA_TFUNCTION: |
| 852 | case LUA_TTHREAD: |
| 853 | case LUA_TLIGHTUSERDATA: |
Thierry FOURNIER | 93405e1 | 2015-08-26 14:19:03 +0200 | [diff] [blame] | 854 | case LUA_TNONE: |
| 855 | default: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 856 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 857 | smp->data.u.sint = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 858 | break; |
| 859 | } |
| 860 | return 1; |
| 861 | } |
| 862 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 863 | /* This function check the "argp" built by another conversion function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 864 | * 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] | 865 | * 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] | 866 | * |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 867 | * This function assumes that the argp argument contains ARGM_NBARGS + 1 |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 868 | * entries. |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 869 | */ |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 870 | __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] | 871 | uint64_t mask, struct proxy *p) |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 872 | { |
| 873 | int min_arg; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 874 | int i, idx; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 875 | struct proxy *px; |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 876 | struct userlist *ul; |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 877 | struct my_regex *reg; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 878 | const char *msg = NULL; |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 879 | char *sname, *pname, *err = NULL; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 880 | |
| 881 | idx = 0; |
| 882 | min_arg = ARGM(mask); |
| 883 | mask >>= ARGM_BITS; |
| 884 | |
| 885 | while (1) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 886 | struct buffer tmp = BUF_NULL; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 887 | |
| 888 | /* Check oversize. */ |
| 889 | if (idx >= ARGM_NBARGS && argp[idx].type != ARGT_STOP) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 890 | msg = "Malformed argument mask"; |
| 891 | goto error; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 892 | } |
| 893 | |
| 894 | /* Check for mandatory arguments. */ |
| 895 | if (argp[idx].type == ARGT_STOP) { |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 896 | if (idx < min_arg) { |
| 897 | |
| 898 | /* If miss other argument than the first one, we return an error. */ |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 899 | if (idx > 0) { |
| 900 | msg = "Mandatory argument expected"; |
| 901 | goto error; |
| 902 | } |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 903 | |
| 904 | /* If first argument have a certain type, some default values |
| 905 | * may be used. See the function smp_resolve_args(). |
| 906 | */ |
| 907 | switch (mask & ARGT_MASK) { |
| 908 | |
| 909 | case ARGT_FE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 910 | if (!(p->cap & PR_CAP_FE)) { |
| 911 | msg = "Mandatory argument expected"; |
| 912 | goto error; |
| 913 | } |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 914 | argp[idx].data.prx = p; |
| 915 | argp[idx].type = ARGT_FE; |
| 916 | argp[idx+1].type = ARGT_STOP; |
| 917 | break; |
| 918 | |
| 919 | case ARGT_BE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 920 | if (!(p->cap & PR_CAP_BE)) { |
| 921 | msg = "Mandatory argument expected"; |
| 922 | goto error; |
| 923 | } |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 924 | argp[idx].data.prx = p; |
| 925 | argp[idx].type = ARGT_BE; |
| 926 | argp[idx+1].type = ARGT_STOP; |
| 927 | break; |
| 928 | |
| 929 | case ARGT_TAB: |
Olivier Houchard | a265827 | 2022-09-13 00:35:53 +0200 | [diff] [blame] | 930 | if (!p->table) { |
| 931 | msg = "Mandatory argument expected"; |
| 932 | goto error; |
| 933 | } |
| 934 | argp[idx].data.t = p->table; |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 935 | argp[idx].type = ARGT_TAB; |
| 936 | argp[idx+1].type = ARGT_STOP; |
| 937 | break; |
| 938 | |
| 939 | default: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 940 | msg = "Mandatory argument expected"; |
| 941 | goto error; |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 942 | break; |
| 943 | } |
| 944 | } |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 945 | break; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 946 | } |
| 947 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 948 | /* Check for exceed the number of required argument. */ |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 949 | if ((mask & ARGT_MASK) == ARGT_STOP && |
| 950 | argp[idx].type != ARGT_STOP) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 951 | msg = "Last argument expected"; |
| 952 | goto error; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 953 | } |
| 954 | |
| 955 | if ((mask & ARGT_MASK) == ARGT_STOP && |
| 956 | argp[idx].type == ARGT_STOP) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 957 | break; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 958 | } |
| 959 | |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 960 | /* Convert some argument types. All string in argp[] are for not |
| 961 | * duplicated yet. |
| 962 | */ |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 963 | switch (mask & ARGT_MASK) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 964 | case ARGT_SINT: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 965 | if (argp[idx].type != ARGT_SINT) { |
| 966 | msg = "integer expected"; |
| 967 | goto error; |
| 968 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 969 | argp[idx].type = ARGT_SINT; |
| 970 | break; |
| 971 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 972 | case ARGT_TIME: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 973 | if (argp[idx].type != ARGT_SINT) { |
| 974 | msg = "integer expected"; |
| 975 | goto error; |
| 976 | } |
Thierry FOURNIER | 29176f3 | 2015-07-07 00:41:29 +0200 | [diff] [blame] | 977 | argp[idx].type = ARGT_TIME; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 978 | break; |
| 979 | |
| 980 | case ARGT_SIZE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 981 | if (argp[idx].type != ARGT_SINT) { |
| 982 | msg = "integer expected"; |
| 983 | goto error; |
| 984 | } |
Thierry FOURNIER | 29176f3 | 2015-07-07 00:41:29 +0200 | [diff] [blame] | 985 | argp[idx].type = ARGT_SIZE; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 986 | break; |
| 987 | |
| 988 | case ARGT_FE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 989 | if (argp[idx].type != ARGT_STR) { |
| 990 | msg = "string expected"; |
| 991 | goto error; |
| 992 | } |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 993 | 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] | 994 | if (!argp[idx].data.prx) { |
| 995 | msg = "frontend doesn't exist"; |
| 996 | goto error; |
| 997 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 998 | argp[idx].type = ARGT_FE; |
| 999 | break; |
| 1000 | |
| 1001 | case ARGT_BE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1002 | if (argp[idx].type != ARGT_STR) { |
| 1003 | msg = "string expected"; |
| 1004 | goto error; |
| 1005 | } |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 1006 | 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] | 1007 | if (!argp[idx].data.prx) { |
| 1008 | msg = "backend doesn't exist"; |
| 1009 | goto error; |
| 1010 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 1011 | argp[idx].type = ARGT_BE; |
| 1012 | break; |
| 1013 | |
| 1014 | case ARGT_TAB: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1015 | if (argp[idx].type != ARGT_STR) { |
| 1016 | msg = "string expected"; |
| 1017 | goto error; |
| 1018 | } |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 1019 | 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] | 1020 | if (!argp[idx].data.t) { |
| 1021 | msg = "table doesn't exist"; |
| 1022 | goto error; |
| 1023 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 1024 | argp[idx].type = ARGT_TAB; |
| 1025 | break; |
| 1026 | |
| 1027 | case ARGT_SRV: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1028 | if (argp[idx].type != ARGT_STR) { |
| 1029 | msg = "string expected"; |
| 1030 | goto error; |
| 1031 | } |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 1032 | sname = strrchr(argp[idx].data.str.area, '/'); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 1033 | if (sname) { |
| 1034 | *sname++ = '\0'; |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 1035 | pname = argp[idx].data.str.area; |
Willy Tarreau | 9e0bb10 | 2015-05-26 11:24:42 +0200 | [diff] [blame] | 1036 | px = proxy_be_by_name(pname); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1037 | if (!px) { |
| 1038 | msg = "backend doesn't exist"; |
| 1039 | goto error; |
| 1040 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 1041 | } |
| 1042 | else { |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 1043 | sname = argp[idx].data.str.area; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 1044 | px = p; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 1045 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 1046 | argp[idx].data.srv = findserver(px, sname); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1047 | if (!argp[idx].data.srv) { |
| 1048 | msg = "server doesn't exist"; |
| 1049 | goto error; |
| 1050 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 1051 | argp[idx].type = ARGT_SRV; |
| 1052 | break; |
| 1053 | |
| 1054 | case ARGT_IPV4: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1055 | if (argp[idx].type != ARGT_STR) { |
| 1056 | msg = "string expected"; |
| 1057 | goto error; |
| 1058 | } |
| 1059 | if (inet_pton(AF_INET, argp[idx].data.str.area, &argp[idx].data.ipv4)) { |
| 1060 | msg = "invalid IPv4 address"; |
| 1061 | goto error; |
| 1062 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 1063 | argp[idx].type = ARGT_IPV4; |
| 1064 | break; |
| 1065 | |
| 1066 | case ARGT_MSK4: |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 1067 | if (argp[idx].type == ARGT_SINT) |
| 1068 | len2mask4(argp[idx].data.sint, &argp[idx].data.ipv4); |
| 1069 | else if (argp[idx].type == ARGT_STR) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1070 | if (!str2mask(argp[idx].data.str.area, &argp[idx].data.ipv4)) { |
| 1071 | msg = "invalid IPv4 mask"; |
| 1072 | goto error; |
| 1073 | } |
| 1074 | } |
| 1075 | else { |
| 1076 | msg = "integer or string expected"; |
| 1077 | goto error; |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 1078 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 1079 | argp[idx].type = ARGT_MSK4; |
| 1080 | break; |
| 1081 | |
| 1082 | case ARGT_IPV6: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1083 | if (argp[idx].type != ARGT_STR) { |
| 1084 | msg = "string expected"; |
| 1085 | goto error; |
| 1086 | } |
| 1087 | if (inet_pton(AF_INET6, argp[idx].data.str.area, &argp[idx].data.ipv6)) { |
| 1088 | msg = "invalid IPv6 address"; |
| 1089 | goto error; |
| 1090 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 1091 | argp[idx].type = ARGT_IPV6; |
| 1092 | break; |
| 1093 | |
| 1094 | case ARGT_MSK6: |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 1095 | if (argp[idx].type == ARGT_SINT) |
| 1096 | len2mask6(argp[idx].data.sint, &argp[idx].data.ipv6); |
| 1097 | else if (argp[idx].type == ARGT_STR) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1098 | if (!str2mask6(argp[idx].data.str.area, &argp[idx].data.ipv6)) { |
| 1099 | msg = "invalid IPv6 mask"; |
| 1100 | goto error; |
| 1101 | } |
| 1102 | } |
| 1103 | else { |
| 1104 | msg = "integer or string expected"; |
| 1105 | goto error; |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 1106 | } |
Tim Duesterhus | b814da6 | 2018-01-25 16:24:50 +0100 | [diff] [blame] | 1107 | argp[idx].type = ARGT_MSK6; |
| 1108 | break; |
| 1109 | |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 1110 | case ARGT_REG: |
| 1111 | if (argp[idx].type != ARGT_STR) { |
| 1112 | msg = "string expected"; |
| 1113 | goto error; |
| 1114 | } |
| 1115 | reg = regex_comp(argp[idx].data.str.area, !(argp[idx].type_flags & ARGF_REG_ICASE), 1, &err); |
| 1116 | if (!reg) { |
Aurelien DARRAGON | 159136c | 2024-06-03 23:18:24 +0200 | [diff] [blame^] | 1117 | msg = hlua_pushfstring_safe(L, "error compiling regex '%s' : '%s'", |
| 1118 | argp[idx].data.str.area, err); |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 1119 | free(err); |
| 1120 | goto error; |
| 1121 | } |
| 1122 | argp[idx].type = ARGT_REG; |
| 1123 | argp[idx].data.reg = reg; |
| 1124 | break; |
| 1125 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 1126 | case ARGT_USR: |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 1127 | if (argp[idx].type != ARGT_STR) { |
| 1128 | msg = "string expected"; |
| 1129 | goto error; |
| 1130 | } |
| 1131 | if (p->uri_auth && p->uri_auth->userlist && |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 1132 | strcmp(p->uri_auth->userlist->name, argp[idx].data.str.area) == 0) |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 1133 | ul = p->uri_auth->userlist; |
| 1134 | else |
| 1135 | ul = auth_find_userlist(argp[idx].data.str.area); |
| 1136 | |
| 1137 | if (!ul) { |
Aurelien DARRAGON | 159136c | 2024-06-03 23:18:24 +0200 | [diff] [blame^] | 1138 | msg = hlua_pushfstring_safe(L, "unable to find userlist '%s'", |
| 1139 | argp[idx].data.str.area); |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 1140 | goto error; |
| 1141 | } |
| 1142 | argp[idx].type = ARGT_USR; |
| 1143 | argp[idx].data.usr = ul; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1144 | break; |
| 1145 | |
| 1146 | case ARGT_STR: |
| 1147 | if (!chunk_dup(&tmp, &argp[idx].data.str)) { |
| 1148 | msg = "unable to duplicate string arg"; |
| 1149 | goto error; |
| 1150 | } |
| 1151 | argp[idx].data.str = tmp; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 1152 | break; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1153 | |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 1154 | case ARGT_MAP: |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 1155 | msg = "type not yet supported"; |
| 1156 | goto error; |
| 1157 | break; |
| 1158 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 1159 | } |
| 1160 | |
| 1161 | /* Check for type of argument. */ |
| 1162 | if ((mask & ARGT_MASK) != argp[idx].type) { |
Aurelien DARRAGON | 159136c | 2024-06-03 23:18:24 +0200 | [diff] [blame^] | 1163 | msg = hlua_pushfstring_safe(L, "'%s' expected, got '%s'", |
| 1164 | arg_type_names[(mask & ARGT_MASK)], |
| 1165 | arg_type_names[argp[idx].type & ARGT_MASK]); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1166 | goto error; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 1167 | } |
| 1168 | |
| 1169 | /* Next argument. */ |
| 1170 | mask >>= ARGT_BITS; |
| 1171 | idx++; |
| 1172 | } |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1173 | return 0; |
| 1174 | |
| 1175 | error: |
Olivier Houchard | da25dac | 2022-09-13 00:31:17 +0200 | [diff] [blame] | 1176 | argp[idx].type = ARGT_STOP; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1177 | for (i = 0; i < idx; i++) { |
| 1178 | if (argp[i].type == ARGT_STR) |
| 1179 | chunk_destroy(&argp[i].data.str); |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 1180 | else if (argp[i].type == ARGT_REG) |
| 1181 | regex_free(argp[i].data.reg); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1182 | } |
| 1183 | WILL_LJMP(luaL_argerror(L, first + idx, msg)); |
| 1184 | return 0; /* Never reached */ |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 1185 | } |
| 1186 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1187 | /* |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 1188 | * The following functions are used to make correspondence between the the |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1189 | * executed lua pointer and the "struct hlua *" that contain the context. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1190 | * |
| 1191 | * - hlua_gethlua : return the hlua context associated with an lua_State. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1192 | * - hlua_sethlua : create the association between hlua context and lua_state. |
| 1193 | */ |
| 1194 | static inline struct hlua *hlua_gethlua(lua_State *L) |
| 1195 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 1196 | struct hlua **hlua = lua_getextraspace(L); |
| 1197 | return *hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1198 | } |
| 1199 | static inline void hlua_sethlua(struct hlua *hlua) |
| 1200 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 1201 | struct hlua **hlua_store = lua_getextraspace(hlua->T); |
| 1202 | *hlua_store = hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1203 | } |
| 1204 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1205 | /* This function is used to send logs. It try to send on screen (stderr) |
| 1206 | * and on the default syslog server. |
| 1207 | */ |
| 1208 | static inline void hlua_sendlog(struct proxy *px, int level, const char *msg) |
| 1209 | { |
| 1210 | struct tm tm; |
| 1211 | char *p; |
| 1212 | |
| 1213 | /* Cleanup the log message. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1214 | p = trash.area; |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1215 | for (; *msg != '\0'; msg++, p++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1216 | if (p >= trash.area + trash.size - 1) { |
Thierry FOURNIER | ccf0063 | 2015-09-16 12:47:03 +0200 | [diff] [blame] | 1217 | /* Break the message if exceed the buffer size. */ |
| 1218 | *(p-4) = ' '; |
| 1219 | *(p-3) = '.'; |
| 1220 | *(p-2) = '.'; |
| 1221 | *(p-1) = '.'; |
| 1222 | break; |
| 1223 | } |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 1224 | if (isprint((unsigned char)*msg)) |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1225 | *p = *msg; |
| 1226 | else |
| 1227 | *p = '.'; |
| 1228 | } |
| 1229 | *p = '\0'; |
| 1230 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1231 | send_log(px, level, "%s\n", trash.area); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1232 | if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) { |
Christopher Faulet | f98d821 | 2020-10-02 18:13:52 +0200 | [diff] [blame] | 1233 | if (level == LOG_DEBUG && !(global.mode & MODE_DEBUG)) |
| 1234 | return; |
| 1235 | |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 1236 | get_localtime(date.tv_sec, &tm); |
| 1237 | fprintf(stderr, "[%s] %03d/%02d%02d%02d (%d) : %s\n", |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1238 | 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] | 1239 | (int)getpid(), trash.area); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1240 | fflush(stderr); |
| 1241 | } |
| 1242 | } |
| 1243 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1244 | /* This function just ensure that the yield will be always |
| 1245 | * returned with a timeout and permit to set some flags |
| 1246 | */ |
Aurelien DARRAGON | f5d3b31 | 2023-07-13 10:18:04 +0200 | [diff] [blame] | 1247 | __LJMP void hlua_yieldk(lua_State *L, int nresults, lua_KContext ctx, |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1248 | lua_KFunction k, int timeout, unsigned int flags) |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1249 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 1250 | struct hlua *hlua; |
| 1251 | |
| 1252 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 1253 | hlua = hlua_gethlua(L); |
| 1254 | if (!hlua) { |
| 1255 | return; |
| 1256 | } |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1257 | |
| 1258 | /* Set the wake timeout. If timeout is required, we set |
| 1259 | * the expiration time. |
| 1260 | */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1261 | hlua->wake_time = timeout; |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1262 | |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 1263 | hlua->flags |= flags; |
| 1264 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1265 | /* Process the yield. */ |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 1266 | MAY_LJMP(lua_yieldk(L, nresults, ctx, k)); |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1267 | } |
| 1268 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1269 | /* This function initialises the Lua environment stored in the stream. |
| 1270 | * 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] | 1271 | * 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] | 1272 | * |
| 1273 | * This function is particular. it initialises a new Lua thread. If the |
| 1274 | * initialisation fails (example: out of memory error), the lua function |
| 1275 | * throws an error (longjmp). |
| 1276 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1277 | * This function manipulates two Lua stacks: the main and the thread. Only |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 1278 | * the main stack can fail. The thread is not manipulated. This function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1279 | * MUST NOT manipulate the created thread stack state, because it is not |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1280 | * protected against errors thrown by the thread stack. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1281 | */ |
Aurelien DARRAGON | 5483bde | 2023-03-21 14:02:16 +0100 | [diff] [blame] | 1282 | 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] | 1283 | { |
| 1284 | lua->Mref = LUA_REFNIL; |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1285 | lua->flags = 0; |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 1286 | lua->gc_count = 0; |
Christopher Faulet | bc275a9 | 2020-02-26 14:55:16 +0100 | [diff] [blame] | 1287 | lua->wake_time = TICK_ETERNITY; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1288 | lua->state_id = state_id; |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 1289 | LIST_INIT(&lua->com); |
Aurelien DARRAGON | 5483bde | 2023-03-21 14:02:16 +0100 | [diff] [blame] | 1290 | if (!SET_SAFE_LJMP_PARENT(lua)) { |
| 1291 | lua->Tref = LUA_REFNIL; |
| 1292 | return 0; |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1293 | } |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1294 | lua->T = lua_newthread(hlua_states[state_id]); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1295 | if (!lua->T) { |
| 1296 | lua->Tref = LUA_REFNIL; |
Aurelien DARRAGON | 5483bde | 2023-03-21 14:02:16 +0100 | [diff] [blame] | 1297 | RESET_SAFE_LJMP_PARENT(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1298 | return 0; |
| 1299 | } |
| 1300 | hlua_sethlua(lua); |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1301 | lua->Tref = luaL_ref(hlua_states[state_id], LUA_REGISTRYINDEX); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1302 | lua->task = task; |
Aurelien DARRAGON | 5483bde | 2023-03-21 14:02:16 +0100 | [diff] [blame] | 1303 | RESET_SAFE_LJMP_PARENT(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1304 | return 1; |
| 1305 | } |
| 1306 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1307 | /* Used to destroy the Lua coroutine when the attached stream or task |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1308 | * is destroyed. The destroy also the memory context. The struct "lua" |
Aurelien DARRAGON | d5c8a10 | 2023-03-01 16:45:50 +0100 | [diff] [blame] | 1309 | * will be freed. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1310 | */ |
| 1311 | void hlua_ctx_destroy(struct hlua *lua) |
| 1312 | { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 1313 | if (!lua) |
Thierry FOURNIER | a718b29 | 2015-03-04 16:48:34 +0100 | [diff] [blame] | 1314 | return; |
| 1315 | |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 1316 | if (!lua->T) |
| 1317 | goto end; |
| 1318 | |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 1319 | /* Purge all the pending signals. */ |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1320 | notification_purge(&lua->com); |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 1321 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1322 | if (!SET_SAFE_LJMP(lua)) |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 1323 | return; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1324 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1325 | RESET_SAFE_LJMP(lua); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 1326 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1327 | if (!SET_SAFE_LJMP_PARENT(lua)) |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 1328 | return; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1329 | luaL_unref(hlua_states[lua->state_id], LUA_REGISTRYINDEX, lua->Tref); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1330 | RESET_SAFE_LJMP_PARENT(lua); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 1331 | /* Forces a garbage collecting process. If the Lua program is finished |
| 1332 | * without error, we run the GC on the thread pointer. Its freed all |
| 1333 | * the unused memory. |
| 1334 | * If the thread is finnish with an error or is currently yielded, |
| 1335 | * it seems that the GC applied on the thread doesn't clean anything, |
| 1336 | * so e run the GC on the main thread. |
| 1337 | * NOTE: maybe this action locks all the Lua threads untiml the en of |
| 1338 | * the garbage collection. |
| 1339 | */ |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 1340 | if (lua->gc_count) { |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1341 | if (!SET_SAFE_LJMP_PARENT(lua)) |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 1342 | return; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1343 | lua_gc(hlua_states[lua->state_id], LUA_GCCOLLECT, 0); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1344 | RESET_SAFE_LJMP_PARENT(lua); |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 1345 | } |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 1346 | |
Thierry FOURNIER | a7b536b | 2015-09-21 22:50:24 +0200 | [diff] [blame] | 1347 | lua->T = NULL; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 1348 | |
| 1349 | end: |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 1350 | pool_free(pool_head_hlua, lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1351 | } |
| 1352 | |
| 1353 | /* This function is used to restore the Lua context when a coroutine |
| 1354 | * fails. This function copy the common memory between old coroutine |
| 1355 | * and the new coroutine. The old coroutine is destroyed, and its |
| 1356 | * replaced by the new coroutine. |
| 1357 | * If the flag "keep_msg" is set, the last entry of the old is assumed |
| 1358 | * as string error message and it is copied in the new stack. |
| 1359 | */ |
| 1360 | static int hlua_ctx_renew(struct hlua *lua, int keep_msg) |
| 1361 | { |
| 1362 | lua_State *T; |
| 1363 | int new_ref; |
| 1364 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1365 | /* New Lua coroutine. */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1366 | T = lua_newthread(hlua_states[lua->state_id]); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1367 | if (!T) |
| 1368 | return 0; |
| 1369 | |
| 1370 | /* Copy last error message. */ |
| 1371 | if (keep_msg) |
| 1372 | lua_xmove(lua->T, T, 1); |
| 1373 | |
| 1374 | /* Copy data between the coroutines. */ |
| 1375 | lua_rawgeti(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 1376 | lua_xmove(lua->T, T, 1); |
Ilya Shipitsin | 46a030c | 2020-07-05 16:36:08 +0500 | [diff] [blame] | 1377 | new_ref = luaL_ref(T, LUA_REGISTRYINDEX); /* Value popped. */ |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1378 | |
| 1379 | /* Destroy old data. */ |
| 1380 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 1381 | |
| 1382 | /* The thread is garbage collected by Lua. */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1383 | luaL_unref(hlua_states[lua->state_id], LUA_REGISTRYINDEX, lua->Tref); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1384 | |
| 1385 | /* Fill the struct with the new coroutine values. */ |
| 1386 | lua->Mref = new_ref; |
| 1387 | lua->T = T; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1388 | lua->Tref = luaL_ref(hlua_states[lua->state_id], LUA_REGISTRYINDEX); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1389 | |
| 1390 | /* Set context. */ |
| 1391 | hlua_sethlua(lua); |
| 1392 | |
| 1393 | return 1; |
| 1394 | } |
| 1395 | |
Aurelien DARRAGON | be7b7ab | 2024-03-12 17:05:54 +0100 | [diff] [blame] | 1396 | /* Helper function to get the lua ctx for a given stream and state_id */ |
| 1397 | 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] | 1398 | { |
Aurelien DARRAGON | be7b7ab | 2024-03-12 17:05:54 +0100 | [diff] [blame] | 1399 | /* state_id == 0 -> global runtime ctx |
| 1400 | * state_id != 0 -> per-thread runtime ctx |
| 1401 | */ |
| 1402 | return s->hlua[!!state_id]; |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 1403 | } |
| 1404 | |
Aurelien DARRAGON | be7b7ab | 2024-03-12 17:05:54 +0100 | [diff] [blame] | 1405 | /* 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] | 1406 | * |
Aurelien DARRAGON | be7b7ab | 2024-03-12 17:05:54 +0100 | [diff] [blame] | 1407 | * It uses the global or per-thread ctx depending on the expected |
| 1408 | * <state_id>. |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 1409 | * |
| 1410 | * Returns hlua ctx on success and NULL on failure |
| 1411 | */ |
| 1412 | static struct hlua *hlua_stream_ctx_prepare(struct stream *s, int state_id) |
| 1413 | { |
| 1414 | /* In the execution wrappers linked with a stream, the |
| 1415 | * Lua context can be not initialized. This behavior |
| 1416 | * permits to save performances because a systematic |
| 1417 | * Lua initialization cause 5% performances loss. |
| 1418 | */ |
Aurelien DARRAGON | be7b7ab | 2024-03-12 17:05:54 +0100 | [diff] [blame] | 1419 | if (!s->hlua[!!state_id]) { |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 1420 | struct hlua *hlua; |
| 1421 | |
| 1422 | hlua = pool_alloc(pool_head_hlua); |
| 1423 | if (!hlua) |
| 1424 | return NULL; |
| 1425 | HLUA_INIT(hlua); |
| 1426 | if (!hlua_ctx_init(hlua, state_id, s->task)) { |
| 1427 | pool_free(pool_head_hlua, hlua); |
| 1428 | return NULL; |
| 1429 | } |
Aurelien DARRAGON | be7b7ab | 2024-03-12 17:05:54 +0100 | [diff] [blame] | 1430 | s->hlua[!!state_id] = hlua; |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 1431 | } |
Aurelien DARRAGON | be7b7ab | 2024-03-12 17:05:54 +0100 | [diff] [blame] | 1432 | return s->hlua[!!state_id]; |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 1433 | } |
| 1434 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1435 | void hlua_hook(lua_State *L, lua_Debug *ar) |
| 1436 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 1437 | struct hlua *hlua; |
| 1438 | |
| 1439 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 1440 | hlua = hlua_gethlua(L); |
| 1441 | if (!hlua) |
| 1442 | return; |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1443 | |
| 1444 | /* Lua cannot yield when its returning from a function, |
| 1445 | * so, we can fix the interrupt hook to 1 instruction, |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1446 | * expecting that the function is finished. |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1447 | */ |
| 1448 | if (lua_gethookmask(L) & LUA_MASKRET) { |
| 1449 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, 1); |
| 1450 | return; |
| 1451 | } |
| 1452 | |
| 1453 | /* restore the interrupt condition. */ |
| 1454 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 1455 | |
| 1456 | /* If we interrupt the Lua processing in yieldable state, we yield. |
| 1457 | * If the state is not yieldable, trying yield causes an error. |
| 1458 | */ |
| 1459 | if (lua_isyieldable(L)) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 1460 | MAY_LJMP(hlua_yieldk(L, 0, 0, NULL, TICK_ETERNITY, HLUA_CTRLYIELD)); |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1461 | |
Thierry FOURNIER | a85cfb1 | 2015-03-13 14:50:06 +0100 | [diff] [blame] | 1462 | /* If we cannot yield, update the clock and check the timeout. */ |
| 1463 | tv_update_date(0, 1); |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1464 | hlua->run_time += now_ms - hlua->start_time; |
| 1465 | if (hlua->max_time && hlua->run_time >= hlua->max_time) { |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1466 | lua_pushfstring(L, "execution timeout"); |
| 1467 | WILL_LJMP(lua_error(L)); |
| 1468 | } |
| 1469 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1470 | /* Update the start time. */ |
| 1471 | hlua->start_time = now_ms; |
| 1472 | |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1473 | /* Try to interrupt the process at the end of the current |
| 1474 | * unyieldable function. |
| 1475 | */ |
| 1476 | 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] | 1477 | } |
| 1478 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1479 | /* This function start or resumes the Lua stack execution. If the flag |
| 1480 | * "yield_allowed" if no set and the LUA stack execution returns a yield |
| 1481 | * The function return an error. |
| 1482 | * |
| 1483 | * The function can returns 4 values: |
| 1484 | * - HLUA_E_OK : The execution is terminated without any errors. |
| 1485 | * - HLUA_E_AGAIN : The execution must continue at the next associated |
| 1486 | * task wakeup. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1487 | * - 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] | 1488 | * the top of the stack. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1489 | * - HLUA_E_ERR : An error has occurred without error message. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1490 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1491 | * 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] | 1492 | * LUA code. |
| 1493 | */ |
| 1494 | static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed) |
| 1495 | { |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1496 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
| 1497 | int nres; |
| 1498 | #endif |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1499 | int ret; |
| 1500 | const char *msg; |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1501 | const char *trace; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1502 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1503 | /* Initialise run time counter. */ |
| 1504 | if (!HLUA_IS_RUNNING(lua)) |
| 1505 | lua->run_time = 0; |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1506 | |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1507 | /* Lock the whole Lua execution. This lock must be before the |
| 1508 | * label "resume_execution". |
| 1509 | */ |
Aurelien DARRAGON | e00bf5d | 2023-03-21 13:22:33 +0100 | [diff] [blame] | 1510 | hlua_lock(lua); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1511 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1512 | resume_execution: |
| 1513 | |
| 1514 | /* This hook interrupts the Lua processing each 'hlua_nb_instruction' |
| 1515 | * instructions. it is used for preventing infinite loops. |
| 1516 | */ |
| 1517 | lua_sethook(lua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 1518 | |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1519 | /* Remove all flags except the running flags. */ |
Thierry FOURNIER | 2f3867f | 2015-09-28 01:02:01 +0200 | [diff] [blame] | 1520 | HLUA_SET_RUN(lua); |
| 1521 | HLUA_CLR_CTRLYIELD(lua); |
| 1522 | HLUA_CLR_WAKERESWR(lua); |
| 1523 | HLUA_CLR_WAKEREQWR(lua); |
Christopher Faulet | a6e792f | 2021-08-04 17:58:21 +0200 | [diff] [blame] | 1524 | HLUA_CLR_NOYIELD(lua); |
| 1525 | if (!yield_allowed) |
| 1526 | HLUA_SET_NOYIELD(lua); |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1527 | |
Christopher Faulet | bc275a9 | 2020-02-26 14:55:16 +0100 | [diff] [blame] | 1528 | /* Update the start time and reset wake_time. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1529 | lua->start_time = now_ms; |
Christopher Faulet | bc275a9 | 2020-02-26 14:55:16 +0100 | [diff] [blame] | 1530 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1531 | |
Aurelien DARRAGON | 5d35196 | 2024-03-12 00:22:44 +0100 | [diff] [blame] | 1532 | HLUA_SET_BUSY(lua); |
| 1533 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1534 | /* Call the function. */ |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1535 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1536 | 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] | 1537 | #else |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1538 | ret = lua_resume(lua->T, hlua_states[lua->state_id], lua->nargs); |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1539 | #endif |
Aurelien DARRAGON | fff2796 | 2023-09-08 14:00:27 +0200 | [diff] [blame] | 1540 | |
Aurelien DARRAGON | 5d35196 | 2024-03-12 00:22:44 +0100 | [diff] [blame] | 1541 | HLUA_CLR_BUSY(lua); |
| 1542 | |
Aurelien DARRAGON | fff2796 | 2023-09-08 14:00:27 +0200 | [diff] [blame] | 1543 | /* reset nargs because those possibly passed to the lua_resume() call |
| 1544 | * were already consumed, and since we may call lua_resume() again |
| 1545 | * after a successful yield, we don't want to pass stale nargs hint |
| 1546 | * to the Lua API. As such, nargs should be set explicitly before each |
| 1547 | * lua_resume() (or hlua_ctx_resume()) invocation if needed. |
| 1548 | */ |
| 1549 | lua->nargs = 0; |
| 1550 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1551 | switch (ret) { |
| 1552 | |
| 1553 | case LUA_OK: |
| 1554 | ret = HLUA_E_OK; |
| 1555 | break; |
| 1556 | |
| 1557 | case LUA_YIELD: |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1558 | /* Check if the execution timeout is expired. It it is the case, we |
| 1559 | * break the Lua execution. |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1560 | */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1561 | tv_update_date(0, 1); |
| 1562 | lua->run_time += now_ms - lua->start_time; |
| 1563 | if (lua->max_time && lua->run_time > lua->max_time) { |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1564 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1565 | ret = HLUA_E_ETMOUT; |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1566 | break; |
| 1567 | } |
| 1568 | /* Process the forced yield. if the general yield is not allowed or |
| 1569 | * if no task were associated this the current Lua execution |
| 1570 | * coroutine, we resume the execution. Else we want to return in the |
| 1571 | * scheduler and we want to be waked up again, to continue the |
| 1572 | * current Lua execution. So we schedule our own task. |
| 1573 | */ |
| 1574 | if (HLUA_IS_CTRLYIELDING(lua)) { |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1575 | if (!yield_allowed || !lua->task) |
| 1576 | goto resume_execution; |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1577 | task_wakeup(lua->task, TASK_WOKEN_MSG); |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1578 | } |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1579 | if (!yield_allowed) { |
| 1580 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1581 | ret = HLUA_E_YIELD; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1582 | break; |
| 1583 | } |
| 1584 | ret = HLUA_E_AGAIN; |
| 1585 | break; |
| 1586 | |
| 1587 | case LUA_ERRRUN: |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1588 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1589 | /* Special exit case. The traditional exit is returned as an error |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1590 | * because the errors ares the only one mean to return immediately |
| 1591 | * from and lua execution. |
| 1592 | */ |
| 1593 | if (lua->flags & HLUA_EXIT) { |
| 1594 | ret = HLUA_E_OK; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 1595 | hlua_ctx_renew(lua, 1); |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1596 | break; |
| 1597 | } |
| 1598 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1599 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1600 | if (!lua_checkstack(lua->T, 1)) { |
| 1601 | ret = HLUA_E_ERR; |
| 1602 | break; |
| 1603 | } |
Aurelien DARRAGON | a374161 | 2024-03-01 15:55:17 +0100 | [diff] [blame] | 1604 | msg = hlua_tostring_safe(lua->T, -1); |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 1605 | trace = hlua_traceback(lua->T, ", "); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1606 | if (msg) |
Aurelien DARRAGON | 159136c | 2024-06-03 23:18:24 +0200 | [diff] [blame^] | 1607 | hlua_pushfstring_safe(lua->T, "[state-id %d] runtime error: %s from %s", |
| 1608 | lua->state_id, msg, trace); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1609 | else |
Aurelien DARRAGON | 159136c | 2024-06-03 23:18:24 +0200 | [diff] [blame^] | 1610 | hlua_pushfstring_safe(lua->T, "[state-id %d] unknown runtime error from %s", |
| 1611 | lua->state_id, trace); |
Aurelien DARRAGON | adac932 | 2024-03-01 19:54:16 +0100 | [diff] [blame] | 1612 | |
| 1613 | /* Move the error msg at the top and then empty the stack except last msg */ |
| 1614 | lua_insert(lua->T, -lua_gettop(lua->T)); |
| 1615 | lua_settop(lua->T, 1); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1616 | ret = HLUA_E_ERRMSG; |
| 1617 | break; |
| 1618 | |
| 1619 | case LUA_ERRMEM: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1620 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1621 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1622 | ret = HLUA_E_NOMEM; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1623 | break; |
| 1624 | |
| 1625 | case LUA_ERRERR: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1626 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1627 | if (!lua_checkstack(lua->T, 1)) { |
| 1628 | ret = HLUA_E_ERR; |
| 1629 | break; |
| 1630 | } |
Aurelien DARRAGON | a374161 | 2024-03-01 15:55:17 +0100 | [diff] [blame] | 1631 | msg = hlua_tostring_safe(lua->T, -1); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1632 | if (msg) |
Aurelien DARRAGON | 159136c | 2024-06-03 23:18:24 +0200 | [diff] [blame^] | 1633 | hlua_pushfstring_safe(lua->T, "[state-id %d] message handler error: %s", |
| 1634 | lua->state_id, msg); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1635 | else |
Aurelien DARRAGON | 159136c | 2024-06-03 23:18:24 +0200 | [diff] [blame^] | 1636 | hlua_pushfstring_safe(lua->T, "[state-id %d] message handler error", |
| 1637 | lua->state_id); |
Aurelien DARRAGON | adac932 | 2024-03-01 19:54:16 +0100 | [diff] [blame] | 1638 | |
| 1639 | /* Move the error msg at the top and then empty the stack except last msg */ |
| 1640 | lua_insert(lua->T, -lua_gettop(lua->T)); |
| 1641 | lua_settop(lua->T, 1); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1642 | ret = HLUA_E_ERRMSG; |
| 1643 | break; |
| 1644 | |
| 1645 | default: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1646 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1647 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1648 | ret = HLUA_E_ERR; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1649 | break; |
| 1650 | } |
| 1651 | |
| 1652 | switch (ret) { |
| 1653 | case HLUA_E_AGAIN: |
| 1654 | break; |
| 1655 | |
| 1656 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1657 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1658 | hlua_ctx_renew(lua, 1); |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1659 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1660 | break; |
| 1661 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1662 | case HLUA_E_ETMOUT: |
| 1663 | case HLUA_E_NOMEM: |
| 1664 | case HLUA_E_YIELD: |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1665 | case HLUA_E_ERR: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1666 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1667 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1668 | hlua_ctx_renew(lua, 0); |
| 1669 | break; |
| 1670 | |
| 1671 | case HLUA_E_OK: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1672 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1673 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1674 | break; |
| 1675 | } |
| 1676 | |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1677 | /* This is the main exit point, remove the Lua lock. */ |
Aurelien DARRAGON | e00bf5d | 2023-03-21 13:22:33 +0100 | [diff] [blame] | 1678 | hlua_unlock(lua); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1679 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1680 | return ret; |
| 1681 | } |
| 1682 | |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1683 | /* This function exit the current code. */ |
| 1684 | __LJMP static int hlua_done(lua_State *L) |
| 1685 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 1686 | struct hlua *hlua; |
| 1687 | |
| 1688 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 1689 | hlua = hlua_gethlua(L); |
| 1690 | if (!hlua) |
| 1691 | return 0; |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1692 | |
| 1693 | hlua->flags |= HLUA_EXIT; |
| 1694 | WILL_LJMP(lua_error(L)); |
| 1695 | |
| 1696 | return 0; |
| 1697 | } |
| 1698 | |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1699 | /* This function is an LUA binding. It provides a function |
| 1700 | * for deleting ACL from a referenced ACL file. |
| 1701 | */ |
| 1702 | __LJMP static int hlua_del_acl(lua_State *L) |
| 1703 | { |
| 1704 | const char *name; |
| 1705 | const char *key; |
| 1706 | struct pat_ref *ref; |
| 1707 | |
| 1708 | MAY_LJMP(check_args(L, 2, "del_acl")); |
| 1709 | |
| 1710 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1711 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1712 | |
| 1713 | ref = pat_ref_lookup(name); |
| 1714 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1715 | WILL_LJMP(luaL_error(L, "'del_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1716 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1717 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1718 | pat_ref_delete(ref, key); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1719 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1720 | return 0; |
| 1721 | } |
| 1722 | |
| 1723 | /* This function is an LUA binding. It provides a function |
| 1724 | * for deleting map entry from a referenced map file. |
| 1725 | */ |
| 1726 | static int hlua_del_map(lua_State *L) |
| 1727 | { |
| 1728 | const char *name; |
| 1729 | const char *key; |
| 1730 | struct pat_ref *ref; |
| 1731 | |
| 1732 | MAY_LJMP(check_args(L, 2, "del_map")); |
| 1733 | |
| 1734 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1735 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1736 | |
| 1737 | ref = pat_ref_lookup(name); |
| 1738 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1739 | WILL_LJMP(luaL_error(L, "'del_map': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1740 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1741 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1742 | pat_ref_delete(ref, key); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1743 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1744 | return 0; |
| 1745 | } |
| 1746 | |
| 1747 | /* This function is an LUA binding. It provides a function |
| 1748 | * for adding ACL pattern from a referenced ACL file. |
| 1749 | */ |
| 1750 | static int hlua_add_acl(lua_State *L) |
| 1751 | { |
| 1752 | const char *name; |
| 1753 | const char *key; |
| 1754 | struct pat_ref *ref; |
| 1755 | |
| 1756 | MAY_LJMP(check_args(L, 2, "add_acl")); |
| 1757 | |
| 1758 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1759 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1760 | |
| 1761 | ref = pat_ref_lookup(name); |
| 1762 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1763 | WILL_LJMP(luaL_error(L, "'add_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1764 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1765 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1766 | if (pat_ref_find_elt(ref, key) == NULL) |
| 1767 | pat_ref_add(ref, key, NULL, NULL); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1768 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1769 | return 0; |
| 1770 | } |
| 1771 | |
| 1772 | /* This function is an LUA binding. It provides a function |
| 1773 | * for setting map pattern and sample from a referenced map |
| 1774 | * file. |
| 1775 | */ |
| 1776 | static int hlua_set_map(lua_State *L) |
| 1777 | { |
| 1778 | const char *name; |
| 1779 | const char *key; |
| 1780 | const char *value; |
| 1781 | struct pat_ref *ref; |
| 1782 | |
| 1783 | MAY_LJMP(check_args(L, 3, "set_map")); |
| 1784 | |
| 1785 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1786 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1787 | value = MAY_LJMP(luaL_checkstring(L, 3)); |
| 1788 | |
| 1789 | ref = pat_ref_lookup(name); |
| 1790 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1791 | WILL_LJMP(luaL_error(L, "'set_map': unknown map file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1792 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1793 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1794 | if (pat_ref_find_elt(ref, key) != NULL) |
| 1795 | pat_ref_set(ref, key, value, NULL); |
| 1796 | else |
| 1797 | pat_ref_add(ref, key, value, NULL); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1798 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1799 | return 0; |
| 1800 | } |
| 1801 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1802 | /* A class is a lot of memory that contain data. This data can be a table, |
| 1803 | * 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] | 1804 | * metatable have an original version registered in the global context with |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1805 | * the name of the object (_G[<name>] = <metable> ). |
| 1806 | * |
| 1807 | * A metable is a table that modify the standard behavior of a standard |
| 1808 | * access to the associated data. The entries of this new metatable are |
| 1809 | * defined as is: |
| 1810 | * |
| 1811 | * http://lua-users.org/wiki/MetatableEvents |
| 1812 | * |
| 1813 | * __index |
| 1814 | * |
| 1815 | * we access an absent field in a table, the result is nil. This is |
| 1816 | * true, but it is not the whole truth. Actually, such access triggers |
| 1817 | * the interpreter to look for an __index metamethod: If there is no |
| 1818 | * such method, as usually happens, then the access results in nil; |
| 1819 | * otherwise, the metamethod will provide the result. |
| 1820 | * |
| 1821 | * Control 'prototype' inheritance. When accessing "myTable[key]" and |
| 1822 | * the key does not appear in the table, but the metatable has an __index |
| 1823 | * property: |
| 1824 | * |
| 1825 | * - if the value is a function, the function is called, passing in the |
| 1826 | * table and the key; the return value of that function is returned as |
| 1827 | * the result. |
| 1828 | * |
| 1829 | * - if the value is another table, the value of the key in that table is |
| 1830 | * asked for and returned (and if it doesn't exist in that table, but that |
| 1831 | * table's metatable has an __index property, then it continues on up) |
| 1832 | * |
| 1833 | * - Use "rawget(myTable,key)" to skip this metamethod. |
| 1834 | * |
| 1835 | * http://www.lua.org/pil/13.4.1.html |
| 1836 | * |
| 1837 | * __newindex |
| 1838 | * |
| 1839 | * Like __index, but control property assignment. |
| 1840 | * |
| 1841 | * __mode - Control weak references. A string value with one or both |
| 1842 | * of the characters 'k' and 'v' which specifies that the the |
| 1843 | * keys and/or values in the table are weak references. |
| 1844 | * |
| 1845 | * __call - Treat a table like a function. When a table is followed by |
| 1846 | * parenthesis such as "myTable( 'foo' )" and the metatable has |
| 1847 | * a __call key pointing to a function, that function is invoked |
| 1848 | * (passing any specified arguments) and the return value is |
| 1849 | * returned. |
| 1850 | * |
| 1851 | * __metatable - Hide the metatable. When "getmetatable( myTable )" is |
| 1852 | * called, if the metatable for myTable has a __metatable |
| 1853 | * key, the value of that key is returned instead of the |
| 1854 | * actual metatable. |
| 1855 | * |
| 1856 | * __tostring - Control string representation. When the builtin |
| 1857 | * "tostring( myTable )" function is called, if the metatable |
| 1858 | * for myTable has a __tostring property set to a function, |
| 1859 | * that function is invoked (passing myTable to it) and the |
| 1860 | * return value is used as the string representation. |
| 1861 | * |
| 1862 | * __len - Control table length. When the table length is requested using |
| 1863 | * the length operator ( '#' ), if the metatable for myTable has |
| 1864 | * a __len key pointing to a function, that function is invoked |
| 1865 | * (passing myTable to it) and the return value used as the value |
| 1866 | * of "#myTable". |
| 1867 | * |
| 1868 | * __gc - Userdata finalizer code. When userdata is set to be garbage |
| 1869 | * collected, if the metatable has a __gc field pointing to a |
| 1870 | * function, that function is first invoked, passing the userdata |
| 1871 | * to it. The __gc metamethod is not called for tables. |
| 1872 | * (See http://lua-users.org/lists/lua-l/2006-11/msg00508.html) |
| 1873 | * |
| 1874 | * Special metamethods for redefining standard operators: |
| 1875 | * http://www.lua.org/pil/13.1.html |
| 1876 | * |
| 1877 | * __add "+" |
| 1878 | * __sub "-" |
| 1879 | * __mul "*" |
| 1880 | * __div "/" |
| 1881 | * __unm "!" |
| 1882 | * __pow "^" |
| 1883 | * __concat ".." |
| 1884 | * |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1885 | * Special methods for redefining standard relations |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1886 | * http://www.lua.org/pil/13.2.html |
| 1887 | * |
| 1888 | * __eq "==" |
| 1889 | * __lt "<" |
| 1890 | * __le "<=" |
| 1891 | */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1892 | |
| 1893 | /* |
| 1894 | * |
| 1895 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1896 | * Class Map |
| 1897 | * |
| 1898 | * |
| 1899 | */ |
| 1900 | |
| 1901 | /* Returns a struct hlua_map if the stack entry "ud" is |
| 1902 | * a class session, otherwise it throws an error. |
| 1903 | */ |
| 1904 | __LJMP static struct map_descriptor *hlua_checkmap(lua_State *L, int ud) |
| 1905 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1906 | return MAY_LJMP(hlua_checkudata(L, ud, class_map_ref)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1907 | } |
| 1908 | |
| 1909 | /* This function is the map constructor. It don't need |
| 1910 | * the class Map object. It creates and return a new Map |
| 1911 | * object. It must be called only during "body" or "init" |
| 1912 | * context because it process some filesystem accesses. |
| 1913 | */ |
| 1914 | __LJMP static int hlua_map_new(struct lua_State *L) |
| 1915 | { |
| 1916 | const char *fn; |
| 1917 | int match = PAT_MATCH_STR; |
| 1918 | struct sample_conv conv; |
| 1919 | const char *file = ""; |
| 1920 | int line = 0; |
| 1921 | lua_Debug ar; |
| 1922 | char *err = NULL; |
| 1923 | struct arg args[2]; |
| 1924 | |
| 1925 | if (lua_gettop(L) < 1 || lua_gettop(L) > 2) |
| 1926 | WILL_LJMP(luaL_error(L, "'new' needs at least 1 argument.")); |
| 1927 | |
| 1928 | fn = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1929 | |
| 1930 | if (lua_gettop(L) >= 2) { |
| 1931 | match = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 1932 | if (match < 0 || match >= PAT_MATCH_NUM) |
| 1933 | WILL_LJMP(luaL_error(L, "'new' needs a valid match method.")); |
| 1934 | } |
| 1935 | |
| 1936 | /* Get Lua filename and line number. */ |
| 1937 | if (lua_getstack(L, 1, &ar)) { /* check function at level */ |
| 1938 | lua_getinfo(L, "Sl", &ar); /* get info about it */ |
| 1939 | if (ar.currentline > 0) { /* is there info? */ |
| 1940 | file = ar.short_src; |
| 1941 | line = ar.currentline; |
| 1942 | } |
| 1943 | } |
| 1944 | |
| 1945 | /* fill fake sample_conv struct. */ |
| 1946 | conv.kw = ""; /* unused. */ |
| 1947 | conv.process = NULL; /* unused. */ |
| 1948 | conv.arg_mask = 0; /* unused. */ |
| 1949 | conv.val_args = NULL; /* unused. */ |
| 1950 | conv.out_type = SMP_T_STR; |
| 1951 | conv.private = (void *)(long)match; |
| 1952 | switch (match) { |
| 1953 | case PAT_MATCH_STR: conv.in_type = SMP_T_STR; break; |
| 1954 | case PAT_MATCH_BEG: conv.in_type = SMP_T_STR; break; |
| 1955 | case PAT_MATCH_SUB: conv.in_type = SMP_T_STR; break; |
| 1956 | case PAT_MATCH_DIR: conv.in_type = SMP_T_STR; break; |
| 1957 | case PAT_MATCH_DOM: conv.in_type = SMP_T_STR; break; |
| 1958 | case PAT_MATCH_END: conv.in_type = SMP_T_STR; break; |
| 1959 | case PAT_MATCH_REG: conv.in_type = SMP_T_STR; break; |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1960 | case PAT_MATCH_INT: conv.in_type = SMP_T_SINT; break; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1961 | case PAT_MATCH_IP: conv.in_type = SMP_T_ADDR; break; |
| 1962 | default: |
| 1963 | WILL_LJMP(luaL_error(L, "'new' doesn't support this match mode.")); |
| 1964 | } |
| 1965 | |
| 1966 | /* fill fake args. */ |
| 1967 | args[0].type = ARGT_STR; |
Christopher Faulet | 73292e9 | 2020-08-06 08:40:09 +0200 | [diff] [blame] | 1968 | args[0].data.str.area = strdup(fn); |
| 1969 | args[0].data.str.data = strlen(fn); |
| 1970 | args[0].data.str.size = args[0].data.str.data+1; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1971 | args[1].type = ARGT_STOP; |
| 1972 | |
| 1973 | /* load the map. */ |
| 1974 | if (!sample_load_map(args, &conv, file, line, &err)) { |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 1975 | /* error case: we can't use luaL_error because we must |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1976 | * free the err variable. |
| 1977 | */ |
Aurelien DARRAGON | ca0cdfe | 2024-06-04 12:48:45 +0200 | [diff] [blame] | 1978 | hlua_pusherror(L, "'new': %s.", err); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1979 | free(err); |
Christopher Faulet | 6ad7df4 | 2020-08-07 11:45:18 +0200 | [diff] [blame] | 1980 | chunk_destroy(&args[0].data.str); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1981 | WILL_LJMP(lua_error(L)); |
| 1982 | } |
| 1983 | |
| 1984 | /* create the lua object. */ |
| 1985 | lua_newtable(L); |
| 1986 | lua_pushlightuserdata(L, args[0].data.map); |
| 1987 | lua_rawseti(L, -2, 0); |
| 1988 | |
| 1989 | /* Pop a class Map metatable and affect it to the userdata. */ |
| 1990 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_map_ref); |
| 1991 | lua_setmetatable(L, -2); |
| 1992 | |
| 1993 | |
| 1994 | return 1; |
| 1995 | } |
| 1996 | |
| 1997 | __LJMP static inline int _hlua_map_lookup(struct lua_State *L, int str) |
| 1998 | { |
| 1999 | struct map_descriptor *desc; |
| 2000 | struct pattern *pat; |
| 2001 | struct sample smp; |
| 2002 | |
| 2003 | MAY_LJMP(check_args(L, 2, "lookup")); |
| 2004 | desc = MAY_LJMP(hlua_checkmap(L, 1)); |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 2005 | if (desc->pat.expect_type == SMP_T_SINT) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 2006 | smp.data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 2007 | smp.data.u.sint = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2008 | } |
| 2009 | else { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 2010 | smp.data.type = SMP_T_STR; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2011 | smp.flags = SMP_F_CONST; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2012 | 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] | 2013 | smp.data.u.str.size = smp.data.u.str.data + 1; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2014 | } |
| 2015 | |
| 2016 | pat = pattern_exec_match(&desc->pat, &smp, 1); |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 2017 | if (!pat || !pat->data) { |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2018 | if (str) |
| 2019 | lua_pushstring(L, ""); |
| 2020 | else |
| 2021 | lua_pushnil(L); |
| 2022 | return 1; |
| 2023 | } |
| 2024 | |
| 2025 | /* 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] | 2026 | 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] | 2027 | return 1; |
| 2028 | } |
| 2029 | |
| 2030 | __LJMP static int hlua_map_lookup(struct lua_State *L) |
| 2031 | { |
| 2032 | return _hlua_map_lookup(L, 0); |
| 2033 | } |
| 2034 | |
| 2035 | __LJMP static int hlua_map_slookup(struct lua_State *L) |
| 2036 | { |
| 2037 | return _hlua_map_lookup(L, 1); |
| 2038 | } |
| 2039 | |
| 2040 | /* |
| 2041 | * |
| 2042 | * |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2043 | * Class Socket |
| 2044 | * |
| 2045 | * |
| 2046 | */ |
| 2047 | |
| 2048 | __LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud) |
| 2049 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 2050 | return MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2051 | } |
| 2052 | |
| 2053 | /* 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] | 2054 | * connection. It is used for notify space available to send or data |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2055 | * received. |
| 2056 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 2057 | static void hlua_socket_handler(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2058 | { |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 2059 | struct stream_interface *si = appctx->owner; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2060 | |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 2061 | if (appctx->ctx.hlua_cosocket.die) { |
| 2062 | si_shutw(si); |
| 2063 | si_shutr(si); |
| 2064 | si_ic(si)->flags |= CF_READ_NULL; |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 2065 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 2066 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 2067 | stream_shutdown(si_strm(si), SF_ERR_KILLED); |
| 2068 | } |
| 2069 | |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 2070 | /* If we can't write, wakeup the pending write signals. */ |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 2071 | if (channel_output_closed(si_ic(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 2072 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 2073 | |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 2074 | /* If we can't read, wakeup the pending read signals. */ |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 2075 | if (channel_input_closed(si_oc(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 2076 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 2077 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2078 | /* if the connection is not established, inform the stream that we want |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 2079 | * to be notified whenever the connection completes. |
| 2080 | */ |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2081 | if (si_opposite(si)->state < SI_ST_EST) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 2082 | si_cant_get(si); |
Willy Tarreau | 12c2423 | 2018-12-06 15:29:50 +0100 | [diff] [blame] | 2083 | si_rx_conn_blk(si); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 2084 | si_rx_endp_more(si); |
Willy Tarreau | d4da196 | 2015-04-20 01:31:23 +0200 | [diff] [blame] | 2085 | return; |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 2086 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2087 | |
| 2088 | /* This function is called after the connect. */ |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2089 | appctx->ctx.hlua_cosocket.connected = 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2090 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2091 | /* 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] | 2092 | if (channel_may_recv(si_ic(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 2093 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2094 | |
| 2095 | /* Wake the tasks which wants to read if the buffer contains data. */ |
Christopher Faulet | 174144c | 2024-02-16 15:33:44 +0100 | [diff] [blame] | 2096 | if (!channel_is_empty(si_oc(si))) { |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 2097 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
Christopher Faulet | 174144c | 2024-02-16 15:33:44 +0100 | [diff] [blame] | 2098 | si_cant_get(si); |
| 2099 | } |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 2100 | |
| 2101 | /* Some data were injected in the buffer, notify the stream |
| 2102 | * interface. |
| 2103 | */ |
| 2104 | if (!channel_is_empty(si_ic(si))) |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 2105 | si_update(si); |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 2106 | |
| 2107 | /* If write notifications are registered, we considers we want |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 2108 | * to write, so we clear the blocking flag. |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 2109 | */ |
| 2110 | if (notification_registered(&appctx->ctx.hlua_cosocket.wake_on_write)) |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 2111 | si_rx_endp_more(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2112 | } |
| 2113 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2114 | /* This function is called when the "struct stream" is destroyed. |
| 2115 | * Remove the link from the object to this stream. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2116 | * Wake all the pending signals. |
| 2117 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 2118 | static void hlua_socket_release(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2119 | { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2120 | struct xref *peer; |
| 2121 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2122 | /* Remove my link in the original object. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2123 | peer = xref_get_peer_and_lock(&appctx->ctx.hlua_cosocket.xref); |
| 2124 | if (peer) |
| 2125 | xref_disconnect(&appctx->ctx.hlua_cosocket.xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2126 | |
| 2127 | /* Wake all the task waiting for me. */ |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 2128 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 2129 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2130 | } |
| 2131 | |
| 2132 | /* If the garbage collectio of the object is launch, nobody |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2133 | * uses this object. If the stream does not exists, just quit. |
| 2134 | * Send the shutdown signal to the stream. In some cases, |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2135 | * pending signal can rest in the read and write lists. destroy |
| 2136 | * it. |
| 2137 | */ |
| 2138 | __LJMP static int hlua_socket_gc(lua_State *L) |
| 2139 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2140 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2141 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2142 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2143 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2144 | MAY_LJMP(check_args(L, 1, "__gc")); |
| 2145 | |
| 2146 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2147 | peer = xref_get_peer_and_lock(&socket->xref); |
| 2148 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2149 | return 0; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2150 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2151 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2152 | /* Set the flag which destroy the session. */ |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 2153 | appctx->ctx.hlua_cosocket.die = 1; |
| 2154 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2155 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2156 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2157 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2158 | return 0; |
| 2159 | } |
| 2160 | |
| 2161 | /* The close function send shutdown signal and break the |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2162 | * links between the stream and the object. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2163 | */ |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 2164 | __LJMP static int hlua_socket_close_helper(lua_State *L) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2165 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2166 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2167 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2168 | struct xref *peer; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2169 | struct hlua *hlua; |
| 2170 | |
| 2171 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 2172 | hlua = hlua_gethlua(L); |
| 2173 | if (!hlua) |
| 2174 | return 0; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2175 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2176 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2177 | |
| 2178 | /* Check if we run on the same thread than the xreator thread. |
| 2179 | * We cannot access to the socket if the thread is different. |
| 2180 | */ |
| 2181 | if (socket->tid != tid) |
| 2182 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2183 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2184 | peer = xref_get_peer_and_lock(&socket->xref); |
| 2185 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2186 | return 0; |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 2187 | |
| 2188 | hlua->gc_count--; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2189 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2190 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2191 | /* Set the flag which destroy the session. */ |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 2192 | appctx->ctx.hlua_cosocket.die = 1; |
| 2193 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2194 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2195 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2196 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2197 | return 0; |
| 2198 | } |
| 2199 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 2200 | /* The close function calls close_helper. |
| 2201 | */ |
| 2202 | __LJMP static int hlua_socket_close(lua_State *L) |
| 2203 | { |
| 2204 | MAY_LJMP(check_args(L, 1, "close")); |
| 2205 | return hlua_socket_close_helper(L); |
| 2206 | } |
| 2207 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2208 | /* This Lua function assumes that the stack contain three parameters. |
| 2209 | * 1 - USERDATA containing a struct socket |
| 2210 | * 2 - INTEGER with values of the macro defined below |
| 2211 | * If the integer is -1, we must read at most one line. |
| 2212 | * If the integer is -2, we ust read all the data until the |
| 2213 | * end of the stream. |
| 2214 | * If the integer is positive value, we must read a number of |
| 2215 | * bytes corresponding to this value. |
| 2216 | */ |
| 2217 | #define HLSR_READ_LINE (-1) |
| 2218 | #define HLSR_READ_ALL (-2) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2219 | __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] | 2220 | { |
| 2221 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2222 | int wanted = lua_tointeger(L, 2); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2223 | struct hlua *hlua; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2224 | struct appctx *appctx; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2225 | size_t len; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2226 | int nblk; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 2227 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2228 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 2229 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2230 | size_t len2; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 2231 | int skip_at_end = 0; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 2232 | struct channel *oc; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2233 | struct stream_interface *si; |
| 2234 | struct stream *s; |
| 2235 | struct xref *peer; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 2236 | int missing_bytes; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2237 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2238 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 2239 | hlua = hlua_gethlua(L); |
| 2240 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2241 | /* Check if this lua stack is schedulable. */ |
| 2242 | if (!hlua || !hlua->task) |
| 2243 | WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in " |
| 2244 | "'frontend', 'backend' or 'task'")); |
| 2245 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2246 | /* Check if we run on the same thread than the xreator thread. |
| 2247 | * We cannot access to the socket if the thread is different. |
| 2248 | */ |
| 2249 | if (socket->tid != tid) |
| 2250 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2251 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2252 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2253 | peer = xref_get_peer_and_lock(&socket->xref); |
| 2254 | if (!peer) |
| 2255 | goto no_peer; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2256 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2257 | si = appctx->owner; |
| 2258 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2259 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2260 | oc = &s->res; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2261 | if (wanted == HLSR_READ_LINE) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2262 | /* Read line. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2263 | nblk = co_getline_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2264 | if (nblk < 0) /* Connection close. */ |
| 2265 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2266 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2267 | goto connection_empty; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 2268 | |
| 2269 | /* remove final \r\n. */ |
| 2270 | if (nblk == 1) { |
| 2271 | if (blk1[len1-1] == '\n') { |
| 2272 | len1--; |
| 2273 | skip_at_end++; |
| 2274 | if (blk1[len1-1] == '\r') { |
| 2275 | len1--; |
| 2276 | skip_at_end++; |
| 2277 | } |
| 2278 | } |
| 2279 | } |
| 2280 | else { |
| 2281 | if (blk2[len2-1] == '\n') { |
| 2282 | len2--; |
| 2283 | skip_at_end++; |
| 2284 | if (blk2[len2-1] == '\r') { |
| 2285 | len2--; |
| 2286 | skip_at_end++; |
| 2287 | } |
| 2288 | } |
| 2289 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2290 | } |
| 2291 | |
| 2292 | else if (wanted == HLSR_READ_ALL) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2293 | /* Read all the available data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2294 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2295 | if (nblk < 0) /* Connection close. */ |
| 2296 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2297 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2298 | goto connection_empty; |
| 2299 | } |
| 2300 | |
| 2301 | else { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2302 | /* Read a block of data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2303 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2304 | if (nblk < 0) /* Connection close. */ |
| 2305 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2306 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2307 | goto connection_empty; |
| 2308 | |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 2309 | missing_bytes = wanted - socket->b.n; |
| 2310 | if (len1 > missing_bytes) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2311 | nblk = 1; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 2312 | len1 = missing_bytes; |
| 2313 | } if (nblk == 2 && len1 + len2 > missing_bytes) |
| 2314 | len2 = missing_bytes - len1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2315 | } |
| 2316 | |
| 2317 | len = len1; |
| 2318 | |
| 2319 | luaL_addlstring(&socket->b, blk1, len1); |
| 2320 | if (nblk == 2) { |
| 2321 | len += len2; |
| 2322 | luaL_addlstring(&socket->b, blk2, len2); |
| 2323 | } |
| 2324 | |
| 2325 | /* Consume data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2326 | co_skip(oc, len + skip_at_end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2327 | |
| 2328 | /* Don't wait anything. */ |
Christopher Faulet | 174144c | 2024-02-16 15:33:44 +0100 | [diff] [blame] | 2329 | si_want_get(si); |
Thierry FOURNIER | 7e4ee47 | 2018-05-25 15:03:50 +0200 | [diff] [blame] | 2330 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2331 | |
| 2332 | /* If the pattern reclaim to read all the data |
| 2333 | * in the connection, got out. |
| 2334 | */ |
| 2335 | if (wanted == HLSR_READ_ALL) |
| 2336 | goto connection_empty; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 2337 | else if (wanted >= 0 && socket->b.n < wanted) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2338 | goto connection_empty; |
| 2339 | |
| 2340 | /* Return result. */ |
| 2341 | luaL_pushresult(&socket->b); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2342 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2343 | return 1; |
| 2344 | |
| 2345 | connection_closed: |
| 2346 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2347 | xref_unlock(&socket->xref, peer); |
| 2348 | |
| 2349 | no_peer: |
| 2350 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2351 | /* If the buffer containds data. */ |
| 2352 | if (socket->b.n > 0) { |
| 2353 | luaL_pushresult(&socket->b); |
| 2354 | return 1; |
| 2355 | } |
| 2356 | lua_pushnil(L); |
| 2357 | lua_pushstring(L, "connection closed."); |
| 2358 | return 2; |
| 2359 | |
| 2360 | connection_empty: |
| 2361 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2362 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_read, hlua->task)) { |
| 2363 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2364 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2365 | } |
| 2366 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2367 | 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] | 2368 | return 0; |
| 2369 | } |
| 2370 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2371 | /* This Lua function gets two parameters. The first one can be string |
| 2372 | * or a number. If the string is "*l", the user requires one line. If |
| 2373 | * 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] | 2374 | * If the value is a number, the user require a number of bytes equal |
| 2375 | * to the value. The default value is "*l" (a line). |
| 2376 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2377 | * This parameter with a variable type is converted in integer. This |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2378 | * integer takes this values: |
| 2379 | * -1 : read a line |
| 2380 | * -2 : read all the stream |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2381 | * >0 : amount of bytes. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2382 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2383 | * The second parameter is optional. It contains a string that must be |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2384 | * concatenated with the read data. |
| 2385 | */ |
| 2386 | __LJMP static int hlua_socket_receive(struct lua_State *L) |
| 2387 | { |
| 2388 | int wanted = HLSR_READ_LINE; |
| 2389 | const char *pattern; |
Christopher Faulet | c31b200 | 2021-05-03 10:11:13 +0200 | [diff] [blame] | 2390 | int lastarg, type; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2391 | char *error; |
| 2392 | size_t len; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2393 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2394 | |
| 2395 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 2396 | WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments.")); |
| 2397 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2398 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2399 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2400 | /* Check if we run on the same thread than the xreator thread. |
| 2401 | * We cannot access to the socket if the thread is different. |
| 2402 | */ |
| 2403 | if (socket->tid != tid) |
| 2404 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2405 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2406 | /* check for pattern. */ |
| 2407 | if (lua_gettop(L) >= 2) { |
| 2408 | type = lua_type(L, 2); |
| 2409 | if (type == LUA_TSTRING) { |
| 2410 | pattern = lua_tostring(L, 2); |
| 2411 | if (strcmp(pattern, "*a") == 0) |
| 2412 | wanted = HLSR_READ_ALL; |
| 2413 | else if (strcmp(pattern, "*l") == 0) |
| 2414 | wanted = HLSR_READ_LINE; |
| 2415 | else { |
| 2416 | wanted = strtoll(pattern, &error, 10); |
| 2417 | if (*error != '\0') |
| 2418 | WILL_LJMP(luaL_error(L, "Unsupported pattern.")); |
| 2419 | } |
| 2420 | } |
| 2421 | else if (type == LUA_TNUMBER) { |
| 2422 | wanted = lua_tointeger(L, 2); |
| 2423 | if (wanted < 0) |
| 2424 | WILL_LJMP(luaL_error(L, "Unsupported size.")); |
| 2425 | } |
| 2426 | } |
| 2427 | |
| 2428 | /* Set pattern. */ |
| 2429 | lua_pushinteger(L, wanted); |
Tim Duesterhus | c6e377e | 2018-01-04 19:32:13 +0100 | [diff] [blame] | 2430 | |
| 2431 | /* Check if we would replace the top by itself. */ |
| 2432 | if (lua_gettop(L) != 2) |
| 2433 | lua_replace(L, 2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2434 | |
Christopher Faulet | c31b200 | 2021-05-03 10:11:13 +0200 | [diff] [blame] | 2435 | /* Save index of the top of the stack because since buffers are used, it |
| 2436 | * may change |
| 2437 | */ |
| 2438 | lastarg = lua_gettop(L); |
| 2439 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2440 | /* init buffer, and fill it with prefix. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2441 | luaL_buffinit(L, &socket->b); |
| 2442 | |
| 2443 | /* Check prefix. */ |
Christopher Faulet | c31b200 | 2021-05-03 10:11:13 +0200 | [diff] [blame] | 2444 | if (lastarg >= 3) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2445 | if (lua_type(L, 3) != LUA_TSTRING) |
| 2446 | WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix")); |
| 2447 | pattern = lua_tolstring(L, 3, &len); |
| 2448 | luaL_addlstring(&socket->b, pattern, len); |
| 2449 | } |
| 2450 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2451 | return __LJMP(hlua_socket_receive_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2452 | } |
| 2453 | |
| 2454 | /* Write the Lua input string in the output buffer. |
Mark Lakes | 22154b4 | 2018-01-29 14:38:40 -0800 | [diff] [blame] | 2455 | * This function returns a yield if no space is available. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2456 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2457 | 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] | 2458 | { |
| 2459 | struct hlua_socket *socket; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2460 | struct hlua *hlua; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2461 | struct appctx *appctx; |
| 2462 | size_t buf_len; |
| 2463 | const char *buf; |
| 2464 | int len; |
| 2465 | int send_len; |
| 2466 | int sent; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2467 | struct xref *peer; |
| 2468 | struct stream_interface *si; |
| 2469 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2470 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2471 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 2472 | hlua = hlua_gethlua(L); |
| 2473 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2474 | /* Check if this lua stack is schedulable. */ |
| 2475 | if (!hlua || !hlua->task) |
| 2476 | WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in " |
| 2477 | "'frontend', 'backend' or 'task'")); |
| 2478 | |
| 2479 | /* Get object */ |
| 2480 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2481 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2482 | sent = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2483 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2484 | /* Check if we run on the same thread than the xreator thread. |
| 2485 | * We cannot access to the socket if the thread is different. |
| 2486 | */ |
| 2487 | if (socket->tid != tid) |
| 2488 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2489 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2490 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2491 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2492 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2493 | lua_pushinteger(L, -1); |
| 2494 | return 1; |
| 2495 | } |
| 2496 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2497 | si = appctx->owner; |
| 2498 | s = si_strm(si); |
| 2499 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2500 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2501 | if (channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2502 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2503 | lua_pushinteger(L, -1); |
| 2504 | return 1; |
| 2505 | } |
| 2506 | |
| 2507 | /* Update the input buffer data. */ |
| 2508 | buf += sent; |
| 2509 | send_len = buf_len - sent; |
| 2510 | |
| 2511 | /* All the data are sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2512 | if (sent >= buf_len) { |
| 2513 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2514 | return 1; /* Implicitly return the length sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2515 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2516 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2517 | /* Check if the buffer is available because HAProxy doesn't allocate |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 2518 | * the request buffer if its not required. |
| 2519 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2520 | if (s->req.buf.size == 0) { |
Willy Tarreau | 581abd3 | 2018-10-25 10:21:41 +0200 | [diff] [blame] | 2521 | if (!si_alloc_ibuf(si, &appctx->buffer_wait)) |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2522 | goto hlua_socket_write_yield_return; |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 2523 | } |
| 2524 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2525 | /* Check for available space. */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2526 | len = b_room(&s->req.buf); |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2527 | if (len <= 0) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2528 | goto hlua_socket_write_yield_return; |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2529 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2530 | |
| 2531 | /* send data */ |
| 2532 | if (len < send_len) |
| 2533 | send_len = len; |
Thierry FOURNIER | 66b8919 | 2018-05-27 01:14:47 +0200 | [diff] [blame] | 2534 | len = ci_putblk(&s->req, buf, send_len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2535 | |
| 2536 | /* "Not enough space" (-1), "Buffer too little to contain |
| 2537 | * the data" (-2) are not expected because the available length |
| 2538 | * is tested. |
| 2539 | * Other unknown error are also not expected. |
| 2540 | */ |
| 2541 | if (len <= 0) { |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2542 | if (len == -1) |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2543 | s->req.flags |= CF_WAKE_WRITE; |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2544 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 2545 | MAY_LJMP(hlua_socket_close_helper(L)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2546 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2547 | lua_pushinteger(L, -1); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2548 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2549 | return 1; |
| 2550 | } |
| 2551 | |
| 2552 | /* update buffers. */ |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 2553 | appctx_wakeup(appctx); |
Willy Tarreau | de70fa1 | 2015-09-26 11:25:05 +0200 | [diff] [blame] | 2554 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2555 | s->req.rex = TICK_ETERNITY; |
| 2556 | s->res.wex = TICK_ETERNITY; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2557 | |
| 2558 | /* Update length sent. */ |
| 2559 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2560 | lua_pushinteger(L, sent + len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2561 | |
| 2562 | /* All the data buffer is sent ? */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2563 | if (sent + len >= buf_len) { |
| 2564 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2565 | return 1; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2566 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2567 | |
| 2568 | hlua_socket_write_yield_return: |
Thierry FOURNIER | ba42fcd | 2018-05-27 00:59:48 +0200 | [diff] [blame] | 2569 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2570 | xref_unlock(&socket->xref, peer); |
| 2571 | WILL_LJMP(luaL_error(L, "out of memory")); |
| 2572 | } |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2573 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2574 | 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] | 2575 | return 0; |
| 2576 | } |
| 2577 | |
| 2578 | /* This function initiate the send of data. It just check the input |
| 2579 | * parameters and push an integer in the Lua stack that contain the |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2580 | * 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] | 2581 | * "hlua_socket_write_yield" that can yield. |
| 2582 | * |
| 2583 | * The Lua function gets between 3 and 4 parameters. The first one is |
| 2584 | * the associated object. The second is a string buffer. The third is |
| 2585 | * a facultative integer that represents where is the buffer position |
| 2586 | * of the start of the data that can send. The first byte is the |
| 2587 | * position "1". The default value is "1". The fourth argument is a |
| 2588 | * facultative integer that represents where is the buffer position |
| 2589 | * of the end of the data that can send. The default is the last byte. |
| 2590 | */ |
| 2591 | static int hlua_socket_send(struct lua_State *L) |
| 2592 | { |
| 2593 | int i; |
| 2594 | int j; |
| 2595 | const char *buf; |
| 2596 | size_t buf_len; |
| 2597 | |
| 2598 | /* Check number of arguments. */ |
| 2599 | if (lua_gettop(L) < 2 || lua_gettop(L) > 4) |
| 2600 | WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments")); |
| 2601 | |
| 2602 | /* Get the string. */ |
| 2603 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
| 2604 | |
| 2605 | /* Get and check j. */ |
| 2606 | if (lua_gettop(L) == 4) { |
| 2607 | j = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 2608 | if (j < 0) |
| 2609 | j = buf_len + j + 1; |
| 2610 | if (j > buf_len) |
| 2611 | j = buf_len + 1; |
| 2612 | lua_pop(L, 1); |
| 2613 | } |
| 2614 | else |
| 2615 | j = buf_len; |
| 2616 | |
| 2617 | /* Get and check i. */ |
| 2618 | if (lua_gettop(L) == 3) { |
| 2619 | i = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2620 | if (i < 0) |
| 2621 | i = buf_len + i + 1; |
| 2622 | if (i > buf_len) |
| 2623 | i = buf_len + 1; |
| 2624 | lua_pop(L, 1); |
| 2625 | } else |
| 2626 | i = 1; |
| 2627 | |
| 2628 | /* Check bth i and j. */ |
| 2629 | if (i > j) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2630 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2631 | return 1; |
| 2632 | } |
| 2633 | if (i == 0 && j == 0) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2634 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2635 | return 1; |
| 2636 | } |
| 2637 | if (i == 0) |
| 2638 | i = 1; |
| 2639 | if (j == 0) |
| 2640 | j = 1; |
| 2641 | |
| 2642 | /* Pop the string. */ |
| 2643 | lua_pop(L, 1); |
| 2644 | |
| 2645 | /* Update the buffer length. */ |
| 2646 | buf += i - 1; |
| 2647 | buf_len = j - i + 1; |
| 2648 | lua_pushlstring(L, buf, buf_len); |
| 2649 | |
| 2650 | /* This unsigned is used to remember the amount of sent data. */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2651 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2652 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2653 | return MAY_LJMP(hlua_socket_write_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2654 | } |
| 2655 | |
Willy Tarreau | 22b0a68 | 2015-06-17 19:43:49 +0200 | [diff] [blame] | 2656 | #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] | 2657 | __LJMP static inline int hlua_socket_info(struct lua_State *L, struct sockaddr_storage *addr) |
| 2658 | { |
| 2659 | static char buffer[SOCKET_INFO_MAX_LEN]; |
| 2660 | int ret; |
| 2661 | int len; |
| 2662 | char *p; |
| 2663 | |
| 2664 | ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1); |
| 2665 | if (ret <= 0) { |
| 2666 | lua_pushnil(L); |
| 2667 | return 1; |
| 2668 | } |
| 2669 | |
| 2670 | if (ret == AF_UNIX) { |
| 2671 | lua_pushstring(L, buffer+1); |
| 2672 | return 1; |
| 2673 | } |
| 2674 | else if (ret == AF_INET6) { |
| 2675 | buffer[0] = '['; |
| 2676 | len = strlen(buffer); |
| 2677 | buffer[len] = ']'; |
| 2678 | len++; |
| 2679 | buffer[len] = ':'; |
| 2680 | len++; |
| 2681 | p = buffer; |
| 2682 | } |
| 2683 | else if (ret == AF_INET) { |
| 2684 | p = buffer + 1; |
| 2685 | len = strlen(p); |
| 2686 | p[len] = ':'; |
| 2687 | len++; |
| 2688 | } |
| 2689 | else { |
| 2690 | lua_pushnil(L); |
| 2691 | return 1; |
| 2692 | } |
| 2693 | |
| 2694 | if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) { |
| 2695 | lua_pushnil(L); |
| 2696 | return 1; |
| 2697 | } |
| 2698 | |
| 2699 | lua_pushstring(L, p); |
| 2700 | return 1; |
| 2701 | } |
| 2702 | |
| 2703 | /* Returns information about the peer of the connection. */ |
| 2704 | __LJMP static int hlua_socket_getpeername(struct lua_State *L) |
| 2705 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2706 | struct hlua_socket *socket; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2707 | struct xref *peer; |
| 2708 | struct appctx *appctx; |
| 2709 | struct stream_interface *si; |
| 2710 | struct stream *s; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2711 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2712 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2713 | MAY_LJMP(check_args(L, 1, "getpeername")); |
| 2714 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2715 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2716 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2717 | /* Check if we run on the same thread than the xreator thread. |
| 2718 | * We cannot access to the socket if the thread is different. |
| 2719 | */ |
| 2720 | if (socket->tid != tid) |
| 2721 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2722 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2723 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2724 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2725 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2726 | lua_pushnil(L); |
| 2727 | return 1; |
| 2728 | } |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2729 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2730 | si = appctx->owner; |
| 2731 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2732 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2733 | if (!s->target_addr) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2734 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2735 | lua_pushnil(L); |
| 2736 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2737 | } |
| 2738 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2739 | ret = MAY_LJMP(hlua_socket_info(L, s->target_addr)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2740 | xref_unlock(&socket->xref, peer); |
| 2741 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2742 | } |
| 2743 | |
| 2744 | /* Returns information about my connection side. */ |
| 2745 | static int hlua_socket_getsockname(struct lua_State *L) |
| 2746 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2747 | struct hlua_socket *socket; |
| 2748 | struct connection *conn; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2749 | struct appctx *appctx; |
| 2750 | struct xref *peer; |
| 2751 | struct stream_interface *si; |
| 2752 | struct stream *s; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2753 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2754 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2755 | MAY_LJMP(check_args(L, 1, "getsockname")); |
| 2756 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2757 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
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 | */ |
| 2762 | if (socket->tid != tid) |
| 2763 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2764 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2765 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2766 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2767 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2768 | lua_pushnil(L); |
| 2769 | return 1; |
| 2770 | } |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2771 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2772 | si = appctx->owner; |
| 2773 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2774 | |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 2775 | conn = cs_conn(objt_cs(s->si[1].end)); |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2776 | if (!conn || !conn_get_src(conn)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2777 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2778 | lua_pushnil(L); |
| 2779 | return 1; |
| 2780 | } |
| 2781 | |
Willy Tarreau | 9da9a6f | 2019-07-17 14:49:44 +0200 | [diff] [blame] | 2782 | ret = hlua_socket_info(L, conn->src); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2783 | xref_unlock(&socket->xref, peer); |
| 2784 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2785 | } |
| 2786 | |
| 2787 | /* This struct define the applet. */ |
Willy Tarreau | 3057645 | 2015-04-13 13:50:30 +0200 | [diff] [blame] | 2788 | static struct applet update_applet = { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2789 | .obj_type = OBJ_TYPE_APPLET, |
| 2790 | .name = "<LUA_TCP>", |
| 2791 | .fct = hlua_socket_handler, |
| 2792 | .release = hlua_socket_release, |
| 2793 | }; |
| 2794 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2795 | __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] | 2796 | { |
| 2797 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2798 | struct hlua *hlua; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2799 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2800 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2801 | struct stream_interface *si; |
| 2802 | struct stream *s; |
| 2803 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2804 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 2805 | hlua = hlua_gethlua(L); |
| 2806 | if (!hlua) |
| 2807 | return 0; |
| 2808 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2809 | /* Check if we run on the same thread than the xreator thread. |
| 2810 | * We cannot access to the socket if the thread is different. |
| 2811 | */ |
| 2812 | if (socket->tid != tid) |
| 2813 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2814 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2815 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2816 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2817 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2818 | lua_pushnil(L); |
| 2819 | lua_pushstring(L, "Can't connect"); |
| 2820 | return 2; |
| 2821 | } |
| 2822 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2823 | si = appctx->owner; |
| 2824 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2825 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2826 | /* Check if we run on the same thread than the xreator thread. |
| 2827 | * We cannot access to the socket if the thread is different. |
| 2828 | */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2829 | if (socket->tid != tid) { |
| 2830 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2831 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2832 | } |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2833 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2834 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2835 | if (!hlua || channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2836 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2837 | lua_pushnil(L); |
| 2838 | lua_pushstring(L, "Can't connect"); |
| 2839 | return 2; |
| 2840 | } |
| 2841 | |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 2842 | appctx = __objt_appctx(s->si[0].end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2843 | |
| 2844 | /* Check for connection established. */ |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2845 | if (appctx->ctx.hlua_cosocket.connected) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2846 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2847 | lua_pushinteger(L, 1); |
| 2848 | return 1; |
| 2849 | } |
| 2850 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2851 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2852 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2853 | WILL_LJMP(luaL_error(L, "out of memory error")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2854 | } |
| 2855 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2856 | 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] | 2857 | return 0; |
| 2858 | } |
| 2859 | |
| 2860 | /* This function fail or initite the connection. */ |
| 2861 | __LJMP static int hlua_socket_connect(struct lua_State *L) |
| 2862 | { |
| 2863 | struct hlua_socket *socket; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2864 | int port = -1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2865 | const char *ip; |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2866 | struct hlua *hlua; |
| 2867 | struct appctx *appctx; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2868 | int low, high; |
| 2869 | struct sockaddr_storage *addr; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2870 | struct xref *peer; |
| 2871 | struct stream_interface *si; |
| 2872 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2873 | |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2874 | if (lua_gettop(L) < 2) |
| 2875 | WILL_LJMP(luaL_error(L, "connect: need at least 2 arguments")); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2876 | |
| 2877 | /* Get args. */ |
| 2878 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2879 | |
| 2880 | /* Check if we run on the same thread than the xreator thread. |
| 2881 | * We cannot access to the socket if the thread is different. |
| 2882 | */ |
| 2883 | if (socket->tid != tid) |
| 2884 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2885 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2886 | ip = MAY_LJMP(luaL_checkstring(L, 2)); |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 2887 | if (lua_gettop(L) >= 3) { |
| 2888 | luaL_Buffer b; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2889 | port = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2890 | |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 2891 | /* Force the ip to end with a colon, to support IPv6 addresses |
| 2892 | * that are not enclosed within square brackets. |
| 2893 | */ |
| 2894 | if (port > 0) { |
| 2895 | luaL_buffinit(L, &b); |
| 2896 | luaL_addstring(&b, ip); |
| 2897 | luaL_addchar(&b, ':'); |
| 2898 | luaL_pushresult(&b); |
| 2899 | ip = lua_tolstring(L, lua_gettop(L), NULL); |
| 2900 | } |
| 2901 | } |
| 2902 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2903 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2904 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2905 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2906 | lua_pushnil(L); |
| 2907 | return 1; |
| 2908 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2909 | |
| 2910 | /* Parse ip address. */ |
Willy Tarreau | 5fc9328 | 2020-09-16 18:25:03 +0200 | [diff] [blame] | 2911 | 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] | 2912 | if (!addr) { |
| 2913 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2914 | WILL_LJMP(luaL_error(L, "connect: cannot parse destination address '%s'", ip)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2915 | } |
Willy Tarreau | 9da9a6f | 2019-07-17 14:49:44 +0200 | [diff] [blame] | 2916 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2917 | /* Set port. */ |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2918 | if (low == 0) { |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2919 | if (addr->ss_family == AF_INET) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2920 | if (port == -1) { |
| 2921 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2922 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2923 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2924 | ((struct sockaddr_in *)addr)->sin_port = htons(port); |
| 2925 | } else if (addr->ss_family == AF_INET6) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2926 | if (port == -1) { |
| 2927 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2928 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2929 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2930 | ((struct sockaddr_in6 *)addr)->sin6_port = htons(port); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2931 | } |
| 2932 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2933 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2934 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2935 | si = appctx->owner; |
| 2936 | s = si_strm(si); |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2937 | |
Willy Tarreau | 9b7587a | 2020-10-15 07:32:10 +0200 | [diff] [blame] | 2938 | if (!sockaddr_alloc(&s->target_addr, addr, sizeof(*addr))) { |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2939 | xref_unlock(&socket->xref, peer); |
| 2940 | WILL_LJMP(luaL_error(L, "connect: internal error")); |
| 2941 | } |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2942 | s->flags |= SF_ADDR_SET; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2943 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2944 | /* Get hlua struct, or NULL if we execute from main lua state */ |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2945 | hlua = hlua_gethlua(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2946 | if (!hlua) |
| 2947 | return 0; |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2948 | |
| 2949 | /* inform the stream that we want to be notified whenever the |
| 2950 | * connection completes. |
| 2951 | */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 2952 | si_cant_get(&s->si[0]); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 2953 | si_rx_endp_more(&s->si[0]); |
Thierry FOURNIER | 8c8fbbe | 2015-09-26 17:02:35 +0200 | [diff] [blame] | 2954 | appctx_wakeup(appctx); |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2955 | |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 2956 | hlua->gc_count++; |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 2957 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2958 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2959 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2960 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2961 | } |
| 2962 | xref_unlock(&socket->xref, peer); |
PiBa-NL | 706d5ee | 2018-05-05 23:51:42 +0200 | [diff] [blame] | 2963 | |
| 2964 | task_wakeup(s->task, TASK_WOKEN_INIT); |
| 2965 | /* Return yield waiting for connection. */ |
| 2966 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2967 | 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] | 2968 | |
| 2969 | return 0; |
| 2970 | } |
| 2971 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2972 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2973 | __LJMP static int hlua_socket_connect_ssl(struct lua_State *L) |
| 2974 | { |
| 2975 | struct hlua_socket *socket; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2976 | struct xref *peer; |
| 2977 | struct appctx *appctx; |
| 2978 | struct stream_interface *si; |
| 2979 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2980 | |
| 2981 | MAY_LJMP(check_args(L, 3, "connect_ssl")); |
| 2982 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2983 | |
| 2984 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2985 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2986 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2987 | lua_pushnil(L); |
| 2988 | return 1; |
| 2989 | } |
| 2990 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2991 | si = appctx->owner; |
| 2992 | s = si_strm(si); |
| 2993 | |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 2994 | s->target = &socket_ssl->obj_type; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2995 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2996 | return MAY_LJMP(hlua_socket_connect(L)); |
| 2997 | } |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2998 | #endif |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2999 | |
| 3000 | __LJMP static int hlua_socket_setoption(struct lua_State *L) |
| 3001 | { |
| 3002 | return 0; |
| 3003 | } |
| 3004 | |
| 3005 | __LJMP static int hlua_socket_settimeout(struct lua_State *L) |
| 3006 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3007 | struct hlua_socket *socket; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3008 | int tmout; |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 3009 | double dtmout; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 3010 | struct xref *peer; |
| 3011 | struct appctx *appctx; |
| 3012 | struct stream_interface *si; |
| 3013 | struct stream *s; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3014 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3015 | MAY_LJMP(check_args(L, 2, "settimeout")); |
| 3016 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3017 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 3018 | |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 3019 | /* convert the timeout to millis */ |
| 3020 | dtmout = MAY_LJMP(luaL_checknumber(L, 2)) * 1000; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3021 | |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 3022 | /* Check for negative values */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 3023 | if (dtmout < 0) |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 3024 | WILL_LJMP(luaL_error(L, "settimeout: cannot set negatives values")); |
| 3025 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 3026 | if (dtmout > INT_MAX) /* overflow check */ |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 3027 | 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] | 3028 | |
| 3029 | tmout = MS_TO_TICKS((int)dtmout); |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 3030 | if (tmout == 0) |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3031 | tmout++; /* very small timeouts are adjusted to a minimum of 1ms */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 3032 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 3033 | /* Check if we run on the same thread than the xreator thread. |
| 3034 | * We cannot access to the socket if the thread is different. |
| 3035 | */ |
| 3036 | if (socket->tid != tid) |
| 3037 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 3038 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 3039 | /* check for connection break. If some data were read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 3040 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 3041 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 3042 | hlua_pusherror(L, "socket: not yet initialised, you can't set timeouts."); |
| 3043 | WILL_LJMP(lua_error(L)); |
| 3044 | return 0; |
| 3045 | } |
| 3046 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 3047 | si = appctx->owner; |
| 3048 | s = si_strm(si); |
| 3049 | |
Cyril Bonté | 7bb6345 | 2018-08-17 23:51:02 +0200 | [diff] [blame] | 3050 | s->sess->fe->timeout.connect = tmout; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 3051 | s->req.rto = tmout; |
| 3052 | s->req.wto = tmout; |
| 3053 | s->res.rto = tmout; |
| 3054 | s->res.wto = tmout; |
Cyril Bonté | 7bb6345 | 2018-08-17 23:51:02 +0200 | [diff] [blame] | 3055 | s->req.rex = tick_add_ifset(now_ms, tmout); |
| 3056 | s->req.wex = tick_add_ifset(now_ms, tmout); |
| 3057 | s->res.rex = tick_add_ifset(now_ms, tmout); |
| 3058 | s->res.wex = tick_add_ifset(now_ms, tmout); |
| 3059 | |
| 3060 | s->task->expire = tick_add_ifset(now_ms, tmout); |
| 3061 | task_queue(s->task); |
| 3062 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 3063 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3064 | |
Thierry Fournier | e9636f1 | 2018-03-08 09:54:32 +0100 | [diff] [blame] | 3065 | lua_pushinteger(L, 1); |
Tim Duesterhus | 119a5f1 | 2018-01-06 19:16:25 +0100 | [diff] [blame] | 3066 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3067 | } |
| 3068 | |
| 3069 | __LJMP static int hlua_socket_new(lua_State *L) |
| 3070 | { |
| 3071 | struct hlua_socket *socket; |
| 3072 | struct appctx *appctx; |
Willy Tarreau | 15b5e14 | 2015-04-04 14:38:25 +0200 | [diff] [blame] | 3073 | struct session *sess; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 3074 | struct stream *strm; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3075 | |
| 3076 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 3077 | if (!lua_checkstack(L, 3)) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3078 | hlua_pusherror(L, "socket: full stack"); |
| 3079 | goto out_fail_conf; |
| 3080 | } |
| 3081 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 3082 | /* Create the object: obj[0] = userdata. */ |
| 3083 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3084 | socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket))); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 3085 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3086 | memset(socket, 0, sizeof(*socket)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 3087 | socket->tid = tid; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3088 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3089 | /* Check if the various memory pools are initialized. */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 3090 | if (!pool_head_stream || !pool_head_buffer) { |
Thierry FOURNIER | 4a6170c | 2015-03-09 17:07:10 +0100 | [diff] [blame] | 3091 | hlua_pusherror(L, "socket: uninitialized pools."); |
| 3092 | goto out_fail_conf; |
| 3093 | } |
| 3094 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3095 | /* Pop a class stream metatable and affect it to the userdata. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3096 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref); |
| 3097 | lua_setmetatable(L, -2); |
| 3098 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 3099 | /* Create the applet context */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 3100 | appctx = appctx_new(&update_applet, tid_bit); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 3101 | if (!appctx) { |
| 3102 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | feb7640 | 2015-04-03 14:10:06 +0200 | [diff] [blame] | 3103 | goto out_fail_conf; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 3104 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3105 | |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 3106 | appctx->ctx.hlua_cosocket.connected = 0; |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 3107 | appctx->ctx.hlua_cosocket.die = 0; |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 3108 | LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_write); |
| 3109 | LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_read); |
Willy Tarreau | b2bf833 | 2015-04-04 15:58:58 +0200 | [diff] [blame] | 3110 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 3111 | /* Now create a session, task and stream for this applet */ |
Amaury Denoyelle | 239fdbf | 2021-03-24 10:22:03 +0100 | [diff] [blame] | 3112 | sess = session_new(socket_proxy, NULL, &appctx->obj_type); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 3113 | if (!sess) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3114 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 3115 | goto out_fail_sess; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3116 | } |
| 3117 | |
Christopher Faulet | 26256f8 | 2020-09-14 11:40:13 +0200 | [diff] [blame] | 3118 | strm = stream_new(sess, &appctx->obj_type, &BUF_NULL); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 3119 | if (!strm) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3120 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 3121 | goto out_fail_stream; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3122 | } |
| 3123 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 3124 | /* Initialise cross reference between stream and Lua socket object. */ |
| 3125 | xref_create(&socket->xref, &appctx->ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3126 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3127 | /* Configure "right" stream interface. this "si" is used to connect |
| 3128 | * and retrieve data from the server. The connection is initialized |
| 3129 | * with the "struct server". |
| 3130 | */ |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 3131 | si_set_state(&strm->si[1], SI_ST_ASS); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3132 | |
| 3133 | /* Force destination server. */ |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 3134 | strm->flags |= SF_DIRECT | SF_ASSIGNED | SF_BE_ASSIGNED; |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 3135 | strm->target = &socket_tcp->obj_type; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3136 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3137 | return 1; |
| 3138 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 3139 | out_fail_stream: |
Willy Tarreau | 11c3624 | 2015-04-04 15:54:03 +0200 | [diff] [blame] | 3140 | session_free(sess); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 3141 | out_fail_sess: |
| 3142 | appctx_free(appctx); |
| 3143 | out_fail_conf: |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3144 | WILL_LJMP(lua_error(L)); |
| 3145 | return 0; |
| 3146 | } |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 3147 | |
| 3148 | /* |
| 3149 | * |
| 3150 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3151 | * Class Channel |
| 3152 | * |
| 3153 | * |
| 3154 | */ |
| 3155 | |
| 3156 | /* Returns the struct hlua_channel join to the class channel in the |
| 3157 | * stack entry "ud" or throws an argument error. |
| 3158 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3159 | __LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3160 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3161 | return MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3162 | } |
| 3163 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3164 | /* Pushes the channel onto the top of the stack. If the stask does not have a |
| 3165 | * free slots, the function fails and returns 0; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3166 | */ |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 3167 | static int hlua_channel_new(lua_State *L, struct channel *channel) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3168 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3169 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 3170 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3171 | return 0; |
| 3172 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 3173 | lua_newtable(L); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3174 | lua_pushlightuserdata(L, channel); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 3175 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3176 | |
| 3177 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 3178 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref); |
| 3179 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3180 | return 1; |
| 3181 | } |
| 3182 | |
| 3183 | /* Duplicate all the data present in the input channel and put it |
| 3184 | * in a string LUA variables. Returns -1 and push a nil value in |
| 3185 | * the stack if the channel is closed and all the data are consumed, |
| 3186 | * returns 0 if no data are available, otherwise it returns the length |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 3187 | * of the built string. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3188 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3189 | static inline int _hlua_channel_dup(struct channel *chn, lua_State *L) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3190 | { |
| 3191 | char *blk1; |
| 3192 | char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3193 | size_t len1; |
| 3194 | size_t len2; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3195 | int ret; |
| 3196 | luaL_Buffer b; |
| 3197 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3198 | ret = ci_getblk_nc(chn, &blk1, &len1, &blk2, &len2); |
Christopher Faulet | 055310f | 2021-08-05 11:58:37 +0200 | [diff] [blame] | 3199 | if (unlikely(ret <= 0)) { |
| 3200 | if (ret < 0 || HLUA_CANT_YIELD(hlua_gethlua(L))) { |
| 3201 | lua_pushnil(L); |
| 3202 | return -1; |
| 3203 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3204 | return 0; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3205 | } |
| 3206 | |
| 3207 | luaL_buffinit(L, &b); |
| 3208 | luaL_addlstring(&b, blk1, len1); |
| 3209 | if (unlikely(ret == 2)) |
| 3210 | luaL_addlstring(&b, blk2, len2); |
| 3211 | luaL_pushresult(&b); |
| 3212 | |
| 3213 | if (unlikely(ret == 2)) |
| 3214 | return len1 + len2; |
| 3215 | return len1; |
| 3216 | } |
| 3217 | |
| 3218 | /* "_hlua_channel_dup" wrapper. If no data are available, it returns |
| 3219 | * a yield. This function keep the data in the buffer. |
| 3220 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3221 | __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] | 3222 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3223 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3224 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3225 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3226 | |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 3227 | if (IS_HTX_STRM(chn_strm(chn))) { |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3228 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3229 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3230 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3231 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3232 | if (_hlua_channel_dup(chn, L) == 0) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3233 | 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] | 3234 | return 1; |
| 3235 | } |
| 3236 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3237 | /* Check arguments for the function "hlua_channel_dup_yield". */ |
| 3238 | __LJMP static int hlua_channel_dup(lua_State *L) |
| 3239 | { |
| 3240 | MAY_LJMP(check_args(L, 1, "dup")); |
| 3241 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3242 | return MAY_LJMP(hlua_channel_dup_yield(L, 0, 0)); |
| 3243 | } |
| 3244 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3245 | /* "_hlua_channel_dup" wrapper. If no data are available, it returns |
| 3246 | * a yield. This function consumes the data in the buffer. It returns |
| 3247 | * a string containing the data or a nil pointer if no data are available |
| 3248 | * and the channel is closed. |
| 3249 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3250 | __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] | 3251 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3252 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3253 | int ret; |
| 3254 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3255 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3256 | |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 3257 | if (IS_HTX_STRM(chn_strm(chn))) { |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3258 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3259 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3260 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3261 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3262 | ret = _hlua_channel_dup(chn, L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3263 | if (unlikely(ret == 0)) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3264 | 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] | 3265 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3266 | if (unlikely(ret == -1)) |
| 3267 | return 1; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3268 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3269 | b_sub(&chn->buf, ret); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3270 | return 1; |
| 3271 | } |
| 3272 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3273 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3274 | __LJMP static int hlua_channel_get(lua_State *L) |
| 3275 | { |
| 3276 | MAY_LJMP(check_args(L, 1, "get")); |
| 3277 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3278 | return MAY_LJMP(hlua_channel_get_yield(L, 0, 0)); |
| 3279 | } |
| 3280 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3281 | /* This functions consumes and returns one line. If the channel is closed, |
| 3282 | * 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] | 3283 | * 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] | 3284 | * value. |
| 3285 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3286 | __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] | 3287 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3288 | char *blk1; |
| 3289 | char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3290 | size_t len1; |
| 3291 | size_t len2; |
| 3292 | size_t len; |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3293 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3294 | int ret; |
| 3295 | luaL_Buffer b; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3296 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3297 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3298 | |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 3299 | if (IS_HTX_STRM(chn_strm(chn))) { |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3300 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3301 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3302 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3303 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3304 | ret = ci_getline_nc(chn, &blk1, &len1, &blk2, &len2); |
Christopher Faulet | 055310f | 2021-08-05 11:58:37 +0200 | [diff] [blame] | 3305 | if (ret == 0) { |
| 3306 | if (HLUA_CANT_YIELD(hlua_gethlua(L))) { |
| 3307 | _hlua_channel_dup(chn, L); |
| 3308 | return 1; |
| 3309 | } |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3310 | 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] | 3311 | } |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3312 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3313 | if (ret == -1) { |
| 3314 | lua_pushnil(L); |
| 3315 | return 1; |
| 3316 | } |
| 3317 | |
| 3318 | luaL_buffinit(L, &b); |
| 3319 | luaL_addlstring(&b, blk1, len1); |
| 3320 | len = len1; |
| 3321 | if (unlikely(ret == 2)) { |
| 3322 | luaL_addlstring(&b, blk2, len2); |
| 3323 | len += len2; |
| 3324 | } |
| 3325 | luaL_pushresult(&b); |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3326 | 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] | 3327 | return 1; |
| 3328 | } |
| 3329 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3330 | /* Check arguments for the function "hlua_channel_getline_yield". */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3331 | __LJMP static int hlua_channel_getline(lua_State *L) |
| 3332 | { |
| 3333 | MAY_LJMP(check_args(L, 1, "getline")); |
| 3334 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3335 | return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0)); |
| 3336 | } |
| 3337 | |
Christopher Faulet | 3b7bc3e | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3338 | /* This function takes a string as argument, and append it at the input side of |
| 3339 | * channel. If data cannot be copied, because there is not enough space to do so |
| 3340 | * or because the channel is closed, it returns -1. Otherwise, it returns the |
| 3341 | * amount of data written (the string length). This function does not yield. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3342 | */ |
Christopher Faulet | 3b7bc3e | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3343 | __LJMP static int hlua_channel_append(lua_State *L) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3344 | { |
Christopher Faulet | 3b7bc3e | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3345 | struct channel *chn; |
| 3346 | const char *str; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3347 | size_t len; |
Christopher Faulet | 3b7bc3e | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3348 | |
| 3349 | MAY_LJMP(check_args(L, 2, "append")); |
| 3350 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3351 | str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3352 | |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 3353 | if (IS_HTX_STRM(chn_strm(chn))) { |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3354 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3355 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3356 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3357 | |
Christopher Faulet | 3b7bc3e | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3358 | if (len > c_room(chn) || ci_putblk(chn, str, len) < 0) |
| 3359 | lua_pushinteger(L, -1); |
| 3360 | else |
| 3361 | lua_pushinteger(L, len); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3362 | return 1; |
| 3363 | } |
| 3364 | |
Christopher Faulet | 3b7bc3e | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3365 | /* The function replaces input data from the channel by the string passed as |
| 3366 | * argument. Before clearing the buffer, we take care the copy will be |
| 3367 | * possible. It returns the amount of data written (the string length) on |
| 3368 | * 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] | 3369 | */ |
| 3370 | __LJMP static int hlua_channel_set(lua_State *L) |
| 3371 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3372 | struct channel *chn; |
Christopher Faulet | 3b7bc3e | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3373 | const char *str; |
| 3374 | size_t len; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3375 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3376 | MAY_LJMP(check_args(L, 2, "set")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3377 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 3b7bc3e | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3378 | str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3379 | |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 3380 | if (IS_HTX_STRM(chn_strm(chn))) { |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3381 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3382 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3383 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3384 | |
Christopher Faulet | 3b7bc3e | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3385 | /* Be sure we can copied the string once input data will be removed. */ |
| 3386 | if (len > ci_data(chn) || channel_input_closed(chn)) |
| 3387 | lua_pushinteger(L, -1); |
| 3388 | else { |
| 3389 | b_set_data(&chn->buf, co_data(chn)); |
| 3390 | if (ci_putblk(chn, str, len) < 0) |
| 3391 | lua_pushinteger(L, -1); |
| 3392 | else |
| 3393 | lua_pushinteger(L, len); |
| 3394 | } |
| 3395 | return -1; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3396 | } |
| 3397 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3398 | /* Append data in the output side of the buffer. This data is immediately |
| 3399 | * sent. The function returns the amount of data written. If the buffer |
| 3400 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3401 | * if the channel is closed. |
| 3402 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3403 | __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] | 3404 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3405 | struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3406 | size_t len; |
| 3407 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3408 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3409 | int max; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 3410 | struct hlua *hlua; |
| 3411 | |
| 3412 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 3413 | hlua = hlua_gethlua(L); |
| 3414 | if (!hlua) { |
| 3415 | lua_pushnil(L); |
| 3416 | return 1; |
| 3417 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3418 | |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 3419 | if (IS_HTX_STRM(chn_strm(chn))) { |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3420 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3421 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3422 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3423 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3424 | if (unlikely(channel_output_closed(chn))) { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3425 | lua_pushinteger(L, -1); |
| 3426 | return 1; |
| 3427 | } |
| 3428 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3429 | /* Check if the buffer is available because HAProxy doesn't allocate |
Thierry FOURNIER | 3e3a608 | 2015-03-05 17:06:12 +0100 | [diff] [blame] | 3430 | * the request buffer if its not required. |
| 3431 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3432 | if (chn->buf.size == 0) { |
Christopher Faulet | 055310f | 2021-08-05 11:58:37 +0200 | [diff] [blame] | 3433 | if (HLUA_CANT_YIELD(hlua_gethlua(L))) |
| 3434 | return 1; |
Willy Tarreau | 4b962a4 | 2018-11-15 11:03:21 +0100 | [diff] [blame] | 3435 | si_rx_buff_blk(chn_prod(chn)); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3436 | 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] | 3437 | } |
| 3438 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3439 | /* The written data will be immediately sent, so we can check |
| 3440 | * the available space without taking in account the reserve. |
| 3441 | * The reserve is guaranteed for the processing of incoming |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3442 | * data, because the buffer will be flushed. |
| 3443 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3444 | max = b_room(&chn->buf); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3445 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3446 | /* If there is no space available, and the output buffer is empty. |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3447 | * in this case, we cannot add more data, so we cannot yield, |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3448 | * we return the amount of copied data. |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3449 | */ |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3450 | if (max == 0 && co_data(chn) == 0) |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3451 | return 1; |
| 3452 | |
| 3453 | /* Adjust the real required length. */ |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3454 | if (max > len - l) |
| 3455 | max = len - l; |
| 3456 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3457 | /* The buffer available size may be not contiguous. This test |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3458 | * detects a non contiguous buffer and realign it. |
| 3459 | */ |
Willy Tarreau | 3f67999 | 2018-06-15 15:06:42 +0200 | [diff] [blame] | 3460 | if (ci_space_for_replace(chn) < max) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3461 | channel_slow_realign(chn, trash.area); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3462 | |
| 3463 | /* Copy input data in the buffer. */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3464 | 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] | 3465 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3466 | /* buffer replace considers that the input part is filled. |
| 3467 | * so, I must forward these new data in the output part. |
| 3468 | */ |
Willy Tarreau | bcbd393 | 2018-06-06 07:13:22 +0200 | [diff] [blame] | 3469 | c_adv(chn, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3470 | |
| 3471 | l += max; |
| 3472 | lua_pop(L, 1); |
| 3473 | lua_pushinteger(L, l); |
| 3474 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3475 | if (l < len) { |
Christopher Faulet | 055310f | 2021-08-05 11:58:37 +0200 | [diff] [blame] | 3476 | /* If there is no space available, and the output buffer is empty. |
| 3477 | * in this case, we cannot add more data, so we cannot yield, |
| 3478 | * we return the amount of copied data. |
| 3479 | */ |
| 3480 | max = b_room(&chn->buf); |
| 3481 | if ((max == 0 && co_data(chn) == 0) || HLUA_CANT_YIELD(hlua_gethlua(L))) |
| 3482 | return 1; |
| 3483 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3484 | /* If we are waiting for space in the response buffer, we |
| 3485 | * must set the flag WAKERESWR. This flag required the task |
| 3486 | * wake up if any activity is detected on the response buffer. |
| 3487 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3488 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3489 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3490 | else |
| 3491 | HLUA_SET_WAKEREQWR(hlua); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3492 | 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] | 3493 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3494 | |
| 3495 | return 1; |
| 3496 | } |
| 3497 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3498 | /* Just a wrapper of "_hlua_channel_send". This wrapper permits |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3499 | * yield the LUA process, and resume it without checking the |
| 3500 | * input arguments. |
| 3501 | */ |
| 3502 | __LJMP static int hlua_channel_send(lua_State *L) |
| 3503 | { |
| 3504 | MAY_LJMP(check_args(L, 2, "send")); |
| 3505 | lua_pushinteger(L, 0); |
| 3506 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3507 | return MAY_LJMP(hlua_channel_send_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3508 | } |
| 3509 | |
| 3510 | /* This function forward and amount of butes. The data pass from |
| 3511 | * the input side of the buffer to the output side, and can be |
| 3512 | * forwarded. This function never fails. |
| 3513 | * |
| 3514 | * The Lua function takes an amount of bytes to be forwarded in |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3515 | * input. It returns the number of bytes forwarded. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3516 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3517 | __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] | 3518 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3519 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3520 | int len; |
| 3521 | int l; |
| 3522 | int max; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 3523 | struct hlua *hlua; |
| 3524 | |
| 3525 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 3526 | hlua = hlua_gethlua(L); |
| 3527 | if (!hlua) |
| 3528 | return 1; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3529 | |
| 3530 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3531 | |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 3532 | if (IS_HTX_STRM(chn_strm(chn))) { |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3533 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3534 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3535 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3536 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3537 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3538 | l = MAY_LJMP(luaL_checkinteger(L, -1)); |
| 3539 | |
| 3540 | max = len - l; |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3541 | if (max > ci_data(chn)) |
| 3542 | max = ci_data(chn); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3543 | channel_forward(chn, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3544 | l += max; |
| 3545 | |
| 3546 | lua_pop(L, 1); |
| 3547 | lua_pushinteger(L, l); |
| 3548 | |
| 3549 | /* Check if it miss bytes to forward. */ |
| 3550 | if (l < len) { |
| 3551 | /* The the input channel or the output channel are closed, we |
| 3552 | * must return the amount of data forwarded. |
| 3553 | */ |
Christopher Faulet | 055310f | 2021-08-05 11:58:37 +0200 | [diff] [blame] | 3554 | 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] | 3555 | return 1; |
| 3556 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3557 | /* If we are waiting for space data in the response buffer, we |
| 3558 | * must set the flag WAKERESWR. This flag required the task |
| 3559 | * wake up if any activity is detected on the response buffer. |
| 3560 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3561 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3562 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3563 | else |
| 3564 | HLUA_SET_WAKEREQWR(hlua); |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3565 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3566 | /* Otherwise, we can yield waiting for new data in the inpout side. */ |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3567 | 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] | 3568 | } |
| 3569 | |
| 3570 | return 1; |
| 3571 | } |
| 3572 | |
| 3573 | /* Just check the input and prepare the stack for the previous |
| 3574 | * function "hlua_channel_forward_yield" |
| 3575 | */ |
| 3576 | __LJMP static int hlua_channel_forward(lua_State *L) |
| 3577 | { |
| 3578 | MAY_LJMP(check_args(L, 2, "forward")); |
| 3579 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3580 | MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3581 | |
| 3582 | lua_pushinteger(L, 0); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3583 | return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3584 | } |
| 3585 | |
| 3586 | /* Just returns the number of bytes available in the input |
| 3587 | * side of the buffer. This function never fails. |
| 3588 | */ |
| 3589 | __LJMP static int hlua_channel_get_in_len(lua_State *L) |
| 3590 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3591 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3592 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3593 | MAY_LJMP(check_args(L, 1, "get_in_len")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3594 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a3ceac1 | 2018-12-14 13:39:09 +0100 | [diff] [blame] | 3595 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3596 | struct htx *htx = htxbuf(&chn->buf); |
| 3597 | lua_pushinteger(L, htx->data - co_data(chn)); |
| 3598 | } |
| 3599 | else |
| 3600 | lua_pushinteger(L, ci_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3601 | return 1; |
| 3602 | } |
| 3603 | |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3604 | /* Returns true if the channel is full. */ |
| 3605 | __LJMP static int hlua_channel_is_full(lua_State *L) |
| 3606 | { |
| 3607 | struct channel *chn; |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3608 | |
| 3609 | MAY_LJMP(check_args(L, 1, "is_full")); |
| 3610 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 0ec740e | 2020-02-26 11:59:19 +0100 | [diff] [blame] | 3611 | /* ignore the reserve, we are not on a producer side (ie in an |
| 3612 | * applet). |
| 3613 | */ |
| 3614 | lua_pushboolean(L, channel_full(chn, 0)); |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3615 | return 1; |
| 3616 | } |
| 3617 | |
Christopher Faulet | 2ac9ba2 | 2020-02-25 10:15:50 +0100 | [diff] [blame] | 3618 | /* Returns true if the channel is the response channel. */ |
| 3619 | __LJMP static int hlua_channel_is_resp(lua_State *L) |
| 3620 | { |
| 3621 | struct channel *chn; |
| 3622 | |
| 3623 | MAY_LJMP(check_args(L, 1, "is_resp")); |
| 3624 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3625 | |
| 3626 | lua_pushboolean(L, !!(chn->flags & CF_ISRESP)); |
| 3627 | return 1; |
| 3628 | } |
| 3629 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3630 | /* Just returns the number of bytes available in the output |
| 3631 | * side of the buffer. This function never fails. |
| 3632 | */ |
| 3633 | __LJMP static int hlua_channel_get_out_len(lua_State *L) |
| 3634 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3635 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3636 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3637 | MAY_LJMP(check_args(L, 1, "get_out_len")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3638 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3639 | lua_pushinteger(L, co_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3640 | return 1; |
| 3641 | } |
| 3642 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3643 | /* |
| 3644 | * |
| 3645 | * |
| 3646 | * Class Fetches |
| 3647 | * |
| 3648 | * |
| 3649 | */ |
| 3650 | |
| 3651 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3652 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3653 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3654 | __LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud) |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3655 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3656 | return MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3657 | } |
| 3658 | |
| 3659 | /* This function creates and push in the stack a fetch object according |
| 3660 | * with a current TXN. |
| 3661 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3662 | 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] | 3663 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3664 | struct hlua_smp *hsmp; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3665 | |
| 3666 | /* Check stack size. */ |
| 3667 | if (!lua_checkstack(L, 3)) |
| 3668 | return 0; |
| 3669 | |
| 3670 | /* Create the object: obj[0] = userdata. |
| 3671 | * Note that the base of the Fetches object is the |
| 3672 | * transaction object. |
| 3673 | */ |
| 3674 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3675 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3676 | lua_rawseti(L, -2, 0); |
| 3677 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3678 | hsmp->s = txn->s; |
| 3679 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 3680 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3681 | hsmp->flags = flags; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3682 | |
| 3683 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 3684 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref); |
| 3685 | lua_setmetatable(L, -2); |
| 3686 | |
| 3687 | return 1; |
| 3688 | } |
| 3689 | |
| 3690 | /* This function is an LUA binding. It is called with each sample-fetch. |
| 3691 | * It uses closure argument to store the associated sample-fetch. It |
| 3692 | * returns only one argument or throws an error. An error is thrown |
| 3693 | * only if an error is encountered during the argument parsing. If |
| 3694 | * the "sample-fetch" function fails, nil is returned. |
| 3695 | */ |
| 3696 | __LJMP static int hlua_run_sample_fetch(lua_State *L) |
| 3697 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3698 | struct hlua_smp *hsmp; |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 3699 | struct sample_fetch *f; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 3700 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3701 | int i; |
| 3702 | struct sample smp; |
| 3703 | |
| 3704 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3705 | f = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3706 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3707 | /* Get traditional arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3708 | hsmp = MAY_LJMP(hlua_checkfetches(L, 1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3709 | |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 3710 | /* Check execution authorization. */ |
| 3711 | if (f->use & SMP_USE_HTTP_ANY && |
| 3712 | !(hsmp->flags & HLUA_F_MAY_USE_HTTP)) { |
| 3713 | lua_pushfstring(L, "the sample-fetch '%s' needs an HTTP parser which " |
| 3714 | "is not available in Lua services", f->kw); |
| 3715 | WILL_LJMP(lua_error(L)); |
| 3716 | } |
| 3717 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3718 | /* Get extra arguments. */ |
| 3719 | for (i = 0; i < lua_gettop(L) - 1; i++) { |
| 3720 | if (i >= ARGM_NBARGS) |
| 3721 | break; |
| 3722 | hlua_lua2arg(L, i + 2, &args[i]); |
| 3723 | } |
| 3724 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3725 | args[i].data.str.area = NULL; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3726 | |
| 3727 | /* Check arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3728 | 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] | 3729 | |
| 3730 | /* Run the special args checker. */ |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 3731 | if (f->val_args && !f->val_args(args, NULL)) { |
Aurelien DARRAGON | 159136c | 2024-06-03 23:18:24 +0200 | [diff] [blame^] | 3732 | hlua_pushfstring_safe(L, "error in arguments"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3733 | goto error; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3734 | } |
| 3735 | |
| 3736 | /* Initialise the sample. */ |
| 3737 | memset(&smp, 0, sizeof(smp)); |
| 3738 | |
| 3739 | /* Run the sample fetch process. */ |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 3740 | 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] | 3741 | if (!f->process(args, &smp, f->kw, f->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3742 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3743 | lua_pushstring(L, ""); |
| 3744 | else |
| 3745 | lua_pushnil(L); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3746 | goto end; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3747 | } |
| 3748 | |
| 3749 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3750 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3751 | hlua_smp2lua_str(L, &smp); |
| 3752 | else |
| 3753 | hlua_smp2lua(L, &smp); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3754 | |
| 3755 | end: |
| 3756 | for (i = 0; args[i].type != ARGT_STOP; i++) { |
| 3757 | if (args[i].type == ARGT_STR) |
| 3758 | chunk_destroy(&args[i].data.str); |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 3759 | else if (args[i].type == ARGT_REG) |
| 3760 | regex_free(args[i].data.reg); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3761 | } |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3762 | return 1; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3763 | |
| 3764 | error: |
| 3765 | for (i = 0; args[i].type != ARGT_STOP; i++) { |
| 3766 | if (args[i].type == ARGT_STR) |
| 3767 | chunk_destroy(&args[i].data.str); |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 3768 | else if (args[i].type == ARGT_REG) |
| 3769 | regex_free(args[i].data.reg); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3770 | } |
| 3771 | WILL_LJMP(lua_error(L)); |
| 3772 | return 0; /* Never reached */ |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3773 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3774 | |
| 3775 | /* |
| 3776 | * |
| 3777 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3778 | * Class Converters |
| 3779 | * |
| 3780 | * |
| 3781 | */ |
| 3782 | |
| 3783 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3784 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3785 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3786 | __LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3787 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3788 | return MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3789 | } |
| 3790 | |
| 3791 | /* This function creates and push in the stack a Converters object |
| 3792 | * according with a current TXN. |
| 3793 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3794 | 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] | 3795 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3796 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3797 | |
| 3798 | /* Check stack size. */ |
| 3799 | if (!lua_checkstack(L, 3)) |
| 3800 | return 0; |
| 3801 | |
| 3802 | /* Create the object: obj[0] = userdata. |
| 3803 | * Note that the base of the Converters object is the |
| 3804 | * same than the TXN object. |
| 3805 | */ |
| 3806 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3807 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3808 | lua_rawseti(L, -2, 0); |
| 3809 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3810 | hsmp->s = txn->s; |
| 3811 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 3812 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3813 | hsmp->flags = flags; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3814 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3815 | /* Pop a class stream metatable and affect it to the table. */ |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3816 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref); |
| 3817 | lua_setmetatable(L, -2); |
| 3818 | |
| 3819 | return 1; |
| 3820 | } |
| 3821 | |
| 3822 | /* This function is an LUA binding. It is called with each converter. |
| 3823 | * It uses closure argument to store the associated converter. It |
| 3824 | * returns only one argument or throws an error. An error is thrown |
| 3825 | * only if an error is encountered during the argument parsing. If |
| 3826 | * the converter function function fails, nil is returned. |
| 3827 | */ |
| 3828 | __LJMP static int hlua_run_sample_conv(lua_State *L) |
| 3829 | { |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3830 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3831 | struct sample_conv *conv; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 3832 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3833 | int i; |
| 3834 | struct sample smp; |
| 3835 | |
| 3836 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3837 | conv = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3838 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3839 | /* Get traditional arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3840 | hsmp = MAY_LJMP(hlua_checkconverters(L, 1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3841 | |
| 3842 | /* Get extra arguments. */ |
| 3843 | for (i = 0; i < lua_gettop(L) - 2; i++) { |
| 3844 | if (i >= ARGM_NBARGS) |
| 3845 | break; |
| 3846 | hlua_lua2arg(L, i + 3, &args[i]); |
| 3847 | } |
| 3848 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3849 | args[i].data.str.area = NULL; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3850 | |
| 3851 | /* Check arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3852 | 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] | 3853 | |
| 3854 | /* Run the special args checker. */ |
| 3855 | if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) { |
| 3856 | hlua_pusherror(L, "error in arguments"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3857 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3858 | } |
| 3859 | |
| 3860 | /* Initialise the sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 3861 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3862 | if (!hlua_lua2smp(L, 2, &smp)) { |
| 3863 | hlua_pusherror(L, "error in the input argument"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3864 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3865 | } |
| 3866 | |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 3867 | smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR); |
| 3868 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3869 | /* Apply expected cast. */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3870 | if (!sample_casts[smp.data.type][conv->in_type]) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3871 | hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'", |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3872 | smp_to_type[smp.data.type], smp_to_type[conv->in_type]); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3873 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3874 | } |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3875 | if (sample_casts[smp.data.type][conv->in_type] != c_none && |
| 3876 | !sample_casts[smp.data.type][conv->in_type](&smp)) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3877 | hlua_pusherror(L, "error during the input argument casting"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3878 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3879 | } |
| 3880 | |
| 3881 | /* Run the sample conversion process. */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 3882 | if (!conv->process(args, &smp, conv->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3883 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3884 | lua_pushstring(L, ""); |
| 3885 | else |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 3886 | lua_pushnil(L); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3887 | goto end; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3888 | } |
| 3889 | |
| 3890 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3891 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3892 | hlua_smp2lua_str(L, &smp); |
| 3893 | else |
| 3894 | hlua_smp2lua(L, &smp); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3895 | end: |
| 3896 | for (i = 0; args[i].type != ARGT_STOP; i++) { |
| 3897 | if (args[i].type == ARGT_STR) |
| 3898 | chunk_destroy(&args[i].data.str); |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 3899 | else if (args[i].type == ARGT_REG) |
| 3900 | regex_free(args[i].data.reg); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3901 | } |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 3902 | return 1; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3903 | |
| 3904 | error: |
| 3905 | for (i = 0; args[i].type != ARGT_STOP; i++) { |
| 3906 | if (args[i].type == ARGT_STR) |
| 3907 | chunk_destroy(&args[i].data.str); |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 3908 | else if (args[i].type == ARGT_REG) |
| 3909 | regex_free(args[i].data.reg); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3910 | } |
| 3911 | WILL_LJMP(lua_error(L)); |
| 3912 | return 0; /* Never reached */ |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3913 | } |
| 3914 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3915 | /* |
| 3916 | * |
| 3917 | * |
| 3918 | * Class AppletTCP |
| 3919 | * |
| 3920 | * |
| 3921 | */ |
| 3922 | |
| 3923 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 3924 | * a class stream, otherwise it throws an error. |
| 3925 | */ |
| 3926 | __LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud) |
| 3927 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3928 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3929 | } |
| 3930 | |
| 3931 | /* This function creates and push in the stack an Applet object |
| 3932 | * according with a current TXN. |
| 3933 | */ |
| 3934 | static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx) |
| 3935 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3936 | struct hlua_appctx *luactx; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3937 | struct stream_interface *si = ctx->owner; |
| 3938 | struct stream *s = si_strm(si); |
| 3939 | struct proxy *p = s->be; |
| 3940 | |
| 3941 | /* Check stack size. */ |
| 3942 | if (!lua_checkstack(L, 3)) |
| 3943 | return 0; |
| 3944 | |
| 3945 | /* Create the object: obj[0] = userdata. |
| 3946 | * Note that the base of the Converters object is the |
| 3947 | * same than the TXN object. |
| 3948 | */ |
| 3949 | lua_newtable(L); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3950 | luactx = lua_newuserdata(L, sizeof(*luactx)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3951 | lua_rawseti(L, -2, 0); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3952 | luactx->appctx = ctx; |
| 3953 | luactx->htxn.s = s; |
| 3954 | luactx->htxn.p = p; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3955 | |
| 3956 | /* Create the "f" field that contains a list of fetches. */ |
| 3957 | lua_pushstring(L, "f"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3958 | if (!hlua_fetches_new(L, &luactx->htxn, 0)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3959 | return 0; |
| 3960 | lua_settable(L, -3); |
| 3961 | |
| 3962 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 3963 | lua_pushstring(L, "sf"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3964 | if (!hlua_fetches_new(L, &luactx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3965 | return 0; |
| 3966 | lua_settable(L, -3); |
| 3967 | |
| 3968 | /* Create the "c" field that contains a list of converters. */ |
| 3969 | lua_pushstring(L, "c"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3970 | if (!hlua_converters_new(L, &luactx->htxn, 0)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3971 | return 0; |
| 3972 | lua_settable(L, -3); |
| 3973 | |
| 3974 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 3975 | lua_pushstring(L, "sc"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3976 | if (!hlua_converters_new(L, &luactx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3977 | return 0; |
| 3978 | lua_settable(L, -3); |
| 3979 | |
| 3980 | /* Pop a class stream metatable and affect it to the table. */ |
| 3981 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref); |
| 3982 | lua_setmetatable(L, -2); |
| 3983 | |
| 3984 | return 1; |
| 3985 | } |
| 3986 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3987 | __LJMP static int hlua_applet_tcp_set_var(lua_State *L) |
| 3988 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3989 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3990 | struct stream *s; |
| 3991 | const char *name; |
| 3992 | size_t len; |
| 3993 | struct sample smp; |
| 3994 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 3995 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 3996 | 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] | 3997 | |
| 3998 | /* It is useles to retrieve the stream, but this function |
| 3999 | * runs only in a stream context. |
| 4000 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4001 | luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4002 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4003 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4004 | |
| 4005 | /* Converts the third argument in a sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 4006 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4007 | hlua_lua2smp(L, 3, &smp); |
| 4008 | |
Aurelien DARRAGON | d39fb74 | 2023-05-17 16:06:11 +0200 | [diff] [blame] | 4009 | /* Store the sample in a variable. We don't need to dup the smp, vars API |
| 4010 | * already takes care of duplicating dynamic var data. |
| 4011 | */ |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4012 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 4013 | |
| 4014 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 4015 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 4016 | else |
| 4017 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 4018 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 4019 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4020 | } |
| 4021 | |
| 4022 | __LJMP static int hlua_applet_tcp_unset_var(lua_State *L) |
| 4023 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4024 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4025 | struct stream *s; |
| 4026 | const char *name; |
| 4027 | size_t len; |
| 4028 | struct sample smp; |
| 4029 | |
| 4030 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 4031 | |
| 4032 | /* It is useles to retrieve the stream, but this function |
| 4033 | * runs only in a stream context. |
| 4034 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4035 | luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4036 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4037 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4038 | |
| 4039 | /* Unset the variable. */ |
| 4040 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 4041 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 4042 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4043 | } |
| 4044 | |
| 4045 | __LJMP static int hlua_applet_tcp_get_var(lua_State *L) |
| 4046 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4047 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4048 | struct stream *s; |
| 4049 | const char *name; |
| 4050 | size_t len; |
| 4051 | struct sample smp; |
| 4052 | |
| 4053 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 4054 | |
| 4055 | /* It is useles to retrieve the stream, but this function |
| 4056 | * runs only in a stream context. |
| 4057 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4058 | luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4059 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4060 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4061 | |
| 4062 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 4063 | if (!vars_get_by_name(name, len, &smp)) { |
| 4064 | lua_pushnil(L); |
| 4065 | return 1; |
| 4066 | } |
| 4067 | |
| 4068 | return hlua_smp2lua(L, &smp); |
| 4069 | } |
| 4070 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4071 | __LJMP static int hlua_applet_tcp_set_priv(lua_State *L) |
| 4072 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4073 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 4074 | struct stream *s = luactx->htxn.s; |
Aurelien DARRAGON | be7b7ab | 2024-03-12 17:05:54 +0100 | [diff] [blame] | 4075 | 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] | 4076 | |
| 4077 | /* 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] | 4078 | if (!hlua) |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 4079 | return 0; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4080 | |
| 4081 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 4082 | |
| 4083 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 4084 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4085 | |
| 4086 | /* Get and store new value. */ |
| 4087 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 4088 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 4089 | |
| 4090 | return 0; |
| 4091 | } |
| 4092 | |
| 4093 | __LJMP static int hlua_applet_tcp_get_priv(lua_State *L) |
| 4094 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4095 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 4096 | struct stream *s = luactx->htxn.s; |
Aurelien DARRAGON | be7b7ab | 2024-03-12 17:05:54 +0100 | [diff] [blame] | 4097 | 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] | 4098 | |
| 4099 | /* 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] | 4100 | if (!hlua) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 4101 | lua_pushnil(L); |
| 4102 | return 1; |
| 4103 | } |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4104 | |
| 4105 | /* Push configuration index in the stack. */ |
| 4106 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 4107 | |
| 4108 | return 1; |
| 4109 | } |
| 4110 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4111 | /* If expected data not yet available, it returns a yield. This function |
| 4112 | * consumes the data in the buffer. It returns a string containing the |
| 4113 | * data. This string can be empty. |
| 4114 | */ |
| 4115 | __LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx) |
| 4116 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4117 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 4118 | struct stream_interface *si = luactx->appctx->owner; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4119 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4120 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4121 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4122 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4123 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4124 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4125 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4126 | ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4127 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4128 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4129 | if (ret == 0) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 4130 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4131 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_getline_yield, TICK_ETERNITY, 0)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4132 | } |
| 4133 | |
| 4134 | /* End of data: commit the total strings and return. */ |
| 4135 | if (ret < 0) { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4136 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4137 | return 1; |
| 4138 | } |
| 4139 | |
| 4140 | /* Ensure that the block 2 length is usable. */ |
| 4141 | if (ret == 1) |
| 4142 | len2 = 0; |
| 4143 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 4144 | /* don't check the max length read and don't check. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4145 | luaL_addlstring(&luactx->b, blk1, len1); |
| 4146 | luaL_addlstring(&luactx->b, blk2, len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4147 | |
| 4148 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4149 | co_skip(si_oc(si), len1 + len2); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4150 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4151 | return 1; |
| 4152 | } |
| 4153 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4154 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4155 | __LJMP static int hlua_applet_tcp_getline(lua_State *L) |
| 4156 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4157 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4158 | |
| 4159 | /* Initialise the string catenation. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4160 | luaL_buffinit(L, &luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4161 | |
| 4162 | return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0)); |
| 4163 | } |
| 4164 | |
| 4165 | /* If expected data not yet available, it returns a yield. This function |
| 4166 | * consumes the data in the buffer. It returns a string containing the |
| 4167 | * data. This string can be empty. |
| 4168 | */ |
| 4169 | __LJMP static int hlua_applet_tcp_recv_yield(lua_State *L, int status, lua_KContext ctx) |
| 4170 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4171 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 4172 | struct stream_interface *si = luactx->appctx->owner; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4173 | size_t len = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4174 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4175 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4176 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4177 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4178 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4179 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4180 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4181 | ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4182 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4183 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4184 | if (ret == 0) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 4185 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4186 | 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] | 4187 | } |
| 4188 | |
| 4189 | /* End of data: commit the total strings and return. */ |
| 4190 | if (ret < 0) { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4191 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4192 | return 1; |
| 4193 | } |
| 4194 | |
| 4195 | /* Ensure that the block 2 length is usable. */ |
| 4196 | if (ret == 1) |
| 4197 | len2 = 0; |
| 4198 | |
| 4199 | if (len == -1) { |
| 4200 | |
| 4201 | /* If len == -1, catenate all the data avalaile and |
| 4202 | * yield because we want to get all the data until |
| 4203 | * the end of data stream. |
| 4204 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4205 | luaL_addlstring(&luactx->b, blk1, len1); |
| 4206 | luaL_addlstring(&luactx->b, blk2, len2); |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4207 | co_skip(si_oc(si), len1 + len2); |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 4208 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4209 | 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] | 4210 | |
| 4211 | } else { |
| 4212 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4213 | /* Copy the first block caping to the length required. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4214 | if (len1 > len) |
| 4215 | len1 = len; |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4216 | luaL_addlstring(&luactx->b, blk1, len1); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4217 | len -= len1; |
| 4218 | |
| 4219 | /* Copy the second block. */ |
| 4220 | if (len2 > len) |
| 4221 | len2 = len; |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4222 | luaL_addlstring(&luactx->b, blk2, len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4223 | len -= len2; |
| 4224 | |
| 4225 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4226 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4227 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4228 | /* If there is no other data available, yield waiting for new data. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4229 | if (len > 0) { |
| 4230 | lua_pushinteger(L, len); |
| 4231 | lua_replace(L, 2); |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 4232 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4233 | 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] | 4234 | } |
| 4235 | |
| 4236 | /* return the result. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4237 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4238 | return 1; |
| 4239 | } |
| 4240 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4241 | /* we never execute this */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4242 | hlua_pusherror(L, "Lua: internal error"); |
| 4243 | WILL_LJMP(lua_error(L)); |
| 4244 | return 0; |
| 4245 | } |
| 4246 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4247 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4248 | __LJMP static int hlua_applet_tcp_recv(lua_State *L) |
| 4249 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4250 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4251 | int len = -1; |
| 4252 | |
| 4253 | if (lua_gettop(L) > 2) |
| 4254 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 4255 | if (lua_gettop(L) >= 2) { |
| 4256 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4257 | lua_pop(L, 1); |
| 4258 | } |
| 4259 | |
| 4260 | /* Confirm or set the required length */ |
| 4261 | lua_pushinteger(L, len); |
| 4262 | |
| 4263 | /* Initialise the string catenation. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4264 | luaL_buffinit(L, &luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4265 | |
| 4266 | return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0)); |
| 4267 | } |
| 4268 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4269 | /* Append data in the output side of the buffer. This data is immediately |
| 4270 | * sent. The function returns the amount of data written. If the buffer |
| 4271 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4272 | * if the channel is closed. |
| 4273 | */ |
| 4274 | __LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx) |
| 4275 | { |
| 4276 | size_t len; |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4277 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4278 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4279 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4280 | struct stream_interface *si = luactx->appctx->owner; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4281 | struct channel *chn = si_ic(si); |
| 4282 | int max; |
| 4283 | |
| 4284 | /* Get the max amount of data which can write as input in the channel. */ |
| 4285 | max = channel_recv_max(chn); |
| 4286 | if (max > (len - l)) |
| 4287 | max = len - l; |
| 4288 | |
| 4289 | /* Copy data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4290 | ci_putblk(chn, str + l, max); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4291 | |
| 4292 | /* update counters. */ |
| 4293 | l += max; |
| 4294 | lua_pop(L, 1); |
| 4295 | lua_pushinteger(L, l); |
| 4296 | |
| 4297 | /* If some data is not send, declares the situation to the |
| 4298 | * applet, and returns a yield. |
| 4299 | */ |
| 4300 | if (l < len) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 4301 | si_rx_room_blk(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4302 | 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] | 4303 | } |
| 4304 | |
| 4305 | return 1; |
| 4306 | } |
| 4307 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4308 | /* Just a wrapper of "hlua_applet_tcp_send_yield". This wrapper permits |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4309 | * yield the LUA process, and resume it without checking the |
| 4310 | * input arguments. |
| 4311 | */ |
| 4312 | __LJMP static int hlua_applet_tcp_send(lua_State *L) |
| 4313 | { |
| 4314 | MAY_LJMP(check_args(L, 2, "send")); |
| 4315 | lua_pushinteger(L, 0); |
| 4316 | |
| 4317 | return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0)); |
| 4318 | } |
| 4319 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4320 | /* |
| 4321 | * |
| 4322 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4323 | * Class AppletHTTP |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4324 | * |
| 4325 | * |
| 4326 | */ |
| 4327 | |
| 4328 | /* Returns a struct hlua_txn if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4329 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4330 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4331 | __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] | 4332 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4333 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_http_ref)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4334 | } |
| 4335 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4336 | /* This function creates and push in the stack an Applet object |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4337 | * according with a current TXN. |
| 4338 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4339 | static int hlua_applet_http_new(lua_State *L, struct appctx *ctx) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4340 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4341 | struct hlua_appctx *luactx; |
Thierry FOURNIER | 841475e | 2015-12-11 17:10:09 +0100 | [diff] [blame] | 4342 | struct hlua_txn htxn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4343 | struct stream_interface *si = ctx->owner; |
| 4344 | struct stream *s = si_strm(si); |
| 4345 | struct proxy *px = s->be; |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4346 | struct htx *htx; |
| 4347 | struct htx_blk *blk; |
| 4348 | struct htx_sl *sl; |
| 4349 | struct ist path; |
| 4350 | unsigned long long len = 0; |
| 4351 | int32_t pos; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4352 | |
| 4353 | /* Check stack size. */ |
| 4354 | if (!lua_checkstack(L, 3)) |
| 4355 | return 0; |
| 4356 | |
| 4357 | /* Create the object: obj[0] = userdata. |
| 4358 | * Note that the base of the Converters object is the |
| 4359 | * same than the TXN object. |
| 4360 | */ |
| 4361 | lua_newtable(L); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4362 | luactx = lua_newuserdata(L, sizeof(*luactx)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4363 | lua_rawseti(L, -2, 0); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4364 | luactx->appctx = ctx; |
| 4365 | luactx->appctx->ctx.hlua_apphttp.status = 200; /* Default status code returned. */ |
| 4366 | luactx->appctx->ctx.hlua_apphttp.reason = NULL; /* Use default reason based on status */ |
| 4367 | luactx->htxn.s = s; |
| 4368 | luactx->htxn.p = px; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4369 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4370 | /* Create the "f" field that contains a list of fetches. */ |
| 4371 | lua_pushstring(L, "f"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4372 | if (!hlua_fetches_new(L, &luactx->htxn, 0)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4373 | return 0; |
| 4374 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4375 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4376 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 4377 | lua_pushstring(L, "sf"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4378 | if (!hlua_fetches_new(L, &luactx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4379 | return 0; |
| 4380 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4381 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4382 | /* Create the "c" field that contains a list of converters. */ |
| 4383 | lua_pushstring(L, "c"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4384 | if (!hlua_converters_new(L, &luactx->htxn, 0)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4385 | return 0; |
| 4386 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4387 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4388 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 4389 | lua_pushstring(L, "sc"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4390 | if (!hlua_converters_new(L, &luactx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4391 | return 0; |
| 4392 | lua_settable(L, -3); |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4393 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4394 | htx = htxbuf(&s->req.buf); |
| 4395 | blk = htx_get_first_blk(htx); |
Christopher Faulet | ea00973 | 2019-11-18 15:50:25 +0100 | [diff] [blame] | 4396 | BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4397 | sl = htx_get_blk_ptr(htx, blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4398 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4399 | /* Stores the request method. */ |
| 4400 | lua_pushstring(L, "method"); |
| 4401 | lua_pushlstring(L, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); |
| 4402 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4403 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4404 | /* Stores the http version. */ |
| 4405 | lua_pushstring(L, "version"); |
| 4406 | lua_pushlstring(L, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); |
| 4407 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4408 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4409 | /* creates an array of headers. hlua_http_get_headers() crates and push |
| 4410 | * the array on the top of the stack. |
| 4411 | */ |
| 4412 | lua_pushstring(L, "headers"); |
| 4413 | htxn.s = s; |
| 4414 | htxn.p = px; |
| 4415 | htxn.dir = SMP_OPT_DIR_REQ; |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 4416 | if (!hlua_http_get_headers(L, &htxn.s->txn->req)) |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4417 | return 0; |
| 4418 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4419 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4420 | path = http_get_path(htx_sl_req_uri(sl)); |
Tim Duesterhus | ed52637 | 2020-03-05 17:56:33 +0100 | [diff] [blame] | 4421 | if (isttest(path)) { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4422 | char *p, *q, *end; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4423 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4424 | p = path.ptr; |
| 4425 | end = path.ptr + path.len; |
| 4426 | q = p; |
| 4427 | while (q < end && *q != '?') |
| 4428 | q++; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4429 | |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 4430 | /* Stores the request path. */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4431 | lua_pushstring(L, "path"); |
| 4432 | lua_pushlstring(L, p, q - p); |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 4433 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4434 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4435 | /* Stores the query string. */ |
| 4436 | lua_pushstring(L, "qs"); |
| 4437 | if (*q == '?') |
| 4438 | q++; |
| 4439 | lua_pushlstring(L, q, end - q); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4440 | lua_settable(L, -3); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4441 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4442 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4443 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 4444 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 4445 | enum htx_blk_type type = htx_get_blk_type(blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4446 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 4447 | if (type == HTX_BLK_TLR || type == HTX_BLK_EOT) |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4448 | break; |
| 4449 | if (type == HTX_BLK_DATA) |
| 4450 | len += htx_get_blksz(blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4451 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4452 | if (htx->extra != ULLONG_MAX) |
| 4453 | len += htx->extra; |
| 4454 | |
| 4455 | /* Stores the request path. */ |
| 4456 | lua_pushstring(L, "length"); |
| 4457 | lua_pushinteger(L, len); |
| 4458 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4459 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4460 | /* Create an empty array of HTTP request headers. */ |
| 4461 | lua_pushstring(L, "response"); |
| 4462 | lua_newtable(L); |
| 4463 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4464 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4465 | /* Pop a class stream metatable and affect it to the table. */ |
| 4466 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref); |
| 4467 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4468 | |
| 4469 | return 1; |
| 4470 | } |
| 4471 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4472 | __LJMP static int hlua_applet_http_set_var(lua_State *L) |
| 4473 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4474 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4475 | struct stream *s; |
| 4476 | const char *name; |
| 4477 | size_t len; |
| 4478 | struct sample smp; |
| 4479 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 4480 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 4481 | 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] | 4482 | |
| 4483 | /* It is useles to retrieve the stream, but this function |
| 4484 | * runs only in a stream context. |
| 4485 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4486 | luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4487 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4488 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4489 | |
| 4490 | /* Converts the third argument in a sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 4491 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4492 | hlua_lua2smp(L, 3, &smp); |
| 4493 | |
Aurelien DARRAGON | d39fb74 | 2023-05-17 16:06:11 +0200 | [diff] [blame] | 4494 | /* Store the sample in a variable. We don't need to dup the smp, vars API |
| 4495 | * already takes care of duplicating dynamic var data. |
| 4496 | */ |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4497 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 4498 | |
| 4499 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 4500 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 4501 | else |
| 4502 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 4503 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 4504 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4505 | } |
| 4506 | |
| 4507 | __LJMP static int hlua_applet_http_unset_var(lua_State *L) |
| 4508 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4509 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4510 | struct stream *s; |
| 4511 | const char *name; |
| 4512 | size_t len; |
| 4513 | struct sample smp; |
| 4514 | |
| 4515 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 4516 | |
| 4517 | /* It is useles to retrieve the stream, but this function |
| 4518 | * runs only in a stream context. |
| 4519 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4520 | luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4521 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4522 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4523 | |
| 4524 | /* Unset the variable. */ |
| 4525 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 4526 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 4527 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4528 | } |
| 4529 | |
| 4530 | __LJMP static int hlua_applet_http_get_var(lua_State *L) |
| 4531 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4532 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4533 | struct stream *s; |
| 4534 | const char *name; |
| 4535 | size_t len; |
| 4536 | struct sample smp; |
| 4537 | |
| 4538 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 4539 | |
| 4540 | /* It is useles to retrieve the stream, but this function |
| 4541 | * runs only in a stream context. |
| 4542 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4543 | luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4544 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4545 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4546 | |
| 4547 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 4548 | if (!vars_get_by_name(name, len, &smp)) { |
| 4549 | lua_pushnil(L); |
| 4550 | return 1; |
| 4551 | } |
| 4552 | |
| 4553 | return hlua_smp2lua(L, &smp); |
| 4554 | } |
| 4555 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4556 | __LJMP static int hlua_applet_http_set_priv(lua_State *L) |
| 4557 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4558 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4559 | struct stream *s = luactx->htxn.s; |
Aurelien DARRAGON | be7b7ab | 2024-03-12 17:05:54 +0100 | [diff] [blame] | 4560 | 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] | 4561 | |
| 4562 | /* 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] | 4563 | if (!hlua) |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 4564 | return 0; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4565 | |
| 4566 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 4567 | |
| 4568 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 4569 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4570 | |
| 4571 | /* Get and store new value. */ |
| 4572 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 4573 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 4574 | |
| 4575 | return 0; |
| 4576 | } |
| 4577 | |
| 4578 | __LJMP static int hlua_applet_http_get_priv(lua_State *L) |
| 4579 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4580 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4581 | struct stream *s = luactx->htxn.s; |
Aurelien DARRAGON | be7b7ab | 2024-03-12 17:05:54 +0100 | [diff] [blame] | 4582 | 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] | 4583 | |
| 4584 | /* 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] | 4585 | if (!hlua) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 4586 | lua_pushnil(L); |
| 4587 | return 1; |
| 4588 | } |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4589 | |
| 4590 | /* Push configuration index in the stack. */ |
| 4591 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 4592 | |
| 4593 | return 1; |
| 4594 | } |
| 4595 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4596 | /* If expected data not yet available, it returns a yield. This function |
| 4597 | * consumes the data in the buffer. It returns a string containing the |
| 4598 | * data. This string can be empty. |
| 4599 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4600 | __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] | 4601 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4602 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4603 | struct stream_interface *si = luactx->appctx->owner; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4604 | struct channel *req = si_oc(si); |
| 4605 | struct htx *htx; |
| 4606 | struct htx_blk *blk; |
| 4607 | size_t count; |
| 4608 | int stop = 0; |
| 4609 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4610 | htx = htx_from_buf(&req->buf); |
| 4611 | count = co_data(req); |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 4612 | blk = htx_get_first_blk(htx); |
Christopher Faulet | cc26b13 | 2018-12-18 21:20:57 +0100 | [diff] [blame] | 4613 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4614 | while (count && !stop && blk) { |
| 4615 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4616 | uint32_t sz = htx_get_blksz(blk); |
| 4617 | struct ist v; |
| 4618 | uint32_t vlen; |
| 4619 | char *nl; |
| 4620 | |
| 4621 | vlen = sz; |
| 4622 | if (vlen > count) { |
| 4623 | if (type != HTX_BLK_DATA) |
| 4624 | break; |
| 4625 | vlen = count; |
| 4626 | } |
| 4627 | |
| 4628 | switch (type) { |
| 4629 | case HTX_BLK_UNUSED: |
| 4630 | break; |
| 4631 | |
| 4632 | case HTX_BLK_DATA: |
| 4633 | v = htx_get_blk_value(htx, blk); |
| 4634 | v.len = vlen; |
| 4635 | nl = istchr(v, '\n'); |
| 4636 | if (nl != NULL) { |
| 4637 | stop = 1; |
| 4638 | vlen = nl - v.ptr + 1; |
| 4639 | } |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4640 | luaL_addlstring(&luactx->b, v.ptr, vlen); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4641 | break; |
| 4642 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4643 | case HTX_BLK_TLR: |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 4644 | case HTX_BLK_EOT: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4645 | stop = 1; |
| 4646 | break; |
| 4647 | |
| 4648 | default: |
| 4649 | break; |
| 4650 | } |
| 4651 | |
| 4652 | co_set_data(req, co_data(req) - vlen); |
| 4653 | count -= vlen; |
| 4654 | if (sz == vlen) |
| 4655 | blk = htx_remove_blk(htx, blk); |
| 4656 | else { |
| 4657 | htx_cut_data_blk(htx, blk, vlen); |
| 4658 | break; |
| 4659 | } |
| 4660 | } |
| 4661 | |
Christopher Faulet | eccb31c | 2021-04-02 14:24:56 +0200 | [diff] [blame] | 4662 | /* The message was fully consumed and no more data are expected |
| 4663 | * (EOM flag set). |
| 4664 | */ |
Christopher Faulet | a5a25b1 | 2022-08-29 15:37:16 +0200 | [diff] [blame] | 4665 | if (htx_is_empty(htx) && (req->flags & CF_EOI)) |
Christopher Faulet | eccb31c | 2021-04-02 14:24:56 +0200 | [diff] [blame] | 4666 | stop = 1; |
| 4667 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4668 | htx_to_buf(htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4669 | if (!stop) { |
| 4670 | si_cant_get(si); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4671 | 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] | 4672 | } |
| 4673 | |
| 4674 | /* return the result. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4675 | luaL_pushresult(&luactx->b); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4676 | return 1; |
| 4677 | } |
| 4678 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4679 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4680 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4681 | __LJMP static int hlua_applet_http_getline(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4682 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4683 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4684 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4685 | /* Initialise the string catenation. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4686 | luaL_buffinit(L, &luactx->b); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4687 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4688 | return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4689 | } |
| 4690 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4691 | /* If expected data not yet available, it returns a yield. This function |
| 4692 | * consumes the data in the buffer. It returns a string containing the |
| 4693 | * data. This string can be empty. |
| 4694 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4695 | __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] | 4696 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4697 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4698 | struct stream_interface *si = luactx->appctx->owner; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4699 | struct channel *req = si_oc(si); |
| 4700 | struct htx *htx; |
| 4701 | struct htx_blk *blk; |
| 4702 | size_t count; |
| 4703 | int len; |
| 4704 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4705 | htx = htx_from_buf(&req->buf); |
| 4706 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4707 | count = co_data(req); |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 4708 | blk = htx_get_head_blk(htx); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4709 | while (count && len && blk) { |
| 4710 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4711 | uint32_t sz = htx_get_blksz(blk); |
| 4712 | struct ist v; |
| 4713 | uint32_t vlen; |
| 4714 | |
| 4715 | vlen = sz; |
| 4716 | if (len > 0 && vlen > len) |
| 4717 | vlen = len; |
| 4718 | if (vlen > count) { |
| 4719 | if (type != HTX_BLK_DATA) |
| 4720 | break; |
| 4721 | vlen = count; |
| 4722 | } |
| 4723 | |
| 4724 | switch (type) { |
| 4725 | case HTX_BLK_UNUSED: |
| 4726 | break; |
| 4727 | |
| 4728 | case HTX_BLK_DATA: |
| 4729 | v = htx_get_blk_value(htx, blk); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4730 | luaL_addlstring(&luactx->b, v.ptr, vlen); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4731 | break; |
| 4732 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4733 | case HTX_BLK_TLR: |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 4734 | case HTX_BLK_EOT: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4735 | len = 0; |
| 4736 | break; |
| 4737 | |
| 4738 | default: |
| 4739 | break; |
| 4740 | } |
| 4741 | |
| 4742 | co_set_data(req, co_data(req) - vlen); |
| 4743 | count -= vlen; |
| 4744 | if (len > 0) |
| 4745 | len -= vlen; |
| 4746 | if (sz == vlen) |
| 4747 | blk = htx_remove_blk(htx, blk); |
| 4748 | else { |
| 4749 | htx_cut_data_blk(htx, blk, vlen); |
| 4750 | break; |
| 4751 | } |
| 4752 | } |
| 4753 | |
Christopher Faulet | eccb31c | 2021-04-02 14:24:56 +0200 | [diff] [blame] | 4754 | /* The message was fully consumed and no more data are expected |
| 4755 | * (EOM flag set). |
| 4756 | */ |
Christopher Faulet | a5a25b1 | 2022-08-29 15:37:16 +0200 | [diff] [blame] | 4757 | if (htx_is_empty(htx) && (req->flags & CF_EOI)) |
Christopher Faulet | eccb31c | 2021-04-02 14:24:56 +0200 | [diff] [blame] | 4758 | len = 0; |
| 4759 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4760 | htx_to_buf(htx, &req->buf); |
| 4761 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4762 | /* If we are no other data available, yield waiting for new data. */ |
| 4763 | if (len) { |
| 4764 | if (len > 0) { |
| 4765 | lua_pushinteger(L, len); |
| 4766 | lua_replace(L, 2); |
| 4767 | } |
| 4768 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4769 | 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] | 4770 | } |
| 4771 | |
| 4772 | /* return the result. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4773 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4774 | return 1; |
| 4775 | } |
| 4776 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4777 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4778 | __LJMP static int hlua_applet_http_recv(lua_State *L) |
| 4779 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4780 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4781 | int len = -1; |
| 4782 | |
| 4783 | /* Check arguments. */ |
| 4784 | if (lua_gettop(L) > 2) |
| 4785 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 4786 | if (lua_gettop(L) >= 2) { |
| 4787 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4788 | lua_pop(L, 1); |
| 4789 | } |
| 4790 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4791 | lua_pushinteger(L, len); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4792 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4793 | /* Initialise the string catenation. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4794 | luaL_buffinit(L, &luactx->b); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4795 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4796 | return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4797 | } |
| 4798 | |
| 4799 | /* Append data in the output side of the buffer. This data is immediately |
| 4800 | * sent. The function returns the amount of data written. If the buffer |
| 4801 | * cannot contain the data, the function yields. The function returns -1 |
| 4802 | * if the channel is closed. |
| 4803 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4804 | __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] | 4805 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4806 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4807 | struct stream_interface *si = luactx->appctx->owner; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4808 | struct channel *res = si_ic(si); |
| 4809 | struct htx *htx = htx_from_buf(&res->buf); |
| 4810 | const char *data; |
| 4811 | size_t len; |
| 4812 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 4813 | int max; |
| 4814 | |
Christopher Faulet | 9060fc0 | 2019-07-03 11:39:30 +0200 | [diff] [blame] | 4815 | max = htx_get_max_blksz(htx, channel_htx_recv_max(res, htx)); |
Christopher Faulet | 4b0e9b2 | 2019-01-09 12:16:58 +0100 | [diff] [blame] | 4816 | if (!max) |
| 4817 | goto snd_yield; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4818 | |
| 4819 | data = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4820 | |
| 4821 | /* Get the max amount of data which can write as input in the channel. */ |
| 4822 | if (max > (len - l)) |
| 4823 | max = len - l; |
| 4824 | |
| 4825 | /* Copy data. */ |
Willy Tarreau | 0a7ef02 | 2019-05-28 10:30:11 +0200 | [diff] [blame] | 4826 | max = htx_add_data(htx, ist2(data + l, max)); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 4827 | channel_add_input(res, max); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4828 | |
| 4829 | /* update counters. */ |
| 4830 | l += max; |
| 4831 | lua_pop(L, 1); |
| 4832 | lua_pushinteger(L, l); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4833 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4834 | /* If some data is not send, declares the situation to the |
| 4835 | * applet, and returns a yield. |
| 4836 | */ |
| 4837 | if (l < len) { |
Christopher Faulet | 4b0e9b2 | 2019-01-09 12:16:58 +0100 | [diff] [blame] | 4838 | snd_yield: |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4839 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4840 | si_rx_room_blk(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4841 | 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] | 4842 | } |
| 4843 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4844 | htx_to_buf(htx, &res->buf); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4845 | return 1; |
| 4846 | } |
| 4847 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4848 | /* Just a wrapper of "hlua_applet_send_yield". This wrapper permits |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4849 | * yield the LUA process, and resume it without checking the |
| 4850 | * input arguments. |
| 4851 | */ |
| 4852 | __LJMP static int hlua_applet_http_send(lua_State *L) |
| 4853 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4854 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4855 | |
| 4856 | /* We want to send some data. Headers must be sent. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4857 | if (!(luactx->appctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4858 | hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data."); |
| 4859 | WILL_LJMP(lua_error(L)); |
| 4860 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4861 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4862 | /* This integer is used for followinf the amount of data sent. */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4863 | lua_pushinteger(L, 0); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4864 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4865 | return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4866 | } |
| 4867 | |
| 4868 | __LJMP static int hlua_applet_http_addheader(lua_State *L) |
| 4869 | { |
| 4870 | const char *name; |
| 4871 | int ret; |
| 4872 | |
| 4873 | MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4874 | name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 4875 | MAY_LJMP(luaL_checkstring(L, 3)); |
| 4876 | |
| 4877 | /* Push in the stack the "response" entry. */ |
| 4878 | ret = lua_getfield(L, 1, "response"); |
| 4879 | if (ret != LUA_TTABLE) { |
| 4880 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] " |
| 4881 | "is expected as an array. %s found", lua_typename(L, ret)); |
| 4882 | WILL_LJMP(lua_error(L)); |
| 4883 | } |
| 4884 | |
| 4885 | /* check if the header is already registered if it is not |
| 4886 | * the case, register it. |
| 4887 | */ |
| 4888 | ret = lua_getfield(L, -1, name); |
| 4889 | if (ret == LUA_TNIL) { |
| 4890 | |
| 4891 | /* Entry not found. */ |
| 4892 | lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */ |
| 4893 | |
| 4894 | /* Insert the new header name in the array in the top of the stack. |
| 4895 | * It left the new array in the top of the stack. |
| 4896 | */ |
| 4897 | lua_newtable(L); |
| 4898 | lua_pushvalue(L, 2); |
| 4899 | lua_pushvalue(L, -2); |
| 4900 | lua_settable(L, -4); |
| 4901 | |
| 4902 | } else if (ret != LUA_TTABLE) { |
| 4903 | |
| 4904 | /* corruption error. */ |
| 4905 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] " |
| 4906 | "is expected as an array. %s found", name, lua_typename(L, ret)); |
| 4907 | WILL_LJMP(lua_error(L)); |
| 4908 | } |
| 4909 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 4910 | /* Now the top of thestack is an array of values. We push |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4911 | * the header value as new entry. |
| 4912 | */ |
| 4913 | lua_pushvalue(L, 3); |
| 4914 | ret = lua_rawlen(L, -2); |
| 4915 | lua_rawseti(L, -2, ret + 1); |
| 4916 | lua_pushboolean(L, 1); |
| 4917 | return 1; |
| 4918 | } |
| 4919 | |
| 4920 | __LJMP static int hlua_applet_http_status(lua_State *L) |
| 4921 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4922 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4923 | int status = MAY_LJMP(luaL_checkinteger(L, 2)); |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4924 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4925 | |
| 4926 | if (status < 100 || status > 599) { |
| 4927 | lua_pushboolean(L, 0); |
| 4928 | return 1; |
| 4929 | } |
| 4930 | |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4931 | luactx->appctx->ctx.hlua_apphttp.status = status; |
| 4932 | luactx->appctx->ctx.hlua_apphttp.reason = reason; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4933 | lua_pushboolean(L, 1); |
| 4934 | return 1; |
| 4935 | } |
| 4936 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4937 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4938 | __LJMP static int hlua_applet_http_send_response(lua_State *L) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4939 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4940 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4941 | struct stream_interface *si = luactx->appctx->owner; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4942 | struct channel *res = si_ic(si); |
| 4943 | struct htx *htx; |
| 4944 | struct htx_sl *sl; |
| 4945 | struct h1m h1m; |
| 4946 | const char *status, *reason; |
| 4947 | const char *name, *value; |
| 4948 | size_t nlen, vlen; |
| 4949 | unsigned int flags; |
| 4950 | |
| 4951 | /* Send the message at once. */ |
| 4952 | htx = htx_from_buf(&res->buf); |
| 4953 | h1m_init_res(&h1m); |
| 4954 | |
| 4955 | /* Use the same http version than the request. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4956 | status = ultoa_r(luactx->appctx->ctx.hlua_apphttp.status, trash.area, trash.size); |
| 4957 | reason = luactx->appctx->ctx.hlua_apphttp.reason; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4958 | if (reason == NULL) |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4959 | reason = http_get_reason(luactx->appctx->ctx.hlua_apphttp.status); |
| 4960 | if (luactx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4961 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11); |
| 4962 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist(status), ist(reason)); |
| 4963 | } |
| 4964 | else { |
| 4965 | flags = HTX_SL_F_IS_RESP; |
| 4966 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), ist(status), ist(reason)); |
| 4967 | } |
| 4968 | if (!sl) { |
| 4969 | hlua_pusherror(L, "Lua applet http '%s': Failed to create response.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4970 | luactx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4971 | WILL_LJMP(lua_error(L)); |
| 4972 | } |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4973 | sl->info.res.status = luactx->appctx->ctx.hlua_apphttp.status; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4974 | |
| 4975 | /* Get the array associated to the field "response" in the object AppletHTTP. */ |
| 4976 | lua_pushvalue(L, 0); |
| 4977 | if (lua_getfield(L, 1, "response") != LUA_TTABLE) { |
| 4978 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4979 | luactx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4980 | WILL_LJMP(lua_error(L)); |
| 4981 | } |
| 4982 | |
| 4983 | /* Browse the list of headers. */ |
| 4984 | lua_pushnil(L); |
| 4985 | while(lua_next(L, -2) != 0) { |
| 4986 | /* We expect a string as -2. */ |
| 4987 | if (lua_type(L, -2) != LUA_TSTRING) { |
| 4988 | 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] | 4989 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4990 | lua_typename(L, lua_type(L, -2))); |
| 4991 | WILL_LJMP(lua_error(L)); |
| 4992 | } |
| 4993 | name = lua_tolstring(L, -2, &nlen); |
| 4994 | |
| 4995 | /* We expect an array as -1. */ |
| 4996 | if (lua_type(L, -1) != LUA_TTABLE) { |
| 4997 | 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] | 4998 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4999 | name, |
| 5000 | lua_typename(L, lua_type(L, -1))); |
| 5001 | WILL_LJMP(lua_error(L)); |
| 5002 | } |
| 5003 | |
| 5004 | /* Browse the table who is on the top of the stack. */ |
| 5005 | lua_pushnil(L); |
| 5006 | while(lua_next(L, -2) != 0) { |
| 5007 | int id; |
| 5008 | |
| 5009 | /* We expect a number as -2. */ |
| 5010 | if (lua_type(L, -2) != LUA_TNUMBER) { |
| 5011 | 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] | 5012 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5013 | name, |
| 5014 | lua_typename(L, lua_type(L, -2))); |
| 5015 | WILL_LJMP(lua_error(L)); |
| 5016 | } |
| 5017 | id = lua_tointeger(L, -2); |
| 5018 | |
| 5019 | /* We expect a string as -2. */ |
| 5020 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 5021 | 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] | 5022 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5023 | name, id, |
| 5024 | lua_typename(L, lua_type(L, -1))); |
| 5025 | WILL_LJMP(lua_error(L)); |
| 5026 | } |
| 5027 | value = lua_tolstring(L, -1, &vlen); |
| 5028 | |
| 5029 | /* Simple Protocol checks. */ |
| 5030 | if (isteqi(ist2(name, nlen), ist("transfer-encoding"))) |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5031 | h1_parse_xfer_enc_header(&h1m, ist2(value, vlen)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5032 | else if (isteqi(ist2(name, nlen), ist("content-length"))) { |
| 5033 | struct ist v = ist2(value, vlen); |
| 5034 | int ret; |
| 5035 | |
| 5036 | ret = h1_parse_cont_len_header(&h1m, &v); |
| 5037 | if (ret < 0) { |
| 5038 | hlua_pusherror(L, "Lua applet http '%s': Invalid '%s' header.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5039 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5040 | name); |
| 5041 | WILL_LJMP(lua_error(L)); |
| 5042 | } |
| 5043 | else if (ret == 0) |
| 5044 | goto next; /* Skip it */ |
| 5045 | } |
| 5046 | |
| 5047 | /* Add a new header */ |
| 5048 | if (!htx_add_header(htx, ist2(name, nlen), ist2(value, vlen))) { |
| 5049 | 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] | 5050 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5051 | name); |
| 5052 | WILL_LJMP(lua_error(L)); |
| 5053 | } |
| 5054 | next: |
| 5055 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 5056 | lua_pop(L, 1); |
| 5057 | } |
| 5058 | |
| 5059 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 5060 | lua_pop(L, 1); |
| 5061 | } |
| 5062 | |
| 5063 | if (h1m.flags & H1_MF_CHNK) |
| 5064 | h1m.flags &= ~H1_MF_CLEN; |
| 5065 | if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK)) |
| 5066 | h1m.flags |= H1_MF_XFER_LEN; |
| 5067 | |
| 5068 | /* Uset HTX start-line flags */ |
| 5069 | if (h1m.flags & H1_MF_XFER_ENC) |
| 5070 | flags |= HTX_SL_F_XFER_ENC; |
| 5071 | if (h1m.flags & H1_MF_XFER_LEN) { |
| 5072 | flags |= HTX_SL_F_XFER_LEN; |
| 5073 | if (h1m.flags & H1_MF_CHNK) |
| 5074 | flags |= HTX_SL_F_CHNK; |
| 5075 | else if (h1m.flags & H1_MF_CLEN) |
| 5076 | flags |= HTX_SL_F_CLEN; |
| 5077 | if (h1m.body_len == 0) |
| 5078 | flags |= HTX_SL_F_BODYLESS; |
| 5079 | } |
| 5080 | sl->flags |= flags; |
| 5081 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 5082 | /* 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] | 5083 | * and the status code implies the presence of a message body, we must |
| 5084 | * announce a transfer encoding chunked. This is required by haproxy |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 5085 | * for the keepalive compliance. If the applet announces a transfer-encoding |
| 5086 | * chunked itself, don't do anything. |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5087 | */ |
| 5088 | 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] | 5089 | luactx->appctx->ctx.hlua_apphttp.status >= 200 && |
| 5090 | luactx->appctx->ctx.hlua_apphttp.status != 204 && |
| 5091 | luactx->appctx->ctx.hlua_apphttp.status != 304) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5092 | /* Add a new header */ |
| 5093 | sl->flags |= (HTX_SL_F_XFER_ENC|H1_MF_CHNK|H1_MF_XFER_LEN); |
| 5094 | if (!htx_add_header(htx, ist("transfer-encoding"), ist("chunked"))) { |
| 5095 | 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] | 5096 | luactx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5097 | WILL_LJMP(lua_error(L)); |
| 5098 | } |
| 5099 | } |
| 5100 | |
| 5101 | /* Finalize headers. */ |
| 5102 | if (!htx_add_endof(htx, HTX_BLK_EOH)) { |
| 5103 | hlua_pusherror(L, "Lua applet http '%s': Failed create the response.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5104 | luactx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5105 | WILL_LJMP(lua_error(L)); |
| 5106 | } |
| 5107 | |
| 5108 | if (htx_used_space(htx) > b_size(&res->buf) - global.tune.maxrewrite) { |
| 5109 | b_reset(&res->buf); |
| 5110 | hlua_pusherror(L, "Lua: 'start_response': response header block too big"); |
| 5111 | WILL_LJMP(lua_error(L)); |
| 5112 | } |
| 5113 | |
| 5114 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 5115 | channel_add_input(res, htx->data); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5116 | |
| 5117 | /* Headers sent, set the flag. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5118 | luactx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5119 | return 0; |
| 5120 | |
| 5121 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5122 | /* We will build the status line and the headers of the HTTP response. |
| 5123 | * We will try send at once if its not possible, we give back the hand |
| 5124 | * waiting for more room. |
| 5125 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5126 | __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] | 5127 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5128 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 5129 | struct stream_interface *si = luactx->appctx->owner; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5130 | struct channel *res = si_ic(si); |
| 5131 | |
| 5132 | if (co_data(res)) { |
| 5133 | si_rx_room_blk(si); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5134 | 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] | 5135 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5136 | return MAY_LJMP(hlua_applet_http_send_response(L)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5137 | } |
| 5138 | |
| 5139 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5140 | __LJMP static int hlua_applet_http_start_response(lua_State *L) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5141 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5142 | return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5143 | } |
| 5144 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5145 | /* |
| 5146 | * |
| 5147 | * |
| 5148 | * Class HTTP |
| 5149 | * |
| 5150 | * |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5151 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5152 | |
| 5153 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 5154 | * a class stream, otherwise it throws an error. |
| 5155 | */ |
| 5156 | __LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5157 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5158 | return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref)); |
| 5159 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5160 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5161 | /* This function creates and push in the stack a HTTP object |
| 5162 | * according with a current TXN. |
| 5163 | */ |
| 5164 | static int hlua_http_new(lua_State *L, struct hlua_txn *txn) |
| 5165 | { |
| 5166 | struct hlua_txn *htxn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5167 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5168 | /* Check stack size. */ |
| 5169 | if (!lua_checkstack(L, 3)) |
| 5170 | return 0; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5171 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5172 | /* Create the object: obj[0] = userdata. |
| 5173 | * Note that the base of the Converters object is the |
| 5174 | * same than the TXN object. |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5175 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5176 | lua_newtable(L); |
| 5177 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
| 5178 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5179 | |
| 5180 | htxn->s = txn->s; |
| 5181 | htxn->p = txn->p; |
Christopher Faulet | 256b69a | 2019-05-23 11:14:21 +0200 | [diff] [blame] | 5182 | htxn->dir = txn->dir; |
| 5183 | htxn->flags = txn->flags; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5184 | |
| 5185 | /* Pop a class stream metatable and affect it to the table. */ |
| 5186 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref); |
| 5187 | lua_setmetatable(L, -2); |
| 5188 | |
| 5189 | return 1; |
| 5190 | } |
| 5191 | |
| 5192 | /* This function creates ans returns an array of HTTP headers. |
| 5193 | * This function does not fails. It is used as wrapper with the |
| 5194 | * 2 following functions. |
| 5195 | */ |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 5196 | __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] | 5197 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5198 | struct htx *htx; |
| 5199 | int32_t pos; |
| 5200 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5201 | /* Create the table. */ |
| 5202 | lua_newtable(L); |
| 5203 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5204 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5205 | htx = htxbuf(&msg->chn->buf); |
| 5206 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 5207 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 5208 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 5209 | struct ist n, v; |
| 5210 | int len; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5211 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5212 | if (type == HTX_BLK_HDR) { |
| 5213 | n = htx_get_blk_name(htx,blk); |
| 5214 | v = htx_get_blk_value(htx, blk); |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5215 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5216 | else if (type == HTX_BLK_EOH) |
| 5217 | break; |
| 5218 | else |
| 5219 | continue; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5220 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5221 | /* Check for existing entry: |
| 5222 | * assume that the table is on the top of the stack, and |
| 5223 | * push the key in the stack, the function lua_gettable() |
| 5224 | * perform the lookup. |
| 5225 | */ |
| 5226 | lua_pushlstring(L, n.ptr, n.len); |
| 5227 | lua_gettable(L, -2); |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5228 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5229 | switch (lua_type(L, -1)) { |
| 5230 | case LUA_TNIL: |
| 5231 | /* Table not found, create it. */ |
| 5232 | lua_pop(L, 1); /* remove the nil value. */ |
| 5233 | lua_pushlstring(L, n.ptr, n.len); /* push the header name as key. */ |
| 5234 | lua_newtable(L); /* create and push empty table. */ |
| 5235 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 5236 | lua_rawseti(L, -2, 0); /* index header value (pop it). */ |
| 5237 | 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] | 5238 | break; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5239 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5240 | case LUA_TTABLE: |
| 5241 | /* Entry found: push the value in the table. */ |
| 5242 | len = lua_rawlen(L, -1); |
| 5243 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 5244 | lua_rawseti(L, -2, len+1); /* index header value (pop it). */ |
| 5245 | lua_pop(L, 1); /* remove the table (it is stored in the main table). */ |
| 5246 | break; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5247 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5248 | default: |
| 5249 | /* Other cases are errors. */ |
| 5250 | hlua_pusherror(L, "internal error during the parsing of headers."); |
| 5251 | WILL_LJMP(lua_error(L)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5252 | } |
| 5253 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5254 | return 1; |
| 5255 | } |
| 5256 | |
| 5257 | __LJMP static int hlua_http_req_get_headers(lua_State *L) |
| 5258 | { |
| 5259 | struct hlua_txn *htxn; |
| 5260 | |
| 5261 | MAY_LJMP(check_args(L, 1, "req_get_headers")); |
| 5262 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5263 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5264 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5265 | WILL_LJMP(lua_error(L)); |
| 5266 | |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 5267 | return hlua_http_get_headers(L, &htxn->s->txn->req); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5268 | } |
| 5269 | |
| 5270 | __LJMP static int hlua_http_res_get_headers(lua_State *L) |
| 5271 | { |
| 5272 | struct hlua_txn *htxn; |
| 5273 | |
| 5274 | MAY_LJMP(check_args(L, 1, "res_get_headers")); |
| 5275 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5276 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5277 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5278 | WILL_LJMP(lua_error(L)); |
| 5279 | |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 5280 | return hlua_http_get_headers(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5281 | } |
| 5282 | |
| 5283 | /* This function replace full header, or just a value in |
| 5284 | * the request or in the response. It is a wrapper fir the |
| 5285 | * 4 following functions. |
| 5286 | */ |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5287 | __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] | 5288 | { |
| 5289 | size_t name_len; |
| 5290 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5291 | const char *reg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 5292 | const char *value = MAY_LJMP(luaL_checkstring(L, 4)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5293 | struct htx *htx; |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 5294 | struct my_regex *re; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5295 | |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 5296 | if (!(re = regex_comp(reg, 1, 1, NULL))) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5297 | WILL_LJMP(luaL_argerror(L, 3, "invalid regex")); |
| 5298 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5299 | htx = htxbuf(&msg->chn->buf); |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5300 | 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] | 5301 | regex_free(re); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5302 | return 0; |
| 5303 | } |
| 5304 | |
| 5305 | __LJMP static int hlua_http_req_rep_hdr(lua_State *L) |
| 5306 | { |
| 5307 | struct hlua_txn *htxn; |
| 5308 | |
| 5309 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 5310 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5311 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5312 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5313 | WILL_LJMP(lua_error(L)); |
| 5314 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5315 | 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] | 5316 | } |
| 5317 | |
| 5318 | __LJMP static int hlua_http_res_rep_hdr(lua_State *L) |
| 5319 | { |
| 5320 | struct hlua_txn *htxn; |
| 5321 | |
| 5322 | MAY_LJMP(check_args(L, 4, "res_rep_hdr")); |
| 5323 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5324 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5325 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5326 | WILL_LJMP(lua_error(L)); |
| 5327 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5328 | 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] | 5329 | } |
| 5330 | |
| 5331 | __LJMP static int hlua_http_req_rep_val(lua_State *L) |
| 5332 | { |
| 5333 | struct hlua_txn *htxn; |
| 5334 | |
| 5335 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 5336 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5337 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5338 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5339 | WILL_LJMP(lua_error(L)); |
| 5340 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5341 | 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] | 5342 | } |
| 5343 | |
| 5344 | __LJMP static int hlua_http_res_rep_val(lua_State *L) |
| 5345 | { |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5346 | struct hlua_txn *htxn; |
| 5347 | |
| 5348 | MAY_LJMP(check_args(L, 4, "res_rep_val")); |
| 5349 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5350 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5351 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5352 | WILL_LJMP(lua_error(L)); |
| 5353 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5354 | 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] | 5355 | } |
| 5356 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5357 | /* This function deletes all the occurrences of an header. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5358 | * It is a wrapper for the 2 following functions. |
| 5359 | */ |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5360 | __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] | 5361 | { |
| 5362 | size_t len; |
| 5363 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5364 | struct htx *htx = htxbuf(&msg->chn->buf); |
| 5365 | struct http_hdr_ctx ctx; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5366 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5367 | ctx.blk = NULL; |
| 5368 | while (http_find_header(htx, ist2(name, len), &ctx, 1)) |
| 5369 | http_remove_header(htx, &ctx); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5370 | return 0; |
| 5371 | } |
| 5372 | |
| 5373 | __LJMP static int hlua_http_req_del_hdr(lua_State *L) |
| 5374 | { |
| 5375 | struct hlua_txn *htxn; |
| 5376 | |
| 5377 | MAY_LJMP(check_args(L, 2, "req_del_hdr")); |
| 5378 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5379 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5380 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5381 | WILL_LJMP(lua_error(L)); |
| 5382 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5383 | return hlua_http_del_hdr(L, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5384 | } |
| 5385 | |
| 5386 | __LJMP static int hlua_http_res_del_hdr(lua_State *L) |
| 5387 | { |
| 5388 | struct hlua_txn *htxn; |
| 5389 | |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5390 | MAY_LJMP(check_args(L, 2, "res_del_hdr")); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5391 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5392 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5393 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5394 | WILL_LJMP(lua_error(L)); |
| 5395 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5396 | return hlua_http_del_hdr(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5397 | } |
| 5398 | |
| 5399 | /* This function adds an header. It is a wrapper used by |
| 5400 | * the 2 following functions. |
| 5401 | */ |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5402 | __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] | 5403 | { |
| 5404 | size_t name_len; |
| 5405 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5406 | size_t value_len; |
| 5407 | const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5408 | struct htx *htx = htxbuf(&msg->chn->buf); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5409 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5410 | lua_pushboolean(L, http_add_header(htx, ist2(name, name_len), |
| 5411 | ist2(value, value_len))); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5412 | return 0; |
| 5413 | } |
| 5414 | |
| 5415 | __LJMP static int hlua_http_req_add_hdr(lua_State *L) |
| 5416 | { |
| 5417 | struct hlua_txn *htxn; |
| 5418 | |
| 5419 | MAY_LJMP(check_args(L, 3, "req_add_hdr")); |
| 5420 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5421 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5422 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5423 | WILL_LJMP(lua_error(L)); |
| 5424 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5425 | return hlua_http_add_hdr(L, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5426 | } |
| 5427 | |
| 5428 | __LJMP static int hlua_http_res_add_hdr(lua_State *L) |
| 5429 | { |
| 5430 | struct hlua_txn *htxn; |
| 5431 | |
| 5432 | MAY_LJMP(check_args(L, 3, "res_add_hdr")); |
| 5433 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5434 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5435 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5436 | WILL_LJMP(lua_error(L)); |
| 5437 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5438 | return hlua_http_add_hdr(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5439 | } |
| 5440 | |
| 5441 | static int hlua_http_req_set_hdr(lua_State *L) |
| 5442 | { |
| 5443 | struct hlua_txn *htxn; |
| 5444 | |
| 5445 | MAY_LJMP(check_args(L, 3, "req_set_hdr")); |
| 5446 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5447 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5448 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5449 | WILL_LJMP(lua_error(L)); |
| 5450 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5451 | hlua_http_del_hdr(L, &htxn->s->txn->req); |
| 5452 | return hlua_http_add_hdr(L, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5453 | } |
| 5454 | |
| 5455 | static int hlua_http_res_set_hdr(lua_State *L) |
| 5456 | { |
| 5457 | struct hlua_txn *htxn; |
| 5458 | |
| 5459 | MAY_LJMP(check_args(L, 3, "res_set_hdr")); |
| 5460 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5461 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5462 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5463 | WILL_LJMP(lua_error(L)); |
| 5464 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5465 | hlua_http_del_hdr(L, &htxn->s->txn->rsp); |
| 5466 | return hlua_http_add_hdr(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5467 | } |
| 5468 | |
| 5469 | /* This function set the method. */ |
| 5470 | static int hlua_http_req_set_meth(lua_State *L) |
| 5471 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5472 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5473 | size_t name_len; |
| 5474 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5475 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5476 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5477 | WILL_LJMP(lua_error(L)); |
| 5478 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5479 | lua_pushboolean(L, http_req_replace_stline(0, name, name_len, htxn->p, htxn->s) != -1); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5480 | return 1; |
| 5481 | } |
| 5482 | |
| 5483 | /* This function set the method. */ |
| 5484 | static int hlua_http_req_set_path(lua_State *L) |
| 5485 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5486 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5487 | size_t name_len; |
| 5488 | 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] | 5489 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5490 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5491 | WILL_LJMP(lua_error(L)); |
| 5492 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5493 | 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] | 5494 | return 1; |
| 5495 | } |
| 5496 | |
| 5497 | /* This function set the query-string. */ |
| 5498 | static int hlua_http_req_set_query(lua_State *L) |
| 5499 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5500 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5501 | size_t name_len; |
| 5502 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5503 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5504 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5505 | WILL_LJMP(lua_error(L)); |
| 5506 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5507 | /* Check length. */ |
| 5508 | if (name_len > trash.size - 1) { |
| 5509 | lua_pushboolean(L, 0); |
| 5510 | return 1; |
| 5511 | } |
| 5512 | |
| 5513 | /* Add the mark question as prefix. */ |
| 5514 | chunk_reset(&trash); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5515 | trash.area[trash.data++] = '?'; |
| 5516 | memcpy(trash.area + trash.data, name, name_len); |
| 5517 | trash.data += name_len; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5518 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5519 | lua_pushboolean(L, |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5520 | 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] | 5521 | return 1; |
| 5522 | } |
| 5523 | |
| 5524 | /* This function set the uri. */ |
| 5525 | static int hlua_http_req_set_uri(lua_State *L) |
| 5526 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5527 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5528 | size_t name_len; |
| 5529 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5530 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5531 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5532 | WILL_LJMP(lua_error(L)); |
| 5533 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5534 | 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] | 5535 | return 1; |
| 5536 | } |
| 5537 | |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5538 | /* This function set the response code & optionally reason. */ |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5539 | static int hlua_http_res_set_status(lua_State *L) |
| 5540 | { |
| 5541 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5542 | unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2)); |
Christopher Faulet | 96bff76 | 2019-12-17 13:46:18 +0100 | [diff] [blame] | 5543 | const char *str = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
| 5544 | const struct ist reason = ist2(str, (str ? strlen(str) : 0)); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5545 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5546 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5547 | WILL_LJMP(lua_error(L)); |
| 5548 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5549 | http_res_set_status(code, reason, htxn->s); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5550 | return 0; |
| 5551 | } |
| 5552 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5553 | /* |
| 5554 | * |
| 5555 | * |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5556 | * Class TXN |
| 5557 | * |
| 5558 | * |
| 5559 | */ |
| 5560 | |
| 5561 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5562 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5563 | */ |
| 5564 | __LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud) |
| 5565 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 5566 | return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref)); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5567 | } |
| 5568 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5569 | __LJMP static int hlua_set_var(lua_State *L) |
| 5570 | { |
| 5571 | struct hlua_txn *htxn; |
| 5572 | const char *name; |
| 5573 | size_t len; |
| 5574 | struct sample smp; |
| 5575 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 5576 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 5577 | 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] | 5578 | |
| 5579 | /* It is useles to retrieve the stream, but this function |
| 5580 | * runs only in a stream context. |
| 5581 | */ |
| 5582 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5583 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5584 | |
| 5585 | /* Converts the third argument in a sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 5586 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5587 | hlua_lua2smp(L, 3, &smp); |
| 5588 | |
Aurelien DARRAGON | d39fb74 | 2023-05-17 16:06:11 +0200 | [diff] [blame] | 5589 | /* Store the sample in a variable. We don't need to dup the smp, vars API |
| 5590 | * already takes care of duplicating dynamic var data. |
| 5591 | */ |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 5592 | 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] | 5593 | |
| 5594 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 5595 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 5596 | else |
| 5597 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 5598 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 5599 | return 1; |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5600 | } |
| 5601 | |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 5602 | __LJMP static int hlua_unset_var(lua_State *L) |
| 5603 | { |
| 5604 | struct hlua_txn *htxn; |
| 5605 | const char *name; |
| 5606 | size_t len; |
| 5607 | struct sample smp; |
| 5608 | |
| 5609 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 5610 | |
| 5611 | /* It is useles to retrieve the stream, but this function |
| 5612 | * runs only in a stream context. |
| 5613 | */ |
| 5614 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5615 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5616 | |
| 5617 | /* Unset the variable. */ |
| 5618 | 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] | 5619 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 5620 | return 1; |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 5621 | } |
| 5622 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5623 | __LJMP static int hlua_get_var(lua_State *L) |
| 5624 | { |
| 5625 | struct hlua_txn *htxn; |
| 5626 | const char *name; |
| 5627 | size_t len; |
| 5628 | struct sample smp; |
| 5629 | |
| 5630 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 5631 | |
| 5632 | /* It is useles to retrieve the stream, but this function |
| 5633 | * runs only in a stream context. |
| 5634 | */ |
| 5635 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5636 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5637 | |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 5638 | 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] | 5639 | if (!vars_get_by_name(name, len, &smp)) { |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5640 | lua_pushnil(L); |
| 5641 | return 1; |
| 5642 | } |
| 5643 | |
| 5644 | return hlua_smp2lua(L, &smp); |
| 5645 | } |
| 5646 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5647 | __LJMP static int hlua_set_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5648 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5649 | struct hlua *hlua; |
| 5650 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5651 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 5652 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5653 | /* It is useles to retrieve the stream, but this function |
| 5654 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5655 | */ |
| 5656 | MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 5657 | |
| 5658 | /* Get hlua struct, or NULL if we execute from main lua state */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5659 | hlua = hlua_gethlua(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 5660 | if (!hlua) |
| 5661 | return 0; |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5662 | |
| 5663 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 5664 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5665 | |
| 5666 | /* Get and store new value. */ |
| 5667 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 5668 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 5669 | |
| 5670 | return 0; |
| 5671 | } |
| 5672 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5673 | __LJMP static int hlua_get_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5674 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5675 | struct hlua *hlua; |
| 5676 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5677 | MAY_LJMP(check_args(L, 1, "get_priv")); |
| 5678 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5679 | /* It is useles to retrieve the stream, but this function |
| 5680 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5681 | */ |
| 5682 | MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 5683 | |
| 5684 | /* Get hlua struct, or NULL if we execute from main lua state */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5685 | hlua = hlua_gethlua(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 5686 | if (!hlua) { |
| 5687 | lua_pushnil(L); |
| 5688 | return 1; |
| 5689 | } |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5690 | |
| 5691 | /* Push configuration index in the stack. */ |
| 5692 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 5693 | |
| 5694 | return 1; |
| 5695 | } |
| 5696 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5697 | /* Create stack entry containing a class TXN. This function |
| 5698 | * return 0 if the stack does not contains free slots, |
| 5699 | * otherwise it returns 1. |
| 5700 | */ |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5701 | 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] | 5702 | { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5703 | struct hlua_txn *htxn; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5704 | |
| 5705 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5706 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5707 | return 0; |
| 5708 | |
| 5709 | /* NOTE: The allocation never fails. The failure |
| 5710 | * throw an error, and the function never returns. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5711 | * if the throw is not available, the process is aborted. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5712 | */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5713 | /* Create the object: obj[0] = userdata. */ |
| 5714 | lua_newtable(L); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5715 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5716 | lua_rawseti(L, -2, 0); |
| 5717 | |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5718 | htxn->s = s; |
| 5719 | htxn->p = p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 5720 | htxn->dir = dir; |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5721 | htxn->flags = flags; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5722 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 5723 | /* Create the "f" field that contains a list of fetches. */ |
| 5724 | lua_pushstring(L, "f"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 5725 | if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5726 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5727 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5728 | |
| 5729 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 5730 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 5731 | 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] | 5732 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5733 | lua_rawset(L, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 5734 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5735 | /* Create the "c" field that contains a list of converters. */ |
| 5736 | lua_pushstring(L, "c"); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5737 | if (!hlua_converters_new(L, htxn, 0)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5738 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5739 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5740 | |
| 5741 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 5742 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 5743 | if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5744 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5745 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5746 | |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5747 | /* Create the "req" field that contains the request channel object. */ |
| 5748 | lua_pushstring(L, "req"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 5749 | if (!hlua_channel_new(L, &s->req)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5750 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5751 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5752 | |
| 5753 | /* Create the "res" field that contains the response channel object. */ |
| 5754 | lua_pushstring(L, "res"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 5755 | if (!hlua_channel_new(L, &s->res)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5756 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5757 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5758 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5759 | /* Creates the HTTP object is the current proxy allows http. */ |
| 5760 | lua_pushstring(L, "http"); |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 5761 | if (IS_HTX_STRM(s)) { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5762 | if (!hlua_http_new(L, htxn)) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5763 | return 0; |
| 5764 | } |
| 5765 | else |
| 5766 | lua_pushnil(L); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5767 | lua_rawset(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5768 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5769 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 5770 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref); |
| 5771 | lua_setmetatable(L, -2); |
| 5772 | |
| 5773 | return 1; |
| 5774 | } |
| 5775 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 5776 | __LJMP static int hlua_txn_deflog(lua_State *L) |
| 5777 | { |
| 5778 | const char *msg; |
| 5779 | struct hlua_txn *htxn; |
| 5780 | |
| 5781 | MAY_LJMP(check_args(L, 2, "deflog")); |
| 5782 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5783 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5784 | |
| 5785 | hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg); |
| 5786 | return 0; |
| 5787 | } |
| 5788 | |
| 5789 | __LJMP static int hlua_txn_log(lua_State *L) |
| 5790 | { |
| 5791 | int level; |
| 5792 | const char *msg; |
| 5793 | struct hlua_txn *htxn; |
| 5794 | |
| 5795 | MAY_LJMP(check_args(L, 3, "log")); |
| 5796 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5797 | level = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5798 | msg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 5799 | |
| 5800 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 5801 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 5802 | |
| 5803 | hlua_sendlog(htxn->s->be, level, msg); |
| 5804 | return 0; |
| 5805 | } |
| 5806 | |
| 5807 | __LJMP static int hlua_txn_log_debug(lua_State *L) |
| 5808 | { |
| 5809 | const char *msg; |
| 5810 | struct hlua_txn *htxn; |
| 5811 | |
| 5812 | MAY_LJMP(check_args(L, 2, "Debug")); |
| 5813 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5814 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5815 | hlua_sendlog(htxn->s->be, LOG_DEBUG, msg); |
| 5816 | return 0; |
| 5817 | } |
| 5818 | |
| 5819 | __LJMP static int hlua_txn_log_info(lua_State *L) |
| 5820 | { |
| 5821 | const char *msg; |
| 5822 | struct hlua_txn *htxn; |
| 5823 | |
| 5824 | MAY_LJMP(check_args(L, 2, "Info")); |
| 5825 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5826 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5827 | hlua_sendlog(htxn->s->be, LOG_INFO, msg); |
| 5828 | return 0; |
| 5829 | } |
| 5830 | |
| 5831 | __LJMP static int hlua_txn_log_warning(lua_State *L) |
| 5832 | { |
| 5833 | const char *msg; |
| 5834 | struct hlua_txn *htxn; |
| 5835 | |
| 5836 | MAY_LJMP(check_args(L, 2, "Warning")); |
| 5837 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5838 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5839 | hlua_sendlog(htxn->s->be, LOG_WARNING, msg); |
| 5840 | return 0; |
| 5841 | } |
| 5842 | |
| 5843 | __LJMP static int hlua_txn_log_alert(lua_State *L) |
| 5844 | { |
| 5845 | const char *msg; |
| 5846 | struct hlua_txn *htxn; |
| 5847 | |
| 5848 | MAY_LJMP(check_args(L, 2, "Alert")); |
| 5849 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5850 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5851 | hlua_sendlog(htxn->s->be, LOG_ALERT, msg); |
| 5852 | return 0; |
| 5853 | } |
| 5854 | |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5855 | __LJMP static int hlua_txn_set_loglevel(lua_State *L) |
| 5856 | { |
| 5857 | struct hlua_txn *htxn; |
| 5858 | int ll; |
| 5859 | |
| 5860 | MAY_LJMP(check_args(L, 2, "set_loglevel")); |
| 5861 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5862 | ll = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5863 | |
Christopher Faulet | 8263e94 | 2024-02-29 15:41:17 +0100 | [diff] [blame] | 5864 | if (ll < -1 || ll > NB_LOG_LEVELS) |
| 5865 | WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be one of the following value:" |
| 5866 | " core.silent(-1), core.emerg(0), core.alert(1), core.crit(2), core.error(3)," |
| 5867 | " 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] | 5868 | |
Christopher Faulet | 8263e94 | 2024-02-29 15:41:17 +0100 | [diff] [blame] | 5869 | htxn->s->logs.level = (ll == -1) ? ll : ll + 1; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5870 | return 0; |
| 5871 | } |
| 5872 | |
| 5873 | __LJMP static int hlua_txn_set_tos(lua_State *L) |
| 5874 | { |
| 5875 | struct hlua_txn *htxn; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5876 | int tos; |
| 5877 | |
| 5878 | MAY_LJMP(check_args(L, 2, "set_tos")); |
| 5879 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5880 | tos = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5881 | |
Willy Tarreau | 1a18b54 | 2018-12-11 16:37:42 +0100 | [diff] [blame] | 5882 | conn_set_tos(objt_conn(htxn->s->sess->origin), tos); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5883 | return 0; |
| 5884 | } |
| 5885 | |
| 5886 | __LJMP static int hlua_txn_set_mark(lua_State *L) |
| 5887 | { |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5888 | struct hlua_txn *htxn; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5889 | int mark; |
| 5890 | |
| 5891 | MAY_LJMP(check_args(L, 2, "set_mark")); |
| 5892 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5893 | mark = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5894 | |
Lukas Tribus | 579e3e3 | 2019-08-11 18:03:45 +0200 | [diff] [blame] | 5895 | conn_set_mark(objt_conn(htxn->s->sess->origin), mark); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5896 | return 0; |
| 5897 | } |
| 5898 | |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 5899 | __LJMP static int hlua_txn_set_priority_class(lua_State *L) |
| 5900 | { |
| 5901 | struct hlua_txn *htxn; |
| 5902 | |
| 5903 | MAY_LJMP(check_args(L, 2, "set_priority_class")); |
| 5904 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5905 | htxn->s->priority_class = queue_limit_class(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 5906 | return 0; |
| 5907 | } |
| 5908 | |
| 5909 | __LJMP static int hlua_txn_set_priority_offset(lua_State *L) |
| 5910 | { |
| 5911 | struct hlua_txn *htxn; |
| 5912 | |
| 5913 | MAY_LJMP(check_args(L, 2, "set_priority_offset")); |
| 5914 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5915 | htxn->s->priority_offset = queue_limit_offset(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 5916 | return 0; |
| 5917 | } |
| 5918 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5919 | /* 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] | 5920 | * 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] | 5921 | * message and terminate the transaction. It returns 1 on success and 0 on |
| 5922 | * error. The Reply must be on top of the stack. |
| 5923 | */ |
| 5924 | __LJMP static int hlua_txn_forward_reply(lua_State *L, struct stream *s) |
| 5925 | { |
| 5926 | struct htx *htx; |
| 5927 | struct htx_sl *sl; |
| 5928 | struct h1m h1m; |
| 5929 | const char *status, *reason, *body; |
| 5930 | size_t status_len, reason_len, body_len; |
| 5931 | int ret, code, flags; |
| 5932 | |
| 5933 | code = 200; |
| 5934 | status = "200"; |
| 5935 | status_len = 3; |
| 5936 | ret = lua_getfield(L, -1, "status"); |
| 5937 | if (ret == LUA_TNUMBER) { |
| 5938 | code = lua_tointeger(L, -1); |
| 5939 | status = lua_tolstring(L, -1, &status_len); |
| 5940 | } |
| 5941 | lua_pop(L, 1); |
| 5942 | |
| 5943 | reason = http_get_reason(code); |
| 5944 | reason_len = strlen(reason); |
| 5945 | ret = lua_getfield(L, -1, "reason"); |
| 5946 | if (ret == LUA_TSTRING) |
| 5947 | reason = lua_tolstring(L, -1, &reason_len); |
| 5948 | lua_pop(L, 1); |
| 5949 | |
| 5950 | body = NULL; |
| 5951 | body_len = 0; |
| 5952 | ret = lua_getfield(L, -1, "body"); |
| 5953 | if (ret == LUA_TSTRING) |
| 5954 | body = lua_tolstring(L, -1, &body_len); |
| 5955 | lua_pop(L, 1); |
| 5956 | |
| 5957 | /* Prepare the response before inserting the headers */ |
| 5958 | h1m_init_res(&h1m); |
| 5959 | htx = htx_from_buf(&s->res.buf); |
| 5960 | channel_htx_truncate(&s->res, htx); |
| 5961 | if (s->txn->req.flags & HTTP_MSGF_VER_11) { |
| 5962 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11); |
| 5963 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), |
| 5964 | ist2(status, status_len), ist2(reason, reason_len)); |
| 5965 | } |
| 5966 | else { |
| 5967 | flags = HTX_SL_F_IS_RESP; |
| 5968 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), |
| 5969 | ist2(status, status_len), ist2(reason, reason_len)); |
| 5970 | } |
| 5971 | if (!sl) |
| 5972 | goto fail; |
| 5973 | sl->info.res.status = code; |
| 5974 | |
| 5975 | /* Push in the stack the "headers" entry. */ |
| 5976 | ret = lua_getfield(L, -1, "headers"); |
| 5977 | if (ret != LUA_TTABLE) |
| 5978 | goto skip_headers; |
| 5979 | |
| 5980 | lua_pushnil(L); |
| 5981 | while (lua_next(L, -2) != 0) { |
| 5982 | struct ist name, value; |
| 5983 | const char *n, *v; |
| 5984 | size_t nlen, vlen; |
| 5985 | |
| 5986 | if (!lua_isstring(L, -2) || !lua_istable(L, -1)) { |
| 5987 | /* Skip element if the key is not a string or if the value is not a table */ |
| 5988 | goto next_hdr; |
| 5989 | } |
| 5990 | |
| 5991 | n = lua_tolstring(L, -2, &nlen); |
| 5992 | name = ist2(n, nlen); |
| 5993 | if (isteqi(name, ist("content-length"))) { |
| 5994 | /* Always skip content-length header. It will be added |
| 5995 | * later with the correct len |
| 5996 | */ |
| 5997 | goto next_hdr; |
| 5998 | } |
| 5999 | |
| 6000 | /* Loop on header's values */ |
| 6001 | lua_pushnil(L); |
| 6002 | while (lua_next(L, -2)) { |
| 6003 | if (!lua_isstring(L, -1)) { |
| 6004 | /* Skip the value if it is not a string */ |
| 6005 | goto next_value; |
| 6006 | } |
| 6007 | |
| 6008 | v = lua_tolstring(L, -1, &vlen); |
| 6009 | value = ist2(v, vlen); |
| 6010 | |
| 6011 | if (isteqi(name, ist("transfer-encoding"))) |
| 6012 | h1_parse_xfer_enc_header(&h1m, value); |
| 6013 | if (!htx_add_header(htx, ist2(n, nlen), ist2(v, vlen))) |
| 6014 | goto fail; |
| 6015 | |
| 6016 | next_value: |
| 6017 | lua_pop(L, 1); |
| 6018 | } |
| 6019 | |
| 6020 | next_hdr: |
| 6021 | lua_pop(L, 1); |
| 6022 | } |
| 6023 | skip_headers: |
| 6024 | lua_pop(L, 1); |
| 6025 | |
| 6026 | /* Update h1m flags: CLEN is set if CHNK is not present */ |
| 6027 | if (!(h1m.flags & H1_MF_CHNK)) { |
| 6028 | const char *clen = ultoa(body_len); |
| 6029 | |
| 6030 | h1m.flags |= H1_MF_CLEN; |
| 6031 | if (!htx_add_header(htx, ist("content-length"), ist(clen))) |
| 6032 | goto fail; |
| 6033 | } |
| 6034 | if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK)) |
| 6035 | h1m.flags |= H1_MF_XFER_LEN; |
| 6036 | |
| 6037 | /* Update HTX start-line flags */ |
| 6038 | if (h1m.flags & H1_MF_XFER_ENC) |
| 6039 | flags |= HTX_SL_F_XFER_ENC; |
| 6040 | if (h1m.flags & H1_MF_XFER_LEN) { |
| 6041 | flags |= HTX_SL_F_XFER_LEN; |
| 6042 | if (h1m.flags & H1_MF_CHNK) |
| 6043 | flags |= HTX_SL_F_CHNK; |
| 6044 | else if (h1m.flags & H1_MF_CLEN) |
| 6045 | flags |= HTX_SL_F_CLEN; |
| 6046 | if (h1m.body_len == 0) |
| 6047 | flags |= HTX_SL_F_BODYLESS; |
| 6048 | } |
| 6049 | sl->flags |= flags; |
| 6050 | |
| 6051 | |
| 6052 | if (!htx_add_endof(htx, HTX_BLK_EOH) || |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 6053 | (body_len && !htx_add_data_atonce(htx, ist2(body, body_len)))) |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 6054 | goto fail; |
| 6055 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 6056 | htx->flags |= HTX_FL_EOM; |
| 6057 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 6058 | /* Now, forward the response and terminate the transaction */ |
| 6059 | s->txn->status = code; |
| 6060 | htx_to_buf(htx, &s->res.buf); |
| 6061 | if (!http_forward_proxy_resp(s, 1)) |
| 6062 | goto fail; |
| 6063 | |
| 6064 | return 1; |
| 6065 | |
| 6066 | fail: |
| 6067 | channel_htx_truncate(&s->res, htx); |
| 6068 | return 0; |
| 6069 | } |
| 6070 | |
| 6071 | /* Terminate a transaction if called from a lua action. For TCP streams, |
| 6072 | * processing is just aborted. Nothing is returned to the client and all |
| 6073 | * arguments are ignored. For HTTP streams, if a reply is passed as argument, it |
| 6074 | * is forwarded to the client before terminating the transaction. On success, |
| 6075 | * the function exits with ACT_RET_DONE code. If an error occurred, it exits |
| 6076 | * with ACT_RET_ERR code. If this function is not called from a lua action, it |
| 6077 | * just exits without any processing. |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 6078 | */ |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 6079 | __LJMP static int hlua_txn_done(lua_State *L) |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 6080 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 6081 | struct hlua_txn *htxn; |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 6082 | struct stream *s; |
| 6083 | int finst; |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 6084 | |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 6085 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 6086 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 6087 | /* If the flags NOTERM is set, we cannot terminate the session, so we |
| 6088 | * just end the execution of the current lua code. */ |
| 6089 | if (htxn->flags & HLUA_TXN_NOTERM) |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 6090 | WILL_LJMP(hlua_done(L)); |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 6091 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 6092 | s = htxn->s; |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 6093 | if (!IS_HTX_STRM(htxn->s)) { |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 6094 | struct channel *req = &s->req; |
| 6095 | struct channel *res = &s->res; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 6096 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 6097 | channel_auto_read(req); |
| 6098 | channel_abort(req); |
| 6099 | channel_auto_close(req); |
| 6100 | channel_erase(req); |
| 6101 | |
| 6102 | res->wex = tick_add_ifset(now_ms, res->wto); |
| 6103 | channel_auto_read(res); |
| 6104 | channel_auto_close(res); |
| 6105 | channel_shutr_now(res); |
| 6106 | |
| 6107 | finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_D); |
| 6108 | goto done; |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 6109 | } |
Thierry FOURNIER | 10ec214 | 2015-08-24 17:23:45 +0200 | [diff] [blame] | 6110 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 6111 | if (lua_gettop(L) == 1 || !lua_istable(L, 2)) { |
| 6112 | /* No reply or invalid reply */ |
| 6113 | s->txn->status = 0; |
| 6114 | http_reply_and_close(s, 0, NULL); |
| 6115 | } |
| 6116 | else { |
| 6117 | /* Remove extra args to have the reply on top of the stack */ |
| 6118 | if (lua_gettop(L) > 2) |
| 6119 | lua_pop(L, lua_gettop(L) - 2); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 6120 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 6121 | if (!hlua_txn_forward_reply(L, s)) { |
| 6122 | if (!(s->flags & SF_ERR_MASK)) |
| 6123 | s->flags |= SF_ERR_PRXCOND; |
| 6124 | lua_pushinteger(L, ACT_RET_ERR); |
| 6125 | WILL_LJMP(hlua_done(L)); |
| 6126 | return 0; /* Never reached */ |
| 6127 | } |
Christopher Faulet | 4d0e263 | 2019-07-16 10:52:40 +0200 | [diff] [blame] | 6128 | } |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 6129 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 6130 | finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_H); |
| 6131 | if (htxn->dir == SMP_OPT_DIR_REQ) { |
| 6132 | /* let's log the request time */ |
| 6133 | s->logs.tv_request = now; |
| 6134 | 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] | 6135 | _HA_ATOMIC_INC(&s->sess->fe->fe_counters.intercepted_req); |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 6136 | } |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 6137 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 6138 | done: |
| 6139 | if (!(s->flags & SF_ERR_MASK)) |
| 6140 | s->flags |= SF_ERR_LOCAL; |
| 6141 | if (!(s->flags & SF_FINST_MASK)) |
| 6142 | s->flags |= finst; |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 6143 | |
Christopher Faulet | 4ad7310 | 2020-03-05 11:07:31 +0100 | [diff] [blame] | 6144 | lua_pushinteger(L, ACT_RET_ABRT); |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 6145 | WILL_LJMP(hlua_done(L)); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 6146 | return 0; |
| 6147 | } |
| 6148 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 6149 | /* |
| 6150 | * |
| 6151 | * |
| 6152 | * Class REPLY |
| 6153 | * |
| 6154 | * |
| 6155 | */ |
| 6156 | |
| 6157 | /* Pushes the TXN reply onto the top of the stack. If the stask does not have a |
| 6158 | * free slots, the function fails and returns 0; |
| 6159 | */ |
| 6160 | static int hlua_txn_reply_new(lua_State *L) |
| 6161 | { |
| 6162 | struct hlua_txn *htxn; |
| 6163 | const char *reason, *body = NULL; |
| 6164 | int ret, status; |
| 6165 | |
| 6166 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 6167 | if (!IS_HTX_STRM(htxn->s)) { |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 6168 | hlua_pusherror(L, "txn object is not an HTTP transaction."); |
| 6169 | WILL_LJMP(lua_error(L)); |
| 6170 | } |
| 6171 | |
| 6172 | /* Default value */ |
| 6173 | status = 200; |
| 6174 | reason = http_get_reason(status); |
| 6175 | |
| 6176 | if (lua_istable(L, 2)) { |
| 6177 | /* load status and reason from the table argument at index 2 */ |
| 6178 | ret = lua_getfield(L, 2, "status"); |
| 6179 | if (ret == LUA_TNIL) |
| 6180 | goto reason; |
| 6181 | else if (ret != LUA_TNUMBER) { |
| 6182 | /* invalid status: ignore the reason */ |
| 6183 | goto body; |
| 6184 | } |
| 6185 | status = lua_tointeger(L, -1); |
| 6186 | |
| 6187 | reason: |
| 6188 | lua_pop(L, 1); /* restore the stack: remove status */ |
| 6189 | ret = lua_getfield(L, 2, "reason"); |
| 6190 | if (ret == LUA_TSTRING) |
| 6191 | reason = lua_tostring(L, -1); |
| 6192 | |
| 6193 | body: |
| 6194 | lua_pop(L, 1); /* restore the stack: remove invalid status or reason */ |
| 6195 | ret = lua_getfield(L, 2, "body"); |
| 6196 | if (ret == LUA_TSTRING) |
| 6197 | body = lua_tostring(L, -1); |
| 6198 | lua_pop(L, 1); /* restore the stack: remove body */ |
| 6199 | } |
| 6200 | |
| 6201 | /* Create the Reply table */ |
| 6202 | lua_newtable(L); |
| 6203 | |
| 6204 | /* Add status element */ |
| 6205 | lua_pushstring(L, "status"); |
| 6206 | lua_pushinteger(L, status); |
| 6207 | lua_settable(L, -3); |
| 6208 | |
| 6209 | /* Add reason element */ |
| 6210 | reason = http_get_reason(status); |
| 6211 | lua_pushstring(L, "reason"); |
| 6212 | lua_pushstring(L, reason); |
| 6213 | lua_settable(L, -3); |
| 6214 | |
| 6215 | /* Add body element, nil if undefined */ |
| 6216 | lua_pushstring(L, "body"); |
| 6217 | if (body) |
| 6218 | lua_pushstring(L, body); |
| 6219 | else |
| 6220 | lua_pushnil(L); |
| 6221 | lua_settable(L, -3); |
| 6222 | |
| 6223 | /* Add headers element */ |
| 6224 | lua_pushstring(L, "headers"); |
| 6225 | lua_newtable(L); |
| 6226 | |
| 6227 | /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */ |
| 6228 | if (lua_istable(L, 2)) { |
| 6229 | /* load headers from the table argument at index 2. If it is a table, copy it. */ |
| 6230 | ret = lua_getfield(L, 2, "headers"); |
| 6231 | if (ret == LUA_TTABLE) { |
| 6232 | /* stack: [ ... <headers:table>, <table> ] */ |
| 6233 | lua_pushnil(L); |
| 6234 | while (lua_next(L, -2) != 0) { |
| 6235 | /* stack: [ ... <headers:table>, <table>, k, v] */ |
| 6236 | if (!lua_isstring(L, -1) && !lua_istable(L, -1)) { |
| 6237 | /* invalid value type, skip it */ |
| 6238 | lua_pop(L, 1); |
| 6239 | continue; |
| 6240 | } |
| 6241 | |
| 6242 | |
| 6243 | /* Duplicate the key and swap it with the value. */ |
| 6244 | lua_pushvalue(L, -2); |
| 6245 | lua_insert(L, -2); |
| 6246 | /* stack: [ ... <headers:table>, <table>, k, k, v ] */ |
| 6247 | |
| 6248 | lua_newtable(L); |
| 6249 | lua_insert(L, -2); |
| 6250 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, v ] */ |
| 6251 | |
| 6252 | if (lua_isstring(L, -1)) { |
| 6253 | /* push the value in the inner table */ |
| 6254 | lua_rawseti(L, -2, 1); |
| 6255 | } |
| 6256 | else { /* table */ |
| 6257 | lua_pushnil(L); |
| 6258 | while (lua_next(L, -2) != 0) { |
| 6259 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2, v2 ] */ |
| 6260 | if (!lua_isstring(L, -1)) { |
| 6261 | /* invalid value type, skip it*/ |
| 6262 | lua_pop(L, 1); |
| 6263 | continue; |
| 6264 | } |
| 6265 | /* push the value in the inner table */ |
| 6266 | lua_rawseti(L, -4, lua_rawlen(L, -4) + 1); |
| 6267 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2 ] */ |
| 6268 | } |
| 6269 | lua_pop(L, 1); |
| 6270 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table> ] */ |
| 6271 | } |
| 6272 | |
| 6273 | /* push (k,v) on the stack in the headers table: |
| 6274 | * stack: [ ... <headers:table>, <table>, k, k, v ] |
| 6275 | */ |
| 6276 | lua_settable(L, -5); |
| 6277 | /* stack: [ ... <headers:table>, <table>, k ] */ |
| 6278 | } |
| 6279 | } |
| 6280 | lua_pop(L, 1); |
| 6281 | } |
| 6282 | /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */ |
| 6283 | lua_settable(L, -3); |
| 6284 | /* stack: [ txn, <Arg:table>, <Reply:table> ] */ |
| 6285 | |
| 6286 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 6287 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_reply_ref); |
| 6288 | lua_setmetatable(L, -2); |
| 6289 | return 1; |
| 6290 | } |
| 6291 | |
| 6292 | /* Set the reply status code, and optionally the reason. If no reason is |
| 6293 | * provided, the default one corresponding to the status code is used. |
| 6294 | */ |
| 6295 | __LJMP static int hlua_txn_reply_set_status(lua_State *L) |
| 6296 | { |
| 6297 | int status = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 6298 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
| 6299 | |
| 6300 | /* First argument (self) must be a table */ |
| 6301 | luaL_checktype(L, 1, LUA_TTABLE); |
| 6302 | |
| 6303 | if (status < 100 || status > 599) { |
| 6304 | lua_pushboolean(L, 0); |
| 6305 | return 1; |
| 6306 | } |
| 6307 | if (!reason) |
| 6308 | reason = http_get_reason(status); |
| 6309 | |
| 6310 | lua_pushinteger(L, status); |
| 6311 | lua_setfield(L, 1, "status"); |
| 6312 | |
| 6313 | lua_pushstring(L, reason); |
| 6314 | lua_setfield(L, 1, "reason"); |
| 6315 | |
| 6316 | lua_pushboolean(L, 1); |
| 6317 | return 1; |
| 6318 | } |
| 6319 | |
| 6320 | /* Add a header into the reply object. Each header name is associated to an |
| 6321 | * array of values in the "headers" table. If the header name is not found, a |
| 6322 | * new entry is created. |
| 6323 | */ |
| 6324 | __LJMP static int hlua_txn_reply_add_header(lua_State *L) |
| 6325 | { |
| 6326 | const char *name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 6327 | const char *value = MAY_LJMP(luaL_checkstring(L, 3)); |
| 6328 | int ret; |
| 6329 | |
| 6330 | /* First argument (self) must be a table */ |
| 6331 | luaL_checktype(L, 1, LUA_TTABLE); |
| 6332 | |
| 6333 | /* Push in the stack the "headers" entry. */ |
| 6334 | ret = lua_getfield(L, 1, "headers"); |
| 6335 | if (ret != LUA_TTABLE) { |
| 6336 | hlua_pusherror(L, "Reply['headers'] is expected to a an array. %s found", lua_typename(L, ret)); |
| 6337 | WILL_LJMP(lua_error(L)); |
| 6338 | } |
| 6339 | |
| 6340 | /* check if the header is already registered. If not, register it. */ |
| 6341 | ret = lua_getfield(L, -1, name); |
| 6342 | if (ret == LUA_TNIL) { |
| 6343 | /* Entry not found. */ |
| 6344 | lua_pop(L, 1); /* remove the nil. The "headers" table is the top of the stack. */ |
| 6345 | |
| 6346 | /* Insert the new header name in the array in the top of the stack. |
| 6347 | * It left the new array in the top of the stack. |
| 6348 | */ |
| 6349 | lua_newtable(L); |
| 6350 | lua_pushstring(L, name); |
| 6351 | lua_pushvalue(L, -2); |
| 6352 | lua_settable(L, -4); |
| 6353 | } |
| 6354 | else if (ret != LUA_TTABLE) { |
| 6355 | hlua_pusherror(L, "Reply['headers']['%s'] is expected to be an array. %s found", name, lua_typename(L, ret)); |
| 6356 | WILL_LJMP(lua_error(L)); |
| 6357 | } |
| 6358 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 6359 | /* Now the top of thestack is an array of values. We push |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 6360 | * the header value as new entry. |
| 6361 | */ |
| 6362 | lua_pushstring(L, value); |
| 6363 | ret = lua_rawlen(L, -2); |
| 6364 | lua_rawseti(L, -2, ret + 1); |
| 6365 | |
| 6366 | lua_pushboolean(L, 1); |
| 6367 | return 1; |
| 6368 | } |
| 6369 | |
| 6370 | /* Remove all occurrences of a given header name. */ |
| 6371 | __LJMP static int hlua_txn_reply_del_header(lua_State *L) |
| 6372 | { |
| 6373 | const char *name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 6374 | int ret; |
| 6375 | |
| 6376 | /* First argument (self) must be a table */ |
| 6377 | luaL_checktype(L, 1, LUA_TTABLE); |
| 6378 | |
| 6379 | /* Push in the stack the "headers" entry. */ |
| 6380 | ret = lua_getfield(L, 1, "headers"); |
| 6381 | if (ret != LUA_TTABLE) { |
| 6382 | hlua_pusherror(L, "Reply['headers'] is expected to be an array. %s found", lua_typename(L, ret)); |
| 6383 | WILL_LJMP(lua_error(L)); |
| 6384 | } |
| 6385 | |
| 6386 | lua_pushstring(L, name); |
| 6387 | lua_pushnil(L); |
| 6388 | lua_settable(L, -3); |
| 6389 | |
| 6390 | lua_pushboolean(L, 1); |
| 6391 | return 1; |
| 6392 | } |
| 6393 | |
| 6394 | /* Set the reply's body. Overwrite any existing entry. */ |
| 6395 | __LJMP static int hlua_txn_reply_set_body(lua_State *L) |
| 6396 | { |
| 6397 | const char *payload = MAY_LJMP(luaL_checkstring(L, 2)); |
| 6398 | |
| 6399 | /* First argument (self) must be a table */ |
| 6400 | luaL_checktype(L, 1, LUA_TTABLE); |
| 6401 | |
| 6402 | lua_pushstring(L, payload); |
| 6403 | lua_setfield(L, 1, "body"); |
| 6404 | |
| 6405 | lua_pushboolean(L, 1); |
| 6406 | return 1; |
| 6407 | } |
| 6408 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 6409 | __LJMP static int hlua_log(lua_State *L) |
| 6410 | { |
| 6411 | int level; |
| 6412 | const char *msg; |
| 6413 | |
| 6414 | MAY_LJMP(check_args(L, 2, "log")); |
| 6415 | level = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 6416 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 6417 | |
| 6418 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 6419 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 6420 | |
| 6421 | hlua_sendlog(NULL, level, msg); |
| 6422 | return 0; |
| 6423 | } |
| 6424 | |
| 6425 | __LJMP static int hlua_log_debug(lua_State *L) |
| 6426 | { |
| 6427 | const char *msg; |
| 6428 | |
| 6429 | MAY_LJMP(check_args(L, 1, "debug")); |
| 6430 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6431 | hlua_sendlog(NULL, LOG_DEBUG, msg); |
| 6432 | return 0; |
| 6433 | } |
| 6434 | |
| 6435 | __LJMP static int hlua_log_info(lua_State *L) |
| 6436 | { |
| 6437 | const char *msg; |
| 6438 | |
| 6439 | MAY_LJMP(check_args(L, 1, "info")); |
| 6440 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6441 | hlua_sendlog(NULL, LOG_INFO, msg); |
| 6442 | return 0; |
| 6443 | } |
| 6444 | |
| 6445 | __LJMP static int hlua_log_warning(lua_State *L) |
| 6446 | { |
| 6447 | const char *msg; |
| 6448 | |
| 6449 | MAY_LJMP(check_args(L, 1, "warning")); |
| 6450 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6451 | hlua_sendlog(NULL, LOG_WARNING, msg); |
| 6452 | return 0; |
| 6453 | } |
| 6454 | |
| 6455 | __LJMP static int hlua_log_alert(lua_State *L) |
| 6456 | { |
| 6457 | const char *msg; |
| 6458 | |
| 6459 | MAY_LJMP(check_args(L, 1, "alert")); |
| 6460 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6461 | hlua_sendlog(NULL, LOG_ALERT, msg); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 6462 | return 0; |
| 6463 | } |
| 6464 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 6465 | __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] | 6466 | { |
| 6467 | int wakeup_ms = lua_tointeger(L, -1); |
Willy Tarreau | 9cc2e4c | 2021-09-30 16:12:31 +0200 | [diff] [blame] | 6468 | if (!tick_is_expired(wakeup_ms, now_ms)) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 6469 | 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] | 6470 | return 0; |
| 6471 | } |
| 6472 | |
| 6473 | __LJMP static int hlua_sleep(lua_State *L) |
| 6474 | { |
| 6475 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6476 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6477 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6478 | MAY_LJMP(check_args(L, 1, "sleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6479 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 6480 | delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6481 | wakeup_ms = tick_add(now_ms, delay); |
| 6482 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6483 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 6484 | 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] | 6485 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6486 | } |
| 6487 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6488 | __LJMP static int hlua_msleep(lua_State *L) |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6489 | { |
| 6490 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6491 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6492 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6493 | MAY_LJMP(check_args(L, 1, "msleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6494 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 6495 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6496 | wakeup_ms = tick_add(now_ms, delay); |
| 6497 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6498 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 6499 | 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] | 6500 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6501 | } |
| 6502 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 6503 | /* This functionis an LUA binding. it permits to give back |
| 6504 | * the hand at the HAProxy scheduler. It is used when the |
| 6505 | * LUA processing consumes a lot of time. |
| 6506 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 6507 | __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] | 6508 | { |
| 6509 | return 0; |
| 6510 | } |
| 6511 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 6512 | __LJMP static int hlua_yield(lua_State *L) |
| 6513 | { |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 6514 | 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] | 6515 | return 0; |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 6516 | } |
| 6517 | |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6518 | /* This function change the nice of the currently executed |
| 6519 | * task. It is used set low or high priority at the current |
| 6520 | * task. |
| 6521 | */ |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 6522 | __LJMP static int hlua_set_nice(lua_State *L) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6523 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6524 | struct hlua *hlua; |
| 6525 | int nice; |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6526 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6527 | MAY_LJMP(check_args(L, 1, "set_nice")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6528 | nice = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6529 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 6530 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 6531 | hlua = hlua_gethlua(L); |
| 6532 | |
| 6533 | /* If the task is not set, I'm in a start mode. */ |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6534 | if (!hlua || !hlua->task) |
| 6535 | return 0; |
| 6536 | |
| 6537 | if (nice < -1024) |
| 6538 | nice = -1024; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6539 | else if (nice > 1024) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6540 | nice = 1024; |
| 6541 | |
| 6542 | hlua->task->nice = nice; |
| 6543 | return 0; |
| 6544 | } |
| 6545 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6546 | /* 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] | 6547 | * HAProxy task subsystem when the task is awaked. The LUA runtime can |
| 6548 | * return an E_AGAIN signal, the emmiter of this signal must set a |
| 6549 | * signal to wake the task. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6550 | * |
| 6551 | * Task wrapper are longjmp safe because the only one Lua code |
| 6552 | * executed is the safe hlua_ctx_resume(); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6553 | */ |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 6554 | 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] | 6555 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 6556 | struct hlua *hlua = context; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6557 | enum hlua_exec status; |
| 6558 | |
Christopher Faulet | 5bc9972 | 2018-04-25 10:34:45 +0200 | [diff] [blame] | 6559 | if (task->thread_mask == MAX_THREADS_MASK) |
| 6560 | task_set_affinity(task, tid_bit); |
| 6561 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6562 | /* If it is the first call to the task, we must initialize the |
| 6563 | * execution timeouts. |
| 6564 | */ |
| 6565 | if (!HLUA_IS_RUNNING(hlua)) |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 6566 | hlua->max_time = hlua_timeout_task; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6567 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6568 | /* Execute the Lua code. */ |
| 6569 | status = hlua_ctx_resume(hlua, 1); |
| 6570 | |
| 6571 | switch (status) { |
| 6572 | /* finished or yield */ |
| 6573 | case HLUA_E_OK: |
| 6574 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 6575 | task_destroy(task); |
Tim Duesterhus | cd235c6 | 2018-04-24 13:56:01 +0200 | [diff] [blame] | 6576 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6577 | break; |
| 6578 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6579 | case HLUA_E_AGAIN: /* co process or timeout wake me later. */ |
Thierry FOURNIER | cb14688 | 2017-12-10 17:10:57 +0100 | [diff] [blame] | 6580 | notification_gc(&hlua->com); |
PiBa-NL | fe971b3 | 2018-05-02 22:27:14 +0200 | [diff] [blame] | 6581 | task->expire = hlua->wake_time; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6582 | break; |
| 6583 | |
| 6584 | /* finished with error. */ |
| 6585 | case HLUA_E_ERRMSG: |
Aurelien DARRAGON | 6ab7ec9 | 2024-03-01 21:17:51 +0100 | [diff] [blame] | 6586 | hlua_lock(hlua); |
Aurelien DARRAGON | a374161 | 2024-03-01 15:55:17 +0100 | [diff] [blame] | 6587 | 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] | 6588 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 6589 | task_destroy(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 6590 | task = NULL; |
Aurelien DARRAGON | 6ab7ec9 | 2024-03-01 21:17:51 +0100 | [diff] [blame] | 6591 | hlua_unlock(hlua); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6592 | break; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6593 | case HLUA_E_ERR: |
| 6594 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6595 | SEND_ERR(NULL, "Lua task: unknown error.\n"); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6596 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 6597 | task_destroy(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 6598 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6599 | break; |
| 6600 | } |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 6601 | return task; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6602 | } |
| 6603 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6604 | /* This function is an LUA binding that register LUA function to be |
| 6605 | * executed after the HAProxy configuration parsing and before the |
| 6606 | * HAProxy scheduler starts. This function expect only one LUA |
| 6607 | * argument that is a function. This function returns nothing, but |
| 6608 | * throws if an error is encountered. |
| 6609 | */ |
| 6610 | __LJMP static int hlua_register_init(lua_State *L) |
| 6611 | { |
| 6612 | struct hlua_init_function *init; |
| 6613 | int ref; |
| 6614 | |
| 6615 | MAY_LJMP(check_args(L, 1, "register_init")); |
| 6616 | |
Aurelien DARRAGON | 8985ab8 | 2023-02-24 09:43:54 +0100 | [diff] [blame] | 6617 | if (hlua_gethlua(L)) { |
| 6618 | /* runtime processing */ |
| 6619 | WILL_LJMP(luaL_error(L, "register_init: not available outside of body context")); |
| 6620 | } |
| 6621 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6622 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 6623 | |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6624 | init = calloc(1, sizeof(*init)); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6625 | if (!init) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6626 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6627 | |
| 6628 | init->function_ref = ref; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 6629 | LIST_APPEND(&hlua_init_functions[hlua_state_id], &init->l); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6630 | return 0; |
| 6631 | } |
| 6632 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6633 | /* This functio is an LUA binding. It permits to register a task |
| 6634 | * executed in parallel of the main HAroxy activity. The task is |
| 6635 | * created and it is set in the HAProxy scheduler. It can be called |
| 6636 | * from the "init" section, "post init" or during the runtime. |
| 6637 | * |
| 6638 | * Lua prototype: |
| 6639 | * |
| 6640 | * <none> core.register_task(<function>) |
| 6641 | */ |
| 6642 | static int hlua_register_task(lua_State *L) |
| 6643 | { |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 6644 | struct hlua *hlua = NULL; |
| 6645 | struct task *task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6646 | int ref; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 6647 | int state_id; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6648 | |
| 6649 | MAY_LJMP(check_args(L, 1, "register_task")); |
| 6650 | |
| 6651 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 6652 | |
Thierry Fournier | 75fc029 | 2020-11-28 13:18:56 +0100 | [diff] [blame] | 6653 | /* Get the reference state. If the reference is NULL, L is the master |
| 6654 | * state, otherwise hlua->T is. |
| 6655 | */ |
| 6656 | hlua = hlua_gethlua(L); |
| 6657 | if (hlua) |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 6658 | /* we are in runtime processing */ |
| 6659 | state_id = hlua->state_id; |
Thierry Fournier | 75fc029 | 2020-11-28 13:18:56 +0100 | [diff] [blame] | 6660 | else |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 6661 | /* we are in initialization mode */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 6662 | state_id = hlua_state_id; |
Thierry Fournier | 75fc029 | 2020-11-28 13:18:56 +0100 | [diff] [blame] | 6663 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6664 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6665 | if (!hlua) |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 6666 | goto alloc_error; |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 6667 | HLUA_INIT(hlua); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6668 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 6669 | /* We are in the common lua state, execute the task anywhere, |
| 6670 | * otherwise, inherit the current thread identifier |
| 6671 | */ |
| 6672 | if (state_id == 0) |
| 6673 | task = task_new(MAX_THREADS_MASK); |
| 6674 | else |
| 6675 | task = task_new(tid_bit); |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 6676 | if (!task) |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 6677 | goto alloc_error; |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 6678 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6679 | task->context = hlua; |
| 6680 | task->process = hlua_process_task; |
| 6681 | |
Aurelien DARRAGON | 5483bde | 2023-03-21 14:02:16 +0100 | [diff] [blame] | 6682 | if (!hlua_ctx_init(hlua, state_id, task)) |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 6683 | goto alloc_error; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6684 | |
| 6685 | /* Restore the function in the stack. */ |
| 6686 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref); |
Aurelien DARRAGON | b6aade3 | 2023-03-13 14:09:21 +0100 | [diff] [blame] | 6687 | /* function ref not needed anymore since it was pushed to the substack */ |
| 6688 | hlua_unref(L, ref); |
| 6689 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6690 | hlua->nargs = 0; |
| 6691 | |
| 6692 | /* Schedule task. */ |
Willy Tarreau | 790810f | 2021-09-30 16:17:37 +0200 | [diff] [blame] | 6693 | task_wakeup(task, TASK_WOKEN_INIT); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6694 | |
| 6695 | return 0; |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 6696 | |
| 6697 | alloc_error: |
| 6698 | task_destroy(task); |
| 6699 | hlua_ctx_destroy(hlua); |
| 6700 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 6701 | return 0; /* Never reached */ |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6702 | } |
| 6703 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6704 | /* Wrapper called by HAProxy to execute an LUA converter. This wrapper |
| 6705 | * doesn't allow "yield" functions because the HAProxy engine cannot |
| 6706 | * resume converters. |
| 6707 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6708 | 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] | 6709 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 6710 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6711 | struct stream *stream = smp->strm; |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 6712 | struct hlua *hlua = NULL; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6713 | const char *error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6714 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6715 | if (!stream) |
| 6716 | return 0; |
| 6717 | |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 6718 | 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] | 6719 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 6720 | return 0; |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6721 | } |
| 6722 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6723 | /* If it is the first run, initialize the data for the call. */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 6724 | if (!HLUA_IS_RUNNING(hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6725 | |
| 6726 | /* The following Lua calls can fail. */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 6727 | if (!SET_SAFE_LJMP(hlua)) { |
| 6728 | hlua_lock(hlua); |
| 6729 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 6730 | error = hlua_tostring_safe(hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6731 | else |
| 6732 | error = "critical error"; |
| 6733 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error); |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 6734 | hlua_unlock(hlua); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6735 | return 0; |
| 6736 | } |
| 6737 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6738 | /* Check stack available size. */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 6739 | if (!lua_checkstack(hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6740 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 6741 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6742 | return 0; |
| 6743 | } |
| 6744 | |
| 6745 | /* Restore the function in the stack. */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 6746 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, fcn->function_ref[hlua->state_id]); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6747 | |
| 6748 | /* convert input sample and pust-it in the stack. */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 6749 | if (!lua_checkstack(hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6750 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 6751 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6752 | return 0; |
| 6753 | } |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 6754 | hlua_smp2lua(hlua->T, smp); |
| 6755 | hlua->nargs = 1; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6756 | |
| 6757 | /* push keywords in the stack. */ |
| 6758 | if (arg_p) { |
| 6759 | for (; arg_p->type != ARGT_STOP; arg_p++) { |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 6760 | if (!lua_checkstack(hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6761 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 6762 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6763 | return 0; |
| 6764 | } |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 6765 | hlua_arg2lua(hlua->T, arg_p); |
| 6766 | hlua->nargs++; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6767 | } |
| 6768 | } |
| 6769 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6770 | /* We must initialize the execution timeouts. */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 6771 | hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6772 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6773 | /* At this point the execution is safe. */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 6774 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6775 | } |
| 6776 | |
| 6777 | /* Execute the function. */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 6778 | switch (hlua_ctx_resume(hlua, 0)) { |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6779 | /* finished. */ |
| 6780 | case HLUA_E_OK: |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 6781 | hlua_lock(hlua); |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 6782 | /* If the stack is empty, the function fails. */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 6783 | if (lua_gettop(hlua->T) <= 0) { |
| 6784 | hlua_unlock(hlua); |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 6785 | return 0; |
Aurelien DARRAGON | 6ab7ec9 | 2024-03-01 21:17:51 +0100 | [diff] [blame] | 6786 | } |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 6787 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6788 | /* Convert the returned value in sample. */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 6789 | hlua_lua2smp(hlua->T, -1, smp); |
Aurelien DARRAGON | d39fb74 | 2023-05-17 16:06:11 +0200 | [diff] [blame] | 6790 | /* dup the smp before popping the related lua value and |
| 6791 | * returning it to haproxy |
| 6792 | */ |
| 6793 | smp_dup(smp); |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 6794 | lua_pop(hlua->T, 1); |
| 6795 | hlua_unlock(hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6796 | return 1; |
| 6797 | |
| 6798 | /* yield. */ |
| 6799 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6800 | 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] | 6801 | return 0; |
| 6802 | |
| 6803 | /* finished with error. */ |
| 6804 | case HLUA_E_ERRMSG: |
| 6805 | /* Display log. */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 6806 | hlua_lock(hlua); |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6807 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 6808 | fcn->name, hlua_tostring_safe(hlua->T, -1)); |
| 6809 | lua_pop(hlua->T, 1); |
| 6810 | hlua_unlock(hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6811 | return 0; |
| 6812 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6813 | case HLUA_E_ETMOUT: |
| 6814 | SEND_ERR(stream->be, "Lua converter '%s': execution timeout.\n", fcn->name); |
| 6815 | return 0; |
| 6816 | |
| 6817 | case HLUA_E_NOMEM: |
| 6818 | SEND_ERR(stream->be, "Lua converter '%s': out of memory error.\n", fcn->name); |
| 6819 | return 0; |
| 6820 | |
| 6821 | case HLUA_E_YIELD: |
| 6822 | SEND_ERR(stream->be, "Lua converter '%s': yield functions like core.tcp() or core.sleep() are not allowed.\n", fcn->name); |
| 6823 | return 0; |
| 6824 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6825 | case HLUA_E_ERR: |
| 6826 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6827 | 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] | 6828 | /* fall through */ |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6829 | |
| 6830 | default: |
| 6831 | return 0; |
| 6832 | } |
| 6833 | } |
| 6834 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6835 | /* Wrapper called by HAProxy to execute a sample-fetch. this wrapper |
| 6836 | * doesn't allow "yield" functions because the HAProxy engine cannot |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6837 | * resume sample-fetches. This function will be called by the sample |
| 6838 | * fetch engine to call lua-based fetch operations. |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6839 | */ |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6840 | static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp, |
| 6841 | const char *kw, void *private) |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6842 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 6843 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6844 | struct stream *stream = smp->strm; |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 6845 | struct hlua *hlua = NULL; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6846 | const char *error; |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6847 | unsigned int hflags = HLUA_TXN_NOTERM; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6848 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6849 | if (!stream) |
| 6850 | return 0; |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 6851 | |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 6852 | 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] | 6853 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 6854 | return 0; |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6855 | } |
| 6856 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6857 | /* If it is the first run, initialize the data for the call. */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 6858 | if (!HLUA_IS_RUNNING(hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6859 | |
| 6860 | /* The following Lua calls can fail. */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 6861 | if (!SET_SAFE_LJMP(hlua)) { |
| 6862 | hlua_lock(hlua); |
| 6863 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 6864 | error = hlua_tostring_safe(hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6865 | else |
| 6866 | error = "critical error"; |
| 6867 | 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] | 6868 | hlua_unlock(hlua); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6869 | return 0; |
| 6870 | } |
| 6871 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6872 | /* Check stack available size. */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 6873 | if (!lua_checkstack(hlua->T, 2)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6874 | 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] | 6875 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6876 | return 0; |
| 6877 | } |
| 6878 | |
| 6879 | /* Restore the function in the stack. */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 6880 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, fcn->function_ref[hlua->state_id]); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6881 | |
| 6882 | /* push arguments in the stack. */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 6883 | 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] | 6884 | 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] | 6885 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6886 | return 0; |
| 6887 | } |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 6888 | hlua->nargs = 1; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6889 | |
| 6890 | /* push keywords in the stack. */ |
| 6891 | for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) { |
| 6892 | /* Check stack available size. */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 6893 | if (!lua_checkstack(hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6894 | 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] | 6895 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6896 | return 0; |
| 6897 | } |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 6898 | hlua_arg2lua(hlua->T, arg_p); |
| 6899 | hlua->nargs++; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6900 | } |
| 6901 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6902 | /* We must initialize the execution timeouts. */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 6903 | hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6904 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6905 | /* At this point the execution is safe. */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 6906 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6907 | } |
| 6908 | |
| 6909 | /* Execute the function. */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 6910 | switch (hlua_ctx_resume(hlua, 0)) { |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6911 | /* finished. */ |
| 6912 | case HLUA_E_OK: |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 6913 | hlua_lock(hlua); |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 6914 | /* If the stack is empty, the function fails. */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 6915 | if (lua_gettop(hlua->T) <= 0) { |
| 6916 | hlua_unlock(hlua); |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 6917 | return 0; |
Aurelien DARRAGON | 6ab7ec9 | 2024-03-01 21:17:51 +0100 | [diff] [blame] | 6918 | } |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 6919 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6920 | /* Convert the returned value in sample. */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 6921 | hlua_lua2smp(hlua->T, -1, smp); |
Aurelien DARRAGON | d39fb74 | 2023-05-17 16:06:11 +0200 | [diff] [blame] | 6922 | /* dup the smp before popping the related lua value and |
| 6923 | * returning it to haproxy |
| 6924 | */ |
| 6925 | smp_dup(smp); |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 6926 | lua_pop(hlua->T, 1); |
| 6927 | hlua_unlock(hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6928 | |
| 6929 | /* Set the end of execution flag. */ |
| 6930 | smp->flags &= ~SMP_F_MAY_CHANGE; |
| 6931 | return 1; |
| 6932 | |
| 6933 | /* yield. */ |
| 6934 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6935 | 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] | 6936 | return 0; |
| 6937 | |
| 6938 | /* finished with error. */ |
| 6939 | case HLUA_E_ERRMSG: |
| 6940 | /* Display log. */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 6941 | hlua_lock(hlua); |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6942 | SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 6943 | fcn->name, hlua_tostring_safe(hlua->T, -1)); |
| 6944 | lua_pop(hlua->T, 1); |
| 6945 | hlua_unlock(hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6946 | return 0; |
| 6947 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6948 | case HLUA_E_ETMOUT: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6949 | SEND_ERR(smp->px, "Lua sample-fetch '%s': execution timeout.\n", fcn->name); |
| 6950 | return 0; |
| 6951 | |
| 6952 | case HLUA_E_NOMEM: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6953 | SEND_ERR(smp->px, "Lua sample-fetch '%s': out of memory error.\n", fcn->name); |
| 6954 | return 0; |
| 6955 | |
| 6956 | case HLUA_E_YIELD: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6957 | SEND_ERR(smp->px, "Lua sample-fetch '%s': yield not allowed.\n", fcn->name); |
| 6958 | return 0; |
| 6959 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6960 | case HLUA_E_ERR: |
| 6961 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6962 | 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] | 6963 | /* fall through */ |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6964 | |
| 6965 | default: |
| 6966 | return 0; |
| 6967 | } |
| 6968 | } |
| 6969 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6970 | /* This function is an LUA binding used for registering |
| 6971 | * "sample-conv" functions. It expects a converter name used |
| 6972 | * in the haproxy configuration file, and an LUA function. |
| 6973 | */ |
| 6974 | __LJMP static int hlua_register_converters(lua_State *L) |
| 6975 | { |
| 6976 | struct sample_conv_kw_list *sck; |
| 6977 | const char *name; |
| 6978 | int ref; |
| 6979 | int len; |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 6980 | struct hlua_function *fcn = NULL; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 6981 | struct sample_conv *sc; |
| 6982 | struct buffer *trash; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6983 | |
| 6984 | MAY_LJMP(check_args(L, 2, "register_converters")); |
| 6985 | |
Aurelien DARRAGON | 8985ab8 | 2023-02-24 09:43:54 +0100 | [diff] [blame] | 6986 | if (hlua_gethlua(L)) { |
| 6987 | /* runtime processing */ |
| 6988 | WILL_LJMP(luaL_error(L, "register_converters: not available outside of body context")); |
| 6989 | } |
| 6990 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6991 | /* First argument : converter name. */ |
| 6992 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6993 | |
| 6994 | /* Second argument : lua function. */ |
| 6995 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 6996 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 6997 | /* Check if the converter is already registered */ |
| 6998 | trash = get_trash_chunk(); |
| 6999 | chunk_printf(trash, "lua.%s", name); |
| 7000 | sc = find_sample_conv(trash->area, trash->data); |
| 7001 | if (sc != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 7002 | fcn = sc->private; |
| 7003 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 7004 | ha_warning("Trying to register converter 'lua.%s' more than once. " |
| 7005 | "This will become a hard error in version 2.5.\n", name); |
| 7006 | } |
| 7007 | fcn->function_ref[hlua_state_id] = ref; |
| 7008 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 7009 | } |
| 7010 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 7011 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7012 | sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 7013 | if (!sck) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 7014 | goto alloc_error; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 7015 | fcn = new_hlua_function(); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 7016 | if (!fcn) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 7017 | goto alloc_error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 7018 | |
| 7019 | /* Fill fcn. */ |
| 7020 | fcn->name = strdup(name); |
| 7021 | if (!fcn->name) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 7022 | goto alloc_error; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 7023 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 7024 | |
| 7025 | /* List head */ |
| 7026 | sck->list.n = sck->list.p = NULL; |
| 7027 | |
| 7028 | /* converter keyword. */ |
| 7029 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7030 | sck->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 7031 | if (!sck->kw[0].kw) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 7032 | goto alloc_error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 7033 | |
| 7034 | snprintf((char *)sck->kw[0].kw, len, "lua.%s", name); |
| 7035 | sck->kw[0].process = hlua_sample_conv_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 7036 | 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] | 7037 | sck->kw[0].val_args = NULL; |
| 7038 | sck->kw[0].in_type = SMP_T_STR; |
| 7039 | sck->kw[0].out_type = SMP_T_STR; |
| 7040 | sck->kw[0].private = fcn; |
| 7041 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 7042 | /* Register this new converter */ |
| 7043 | sample_register_convs(sck); |
| 7044 | |
| 7045 | return 0; |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 7046 | |
| 7047 | alloc_error: |
| 7048 | release_hlua_function(fcn); |
| 7049 | ha_free(&sck); |
| 7050 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 7051 | return 0; /* Never reached */ |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 7052 | } |
| 7053 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7054 | /* This function is an LUA binding used for registering |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 7055 | * "sample-fetch" functions. It expects a converter name used |
| 7056 | * in the haproxy configuration file, and an LUA function. |
| 7057 | */ |
| 7058 | __LJMP static int hlua_register_fetches(lua_State *L) |
| 7059 | { |
| 7060 | const char *name; |
| 7061 | int ref; |
| 7062 | int len; |
| 7063 | struct sample_fetch_kw_list *sfk; |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 7064 | struct hlua_function *fcn = NULL; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 7065 | struct sample_fetch *sf; |
| 7066 | struct buffer *trash; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 7067 | |
| 7068 | MAY_LJMP(check_args(L, 2, "register_fetches")); |
| 7069 | |
Aurelien DARRAGON | 8985ab8 | 2023-02-24 09:43:54 +0100 | [diff] [blame] | 7070 | if (hlua_gethlua(L)) { |
| 7071 | /* runtime processing */ |
| 7072 | WILL_LJMP(luaL_error(L, "register_fetches: not available outside of body context")); |
| 7073 | } |
| 7074 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 7075 | /* First argument : sample-fetch name. */ |
| 7076 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 7077 | |
| 7078 | /* Second argument : lua function. */ |
| 7079 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 7080 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 7081 | /* Check if the sample-fetch is already registered */ |
| 7082 | trash = get_trash_chunk(); |
| 7083 | chunk_printf(trash, "lua.%s", name); |
| 7084 | sf = find_sample_fetch(trash->area, trash->data); |
| 7085 | if (sf != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 7086 | fcn = sf->private; |
| 7087 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 7088 | ha_warning("Trying to register sample-fetch 'lua.%s' more than once. " |
| 7089 | "This will become a hard error in version 2.5.\n", name); |
| 7090 | } |
| 7091 | fcn->function_ref[hlua_state_id] = ref; |
| 7092 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 7093 | } |
| 7094 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 7095 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7096 | sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 7097 | if (!sfk) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 7098 | goto alloc_error; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 7099 | fcn = new_hlua_function(); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 7100 | if (!fcn) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 7101 | goto alloc_error; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 7102 | |
| 7103 | /* Fill fcn. */ |
| 7104 | fcn->name = strdup(name); |
| 7105 | if (!fcn->name) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 7106 | goto alloc_error; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 7107 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 7108 | |
| 7109 | /* List head */ |
| 7110 | sfk->list.n = sfk->list.p = NULL; |
| 7111 | |
| 7112 | /* sample-fetch keyword. */ |
| 7113 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7114 | sfk->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 7115 | if (!sfk->kw[0].kw) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 7116 | goto alloc_error; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 7117 | |
| 7118 | snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name); |
| 7119 | sfk->kw[0].process = hlua_sample_fetch_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 7120 | 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] | 7121 | sfk->kw[0].val_args = NULL; |
| 7122 | sfk->kw[0].out_type = SMP_T_STR; |
| 7123 | sfk->kw[0].use = SMP_USE_HTTP_ANY; |
| 7124 | sfk->kw[0].val = 0; |
| 7125 | sfk->kw[0].private = fcn; |
| 7126 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 7127 | /* Register this new fetch. */ |
| 7128 | sample_register_fetches(sfk); |
| 7129 | |
| 7130 | return 0; |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 7131 | |
| 7132 | alloc_error: |
| 7133 | release_hlua_function(fcn); |
| 7134 | ha_free(&sfk); |
| 7135 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 7136 | return 0; /* Never reached */ |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 7137 | } |
| 7138 | |
Christopher Faulet | 501465d | 2020-02-26 14:54:16 +0100 | [diff] [blame] | 7139 | /* This function is a lua binding to set the wake_time. |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 7140 | */ |
Christopher Faulet | 501465d | 2020-02-26 14:54:16 +0100 | [diff] [blame] | 7141 | __LJMP static int hlua_set_wake_time(lua_State *L) |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 7142 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 7143 | struct hlua *hlua; |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 7144 | unsigned int delay; |
| 7145 | unsigned int wakeup_ms; |
| 7146 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 7147 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 7148 | hlua = hlua_gethlua(L); |
| 7149 | if (!hlua) { |
| 7150 | return 0; |
| 7151 | } |
| 7152 | |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 7153 | MAY_LJMP(check_args(L, 1, "wake_time")); |
| 7154 | |
| 7155 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 7156 | wakeup_ms = tick_add(now_ms, delay); |
| 7157 | hlua->wake_time = wakeup_ms; |
| 7158 | return 0; |
| 7159 | } |
| 7160 | |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7161 | /* This function is a wrapper to execute each LUA function declared as an action |
| 7162 | * wrapper during the initialisation period. This function may return any |
| 7163 | * ACT_RET_* value. On error ACT_RET_CONT is returned and the action is |
| 7164 | * ignored. If the lua action yields, ACT_RET_YIELD is returned. On success, the |
| 7165 | * return value is the first element on the stack. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7166 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7167 | 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] | 7168 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7169 | { |
| 7170 | char **arg; |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 7171 | unsigned int hflags = 0; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7172 | int dir, act_ret = ACT_RET_CONT; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7173 | const char *error; |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 7174 | struct hlua *hlua = NULL; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7175 | |
| 7176 | switch (rule->from) { |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 7177 | case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break; |
| 7178 | case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break; |
| 7179 | case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break; |
| 7180 | case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7181 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 7182 | SEND_ERR(px, "Lua: internal error while execute action.\n"); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7183 | goto end; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7184 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7185 | |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 7186 | 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] | 7187 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
| 7188 | rule->arg.hlua_rule->fcn->name); |
| 7189 | goto end; |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 7190 | } |
| 7191 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7192 | /* If it is the first run, initialize the data for the call. */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 7193 | if (!HLUA_IS_RUNNING(hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7194 | |
| 7195 | /* The following Lua calls can fail. */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 7196 | if (!SET_SAFE_LJMP(hlua)) { |
| 7197 | hlua_lock(hlua); |
| 7198 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 7199 | error = hlua_tostring_safe(hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7200 | else |
| 7201 | error = "critical error"; |
| 7202 | SEND_ERR(px, "Lua function '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7203 | rule->arg.hlua_rule->fcn->name, error); |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 7204 | hlua_unlock(hlua); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7205 | goto end; |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7206 | } |
| 7207 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7208 | /* Check stack available size. */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 7209 | if (!lua_checkstack(hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 7210 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7211 | rule->arg.hlua_rule->fcn->name); |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 7212 | RESET_SAFE_LJMP(hlua); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7213 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7214 | } |
| 7215 | |
| 7216 | /* Restore the function in the stack. */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 7217 | 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] | 7218 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7219 | /* Create and and push object stream in the stack. */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 7220 | if (!hlua_txn_new(hlua->T, s, px, dir, hflags)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 7221 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7222 | rule->arg.hlua_rule->fcn->name); |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 7223 | RESET_SAFE_LJMP(hlua); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7224 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7225 | } |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 7226 | hlua->nargs = 1; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7227 | |
| 7228 | /* push keywords in the stack. */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7229 | for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) { |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 7230 | if (!lua_checkstack(hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 7231 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7232 | rule->arg.hlua_rule->fcn->name); |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 7233 | RESET_SAFE_LJMP(hlua); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7234 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7235 | } |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 7236 | lua_pushstring(hlua->T, *arg); |
| 7237 | hlua->nargs++; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7238 | } |
| 7239 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7240 | /* Now the execution is safe. */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 7241 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7242 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 7243 | /* We must initialize the execution timeouts. */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 7244 | hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7245 | } |
| 7246 | |
| 7247 | /* Execute the function. */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 7248 | switch (hlua_ctx_resume(hlua, !(flags & ACT_OPT_FINAL))) { |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7249 | /* finished. */ |
| 7250 | case HLUA_E_OK: |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7251 | /* Catch the return value */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 7252 | hlua_lock(hlua); |
| 7253 | if (lua_gettop(hlua->T) > 0) |
| 7254 | act_ret = lua_tointeger(hlua->T, -1); |
| 7255 | hlua_unlock(hlua); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7256 | |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 7257 | /* Set timeout in the required channel. */ |
Christopher Faulet | 498c483 | 2020-07-28 11:59:58 +0200 | [diff] [blame] | 7258 | if (act_ret == ACT_RET_YIELD) { |
| 7259 | if (flags & ACT_OPT_FINAL) |
| 7260 | goto err_yield; |
| 7261 | |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 7262 | if (dir == SMP_OPT_DIR_REQ) |
| 7263 | 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] | 7264 | hlua->wake_time); |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 7265 | else |
| 7266 | 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] | 7267 | hlua->wake_time); |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 7268 | } |
| 7269 | goto end; |
| 7270 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7271 | /* yield. */ |
| 7272 | case HLUA_E_AGAIN: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 7273 | /* Set timeout in the required channel. */ |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 7274 | if (dir == SMP_OPT_DIR_REQ) |
| 7275 | 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] | 7276 | hlua->wake_time); |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 7277 | else |
| 7278 | 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] | 7279 | hlua->wake_time); |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 7280 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7281 | /* Some actions can be wake up when a "write" event |
| 7282 | * is detected on a response channel. This is useful |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7283 | * only for actions targeted on the requests. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7284 | */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 7285 | if (HLUA_IS_WAKERESWR(hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 7286 | s->res.flags |= CF_WAKE_WRITE; |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 7287 | if (HLUA_IS_WAKEREQWR(hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 7288 | s->req.flags |= CF_WAKE_WRITE; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7289 | act_ret = ACT_RET_YIELD; |
| 7290 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7291 | |
| 7292 | /* finished with error. */ |
| 7293 | case HLUA_E_ERRMSG: |
| 7294 | /* Display log. */ |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 7295 | hlua_lock(hlua); |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 7296 | SEND_ERR(px, "Lua function '%s': %s.\n", |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 7297 | rule->arg.hlua_rule->fcn->name, hlua_tostring_safe(hlua->T, -1)); |
| 7298 | lua_pop(hlua->T, 1); |
| 7299 | hlua_unlock(hlua); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7300 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7301 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7302 | case HLUA_E_ETMOUT: |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7303 | 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] | 7304 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7305 | |
| 7306 | case HLUA_E_NOMEM: |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7307 | 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] | 7308 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7309 | |
| 7310 | case HLUA_E_YIELD: |
Christopher Faulet | 498c483 | 2020-07-28 11:59:58 +0200 | [diff] [blame] | 7311 | err_yield: |
| 7312 | act_ret = ACT_RET_CONT; |
Aurelien DARRAGON | 602c4af | 2023-08-31 21:45:21 +0200 | [diff] [blame] | 7313 | SEND_ERR(px, "Lua function '%s': yield not allowed.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7314 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7315 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7316 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7317 | case HLUA_E_ERR: |
| 7318 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 7319 | SEND_ERR(px, "Lua function '%s' return an unknown error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7320 | rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7321 | |
| 7322 | default: |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7323 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7324 | } |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7325 | |
| 7326 | end: |
Aurelien DARRAGON | d591135 | 2024-03-12 17:00:25 +0100 | [diff] [blame] | 7327 | if (act_ret != ACT_RET_YIELD && hlua) |
| 7328 | hlua->wake_time = TICK_ETERNITY; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7329 | return act_ret; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7330 | } |
| 7331 | |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 7332 | 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] | 7333 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 7334 | struct appctx *ctx = context; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7335 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7336 | appctx_wakeup(ctx); |
Willy Tarreau | d958741 | 2017-08-23 16:07:33 +0200 | [diff] [blame] | 7337 | t->expire = TICK_ETERNITY; |
Willy Tarreau | d1aa41f | 2017-07-21 16:41:56 +0200 | [diff] [blame] | 7338 | return t; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7339 | } |
| 7340 | |
| 7341 | static int hlua_applet_tcp_init(struct appctx *ctx, struct proxy *px, struct stream *strm) |
| 7342 | { |
| 7343 | struct stream_interface *si = ctx->owner; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7344 | struct hlua *hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7345 | struct task *task; |
| 7346 | char **arg; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7347 | const char *error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7348 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7349 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7350 | if (!hlua) { |
| 7351 | SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7352 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7353 | return 0; |
| 7354 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7355 | HLUA_INIT(hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7356 | ctx->ctx.hlua_apptcp.hlua = hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7357 | ctx->ctx.hlua_apptcp.flags = 0; |
| 7358 | |
| 7359 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 7360 | task = task_new(tid_bit); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7361 | if (!task) { |
| 7362 | SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7363 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7364 | return 0; |
| 7365 | } |
| 7366 | task->nice = 0; |
| 7367 | task->context = ctx; |
| 7368 | task->process = hlua_applet_wakeup; |
| 7369 | ctx->ctx.hlua_apptcp.task = task; |
| 7370 | |
| 7371 | /* In the execution wrappers linked with a stream, the |
| 7372 | * Lua context can be not initialized. This behavior |
| 7373 | * permits to save performances because a systematic |
| 7374 | * Lua initialization cause 5% performances loss. |
| 7375 | */ |
Aurelien DARRAGON | 5483bde | 2023-03-21 14:02:16 +0100 | [diff] [blame] | 7376 | 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] | 7377 | 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] | 7378 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7379 | return 0; |
| 7380 | } |
| 7381 | |
| 7382 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 7383 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7384 | |
| 7385 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7386 | if (!SET_SAFE_LJMP(hlua)) { |
Aurelien DARRAGON | d4df562 | 2024-03-04 11:17:58 +0100 | [diff] [blame] | 7387 | hlua_lock(hlua); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7388 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
Aurelien DARRAGON | a374161 | 2024-03-01 15:55:17 +0100 | [diff] [blame] | 7389 | error = hlua_tostring_safe(hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7390 | else |
| 7391 | error = "critical error"; |
| 7392 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7393 | ctx->rule->arg.hlua_rule->fcn->name, error); |
Aurelien DARRAGON | d4df562 | 2024-03-04 11:17:58 +0100 | [diff] [blame] | 7394 | hlua_unlock(hlua); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7395 | return 0; |
| 7396 | } |
| 7397 | |
| 7398 | /* Check stack available size. */ |
| 7399 | if (!lua_checkstack(hlua->T, 1)) { |
| 7400 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7401 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7402 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7403 | return 0; |
| 7404 | } |
| 7405 | |
| 7406 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 7407 | 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] | 7408 | |
| 7409 | /* Create and and push object stream in the stack. */ |
| 7410 | if (!hlua_applet_tcp_new(hlua->T, ctx)) { |
| 7411 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7412 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7413 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7414 | return 0; |
| 7415 | } |
| 7416 | hlua->nargs = 1; |
| 7417 | |
| 7418 | /* push keywords in the stack. */ |
| 7419 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 7420 | if (!lua_checkstack(hlua->T, 1)) { |
| 7421 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7422 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7423 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7424 | return 0; |
| 7425 | } |
| 7426 | lua_pushstring(hlua->T, *arg); |
| 7427 | hlua->nargs++; |
| 7428 | } |
| 7429 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7430 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7431 | |
| 7432 | /* Wakeup the applet ASAP. */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 7433 | si_cant_get(si); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 7434 | si_rx_endp_more(si); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7435 | |
| 7436 | return 1; |
| 7437 | } |
| 7438 | |
Willy Tarreau | 60409db | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 7439 | void hlua_applet_tcp_fct(struct appctx *ctx) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7440 | { |
| 7441 | struct stream_interface *si = ctx->owner; |
| 7442 | struct stream *strm = si_strm(si); |
| 7443 | struct channel *res = si_ic(si); |
| 7444 | struct act_rule *rule = ctx->rule; |
| 7445 | struct proxy *px = strm->be; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7446 | struct hlua *hlua = ctx->ctx.hlua_apptcp.hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7447 | |
| 7448 | /* The applet execution is already done. */ |
Olivier Houchard | 594c8c5 | 2018-08-28 14:41:31 +0200 | [diff] [blame] | 7449 | if (ctx->ctx.hlua_apptcp.flags & APPLET_DONE) { |
| 7450 | /* eat the whole request */ |
| 7451 | co_skip(si_oc(si), co_data(si_oc(si))); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7452 | return; |
Olivier Houchard | 594c8c5 | 2018-08-28 14:41:31 +0200 | [diff] [blame] | 7453 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7454 | |
| 7455 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 7456 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 7457 | return; |
| 7458 | |
| 7459 | /* Execute the function. */ |
| 7460 | switch (hlua_ctx_resume(hlua, 1)) { |
| 7461 | /* finished. */ |
| 7462 | case HLUA_E_OK: |
| 7463 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 7464 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7465 | /* eat the whole request */ |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 7466 | co_skip(si_oc(si), co_data(si_oc(si))); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7467 | res->flags |= CF_READ_NULL; |
| 7468 | si_shutr(si); |
| 7469 | return; |
| 7470 | |
| 7471 | /* yield. */ |
| 7472 | case HLUA_E_AGAIN: |
Thierry Fournier | 0164f20 | 2016-02-20 17:47:43 +0100 | [diff] [blame] | 7473 | if (hlua->wake_time != TICK_ETERNITY) |
| 7474 | task_schedule(ctx->ctx.hlua_apptcp.task, hlua->wake_time); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7475 | return; |
| 7476 | |
| 7477 | /* finished with error. */ |
| 7478 | case HLUA_E_ERRMSG: |
| 7479 | /* Display log. */ |
Aurelien DARRAGON | 6ab7ec9 | 2024-03-01 21:17:51 +0100 | [diff] [blame] | 7480 | hlua_lock(hlua); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7481 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
Aurelien DARRAGON | a374161 | 2024-03-01 15:55:17 +0100 | [diff] [blame] | 7482 | rule->arg.hlua_rule->fcn->name, hlua_tostring_safe(hlua->T, -1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7483 | lua_pop(hlua->T, 1); |
Aurelien DARRAGON | 6ab7ec9 | 2024-03-01 21:17:51 +0100 | [diff] [blame] | 7484 | hlua_unlock(hlua); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7485 | goto error; |
| 7486 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7487 | case HLUA_E_ETMOUT: |
| 7488 | SEND_ERR(px, "Lua applet tcp '%s': execution timeout.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7489 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7490 | goto error; |
| 7491 | |
| 7492 | case HLUA_E_NOMEM: |
| 7493 | SEND_ERR(px, "Lua applet tcp '%s': out of memory error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7494 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7495 | goto error; |
| 7496 | |
| 7497 | case HLUA_E_YIELD: /* unexpected */ |
| 7498 | SEND_ERR(px, "Lua applet tcp '%s': yield not allowed.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7499 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7500 | goto error; |
| 7501 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7502 | case HLUA_E_ERR: |
| 7503 | /* Display log. */ |
| 7504 | SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7505 | rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7506 | goto error; |
| 7507 | |
| 7508 | default: |
| 7509 | goto error; |
| 7510 | } |
| 7511 | |
| 7512 | error: |
| 7513 | |
| 7514 | /* For all other cases, just close the stream. */ |
| 7515 | si_shutw(si); |
| 7516 | si_shutr(si); |
| 7517 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 7518 | } |
| 7519 | |
| 7520 | static void hlua_applet_tcp_release(struct appctx *ctx) |
| 7521 | { |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 7522 | task_destroy(ctx->ctx.hlua_apptcp.task); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7523 | ctx->ctx.hlua_apptcp.task = NULL; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7524 | hlua_ctx_destroy(ctx->ctx.hlua_apptcp.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7525 | ctx->ctx.hlua_apptcp.hlua = NULL; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7526 | } |
| 7527 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7528 | /* The function returns 1 if the initialisation is complete, 0 if |
| 7529 | * an errors occurs and -1 if more data are required for initializing |
| 7530 | * the applet. |
| 7531 | */ |
| 7532 | static int hlua_applet_http_init(struct appctx *ctx, struct proxy *px, struct stream *strm) |
| 7533 | { |
| 7534 | struct stream_interface *si = ctx->owner; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7535 | struct http_txn *txn; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7536 | struct hlua *hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7537 | char **arg; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7538 | struct task *task; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7539 | const char *error; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7540 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7541 | txn = strm->txn; |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7542 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7543 | if (!hlua) { |
| 7544 | SEND_ERR(px, "Lua applet http '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7545 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7546 | return 0; |
| 7547 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7548 | HLUA_INIT(hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7549 | ctx->ctx.hlua_apphttp.hlua = hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7550 | ctx->ctx.hlua_apphttp.left_bytes = -1; |
| 7551 | ctx->ctx.hlua_apphttp.flags = 0; |
| 7552 | |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 7553 | if (txn->req.flags & HTTP_MSGF_VER_11) |
| 7554 | ctx->ctx.hlua_apphttp.flags |= APPLET_HTTP11; |
| 7555 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7556 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 7557 | task = task_new(tid_bit); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7558 | if (!task) { |
| 7559 | SEND_ERR(px, "Lua applet http '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7560 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7561 | return 0; |
| 7562 | } |
| 7563 | task->nice = 0; |
| 7564 | task->context = ctx; |
| 7565 | task->process = hlua_applet_wakeup; |
| 7566 | ctx->ctx.hlua_apphttp.task = task; |
| 7567 | |
| 7568 | /* In the execution wrappers linked with a stream, the |
| 7569 | * Lua context can be not initialized. This behavior |
| 7570 | * permits to save performances because a systematic |
| 7571 | * Lua initialization cause 5% performances loss. |
| 7572 | */ |
Aurelien DARRAGON | 5483bde | 2023-03-21 14:02:16 +0100 | [diff] [blame] | 7573 | 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] | 7574 | 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] | 7575 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7576 | return 0; |
| 7577 | } |
| 7578 | |
| 7579 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 7580 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7581 | |
| 7582 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7583 | if (!SET_SAFE_LJMP(hlua)) { |
Aurelien DARRAGON | d4df562 | 2024-03-04 11:17:58 +0100 | [diff] [blame] | 7584 | hlua_lock(hlua); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7585 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
Aurelien DARRAGON | a374161 | 2024-03-01 15:55:17 +0100 | [diff] [blame] | 7586 | error = hlua_tostring_safe(hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7587 | else |
| 7588 | error = "critical error"; |
| 7589 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7590 | ctx->rule->arg.hlua_rule->fcn->name, error); |
Aurelien DARRAGON | d4df562 | 2024-03-04 11:17:58 +0100 | [diff] [blame] | 7591 | hlua_unlock(hlua); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7592 | return 0; |
| 7593 | } |
| 7594 | |
| 7595 | /* Check stack available size. */ |
| 7596 | if (!lua_checkstack(hlua->T, 1)) { |
| 7597 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7598 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7599 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7600 | return 0; |
| 7601 | } |
| 7602 | |
| 7603 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 7604 | 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] | 7605 | |
| 7606 | /* Create and and push object stream in the stack. */ |
| 7607 | if (!hlua_applet_http_new(hlua->T, ctx)) { |
| 7608 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7609 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7610 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7611 | return 0; |
| 7612 | } |
| 7613 | hlua->nargs = 1; |
| 7614 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7615 | /* push keywords in the stack. */ |
| 7616 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 7617 | if (!lua_checkstack(hlua->T, 1)) { |
| 7618 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7619 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7620 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7621 | return 0; |
| 7622 | } |
| 7623 | lua_pushstring(hlua->T, *arg); |
| 7624 | hlua->nargs++; |
| 7625 | } |
| 7626 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7627 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7628 | |
| 7629 | /* Wakeup the applet when data is ready for read. */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 7630 | si_cant_get(si); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7631 | |
| 7632 | return 1; |
| 7633 | } |
| 7634 | |
Willy Tarreau | 60409db | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 7635 | void hlua_applet_http_fct(struct appctx *ctx) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7636 | { |
| 7637 | struct stream_interface *si = ctx->owner; |
| 7638 | struct stream *strm = si_strm(si); |
| 7639 | struct channel *req = si_oc(si); |
| 7640 | struct channel *res = si_ic(si); |
| 7641 | struct act_rule *rule = ctx->rule; |
| 7642 | struct proxy *px = strm->be; |
| 7643 | struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua; |
| 7644 | struct htx *req_htx, *res_htx; |
| 7645 | |
| 7646 | res_htx = htx_from_buf(&res->buf); |
| 7647 | |
| 7648 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 7649 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 7650 | goto out; |
| 7651 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 7652 | /* Check if the input buffer is available. */ |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7653 | if (!b_size(&res->buf)) { |
| 7654 | si_rx_room_blk(si); |
| 7655 | goto out; |
| 7656 | } |
| 7657 | /* check that the output is not closed */ |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7658 | if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR)) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7659 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 7660 | |
| 7661 | /* Set the currently running flag. */ |
| 7662 | if (!HLUA_IS_RUNNING(hlua) && |
| 7663 | !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
Christopher Faulet | bd878d2 | 2021-04-28 10:50:21 +0200 | [diff] [blame] | 7664 | if (!co_data(req)) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7665 | si_cant_get(si); |
| 7666 | goto out; |
| 7667 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7668 | } |
| 7669 | |
| 7670 | /* Executes The applet if it is not done. */ |
| 7671 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
| 7672 | |
| 7673 | /* Execute the function. */ |
| 7674 | switch (hlua_ctx_resume(hlua, 1)) { |
| 7675 | /* finished. */ |
| 7676 | case HLUA_E_OK: |
| 7677 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 7678 | break; |
| 7679 | |
| 7680 | /* yield. */ |
| 7681 | case HLUA_E_AGAIN: |
| 7682 | if (hlua->wake_time != TICK_ETERNITY) |
| 7683 | task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time); |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7684 | goto out; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7685 | |
| 7686 | /* finished with error. */ |
| 7687 | case HLUA_E_ERRMSG: |
| 7688 | /* Display log. */ |
Aurelien DARRAGON | 6ab7ec9 | 2024-03-01 21:17:51 +0100 | [diff] [blame] | 7689 | hlua_lock(hlua); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7690 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
Aurelien DARRAGON | a374161 | 2024-03-01 15:55:17 +0100 | [diff] [blame] | 7691 | rule->arg.hlua_rule->fcn->name, hlua_tostring_safe(hlua->T, -1)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7692 | lua_pop(hlua->T, 1); |
Aurelien DARRAGON | 6ab7ec9 | 2024-03-01 21:17:51 +0100 | [diff] [blame] | 7693 | hlua_unlock(hlua); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7694 | goto error; |
| 7695 | |
| 7696 | case HLUA_E_ETMOUT: |
| 7697 | SEND_ERR(px, "Lua applet http '%s': execution timeout.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7698 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7699 | goto error; |
| 7700 | |
| 7701 | case HLUA_E_NOMEM: |
| 7702 | SEND_ERR(px, "Lua applet http '%s': out of memory error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7703 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7704 | goto error; |
| 7705 | |
| 7706 | case HLUA_E_YIELD: /* unexpected */ |
| 7707 | SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7708 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7709 | goto error; |
| 7710 | |
| 7711 | case HLUA_E_ERR: |
| 7712 | /* Display log. */ |
| 7713 | SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7714 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7715 | goto error; |
| 7716 | |
| 7717 | default: |
| 7718 | goto error; |
| 7719 | } |
| 7720 | } |
| 7721 | |
| 7722 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7723 | if (ctx->ctx.hlua_apphttp.flags & APPLET_RSP_SENT) |
| 7724 | goto done; |
| 7725 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7726 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) |
| 7727 | goto error; |
| 7728 | |
Christopher Faulet | 868b3b1 | 2022-04-07 10:07:18 +0200 | [diff] [blame] | 7729 | /* no more data are expected. If the response buffer is empty |
| 7730 | * for a chunked message, be sure to add something (EOT block in |
| 7731 | * this case) to have something to send. It is important to be |
| 7732 | * sure the EOM flags will be handled by the endpoint. |
| 7733 | */ |
| 7734 | if (htx_is_empty(res_htx) && (strm->txn->rsp.flags & (HTTP_MSGF_XFER_LEN|HTTP_MSGF_CNT_LEN)) == HTTP_MSGF_XFER_LEN) { |
| 7735 | if (!htx_add_endof(res_htx, HTX_BLK_EOT)) { |
| 7736 | si_rx_room_blk(si); |
| 7737 | goto out; |
| 7738 | } |
| 7739 | channel_add_input(res, 1); |
| 7740 | } |
| 7741 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 7742 | res_htx->flags |= HTX_FL_EOM; |
Christopher Faulet | 79c0fc7 | 2022-03-07 15:50:54 +0100 | [diff] [blame] | 7743 | res->flags |= CF_EOI; |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7744 | strm->txn->status = ctx->ctx.hlua_apphttp.status; |
| 7745 | ctx->ctx.hlua_apphttp.flags |= APPLET_RSP_SENT; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7746 | } |
| 7747 | |
| 7748 | done: |
| 7749 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7750 | if (!(res->flags & CF_SHUTR)) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7751 | res->flags |= CF_READ_NULL; |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7752 | si_shutr(si); |
| 7753 | } |
| 7754 | |
| 7755 | /* eat the whole request */ |
| 7756 | if (co_data(req)) { |
| 7757 | req_htx = htx_from_buf(&req->buf); |
| 7758 | co_htx_skip(req, req_htx, co_data(req)); |
| 7759 | htx_to_buf(req_htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7760 | } |
| 7761 | } |
| 7762 | |
| 7763 | out: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7764 | htx_to_buf(res_htx, &res->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7765 | return; |
| 7766 | |
| 7767 | error: |
| 7768 | |
| 7769 | /* If we are in HTTP mode, and we are not send any |
| 7770 | * data, return a 500 server error in best effort: |
| 7771 | * if there is no room available in the buffer, |
| 7772 | * just close the connection. |
| 7773 | */ |
| 7774 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) { |
Christopher Faulet | f734638 | 2019-07-17 22:02:08 +0200 | [diff] [blame] | 7775 | struct buffer *err = &http_err_chunks[HTTP_ERR_500]; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7776 | |
| 7777 | channel_erase(res); |
| 7778 | res->buf.data = b_data(err); |
| 7779 | memcpy(res->buf.area, b_head(err), b_data(err)); |
| 7780 | res_htx = htx_from_buf(&res->buf); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 7781 | channel_add_input(res, res_htx->data); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7782 | } |
| 7783 | if (!(strm->flags & SF_ERR_MASK)) |
| 7784 | strm->flags |= SF_ERR_RESOURCE; |
| 7785 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 7786 | goto done; |
| 7787 | } |
| 7788 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7789 | static void hlua_applet_http_release(struct appctx *ctx) |
| 7790 | { |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 7791 | task_destroy(ctx->ctx.hlua_apphttp.task); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7792 | ctx->ctx.hlua_apphttp.task = NULL; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7793 | hlua_ctx_destroy(ctx->ctx.hlua_apphttp.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7794 | ctx->ctx.hlua_apphttp.hlua = NULL; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7795 | } |
| 7796 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7797 | /* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 7798 | * success case, else return ACT_RET_PRS_ERR. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7799 | * |
| 7800 | * This function can fail with an abort() due to an Lua critical error. |
| 7801 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 7802 | * tolerated. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7803 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7804 | static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px, |
| 7805 | struct act_rule *rule, char **err) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7806 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7807 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7808 | int i; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7809 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7810 | /* Memory for the rule. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7811 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7812 | if (!rule->arg.hlua_rule) { |
| 7813 | memprintf(err, "out of memory error"); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 7814 | goto error; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7815 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7816 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7817 | /* Memory for arguments. */ |
Tim Duesterhus | e52b6e5 | 2020-09-12 20:26:43 +0200 | [diff] [blame] | 7818 | rule->arg.hlua_rule->args = calloc(fcn->nargs + 1, |
| 7819 | sizeof(*rule->arg.hlua_rule->args)); |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7820 | if (!rule->arg.hlua_rule->args) { |
| 7821 | memprintf(err, "out of memory error"); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 7822 | goto error; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7823 | } |
| 7824 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7825 | /* Reference the Lua function and store the reference. */ |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7826 | rule->arg.hlua_rule->fcn = fcn; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7827 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7828 | /* Expect some arguments */ |
| 7829 | for (i = 0; i < fcn->nargs; i++) { |
Thierry FOURNIER | 1725c2e | 2019-01-06 19:38:49 +0100 | [diff] [blame] | 7830 | if (*args[*cur_arg] == '\0') { |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7831 | memprintf(err, "expect %d arguments", fcn->nargs); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 7832 | goto error; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7833 | } |
Thierry FOURNIER | 1725c2e | 2019-01-06 19:38:49 +0100 | [diff] [blame] | 7834 | rule->arg.hlua_rule->args[i] = strdup(args[*cur_arg]); |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7835 | if (!rule->arg.hlua_rule->args[i]) { |
| 7836 | memprintf(err, "out of memory error"); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 7837 | goto error; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7838 | } |
| 7839 | (*cur_arg)++; |
| 7840 | } |
| 7841 | rule->arg.hlua_rule->args[i] = NULL; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7842 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 7843 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7844 | rule->action_ptr = hlua_action; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 7845 | return ACT_RET_PRS_OK; |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 7846 | |
| 7847 | error: |
| 7848 | if (rule->arg.hlua_rule) { |
| 7849 | if (rule->arg.hlua_rule->args) { |
| 7850 | for (i = 0; i < fcn->nargs; i++) |
| 7851 | ha_free(&rule->arg.hlua_rule->args[i]); |
| 7852 | ha_free(&rule->arg.hlua_rule->args); |
| 7853 | } |
| 7854 | ha_free(&rule->arg.hlua_rule); |
| 7855 | } |
| 7856 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7857 | } |
| 7858 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7859 | static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px, |
| 7860 | struct act_rule *rule, char **err) |
| 7861 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7862 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7863 | |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 7864 | /* HTTP applets are forbidden in tcp-request rules. |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 7865 | * HTTP applet request requires everything initialized by |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 7866 | * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER). |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 7867 | * The applet will be immediately initialized, but its before |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 7868 | * the call of this analyzer. |
| 7869 | */ |
| 7870 | if (rule->from != ACT_F_HTTP_REQ) { |
| 7871 | memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets"); |
| 7872 | return ACT_RET_PRS_ERR; |
| 7873 | } |
| 7874 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7875 | /* Memory for the rule. */ |
| 7876 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 7877 | if (!rule->arg.hlua_rule) { |
| 7878 | memprintf(err, "out of memory error"); |
| 7879 | return ACT_RET_PRS_ERR; |
| 7880 | } |
| 7881 | |
| 7882 | /* Reference the Lua function and store the reference. */ |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7883 | rule->arg.hlua_rule->fcn = fcn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7884 | |
| 7885 | /* TODO: later accept arguments. */ |
| 7886 | rule->arg.hlua_rule->args = NULL; |
| 7887 | |
| 7888 | /* Add applet pointer in the rule. */ |
| 7889 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 7890 | rule->applet.name = fcn->name; |
| 7891 | rule->applet.init = hlua_applet_http_init; |
| 7892 | rule->applet.fct = hlua_applet_http_fct; |
| 7893 | rule->applet.release = hlua_applet_http_release; |
| 7894 | rule->applet.timeout = hlua_timeout_applet; |
| 7895 | |
| 7896 | return ACT_RET_PRS_OK; |
| 7897 | } |
| 7898 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7899 | /* This function is an LUA binding used for registering |
| 7900 | * "sample-conv" functions. It expects a converter name used |
| 7901 | * in the haproxy configuration file, and an LUA function. |
| 7902 | */ |
| 7903 | __LJMP static int hlua_register_action(lua_State *L) |
| 7904 | { |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 7905 | struct action_kw_list *akl = NULL; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7906 | const char *name; |
| 7907 | int ref; |
| 7908 | int len; |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 7909 | struct hlua_function *fcn = NULL; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7910 | int nargs; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 7911 | struct buffer *trash; |
| 7912 | struct action_kw *akw; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7913 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7914 | /* Initialise the number of expected arguments at 0. */ |
| 7915 | nargs = 0; |
| 7916 | |
| 7917 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 7918 | 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] | 7919 | |
Aurelien DARRAGON | 8985ab8 | 2023-02-24 09:43:54 +0100 | [diff] [blame] | 7920 | if (hlua_gethlua(L)) { |
| 7921 | /* runtime processing */ |
| 7922 | WILL_LJMP(luaL_error(L, "register_action: not available outside of body context")); |
| 7923 | } |
| 7924 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7925 | /* First argument : converter name. */ |
| 7926 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 7927 | |
| 7928 | /* Second argument : environment. */ |
| 7929 | if (lua_type(L, 2) != LUA_TTABLE) |
| 7930 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 7931 | |
| 7932 | /* Third argument : lua function. */ |
| 7933 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 7934 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7935 | /* 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] | 7936 | if (lua_gettop(L) >= 4) |
| 7937 | nargs = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 7938 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7939 | /* browse the second argument as an array. */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7940 | lua_pushnil(L); |
| 7941 | while (lua_next(L, 2) != 0) { |
| 7942 | if (lua_type(L, -1) != LUA_TSTRING) |
| 7943 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 7944 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 7945 | /* Check if action exists */ |
| 7946 | trash = get_trash_chunk(); |
| 7947 | chunk_printf(trash, "lua.%s", name); |
| 7948 | if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) { |
| 7949 | akw = tcp_req_cont_action(trash->area); |
| 7950 | } else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) { |
| 7951 | akw = tcp_res_cont_action(trash->area); |
| 7952 | } else if (strcmp(lua_tostring(L, -1), "http-req") == 0) { |
| 7953 | akw = action_http_req_custom(trash->area); |
| 7954 | } else if (strcmp(lua_tostring(L, -1), "http-res") == 0) { |
| 7955 | akw = action_http_res_custom(trash->area); |
| 7956 | } else { |
| 7957 | akw = NULL; |
| 7958 | } |
| 7959 | if (akw != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 7960 | fcn = akw->private; |
| 7961 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 7962 | ha_warning("Trying to register action 'lua.%s' more than once. " |
| 7963 | "This will become a hard error in version 2.5.\n", name); |
| 7964 | } |
| 7965 | fcn->function_ref[hlua_state_id] = ref; |
| 7966 | |
| 7967 | /* pop the environment string. */ |
| 7968 | lua_pop(L, 1); |
| 7969 | continue; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 7970 | } |
| 7971 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7972 | /* Check required environment. Only accepted "http" or "tcp". */ |
| 7973 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7974 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7975 | if (!akl) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 7976 | goto alloc_error;; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 7977 | fcn = new_hlua_function(); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7978 | if (!fcn) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 7979 | goto alloc_error; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7980 | |
| 7981 | /* Fill fcn. */ |
| 7982 | fcn->name = strdup(name); |
| 7983 | if (!fcn->name) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 7984 | goto alloc_error; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 7985 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7986 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 7987 | /* Set the expected number of arguments. */ |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7988 | fcn->nargs = nargs; |
| 7989 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7990 | /* List head */ |
| 7991 | akl->list.n = akl->list.p = NULL; |
| 7992 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7993 | /* action keyword. */ |
| 7994 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7995 | akl->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7996 | if (!akl->kw[0].kw) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 7997 | goto alloc_error; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7998 | |
| 7999 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 8000 | |
Amaury Denoyelle | e4a617c | 2021-05-06 15:33:09 +0200 | [diff] [blame] | 8001 | akl->kw[0].flags = 0; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 8002 | akl->kw[0].private = fcn; |
| 8003 | akl->kw[0].parse = action_register_lua; |
| 8004 | |
| 8005 | /* select the action registering point. */ |
| 8006 | if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) |
| 8007 | tcp_req_cont_keywords_register(akl); |
| 8008 | else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) |
| 8009 | tcp_res_cont_keywords_register(akl); |
| 8010 | else if (strcmp(lua_tostring(L, -1), "http-req") == 0) |
| 8011 | http_req_keywords_register(akl); |
| 8012 | else if (strcmp(lua_tostring(L, -1), "http-res") == 0) |
| 8013 | http_res_keywords_register(akl); |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 8014 | else { |
| 8015 | release_hlua_function(fcn); |
| 8016 | if (akl) |
| 8017 | ha_free((char **)&(akl->kw[0].kw)); |
| 8018 | ha_free(&akl); |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8019 | WILL_LJMP(luaL_error(L, "Lua action environment '%s' is unknown. " |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 8020 | "'tcp-req', 'tcp-res', 'http-req' or 'http-res' " |
| 8021 | "are expected.", lua_tostring(L, -1))); |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 8022 | } |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 8023 | |
| 8024 | /* pop the environment string. */ |
| 8025 | lua_pop(L, 1); |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 8026 | |
| 8027 | /* reset for next loop */ |
| 8028 | akl = NULL; |
| 8029 | fcn = NULL; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 8030 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8031 | return ACT_RET_PRS_OK; |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 8032 | |
| 8033 | alloc_error: |
| 8034 | release_hlua_function(fcn); |
| 8035 | ha_free(&akl); |
| 8036 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 8037 | return 0; /* Never reached */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8038 | } |
| 8039 | |
| 8040 | static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px, |
| 8041 | struct act_rule *rule, char **err) |
| 8042 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 8043 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8044 | |
Christopher Faulet | 280f85b | 2019-07-15 15:02:04 +0200 | [diff] [blame] | 8045 | if (px->mode == PR_MODE_HTTP) { |
| 8046 | memprintf(err, "Lua TCP services cannot be used on HTTP proxies"); |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 8047 | return ACT_RET_PRS_ERR; |
| 8048 | } |
| 8049 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8050 | /* Memory for the rule. */ |
| 8051 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 8052 | if (!rule->arg.hlua_rule) { |
| 8053 | memprintf(err, "out of memory error"); |
| 8054 | return ACT_RET_PRS_ERR; |
| 8055 | } |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 8056 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8057 | /* Reference the Lua function and store the reference. */ |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 8058 | rule->arg.hlua_rule->fcn = fcn; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8059 | |
| 8060 | /* TODO: later accept arguments. */ |
| 8061 | rule->arg.hlua_rule->args = NULL; |
| 8062 | |
| 8063 | /* Add applet pointer in the rule. */ |
| 8064 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 8065 | rule->applet.name = fcn->name; |
| 8066 | rule->applet.init = hlua_applet_tcp_init; |
| 8067 | rule->applet.fct = hlua_applet_tcp_fct; |
| 8068 | rule->applet.release = hlua_applet_tcp_release; |
| 8069 | rule->applet.timeout = hlua_timeout_applet; |
| 8070 | |
| 8071 | return 0; |
| 8072 | } |
| 8073 | |
| 8074 | /* This function is an LUA binding used for registering |
| 8075 | * "sample-conv" functions. It expects a converter name used |
| 8076 | * in the haproxy configuration file, and an LUA function. |
| 8077 | */ |
| 8078 | __LJMP static int hlua_register_service(lua_State *L) |
| 8079 | { |
| 8080 | struct action_kw_list *akl; |
| 8081 | const char *name; |
| 8082 | const char *env; |
| 8083 | int ref; |
| 8084 | int len; |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 8085 | struct hlua_function *fcn = NULL; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8086 | struct buffer *trash; |
| 8087 | struct action_kw *akw; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8088 | |
| 8089 | MAY_LJMP(check_args(L, 3, "register_service")); |
| 8090 | |
Aurelien DARRAGON | 8985ab8 | 2023-02-24 09:43:54 +0100 | [diff] [blame] | 8091 | if (hlua_gethlua(L)) { |
| 8092 | /* runtime processing */ |
| 8093 | WILL_LJMP(luaL_error(L, "register_service: not available outside of body context")); |
| 8094 | } |
| 8095 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8096 | /* First argument : converter name. */ |
| 8097 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 8098 | |
| 8099 | /* Second argument : environment. */ |
| 8100 | env = MAY_LJMP(luaL_checkstring(L, 2)); |
| 8101 | |
| 8102 | /* Third argument : lua function. */ |
| 8103 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 8104 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8105 | /* Check for service already registered */ |
| 8106 | trash = get_trash_chunk(); |
| 8107 | chunk_printf(trash, "lua.%s", name); |
| 8108 | akw = service_find(trash->area); |
| 8109 | if (akw != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8110 | fcn = akw->private; |
| 8111 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 8112 | ha_warning("Trying to register service 'lua.%s' more than once. " |
| 8113 | "This will become a hard error in version 2.5.\n", name); |
| 8114 | } |
| 8115 | fcn->function_ref[hlua_state_id] = ref; |
| 8116 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8117 | } |
| 8118 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8119 | /* Allocate and fill the sample fetch keyword struct. */ |
| 8120 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
| 8121 | if (!akl) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 8122 | goto alloc_error; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 8123 | fcn = new_hlua_function(); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8124 | if (!fcn) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 8125 | goto alloc_error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8126 | |
| 8127 | /* Fill fcn. */ |
| 8128 | len = strlen("<lua.>") + strlen(name) + 1; |
| 8129 | fcn->name = calloc(1, len); |
| 8130 | if (!fcn->name) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 8131 | goto alloc_error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8132 | snprintf((char *)fcn->name, len, "<lua.%s>", name); |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8133 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8134 | |
| 8135 | /* List head */ |
| 8136 | akl->list.n = akl->list.p = NULL; |
| 8137 | |
| 8138 | /* converter keyword. */ |
| 8139 | len = strlen("lua.") + strlen(name) + 1; |
| 8140 | akl->kw[0].kw = calloc(1, len); |
| 8141 | if (!akl->kw[0].kw) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 8142 | goto alloc_error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8143 | |
| 8144 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 8145 | |
Thierry FOURNIER / OZON.IO | 02564fd | 2016-11-12 11:07:05 +0100 | [diff] [blame] | 8146 | /* Check required environment. Only accepted "http" or "tcp". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8147 | if (strcmp(env, "tcp") == 0) |
| 8148 | akl->kw[0].parse = action_register_service_tcp; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8149 | else if (strcmp(env, "http") == 0) |
| 8150 | akl->kw[0].parse = action_register_service_http; |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 8151 | else { |
| 8152 | release_hlua_function(fcn); |
| 8153 | if (akl) |
| 8154 | ha_free((char **)&(akl->kw[0].kw)); |
| 8155 | ha_free(&akl); |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8156 | WILL_LJMP(luaL_error(L, "Lua service environment '%s' is unknown. " |
Eric Salama | fe7456f | 2017-12-21 14:30:07 +0100 | [diff] [blame] | 8157 | "'tcp' or 'http' are expected.", env)); |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 8158 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8159 | |
Amaury Denoyelle | e4a617c | 2021-05-06 15:33:09 +0200 | [diff] [blame] | 8160 | akl->kw[0].flags = 0; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8161 | akl->kw[0].private = fcn; |
| 8162 | |
| 8163 | /* End of array. */ |
| 8164 | memset(&akl->kw[1], 0, sizeof(*akl->kw)); |
| 8165 | |
| 8166 | /* Register this new converter */ |
| 8167 | service_keywords_register(akl); |
| 8168 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 8169 | return 0; |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 8170 | |
| 8171 | alloc_error: |
| 8172 | release_hlua_function(fcn); |
| 8173 | ha_free(&akl); |
| 8174 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 8175 | return 0; /* Never reached */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 8176 | } |
| 8177 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8178 | /* This function initialises Lua cli handler. It copies the |
| 8179 | * arguments in the Lua stack and create channel IO objects. |
| 8180 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 8181 | 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] | 8182 | { |
| 8183 | struct hlua *hlua; |
| 8184 | struct hlua_function *fcn; |
| 8185 | int i; |
| 8186 | const char *error; |
| 8187 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8188 | fcn = private; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 8189 | appctx->ctx.hlua_cli.fcn = private; |
| 8190 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 8191 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 8192 | if (!hlua) { |
| 8193 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 8194 | return 1; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 8195 | } |
| 8196 | HLUA_INIT(hlua); |
| 8197 | appctx->ctx.hlua_cli.hlua = hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8198 | |
| 8199 | /* Create task used by signal to wakeup applets. |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8200 | * 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] | 8201 | * applet_http. It is absolutely compatible. |
| 8202 | */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 8203 | appctx->ctx.hlua_cli.task = task_new(tid_bit); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8204 | if (!appctx->ctx.hlua_cli.task) { |
Thierry FOURNIER | ffbf569 | 2016-12-16 11:14:06 +0100 | [diff] [blame] | 8205 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 8206 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8207 | } |
| 8208 | appctx->ctx.hlua_cli.task->nice = 0; |
| 8209 | appctx->ctx.hlua_cli.task->context = appctx; |
| 8210 | appctx->ctx.hlua_cli.task->process = hlua_applet_wakeup; |
| 8211 | |
| 8212 | /* Initialises the Lua context */ |
Aurelien DARRAGON | 5483bde | 2023-03-21 14:02:16 +0100 | [diff] [blame] | 8213 | 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] | 8214 | 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] | 8215 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8216 | } |
| 8217 | |
| 8218 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8219 | if (!SET_SAFE_LJMP(hlua)) { |
Aurelien DARRAGON | d4df562 | 2024-03-04 11:17:58 +0100 | [diff] [blame] | 8220 | hlua_lock(hlua); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8221 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
Aurelien DARRAGON | a374161 | 2024-03-01 15:55:17 +0100 | [diff] [blame] | 8222 | error = hlua_tostring_safe(hlua->T, -1); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8223 | else |
| 8224 | error = "critical error"; |
| 8225 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error); |
Aurelien DARRAGON | d4df562 | 2024-03-04 11:17:58 +0100 | [diff] [blame] | 8226 | hlua_unlock(hlua); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8227 | goto error; |
| 8228 | } |
| 8229 | |
| 8230 | /* Check stack available size. */ |
| 8231 | if (!lua_checkstack(hlua->T, 2)) { |
| 8232 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 8233 | goto error; |
| 8234 | } |
| 8235 | |
| 8236 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8237 | 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] | 8238 | |
| 8239 | /* Once the arguments parsed, the CLI is like an AppletTCP, |
| 8240 | * so push AppletTCP in the stack. |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8241 | */ |
| 8242 | if (!hlua_applet_tcp_new(hlua->T, appctx)) { |
| 8243 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 8244 | goto error; |
| 8245 | } |
| 8246 | hlua->nargs = 1; |
| 8247 | |
| 8248 | /* push keywords in the stack. */ |
| 8249 | for (i = 0; *args[i]; i++) { |
| 8250 | /* Check stack available size. */ |
| 8251 | if (!lua_checkstack(hlua->T, 1)) { |
| 8252 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 8253 | goto error; |
| 8254 | } |
| 8255 | lua_pushstring(hlua->T, args[i]); |
| 8256 | hlua->nargs++; |
| 8257 | } |
| 8258 | |
| 8259 | /* We must initialize the execution timeouts. */ |
| 8260 | hlua->max_time = hlua_timeout_session; |
| 8261 | |
| 8262 | /* At this point the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8263 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8264 | |
| 8265 | /* It's ok */ |
| 8266 | return 0; |
| 8267 | |
| 8268 | /* It's not ok. */ |
| 8269 | error: |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8270 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8271 | hlua_ctx_destroy(hlua); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 8272 | appctx->ctx.hlua_cli.hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8273 | return 1; |
| 8274 | } |
| 8275 | |
| 8276 | static int hlua_cli_io_handler_fct(struct appctx *appctx) |
| 8277 | { |
| 8278 | struct hlua *hlua; |
| 8279 | struct stream_interface *si; |
| 8280 | struct hlua_function *fcn; |
| 8281 | |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 8282 | hlua = appctx->ctx.hlua_cli.hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8283 | si = appctx->owner; |
Willy Tarreau | 8ae4f75 | 2016-12-14 15:41:45 +0100 | [diff] [blame] | 8284 | fcn = appctx->ctx.hlua_cli.fcn; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8285 | |
| 8286 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 8287 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 8288 | return 1; |
| 8289 | |
| 8290 | /* Execute the function. */ |
| 8291 | switch (hlua_ctx_resume(hlua, 1)) { |
| 8292 | |
| 8293 | /* finished. */ |
| 8294 | case HLUA_E_OK: |
| 8295 | return 1; |
| 8296 | |
| 8297 | /* yield. */ |
| 8298 | case HLUA_E_AGAIN: |
| 8299 | /* We want write. */ |
| 8300 | if (HLUA_IS_WAKERESWR(hlua)) |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 8301 | si_rx_room_blk(si); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8302 | /* Set the timeout. */ |
| 8303 | if (hlua->wake_time != TICK_ETERNITY) |
| 8304 | task_schedule(hlua->task, hlua->wake_time); |
| 8305 | return 0; |
| 8306 | |
| 8307 | /* finished with error. */ |
| 8308 | case HLUA_E_ERRMSG: |
| 8309 | /* Display log. */ |
Aurelien DARRAGON | 6ab7ec9 | 2024-03-01 21:17:51 +0100 | [diff] [blame] | 8310 | hlua_lock(hlua); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8311 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", |
Aurelien DARRAGON | a374161 | 2024-03-01 15:55:17 +0100 | [diff] [blame] | 8312 | fcn->name, hlua_tostring_safe(hlua->T, -1)); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8313 | lua_pop(hlua->T, 1); |
Aurelien DARRAGON | 6ab7ec9 | 2024-03-01 21:17:51 +0100 | [diff] [blame] | 8314 | hlua_unlock(hlua); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8315 | return 1; |
| 8316 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 8317 | case HLUA_E_ETMOUT: |
| 8318 | SEND_ERR(NULL, "Lua converter '%s': execution timeout.\n", |
| 8319 | fcn->name); |
| 8320 | return 1; |
| 8321 | |
| 8322 | case HLUA_E_NOMEM: |
| 8323 | SEND_ERR(NULL, "Lua converter '%s': out of memory error.\n", |
| 8324 | fcn->name); |
| 8325 | return 1; |
| 8326 | |
| 8327 | case HLUA_E_YIELD: /* unexpected */ |
| 8328 | SEND_ERR(NULL, "Lua converter '%s': yield not allowed.\n", |
| 8329 | fcn->name); |
| 8330 | return 1; |
| 8331 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8332 | case HLUA_E_ERR: |
| 8333 | /* Display log. */ |
| 8334 | SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n", |
| 8335 | fcn->name); |
| 8336 | return 1; |
| 8337 | |
| 8338 | default: |
| 8339 | return 1; |
| 8340 | } |
| 8341 | |
| 8342 | return 1; |
| 8343 | } |
| 8344 | |
| 8345 | static void hlua_cli_io_release_fct(struct appctx *appctx) |
| 8346 | { |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 8347 | hlua_ctx_destroy(appctx->ctx.hlua_cli.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 8348 | appctx->ctx.hlua_cli.hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8349 | } |
| 8350 | |
| 8351 | /* This function is an LUA binding used for registering |
| 8352 | * new keywords in the cli. It expects a list of keywords |
| 8353 | * which are the "path". It is limited to 5 keywords. A |
| 8354 | * description of the command, a function to be executed |
| 8355 | * for the parsing and a function for io handlers. |
| 8356 | */ |
| 8357 | __LJMP static int hlua_register_cli(lua_State *L) |
| 8358 | { |
| 8359 | struct cli_kw_list *cli_kws; |
| 8360 | const char *message; |
| 8361 | int ref_io; |
| 8362 | int len; |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 8363 | struct hlua_function *fcn = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8364 | int index; |
| 8365 | int i; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8366 | struct buffer *trash; |
| 8367 | const char *kw[5]; |
| 8368 | struct cli_kw *cli_kw; |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 8369 | const char *errmsg; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8370 | |
| 8371 | MAY_LJMP(check_args(L, 3, "register_cli")); |
| 8372 | |
Aurelien DARRAGON | 8985ab8 | 2023-02-24 09:43:54 +0100 | [diff] [blame] | 8373 | if (hlua_gethlua(L)) { |
| 8374 | /* runtime processing */ |
| 8375 | WILL_LJMP(luaL_error(L, "register_cli: not available outside of body context")); |
| 8376 | } |
| 8377 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8378 | /* First argument : an array of maximum 5 keywords. */ |
| 8379 | if (!lua_istable(L, 1)) |
| 8380 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table")); |
| 8381 | |
| 8382 | /* Second argument : string with contextual message. */ |
| 8383 | message = MAY_LJMP(luaL_checkstring(L, 2)); |
| 8384 | |
| 8385 | /* Third and fourth argument : lua function. */ |
| 8386 | ref_io = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 8387 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8388 | /* Check for CLI service already registered */ |
| 8389 | trash = get_trash_chunk(); |
| 8390 | index = 0; |
| 8391 | lua_pushnil(L); |
| 8392 | memset(kw, 0, sizeof(kw)); |
| 8393 | while (lua_next(L, 1) != 0) { |
| 8394 | if (index >= CLI_PREFIX_KW_NB) |
| 8395 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries")); |
| 8396 | if (lua_type(L, -1) != LUA_TSTRING) |
| 8397 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings")); |
| 8398 | kw[index] = lua_tostring(L, -1); |
| 8399 | if (index == 0) |
| 8400 | chunk_printf(trash, "%s", kw[index]); |
| 8401 | else |
| 8402 | chunk_appendf(trash, " %s", kw[index]); |
| 8403 | index++; |
| 8404 | lua_pop(L, 1); |
| 8405 | } |
| 8406 | cli_kw = cli_find_kw_exact((char **)kw); |
| 8407 | if (cli_kw != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8408 | fcn = cli_kw->private; |
| 8409 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 8410 | ha_warning("Trying to register CLI keyword 'lua.%s' more than once. " |
| 8411 | "This will become a hard error in version 2.5.\n", trash->area); |
| 8412 | } |
| 8413 | fcn->function_ref[hlua_state_id] = ref_io; |
| 8414 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8415 | } |
| 8416 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8417 | /* Allocate and fill the sample fetch keyword struct. */ |
| 8418 | cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 8419 | if (!cli_kws) { |
| 8420 | errmsg = "Lua out of memory error."; |
| 8421 | goto error; |
| 8422 | } |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 8423 | fcn = new_hlua_function(); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 8424 | if (!fcn) { |
| 8425 | errmsg = "Lua out of memory error."; |
| 8426 | goto error; |
| 8427 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8428 | |
| 8429 | /* Fill path. */ |
| 8430 | index = 0; |
| 8431 | lua_pushnil(L); |
| 8432 | while(lua_next(L, 1) != 0) { |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 8433 | if (index >= 5) { |
| 8434 | errmsg = "1st argument must be a table with a maximum of 5 entries"; |
| 8435 | goto error; |
| 8436 | } |
| 8437 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 8438 | errmsg = "1st argument must be a table filled with strings"; |
| 8439 | goto error; |
| 8440 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8441 | cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1)); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 8442 | if (!cli_kws->kw[0].str_kw[index]) { |
| 8443 | errmsg = "Lua out of memory error."; |
| 8444 | goto error; |
| 8445 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8446 | index++; |
| 8447 | lua_pop(L, 1); |
| 8448 | } |
| 8449 | |
| 8450 | /* Copy help message. */ |
| 8451 | cli_kws->kw[0].usage = strdup(message); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 8452 | if (!cli_kws->kw[0].usage) { |
| 8453 | errmsg = "Lua out of memory error."; |
| 8454 | goto error; |
| 8455 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8456 | |
| 8457 | /* Fill fcn io handler. */ |
| 8458 | len = strlen("<lua.cli>") + 1; |
| 8459 | for (i = 0; i < index; i++) |
| 8460 | len += strlen(cli_kws->kw[0].str_kw[i]) + 1; |
| 8461 | fcn->name = calloc(1, len); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 8462 | if (!fcn->name) { |
| 8463 | errmsg = "Lua out of memory error."; |
| 8464 | goto error; |
| 8465 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8466 | strncat((char *)fcn->name, "<lua.cli", len); |
| 8467 | for (i = 0; i < index; i++) { |
| 8468 | strncat((char *)fcn->name, ".", len); |
| 8469 | strncat((char *)fcn->name, cli_kws->kw[0].str_kw[i], len); |
| 8470 | } |
| 8471 | strncat((char *)fcn->name, ">", len); |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8472 | fcn->function_ref[hlua_state_id] = ref_io; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8473 | |
| 8474 | /* Fill last entries. */ |
| 8475 | cli_kws->kw[0].private = fcn; |
| 8476 | cli_kws->kw[0].parse = hlua_cli_parse_fct; |
| 8477 | cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct; |
| 8478 | cli_kws->kw[0].io_release = hlua_cli_io_release_fct; |
| 8479 | |
| 8480 | /* Register this new converter */ |
| 8481 | cli_register_kw(cli_kws); |
| 8482 | |
| 8483 | return 0; |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 8484 | |
| 8485 | error: |
| 8486 | release_hlua_function(fcn); |
| 8487 | if (cli_kws) { |
| 8488 | for (i = 0; i < index; i++) |
| 8489 | ha_free((char **)&(cli_kws->kw[0].str_kw[i])); |
| 8490 | ha_free((char **)&(cli_kws->kw[0].usage)); |
| 8491 | } |
| 8492 | ha_free(&cli_kws); |
| 8493 | WILL_LJMP(luaL_error(L, errmsg)); |
| 8494 | return 0; /* Never reached */ |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8495 | } |
| 8496 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8497 | 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] | 8498 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8499 | char **err, unsigned int *timeout) |
| 8500 | { |
| 8501 | const char *error; |
| 8502 | |
| 8503 | error = parse_time_err(args[1], timeout, TIME_UNIT_MS); |
Willy Tarreau | 9faebe3 | 2019-06-07 19:00:37 +0200 | [diff] [blame] | 8504 | if (error == PARSE_TIME_OVER) { |
| 8505 | memprintf(err, "timer overflow in argument <%s> to <%s> (maximum value is 2147483647 ms or ~24.8 days)", |
| 8506 | args[1], args[0]); |
| 8507 | return -1; |
| 8508 | } |
| 8509 | else if (error == PARSE_TIME_UNDER) { |
| 8510 | memprintf(err, "timer underflow in argument <%s> to <%s> (minimum non-null value is 1 ms)", |
| 8511 | args[1], args[0]); |
| 8512 | return -1; |
| 8513 | } |
| 8514 | else if (error) { |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8515 | memprintf(err, "%s: invalid timeout", args[0]); |
| 8516 | return -1; |
| 8517 | } |
| 8518 | return 0; |
| 8519 | } |
| 8520 | |
| 8521 | 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] | 8522 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8523 | char **err) |
| 8524 | { |
| 8525 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 8526 | file, line, err, &hlua_timeout_session); |
| 8527 | } |
| 8528 | |
| 8529 | 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] | 8530 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8531 | char **err) |
| 8532 | { |
| 8533 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 8534 | file, line, err, &hlua_timeout_task); |
| 8535 | } |
| 8536 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8537 | 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] | 8538 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8539 | char **err) |
| 8540 | { |
| 8541 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 8542 | file, line, err, &hlua_timeout_applet); |
| 8543 | } |
| 8544 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 8545 | 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] | 8546 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 8547 | char **err) |
| 8548 | { |
| 8549 | char *error; |
| 8550 | |
| 8551 | hlua_nb_instruction = strtoll(args[1], &error, 10); |
| 8552 | if (*error != '\0') { |
| 8553 | memprintf(err, "%s: invalid number", args[0]); |
| 8554 | return -1; |
| 8555 | } |
| 8556 | return 0; |
| 8557 | } |
| 8558 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8559 | 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] | 8560 | const struct proxy *defpx, const char *file, int line, |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8561 | char **err) |
| 8562 | { |
| 8563 | char *error; |
| 8564 | |
| 8565 | if (*(args[1]) == 0) { |
| 8566 | memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]); |
| 8567 | return -1; |
| 8568 | } |
| 8569 | hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L; |
| 8570 | if (*error != '\0') { |
| 8571 | memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error); |
| 8572 | return -1; |
| 8573 | } |
| 8574 | return 0; |
| 8575 | } |
| 8576 | |
| 8577 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8578 | /* This function is called by the main configuration key "lua-load". It loads and |
| 8579 | * execute an lua file during the parsing of the HAProxy configuration file. It is |
| 8580 | * the main lua entry point. |
| 8581 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 8582 | * This function runs with the HAProxy keywords API. It returns -1 if an error |
| 8583 | * occurs, otherwise it returns 0. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8584 | * |
| 8585 | * In some error case, LUA set an error message in top of the stack. This function |
| 8586 | * 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] | 8587 | * |
| 8588 | * This function can fail with an abort() due to an Lua critical error. |
| 8589 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 8590 | * tolerated. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8591 | */ |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8592 | static int hlua_load_state(char *filename, lua_State *L, char **err) |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8593 | { |
| 8594 | int error; |
| 8595 | |
| 8596 | /* Just load and compile the file. */ |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8597 | error = luaL_loadfile(L, filename); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8598 | if (error) { |
Aurelien DARRAGON | a374161 | 2024-03-01 15:55:17 +0100 | [diff] [blame] | 8599 | 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] | 8600 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8601 | return -1; |
| 8602 | } |
| 8603 | |
| 8604 | /* If no syntax error where detected, execute the code. */ |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8605 | error = lua_pcall(L, 0, LUA_MULTRET, 0); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8606 | switch (error) { |
| 8607 | case LUA_OK: |
| 8608 | break; |
| 8609 | case LUA_ERRRUN: |
Aurelien DARRAGON | a374161 | 2024-03-01 15:55:17 +0100 | [diff] [blame] | 8610 | memprintf(err, "Lua runtime error: %s\n", hlua_tostring_safe(L, -1)); |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8611 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8612 | return -1; |
| 8613 | case LUA_ERRMEM: |
Thierry Fournier | de6145f | 2020-11-29 00:55:53 +0100 | [diff] [blame] | 8614 | memprintf(err, "Lua out of memory error\n"); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8615 | return -1; |
| 8616 | case LUA_ERRERR: |
Aurelien DARRAGON | a374161 | 2024-03-01 15:55:17 +0100 | [diff] [blame] | 8617 | 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] | 8618 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8619 | return -1; |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 8620 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM <= 503 |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8621 | case LUA_ERRGCMM: |
Aurelien DARRAGON | a374161 | 2024-03-01 15:55:17 +0100 | [diff] [blame] | 8622 | 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] | 8623 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8624 | return -1; |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 8625 | #endif |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8626 | default: |
Aurelien DARRAGON | a374161 | 2024-03-01 15:55:17 +0100 | [diff] [blame] | 8627 | memprintf(err, "Lua unknown error: %s\n", hlua_tostring_safe(L, -1)); |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8628 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8629 | return -1; |
| 8630 | } |
| 8631 | |
| 8632 | return 0; |
| 8633 | } |
| 8634 | |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8635 | static int hlua_load(char **args, int section_type, struct proxy *curpx, |
Willy Tarreau | 0182516 | 2021-03-09 09:53:46 +0100 | [diff] [blame] | 8636 | const struct proxy *defpx, const char *file, int line, |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8637 | char **err) |
| 8638 | { |
| 8639 | if (*(args[1]) == 0) { |
| 8640 | memprintf(err, "'%s' expects a file name as parameter.\n", args[0]); |
| 8641 | return -1; |
| 8642 | } |
| 8643 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8644 | /* loading for global state */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8645 | hlua_state_id = 0; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8646 | ha_set_tid(0); |
Thierry Fournier | afc63e2 | 2020-11-28 17:06:51 +0100 | [diff] [blame] | 8647 | return hlua_load_state(args[1], hlua_states[0], err); |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8648 | } |
| 8649 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8650 | 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] | 8651 | const struct proxy *defpx, const char *file, int line, |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8652 | char **err) |
| 8653 | { |
| 8654 | int len; |
| 8655 | |
| 8656 | if (*(args[1]) == 0) { |
| 8657 | memprintf(err, "'%s' expects a file as parameter.\n", args[0]); |
| 8658 | return -1; |
| 8659 | } |
| 8660 | |
| 8661 | if (per_thread_load == NULL) { |
| 8662 | /* allocate the first entry large enough to store the final NULL */ |
| 8663 | per_thread_load = calloc(1, sizeof(*per_thread_load)); |
| 8664 | if (per_thread_load == NULL) { |
| 8665 | memprintf(err, "out of memory error"); |
| 8666 | return -1; |
| 8667 | } |
| 8668 | } |
| 8669 | |
| 8670 | /* count used entries */ |
| 8671 | for (len = 0; per_thread_load[len] != NULL; len++) |
| 8672 | ; |
| 8673 | |
| 8674 | per_thread_load = realloc(per_thread_load, (len + 2) * sizeof(*per_thread_load)); |
| 8675 | if (per_thread_load == NULL) { |
| 8676 | memprintf(err, "out of memory error"); |
| 8677 | return -1; |
| 8678 | } |
| 8679 | |
| 8680 | per_thread_load[len] = strdup(args[1]); |
| 8681 | per_thread_load[len + 1] = NULL; |
| 8682 | |
| 8683 | if (per_thread_load[len] == NULL) { |
| 8684 | memprintf(err, "out of memory error"); |
| 8685 | return -1; |
| 8686 | } |
| 8687 | |
| 8688 | /* loading for thread 1 only */ |
| 8689 | hlua_state_id = 1; |
| 8690 | ha_set_tid(0); |
| 8691 | return hlua_load_state(args[1], hlua_states[1], err); |
| 8692 | } |
| 8693 | |
Tim Duesterhus | c9fc9f2 | 2020-01-12 13:55:39 +0100 | [diff] [blame] | 8694 | /* Prepend the given <path> followed by a semicolon to the `package.<type>` variable |
| 8695 | * in the given <ctx>. |
| 8696 | */ |
Thierry Fournier | 3fb9e51 | 2020-11-28 10:13:12 +0100 | [diff] [blame] | 8697 | static int hlua_prepend_path(lua_State *L, char *type, char *path) |
Tim Duesterhus | c9fc9f2 | 2020-01-12 13:55:39 +0100 | [diff] [blame] | 8698 | { |
Thierry Fournier | 3fb9e51 | 2020-11-28 10:13:12 +0100 | [diff] [blame] | 8699 | lua_getglobal(L, "package"); /* push package variable */ |
| 8700 | lua_pushstring(L, path); /* push given path */ |
| 8701 | lua_pushstring(L, ";"); /* push semicolon */ |
| 8702 | lua_getfield(L, -3, type); /* push old path */ |
| 8703 | lua_concat(L, 3); /* concatenate to new path */ |
| 8704 | lua_setfield(L, -2, type); /* store new path */ |
| 8705 | lua_pop(L, 1); /* pop package variable */ |
Tim Duesterhus | c9fc9f2 | 2020-01-12 13:55:39 +0100 | [diff] [blame] | 8706 | |
| 8707 | return 0; |
| 8708 | } |
| 8709 | |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8710 | 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] | 8711 | const struct proxy *defpx, const char *file, int line, |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8712 | char **err) |
| 8713 | { |
| 8714 | char *path; |
| 8715 | char *type = "path"; |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 8716 | struct prepend_path *p = NULL; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8717 | |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8718 | if (too_many_args(2, args, err, NULL)) { |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 8719 | goto err; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8720 | } |
| 8721 | |
| 8722 | if (!(*args[1])) { |
| 8723 | memprintf(err, "'%s' expects to receive a <path> as argument", args[0]); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 8724 | goto err; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8725 | } |
| 8726 | path = args[1]; |
| 8727 | |
| 8728 | if (*args[2]) { |
| 8729 | if (strcmp(args[2], "path") != 0 && strcmp(args[2], "cpath") != 0) { |
| 8730 | 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] | 8731 | goto err; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8732 | } |
| 8733 | type = args[2]; |
| 8734 | } |
| 8735 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8736 | p = calloc(1, sizeof(*p)); |
| 8737 | if (p == NULL) { |
Tim Duesterhus | f89d43a | 2021-01-03 20:04:37 +0100 | [diff] [blame] | 8738 | memprintf(err, "memory allocation failed"); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 8739 | goto err; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8740 | } |
| 8741 | p->path = strdup(path); |
| 8742 | if (p->path == NULL) { |
Tim Duesterhus | f89d43a | 2021-01-03 20:04:37 +0100 | [diff] [blame] | 8743 | memprintf(err, "memory allocation failed"); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 8744 | goto err2; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8745 | } |
| 8746 | p->type = strdup(type); |
| 8747 | if (p->type == NULL) { |
Tim Duesterhus | f89d43a | 2021-01-03 20:04:37 +0100 | [diff] [blame] | 8748 | memprintf(err, "memory allocation failed"); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 8749 | goto err2; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8750 | } |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 8751 | LIST_APPEND(&prepend_path_list, &p->l); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8752 | |
| 8753 | hlua_prepend_path(hlua_states[0], type, path); |
| 8754 | hlua_prepend_path(hlua_states[1], type, path); |
| 8755 | return 0; |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 8756 | |
| 8757 | err2: |
| 8758 | free(p->type); |
| 8759 | free(p->path); |
| 8760 | err: |
| 8761 | free(p); |
| 8762 | return -1; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8763 | } |
| 8764 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8765 | /* configuration keywords declaration */ |
| 8766 | static struct cfg_kw_list cfg_kws = {{ },{ |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8767 | { CFG_GLOBAL, "lua-prepend-path", hlua_config_prepend_path }, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8768 | { CFG_GLOBAL, "lua-load", hlua_load }, |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8769 | { CFG_GLOBAL, "lua-load-per-thread", hlua_load_per_thread }, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8770 | { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout }, |
| 8771 | { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout }, |
Thierry FOURNIER | 56da101 | 2015-10-01 08:42:31 +0200 | [diff] [blame] | 8772 | { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout }, |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 8773 | { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield }, |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8774 | { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem }, |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8775 | { 0, NULL, NULL }, |
| 8776 | }}; |
| 8777 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 8778 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |
| 8779 | |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 8780 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8781 | /* This function can fail with an abort() due to an Lua critical error. |
| 8782 | * We are in the initialisation process of HAProxy, this abort() is |
| 8783 | * tolerated. |
| 8784 | */ |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8785 | int hlua_post_init_state(lua_State *L) |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8786 | { |
| 8787 | struct hlua_init_function *init; |
| 8788 | const char *msg; |
| 8789 | enum hlua_exec ret; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8790 | const char *error; |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8791 | const char *kind; |
| 8792 | const char *trace; |
| 8793 | int return_status = 1; |
| 8794 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
| 8795 | int nres; |
| 8796 | #endif |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8797 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 8798 | /* disable memory limit checks if limit is not set */ |
| 8799 | if (!hlua_global_allocator.limit) |
| 8800 | hlua_global_allocator.limit = ~hlua_global_allocator.limit; |
| 8801 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 8802 | /* Call post initialisation function in safe environment. */ |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 8803 | if (setjmp(safe_ljmp_env) != 0) { |
| 8804 | lua_atpanic(L, hlua_panic_safe); |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8805 | if (lua_type(L, -1) == LUA_TSTRING) |
Aurelien DARRAGON | a374161 | 2024-03-01 15:55:17 +0100 | [diff] [blame] | 8806 | error = hlua_tostring_safe(L, -1); |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 8807 | else |
| 8808 | error = "critical error"; |
| 8809 | fprintf(stderr, "Lua post-init: %s.\n", error); |
| 8810 | exit(1); |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 8811 | } else { |
| 8812 | lua_atpanic(L, hlua_panic_ljmp); |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 8813 | } |
Frédéric Lécaille | 54f2bcf | 2018-08-29 13:46:24 +0200 | [diff] [blame] | 8814 | |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8815 | hlua_fcn_post_init(L); |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 8816 | |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8817 | list_for_each_entry(init, &hlua_init_functions[hlua_state_id], l) { |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8818 | lua_rawgeti(L, LUA_REGISTRYINDEX, init->function_ref); |
Aurelien DARRAGON | 93591b4 | 2023-03-20 16:29:55 +0100 | [diff] [blame] | 8819 | /* function ref should be released right away since it was pushed |
| 8820 | * on the stack and will not be used anymore |
| 8821 | */ |
| 8822 | hlua_unref(L, init->function_ref); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8823 | |
| 8824 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
Aurelien DARRAGON | 2dec950 | 2023-09-08 19:29:08 +0200 | [diff] [blame] | 8825 | ret = lua_resume(L, NULL, 0, &nres); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8826 | #else |
Aurelien DARRAGON | 2dec950 | 2023-09-08 19:29:08 +0200 | [diff] [blame] | 8827 | ret = lua_resume(L, NULL, 0); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8828 | #endif |
| 8829 | kind = NULL; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8830 | switch (ret) { |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8831 | |
| 8832 | case LUA_OK: |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8833 | lua_pop(L, -1); |
Thierry Fournier | 13d08b7 | 2020-11-28 11:02:58 +0100 | [diff] [blame] | 8834 | break; |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8835 | |
| 8836 | case LUA_ERRERR: |
| 8837 | kind = "message handler error"; |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 8838 | /* Fall through */ |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8839 | case LUA_ERRRUN: |
| 8840 | if (!kind) |
| 8841 | kind = "runtime error"; |
Aurelien DARRAGON | a374161 | 2024-03-01 15:55:17 +0100 | [diff] [blame] | 8842 | msg = hlua_tostring_safe(L, -1); |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 8843 | trace = hlua_traceback(L, ", "); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8844 | if (msg) |
| 8845 | ha_alert("Lua init: %s: '%s' from %s\n", kind, msg, trace); |
| 8846 | else |
| 8847 | ha_alert("Lua init: unknown %s from %s\n", kind, trace); |
Aurelien DARRAGON | adac932 | 2024-03-01 19:54:16 +0100 | [diff] [blame] | 8848 | |
| 8849 | lua_settop(L, 0); /* Empty the stack. */ |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8850 | return_status = 0; |
| 8851 | break; |
| 8852 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8853 | default: |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8854 | /* Unknown error */ |
| 8855 | kind = "Unknown error"; |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 8856 | /* Fall through */ |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8857 | case LUA_YIELD: |
| 8858 | /* yield is not configured at this step, this state doesn't happen */ |
| 8859 | if (!kind) |
| 8860 | kind = "yield not allowed"; |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 8861 | /* Fall through */ |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8862 | case LUA_ERRMEM: |
| 8863 | if (!kind) |
| 8864 | kind = "out of memory error"; |
Aurelien DARRAGON | 93f5d8d | 2023-08-09 10:11:49 +0200 | [diff] [blame] | 8865 | lua_settop(L, 0); /* Empty the stack. */ |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 8866 | trace = hlua_traceback(L, ", "); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8867 | ha_alert("Lua init: %s: %s\n", kind, trace); |
| 8868 | return_status = 0; |
| 8869 | break; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8870 | } |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8871 | if (!return_status) |
| 8872 | break; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8873 | } |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 8874 | |
| 8875 | lua_atpanic(L, hlua_panic_safe); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8876 | return return_status; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8877 | } |
| 8878 | |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8879 | int hlua_post_init() |
| 8880 | { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8881 | int ret; |
| 8882 | int i; |
| 8883 | int errors; |
| 8884 | char *err = NULL; |
| 8885 | struct hlua_function *fcn; |
| 8886 | |
Willy Tarreau | 42afc59 | 2021-08-30 09:35:18 +0200 | [diff] [blame] | 8887 | #if defined(USE_OPENSSL) |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8888 | /* Initialize SSL server. */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 8889 | if (socket_ssl->xprt->prepare_srv) { |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8890 | int saved_used_backed = global.ssl_used_backend; |
| 8891 | // don't affect maxconn automatic computation |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 8892 | socket_ssl->xprt->prepare_srv(socket_ssl); |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8893 | global.ssl_used_backend = saved_used_backed; |
| 8894 | } |
| 8895 | #endif |
| 8896 | |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8897 | /* Perform post init of common thread */ |
| 8898 | hlua_state_id = 0; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8899 | ha_set_tid(0); |
| 8900 | ret = hlua_post_init_state(hlua_states[hlua_state_id]); |
| 8901 | if (ret == 0) |
| 8902 | return 0; |
| 8903 | |
| 8904 | /* init remaining lua states and load files */ |
| 8905 | for (hlua_state_id = 2; hlua_state_id < global.nbthread + 1; hlua_state_id++) { |
| 8906 | |
| 8907 | /* set thread context */ |
| 8908 | ha_set_tid(hlua_state_id - 1); |
| 8909 | |
| 8910 | /* Init lua state */ |
| 8911 | hlua_states[hlua_state_id] = hlua_init_state(hlua_state_id); |
| 8912 | |
| 8913 | /* Load lua files */ |
| 8914 | for (i = 0; per_thread_load && per_thread_load[i]; i++) { |
| 8915 | ret = hlua_load_state(per_thread_load[i], hlua_states[hlua_state_id], &err); |
| 8916 | if (ret != 0) { |
| 8917 | ha_alert("Lua init: %s\n", err); |
| 8918 | return 0; |
| 8919 | } |
| 8920 | } |
| 8921 | } |
| 8922 | |
| 8923 | /* Reset thread context */ |
| 8924 | ha_set_tid(0); |
| 8925 | |
| 8926 | /* Execute post init for all states */ |
| 8927 | for (hlua_state_id = 1; hlua_state_id < global.nbthread + 1; hlua_state_id++) { |
| 8928 | |
| 8929 | /* set thread context */ |
| 8930 | ha_set_tid(hlua_state_id - 1); |
| 8931 | |
| 8932 | /* run post init */ |
| 8933 | ret = hlua_post_init_state(hlua_states[hlua_state_id]); |
| 8934 | if (ret == 0) |
| 8935 | return 0; |
| 8936 | } |
| 8937 | |
| 8938 | /* Reset thread context */ |
| 8939 | ha_set_tid(0); |
| 8940 | |
| 8941 | /* control functions registering. Each function must have: |
| 8942 | * - only the function_ref[0] set positive and all other to -1 |
| 8943 | * - only the function_ref[0] set to -1 and all other positive |
| 8944 | * This ensure a same reference is not used both in shared |
| 8945 | * lua state and thread dedicated lua state. Note: is the case |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 8946 | * reach, the shared state is priority, but the bug will be |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8947 | * complicated to found for the end user. |
| 8948 | */ |
| 8949 | errors = 0; |
| 8950 | list_for_each_entry(fcn, &referenced_functions, l) { |
| 8951 | ret = 0; |
| 8952 | for (i = 1; i < global.nbthread + 1; i++) { |
| 8953 | if (fcn->function_ref[i] == -1) |
| 8954 | ret--; |
| 8955 | else |
| 8956 | ret++; |
| 8957 | } |
| 8958 | if (abs(ret) != global.nbthread) { |
| 8959 | ha_alert("Lua function '%s' is not referenced in all thread. " |
| 8960 | "Expect function in all thread or in none thread.\n", fcn->name); |
| 8961 | errors++; |
| 8962 | continue; |
| 8963 | } |
| 8964 | |
| 8965 | if ((fcn->function_ref[0] == -1) == (ret < 0)) { |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 8966 | ha_alert("Lua function '%s' is referenced both ins shared Lua context (through lua-load) " |
| 8967 | "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] | 8968 | "exclusive.\n", fcn->name); |
| 8969 | errors++; |
| 8970 | } |
| 8971 | } |
| 8972 | |
| 8973 | if (errors > 0) |
| 8974 | return 0; |
| 8975 | |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 8976 | /* 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] | 8977 | * -1 in order to have probably a segfault if someone use it |
| 8978 | */ |
| 8979 | hlua_state_id = -1; |
| 8980 | |
| 8981 | return 1; |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8982 | } |
| 8983 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8984 | /* The memory allocator used by the Lua stack. <ud> is a pointer to the |
| 8985 | * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize> |
| 8986 | * 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] | 8987 | * allocation. <nsize> is the requested new size. A new allocation is |
| 8988 | * indicated by <ptr> being NULL. A free is indicated by <nsize> being |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 8989 | * zero. This one verifies that the limits are respected but is optimized |
| 8990 | * 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] | 8991 | * |
| 8992 | * Warning: while this API ressembles glibc's realloc() a lot, glibc surpasses |
| 8993 | * POSIX by making realloc(ptr,0) an effective free(), but others do not do |
| 8994 | * that and will simply allocate zero as if it were the result of malloc(0), |
| 8995 | * so mapping this onto realloc() will lead to memory leaks on non-glibc |
| 8996 | * systems. |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8997 | */ |
| 8998 | static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize) |
| 8999 | { |
| 9000 | struct hlua_mem_allocator *zone = ud; |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 9001 | size_t limit, old, new; |
| 9002 | |
Tim Duesterhus | 2258652 | 2021-01-08 10:35:33 +0100 | [diff] [blame] | 9003 | if (unlikely(!ptr && !nsize)) |
| 9004 | return NULL; |
| 9005 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 9006 | /* a limit of ~0 means unlimited and boot complete, so there's no need |
| 9007 | * for accounting anymore. |
| 9008 | */ |
Willy Tarreau | e00ad05 | 2021-10-22 16:00:02 +0200 | [diff] [blame] | 9009 | if (likely(~zone->limit == 0)) { |
| 9010 | if (!nsize) |
| 9011 | ha_free(&ptr); |
| 9012 | else |
| 9013 | ptr = realloc(ptr, nsize); |
| 9014 | return ptr; |
| 9015 | } |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 9016 | |
Willy Tarreau | d36c7fa | 2020-12-02 12:26:29 +0100 | [diff] [blame] | 9017 | if (!ptr) |
| 9018 | osize = 0; |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 9019 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 9020 | /* enforce strict limits across all threads */ |
| 9021 | limit = zone->limit; |
| 9022 | old = _HA_ATOMIC_LOAD(&zone->allocated); |
| 9023 | do { |
| 9024 | new = old + nsize - osize; |
| 9025 | if (unlikely(nsize && limit && new > limit)) |
| 9026 | return NULL; |
| 9027 | } while (!_HA_ATOMIC_CAS(&zone->allocated, &old, new)); |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 9028 | |
Willy Tarreau | e00ad05 | 2021-10-22 16:00:02 +0200 | [diff] [blame] | 9029 | if (!nsize) |
| 9030 | ha_free(&ptr); |
| 9031 | else |
| 9032 | ptr = realloc(ptr, nsize); |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 9033 | |
| 9034 | if (unlikely(!ptr && nsize)) // failed |
| 9035 | _HA_ATOMIC_SUB(&zone->allocated, nsize - osize); |
| 9036 | |
| 9037 | __ha_barrier_atomic_store(); |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 9038 | return ptr; |
| 9039 | } |
| 9040 | |
Thierry Fournier | ecb83c2 | 2020-11-28 15:49:44 +0100 | [diff] [blame] | 9041 | /* 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] | 9042 | * We are in the initialisation process of HAProxy, this abort() is |
| 9043 | * tolerated. |
| 9044 | */ |
Thierry Fournier | ecb83c2 | 2020-11-28 15:49:44 +0100 | [diff] [blame] | 9045 | lua_State *hlua_init_state(int thread_num) |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 9046 | { |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 9047 | int i; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 9048 | int idx; |
| 9049 | struct sample_fetch *sf; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 9050 | struct sample_conv *sc; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 9051 | char *p; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 9052 | const char *error_msg; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 9053 | void **context; |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9054 | lua_State *L; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 9055 | struct prepend_path *pp; |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 9056 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 9057 | /* Init main lua stack. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9058 | L = lua_newstate(hlua_alloc, &hlua_global_allocator); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 9059 | |
firexinghe | bdd336c | 2023-07-13 11:03:34 +0800 | [diff] [blame] | 9060 | if (!L) { |
| 9061 | fprintf(stderr, |
| 9062 | "Lua init: critical error: lua_newstate() returned NULL." |
| 9063 | " This may possibly be caused by a memory allocation error.\n"); |
| 9064 | exit(1); |
| 9065 | } |
| 9066 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 9067 | /* Initialise Lua context to NULL */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9068 | context = lua_getextraspace(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 9069 | *context = NULL; |
| 9070 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 9071 | /* From this point, until the end of the initialisation function, |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 9072 | * the Lua function can fail with an abort. We are in the initialisation |
| 9073 | * process of HAProxy, this abort() is tolerated. |
| 9074 | */ |
| 9075 | |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 9076 | /* Call post initialisation function in safe environment. */ |
| 9077 | if (setjmp(safe_ljmp_env) != 0) { |
| 9078 | lua_atpanic(L, hlua_panic_safe); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9079 | if (lua_type(L, -1) == LUA_TSTRING) |
Aurelien DARRAGON | a374161 | 2024-03-01 15:55:17 +0100 | [diff] [blame] | 9080 | error_msg = hlua_tostring_safe(L, -1); |
Thierry Fournier | 2f05cc6 | 2020-11-28 16:08:02 +0100 | [diff] [blame] | 9081 | else |
| 9082 | error_msg = "critical error"; |
| 9083 | fprintf(stderr, "Lua init: %s.\n", error_msg); |
| 9084 | exit(1); |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 9085 | } else { |
| 9086 | lua_atpanic(L, hlua_panic_ljmp); |
Thierry Fournier | 2f05cc6 | 2020-11-28 16:08:02 +0100 | [diff] [blame] | 9087 | } |
| 9088 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 9089 | /* Initialise lua. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9090 | luaL_openlibs(L); |
Tim Duesterhus | 541fe1e | 2020-01-12 13:55:41 +0100 | [diff] [blame] | 9091 | #define HLUA_PREPEND_PATH_TOSTRING1(x) #x |
| 9092 | #define HLUA_PREPEND_PATH_TOSTRING(x) HLUA_PREPEND_PATH_TOSTRING1(x) |
| 9093 | #ifdef HLUA_PREPEND_PATH |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9094 | hlua_prepend_path(L, "path", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_PATH)); |
Tim Duesterhus | 541fe1e | 2020-01-12 13:55:41 +0100 | [diff] [blame] | 9095 | #endif |
| 9096 | #ifdef HLUA_PREPEND_CPATH |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9097 | hlua_prepend_path(L, "cpath", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_CPATH)); |
Tim Duesterhus | 541fe1e | 2020-01-12 13:55:41 +0100 | [diff] [blame] | 9098 | #endif |
| 9099 | #undef HLUA_PREPEND_PATH_TOSTRING |
| 9100 | #undef HLUA_PREPEND_PATH_TOSTRING1 |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 9101 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 9102 | /* Apply configured prepend path */ |
| 9103 | list_for_each_entry(pp, &prepend_path_list, l) |
| 9104 | hlua_prepend_path(L, pp->type, pp->path); |
| 9105 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 9106 | /* |
| 9107 | * |
| 9108 | * Create "core" object. |
| 9109 | * |
| 9110 | */ |
| 9111 | |
Thierry FOURNIER | a2d8c65 | 2015-03-11 17:29:39 +0100 | [diff] [blame] | 9112 | /* This table entry is the object "core" base. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9113 | lua_newtable(L); |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 9114 | |
Thierry Fournier | ecb83c2 | 2020-11-28 15:49:44 +0100 | [diff] [blame] | 9115 | /* set the thread id */ |
| 9116 | hlua_class_const_int(L, "thread", thread_num); |
| 9117 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 9118 | /* Push the loglevel constants. */ |
Christopher Faulet | 8263e94 | 2024-02-29 15:41:17 +0100 | [diff] [blame] | 9119 | hlua_class_const_int(L, "silent", -1); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 9120 | for (i = 0; i < NB_LOG_LEVELS; i++) |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9121 | hlua_class_const_int(L, log_levels[i], i); |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 9122 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 9123 | /* Register special functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9124 | hlua_class_function(L, "register_init", hlua_register_init); |
| 9125 | hlua_class_function(L, "register_task", hlua_register_task); |
| 9126 | hlua_class_function(L, "register_fetches", hlua_register_fetches); |
| 9127 | hlua_class_function(L, "register_converters", hlua_register_converters); |
| 9128 | hlua_class_function(L, "register_action", hlua_register_action); |
| 9129 | hlua_class_function(L, "register_service", hlua_register_service); |
| 9130 | hlua_class_function(L, "register_cli", hlua_register_cli); |
| 9131 | hlua_class_function(L, "yield", hlua_yield); |
| 9132 | hlua_class_function(L, "set_nice", hlua_set_nice); |
| 9133 | hlua_class_function(L, "sleep", hlua_sleep); |
| 9134 | hlua_class_function(L, "msleep", hlua_msleep); |
| 9135 | hlua_class_function(L, "add_acl", hlua_add_acl); |
| 9136 | hlua_class_function(L, "del_acl", hlua_del_acl); |
| 9137 | hlua_class_function(L, "set_map", hlua_set_map); |
| 9138 | hlua_class_function(L, "del_map", hlua_del_map); |
| 9139 | hlua_class_function(L, "tcp", hlua_socket_new); |
| 9140 | hlua_class_function(L, "log", hlua_log); |
| 9141 | hlua_class_function(L, "Debug", hlua_log_debug); |
| 9142 | hlua_class_function(L, "Info", hlua_log_info); |
| 9143 | hlua_class_function(L, "Warning", hlua_log_warning); |
| 9144 | hlua_class_function(L, "Alert", hlua_log_alert); |
| 9145 | hlua_class_function(L, "done", hlua_done); |
| 9146 | hlua_fcn_reg_core_fcn(L); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 9147 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9148 | lua_setglobal(L, "core"); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 9149 | |
| 9150 | /* |
| 9151 | * |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 9152 | * Create "act" object. |
| 9153 | * |
| 9154 | */ |
| 9155 | |
| 9156 | /* This table entry is the object "act" base. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9157 | lua_newtable(L); |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 9158 | |
| 9159 | /* push action return constants */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9160 | hlua_class_const_int(L, "CONTINUE", ACT_RET_CONT); |
| 9161 | hlua_class_const_int(L, "STOP", ACT_RET_STOP); |
| 9162 | hlua_class_const_int(L, "YIELD", ACT_RET_YIELD); |
| 9163 | hlua_class_const_int(L, "ERROR", ACT_RET_ERR); |
| 9164 | hlua_class_const_int(L, "DONE", ACT_RET_DONE); |
| 9165 | hlua_class_const_int(L, "DENY", ACT_RET_DENY); |
| 9166 | hlua_class_const_int(L, "ABORT", ACT_RET_ABRT); |
| 9167 | hlua_class_const_int(L, "INVALID", ACT_RET_INV); |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 9168 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9169 | hlua_class_function(L, "wake_time", hlua_set_wake_time); |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 9170 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9171 | lua_setglobal(L, "act"); |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 9172 | |
| 9173 | /* |
| 9174 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 9175 | * Register class Map |
| 9176 | * |
| 9177 | */ |
| 9178 | |
| 9179 | /* This table entry is the object "Map" base. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9180 | lua_newtable(L); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 9181 | |
| 9182 | /* register pattern types. */ |
| 9183 | for (i=0; i<PAT_MATCH_NUM; i++) |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9184 | hlua_class_const_int(L, pat_match_names[i], i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 9185 | for (i=0; i<PAT_MATCH_NUM; i++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 9186 | snprintf(trash.area, trash.size, "_%s", pat_match_names[i]); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9187 | hlua_class_const_int(L, trash.area, i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 9188 | } |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 9189 | |
| 9190 | /* register constructor. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9191 | hlua_class_function(L, "new", hlua_map_new); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 9192 | |
| 9193 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9194 | lua_newtable(L); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 9195 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 9196 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9197 | lua_pushstring(L, "__index"); |
| 9198 | lua_newtable(L); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 9199 | |
| 9200 | /* Register . */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9201 | hlua_class_function(L, "lookup", hlua_map_lookup); |
| 9202 | hlua_class_function(L, "slookup", hlua_map_slookup); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 9203 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9204 | lua_rawset(L, -3); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 9205 | |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 9206 | /* Register previous table in the registry with reference and named entry. |
| 9207 | * The function hlua_register_metatable() pops the stack, so we |
| 9208 | * previously create a copy of the table. |
| 9209 | */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9210 | lua_pushvalue(L, -1); /* Copy the -1 entry and push it on the stack. */ |
| 9211 | class_map_ref = hlua_register_metatable(L, CLASS_MAP); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 9212 | |
| 9213 | /* Assign the metatable to the mai Map object. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9214 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 9215 | |
| 9216 | /* Set a name to the table. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9217 | lua_setglobal(L, "Map"); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 9218 | |
| 9219 | /* |
| 9220 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 9221 | * Register class Channel |
| 9222 | * |
| 9223 | */ |
| 9224 | |
| 9225 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9226 | lua_newtable(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 9227 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 9228 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9229 | lua_pushstring(L, "__index"); |
| 9230 | lua_newtable(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 9231 | |
| 9232 | /* Register . */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9233 | hlua_class_function(L, "get", hlua_channel_get); |
| 9234 | hlua_class_function(L, "dup", hlua_channel_dup); |
| 9235 | hlua_class_function(L, "getline", hlua_channel_getline); |
| 9236 | hlua_class_function(L, "set", hlua_channel_set); |
| 9237 | hlua_class_function(L, "append", hlua_channel_append); |
| 9238 | hlua_class_function(L, "send", hlua_channel_send); |
| 9239 | hlua_class_function(L, "forward", hlua_channel_forward); |
| 9240 | hlua_class_function(L, "get_in_len", hlua_channel_get_in_len); |
| 9241 | hlua_class_function(L, "get_out_len", hlua_channel_get_out_len); |
| 9242 | hlua_class_function(L, "is_full", hlua_channel_is_full); |
| 9243 | hlua_class_function(L, "is_resp", hlua_channel_is_resp); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 9244 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9245 | lua_rawset(L, -3); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 9246 | |
| 9247 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9248 | class_channel_ref = hlua_register_metatable(L, CLASS_CHANNEL); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 9249 | |
| 9250 | /* |
| 9251 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 9252 | * Register class Fetches |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 9253 | * |
| 9254 | */ |
| 9255 | |
| 9256 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9257 | lua_newtable(L); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 9258 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 9259 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9260 | lua_pushstring(L, "__index"); |
| 9261 | lua_newtable(L); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 9262 | |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 9263 | /* Browse existing fetches and create the associated |
| 9264 | * object method. |
| 9265 | */ |
| 9266 | sf = NULL; |
| 9267 | while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) { |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 9268 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 9269 | * by an underscore. |
| 9270 | */ |
Willy Tarreau | fa92f36 | 2021-08-26 16:48:53 +0200 | [diff] [blame] | 9271 | strlcpy2(trash.area, sf->kw, trash.size); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 9272 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 9273 | if (*p == '.' || *p == '-' || *p == '+') |
| 9274 | *p = '_'; |
| 9275 | |
| 9276 | /* Register the function. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9277 | lua_pushstring(L, trash.area); |
| 9278 | lua_pushlightuserdata(L, sf); |
| 9279 | lua_pushcclosure(L, hlua_run_sample_fetch, 1); |
| 9280 | lua_rawset(L, -3); |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 9281 | } |
| 9282 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9283 | lua_rawset(L, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 9284 | |
| 9285 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9286 | class_fetches_ref = hlua_register_metatable(L, CLASS_FETCHES); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 9287 | |
| 9288 | /* |
| 9289 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 9290 | * Register class Converters |
| 9291 | * |
| 9292 | */ |
| 9293 | |
| 9294 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9295 | lua_newtable(L); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 9296 | |
| 9297 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9298 | lua_pushstring(L, "__index"); |
| 9299 | lua_newtable(L); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 9300 | |
| 9301 | /* Browse existing converters and create the associated |
| 9302 | * object method. |
| 9303 | */ |
| 9304 | sc = NULL; |
| 9305 | while ((sc = sample_conv_getnext(sc, &idx)) != NULL) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 9306 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 9307 | * by an underscore. |
| 9308 | */ |
Willy Tarreau | fa92f36 | 2021-08-26 16:48:53 +0200 | [diff] [blame] | 9309 | strlcpy2(trash.area, sc->kw, trash.size); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 9310 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 9311 | if (*p == '.' || *p == '-' || *p == '+') |
| 9312 | *p = '_'; |
| 9313 | |
| 9314 | /* Register the function. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9315 | lua_pushstring(L, trash.area); |
| 9316 | lua_pushlightuserdata(L, sc); |
| 9317 | lua_pushcclosure(L, hlua_run_sample_conv, 1); |
| 9318 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 9319 | } |
| 9320 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9321 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 9322 | |
| 9323 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9324 | class_converters_ref = hlua_register_metatable(L, CLASS_CONVERTERS); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 9325 | |
| 9326 | /* |
| 9327 | * |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 9328 | * Register class HTTP |
| 9329 | * |
| 9330 | */ |
| 9331 | |
| 9332 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9333 | lua_newtable(L); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 9334 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 9335 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9336 | lua_pushstring(L, "__index"); |
| 9337 | lua_newtable(L); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 9338 | |
| 9339 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9340 | hlua_class_function(L, "req_get_headers",hlua_http_req_get_headers); |
| 9341 | hlua_class_function(L, "req_del_header", hlua_http_req_del_hdr); |
| 9342 | hlua_class_function(L, "req_rep_header", hlua_http_req_rep_hdr); |
| 9343 | hlua_class_function(L, "req_rep_value", hlua_http_req_rep_val); |
| 9344 | hlua_class_function(L, "req_add_header", hlua_http_req_add_hdr); |
| 9345 | hlua_class_function(L, "req_set_header", hlua_http_req_set_hdr); |
| 9346 | hlua_class_function(L, "req_set_method", hlua_http_req_set_meth); |
| 9347 | hlua_class_function(L, "req_set_path", hlua_http_req_set_path); |
| 9348 | hlua_class_function(L, "req_set_query", hlua_http_req_set_query); |
| 9349 | hlua_class_function(L, "req_set_uri", hlua_http_req_set_uri); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 9350 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9351 | hlua_class_function(L, "res_get_headers",hlua_http_res_get_headers); |
| 9352 | hlua_class_function(L, "res_del_header", hlua_http_res_del_hdr); |
| 9353 | hlua_class_function(L, "res_rep_header", hlua_http_res_rep_hdr); |
| 9354 | hlua_class_function(L, "res_rep_value", hlua_http_res_rep_val); |
| 9355 | hlua_class_function(L, "res_add_header", hlua_http_res_add_hdr); |
| 9356 | hlua_class_function(L, "res_set_header", hlua_http_res_set_hdr); |
| 9357 | hlua_class_function(L, "res_set_status", hlua_http_res_set_status); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 9358 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9359 | lua_rawset(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 9360 | |
| 9361 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9362 | class_http_ref = hlua_register_metatable(L, CLASS_HTTP); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 9363 | |
| 9364 | /* |
| 9365 | * |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9366 | * Register class AppletTCP |
| 9367 | * |
| 9368 | */ |
| 9369 | |
| 9370 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9371 | lua_newtable(L); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9372 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 9373 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9374 | lua_pushstring(L, "__index"); |
| 9375 | lua_newtable(L); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9376 | |
| 9377 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9378 | hlua_class_function(L, "getline", hlua_applet_tcp_getline); |
| 9379 | hlua_class_function(L, "receive", hlua_applet_tcp_recv); |
| 9380 | hlua_class_function(L, "send", hlua_applet_tcp_send); |
| 9381 | hlua_class_function(L, "set_priv", hlua_applet_tcp_set_priv); |
| 9382 | hlua_class_function(L, "get_priv", hlua_applet_tcp_get_priv); |
| 9383 | hlua_class_function(L, "set_var", hlua_applet_tcp_set_var); |
| 9384 | hlua_class_function(L, "unset_var", hlua_applet_tcp_unset_var); |
| 9385 | hlua_class_function(L, "get_var", hlua_applet_tcp_get_var); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9386 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9387 | lua_settable(L, -3); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9388 | |
| 9389 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9390 | class_applet_tcp_ref = hlua_register_metatable(L, CLASS_APPLET_TCP); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9391 | |
| 9392 | /* |
| 9393 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9394 | * Register class AppletHTTP |
| 9395 | * |
| 9396 | */ |
| 9397 | |
| 9398 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9399 | lua_newtable(L); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9400 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 9401 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9402 | lua_pushstring(L, "__index"); |
| 9403 | lua_newtable(L); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9404 | |
| 9405 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9406 | hlua_class_function(L, "set_priv", hlua_applet_http_set_priv); |
| 9407 | hlua_class_function(L, "get_priv", hlua_applet_http_get_priv); |
| 9408 | hlua_class_function(L, "set_var", hlua_applet_http_set_var); |
| 9409 | hlua_class_function(L, "unset_var", hlua_applet_http_unset_var); |
| 9410 | hlua_class_function(L, "get_var", hlua_applet_http_get_var); |
| 9411 | hlua_class_function(L, "getline", hlua_applet_http_getline); |
| 9412 | hlua_class_function(L, "receive", hlua_applet_http_recv); |
| 9413 | hlua_class_function(L, "send", hlua_applet_http_send); |
| 9414 | hlua_class_function(L, "add_header", hlua_applet_http_addheader); |
| 9415 | hlua_class_function(L, "set_status", hlua_applet_http_status); |
| 9416 | hlua_class_function(L, "start_response", hlua_applet_http_start_response); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9417 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9418 | lua_settable(L, -3); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9419 | |
| 9420 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9421 | class_applet_http_ref = hlua_register_metatable(L, CLASS_APPLET_HTTP); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9422 | |
| 9423 | /* |
| 9424 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 9425 | * Register class TXN |
| 9426 | * |
| 9427 | */ |
| 9428 | |
| 9429 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9430 | lua_newtable(L); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 9431 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 9432 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9433 | lua_pushstring(L, "__index"); |
| 9434 | lua_newtable(L); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 9435 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 9436 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9437 | hlua_class_function(L, "set_priv", hlua_set_priv); |
| 9438 | hlua_class_function(L, "get_priv", hlua_get_priv); |
| 9439 | hlua_class_function(L, "set_var", hlua_set_var); |
| 9440 | hlua_class_function(L, "unset_var", hlua_unset_var); |
| 9441 | hlua_class_function(L, "get_var", hlua_get_var); |
| 9442 | hlua_class_function(L, "done", hlua_txn_done); |
| 9443 | hlua_class_function(L, "reply", hlua_txn_reply_new); |
| 9444 | hlua_class_function(L, "set_loglevel", hlua_txn_set_loglevel); |
| 9445 | hlua_class_function(L, "set_tos", hlua_txn_set_tos); |
| 9446 | hlua_class_function(L, "set_mark", hlua_txn_set_mark); |
| 9447 | hlua_class_function(L, "set_priority_class", hlua_txn_set_priority_class); |
| 9448 | hlua_class_function(L, "set_priority_offset", hlua_txn_set_priority_offset); |
| 9449 | hlua_class_function(L, "deflog", hlua_txn_deflog); |
| 9450 | hlua_class_function(L, "log", hlua_txn_log); |
| 9451 | hlua_class_function(L, "Debug", hlua_txn_log_debug); |
| 9452 | hlua_class_function(L, "Info", hlua_txn_log_info); |
| 9453 | hlua_class_function(L, "Warning", hlua_txn_log_warning); |
| 9454 | hlua_class_function(L, "Alert", hlua_txn_log_alert); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 9455 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9456 | lua_rawset(L, -3); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 9457 | |
| 9458 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9459 | class_txn_ref = hlua_register_metatable(L, CLASS_TXN); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9460 | |
| 9461 | /* |
| 9462 | * |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 9463 | * Register class reply |
| 9464 | * |
| 9465 | */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9466 | lua_newtable(L); |
| 9467 | lua_pushstring(L, "__index"); |
| 9468 | lua_newtable(L); |
| 9469 | hlua_class_function(L, "set_status", hlua_txn_reply_set_status); |
| 9470 | hlua_class_function(L, "add_header", hlua_txn_reply_add_header); |
| 9471 | hlua_class_function(L, "del_header", hlua_txn_reply_del_header); |
| 9472 | hlua_class_function(L, "set_body", hlua_txn_reply_set_body); |
| 9473 | lua_settable(L, -3); /* Sets the __index entry. */ |
| 9474 | class_txn_reply_ref = luaL_ref(L, LUA_REGISTRYINDEX); |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 9475 | |
| 9476 | |
| 9477 | /* |
| 9478 | * |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9479 | * Register class Socket |
| 9480 | * |
| 9481 | */ |
| 9482 | |
| 9483 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9484 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9485 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 9486 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9487 | lua_pushstring(L, "__index"); |
| 9488 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9489 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 9490 | #ifdef USE_OPENSSL |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9491 | hlua_class_function(L, "connect_ssl", hlua_socket_connect_ssl); |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 9492 | #endif |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9493 | hlua_class_function(L, "connect", hlua_socket_connect); |
| 9494 | hlua_class_function(L, "send", hlua_socket_send); |
| 9495 | hlua_class_function(L, "receive", hlua_socket_receive); |
| 9496 | hlua_class_function(L, "close", hlua_socket_close); |
| 9497 | hlua_class_function(L, "getpeername", hlua_socket_getpeername); |
| 9498 | hlua_class_function(L, "getsockname", hlua_socket_getsockname); |
| 9499 | hlua_class_function(L, "setoption", hlua_socket_setoption); |
| 9500 | hlua_class_function(L, "settimeout", hlua_socket_settimeout); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9501 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9502 | 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] | 9503 | |
| 9504 | /* Register the garbage collector entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9505 | lua_pushstring(L, "__gc"); |
| 9506 | lua_pushcclosure(L, hlua_socket_gc, 0); |
| 9507 | 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] | 9508 | |
| 9509 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9510 | class_socket_ref = hlua_register_metatable(L, CLASS_SOCKET); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9511 | |
Thierry Fournier | aafc777 | 2020-12-04 11:47:47 +0100 | [diff] [blame] | 9512 | lua_atpanic(L, hlua_panic_safe); |
| 9513 | |
| 9514 | return L; |
| 9515 | } |
| 9516 | |
| 9517 | void hlua_init(void) { |
| 9518 | int i; |
Amaury Denoyelle | 239fdbf | 2021-03-24 10:22:03 +0100 | [diff] [blame] | 9519 | char *errmsg; |
Thierry Fournier | aafc777 | 2020-12-04 11:47:47 +0100 | [diff] [blame] | 9520 | #ifdef USE_OPENSSL |
| 9521 | struct srv_kw *kw; |
| 9522 | int tmp_error; |
| 9523 | char *error; |
| 9524 | char *args[] = { /* SSL client configuration. */ |
| 9525 | "ssl", |
| 9526 | "verify", |
| 9527 | "none", |
| 9528 | NULL |
| 9529 | }; |
| 9530 | #endif |
| 9531 | |
| 9532 | /* Init post init function list head */ |
| 9533 | for (i = 0; i < MAX_THREADS + 1; i++) |
| 9534 | LIST_INIT(&hlua_init_functions[i]); |
| 9535 | |
| 9536 | /* Init state for common/shared lua parts */ |
| 9537 | hlua_state_id = 0; |
| 9538 | ha_set_tid(0); |
| 9539 | hlua_states[0] = hlua_init_state(0); |
| 9540 | |
| 9541 | /* Init state 1 for thread 0. We have at least one thread. */ |
| 9542 | hlua_state_id = 1; |
| 9543 | ha_set_tid(0); |
| 9544 | hlua_states[1] = hlua_init_state(1); |
| 9545 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9546 | /* Proxy and server configuration initialisation. */ |
Amaury Denoyelle | 239fdbf | 2021-03-24 10:22:03 +0100 | [diff] [blame] | 9547 | socket_proxy = alloc_new_proxy("LUA-SOCKET", PR_CAP_FE|PR_CAP_BE|PR_CAP_LUA, &errmsg); |
| 9548 | if (!socket_proxy) { |
| 9549 | fprintf(stderr, "Lua init: %s\n", errmsg); |
| 9550 | exit(1); |
| 9551 | } |
| 9552 | proxy_preset_defaults(socket_proxy); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9553 | |
| 9554 | /* Init TCP server: unchanged parameters */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 9555 | socket_tcp = new_server(socket_proxy); |
| 9556 | if (!socket_tcp) { |
| 9557 | fprintf(stderr, "Lua init: failed to allocate tcp server socket\n"); |
| 9558 | exit(1); |
| 9559 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9560 | |
| 9561 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9562 | /* Init TCP server: unchanged parameters */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 9563 | socket_ssl = new_server(socket_proxy); |
| 9564 | if (!socket_ssl) { |
| 9565 | fprintf(stderr, "Lua init: failed to allocate ssl server socket\n"); |
| 9566 | exit(1); |
| 9567 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9568 | |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 9569 | socket_ssl->use_ssl = 1; |
| 9570 | socket_ssl->xprt = xprt_get(XPRT_SSL); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9571 | |
Bertrand Jacquin | 80839ff | 2021-01-21 19:14:46 +0000 | [diff] [blame] | 9572 | for (i = 0; args[i] != NULL; i++) { |
| 9573 | 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] | 9574 | /* |
| 9575 | * |
| 9576 | * If the keyword is not known, we can search in the registered |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 9577 | * server keywords. This is useful to configure special SSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9578 | * features like client certificates and ssl_verify. |
| 9579 | * |
| 9580 | */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 9581 | tmp_error = kw->parse(args, &i, socket_proxy, socket_ssl, &error); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9582 | if (tmp_error != 0) { |
| 9583 | fprintf(stderr, "INTERNAL ERROR: %s\n", error); |
| 9584 | abort(); /* This must be never arrives because the command line |
| 9585 | not editable by the user. */ |
| 9586 | } |
Bertrand Jacquin | 80839ff | 2021-01-21 19:14:46 +0000 | [diff] [blame] | 9587 | i += kw->skip; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9588 | } |
| 9589 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9590 | #endif |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 9591 | |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 9592 | } |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 9593 | |
Tim Duesterhus | d0c0ca2 | 2020-07-04 11:53:26 +0200 | [diff] [blame] | 9594 | static void hlua_deinit() |
| 9595 | { |
Willy Tarreau | 186f376 | 2020-12-04 11:48:12 +0100 | [diff] [blame] | 9596 | int thr; |
| 9597 | |
| 9598 | for (thr = 0; thr < MAX_THREADS+1; thr++) { |
| 9599 | if (hlua_states[thr]) |
| 9600 | lua_close(hlua_states[thr]); |
| 9601 | } |
Willy Tarreau | 430bf4a | 2021-03-04 09:45:32 +0100 | [diff] [blame] | 9602 | |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 9603 | free_server(socket_tcp); |
Willy Tarreau | 430bf4a | 2021-03-04 09:45:32 +0100 | [diff] [blame] | 9604 | |
Willy Tarreau | 0f143af | 2021-03-05 10:41:48 +0100 | [diff] [blame] | 9605 | #ifdef USE_OPENSSL |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 9606 | free_server(socket_ssl); |
Willy Tarreau | 0f143af | 2021-03-05 10:41:48 +0100 | [diff] [blame] | 9607 | #endif |
Amaury Denoyelle | 239fdbf | 2021-03-24 10:22:03 +0100 | [diff] [blame] | 9608 | |
| 9609 | free_proxy(socket_proxy); |
Tim Duesterhus | d0c0ca2 | 2020-07-04 11:53:26 +0200 | [diff] [blame] | 9610 | } |
| 9611 | |
| 9612 | REGISTER_POST_DEINIT(hlua_deinit); |
| 9613 | |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 9614 | static void hlua_register_build_options(void) |
| 9615 | { |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 9616 | char *ptr = NULL; |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 9617 | |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 9618 | memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE); |
| 9619 | hap_register_build_opts(ptr, 1); |
| 9620 | } |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 9621 | |
| 9622 | INITCALL0(STG_REGISTER, hlua_register_build_options); |