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 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 189 | #define SET_SAFE_LJMP_L(__L, __HLUA) \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 190 | ({ \ |
| 191 | int ret; \ |
Aurelien DARRAGON | e00bf5d | 2023-03-21 13:22:33 +0100 | [diff] [blame] | 192 | hlua_lock(__HLUA); \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 193 | if (setjmp(safe_ljmp_env) != 0) { \ |
| 194 | lua_atpanic(__L, hlua_panic_safe); \ |
| 195 | ret = 0; \ |
Aurelien DARRAGON | e00bf5d | 2023-03-21 13:22:33 +0100 | [diff] [blame] | 196 | hlua_unlock(__HLUA); \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 197 | } else { \ |
| 198 | lua_atpanic(__L, hlua_panic_ljmp); \ |
| 199 | ret = 1; \ |
| 200 | } \ |
| 201 | ret; \ |
| 202 | }) |
| 203 | |
| 204 | /* If we are the last function catching Lua errors, we |
| 205 | * must reset the panic function. |
| 206 | */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 207 | #define RESET_SAFE_LJMP_L(__L, __HLUA) \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 208 | do { \ |
| 209 | lua_atpanic(__L, hlua_panic_safe); \ |
Aurelien DARRAGON | e00bf5d | 2023-03-21 13:22:33 +0100 | [diff] [blame] | 210 | hlua_unlock(__HLUA); \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 211 | } while(0) |
| 212 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 213 | #define SET_SAFE_LJMP(__HLUA) \ |
| 214 | SET_SAFE_LJMP_L((__HLUA)->T, __HLUA) |
| 215 | |
| 216 | #define RESET_SAFE_LJMP(__HLUA) \ |
| 217 | RESET_SAFE_LJMP_L((__HLUA)->T, __HLUA) |
| 218 | |
| 219 | #define SET_SAFE_LJMP_PARENT(__HLUA) \ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 220 | SET_SAFE_LJMP_L(hlua_states[(__HLUA)->state_id], __HLUA) |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 221 | |
| 222 | #define RESET_SAFE_LJMP_PARENT(__HLUA) \ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 223 | RESET_SAFE_LJMP_L(hlua_states[(__HLUA)->state_id], __HLUA) |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 224 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 225 | /* Applet status flags */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 226 | #define APPLET_DONE 0x01 /* applet processing is done. */ |
Christopher Faulet | 18c2e8d | 2019-03-01 12:02:08 +0100 | [diff] [blame] | 227 | /* unused: 0x02 */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 228 | #define APPLET_HDR_SENT 0x04 /* Response header sent. */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 229 | /* unused: 0x08, 0x10 */ |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 230 | #define APPLET_HTTP11 0x20 /* Last chunk sent. */ |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 231 | #define APPLET_RSP_SENT 0x40 /* The response was fully sent */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 232 | |
Thierry Fournier | afc63e2 | 2020-11-28 17:06:51 +0100 | [diff] [blame] | 233 | /* The main Lua execution context. The 0 index is the |
| 234 | * common state shared by all threads. |
| 235 | */ |
Willy Tarreau | 186f376 | 2020-12-04 11:48:12 +0100 | [diff] [blame] | 236 | static lua_State *hlua_states[MAX_THREADS + 1]; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 237 | |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 238 | /* This is the memory pool containing struct lua for applets |
| 239 | * (including cli). |
| 240 | */ |
Willy Tarreau | 8ceae72 | 2018-11-26 11:58:30 +0100 | [diff] [blame] | 241 | DECLARE_STATIC_POOL(pool_head_hlua, "hlua", sizeof(struct hlua)); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 242 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 243 | /* Used for Socket connection. */ |
Amaury Denoyelle | 239fdbf | 2021-03-24 10:22:03 +0100 | [diff] [blame] | 244 | static struct proxy *socket_proxy; |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 245 | static struct server *socket_tcp; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 246 | #ifdef USE_OPENSSL |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 247 | static struct server *socket_ssl; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 248 | #endif |
| 249 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 250 | /* List head of the function called at the initialisation time. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 251 | struct list hlua_init_functions[MAX_THREADS + 1]; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 252 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 253 | /* The following variables contains the reference of the different |
| 254 | * Lua classes. These references are useful for identify metadata |
| 255 | * associated with an object. |
| 256 | */ |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 257 | static int class_txn_ref; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 258 | static int class_socket_ref; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 259 | static int class_channel_ref; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 260 | static int class_fetches_ref; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 261 | static int class_converters_ref; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 262 | static int class_http_ref; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 263 | static int class_map_ref; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 264 | static int class_applet_tcp_ref; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 265 | static int class_applet_http_ref; |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 266 | static int class_txn_reply_ref; |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 267 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 268 | /* Global Lua execution timeout. By default Lua, execution linked |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 269 | * with stream (actions, sample-fetches and converters) have a |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 270 | * short timeout. Lua linked with tasks doesn't have a timeout |
| 271 | * because a task may remain alive during all the haproxy execution. |
| 272 | */ |
| 273 | static unsigned int hlua_timeout_session = 4000; /* session timeout. */ |
| 274 | static unsigned int hlua_timeout_task = TICK_ETERNITY; /* task timeout. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 275 | static unsigned int hlua_timeout_applet = 4000; /* applet timeout. */ |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 276 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 277 | /* Interrupts the Lua processing each "hlua_nb_instruction" instructions. |
| 278 | * it is used for preventing infinite loops. |
| 279 | * |
| 280 | * I test the scheer with an infinite loop containing one incrementation |
| 281 | * and one test. I run this loop between 10 seconds, I raise a ceil of |
| 282 | * 710M loops from one interrupt each 9000 instructions, so I fix the value |
| 283 | * to one interrupt each 10 000 instructions. |
| 284 | * |
| 285 | * configured | Number of |
| 286 | * instructions | loops executed |
| 287 | * between two | in milions |
| 288 | * forced yields | |
| 289 | * ---------------+--------------- |
| 290 | * 10 | 160 |
| 291 | * 500 | 670 |
| 292 | * 1000 | 680 |
| 293 | * 5000 | 700 |
| 294 | * 7000 | 700 |
| 295 | * 8000 | 700 |
| 296 | * 9000 | 710 <- ceil |
| 297 | * 10000 | 710 |
| 298 | * 100000 | 710 |
| 299 | * 1000000 | 710 |
| 300 | * |
| 301 | */ |
| 302 | static unsigned int hlua_nb_instruction = 10000; |
| 303 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 304 | /* Descriptor for the memory allocation state. The limit is pre-initialised to |
| 305 | * 0 until it is replaced by "tune.lua.maxmem" during the config parsing, or it |
| 306 | * is replaced with ~0 during post_init after everything was loaded. This way |
| 307 | * it is guaranteed that if limit is ~0 the boot is complete and that if it's |
| 308 | * zero it's not yet limited and proper accounting is required. |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 309 | */ |
| 310 | struct hlua_mem_allocator { |
| 311 | size_t allocated; |
| 312 | size_t limit; |
| 313 | }; |
| 314 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 315 | static struct hlua_mem_allocator hlua_global_allocator THREAD_ALIGNED(64); |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 316 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 317 | /* These functions converts types between HAProxy internal args or |
| 318 | * sample and LUA types. Another function permits to check if the |
| 319 | * LUA stack contains arguments according with an required ARG_T |
| 320 | * format. |
| 321 | */ |
| 322 | static int hlua_arg2lua(lua_State *L, const struct arg *arg); |
| 323 | static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 324 | __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] | 325 | uint64_t mask, struct proxy *p); |
Willy Tarreau | 5eadada | 2015-03-10 17:28:54 +0100 | [diff] [blame] | 326 | static int hlua_smp2lua(lua_State *L, struct sample *smp); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 327 | static int hlua_smp2lua_str(lua_State *L, struct sample *smp); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 328 | static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp); |
| 329 | |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 330 | __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] | 331 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 332 | struct prepend_path { |
| 333 | struct list l; |
| 334 | char *type; |
| 335 | char *path; |
| 336 | }; |
| 337 | |
| 338 | static struct list prepend_path_list = LIST_HEAD_INIT(prepend_path_list); |
| 339 | |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 340 | #define SEND_ERR(__be, __fmt, __args...) \ |
| 341 | do { \ |
| 342 | send_log(__be, LOG_ERR, __fmt, ## __args); \ |
| 343 | if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) \ |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 344 | ha_alert(__fmt, ## __args); \ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 345 | } while (0) |
| 346 | |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 347 | static inline struct hlua_function *new_hlua_function() |
| 348 | { |
| 349 | struct hlua_function *fcn; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 350 | int i; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 351 | |
| 352 | fcn = calloc(1, sizeof(*fcn)); |
| 353 | if (!fcn) |
| 354 | return NULL; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 355 | LIST_APPEND(&referenced_functions, &fcn->l); |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 356 | for (i = 0; i < MAX_THREADS + 1; i++) |
| 357 | fcn->function_ref[i] = -1; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 358 | return fcn; |
| 359 | } |
| 360 | |
Christopher Faulet | dda4444 | 2021-04-12 14:05:43 +0200 | [diff] [blame] | 361 | static inline void release_hlua_function(struct hlua_function *fcn) |
| 362 | { |
| 363 | if (!fcn) |
| 364 | return; |
| 365 | if (fcn->name) |
| 366 | ha_free(&fcn->name); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 367 | LIST_DELETE(&fcn->l); |
Christopher Faulet | dda4444 | 2021-04-12 14:05:43 +0200 | [diff] [blame] | 368 | ha_free(&fcn); |
| 369 | } |
| 370 | |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 371 | /* If the common state is set, the stack id is 0, otherwise it is the tid + 1 */ |
| 372 | static inline int fcn_ref_to_stack_id(struct hlua_function *fcn) |
| 373 | { |
| 374 | if (fcn->function_ref[0] == -1) |
| 375 | return tid + 1; |
| 376 | return 0; |
| 377 | } |
| 378 | |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 379 | /* Used to check an Lua function type in the stack. It creates and |
| 380 | * returns a reference of the function. This function throws an |
Aurelien DARRAGON | 5ce1152 | 2023-03-02 17:50:49 +0100 | [diff] [blame] | 381 | * error if the argument is not a "function". |
| 382 | * When no longer used, the ref must be released with hlua_unref() |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 383 | */ |
| 384 | __LJMP unsigned int hlua_checkfunction(lua_State *L, int argno) |
| 385 | { |
| 386 | if (!lua_isfunction(L, argno)) { |
Thierry FOURNIER | fd1e955 | 2018-02-23 18:41:18 +0100 | [diff] [blame] | 387 | 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] | 388 | WILL_LJMP(luaL_argerror(L, argno, msg)); |
| 389 | } |
| 390 | lua_pushvalue(L, argno); |
| 391 | return luaL_ref(L, LUA_REGISTRYINDEX); |
| 392 | } |
| 393 | |
Christopher Faulet | ddc9032 | 2020-02-25 10:20:04 +0100 | [diff] [blame] | 394 | /* Used to check an Lua table type in the stack. It creates and |
| 395 | * returns a reference of the table. This function throws an |
Aurelien DARRAGON | 5ce1152 | 2023-03-02 17:50:49 +0100 | [diff] [blame] | 396 | * error if the argument is not a "table". |
| 397 | * When no longer used, the ref must be released with hlua_unref() |
Christopher Faulet | ddc9032 | 2020-02-25 10:20:04 +0100 | [diff] [blame] | 398 | */ |
| 399 | __LJMP unsigned int hlua_checktable(lua_State *L, int argno) |
| 400 | { |
| 401 | if (!lua_istable(L, argno)) { |
| 402 | const char *msg = lua_pushfstring(L, "table expected, got %s", luaL_typename(L, argno)); |
| 403 | WILL_LJMP(luaL_argerror(L, argno, msg)); |
| 404 | } |
| 405 | lua_pushvalue(L, argno); |
| 406 | return luaL_ref(L, LUA_REGISTRYINDEX); |
| 407 | } |
| 408 | |
Aurelien DARRAGON | 5ce1152 | 2023-03-02 17:50:49 +0100 | [diff] [blame] | 409 | /* Get a reference to the object that is at the top of the stack |
| 410 | * The referenced object will be popped from the stack |
| 411 | * |
| 412 | * The function returns the reference to the object which must |
| 413 | * be cleared using hlua_unref() when no longer used |
| 414 | */ |
| 415 | __LJMP int hlua_ref(lua_State *L) |
| 416 | { |
| 417 | return MAY_LJMP(luaL_ref(L, LUA_REGISTRYINDEX)); |
| 418 | } |
| 419 | |
| 420 | /* Pushes a reference previously created using luaL_ref(L, LUA_REGISTRYINDEX) |
| 421 | * on <L> stack |
| 422 | * (ie: hlua_checkfunction(), hlua_checktable() or hlua_ref()) |
| 423 | * |
| 424 | * When the reference is no longer used, it should be released by calling |
| 425 | * hlua_unref() |
| 426 | * |
| 427 | * <L> can be from any co-routine as long as it belongs to the same lua |
| 428 | * parent state that the one used to get the reference. |
| 429 | */ |
| 430 | void hlua_pushref(lua_State *L, int ref) |
| 431 | { |
| 432 | lua_rawgeti(L, LUA_REGISTRYINDEX, ref); |
| 433 | } |
| 434 | |
| 435 | /* Releases a reference previously created using luaL_ref(L, LUA_REGISTRYINDEX) |
| 436 | * (ie: hlua_checkfunction(), hlua_checktable() or hlua_ref()) |
| 437 | * |
| 438 | * This will allow the reference to be reused and the referred object |
| 439 | * to be garbage collected. |
| 440 | * |
| 441 | * <L> can be from any co-routine as long as it belongs to the same lua |
| 442 | * parent state that the one used to get the reference. |
| 443 | */ |
| 444 | void hlua_unref(lua_State *L, int ref) |
| 445 | { |
| 446 | luaL_unref(L, LUA_REGISTRYINDEX, ref); |
| 447 | } |
| 448 | |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 449 | __LJMP const char *hlua_traceback(lua_State *L, const char* sep) |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 450 | { |
| 451 | lua_Debug ar; |
| 452 | int level = 0; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 453 | struct buffer *msg = get_trash_chunk(); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 454 | |
| 455 | while (lua_getstack(L, level++, &ar)) { |
| 456 | |
| 457 | /* Add separator */ |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 458 | if (b_data(msg)) |
| 459 | chunk_appendf(msg, "%s", sep); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 460 | |
| 461 | /* Fill fields: |
| 462 | * 'S': fills in the fields source, short_src, linedefined, lastlinedefined, and what; |
| 463 | * 'l': fills in the field currentline; |
| 464 | * 'n': fills in the field name and namewhat; |
| 465 | * 't': fills in the field istailcall; |
| 466 | */ |
| 467 | lua_getinfo(L, "Slnt", &ar); |
| 468 | |
| 469 | /* Append code localisation */ |
| 470 | if (ar.currentline > 0) |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 471 | chunk_appendf(msg, "%s:%d: ", ar.short_src, ar.currentline); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 472 | else |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 473 | chunk_appendf(msg, "%s: ", ar.short_src); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 474 | |
| 475 | /* |
| 476 | * Get function name |
| 477 | * |
| 478 | * if namewhat is no empty, name is defined. |
| 479 | * what contains "Lua" for Lua function, "C" for C function, |
| 480 | * or "main" for main code. |
| 481 | */ |
| 482 | 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] | 483 | chunk_appendf(msg, "in %s '%s'", ar.namewhat, ar.name); /* use it */ |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 484 | |
| 485 | 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] | 486 | chunk_appendf(msg, "in main chunk"); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 487 | |
| 488 | else if (*ar.what != 'C') /* for Lua functions, use <file:line> */ |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 489 | chunk_appendf(msg, "in function line %d", ar.linedefined); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 490 | |
| 491 | else /* nothing left... */ |
| 492 | chunk_appendf(msg, "?"); |
| 493 | |
| 494 | |
| 495 | /* Display tailed call */ |
| 496 | if (ar.istailcall) |
| 497 | chunk_appendf(msg, " ..."); |
| 498 | } |
| 499 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 500 | return msg->area; |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 504 | /* This function check the number of arguments available in the |
| 505 | * stack. If the number of arguments available is not the same |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 506 | * then <nb> an error is thrown. |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 507 | */ |
| 508 | __LJMP static inline void check_args(lua_State *L, int nb, char *fcn) |
| 509 | { |
| 510 | if (lua_gettop(L) == nb) |
| 511 | return; |
| 512 | WILL_LJMP(luaL_error(L, "'%s' needs %d arguments", fcn, nb)); |
| 513 | } |
| 514 | |
Mark Lakes | 22154b4 | 2018-01-29 14:38:40 -0800 | [diff] [blame] | 515 | /* This function pushes an error string prefixed by the file name |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 516 | * and the line number where the error is encountered. |
| 517 | */ |
| 518 | static int hlua_pusherror(lua_State *L, const char *fmt, ...) |
| 519 | { |
| 520 | va_list argp; |
| 521 | va_start(argp, fmt); |
| 522 | luaL_where(L, 1); |
| 523 | lua_pushvfstring(L, fmt, argp); |
| 524 | va_end(argp); |
| 525 | lua_concat(L, 2); |
| 526 | return 1; |
| 527 | } |
| 528 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 529 | /* This functions is used with sample fetch and converters. It |
| 530 | * converts the HAProxy configuration argument in a lua stack |
| 531 | * values. |
| 532 | * |
| 533 | * It takes an array of "arg", and each entry of the array is |
| 534 | * converted and pushed in the LUA stack. |
| 535 | */ |
| 536 | static int hlua_arg2lua(lua_State *L, const struct arg *arg) |
| 537 | { |
| 538 | switch (arg->type) { |
| 539 | case ARGT_SINT: |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 540 | case ARGT_TIME: |
| 541 | case ARGT_SIZE: |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 542 | lua_pushinteger(L, arg->data.sint); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 543 | break; |
| 544 | |
| 545 | case ARGT_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 546 | lua_pushlstring(L, arg->data.str.area, arg->data.str.data); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 547 | break; |
| 548 | |
| 549 | case ARGT_IPV4: |
| 550 | case ARGT_IPV6: |
| 551 | case ARGT_MSK4: |
| 552 | case ARGT_MSK6: |
| 553 | case ARGT_FE: |
| 554 | case ARGT_BE: |
| 555 | case ARGT_TAB: |
| 556 | case ARGT_SRV: |
| 557 | case ARGT_USR: |
| 558 | case ARGT_MAP: |
| 559 | default: |
| 560 | lua_pushnil(L); |
| 561 | break; |
| 562 | } |
| 563 | return 1; |
| 564 | } |
| 565 | |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 566 | /* 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] | 567 | * 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] | 568 | * with sample fetch wrappers. The input arguments are given to the |
| 569 | * lua wrapper and converted as arg list by the function. |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 570 | */ |
| 571 | static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg) |
| 572 | { |
| 573 | switch (lua_type(L, ud)) { |
| 574 | |
| 575 | case LUA_TNUMBER: |
| 576 | case LUA_TBOOLEAN: |
| 577 | arg->type = ARGT_SINT; |
| 578 | arg->data.sint = lua_tointeger(L, ud); |
| 579 | break; |
| 580 | |
| 581 | case LUA_TSTRING: |
| 582 | arg->type = ARGT_STR; |
Tim Duesterhus | 2e89dec | 2019-09-29 23:03:08 +0200 | [diff] [blame] | 583 | 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] | 584 | /* 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] | 585 | 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] | 586 | arg->data.str.head = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 587 | break; |
| 588 | |
| 589 | case LUA_TUSERDATA: |
| 590 | case LUA_TNIL: |
| 591 | case LUA_TTABLE: |
| 592 | case LUA_TFUNCTION: |
| 593 | case LUA_TTHREAD: |
| 594 | case LUA_TLIGHTUSERDATA: |
| 595 | arg->type = ARGT_SINT; |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 596 | arg->data.sint = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 597 | break; |
| 598 | } |
| 599 | return 1; |
| 600 | } |
| 601 | |
| 602 | /* the following functions are used to convert a struct sample |
| 603 | * in Lua type. This useful to convert the return of the |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 604 | * fetches or converters. |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 605 | */ |
Willy Tarreau | 5eadada | 2015-03-10 17:28:54 +0100 | [diff] [blame] | 606 | static int hlua_smp2lua(lua_State *L, struct sample *smp) |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 607 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 608 | switch (smp->data.type) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 609 | case SMP_T_SINT: |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 610 | case SMP_T_BOOL: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 611 | lua_pushinteger(L, smp->data.u.sint); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 612 | break; |
| 613 | |
| 614 | case SMP_T_BIN: |
| 615 | case SMP_T_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 616 | 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] | 617 | break; |
| 618 | |
| 619 | case SMP_T_METH: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 620 | switch (smp->data.u.meth.meth) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 621 | case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break; |
| 622 | case HTTP_METH_GET: lua_pushstring(L, "GET"); break; |
| 623 | case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break; |
| 624 | case HTTP_METH_POST: lua_pushstring(L, "POST"); break; |
| 625 | case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break; |
| 626 | case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break; |
| 627 | case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break; |
| 628 | case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break; |
| 629 | case HTTP_METH_OTHER: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 630 | 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] | 631 | break; |
| 632 | default: |
| 633 | lua_pushnil(L); |
| 634 | break; |
| 635 | } |
| 636 | break; |
| 637 | |
| 638 | case SMP_T_IPV4: |
| 639 | case SMP_T_IPV6: |
| 640 | 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] | 641 | if (sample_casts[smp->data.type][SMP_T_STR] && |
| 642 | sample_casts[smp->data.type][SMP_T_STR](smp)) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 643 | 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] | 644 | else |
| 645 | lua_pushnil(L); |
| 646 | break; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 647 | default: |
| 648 | lua_pushnil(L); |
| 649 | break; |
| 650 | } |
| 651 | return 1; |
| 652 | } |
| 653 | |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 654 | /* the following functions are used to convert a struct sample |
| 655 | * in Lua strings. This is useful to convert the return of the |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 656 | * fetches or converters. |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 657 | */ |
| 658 | static int hlua_smp2lua_str(lua_State *L, struct sample *smp) |
| 659 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 660 | switch (smp->data.type) { |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 661 | |
| 662 | case SMP_T_BIN: |
| 663 | case SMP_T_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 664 | 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] | 665 | break; |
| 666 | |
| 667 | case SMP_T_METH: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 668 | switch (smp->data.u.meth.meth) { |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 669 | case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break; |
| 670 | case HTTP_METH_GET: lua_pushstring(L, "GET"); break; |
| 671 | case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break; |
| 672 | case HTTP_METH_POST: lua_pushstring(L, "POST"); break; |
| 673 | case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break; |
| 674 | case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break; |
| 675 | case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break; |
| 676 | case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break; |
| 677 | case HTTP_METH_OTHER: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 678 | 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] | 679 | break; |
| 680 | default: |
| 681 | lua_pushstring(L, ""); |
| 682 | break; |
| 683 | } |
| 684 | break; |
| 685 | |
| 686 | case SMP_T_SINT: |
| 687 | case SMP_T_BOOL: |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 688 | case SMP_T_IPV4: |
| 689 | case SMP_T_IPV6: |
| 690 | 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] | 691 | if (sample_casts[smp->data.type][SMP_T_STR] && |
| 692 | sample_casts[smp->data.type][SMP_T_STR](smp)) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 693 | 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] | 694 | else |
| 695 | lua_pushstring(L, ""); |
| 696 | break; |
| 697 | default: |
| 698 | lua_pushstring(L, ""); |
| 699 | break; |
| 700 | } |
| 701 | return 1; |
| 702 | } |
| 703 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 704 | /* the following functions are used to convert an Lua type in a |
| 705 | * struct sample. This is useful to provide data from a converter |
| 706 | * to the LUA code. |
| 707 | */ |
| 708 | static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp) |
| 709 | { |
| 710 | switch (lua_type(L, ud)) { |
| 711 | |
| 712 | case LUA_TNUMBER: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 713 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 714 | smp->data.u.sint = lua_tointeger(L, ud); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 715 | break; |
| 716 | |
| 717 | |
| 718 | case LUA_TBOOLEAN: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 719 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 720 | smp->data.u.sint = lua_toboolean(L, ud); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 721 | break; |
| 722 | |
| 723 | case LUA_TSTRING: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 724 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 725 | smp->flags |= SMP_F_CONST; |
Tim Duesterhus | 2e89dec | 2019-09-29 23:03:08 +0200 | [diff] [blame] | 726 | 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] | 727 | /* 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] | 728 | 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] | 729 | smp->data.u.str.head = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 730 | break; |
| 731 | |
| 732 | case LUA_TUSERDATA: |
| 733 | case LUA_TNIL: |
| 734 | case LUA_TTABLE: |
| 735 | case LUA_TFUNCTION: |
| 736 | case LUA_TTHREAD: |
| 737 | case LUA_TLIGHTUSERDATA: |
Thierry FOURNIER | 93405e1 | 2015-08-26 14:19:03 +0200 | [diff] [blame] | 738 | case LUA_TNONE: |
| 739 | default: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 740 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 741 | smp->data.u.sint = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 742 | break; |
| 743 | } |
| 744 | return 1; |
| 745 | } |
| 746 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 747 | /* This function check the "argp" built by another conversion function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 748 | * 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] | 749 | * 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] | 750 | * |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 751 | * This function assumes that the argp argument contains ARGM_NBARGS + 1 |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 752 | * entries. |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 753 | */ |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 754 | __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] | 755 | uint64_t mask, struct proxy *p) |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 756 | { |
| 757 | int min_arg; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 758 | int i, idx; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 759 | struct proxy *px; |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 760 | struct userlist *ul; |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 761 | struct my_regex *reg; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 762 | const char *msg = NULL; |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 763 | char *sname, *pname, *err = NULL; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 764 | |
| 765 | idx = 0; |
| 766 | min_arg = ARGM(mask); |
| 767 | mask >>= ARGM_BITS; |
| 768 | |
| 769 | while (1) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 770 | struct buffer tmp = BUF_NULL; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 771 | |
| 772 | /* Check oversize. */ |
| 773 | if (idx >= ARGM_NBARGS && argp[idx].type != ARGT_STOP) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 774 | msg = "Malformed argument mask"; |
| 775 | goto error; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 776 | } |
| 777 | |
| 778 | /* Check for mandatory arguments. */ |
| 779 | if (argp[idx].type == ARGT_STOP) { |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 780 | if (idx < min_arg) { |
| 781 | |
| 782 | /* If miss other argument than the first one, we return an error. */ |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 783 | if (idx > 0) { |
| 784 | msg = "Mandatory argument expected"; |
| 785 | goto error; |
| 786 | } |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 787 | |
| 788 | /* If first argument have a certain type, some default values |
| 789 | * may be used. See the function smp_resolve_args(). |
| 790 | */ |
| 791 | switch (mask & ARGT_MASK) { |
| 792 | |
| 793 | case ARGT_FE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 794 | if (!(p->cap & PR_CAP_FE)) { |
| 795 | msg = "Mandatory argument expected"; |
| 796 | goto error; |
| 797 | } |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 798 | argp[idx].data.prx = p; |
| 799 | argp[idx].type = ARGT_FE; |
| 800 | argp[idx+1].type = ARGT_STOP; |
| 801 | break; |
| 802 | |
| 803 | case ARGT_BE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 804 | if (!(p->cap & PR_CAP_BE)) { |
| 805 | msg = "Mandatory argument expected"; |
| 806 | goto error; |
| 807 | } |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 808 | argp[idx].data.prx = p; |
| 809 | argp[idx].type = ARGT_BE; |
| 810 | argp[idx+1].type = ARGT_STOP; |
| 811 | break; |
| 812 | |
| 813 | case ARGT_TAB: |
Olivier Houchard | a265827 | 2022-09-13 00:35:53 +0200 | [diff] [blame] | 814 | if (!p->table) { |
| 815 | msg = "Mandatory argument expected"; |
| 816 | goto error; |
| 817 | } |
| 818 | argp[idx].data.t = p->table; |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 819 | argp[idx].type = ARGT_TAB; |
| 820 | argp[idx+1].type = ARGT_STOP; |
| 821 | break; |
| 822 | |
| 823 | default: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 824 | msg = "Mandatory argument expected"; |
| 825 | goto error; |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 826 | break; |
| 827 | } |
| 828 | } |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 829 | break; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 830 | } |
| 831 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 832 | /* Check for exceed the number of required argument. */ |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 833 | if ((mask & ARGT_MASK) == ARGT_STOP && |
| 834 | argp[idx].type != ARGT_STOP) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 835 | msg = "Last argument expected"; |
| 836 | goto error; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 837 | } |
| 838 | |
| 839 | if ((mask & ARGT_MASK) == ARGT_STOP && |
| 840 | argp[idx].type == ARGT_STOP) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 841 | break; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 842 | } |
| 843 | |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 844 | /* Convert some argument types. All string in argp[] are for not |
| 845 | * duplicated yet. |
| 846 | */ |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 847 | switch (mask & ARGT_MASK) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 848 | case ARGT_SINT: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 849 | if (argp[idx].type != ARGT_SINT) { |
| 850 | msg = "integer expected"; |
| 851 | goto error; |
| 852 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 853 | argp[idx].type = ARGT_SINT; |
| 854 | break; |
| 855 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 856 | case ARGT_TIME: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 857 | if (argp[idx].type != ARGT_SINT) { |
| 858 | msg = "integer expected"; |
| 859 | goto error; |
| 860 | } |
Thierry FOURNIER | 29176f3 | 2015-07-07 00:41:29 +0200 | [diff] [blame] | 861 | argp[idx].type = ARGT_TIME; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 862 | break; |
| 863 | |
| 864 | case ARGT_SIZE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 865 | if (argp[idx].type != ARGT_SINT) { |
| 866 | msg = "integer expected"; |
| 867 | goto error; |
| 868 | } |
Thierry FOURNIER | 29176f3 | 2015-07-07 00:41:29 +0200 | [diff] [blame] | 869 | argp[idx].type = ARGT_SIZE; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 870 | break; |
| 871 | |
| 872 | case ARGT_FE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 873 | if (argp[idx].type != ARGT_STR) { |
| 874 | msg = "string expected"; |
| 875 | goto error; |
| 876 | } |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 877 | 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] | 878 | if (!argp[idx].data.prx) { |
| 879 | msg = "frontend doesn't exist"; |
| 880 | goto error; |
| 881 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 882 | argp[idx].type = ARGT_FE; |
| 883 | break; |
| 884 | |
| 885 | case ARGT_BE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 886 | if (argp[idx].type != ARGT_STR) { |
| 887 | msg = "string expected"; |
| 888 | goto error; |
| 889 | } |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 890 | 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] | 891 | if (!argp[idx].data.prx) { |
| 892 | msg = "backend doesn't exist"; |
| 893 | goto error; |
| 894 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 895 | argp[idx].type = ARGT_BE; |
| 896 | break; |
| 897 | |
| 898 | case ARGT_TAB: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 899 | if (argp[idx].type != ARGT_STR) { |
| 900 | msg = "string expected"; |
| 901 | goto error; |
| 902 | } |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 903 | 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] | 904 | if (!argp[idx].data.t) { |
| 905 | msg = "table doesn't exist"; |
| 906 | goto error; |
| 907 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 908 | argp[idx].type = ARGT_TAB; |
| 909 | break; |
| 910 | |
| 911 | case ARGT_SRV: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 912 | if (argp[idx].type != ARGT_STR) { |
| 913 | msg = "string expected"; |
| 914 | goto error; |
| 915 | } |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 916 | sname = strrchr(argp[idx].data.str.area, '/'); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 917 | if (sname) { |
| 918 | *sname++ = '\0'; |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 919 | pname = argp[idx].data.str.area; |
Willy Tarreau | 9e0bb10 | 2015-05-26 11:24:42 +0200 | [diff] [blame] | 920 | px = proxy_be_by_name(pname); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 921 | if (!px) { |
| 922 | msg = "backend doesn't exist"; |
| 923 | goto error; |
| 924 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 925 | } |
| 926 | else { |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 927 | sname = argp[idx].data.str.area; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 928 | px = p; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 929 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 930 | argp[idx].data.srv = findserver(px, sname); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 931 | if (!argp[idx].data.srv) { |
| 932 | msg = "server doesn't exist"; |
| 933 | goto error; |
| 934 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 935 | argp[idx].type = ARGT_SRV; |
| 936 | break; |
| 937 | |
| 938 | case ARGT_IPV4: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 939 | if (argp[idx].type != ARGT_STR) { |
| 940 | msg = "string expected"; |
| 941 | goto error; |
| 942 | } |
| 943 | if (inet_pton(AF_INET, argp[idx].data.str.area, &argp[idx].data.ipv4)) { |
| 944 | msg = "invalid IPv4 address"; |
| 945 | goto error; |
| 946 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 947 | argp[idx].type = ARGT_IPV4; |
| 948 | break; |
| 949 | |
| 950 | case ARGT_MSK4: |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 951 | if (argp[idx].type == ARGT_SINT) |
| 952 | len2mask4(argp[idx].data.sint, &argp[idx].data.ipv4); |
| 953 | else if (argp[idx].type == ARGT_STR) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 954 | if (!str2mask(argp[idx].data.str.area, &argp[idx].data.ipv4)) { |
| 955 | msg = "invalid IPv4 mask"; |
| 956 | goto error; |
| 957 | } |
| 958 | } |
| 959 | else { |
| 960 | msg = "integer or string expected"; |
| 961 | goto error; |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 962 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 963 | argp[idx].type = ARGT_MSK4; |
| 964 | break; |
| 965 | |
| 966 | case ARGT_IPV6: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 967 | if (argp[idx].type != ARGT_STR) { |
| 968 | msg = "string expected"; |
| 969 | goto error; |
| 970 | } |
| 971 | if (inet_pton(AF_INET6, argp[idx].data.str.area, &argp[idx].data.ipv6)) { |
| 972 | msg = "invalid IPv6 address"; |
| 973 | goto error; |
| 974 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 975 | argp[idx].type = ARGT_IPV6; |
| 976 | break; |
| 977 | |
| 978 | case ARGT_MSK6: |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 979 | if (argp[idx].type == ARGT_SINT) |
| 980 | len2mask6(argp[idx].data.sint, &argp[idx].data.ipv6); |
| 981 | else if (argp[idx].type == ARGT_STR) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 982 | if (!str2mask6(argp[idx].data.str.area, &argp[idx].data.ipv6)) { |
| 983 | msg = "invalid IPv6 mask"; |
| 984 | goto error; |
| 985 | } |
| 986 | } |
| 987 | else { |
| 988 | msg = "integer or string expected"; |
| 989 | goto error; |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 990 | } |
Tim Duesterhus | b814da6 | 2018-01-25 16:24:50 +0100 | [diff] [blame] | 991 | argp[idx].type = ARGT_MSK6; |
| 992 | break; |
| 993 | |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 994 | case ARGT_REG: |
| 995 | if (argp[idx].type != ARGT_STR) { |
| 996 | msg = "string expected"; |
| 997 | goto error; |
| 998 | } |
| 999 | reg = regex_comp(argp[idx].data.str.area, !(argp[idx].type_flags & ARGF_REG_ICASE), 1, &err); |
| 1000 | if (!reg) { |
| 1001 | msg = lua_pushfstring(L, "error compiling regex '%s' : '%s'", |
| 1002 | argp[idx].data.str.area, err); |
| 1003 | free(err); |
| 1004 | goto error; |
| 1005 | } |
| 1006 | argp[idx].type = ARGT_REG; |
| 1007 | argp[idx].data.reg = reg; |
| 1008 | break; |
| 1009 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 1010 | case ARGT_USR: |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 1011 | if (argp[idx].type != ARGT_STR) { |
| 1012 | msg = "string expected"; |
| 1013 | goto error; |
| 1014 | } |
| 1015 | if (p->uri_auth && p->uri_auth->userlist && |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 1016 | strcmp(p->uri_auth->userlist->name, argp[idx].data.str.area) == 0) |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 1017 | ul = p->uri_auth->userlist; |
| 1018 | else |
| 1019 | ul = auth_find_userlist(argp[idx].data.str.area); |
| 1020 | |
| 1021 | if (!ul) { |
| 1022 | msg = lua_pushfstring(L, "unable to find userlist '%s'", argp[idx].data.str.area); |
| 1023 | goto error; |
| 1024 | } |
| 1025 | argp[idx].type = ARGT_USR; |
| 1026 | argp[idx].data.usr = ul; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1027 | break; |
| 1028 | |
| 1029 | case ARGT_STR: |
| 1030 | if (!chunk_dup(&tmp, &argp[idx].data.str)) { |
| 1031 | msg = "unable to duplicate string arg"; |
| 1032 | goto error; |
| 1033 | } |
| 1034 | argp[idx].data.str = tmp; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 1035 | break; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1036 | |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 1037 | case ARGT_MAP: |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 1038 | msg = "type not yet supported"; |
| 1039 | goto error; |
| 1040 | break; |
| 1041 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 1042 | } |
| 1043 | |
| 1044 | /* Check for type of argument. */ |
| 1045 | if ((mask & ARGT_MASK) != argp[idx].type) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1046 | msg = lua_pushfstring(L, "'%s' expected, got '%s'", |
| 1047 | arg_type_names[(mask & ARGT_MASK)], |
| 1048 | arg_type_names[argp[idx].type & ARGT_MASK]); |
| 1049 | goto error; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 1050 | } |
| 1051 | |
| 1052 | /* Next argument. */ |
| 1053 | mask >>= ARGT_BITS; |
| 1054 | idx++; |
| 1055 | } |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1056 | return 0; |
| 1057 | |
| 1058 | error: |
Olivier Houchard | da25dac | 2022-09-13 00:31:17 +0200 | [diff] [blame] | 1059 | argp[idx].type = ARGT_STOP; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1060 | for (i = 0; i < idx; i++) { |
| 1061 | if (argp[i].type == ARGT_STR) |
| 1062 | chunk_destroy(&argp[i].data.str); |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 1063 | else if (argp[i].type == ARGT_REG) |
| 1064 | regex_free(argp[i].data.reg); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1065 | } |
| 1066 | WILL_LJMP(luaL_argerror(L, first + idx, msg)); |
| 1067 | return 0; /* Never reached */ |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 1068 | } |
| 1069 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1070 | /* |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 1071 | * The following functions are used to make correspondence between the the |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1072 | * executed lua pointer and the "struct hlua *" that contain the context. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1073 | * |
| 1074 | * - hlua_gethlua : return the hlua context associated with an lua_State. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1075 | * - hlua_sethlua : create the association between hlua context and lua_state. |
| 1076 | */ |
| 1077 | static inline struct hlua *hlua_gethlua(lua_State *L) |
| 1078 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 1079 | struct hlua **hlua = lua_getextraspace(L); |
| 1080 | return *hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1081 | } |
| 1082 | static inline void hlua_sethlua(struct hlua *hlua) |
| 1083 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 1084 | struct hlua **hlua_store = lua_getextraspace(hlua->T); |
| 1085 | *hlua_store = hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1086 | } |
| 1087 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1088 | /* This function is used to send logs. It try to send on screen (stderr) |
| 1089 | * and on the default syslog server. |
| 1090 | */ |
| 1091 | static inline void hlua_sendlog(struct proxy *px, int level, const char *msg) |
| 1092 | { |
| 1093 | struct tm tm; |
| 1094 | char *p; |
| 1095 | |
| 1096 | /* Cleanup the log message. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1097 | p = trash.area; |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1098 | for (; *msg != '\0'; msg++, p++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1099 | if (p >= trash.area + trash.size - 1) { |
Thierry FOURNIER | ccf0063 | 2015-09-16 12:47:03 +0200 | [diff] [blame] | 1100 | /* Break the message if exceed the buffer size. */ |
| 1101 | *(p-4) = ' '; |
| 1102 | *(p-3) = '.'; |
| 1103 | *(p-2) = '.'; |
| 1104 | *(p-1) = '.'; |
| 1105 | break; |
| 1106 | } |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 1107 | if (isprint((unsigned char)*msg)) |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1108 | *p = *msg; |
| 1109 | else |
| 1110 | *p = '.'; |
| 1111 | } |
| 1112 | *p = '\0'; |
| 1113 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1114 | send_log(px, level, "%s\n", trash.area); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1115 | if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) { |
Christopher Faulet | f98d821 | 2020-10-02 18:13:52 +0200 | [diff] [blame] | 1116 | if (level == LOG_DEBUG && !(global.mode & MODE_DEBUG)) |
| 1117 | return; |
| 1118 | |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 1119 | get_localtime(date.tv_sec, &tm); |
| 1120 | fprintf(stderr, "[%s] %03d/%02d%02d%02d (%d) : %s\n", |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1121 | 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] | 1122 | (int)getpid(), trash.area); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1123 | fflush(stderr); |
| 1124 | } |
| 1125 | } |
| 1126 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1127 | /* This function just ensure that the yield will be always |
| 1128 | * returned with a timeout and permit to set some flags |
| 1129 | */ |
Aurelien DARRAGON | f5d3b31 | 2023-07-13 10:18:04 +0200 | [diff] [blame] | 1130 | __LJMP void hlua_yieldk(lua_State *L, int nresults, lua_KContext ctx, |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1131 | lua_KFunction k, int timeout, unsigned int flags) |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1132 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 1133 | struct hlua *hlua; |
| 1134 | |
| 1135 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 1136 | hlua = hlua_gethlua(L); |
| 1137 | if (!hlua) { |
| 1138 | return; |
| 1139 | } |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1140 | |
| 1141 | /* Set the wake timeout. If timeout is required, we set |
| 1142 | * the expiration time. |
| 1143 | */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1144 | hlua->wake_time = timeout; |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1145 | |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 1146 | hlua->flags |= flags; |
| 1147 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1148 | /* Process the yield. */ |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 1149 | MAY_LJMP(lua_yieldk(L, nresults, ctx, k)); |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1150 | } |
| 1151 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1152 | /* This function initialises the Lua environment stored in the stream. |
| 1153 | * 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] | 1154 | * 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] | 1155 | * |
| 1156 | * This function is particular. it initialises a new Lua thread. If the |
| 1157 | * initialisation fails (example: out of memory error), the lua function |
| 1158 | * throws an error (longjmp). |
| 1159 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1160 | * This function manipulates two Lua stacks: the main and the thread. Only |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 1161 | * the main stack can fail. The thread is not manipulated. This function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1162 | * MUST NOT manipulate the created thread stack state, because it is not |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1163 | * protected against errors thrown by the thread stack. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1164 | */ |
Aurelien DARRAGON | 5483bde | 2023-03-21 14:02:16 +0100 | [diff] [blame] | 1165 | 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] | 1166 | { |
| 1167 | lua->Mref = LUA_REFNIL; |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1168 | lua->flags = 0; |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 1169 | lua->gc_count = 0; |
Christopher Faulet | bc275a9 | 2020-02-26 14:55:16 +0100 | [diff] [blame] | 1170 | lua->wake_time = TICK_ETERNITY; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1171 | lua->state_id = state_id; |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 1172 | LIST_INIT(&lua->com); |
Aurelien DARRAGON | 5483bde | 2023-03-21 14:02:16 +0100 | [diff] [blame] | 1173 | if (!SET_SAFE_LJMP_PARENT(lua)) { |
| 1174 | lua->Tref = LUA_REFNIL; |
| 1175 | return 0; |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1176 | } |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1177 | lua->T = lua_newthread(hlua_states[state_id]); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1178 | if (!lua->T) { |
| 1179 | lua->Tref = LUA_REFNIL; |
Aurelien DARRAGON | 5483bde | 2023-03-21 14:02:16 +0100 | [diff] [blame] | 1180 | RESET_SAFE_LJMP_PARENT(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1181 | return 0; |
| 1182 | } |
| 1183 | hlua_sethlua(lua); |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1184 | lua->Tref = luaL_ref(hlua_states[state_id], LUA_REGISTRYINDEX); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1185 | lua->task = task; |
Aurelien DARRAGON | 5483bde | 2023-03-21 14:02:16 +0100 | [diff] [blame] | 1186 | RESET_SAFE_LJMP_PARENT(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1187 | return 1; |
| 1188 | } |
| 1189 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1190 | /* Used to destroy the Lua coroutine when the attached stream or task |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1191 | * is destroyed. The destroy also the memory context. The struct "lua" |
Aurelien DARRAGON | d5c8a10 | 2023-03-01 16:45:50 +0100 | [diff] [blame] | 1192 | * will be freed. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1193 | */ |
| 1194 | void hlua_ctx_destroy(struct hlua *lua) |
| 1195 | { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 1196 | if (!lua) |
Thierry FOURNIER | a718b29 | 2015-03-04 16:48:34 +0100 | [diff] [blame] | 1197 | return; |
| 1198 | |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 1199 | if (!lua->T) |
| 1200 | goto end; |
| 1201 | |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 1202 | /* Purge all the pending signals. */ |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1203 | notification_purge(&lua->com); |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 1204 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1205 | if (!SET_SAFE_LJMP(lua)) |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 1206 | return; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1207 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1208 | RESET_SAFE_LJMP(lua); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 1209 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1210 | if (!SET_SAFE_LJMP_PARENT(lua)) |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 1211 | return; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1212 | luaL_unref(hlua_states[lua->state_id], LUA_REGISTRYINDEX, lua->Tref); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1213 | RESET_SAFE_LJMP_PARENT(lua); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 1214 | /* Forces a garbage collecting process. If the Lua program is finished |
| 1215 | * without error, we run the GC on the thread pointer. Its freed all |
| 1216 | * the unused memory. |
| 1217 | * If the thread is finnish with an error or is currently yielded, |
| 1218 | * it seems that the GC applied on the thread doesn't clean anything, |
| 1219 | * so e run the GC on the main thread. |
| 1220 | * NOTE: maybe this action locks all the Lua threads untiml the en of |
| 1221 | * the garbage collection. |
| 1222 | */ |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 1223 | if (lua->gc_count) { |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1224 | if (!SET_SAFE_LJMP_PARENT(lua)) |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 1225 | return; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1226 | lua_gc(hlua_states[lua->state_id], LUA_GCCOLLECT, 0); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1227 | RESET_SAFE_LJMP_PARENT(lua); |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 1228 | } |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 1229 | |
Thierry FOURNIER | a7b536b | 2015-09-21 22:50:24 +0200 | [diff] [blame] | 1230 | lua->T = NULL; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 1231 | |
| 1232 | end: |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 1233 | pool_free(pool_head_hlua, lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1234 | } |
| 1235 | |
| 1236 | /* This function is used to restore the Lua context when a coroutine |
| 1237 | * fails. This function copy the common memory between old coroutine |
| 1238 | * and the new coroutine. The old coroutine is destroyed, and its |
| 1239 | * replaced by the new coroutine. |
| 1240 | * If the flag "keep_msg" is set, the last entry of the old is assumed |
| 1241 | * as string error message and it is copied in the new stack. |
| 1242 | */ |
| 1243 | static int hlua_ctx_renew(struct hlua *lua, int keep_msg) |
| 1244 | { |
| 1245 | lua_State *T; |
| 1246 | int new_ref; |
| 1247 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1248 | /* New Lua coroutine. */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1249 | T = lua_newthread(hlua_states[lua->state_id]); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1250 | if (!T) |
| 1251 | return 0; |
| 1252 | |
| 1253 | /* Copy last error message. */ |
| 1254 | if (keep_msg) |
| 1255 | lua_xmove(lua->T, T, 1); |
| 1256 | |
| 1257 | /* Copy data between the coroutines. */ |
| 1258 | lua_rawgeti(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 1259 | lua_xmove(lua->T, T, 1); |
Ilya Shipitsin | 46a030c | 2020-07-05 16:36:08 +0500 | [diff] [blame] | 1260 | new_ref = luaL_ref(T, LUA_REGISTRYINDEX); /* Value popped. */ |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1261 | |
| 1262 | /* Destroy old data. */ |
| 1263 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 1264 | |
| 1265 | /* The thread is garbage collected by Lua. */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1266 | luaL_unref(hlua_states[lua->state_id], LUA_REGISTRYINDEX, lua->Tref); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1267 | |
| 1268 | /* Fill the struct with the new coroutine values. */ |
| 1269 | lua->Mref = new_ref; |
| 1270 | lua->T = T; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1271 | lua->Tref = luaL_ref(hlua_states[lua->state_id], LUA_REGISTRYINDEX); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1272 | |
| 1273 | /* Set context. */ |
| 1274 | hlua_sethlua(lua); |
| 1275 | |
| 1276 | return 1; |
| 1277 | } |
| 1278 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1279 | void hlua_hook(lua_State *L, lua_Debug *ar) |
| 1280 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 1281 | struct hlua *hlua; |
| 1282 | |
| 1283 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 1284 | hlua = hlua_gethlua(L); |
| 1285 | if (!hlua) |
| 1286 | return; |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1287 | |
| 1288 | /* Lua cannot yield when its returning from a function, |
| 1289 | * so, we can fix the interrupt hook to 1 instruction, |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1290 | * expecting that the function is finished. |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1291 | */ |
| 1292 | if (lua_gethookmask(L) & LUA_MASKRET) { |
| 1293 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, 1); |
| 1294 | return; |
| 1295 | } |
| 1296 | |
| 1297 | /* restore the interrupt condition. */ |
| 1298 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 1299 | |
| 1300 | /* If we interrupt the Lua processing in yieldable state, we yield. |
| 1301 | * If the state is not yieldable, trying yield causes an error. |
| 1302 | */ |
| 1303 | if (lua_isyieldable(L)) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 1304 | MAY_LJMP(hlua_yieldk(L, 0, 0, NULL, TICK_ETERNITY, HLUA_CTRLYIELD)); |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1305 | |
Thierry FOURNIER | a85cfb1 | 2015-03-13 14:50:06 +0100 | [diff] [blame] | 1306 | /* If we cannot yield, update the clock and check the timeout. */ |
| 1307 | tv_update_date(0, 1); |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1308 | hlua->run_time += now_ms - hlua->start_time; |
| 1309 | if (hlua->max_time && hlua->run_time >= hlua->max_time) { |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1310 | lua_pushfstring(L, "execution timeout"); |
| 1311 | WILL_LJMP(lua_error(L)); |
| 1312 | } |
| 1313 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1314 | /* Update the start time. */ |
| 1315 | hlua->start_time = now_ms; |
| 1316 | |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1317 | /* Try to interrupt the process at the end of the current |
| 1318 | * unyieldable function. |
| 1319 | */ |
| 1320 | 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] | 1321 | } |
| 1322 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1323 | /* This function start or resumes the Lua stack execution. If the flag |
| 1324 | * "yield_allowed" if no set and the LUA stack execution returns a yield |
| 1325 | * The function return an error. |
| 1326 | * |
| 1327 | * The function can returns 4 values: |
| 1328 | * - HLUA_E_OK : The execution is terminated without any errors. |
| 1329 | * - HLUA_E_AGAIN : The execution must continue at the next associated |
| 1330 | * task wakeup. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1331 | * - 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] | 1332 | * the top of the stack. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1333 | * - HLUA_E_ERR : An error has occurred without error message. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1334 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1335 | * 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] | 1336 | * LUA code. |
| 1337 | */ |
| 1338 | static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed) |
| 1339 | { |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1340 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
| 1341 | int nres; |
| 1342 | #endif |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1343 | int ret; |
| 1344 | const char *msg; |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1345 | const char *trace; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1346 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1347 | /* Initialise run time counter. */ |
| 1348 | if (!HLUA_IS_RUNNING(lua)) |
| 1349 | lua->run_time = 0; |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1350 | |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1351 | /* Lock the whole Lua execution. This lock must be before the |
| 1352 | * label "resume_execution". |
| 1353 | */ |
Aurelien DARRAGON | e00bf5d | 2023-03-21 13:22:33 +0100 | [diff] [blame] | 1354 | hlua_lock(lua); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1355 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1356 | resume_execution: |
| 1357 | |
| 1358 | /* This hook interrupts the Lua processing each 'hlua_nb_instruction' |
| 1359 | * instructions. it is used for preventing infinite loops. |
| 1360 | */ |
| 1361 | lua_sethook(lua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 1362 | |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1363 | /* Remove all flags except the running flags. */ |
Thierry FOURNIER | 2f3867f | 2015-09-28 01:02:01 +0200 | [diff] [blame] | 1364 | HLUA_SET_RUN(lua); |
| 1365 | HLUA_CLR_CTRLYIELD(lua); |
| 1366 | HLUA_CLR_WAKERESWR(lua); |
| 1367 | HLUA_CLR_WAKEREQWR(lua); |
Christopher Faulet | a6e792f | 2021-08-04 17:58:21 +0200 | [diff] [blame] | 1368 | HLUA_CLR_NOYIELD(lua); |
| 1369 | if (!yield_allowed) |
| 1370 | HLUA_SET_NOYIELD(lua); |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1371 | |
Christopher Faulet | bc275a9 | 2020-02-26 14:55:16 +0100 | [diff] [blame] | 1372 | /* Update the start time and reset wake_time. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1373 | lua->start_time = now_ms; |
Christopher Faulet | bc275a9 | 2020-02-26 14:55:16 +0100 | [diff] [blame] | 1374 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1375 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1376 | /* Call the function. */ |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1377 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1378 | 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] | 1379 | #else |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1380 | ret = lua_resume(lua->T, hlua_states[lua->state_id], lua->nargs); |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1381 | #endif |
Aurelien DARRAGON | fff2796 | 2023-09-08 14:00:27 +0200 | [diff] [blame] | 1382 | |
| 1383 | /* reset nargs because those possibly passed to the lua_resume() call |
| 1384 | * were already consumed, and since we may call lua_resume() again |
| 1385 | * after a successful yield, we don't want to pass stale nargs hint |
| 1386 | * to the Lua API. As such, nargs should be set explicitly before each |
| 1387 | * lua_resume() (or hlua_ctx_resume()) invocation if needed. |
| 1388 | */ |
| 1389 | lua->nargs = 0; |
| 1390 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1391 | switch (ret) { |
| 1392 | |
| 1393 | case LUA_OK: |
| 1394 | ret = HLUA_E_OK; |
| 1395 | break; |
| 1396 | |
| 1397 | case LUA_YIELD: |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1398 | /* Check if the execution timeout is expired. It it is the case, we |
| 1399 | * break the Lua execution. |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1400 | */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1401 | tv_update_date(0, 1); |
| 1402 | lua->run_time += now_ms - lua->start_time; |
| 1403 | if (lua->max_time && lua->run_time > lua->max_time) { |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1404 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1405 | ret = HLUA_E_ETMOUT; |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1406 | break; |
| 1407 | } |
| 1408 | /* Process the forced yield. if the general yield is not allowed or |
| 1409 | * if no task were associated this the current Lua execution |
| 1410 | * coroutine, we resume the execution. Else we want to return in the |
| 1411 | * scheduler and we want to be waked up again, to continue the |
| 1412 | * current Lua execution. So we schedule our own task. |
| 1413 | */ |
| 1414 | if (HLUA_IS_CTRLYIELDING(lua)) { |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1415 | if (!yield_allowed || !lua->task) |
| 1416 | goto resume_execution; |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1417 | task_wakeup(lua->task, TASK_WOKEN_MSG); |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1418 | } |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1419 | if (!yield_allowed) { |
| 1420 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1421 | ret = HLUA_E_YIELD; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1422 | break; |
| 1423 | } |
| 1424 | ret = HLUA_E_AGAIN; |
| 1425 | break; |
| 1426 | |
| 1427 | case LUA_ERRRUN: |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1428 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1429 | /* Special exit case. The traditional exit is returned as an error |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1430 | * because the errors ares the only one mean to return immediately |
| 1431 | * from and lua execution. |
| 1432 | */ |
| 1433 | if (lua->flags & HLUA_EXIT) { |
| 1434 | ret = HLUA_E_OK; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 1435 | hlua_ctx_renew(lua, 1); |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1436 | break; |
| 1437 | } |
| 1438 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1439 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1440 | if (!lua_checkstack(lua->T, 1)) { |
| 1441 | ret = HLUA_E_ERR; |
| 1442 | break; |
| 1443 | } |
| 1444 | msg = lua_tostring(lua->T, -1); |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 1445 | trace = hlua_traceback(lua->T, ", "); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1446 | if (msg) |
Thierry Fournier | 46278ff | 2020-11-29 11:48:12 +0100 | [diff] [blame] | 1447 | lua_pushfstring(lua->T, "[state-id %d] runtime error: %s from %s", lua->state_id, msg, trace); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1448 | else |
Thierry Fournier | 46278ff | 2020-11-29 11:48:12 +0100 | [diff] [blame] | 1449 | lua_pushfstring(lua->T, "[state-id %d] unknown runtime error from %s", lua->state_id, trace); |
Aurelien DARRAGON | adac932 | 2024-03-01 19:54:16 +0100 | [diff] [blame^] | 1450 | |
| 1451 | /* Move the error msg at the top and then empty the stack except last msg */ |
| 1452 | lua_insert(lua->T, -lua_gettop(lua->T)); |
| 1453 | lua_settop(lua->T, 1); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1454 | ret = HLUA_E_ERRMSG; |
| 1455 | break; |
| 1456 | |
| 1457 | case LUA_ERRMEM: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1458 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1459 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1460 | ret = HLUA_E_NOMEM; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1461 | break; |
| 1462 | |
| 1463 | case LUA_ERRERR: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1464 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1465 | if (!lua_checkstack(lua->T, 1)) { |
| 1466 | ret = HLUA_E_ERR; |
| 1467 | break; |
| 1468 | } |
| 1469 | msg = lua_tostring(lua->T, -1); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1470 | if (msg) |
Thierry Fournier | 46278ff | 2020-11-29 11:48:12 +0100 | [diff] [blame] | 1471 | lua_pushfstring(lua->T, "[state-id %d] message handler error: %s", lua->state_id, msg); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1472 | else |
Thierry Fournier | 46278ff | 2020-11-29 11:48:12 +0100 | [diff] [blame] | 1473 | lua_pushfstring(lua->T, "[state-id %d] message handler error", lua->state_id); |
Aurelien DARRAGON | adac932 | 2024-03-01 19:54:16 +0100 | [diff] [blame^] | 1474 | |
| 1475 | /* Move the error msg at the top and then empty the stack except last msg */ |
| 1476 | lua_insert(lua->T, -lua_gettop(lua->T)); |
| 1477 | lua_settop(lua->T, 1); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1478 | ret = HLUA_E_ERRMSG; |
| 1479 | break; |
| 1480 | |
| 1481 | default: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1482 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1483 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1484 | ret = HLUA_E_ERR; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1485 | break; |
| 1486 | } |
| 1487 | |
| 1488 | switch (ret) { |
| 1489 | case HLUA_E_AGAIN: |
| 1490 | break; |
| 1491 | |
| 1492 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1493 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1494 | hlua_ctx_renew(lua, 1); |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1495 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1496 | break; |
| 1497 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1498 | case HLUA_E_ETMOUT: |
| 1499 | case HLUA_E_NOMEM: |
| 1500 | case HLUA_E_YIELD: |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1501 | case HLUA_E_ERR: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1502 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1503 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1504 | hlua_ctx_renew(lua, 0); |
| 1505 | break; |
| 1506 | |
| 1507 | case HLUA_E_OK: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1508 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1509 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1510 | break; |
| 1511 | } |
| 1512 | |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1513 | /* This is the main exit point, remove the Lua lock. */ |
Aurelien DARRAGON | e00bf5d | 2023-03-21 13:22:33 +0100 | [diff] [blame] | 1514 | hlua_unlock(lua); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1515 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1516 | return ret; |
| 1517 | } |
| 1518 | |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1519 | /* This function exit the current code. */ |
| 1520 | __LJMP static int hlua_done(lua_State *L) |
| 1521 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 1522 | struct hlua *hlua; |
| 1523 | |
| 1524 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 1525 | hlua = hlua_gethlua(L); |
| 1526 | if (!hlua) |
| 1527 | return 0; |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1528 | |
| 1529 | hlua->flags |= HLUA_EXIT; |
| 1530 | WILL_LJMP(lua_error(L)); |
| 1531 | |
| 1532 | return 0; |
| 1533 | } |
| 1534 | |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1535 | /* This function is an LUA binding. It provides a function |
| 1536 | * for deleting ACL from a referenced ACL file. |
| 1537 | */ |
| 1538 | __LJMP static int hlua_del_acl(lua_State *L) |
| 1539 | { |
| 1540 | const char *name; |
| 1541 | const char *key; |
| 1542 | struct pat_ref *ref; |
| 1543 | |
| 1544 | MAY_LJMP(check_args(L, 2, "del_acl")); |
| 1545 | |
| 1546 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1547 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1548 | |
| 1549 | ref = pat_ref_lookup(name); |
| 1550 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1551 | WILL_LJMP(luaL_error(L, "'del_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1552 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1553 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1554 | pat_ref_delete(ref, key); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1555 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1556 | return 0; |
| 1557 | } |
| 1558 | |
| 1559 | /* This function is an LUA binding. It provides a function |
| 1560 | * for deleting map entry from a referenced map file. |
| 1561 | */ |
| 1562 | static int hlua_del_map(lua_State *L) |
| 1563 | { |
| 1564 | const char *name; |
| 1565 | const char *key; |
| 1566 | struct pat_ref *ref; |
| 1567 | |
| 1568 | MAY_LJMP(check_args(L, 2, "del_map")); |
| 1569 | |
| 1570 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1571 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1572 | |
| 1573 | ref = pat_ref_lookup(name); |
| 1574 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1575 | WILL_LJMP(luaL_error(L, "'del_map': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1576 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1577 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1578 | pat_ref_delete(ref, key); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1579 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1580 | return 0; |
| 1581 | } |
| 1582 | |
| 1583 | /* This function is an LUA binding. It provides a function |
| 1584 | * for adding ACL pattern from a referenced ACL file. |
| 1585 | */ |
| 1586 | static int hlua_add_acl(lua_State *L) |
| 1587 | { |
| 1588 | const char *name; |
| 1589 | const char *key; |
| 1590 | struct pat_ref *ref; |
| 1591 | |
| 1592 | MAY_LJMP(check_args(L, 2, "add_acl")); |
| 1593 | |
| 1594 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1595 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1596 | |
| 1597 | ref = pat_ref_lookup(name); |
| 1598 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1599 | WILL_LJMP(luaL_error(L, "'add_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1600 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1601 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1602 | if (pat_ref_find_elt(ref, key) == NULL) |
| 1603 | pat_ref_add(ref, key, NULL, NULL); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1604 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1605 | return 0; |
| 1606 | } |
| 1607 | |
| 1608 | /* This function is an LUA binding. It provides a function |
| 1609 | * for setting map pattern and sample from a referenced map |
| 1610 | * file. |
| 1611 | */ |
| 1612 | static int hlua_set_map(lua_State *L) |
| 1613 | { |
| 1614 | const char *name; |
| 1615 | const char *key; |
| 1616 | const char *value; |
| 1617 | struct pat_ref *ref; |
| 1618 | |
| 1619 | MAY_LJMP(check_args(L, 3, "set_map")); |
| 1620 | |
| 1621 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1622 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1623 | value = MAY_LJMP(luaL_checkstring(L, 3)); |
| 1624 | |
| 1625 | ref = pat_ref_lookup(name); |
| 1626 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1627 | WILL_LJMP(luaL_error(L, "'set_map': unknown map file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1628 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1629 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1630 | if (pat_ref_find_elt(ref, key) != NULL) |
| 1631 | pat_ref_set(ref, key, value, NULL); |
| 1632 | else |
| 1633 | pat_ref_add(ref, key, value, NULL); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1634 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1635 | return 0; |
| 1636 | } |
| 1637 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1638 | /* A class is a lot of memory that contain data. This data can be a table, |
| 1639 | * 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] | 1640 | * metatable have an original version registered in the global context with |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1641 | * the name of the object (_G[<name>] = <metable> ). |
| 1642 | * |
| 1643 | * A metable is a table that modify the standard behavior of a standard |
| 1644 | * access to the associated data. The entries of this new metatable are |
| 1645 | * defined as is: |
| 1646 | * |
| 1647 | * http://lua-users.org/wiki/MetatableEvents |
| 1648 | * |
| 1649 | * __index |
| 1650 | * |
| 1651 | * we access an absent field in a table, the result is nil. This is |
| 1652 | * true, but it is not the whole truth. Actually, such access triggers |
| 1653 | * the interpreter to look for an __index metamethod: If there is no |
| 1654 | * such method, as usually happens, then the access results in nil; |
| 1655 | * otherwise, the metamethod will provide the result. |
| 1656 | * |
| 1657 | * Control 'prototype' inheritance. When accessing "myTable[key]" and |
| 1658 | * the key does not appear in the table, but the metatable has an __index |
| 1659 | * property: |
| 1660 | * |
| 1661 | * - if the value is a function, the function is called, passing in the |
| 1662 | * table and the key; the return value of that function is returned as |
| 1663 | * the result. |
| 1664 | * |
| 1665 | * - if the value is another table, the value of the key in that table is |
| 1666 | * asked for and returned (and if it doesn't exist in that table, but that |
| 1667 | * table's metatable has an __index property, then it continues on up) |
| 1668 | * |
| 1669 | * - Use "rawget(myTable,key)" to skip this metamethod. |
| 1670 | * |
| 1671 | * http://www.lua.org/pil/13.4.1.html |
| 1672 | * |
| 1673 | * __newindex |
| 1674 | * |
| 1675 | * Like __index, but control property assignment. |
| 1676 | * |
| 1677 | * __mode - Control weak references. A string value with one or both |
| 1678 | * of the characters 'k' and 'v' which specifies that the the |
| 1679 | * keys and/or values in the table are weak references. |
| 1680 | * |
| 1681 | * __call - Treat a table like a function. When a table is followed by |
| 1682 | * parenthesis such as "myTable( 'foo' )" and the metatable has |
| 1683 | * a __call key pointing to a function, that function is invoked |
| 1684 | * (passing any specified arguments) and the return value is |
| 1685 | * returned. |
| 1686 | * |
| 1687 | * __metatable - Hide the metatable. When "getmetatable( myTable )" is |
| 1688 | * called, if the metatable for myTable has a __metatable |
| 1689 | * key, the value of that key is returned instead of the |
| 1690 | * actual metatable. |
| 1691 | * |
| 1692 | * __tostring - Control string representation. When the builtin |
| 1693 | * "tostring( myTable )" function is called, if the metatable |
| 1694 | * for myTable has a __tostring property set to a function, |
| 1695 | * that function is invoked (passing myTable to it) and the |
| 1696 | * return value is used as the string representation. |
| 1697 | * |
| 1698 | * __len - Control table length. When the table length is requested using |
| 1699 | * the length operator ( '#' ), if the metatable for myTable has |
| 1700 | * a __len key pointing to a function, that function is invoked |
| 1701 | * (passing myTable to it) and the return value used as the value |
| 1702 | * of "#myTable". |
| 1703 | * |
| 1704 | * __gc - Userdata finalizer code. When userdata is set to be garbage |
| 1705 | * collected, if the metatable has a __gc field pointing to a |
| 1706 | * function, that function is first invoked, passing the userdata |
| 1707 | * to it. The __gc metamethod is not called for tables. |
| 1708 | * (See http://lua-users.org/lists/lua-l/2006-11/msg00508.html) |
| 1709 | * |
| 1710 | * Special metamethods for redefining standard operators: |
| 1711 | * http://www.lua.org/pil/13.1.html |
| 1712 | * |
| 1713 | * __add "+" |
| 1714 | * __sub "-" |
| 1715 | * __mul "*" |
| 1716 | * __div "/" |
| 1717 | * __unm "!" |
| 1718 | * __pow "^" |
| 1719 | * __concat ".." |
| 1720 | * |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1721 | * Special methods for redefining standard relations |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1722 | * http://www.lua.org/pil/13.2.html |
| 1723 | * |
| 1724 | * __eq "==" |
| 1725 | * __lt "<" |
| 1726 | * __le "<=" |
| 1727 | */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1728 | |
| 1729 | /* |
| 1730 | * |
| 1731 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1732 | * Class Map |
| 1733 | * |
| 1734 | * |
| 1735 | */ |
| 1736 | |
| 1737 | /* Returns a struct hlua_map if the stack entry "ud" is |
| 1738 | * a class session, otherwise it throws an error. |
| 1739 | */ |
| 1740 | __LJMP static struct map_descriptor *hlua_checkmap(lua_State *L, int ud) |
| 1741 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1742 | return MAY_LJMP(hlua_checkudata(L, ud, class_map_ref)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1743 | } |
| 1744 | |
| 1745 | /* This function is the map constructor. It don't need |
| 1746 | * the class Map object. It creates and return a new Map |
| 1747 | * object. It must be called only during "body" or "init" |
| 1748 | * context because it process some filesystem accesses. |
| 1749 | */ |
| 1750 | __LJMP static int hlua_map_new(struct lua_State *L) |
| 1751 | { |
| 1752 | const char *fn; |
| 1753 | int match = PAT_MATCH_STR; |
| 1754 | struct sample_conv conv; |
| 1755 | const char *file = ""; |
| 1756 | int line = 0; |
| 1757 | lua_Debug ar; |
| 1758 | char *err = NULL; |
| 1759 | struct arg args[2]; |
| 1760 | |
| 1761 | if (lua_gettop(L) < 1 || lua_gettop(L) > 2) |
| 1762 | WILL_LJMP(luaL_error(L, "'new' needs at least 1 argument.")); |
| 1763 | |
| 1764 | fn = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1765 | |
| 1766 | if (lua_gettop(L) >= 2) { |
| 1767 | match = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 1768 | if (match < 0 || match >= PAT_MATCH_NUM) |
| 1769 | WILL_LJMP(luaL_error(L, "'new' needs a valid match method.")); |
| 1770 | } |
| 1771 | |
| 1772 | /* Get Lua filename and line number. */ |
| 1773 | if (lua_getstack(L, 1, &ar)) { /* check function at level */ |
| 1774 | lua_getinfo(L, "Sl", &ar); /* get info about it */ |
| 1775 | if (ar.currentline > 0) { /* is there info? */ |
| 1776 | file = ar.short_src; |
| 1777 | line = ar.currentline; |
| 1778 | } |
| 1779 | } |
| 1780 | |
| 1781 | /* fill fake sample_conv struct. */ |
| 1782 | conv.kw = ""; /* unused. */ |
| 1783 | conv.process = NULL; /* unused. */ |
| 1784 | conv.arg_mask = 0; /* unused. */ |
| 1785 | conv.val_args = NULL; /* unused. */ |
| 1786 | conv.out_type = SMP_T_STR; |
| 1787 | conv.private = (void *)(long)match; |
| 1788 | switch (match) { |
| 1789 | case PAT_MATCH_STR: conv.in_type = SMP_T_STR; break; |
| 1790 | case PAT_MATCH_BEG: conv.in_type = SMP_T_STR; break; |
| 1791 | case PAT_MATCH_SUB: conv.in_type = SMP_T_STR; break; |
| 1792 | case PAT_MATCH_DIR: conv.in_type = SMP_T_STR; break; |
| 1793 | case PAT_MATCH_DOM: conv.in_type = SMP_T_STR; break; |
| 1794 | case PAT_MATCH_END: conv.in_type = SMP_T_STR; break; |
| 1795 | case PAT_MATCH_REG: conv.in_type = SMP_T_STR; break; |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1796 | case PAT_MATCH_INT: conv.in_type = SMP_T_SINT; break; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1797 | case PAT_MATCH_IP: conv.in_type = SMP_T_ADDR; break; |
| 1798 | default: |
| 1799 | WILL_LJMP(luaL_error(L, "'new' doesn't support this match mode.")); |
| 1800 | } |
| 1801 | |
| 1802 | /* fill fake args. */ |
| 1803 | args[0].type = ARGT_STR; |
Christopher Faulet | 73292e9 | 2020-08-06 08:40:09 +0200 | [diff] [blame] | 1804 | args[0].data.str.area = strdup(fn); |
| 1805 | args[0].data.str.data = strlen(fn); |
| 1806 | args[0].data.str.size = args[0].data.str.data+1; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1807 | args[1].type = ARGT_STOP; |
| 1808 | |
| 1809 | /* load the map. */ |
| 1810 | if (!sample_load_map(args, &conv, file, line, &err)) { |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 1811 | /* error case: we can't use luaL_error because we must |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1812 | * free the err variable. |
| 1813 | */ |
| 1814 | luaL_where(L, 1); |
| 1815 | lua_pushfstring(L, "'new': %s.", err); |
| 1816 | lua_concat(L, 2); |
| 1817 | free(err); |
Christopher Faulet | 6ad7df4 | 2020-08-07 11:45:18 +0200 | [diff] [blame] | 1818 | chunk_destroy(&args[0].data.str); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1819 | WILL_LJMP(lua_error(L)); |
| 1820 | } |
| 1821 | |
| 1822 | /* create the lua object. */ |
| 1823 | lua_newtable(L); |
| 1824 | lua_pushlightuserdata(L, args[0].data.map); |
| 1825 | lua_rawseti(L, -2, 0); |
| 1826 | |
| 1827 | /* Pop a class Map metatable and affect it to the userdata. */ |
| 1828 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_map_ref); |
| 1829 | lua_setmetatable(L, -2); |
| 1830 | |
| 1831 | |
| 1832 | return 1; |
| 1833 | } |
| 1834 | |
| 1835 | __LJMP static inline int _hlua_map_lookup(struct lua_State *L, int str) |
| 1836 | { |
| 1837 | struct map_descriptor *desc; |
| 1838 | struct pattern *pat; |
| 1839 | struct sample smp; |
| 1840 | |
| 1841 | MAY_LJMP(check_args(L, 2, "lookup")); |
| 1842 | desc = MAY_LJMP(hlua_checkmap(L, 1)); |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1843 | if (desc->pat.expect_type == SMP_T_SINT) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1844 | smp.data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1845 | smp.data.u.sint = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1846 | } |
| 1847 | else { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1848 | smp.data.type = SMP_T_STR; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1849 | smp.flags = SMP_F_CONST; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1850 | 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] | 1851 | smp.data.u.str.size = smp.data.u.str.data + 1; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1852 | } |
| 1853 | |
| 1854 | pat = pattern_exec_match(&desc->pat, &smp, 1); |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1855 | if (!pat || !pat->data) { |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1856 | if (str) |
| 1857 | lua_pushstring(L, ""); |
| 1858 | else |
| 1859 | lua_pushnil(L); |
| 1860 | return 1; |
| 1861 | } |
| 1862 | |
| 1863 | /* 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] | 1864 | 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] | 1865 | return 1; |
| 1866 | } |
| 1867 | |
| 1868 | __LJMP static int hlua_map_lookup(struct lua_State *L) |
| 1869 | { |
| 1870 | return _hlua_map_lookup(L, 0); |
| 1871 | } |
| 1872 | |
| 1873 | __LJMP static int hlua_map_slookup(struct lua_State *L) |
| 1874 | { |
| 1875 | return _hlua_map_lookup(L, 1); |
| 1876 | } |
| 1877 | |
| 1878 | /* |
| 1879 | * |
| 1880 | * |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1881 | * Class Socket |
| 1882 | * |
| 1883 | * |
| 1884 | */ |
| 1885 | |
| 1886 | __LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud) |
| 1887 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1888 | return MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1889 | } |
| 1890 | |
| 1891 | /* 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] | 1892 | * connection. It is used for notify space available to send or data |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1893 | * received. |
| 1894 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1895 | static void hlua_socket_handler(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1896 | { |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1897 | struct stream_interface *si = appctx->owner; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1898 | |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1899 | if (appctx->ctx.hlua_cosocket.die) { |
| 1900 | si_shutw(si); |
| 1901 | si_shutr(si); |
| 1902 | si_ic(si)->flags |= CF_READ_NULL; |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1903 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1904 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1905 | stream_shutdown(si_strm(si), SF_ERR_KILLED); |
| 1906 | } |
| 1907 | |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 1908 | /* If we can't write, wakeup the pending write signals. */ |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1909 | if (channel_output_closed(si_ic(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1910 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1911 | |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 1912 | /* If we can't read, wakeup the pending read signals. */ |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1913 | if (channel_input_closed(si_oc(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1914 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1915 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 1916 | /* if the connection is not established, inform the stream that we want |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 1917 | * to be notified whenever the connection completes. |
| 1918 | */ |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 1919 | if (si_opposite(si)->state < SI_ST_EST) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 1920 | si_cant_get(si); |
Willy Tarreau | 12c2423 | 2018-12-06 15:29:50 +0100 | [diff] [blame] | 1921 | si_rx_conn_blk(si); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1922 | si_rx_endp_more(si); |
Willy Tarreau | d4da196 | 2015-04-20 01:31:23 +0200 | [diff] [blame] | 1923 | return; |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 1924 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1925 | |
| 1926 | /* This function is called after the connect. */ |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 1927 | appctx->ctx.hlua_cosocket.connected = 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1928 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1929 | /* 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] | 1930 | if (channel_may_recv(si_ic(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1931 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1932 | |
| 1933 | /* Wake the tasks which wants to read if the buffer contains data. */ |
Christopher Faulet | 174144c | 2024-02-16 15:33:44 +0100 | [diff] [blame] | 1934 | if (!channel_is_empty(si_oc(si))) { |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1935 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
Christopher Faulet | 174144c | 2024-02-16 15:33:44 +0100 | [diff] [blame] | 1936 | si_cant_get(si); |
| 1937 | } |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1938 | |
| 1939 | /* Some data were injected in the buffer, notify the stream |
| 1940 | * interface. |
| 1941 | */ |
| 1942 | if (!channel_is_empty(si_ic(si))) |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 1943 | si_update(si); |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1944 | |
| 1945 | /* If write notifications are registered, we considers we want |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1946 | * to write, so we clear the blocking flag. |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1947 | */ |
| 1948 | if (notification_registered(&appctx->ctx.hlua_cosocket.wake_on_write)) |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1949 | si_rx_endp_more(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1950 | } |
| 1951 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1952 | /* This function is called when the "struct stream" is destroyed. |
| 1953 | * Remove the link from the object to this stream. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1954 | * Wake all the pending signals. |
| 1955 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1956 | static void hlua_socket_release(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1957 | { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1958 | struct xref *peer; |
| 1959 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1960 | /* Remove my link in the original object. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1961 | peer = xref_get_peer_and_lock(&appctx->ctx.hlua_cosocket.xref); |
| 1962 | if (peer) |
| 1963 | xref_disconnect(&appctx->ctx.hlua_cosocket.xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1964 | |
| 1965 | /* Wake all the task waiting for me. */ |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1966 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1967 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1968 | } |
| 1969 | |
| 1970 | /* If the garbage collectio of the object is launch, nobody |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1971 | * uses this object. If the stream does not exists, just quit. |
| 1972 | * Send the shutdown signal to the stream. In some cases, |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1973 | * pending signal can rest in the read and write lists. destroy |
| 1974 | * it. |
| 1975 | */ |
| 1976 | __LJMP static int hlua_socket_gc(lua_State *L) |
| 1977 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1978 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1979 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1980 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1981 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1982 | MAY_LJMP(check_args(L, 1, "__gc")); |
| 1983 | |
| 1984 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1985 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1986 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1987 | return 0; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1988 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1989 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1990 | /* Set the flag which destroy the session. */ |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1991 | appctx->ctx.hlua_cosocket.die = 1; |
| 1992 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1993 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1994 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1995 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1996 | return 0; |
| 1997 | } |
| 1998 | |
| 1999 | /* The close function send shutdown signal and break the |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2000 | * links between the stream and the object. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2001 | */ |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 2002 | __LJMP static int hlua_socket_close_helper(lua_State *L) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2003 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2004 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2005 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2006 | struct xref *peer; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2007 | struct hlua *hlua; |
| 2008 | |
| 2009 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 2010 | hlua = hlua_gethlua(L); |
| 2011 | if (!hlua) |
| 2012 | return 0; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2013 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2014 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2015 | |
| 2016 | /* Check if we run on the same thread than the xreator thread. |
| 2017 | * We cannot access to the socket if the thread is different. |
| 2018 | */ |
| 2019 | if (socket->tid != tid) |
| 2020 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2021 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2022 | peer = xref_get_peer_and_lock(&socket->xref); |
| 2023 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2024 | return 0; |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 2025 | |
| 2026 | hlua->gc_count--; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2027 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2028 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2029 | /* Set the flag which destroy the session. */ |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 2030 | appctx->ctx.hlua_cosocket.die = 1; |
| 2031 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2032 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2033 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2034 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2035 | return 0; |
| 2036 | } |
| 2037 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 2038 | /* The close function calls close_helper. |
| 2039 | */ |
| 2040 | __LJMP static int hlua_socket_close(lua_State *L) |
| 2041 | { |
| 2042 | MAY_LJMP(check_args(L, 1, "close")); |
| 2043 | return hlua_socket_close_helper(L); |
| 2044 | } |
| 2045 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2046 | /* This Lua function assumes that the stack contain three parameters. |
| 2047 | * 1 - USERDATA containing a struct socket |
| 2048 | * 2 - INTEGER with values of the macro defined below |
| 2049 | * If the integer is -1, we must read at most one line. |
| 2050 | * If the integer is -2, we ust read all the data until the |
| 2051 | * end of the stream. |
| 2052 | * If the integer is positive value, we must read a number of |
| 2053 | * bytes corresponding to this value. |
| 2054 | */ |
| 2055 | #define HLSR_READ_LINE (-1) |
| 2056 | #define HLSR_READ_ALL (-2) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2057 | __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] | 2058 | { |
| 2059 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2060 | int wanted = lua_tointeger(L, 2); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2061 | struct hlua *hlua; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2062 | struct appctx *appctx; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2063 | size_t len; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2064 | int nblk; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 2065 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2066 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 2067 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2068 | size_t len2; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 2069 | int skip_at_end = 0; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 2070 | struct channel *oc; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2071 | struct stream_interface *si; |
| 2072 | struct stream *s; |
| 2073 | struct xref *peer; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 2074 | int missing_bytes; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2075 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2076 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 2077 | hlua = hlua_gethlua(L); |
| 2078 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2079 | /* Check if this lua stack is schedulable. */ |
| 2080 | if (!hlua || !hlua->task) |
| 2081 | WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in " |
| 2082 | "'frontend', 'backend' or 'task'")); |
| 2083 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2084 | /* Check if we run on the same thread than the xreator thread. |
| 2085 | * We cannot access to the socket if the thread is different. |
| 2086 | */ |
| 2087 | if (socket->tid != tid) |
| 2088 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2089 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2090 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2091 | peer = xref_get_peer_and_lock(&socket->xref); |
| 2092 | if (!peer) |
| 2093 | goto no_peer; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2094 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2095 | si = appctx->owner; |
| 2096 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2097 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2098 | oc = &s->res; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2099 | if (wanted == HLSR_READ_LINE) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2100 | /* Read line. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2101 | nblk = co_getline_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2102 | if (nblk < 0) /* Connection close. */ |
| 2103 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2104 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2105 | goto connection_empty; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 2106 | |
| 2107 | /* remove final \r\n. */ |
| 2108 | if (nblk == 1) { |
| 2109 | if (blk1[len1-1] == '\n') { |
| 2110 | len1--; |
| 2111 | skip_at_end++; |
| 2112 | if (blk1[len1-1] == '\r') { |
| 2113 | len1--; |
| 2114 | skip_at_end++; |
| 2115 | } |
| 2116 | } |
| 2117 | } |
| 2118 | else { |
| 2119 | if (blk2[len2-1] == '\n') { |
| 2120 | len2--; |
| 2121 | skip_at_end++; |
| 2122 | if (blk2[len2-1] == '\r') { |
| 2123 | len2--; |
| 2124 | skip_at_end++; |
| 2125 | } |
| 2126 | } |
| 2127 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2128 | } |
| 2129 | |
| 2130 | else if (wanted == HLSR_READ_ALL) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2131 | /* Read all the available data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2132 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2133 | if (nblk < 0) /* Connection close. */ |
| 2134 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2135 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2136 | goto connection_empty; |
| 2137 | } |
| 2138 | |
| 2139 | else { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2140 | /* Read a block of data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2141 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2142 | if (nblk < 0) /* Connection close. */ |
| 2143 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2144 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2145 | goto connection_empty; |
| 2146 | |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 2147 | missing_bytes = wanted - socket->b.n; |
| 2148 | if (len1 > missing_bytes) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2149 | nblk = 1; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 2150 | len1 = missing_bytes; |
| 2151 | } if (nblk == 2 && len1 + len2 > missing_bytes) |
| 2152 | len2 = missing_bytes - len1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2153 | } |
| 2154 | |
| 2155 | len = len1; |
| 2156 | |
| 2157 | luaL_addlstring(&socket->b, blk1, len1); |
| 2158 | if (nblk == 2) { |
| 2159 | len += len2; |
| 2160 | luaL_addlstring(&socket->b, blk2, len2); |
| 2161 | } |
| 2162 | |
| 2163 | /* Consume data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2164 | co_skip(oc, len + skip_at_end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2165 | |
| 2166 | /* Don't wait anything. */ |
Christopher Faulet | 174144c | 2024-02-16 15:33:44 +0100 | [diff] [blame] | 2167 | si_want_get(si); |
Thierry FOURNIER | 7e4ee47 | 2018-05-25 15:03:50 +0200 | [diff] [blame] | 2168 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2169 | |
| 2170 | /* If the pattern reclaim to read all the data |
| 2171 | * in the connection, got out. |
| 2172 | */ |
| 2173 | if (wanted == HLSR_READ_ALL) |
| 2174 | goto connection_empty; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 2175 | else if (wanted >= 0 && socket->b.n < wanted) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2176 | goto connection_empty; |
| 2177 | |
| 2178 | /* Return result. */ |
| 2179 | luaL_pushresult(&socket->b); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2180 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2181 | return 1; |
| 2182 | |
| 2183 | connection_closed: |
| 2184 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2185 | xref_unlock(&socket->xref, peer); |
| 2186 | |
| 2187 | no_peer: |
| 2188 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2189 | /* If the buffer containds data. */ |
| 2190 | if (socket->b.n > 0) { |
| 2191 | luaL_pushresult(&socket->b); |
| 2192 | return 1; |
| 2193 | } |
| 2194 | lua_pushnil(L); |
| 2195 | lua_pushstring(L, "connection closed."); |
| 2196 | return 2; |
| 2197 | |
| 2198 | connection_empty: |
| 2199 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2200 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_read, hlua->task)) { |
| 2201 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2202 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2203 | } |
| 2204 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2205 | 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] | 2206 | return 0; |
| 2207 | } |
| 2208 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2209 | /* This Lua function gets two parameters. The first one can be string |
| 2210 | * or a number. If the string is "*l", the user requires one line. If |
| 2211 | * 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] | 2212 | * If the value is a number, the user require a number of bytes equal |
| 2213 | * to the value. The default value is "*l" (a line). |
| 2214 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2215 | * This parameter with a variable type is converted in integer. This |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2216 | * integer takes this values: |
| 2217 | * -1 : read a line |
| 2218 | * -2 : read all the stream |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2219 | * >0 : amount of bytes. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2220 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2221 | * The second parameter is optional. It contains a string that must be |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2222 | * concatenated with the read data. |
| 2223 | */ |
| 2224 | __LJMP static int hlua_socket_receive(struct lua_State *L) |
| 2225 | { |
| 2226 | int wanted = HLSR_READ_LINE; |
| 2227 | const char *pattern; |
Christopher Faulet | c31b200 | 2021-05-03 10:11:13 +0200 | [diff] [blame] | 2228 | int lastarg, type; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2229 | char *error; |
| 2230 | size_t len; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2231 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2232 | |
| 2233 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 2234 | WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments.")); |
| 2235 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2236 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2237 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2238 | /* Check if we run on the same thread than the xreator thread. |
| 2239 | * We cannot access to the socket if the thread is different. |
| 2240 | */ |
| 2241 | if (socket->tid != tid) |
| 2242 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2243 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2244 | /* check for pattern. */ |
| 2245 | if (lua_gettop(L) >= 2) { |
| 2246 | type = lua_type(L, 2); |
| 2247 | if (type == LUA_TSTRING) { |
| 2248 | pattern = lua_tostring(L, 2); |
| 2249 | if (strcmp(pattern, "*a") == 0) |
| 2250 | wanted = HLSR_READ_ALL; |
| 2251 | else if (strcmp(pattern, "*l") == 0) |
| 2252 | wanted = HLSR_READ_LINE; |
| 2253 | else { |
| 2254 | wanted = strtoll(pattern, &error, 10); |
| 2255 | if (*error != '\0') |
| 2256 | WILL_LJMP(luaL_error(L, "Unsupported pattern.")); |
| 2257 | } |
| 2258 | } |
| 2259 | else if (type == LUA_TNUMBER) { |
| 2260 | wanted = lua_tointeger(L, 2); |
| 2261 | if (wanted < 0) |
| 2262 | WILL_LJMP(luaL_error(L, "Unsupported size.")); |
| 2263 | } |
| 2264 | } |
| 2265 | |
| 2266 | /* Set pattern. */ |
| 2267 | lua_pushinteger(L, wanted); |
Tim Duesterhus | c6e377e | 2018-01-04 19:32:13 +0100 | [diff] [blame] | 2268 | |
| 2269 | /* Check if we would replace the top by itself. */ |
| 2270 | if (lua_gettop(L) != 2) |
| 2271 | lua_replace(L, 2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2272 | |
Christopher Faulet | c31b200 | 2021-05-03 10:11:13 +0200 | [diff] [blame] | 2273 | /* Save index of the top of the stack because since buffers are used, it |
| 2274 | * may change |
| 2275 | */ |
| 2276 | lastarg = lua_gettop(L); |
| 2277 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2278 | /* init buffer, and fill it with prefix. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2279 | luaL_buffinit(L, &socket->b); |
| 2280 | |
| 2281 | /* Check prefix. */ |
Christopher Faulet | c31b200 | 2021-05-03 10:11:13 +0200 | [diff] [blame] | 2282 | if (lastarg >= 3) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2283 | if (lua_type(L, 3) != LUA_TSTRING) |
| 2284 | WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix")); |
| 2285 | pattern = lua_tolstring(L, 3, &len); |
| 2286 | luaL_addlstring(&socket->b, pattern, len); |
| 2287 | } |
| 2288 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2289 | return __LJMP(hlua_socket_receive_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2290 | } |
| 2291 | |
| 2292 | /* Write the Lua input string in the output buffer. |
Mark Lakes | 22154b4 | 2018-01-29 14:38:40 -0800 | [diff] [blame] | 2293 | * This function returns a yield if no space is available. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2294 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2295 | 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] | 2296 | { |
| 2297 | struct hlua_socket *socket; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2298 | struct hlua *hlua; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2299 | struct appctx *appctx; |
| 2300 | size_t buf_len; |
| 2301 | const char *buf; |
| 2302 | int len; |
| 2303 | int send_len; |
| 2304 | int sent; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2305 | struct xref *peer; |
| 2306 | struct stream_interface *si; |
| 2307 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2308 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2309 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 2310 | hlua = hlua_gethlua(L); |
| 2311 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2312 | /* Check if this lua stack is schedulable. */ |
| 2313 | if (!hlua || !hlua->task) |
| 2314 | WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in " |
| 2315 | "'frontend', 'backend' or 'task'")); |
| 2316 | |
| 2317 | /* Get object */ |
| 2318 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2319 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2320 | sent = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2321 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2322 | /* Check if we run on the same thread than the xreator thread. |
| 2323 | * We cannot access to the socket if the thread is different. |
| 2324 | */ |
| 2325 | if (socket->tid != tid) |
| 2326 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2327 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2328 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2329 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2330 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2331 | lua_pushinteger(L, -1); |
| 2332 | return 1; |
| 2333 | } |
| 2334 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2335 | si = appctx->owner; |
| 2336 | s = si_strm(si); |
| 2337 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2338 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2339 | if (channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2340 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2341 | lua_pushinteger(L, -1); |
| 2342 | return 1; |
| 2343 | } |
| 2344 | |
| 2345 | /* Update the input buffer data. */ |
| 2346 | buf += sent; |
| 2347 | send_len = buf_len - sent; |
| 2348 | |
| 2349 | /* All the data are sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2350 | if (sent >= buf_len) { |
| 2351 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2352 | return 1; /* Implicitly return the length sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2353 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2354 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2355 | /* Check if the buffer is available because HAProxy doesn't allocate |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 2356 | * the request buffer if its not required. |
| 2357 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2358 | if (s->req.buf.size == 0) { |
Willy Tarreau | 581abd3 | 2018-10-25 10:21:41 +0200 | [diff] [blame] | 2359 | if (!si_alloc_ibuf(si, &appctx->buffer_wait)) |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2360 | goto hlua_socket_write_yield_return; |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 2361 | } |
| 2362 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2363 | /* Check for available space. */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2364 | len = b_room(&s->req.buf); |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2365 | if (len <= 0) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2366 | goto hlua_socket_write_yield_return; |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2367 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2368 | |
| 2369 | /* send data */ |
| 2370 | if (len < send_len) |
| 2371 | send_len = len; |
Thierry FOURNIER | 66b8919 | 2018-05-27 01:14:47 +0200 | [diff] [blame] | 2372 | len = ci_putblk(&s->req, buf, send_len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2373 | |
| 2374 | /* "Not enough space" (-1), "Buffer too little to contain |
| 2375 | * the data" (-2) are not expected because the available length |
| 2376 | * is tested. |
| 2377 | * Other unknown error are also not expected. |
| 2378 | */ |
| 2379 | if (len <= 0) { |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2380 | if (len == -1) |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2381 | s->req.flags |= CF_WAKE_WRITE; |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2382 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 2383 | MAY_LJMP(hlua_socket_close_helper(L)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2384 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2385 | lua_pushinteger(L, -1); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2386 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2387 | return 1; |
| 2388 | } |
| 2389 | |
| 2390 | /* update buffers. */ |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 2391 | appctx_wakeup(appctx); |
Willy Tarreau | de70fa1 | 2015-09-26 11:25:05 +0200 | [diff] [blame] | 2392 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2393 | s->req.rex = TICK_ETERNITY; |
| 2394 | s->res.wex = TICK_ETERNITY; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2395 | |
| 2396 | /* Update length sent. */ |
| 2397 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2398 | lua_pushinteger(L, sent + len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2399 | |
| 2400 | /* All the data buffer is sent ? */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2401 | if (sent + len >= buf_len) { |
| 2402 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2403 | return 1; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2404 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2405 | |
| 2406 | hlua_socket_write_yield_return: |
Thierry FOURNIER | ba42fcd | 2018-05-27 00:59:48 +0200 | [diff] [blame] | 2407 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2408 | xref_unlock(&socket->xref, peer); |
| 2409 | WILL_LJMP(luaL_error(L, "out of memory")); |
| 2410 | } |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2411 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2412 | 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] | 2413 | return 0; |
| 2414 | } |
| 2415 | |
| 2416 | /* This function initiate the send of data. It just check the input |
| 2417 | * parameters and push an integer in the Lua stack that contain the |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2418 | * 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] | 2419 | * "hlua_socket_write_yield" that can yield. |
| 2420 | * |
| 2421 | * The Lua function gets between 3 and 4 parameters. The first one is |
| 2422 | * the associated object. The second is a string buffer. The third is |
| 2423 | * a facultative integer that represents where is the buffer position |
| 2424 | * of the start of the data that can send. The first byte is the |
| 2425 | * position "1". The default value is "1". The fourth argument is a |
| 2426 | * facultative integer that represents where is the buffer position |
| 2427 | * of the end of the data that can send. The default is the last byte. |
| 2428 | */ |
| 2429 | static int hlua_socket_send(struct lua_State *L) |
| 2430 | { |
| 2431 | int i; |
| 2432 | int j; |
| 2433 | const char *buf; |
| 2434 | size_t buf_len; |
| 2435 | |
| 2436 | /* Check number of arguments. */ |
| 2437 | if (lua_gettop(L) < 2 || lua_gettop(L) > 4) |
| 2438 | WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments")); |
| 2439 | |
| 2440 | /* Get the string. */ |
| 2441 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
| 2442 | |
| 2443 | /* Get and check j. */ |
| 2444 | if (lua_gettop(L) == 4) { |
| 2445 | j = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 2446 | if (j < 0) |
| 2447 | j = buf_len + j + 1; |
| 2448 | if (j > buf_len) |
| 2449 | j = buf_len + 1; |
| 2450 | lua_pop(L, 1); |
| 2451 | } |
| 2452 | else |
| 2453 | j = buf_len; |
| 2454 | |
| 2455 | /* Get and check i. */ |
| 2456 | if (lua_gettop(L) == 3) { |
| 2457 | i = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2458 | if (i < 0) |
| 2459 | i = buf_len + i + 1; |
| 2460 | if (i > buf_len) |
| 2461 | i = buf_len + 1; |
| 2462 | lua_pop(L, 1); |
| 2463 | } else |
| 2464 | i = 1; |
| 2465 | |
| 2466 | /* Check bth i and j. */ |
| 2467 | if (i > j) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2468 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2469 | return 1; |
| 2470 | } |
| 2471 | if (i == 0 && j == 0) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2472 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2473 | return 1; |
| 2474 | } |
| 2475 | if (i == 0) |
| 2476 | i = 1; |
| 2477 | if (j == 0) |
| 2478 | j = 1; |
| 2479 | |
| 2480 | /* Pop the string. */ |
| 2481 | lua_pop(L, 1); |
| 2482 | |
| 2483 | /* Update the buffer length. */ |
| 2484 | buf += i - 1; |
| 2485 | buf_len = j - i + 1; |
| 2486 | lua_pushlstring(L, buf, buf_len); |
| 2487 | |
| 2488 | /* This unsigned is used to remember the amount of sent data. */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2489 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2490 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2491 | return MAY_LJMP(hlua_socket_write_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2492 | } |
| 2493 | |
Willy Tarreau | 22b0a68 | 2015-06-17 19:43:49 +0200 | [diff] [blame] | 2494 | #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] | 2495 | __LJMP static inline int hlua_socket_info(struct lua_State *L, struct sockaddr_storage *addr) |
| 2496 | { |
| 2497 | static char buffer[SOCKET_INFO_MAX_LEN]; |
| 2498 | int ret; |
| 2499 | int len; |
| 2500 | char *p; |
| 2501 | |
| 2502 | ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1); |
| 2503 | if (ret <= 0) { |
| 2504 | lua_pushnil(L); |
| 2505 | return 1; |
| 2506 | } |
| 2507 | |
| 2508 | if (ret == AF_UNIX) { |
| 2509 | lua_pushstring(L, buffer+1); |
| 2510 | return 1; |
| 2511 | } |
| 2512 | else if (ret == AF_INET6) { |
| 2513 | buffer[0] = '['; |
| 2514 | len = strlen(buffer); |
| 2515 | buffer[len] = ']'; |
| 2516 | len++; |
| 2517 | buffer[len] = ':'; |
| 2518 | len++; |
| 2519 | p = buffer; |
| 2520 | } |
| 2521 | else if (ret == AF_INET) { |
| 2522 | p = buffer + 1; |
| 2523 | len = strlen(p); |
| 2524 | p[len] = ':'; |
| 2525 | len++; |
| 2526 | } |
| 2527 | else { |
| 2528 | lua_pushnil(L); |
| 2529 | return 1; |
| 2530 | } |
| 2531 | |
| 2532 | if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) { |
| 2533 | lua_pushnil(L); |
| 2534 | return 1; |
| 2535 | } |
| 2536 | |
| 2537 | lua_pushstring(L, p); |
| 2538 | return 1; |
| 2539 | } |
| 2540 | |
| 2541 | /* Returns information about the peer of the connection. */ |
| 2542 | __LJMP static int hlua_socket_getpeername(struct lua_State *L) |
| 2543 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2544 | struct hlua_socket *socket; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2545 | struct xref *peer; |
| 2546 | struct appctx *appctx; |
| 2547 | struct stream_interface *si; |
| 2548 | struct stream *s; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2549 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2550 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2551 | MAY_LJMP(check_args(L, 1, "getpeername")); |
| 2552 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2553 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2554 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2555 | /* Check if we run on the same thread than the xreator thread. |
| 2556 | * We cannot access to the socket if the thread is different. |
| 2557 | */ |
| 2558 | if (socket->tid != tid) |
| 2559 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2560 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2561 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2562 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2563 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2564 | lua_pushnil(L); |
| 2565 | return 1; |
| 2566 | } |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2567 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2568 | si = appctx->owner; |
| 2569 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2570 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2571 | if (!s->target_addr) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2572 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2573 | lua_pushnil(L); |
| 2574 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2575 | } |
| 2576 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2577 | ret = MAY_LJMP(hlua_socket_info(L, s->target_addr)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2578 | xref_unlock(&socket->xref, peer); |
| 2579 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2580 | } |
| 2581 | |
| 2582 | /* Returns information about my connection side. */ |
| 2583 | static int hlua_socket_getsockname(struct lua_State *L) |
| 2584 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2585 | struct hlua_socket *socket; |
| 2586 | struct connection *conn; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2587 | struct appctx *appctx; |
| 2588 | struct xref *peer; |
| 2589 | struct stream_interface *si; |
| 2590 | struct stream *s; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2591 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2592 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2593 | MAY_LJMP(check_args(L, 1, "getsockname")); |
| 2594 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2595 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2596 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2597 | /* Check if we run on the same thread than the xreator thread. |
| 2598 | * We cannot access to the socket if the thread is different. |
| 2599 | */ |
| 2600 | if (socket->tid != tid) |
| 2601 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2602 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2603 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2604 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2605 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2606 | lua_pushnil(L); |
| 2607 | return 1; |
| 2608 | } |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2609 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2610 | si = appctx->owner; |
| 2611 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2612 | |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 2613 | conn = cs_conn(objt_cs(s->si[1].end)); |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2614 | if (!conn || !conn_get_src(conn)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2615 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2616 | lua_pushnil(L); |
| 2617 | return 1; |
| 2618 | } |
| 2619 | |
Willy Tarreau | 9da9a6f | 2019-07-17 14:49:44 +0200 | [diff] [blame] | 2620 | ret = hlua_socket_info(L, conn->src); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2621 | xref_unlock(&socket->xref, peer); |
| 2622 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2623 | } |
| 2624 | |
| 2625 | /* This struct define the applet. */ |
Willy Tarreau | 3057645 | 2015-04-13 13:50:30 +0200 | [diff] [blame] | 2626 | static struct applet update_applet = { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2627 | .obj_type = OBJ_TYPE_APPLET, |
| 2628 | .name = "<LUA_TCP>", |
| 2629 | .fct = hlua_socket_handler, |
| 2630 | .release = hlua_socket_release, |
| 2631 | }; |
| 2632 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2633 | __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] | 2634 | { |
| 2635 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2636 | struct hlua *hlua; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2637 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2638 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2639 | struct stream_interface *si; |
| 2640 | struct stream *s; |
| 2641 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2642 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 2643 | hlua = hlua_gethlua(L); |
| 2644 | if (!hlua) |
| 2645 | return 0; |
| 2646 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2647 | /* Check if we run on the same thread than the xreator thread. |
| 2648 | * We cannot access to the socket if the thread is different. |
| 2649 | */ |
| 2650 | if (socket->tid != tid) |
| 2651 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2652 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2653 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2654 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2655 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2656 | lua_pushnil(L); |
| 2657 | lua_pushstring(L, "Can't connect"); |
| 2658 | return 2; |
| 2659 | } |
| 2660 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2661 | si = appctx->owner; |
| 2662 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2663 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2664 | /* Check if we run on the same thread than the xreator thread. |
| 2665 | * We cannot access to the socket if the thread is different. |
| 2666 | */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2667 | if (socket->tid != tid) { |
| 2668 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2669 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2670 | } |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2671 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2672 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2673 | if (!hlua || channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2674 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2675 | lua_pushnil(L); |
| 2676 | lua_pushstring(L, "Can't connect"); |
| 2677 | return 2; |
| 2678 | } |
| 2679 | |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 2680 | appctx = __objt_appctx(s->si[0].end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2681 | |
| 2682 | /* Check for connection established. */ |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2683 | if (appctx->ctx.hlua_cosocket.connected) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2684 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2685 | lua_pushinteger(L, 1); |
| 2686 | return 1; |
| 2687 | } |
| 2688 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2689 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2690 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2691 | WILL_LJMP(luaL_error(L, "out of memory error")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2692 | } |
| 2693 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2694 | 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] | 2695 | return 0; |
| 2696 | } |
| 2697 | |
| 2698 | /* This function fail or initite the connection. */ |
| 2699 | __LJMP static int hlua_socket_connect(struct lua_State *L) |
| 2700 | { |
| 2701 | struct hlua_socket *socket; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2702 | int port = -1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2703 | const char *ip; |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2704 | struct hlua *hlua; |
| 2705 | struct appctx *appctx; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2706 | int low, high; |
| 2707 | struct sockaddr_storage *addr; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2708 | struct xref *peer; |
| 2709 | struct stream_interface *si; |
| 2710 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2711 | |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2712 | if (lua_gettop(L) < 2) |
| 2713 | WILL_LJMP(luaL_error(L, "connect: need at least 2 arguments")); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2714 | |
| 2715 | /* Get args. */ |
| 2716 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2717 | |
| 2718 | /* Check if we run on the same thread than the xreator thread. |
| 2719 | * We cannot access to the socket if the thread is different. |
| 2720 | */ |
| 2721 | if (socket->tid != tid) |
| 2722 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2723 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2724 | ip = MAY_LJMP(luaL_checkstring(L, 2)); |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 2725 | if (lua_gettop(L) >= 3) { |
| 2726 | luaL_Buffer b; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2727 | port = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2728 | |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 2729 | /* Force the ip to end with a colon, to support IPv6 addresses |
| 2730 | * that are not enclosed within square brackets. |
| 2731 | */ |
| 2732 | if (port > 0) { |
| 2733 | luaL_buffinit(L, &b); |
| 2734 | luaL_addstring(&b, ip); |
| 2735 | luaL_addchar(&b, ':'); |
| 2736 | luaL_pushresult(&b); |
| 2737 | ip = lua_tolstring(L, lua_gettop(L), NULL); |
| 2738 | } |
| 2739 | } |
| 2740 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2741 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2742 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2743 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2744 | lua_pushnil(L); |
| 2745 | return 1; |
| 2746 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2747 | |
| 2748 | /* Parse ip address. */ |
Willy Tarreau | 5fc9328 | 2020-09-16 18:25:03 +0200 | [diff] [blame] | 2749 | 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] | 2750 | if (!addr) { |
| 2751 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2752 | WILL_LJMP(luaL_error(L, "connect: cannot parse destination address '%s'", ip)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2753 | } |
Willy Tarreau | 9da9a6f | 2019-07-17 14:49:44 +0200 | [diff] [blame] | 2754 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2755 | /* Set port. */ |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2756 | if (low == 0) { |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2757 | if (addr->ss_family == AF_INET) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2758 | if (port == -1) { |
| 2759 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2760 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2761 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2762 | ((struct sockaddr_in *)addr)->sin_port = htons(port); |
| 2763 | } else if (addr->ss_family == AF_INET6) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2764 | if (port == -1) { |
| 2765 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2766 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2767 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2768 | ((struct sockaddr_in6 *)addr)->sin6_port = htons(port); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2769 | } |
| 2770 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2771 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2772 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2773 | si = appctx->owner; |
| 2774 | s = si_strm(si); |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2775 | |
Willy Tarreau | 9b7587a | 2020-10-15 07:32:10 +0200 | [diff] [blame] | 2776 | if (!sockaddr_alloc(&s->target_addr, addr, sizeof(*addr))) { |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2777 | xref_unlock(&socket->xref, peer); |
| 2778 | WILL_LJMP(luaL_error(L, "connect: internal error")); |
| 2779 | } |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2780 | s->flags |= SF_ADDR_SET; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2781 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2782 | /* Get hlua struct, or NULL if we execute from main lua state */ |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2783 | hlua = hlua_gethlua(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2784 | if (!hlua) |
| 2785 | return 0; |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2786 | |
| 2787 | /* inform the stream that we want to be notified whenever the |
| 2788 | * connection completes. |
| 2789 | */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 2790 | si_cant_get(&s->si[0]); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 2791 | si_rx_endp_more(&s->si[0]); |
Thierry FOURNIER | 8c8fbbe | 2015-09-26 17:02:35 +0200 | [diff] [blame] | 2792 | appctx_wakeup(appctx); |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2793 | |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 2794 | hlua->gc_count++; |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 2795 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2796 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2797 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2798 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2799 | } |
| 2800 | xref_unlock(&socket->xref, peer); |
PiBa-NL | 706d5ee | 2018-05-05 23:51:42 +0200 | [diff] [blame] | 2801 | |
| 2802 | task_wakeup(s->task, TASK_WOKEN_INIT); |
| 2803 | /* Return yield waiting for connection. */ |
| 2804 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2805 | 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] | 2806 | |
| 2807 | return 0; |
| 2808 | } |
| 2809 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2810 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2811 | __LJMP static int hlua_socket_connect_ssl(struct lua_State *L) |
| 2812 | { |
| 2813 | struct hlua_socket *socket; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2814 | struct xref *peer; |
| 2815 | struct appctx *appctx; |
| 2816 | struct stream_interface *si; |
| 2817 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2818 | |
| 2819 | MAY_LJMP(check_args(L, 3, "connect_ssl")); |
| 2820 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2821 | |
| 2822 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2823 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2824 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2825 | lua_pushnil(L); |
| 2826 | return 1; |
| 2827 | } |
| 2828 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2829 | si = appctx->owner; |
| 2830 | s = si_strm(si); |
| 2831 | |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 2832 | s->target = &socket_ssl->obj_type; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2833 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2834 | return MAY_LJMP(hlua_socket_connect(L)); |
| 2835 | } |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2836 | #endif |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2837 | |
| 2838 | __LJMP static int hlua_socket_setoption(struct lua_State *L) |
| 2839 | { |
| 2840 | return 0; |
| 2841 | } |
| 2842 | |
| 2843 | __LJMP static int hlua_socket_settimeout(struct lua_State *L) |
| 2844 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2845 | struct hlua_socket *socket; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2846 | int tmout; |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2847 | double dtmout; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2848 | struct xref *peer; |
| 2849 | struct appctx *appctx; |
| 2850 | struct stream_interface *si; |
| 2851 | struct stream *s; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2852 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2853 | MAY_LJMP(check_args(L, 2, "settimeout")); |
| 2854 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2855 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2856 | |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2857 | /* convert the timeout to millis */ |
| 2858 | dtmout = MAY_LJMP(luaL_checknumber(L, 2)) * 1000; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2859 | |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 2860 | /* Check for negative values */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2861 | if (dtmout < 0) |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 2862 | WILL_LJMP(luaL_error(L, "settimeout: cannot set negatives values")); |
| 2863 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2864 | if (dtmout > INT_MAX) /* overflow check */ |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2865 | 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] | 2866 | |
| 2867 | tmout = MS_TO_TICKS((int)dtmout); |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2868 | if (tmout == 0) |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 2869 | tmout++; /* very small timeouts are adjusted to a minimum of 1ms */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2870 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2871 | /* Check if we run on the same thread than the xreator thread. |
| 2872 | * We cannot access to the socket if the thread is different. |
| 2873 | */ |
| 2874 | if (socket->tid != tid) |
| 2875 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2876 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2877 | /* check for connection break. If some data were read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2878 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2879 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2880 | hlua_pusherror(L, "socket: not yet initialised, you can't set timeouts."); |
| 2881 | WILL_LJMP(lua_error(L)); |
| 2882 | return 0; |
| 2883 | } |
| 2884 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2885 | si = appctx->owner; |
| 2886 | s = si_strm(si); |
| 2887 | |
Cyril Bonté | 7bb6345 | 2018-08-17 23:51:02 +0200 | [diff] [blame] | 2888 | s->sess->fe->timeout.connect = tmout; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2889 | s->req.rto = tmout; |
| 2890 | s->req.wto = tmout; |
| 2891 | s->res.rto = tmout; |
| 2892 | s->res.wto = tmout; |
Cyril Bonté | 7bb6345 | 2018-08-17 23:51:02 +0200 | [diff] [blame] | 2893 | s->req.rex = tick_add_ifset(now_ms, tmout); |
| 2894 | s->req.wex = tick_add_ifset(now_ms, tmout); |
| 2895 | s->res.rex = tick_add_ifset(now_ms, tmout); |
| 2896 | s->res.wex = tick_add_ifset(now_ms, tmout); |
| 2897 | |
| 2898 | s->task->expire = tick_add_ifset(now_ms, tmout); |
| 2899 | task_queue(s->task); |
| 2900 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2901 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2902 | |
Thierry Fournier | e9636f1 | 2018-03-08 09:54:32 +0100 | [diff] [blame] | 2903 | lua_pushinteger(L, 1); |
Tim Duesterhus | 119a5f1 | 2018-01-06 19:16:25 +0100 | [diff] [blame] | 2904 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2905 | } |
| 2906 | |
| 2907 | __LJMP static int hlua_socket_new(lua_State *L) |
| 2908 | { |
| 2909 | struct hlua_socket *socket; |
| 2910 | struct appctx *appctx; |
Willy Tarreau | 15b5e14 | 2015-04-04 14:38:25 +0200 | [diff] [blame] | 2911 | struct session *sess; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2912 | struct stream *strm; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2913 | |
| 2914 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2915 | if (!lua_checkstack(L, 3)) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2916 | hlua_pusherror(L, "socket: full stack"); |
| 2917 | goto out_fail_conf; |
| 2918 | } |
| 2919 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2920 | /* Create the object: obj[0] = userdata. */ |
| 2921 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2922 | socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket))); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2923 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2924 | memset(socket, 0, sizeof(*socket)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2925 | socket->tid = tid; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2926 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 2927 | /* Check if the various memory pools are initialized. */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 2928 | if (!pool_head_stream || !pool_head_buffer) { |
Thierry FOURNIER | 4a6170c | 2015-03-09 17:07:10 +0100 | [diff] [blame] | 2929 | hlua_pusherror(L, "socket: uninitialized pools."); |
| 2930 | goto out_fail_conf; |
| 2931 | } |
| 2932 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2933 | /* Pop a class stream metatable and affect it to the userdata. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2934 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref); |
| 2935 | lua_setmetatable(L, -2); |
| 2936 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2937 | /* Create the applet context */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 2938 | appctx = appctx_new(&update_applet, tid_bit); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2939 | if (!appctx) { |
| 2940 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | feb7640 | 2015-04-03 14:10:06 +0200 | [diff] [blame] | 2941 | goto out_fail_conf; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2942 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2943 | |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2944 | appctx->ctx.hlua_cosocket.connected = 0; |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 2945 | appctx->ctx.hlua_cosocket.die = 0; |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2946 | LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_write); |
| 2947 | LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_read); |
Willy Tarreau | b2bf833 | 2015-04-04 15:58:58 +0200 | [diff] [blame] | 2948 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2949 | /* Now create a session, task and stream for this applet */ |
Amaury Denoyelle | 239fdbf | 2021-03-24 10:22:03 +0100 | [diff] [blame] | 2950 | sess = session_new(socket_proxy, NULL, &appctx->obj_type); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2951 | if (!sess) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2952 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2953 | goto out_fail_sess; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2954 | } |
| 2955 | |
Christopher Faulet | 26256f8 | 2020-09-14 11:40:13 +0200 | [diff] [blame] | 2956 | strm = stream_new(sess, &appctx->obj_type, &BUF_NULL); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2957 | if (!strm) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2958 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2959 | goto out_fail_stream; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2960 | } |
| 2961 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2962 | /* Initialise cross reference between stream and Lua socket object. */ |
| 2963 | xref_create(&socket->xref, &appctx->ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2964 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2965 | /* Configure "right" stream interface. this "si" is used to connect |
| 2966 | * and retrieve data from the server. The connection is initialized |
| 2967 | * with the "struct server". |
| 2968 | */ |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2969 | si_set_state(&strm->si[1], SI_ST_ASS); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2970 | |
| 2971 | /* Force destination server. */ |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2972 | strm->flags |= SF_DIRECT | SF_ASSIGNED | SF_BE_ASSIGNED; |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 2973 | strm->target = &socket_tcp->obj_type; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2974 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2975 | return 1; |
| 2976 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2977 | out_fail_stream: |
Willy Tarreau | 11c3624 | 2015-04-04 15:54:03 +0200 | [diff] [blame] | 2978 | session_free(sess); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2979 | out_fail_sess: |
| 2980 | appctx_free(appctx); |
| 2981 | out_fail_conf: |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2982 | WILL_LJMP(lua_error(L)); |
| 2983 | return 0; |
| 2984 | } |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 2985 | |
| 2986 | /* |
| 2987 | * |
| 2988 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2989 | * Class Channel |
| 2990 | * |
| 2991 | * |
| 2992 | */ |
| 2993 | |
| 2994 | /* Returns the struct hlua_channel join to the class channel in the |
| 2995 | * stack entry "ud" or throws an argument error. |
| 2996 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2997 | __LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2998 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 2999 | return MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3000 | } |
| 3001 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3002 | /* Pushes the channel onto the top of the stack. If the stask does not have a |
| 3003 | * free slots, the function fails and returns 0; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3004 | */ |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 3005 | static int hlua_channel_new(lua_State *L, struct channel *channel) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3006 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3007 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 3008 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3009 | return 0; |
| 3010 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 3011 | lua_newtable(L); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3012 | lua_pushlightuserdata(L, channel); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 3013 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3014 | |
| 3015 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 3016 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref); |
| 3017 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3018 | return 1; |
| 3019 | } |
| 3020 | |
| 3021 | /* Duplicate all the data present in the input channel and put it |
| 3022 | * in a string LUA variables. Returns -1 and push a nil value in |
| 3023 | * the stack if the channel is closed and all the data are consumed, |
| 3024 | * returns 0 if no data are available, otherwise it returns the length |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 3025 | * of the built string. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3026 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3027 | static inline int _hlua_channel_dup(struct channel *chn, lua_State *L) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3028 | { |
| 3029 | char *blk1; |
| 3030 | char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3031 | size_t len1; |
| 3032 | size_t len2; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3033 | int ret; |
| 3034 | luaL_Buffer b; |
| 3035 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3036 | ret = ci_getblk_nc(chn, &blk1, &len1, &blk2, &len2); |
Christopher Faulet | 055310f | 2021-08-05 11:58:37 +0200 | [diff] [blame] | 3037 | if (unlikely(ret <= 0)) { |
| 3038 | if (ret < 0 || HLUA_CANT_YIELD(hlua_gethlua(L))) { |
| 3039 | lua_pushnil(L); |
| 3040 | return -1; |
| 3041 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3042 | return 0; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3043 | } |
| 3044 | |
| 3045 | luaL_buffinit(L, &b); |
| 3046 | luaL_addlstring(&b, blk1, len1); |
| 3047 | if (unlikely(ret == 2)) |
| 3048 | luaL_addlstring(&b, blk2, len2); |
| 3049 | luaL_pushresult(&b); |
| 3050 | |
| 3051 | if (unlikely(ret == 2)) |
| 3052 | return len1 + len2; |
| 3053 | return len1; |
| 3054 | } |
| 3055 | |
| 3056 | /* "_hlua_channel_dup" wrapper. If no data are available, it returns |
| 3057 | * a yield. This function keep the data in the buffer. |
| 3058 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3059 | __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] | 3060 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3061 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3062 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3063 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3064 | |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 3065 | if (IS_HTX_STRM(chn_strm(chn))) { |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3066 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3067 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3068 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3069 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3070 | if (_hlua_channel_dup(chn, L) == 0) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3071 | 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] | 3072 | return 1; |
| 3073 | } |
| 3074 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3075 | /* Check arguments for the function "hlua_channel_dup_yield". */ |
| 3076 | __LJMP static int hlua_channel_dup(lua_State *L) |
| 3077 | { |
| 3078 | MAY_LJMP(check_args(L, 1, "dup")); |
| 3079 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3080 | return MAY_LJMP(hlua_channel_dup_yield(L, 0, 0)); |
| 3081 | } |
| 3082 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3083 | /* "_hlua_channel_dup" wrapper. If no data are available, it returns |
| 3084 | * a yield. This function consumes the data in the buffer. It returns |
| 3085 | * a string containing the data or a nil pointer if no data are available |
| 3086 | * and the channel is closed. |
| 3087 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3088 | __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] | 3089 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3090 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3091 | int ret; |
| 3092 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3093 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3094 | |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 3095 | if (IS_HTX_STRM(chn_strm(chn))) { |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3096 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3097 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3098 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3099 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3100 | ret = _hlua_channel_dup(chn, L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3101 | if (unlikely(ret == 0)) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3102 | 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] | 3103 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3104 | if (unlikely(ret == -1)) |
| 3105 | return 1; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3106 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3107 | b_sub(&chn->buf, ret); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3108 | return 1; |
| 3109 | } |
| 3110 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3111 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3112 | __LJMP static int hlua_channel_get(lua_State *L) |
| 3113 | { |
| 3114 | MAY_LJMP(check_args(L, 1, "get")); |
| 3115 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3116 | return MAY_LJMP(hlua_channel_get_yield(L, 0, 0)); |
| 3117 | } |
| 3118 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3119 | /* This functions consumes and returns one line. If the channel is closed, |
| 3120 | * 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] | 3121 | * 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] | 3122 | * value. |
| 3123 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3124 | __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] | 3125 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3126 | char *blk1; |
| 3127 | char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3128 | size_t len1; |
| 3129 | size_t len2; |
| 3130 | size_t len; |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3131 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3132 | int ret; |
| 3133 | luaL_Buffer b; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3134 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3135 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3136 | |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 3137 | if (IS_HTX_STRM(chn_strm(chn))) { |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3138 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3139 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3140 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3141 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3142 | ret = ci_getline_nc(chn, &blk1, &len1, &blk2, &len2); |
Christopher Faulet | 055310f | 2021-08-05 11:58:37 +0200 | [diff] [blame] | 3143 | if (ret == 0) { |
| 3144 | if (HLUA_CANT_YIELD(hlua_gethlua(L))) { |
| 3145 | _hlua_channel_dup(chn, L); |
| 3146 | return 1; |
| 3147 | } |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3148 | 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] | 3149 | } |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3150 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3151 | if (ret == -1) { |
| 3152 | lua_pushnil(L); |
| 3153 | return 1; |
| 3154 | } |
| 3155 | |
| 3156 | luaL_buffinit(L, &b); |
| 3157 | luaL_addlstring(&b, blk1, len1); |
| 3158 | len = len1; |
| 3159 | if (unlikely(ret == 2)) { |
| 3160 | luaL_addlstring(&b, blk2, len2); |
| 3161 | len += len2; |
| 3162 | } |
| 3163 | luaL_pushresult(&b); |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3164 | 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] | 3165 | return 1; |
| 3166 | } |
| 3167 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3168 | /* Check arguments for the function "hlua_channel_getline_yield". */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3169 | __LJMP static int hlua_channel_getline(lua_State *L) |
| 3170 | { |
| 3171 | MAY_LJMP(check_args(L, 1, "getline")); |
| 3172 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3173 | return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0)); |
| 3174 | } |
| 3175 | |
Christopher Faulet | 3b7bc3e | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3176 | /* This function takes a string as argument, and append it at the input side of |
| 3177 | * channel. If data cannot be copied, because there is not enough space to do so |
| 3178 | * or because the channel is closed, it returns -1. Otherwise, it returns the |
| 3179 | * amount of data written (the string length). This function does not yield. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3180 | */ |
Christopher Faulet | 3b7bc3e | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3181 | __LJMP static int hlua_channel_append(lua_State *L) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3182 | { |
Christopher Faulet | 3b7bc3e | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3183 | struct channel *chn; |
| 3184 | const char *str; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3185 | size_t len; |
Christopher Faulet | 3b7bc3e | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3186 | |
| 3187 | MAY_LJMP(check_args(L, 2, "append")); |
| 3188 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3189 | str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3190 | |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 3191 | if (IS_HTX_STRM(chn_strm(chn))) { |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3192 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3193 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3194 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3195 | |
Christopher Faulet | 3b7bc3e | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3196 | if (len > c_room(chn) || ci_putblk(chn, str, len) < 0) |
| 3197 | lua_pushinteger(L, -1); |
| 3198 | else |
| 3199 | lua_pushinteger(L, len); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3200 | return 1; |
| 3201 | } |
| 3202 | |
Christopher Faulet | 3b7bc3e | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3203 | /* The function replaces input data from the channel by the string passed as |
| 3204 | * argument. Before clearing the buffer, we take care the copy will be |
| 3205 | * possible. It returns the amount of data written (the string length) on |
| 3206 | * 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] | 3207 | */ |
| 3208 | __LJMP static int hlua_channel_set(lua_State *L) |
| 3209 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3210 | struct channel *chn; |
Christopher Faulet | 3b7bc3e | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3211 | const char *str; |
| 3212 | size_t len; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3213 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3214 | MAY_LJMP(check_args(L, 2, "set")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3215 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 3b7bc3e | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3216 | str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3217 | |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 3218 | if (IS_HTX_STRM(chn_strm(chn))) { |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3219 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3220 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3221 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3222 | |
Christopher Faulet | 3b7bc3e | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3223 | /* Be sure we can copied the string once input data will be removed. */ |
| 3224 | if (len > ci_data(chn) || channel_input_closed(chn)) |
| 3225 | lua_pushinteger(L, -1); |
| 3226 | else { |
| 3227 | b_set_data(&chn->buf, co_data(chn)); |
| 3228 | if (ci_putblk(chn, str, len) < 0) |
| 3229 | lua_pushinteger(L, -1); |
| 3230 | else |
| 3231 | lua_pushinteger(L, len); |
| 3232 | } |
| 3233 | return -1; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3234 | } |
| 3235 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3236 | /* Append data in the output side of the buffer. This data is immediately |
| 3237 | * sent. The function returns the amount of data written. If the buffer |
| 3238 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3239 | * if the channel is closed. |
| 3240 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3241 | __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] | 3242 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3243 | struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3244 | size_t len; |
| 3245 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3246 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3247 | int max; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 3248 | struct hlua *hlua; |
| 3249 | |
| 3250 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 3251 | hlua = hlua_gethlua(L); |
| 3252 | if (!hlua) { |
| 3253 | lua_pushnil(L); |
| 3254 | return 1; |
| 3255 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +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 | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3262 | if (unlikely(channel_output_closed(chn))) { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3263 | lua_pushinteger(L, -1); |
| 3264 | return 1; |
| 3265 | } |
| 3266 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3267 | /* Check if the buffer is available because HAProxy doesn't allocate |
Thierry FOURNIER | 3e3a608 | 2015-03-05 17:06:12 +0100 | [diff] [blame] | 3268 | * the request buffer if its not required. |
| 3269 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3270 | if (chn->buf.size == 0) { |
Christopher Faulet | 055310f | 2021-08-05 11:58:37 +0200 | [diff] [blame] | 3271 | if (HLUA_CANT_YIELD(hlua_gethlua(L))) |
| 3272 | return 1; |
Willy Tarreau | 4b962a4 | 2018-11-15 11:03:21 +0100 | [diff] [blame] | 3273 | si_rx_buff_blk(chn_prod(chn)); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3274 | 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] | 3275 | } |
| 3276 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3277 | /* The written data will be immediately sent, so we can check |
| 3278 | * the available space without taking in account the reserve. |
| 3279 | * The reserve is guaranteed for the processing of incoming |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3280 | * data, because the buffer will be flushed. |
| 3281 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3282 | max = b_room(&chn->buf); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3283 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3284 | /* If there is no space available, and the output buffer is empty. |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3285 | * in this case, we cannot add more data, so we cannot yield, |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3286 | * we return the amount of copied data. |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3287 | */ |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3288 | if (max == 0 && co_data(chn) == 0) |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3289 | return 1; |
| 3290 | |
| 3291 | /* Adjust the real required length. */ |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3292 | if (max > len - l) |
| 3293 | max = len - l; |
| 3294 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3295 | /* The buffer available size may be not contiguous. This test |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3296 | * detects a non contiguous buffer and realign it. |
| 3297 | */ |
Willy Tarreau | 3f67999 | 2018-06-15 15:06:42 +0200 | [diff] [blame] | 3298 | if (ci_space_for_replace(chn) < max) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3299 | channel_slow_realign(chn, trash.area); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3300 | |
| 3301 | /* Copy input data in the buffer. */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3302 | 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] | 3303 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3304 | /* buffer replace considers that the input part is filled. |
| 3305 | * so, I must forward these new data in the output part. |
| 3306 | */ |
Willy Tarreau | bcbd393 | 2018-06-06 07:13:22 +0200 | [diff] [blame] | 3307 | c_adv(chn, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3308 | |
| 3309 | l += max; |
| 3310 | lua_pop(L, 1); |
| 3311 | lua_pushinteger(L, l); |
| 3312 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3313 | if (l < len) { |
Christopher Faulet | 055310f | 2021-08-05 11:58:37 +0200 | [diff] [blame] | 3314 | /* If there is no space available, and the output buffer is empty. |
| 3315 | * in this case, we cannot add more data, so we cannot yield, |
| 3316 | * we return the amount of copied data. |
| 3317 | */ |
| 3318 | max = b_room(&chn->buf); |
| 3319 | if ((max == 0 && co_data(chn) == 0) || HLUA_CANT_YIELD(hlua_gethlua(L))) |
| 3320 | return 1; |
| 3321 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3322 | /* If we are waiting for space in the response buffer, we |
| 3323 | * must set the flag WAKERESWR. This flag required the task |
| 3324 | * wake up if any activity is detected on the response buffer. |
| 3325 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3326 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3327 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3328 | else |
| 3329 | HLUA_SET_WAKEREQWR(hlua); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3330 | 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] | 3331 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3332 | |
| 3333 | return 1; |
| 3334 | } |
| 3335 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3336 | /* Just a wrapper of "_hlua_channel_send". This wrapper permits |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3337 | * yield the LUA process, and resume it without checking the |
| 3338 | * input arguments. |
| 3339 | */ |
| 3340 | __LJMP static int hlua_channel_send(lua_State *L) |
| 3341 | { |
| 3342 | MAY_LJMP(check_args(L, 2, "send")); |
| 3343 | lua_pushinteger(L, 0); |
| 3344 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3345 | return MAY_LJMP(hlua_channel_send_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3346 | } |
| 3347 | |
| 3348 | /* This function forward and amount of butes. The data pass from |
| 3349 | * the input side of the buffer to the output side, and can be |
| 3350 | * forwarded. This function never fails. |
| 3351 | * |
| 3352 | * The Lua function takes an amount of bytes to be forwarded in |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3353 | * input. It returns the number of bytes forwarded. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3354 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3355 | __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] | 3356 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3357 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3358 | int len; |
| 3359 | int l; |
| 3360 | int max; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 3361 | struct hlua *hlua; |
| 3362 | |
| 3363 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 3364 | hlua = hlua_gethlua(L); |
| 3365 | if (!hlua) |
| 3366 | return 1; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3367 | |
| 3368 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3369 | |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 3370 | if (IS_HTX_STRM(chn_strm(chn))) { |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3371 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3372 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3373 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3374 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3375 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3376 | l = MAY_LJMP(luaL_checkinteger(L, -1)); |
| 3377 | |
| 3378 | max = len - l; |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3379 | if (max > ci_data(chn)) |
| 3380 | max = ci_data(chn); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3381 | channel_forward(chn, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3382 | l += max; |
| 3383 | |
| 3384 | lua_pop(L, 1); |
| 3385 | lua_pushinteger(L, l); |
| 3386 | |
| 3387 | /* Check if it miss bytes to forward. */ |
| 3388 | if (l < len) { |
| 3389 | /* The the input channel or the output channel are closed, we |
| 3390 | * must return the amount of data forwarded. |
| 3391 | */ |
Christopher Faulet | 055310f | 2021-08-05 11:58:37 +0200 | [diff] [blame] | 3392 | 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] | 3393 | return 1; |
| 3394 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3395 | /* If we are waiting for space data in the response buffer, we |
| 3396 | * must set the flag WAKERESWR. This flag required the task |
| 3397 | * wake up if any activity is detected on the response buffer. |
| 3398 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3399 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3400 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3401 | else |
| 3402 | HLUA_SET_WAKEREQWR(hlua); |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3403 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3404 | /* Otherwise, we can yield waiting for new data in the inpout side. */ |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3405 | 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] | 3406 | } |
| 3407 | |
| 3408 | return 1; |
| 3409 | } |
| 3410 | |
| 3411 | /* Just check the input and prepare the stack for the previous |
| 3412 | * function "hlua_channel_forward_yield" |
| 3413 | */ |
| 3414 | __LJMP static int hlua_channel_forward(lua_State *L) |
| 3415 | { |
| 3416 | MAY_LJMP(check_args(L, 2, "forward")); |
| 3417 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3418 | MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3419 | |
| 3420 | lua_pushinteger(L, 0); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3421 | return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3422 | } |
| 3423 | |
| 3424 | /* Just returns the number of bytes available in the input |
| 3425 | * side of the buffer. This function never fails. |
| 3426 | */ |
| 3427 | __LJMP static int hlua_channel_get_in_len(lua_State *L) |
| 3428 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3429 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3430 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3431 | MAY_LJMP(check_args(L, 1, "get_in_len")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3432 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a3ceac1 | 2018-12-14 13:39:09 +0100 | [diff] [blame] | 3433 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3434 | struct htx *htx = htxbuf(&chn->buf); |
| 3435 | lua_pushinteger(L, htx->data - co_data(chn)); |
| 3436 | } |
| 3437 | else |
| 3438 | lua_pushinteger(L, ci_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3439 | return 1; |
| 3440 | } |
| 3441 | |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3442 | /* Returns true if the channel is full. */ |
| 3443 | __LJMP static int hlua_channel_is_full(lua_State *L) |
| 3444 | { |
| 3445 | struct channel *chn; |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3446 | |
| 3447 | MAY_LJMP(check_args(L, 1, "is_full")); |
| 3448 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 0ec740e | 2020-02-26 11:59:19 +0100 | [diff] [blame] | 3449 | /* ignore the reserve, we are not on a producer side (ie in an |
| 3450 | * applet). |
| 3451 | */ |
| 3452 | lua_pushboolean(L, channel_full(chn, 0)); |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3453 | return 1; |
| 3454 | } |
| 3455 | |
Christopher Faulet | 2ac9ba2 | 2020-02-25 10:15:50 +0100 | [diff] [blame] | 3456 | /* Returns true if the channel is the response channel. */ |
| 3457 | __LJMP static int hlua_channel_is_resp(lua_State *L) |
| 3458 | { |
| 3459 | struct channel *chn; |
| 3460 | |
| 3461 | MAY_LJMP(check_args(L, 1, "is_resp")); |
| 3462 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3463 | |
| 3464 | lua_pushboolean(L, !!(chn->flags & CF_ISRESP)); |
| 3465 | return 1; |
| 3466 | } |
| 3467 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3468 | /* Just returns the number of bytes available in the output |
| 3469 | * side of the buffer. This function never fails. |
| 3470 | */ |
| 3471 | __LJMP static int hlua_channel_get_out_len(lua_State *L) |
| 3472 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3473 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3474 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3475 | MAY_LJMP(check_args(L, 1, "get_out_len")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3476 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3477 | lua_pushinteger(L, co_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3478 | return 1; |
| 3479 | } |
| 3480 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3481 | /* |
| 3482 | * |
| 3483 | * |
| 3484 | * Class Fetches |
| 3485 | * |
| 3486 | * |
| 3487 | */ |
| 3488 | |
| 3489 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3490 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3491 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3492 | __LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud) |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3493 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3494 | return MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3495 | } |
| 3496 | |
| 3497 | /* This function creates and push in the stack a fetch object according |
| 3498 | * with a current TXN. |
| 3499 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3500 | 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] | 3501 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3502 | struct hlua_smp *hsmp; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3503 | |
| 3504 | /* Check stack size. */ |
| 3505 | if (!lua_checkstack(L, 3)) |
| 3506 | return 0; |
| 3507 | |
| 3508 | /* Create the object: obj[0] = userdata. |
| 3509 | * Note that the base of the Fetches object is the |
| 3510 | * transaction object. |
| 3511 | */ |
| 3512 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3513 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3514 | lua_rawseti(L, -2, 0); |
| 3515 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3516 | hsmp->s = txn->s; |
| 3517 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 3518 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3519 | hsmp->flags = flags; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3520 | |
| 3521 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 3522 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref); |
| 3523 | lua_setmetatable(L, -2); |
| 3524 | |
| 3525 | return 1; |
| 3526 | } |
| 3527 | |
| 3528 | /* This function is an LUA binding. It is called with each sample-fetch. |
| 3529 | * It uses closure argument to store the associated sample-fetch. It |
| 3530 | * returns only one argument or throws an error. An error is thrown |
| 3531 | * only if an error is encountered during the argument parsing. If |
| 3532 | * the "sample-fetch" function fails, nil is returned. |
| 3533 | */ |
| 3534 | __LJMP static int hlua_run_sample_fetch(lua_State *L) |
| 3535 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3536 | struct hlua_smp *hsmp; |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 3537 | struct sample_fetch *f; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 3538 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3539 | int i; |
| 3540 | struct sample smp; |
| 3541 | |
| 3542 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3543 | f = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3544 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3545 | /* Get traditional arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3546 | hsmp = MAY_LJMP(hlua_checkfetches(L, 1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3547 | |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 3548 | /* Check execution authorization. */ |
| 3549 | if (f->use & SMP_USE_HTTP_ANY && |
| 3550 | !(hsmp->flags & HLUA_F_MAY_USE_HTTP)) { |
| 3551 | lua_pushfstring(L, "the sample-fetch '%s' needs an HTTP parser which " |
| 3552 | "is not available in Lua services", f->kw); |
| 3553 | WILL_LJMP(lua_error(L)); |
| 3554 | } |
| 3555 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3556 | /* Get extra arguments. */ |
| 3557 | for (i = 0; i < lua_gettop(L) - 1; i++) { |
| 3558 | if (i >= ARGM_NBARGS) |
| 3559 | break; |
| 3560 | hlua_lua2arg(L, i + 2, &args[i]); |
| 3561 | } |
| 3562 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3563 | args[i].data.str.area = NULL; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3564 | |
| 3565 | /* Check arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3566 | 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] | 3567 | |
| 3568 | /* Run the special args checker. */ |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 3569 | if (f->val_args && !f->val_args(args, NULL)) { |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3570 | lua_pushfstring(L, "error in arguments"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3571 | goto error; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3572 | } |
| 3573 | |
| 3574 | /* Initialise the sample. */ |
| 3575 | memset(&smp, 0, sizeof(smp)); |
| 3576 | |
| 3577 | /* Run the sample fetch process. */ |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 3578 | 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] | 3579 | if (!f->process(args, &smp, f->kw, f->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3580 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3581 | lua_pushstring(L, ""); |
| 3582 | else |
| 3583 | lua_pushnil(L); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3584 | goto end; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3585 | } |
| 3586 | |
| 3587 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3588 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3589 | hlua_smp2lua_str(L, &smp); |
| 3590 | else |
| 3591 | hlua_smp2lua(L, &smp); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3592 | |
| 3593 | end: |
| 3594 | for (i = 0; args[i].type != ARGT_STOP; i++) { |
| 3595 | if (args[i].type == ARGT_STR) |
| 3596 | chunk_destroy(&args[i].data.str); |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 3597 | else if (args[i].type == ARGT_REG) |
| 3598 | regex_free(args[i].data.reg); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3599 | } |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3600 | return 1; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3601 | |
| 3602 | error: |
| 3603 | for (i = 0; args[i].type != ARGT_STOP; i++) { |
| 3604 | if (args[i].type == ARGT_STR) |
| 3605 | chunk_destroy(&args[i].data.str); |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 3606 | else if (args[i].type == ARGT_REG) |
| 3607 | regex_free(args[i].data.reg); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3608 | } |
| 3609 | WILL_LJMP(lua_error(L)); |
| 3610 | return 0; /* Never reached */ |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3611 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3612 | |
| 3613 | /* |
| 3614 | * |
| 3615 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3616 | * Class Converters |
| 3617 | * |
| 3618 | * |
| 3619 | */ |
| 3620 | |
| 3621 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3622 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3623 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3624 | __LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3625 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3626 | return MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3627 | } |
| 3628 | |
| 3629 | /* This function creates and push in the stack a Converters object |
| 3630 | * according with a current TXN. |
| 3631 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3632 | 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] | 3633 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3634 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3635 | |
| 3636 | /* Check stack size. */ |
| 3637 | if (!lua_checkstack(L, 3)) |
| 3638 | return 0; |
| 3639 | |
| 3640 | /* Create the object: obj[0] = userdata. |
| 3641 | * Note that the base of the Converters object is the |
| 3642 | * same than the TXN object. |
| 3643 | */ |
| 3644 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3645 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3646 | lua_rawseti(L, -2, 0); |
| 3647 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3648 | hsmp->s = txn->s; |
| 3649 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 3650 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3651 | hsmp->flags = flags; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3652 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3653 | /* Pop a class stream metatable and affect it to the table. */ |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3654 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref); |
| 3655 | lua_setmetatable(L, -2); |
| 3656 | |
| 3657 | return 1; |
| 3658 | } |
| 3659 | |
| 3660 | /* This function is an LUA binding. It is called with each converter. |
| 3661 | * It uses closure argument to store the associated converter. It |
| 3662 | * returns only one argument or throws an error. An error is thrown |
| 3663 | * only if an error is encountered during the argument parsing. If |
| 3664 | * the converter function function fails, nil is returned. |
| 3665 | */ |
| 3666 | __LJMP static int hlua_run_sample_conv(lua_State *L) |
| 3667 | { |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3668 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3669 | struct sample_conv *conv; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 3670 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3671 | int i; |
| 3672 | struct sample smp; |
| 3673 | |
| 3674 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3675 | conv = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3676 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3677 | /* Get traditional arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3678 | hsmp = MAY_LJMP(hlua_checkconverters(L, 1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3679 | |
| 3680 | /* Get extra arguments. */ |
| 3681 | for (i = 0; i < lua_gettop(L) - 2; i++) { |
| 3682 | if (i >= ARGM_NBARGS) |
| 3683 | break; |
| 3684 | hlua_lua2arg(L, i + 3, &args[i]); |
| 3685 | } |
| 3686 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3687 | args[i].data.str.area = NULL; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3688 | |
| 3689 | /* Check arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3690 | 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] | 3691 | |
| 3692 | /* Run the special args checker. */ |
| 3693 | if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) { |
| 3694 | hlua_pusherror(L, "error in arguments"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3695 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3696 | } |
| 3697 | |
| 3698 | /* Initialise the sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 3699 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3700 | if (!hlua_lua2smp(L, 2, &smp)) { |
| 3701 | hlua_pusherror(L, "error in the input argument"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3702 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3703 | } |
| 3704 | |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 3705 | smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR); |
| 3706 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3707 | /* Apply expected cast. */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3708 | if (!sample_casts[smp.data.type][conv->in_type]) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3709 | hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'", |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3710 | smp_to_type[smp.data.type], smp_to_type[conv->in_type]); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3711 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3712 | } |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3713 | if (sample_casts[smp.data.type][conv->in_type] != c_none && |
| 3714 | !sample_casts[smp.data.type][conv->in_type](&smp)) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3715 | hlua_pusherror(L, "error during the input argument casting"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3716 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3717 | } |
| 3718 | |
| 3719 | /* Run the sample conversion process. */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 3720 | if (!conv->process(args, &smp, conv->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3721 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3722 | lua_pushstring(L, ""); |
| 3723 | else |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 3724 | lua_pushnil(L); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3725 | goto end; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3726 | } |
| 3727 | |
| 3728 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3729 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3730 | hlua_smp2lua_str(L, &smp); |
| 3731 | else |
| 3732 | hlua_smp2lua(L, &smp); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3733 | end: |
| 3734 | for (i = 0; args[i].type != ARGT_STOP; i++) { |
| 3735 | if (args[i].type == ARGT_STR) |
| 3736 | chunk_destroy(&args[i].data.str); |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 3737 | else if (args[i].type == ARGT_REG) |
| 3738 | regex_free(args[i].data.reg); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3739 | } |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 3740 | return 1; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3741 | |
| 3742 | error: |
| 3743 | for (i = 0; args[i].type != ARGT_STOP; i++) { |
| 3744 | if (args[i].type == ARGT_STR) |
| 3745 | chunk_destroy(&args[i].data.str); |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 3746 | else if (args[i].type == ARGT_REG) |
| 3747 | regex_free(args[i].data.reg); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3748 | } |
| 3749 | WILL_LJMP(lua_error(L)); |
| 3750 | return 0; /* Never reached */ |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3751 | } |
| 3752 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3753 | /* |
| 3754 | * |
| 3755 | * |
| 3756 | * Class AppletTCP |
| 3757 | * |
| 3758 | * |
| 3759 | */ |
| 3760 | |
| 3761 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 3762 | * a class stream, otherwise it throws an error. |
| 3763 | */ |
| 3764 | __LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud) |
| 3765 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3766 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3767 | } |
| 3768 | |
| 3769 | /* This function creates and push in the stack an Applet object |
| 3770 | * according with a current TXN. |
| 3771 | */ |
| 3772 | static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx) |
| 3773 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3774 | struct hlua_appctx *luactx; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3775 | struct stream_interface *si = ctx->owner; |
| 3776 | struct stream *s = si_strm(si); |
| 3777 | struct proxy *p = s->be; |
| 3778 | |
| 3779 | /* Check stack size. */ |
| 3780 | if (!lua_checkstack(L, 3)) |
| 3781 | return 0; |
| 3782 | |
| 3783 | /* Create the object: obj[0] = userdata. |
| 3784 | * Note that the base of the Converters object is the |
| 3785 | * same than the TXN object. |
| 3786 | */ |
| 3787 | lua_newtable(L); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3788 | luactx = lua_newuserdata(L, sizeof(*luactx)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3789 | lua_rawseti(L, -2, 0); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3790 | luactx->appctx = ctx; |
| 3791 | luactx->htxn.s = s; |
| 3792 | luactx->htxn.p = p; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3793 | |
| 3794 | /* Create the "f" field that contains a list of fetches. */ |
| 3795 | lua_pushstring(L, "f"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3796 | if (!hlua_fetches_new(L, &luactx->htxn, 0)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3797 | return 0; |
| 3798 | lua_settable(L, -3); |
| 3799 | |
| 3800 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 3801 | lua_pushstring(L, "sf"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3802 | if (!hlua_fetches_new(L, &luactx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3803 | return 0; |
| 3804 | lua_settable(L, -3); |
| 3805 | |
| 3806 | /* Create the "c" field that contains a list of converters. */ |
| 3807 | lua_pushstring(L, "c"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3808 | if (!hlua_converters_new(L, &luactx->htxn, 0)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3809 | return 0; |
| 3810 | lua_settable(L, -3); |
| 3811 | |
| 3812 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 3813 | lua_pushstring(L, "sc"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3814 | if (!hlua_converters_new(L, &luactx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3815 | return 0; |
| 3816 | lua_settable(L, -3); |
| 3817 | |
| 3818 | /* Pop a class stream metatable and affect it to the table. */ |
| 3819 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref); |
| 3820 | lua_setmetatable(L, -2); |
| 3821 | |
| 3822 | return 1; |
| 3823 | } |
| 3824 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3825 | __LJMP static int hlua_applet_tcp_set_var(lua_State *L) |
| 3826 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3827 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3828 | struct stream *s; |
| 3829 | const char *name; |
| 3830 | size_t len; |
| 3831 | struct sample smp; |
| 3832 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 3833 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 3834 | 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] | 3835 | |
| 3836 | /* It is useles to retrieve the stream, but this function |
| 3837 | * runs only in a stream context. |
| 3838 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3839 | luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3840 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3841 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3842 | |
| 3843 | /* Converts the third argument in a sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 3844 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3845 | hlua_lua2smp(L, 3, &smp); |
| 3846 | |
Aurelien DARRAGON | d39fb74 | 2023-05-17 16:06:11 +0200 | [diff] [blame] | 3847 | /* Store the sample in a variable. We don't need to dup the smp, vars API |
| 3848 | * already takes care of duplicating dynamic var data. |
| 3849 | */ |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3850 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 3851 | |
| 3852 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 3853 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 3854 | else |
| 3855 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 3856 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 3857 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3858 | } |
| 3859 | |
| 3860 | __LJMP static int hlua_applet_tcp_unset_var(lua_State *L) |
| 3861 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3862 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3863 | struct stream *s; |
| 3864 | const char *name; |
| 3865 | size_t len; |
| 3866 | struct sample smp; |
| 3867 | |
| 3868 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 3869 | |
| 3870 | /* It is useles to retrieve the stream, but this function |
| 3871 | * runs only in a stream context. |
| 3872 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3873 | luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3874 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3875 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3876 | |
| 3877 | /* Unset the variable. */ |
| 3878 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 3879 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 3880 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3881 | } |
| 3882 | |
| 3883 | __LJMP static int hlua_applet_tcp_get_var(lua_State *L) |
| 3884 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3885 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3886 | struct stream *s; |
| 3887 | const char *name; |
| 3888 | size_t len; |
| 3889 | struct sample smp; |
| 3890 | |
| 3891 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 3892 | |
| 3893 | /* It is useles to retrieve the stream, but this function |
| 3894 | * runs only in a stream context. |
| 3895 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3896 | luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3897 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3898 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3899 | |
| 3900 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 3901 | if (!vars_get_by_name(name, len, &smp)) { |
| 3902 | lua_pushnil(L); |
| 3903 | return 1; |
| 3904 | } |
| 3905 | |
| 3906 | return hlua_smp2lua(L, &smp); |
| 3907 | } |
| 3908 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3909 | __LJMP static int hlua_applet_tcp_set_priv(lua_State *L) |
| 3910 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3911 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3912 | struct stream *s = luactx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 3913 | struct hlua *hlua; |
| 3914 | |
| 3915 | /* Note that this hlua struct is from the session and not from the applet. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 3916 | if (!s->hlua) |
| 3917 | return 0; |
| 3918 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3919 | |
| 3920 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 3921 | |
| 3922 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 3923 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3924 | |
| 3925 | /* Get and store new value. */ |
| 3926 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 3927 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 3928 | |
| 3929 | return 0; |
| 3930 | } |
| 3931 | |
| 3932 | __LJMP static int hlua_applet_tcp_get_priv(lua_State *L) |
| 3933 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3934 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3935 | struct stream *s = luactx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 3936 | struct hlua *hlua; |
| 3937 | |
| 3938 | /* Note that this hlua struct is from the session and not from the applet. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 3939 | if (!s->hlua) { |
| 3940 | lua_pushnil(L); |
| 3941 | return 1; |
| 3942 | } |
| 3943 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3944 | |
| 3945 | /* Push configuration index in the stack. */ |
| 3946 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 3947 | |
| 3948 | return 1; |
| 3949 | } |
| 3950 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3951 | /* If expected data not yet available, it returns a yield. This function |
| 3952 | * consumes the data in the buffer. It returns a string containing the |
| 3953 | * data. This string can be empty. |
| 3954 | */ |
| 3955 | __LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx) |
| 3956 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3957 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3958 | struct stream_interface *si = luactx->appctx->owner; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3959 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3960 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3961 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3962 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3963 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3964 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3965 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3966 | ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3967 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3968 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3969 | if (ret == 0) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3970 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3971 | 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] | 3972 | } |
| 3973 | |
| 3974 | /* End of data: commit the total strings and return. */ |
| 3975 | if (ret < 0) { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3976 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3977 | return 1; |
| 3978 | } |
| 3979 | |
| 3980 | /* Ensure that the block 2 length is usable. */ |
| 3981 | if (ret == 1) |
| 3982 | len2 = 0; |
| 3983 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 3984 | /* don't check the max length read and don't check. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3985 | luaL_addlstring(&luactx->b, blk1, len1); |
| 3986 | luaL_addlstring(&luactx->b, blk2, len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3987 | |
| 3988 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3989 | co_skip(si_oc(si), len1 + len2); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3990 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3991 | return 1; |
| 3992 | } |
| 3993 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3994 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3995 | __LJMP static int hlua_applet_tcp_getline(lua_State *L) |
| 3996 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3997 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3998 | |
| 3999 | /* Initialise the string catenation. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4000 | luaL_buffinit(L, &luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4001 | |
| 4002 | return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0)); |
| 4003 | } |
| 4004 | |
| 4005 | /* If expected data not yet available, it returns a yield. This function |
| 4006 | * consumes the data in the buffer. It returns a string containing the |
| 4007 | * data. This string can be empty. |
| 4008 | */ |
| 4009 | __LJMP static int hlua_applet_tcp_recv_yield(lua_State *L, int status, lua_KContext ctx) |
| 4010 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4011 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 4012 | struct stream_interface *si = luactx->appctx->owner; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4013 | size_t len = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4014 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4015 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4016 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4017 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4018 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4019 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4020 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4021 | ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4022 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4023 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4024 | if (ret == 0) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 4025 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4026 | 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] | 4027 | } |
| 4028 | |
| 4029 | /* End of data: commit the total strings and return. */ |
| 4030 | if (ret < 0) { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4031 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4032 | return 1; |
| 4033 | } |
| 4034 | |
| 4035 | /* Ensure that the block 2 length is usable. */ |
| 4036 | if (ret == 1) |
| 4037 | len2 = 0; |
| 4038 | |
| 4039 | if (len == -1) { |
| 4040 | |
| 4041 | /* If len == -1, catenate all the data avalaile and |
| 4042 | * yield because we want to get all the data until |
| 4043 | * the end of data stream. |
| 4044 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4045 | luaL_addlstring(&luactx->b, blk1, len1); |
| 4046 | luaL_addlstring(&luactx->b, blk2, len2); |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4047 | co_skip(si_oc(si), len1 + len2); |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 4048 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4049 | 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] | 4050 | |
| 4051 | } else { |
| 4052 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4053 | /* Copy the first block caping to the length required. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4054 | if (len1 > len) |
| 4055 | len1 = len; |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4056 | luaL_addlstring(&luactx->b, blk1, len1); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4057 | len -= len1; |
| 4058 | |
| 4059 | /* Copy the second block. */ |
| 4060 | if (len2 > len) |
| 4061 | len2 = len; |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4062 | luaL_addlstring(&luactx->b, blk2, len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4063 | len -= len2; |
| 4064 | |
| 4065 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4066 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4067 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4068 | /* If there is no other data available, yield waiting for new data. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4069 | if (len > 0) { |
| 4070 | lua_pushinteger(L, len); |
| 4071 | lua_replace(L, 2); |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 4072 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4073 | 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] | 4074 | } |
| 4075 | |
| 4076 | /* return the result. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4077 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4078 | return 1; |
| 4079 | } |
| 4080 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4081 | /* we never execute this */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4082 | hlua_pusherror(L, "Lua: internal error"); |
| 4083 | WILL_LJMP(lua_error(L)); |
| 4084 | return 0; |
| 4085 | } |
| 4086 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4087 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4088 | __LJMP static int hlua_applet_tcp_recv(lua_State *L) |
| 4089 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4090 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4091 | int len = -1; |
| 4092 | |
| 4093 | if (lua_gettop(L) > 2) |
| 4094 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 4095 | if (lua_gettop(L) >= 2) { |
| 4096 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4097 | lua_pop(L, 1); |
| 4098 | } |
| 4099 | |
| 4100 | /* Confirm or set the required length */ |
| 4101 | lua_pushinteger(L, len); |
| 4102 | |
| 4103 | /* Initialise the string catenation. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4104 | luaL_buffinit(L, &luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4105 | |
| 4106 | return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0)); |
| 4107 | } |
| 4108 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4109 | /* Append data in the output side of the buffer. This data is immediately |
| 4110 | * sent. The function returns the amount of data written. If the buffer |
| 4111 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4112 | * if the channel is closed. |
| 4113 | */ |
| 4114 | __LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx) |
| 4115 | { |
| 4116 | size_t len; |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4117 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4118 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4119 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4120 | struct stream_interface *si = luactx->appctx->owner; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4121 | struct channel *chn = si_ic(si); |
| 4122 | int max; |
| 4123 | |
| 4124 | /* Get the max amount of data which can write as input in the channel. */ |
| 4125 | max = channel_recv_max(chn); |
| 4126 | if (max > (len - l)) |
| 4127 | max = len - l; |
| 4128 | |
| 4129 | /* Copy data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4130 | ci_putblk(chn, str + l, max); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4131 | |
| 4132 | /* update counters. */ |
| 4133 | l += max; |
| 4134 | lua_pop(L, 1); |
| 4135 | lua_pushinteger(L, l); |
| 4136 | |
| 4137 | /* If some data is not send, declares the situation to the |
| 4138 | * applet, and returns a yield. |
| 4139 | */ |
| 4140 | if (l < len) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 4141 | si_rx_room_blk(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4142 | 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] | 4143 | } |
| 4144 | |
| 4145 | return 1; |
| 4146 | } |
| 4147 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4148 | /* Just a wrapper of "hlua_applet_tcp_send_yield". This wrapper permits |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4149 | * yield the LUA process, and resume it without checking the |
| 4150 | * input arguments. |
| 4151 | */ |
| 4152 | __LJMP static int hlua_applet_tcp_send(lua_State *L) |
| 4153 | { |
| 4154 | MAY_LJMP(check_args(L, 2, "send")); |
| 4155 | lua_pushinteger(L, 0); |
| 4156 | |
| 4157 | return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0)); |
| 4158 | } |
| 4159 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4160 | /* |
| 4161 | * |
| 4162 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4163 | * Class AppletHTTP |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4164 | * |
| 4165 | * |
| 4166 | */ |
| 4167 | |
| 4168 | /* Returns a struct hlua_txn if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4169 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4170 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4171 | __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] | 4172 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4173 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_http_ref)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4174 | } |
| 4175 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4176 | /* This function creates and push in the stack an Applet object |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4177 | * according with a current TXN. |
| 4178 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4179 | static int hlua_applet_http_new(lua_State *L, struct appctx *ctx) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4180 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4181 | struct hlua_appctx *luactx; |
Thierry FOURNIER | 841475e | 2015-12-11 17:10:09 +0100 | [diff] [blame] | 4182 | struct hlua_txn htxn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4183 | struct stream_interface *si = ctx->owner; |
| 4184 | struct stream *s = si_strm(si); |
| 4185 | struct proxy *px = s->be; |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4186 | struct htx *htx; |
| 4187 | struct htx_blk *blk; |
| 4188 | struct htx_sl *sl; |
| 4189 | struct ist path; |
| 4190 | unsigned long long len = 0; |
| 4191 | int32_t pos; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4192 | |
| 4193 | /* Check stack size. */ |
| 4194 | if (!lua_checkstack(L, 3)) |
| 4195 | return 0; |
| 4196 | |
| 4197 | /* Create the object: obj[0] = userdata. |
| 4198 | * Note that the base of the Converters object is the |
| 4199 | * same than the TXN object. |
| 4200 | */ |
| 4201 | lua_newtable(L); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4202 | luactx = lua_newuserdata(L, sizeof(*luactx)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4203 | lua_rawseti(L, -2, 0); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4204 | luactx->appctx = ctx; |
| 4205 | luactx->appctx->ctx.hlua_apphttp.status = 200; /* Default status code returned. */ |
| 4206 | luactx->appctx->ctx.hlua_apphttp.reason = NULL; /* Use default reason based on status */ |
| 4207 | luactx->htxn.s = s; |
| 4208 | luactx->htxn.p = px; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4209 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4210 | /* Create the "f" field that contains a list of fetches. */ |
| 4211 | lua_pushstring(L, "f"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4212 | if (!hlua_fetches_new(L, &luactx->htxn, 0)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4213 | return 0; |
| 4214 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4215 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4216 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 4217 | lua_pushstring(L, "sf"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4218 | if (!hlua_fetches_new(L, &luactx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4219 | return 0; |
| 4220 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4221 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4222 | /* Create the "c" field that contains a list of converters. */ |
| 4223 | lua_pushstring(L, "c"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4224 | if (!hlua_converters_new(L, &luactx->htxn, 0)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4225 | return 0; |
| 4226 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4227 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4228 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 4229 | lua_pushstring(L, "sc"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4230 | if (!hlua_converters_new(L, &luactx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4231 | return 0; |
| 4232 | lua_settable(L, -3); |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4233 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4234 | htx = htxbuf(&s->req.buf); |
| 4235 | blk = htx_get_first_blk(htx); |
Christopher Faulet | ea00973 | 2019-11-18 15:50:25 +0100 | [diff] [blame] | 4236 | BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4237 | sl = htx_get_blk_ptr(htx, blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4238 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4239 | /* Stores the request method. */ |
| 4240 | lua_pushstring(L, "method"); |
| 4241 | lua_pushlstring(L, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); |
| 4242 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4243 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4244 | /* Stores the http version. */ |
| 4245 | lua_pushstring(L, "version"); |
| 4246 | lua_pushlstring(L, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); |
| 4247 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4248 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4249 | /* creates an array of headers. hlua_http_get_headers() crates and push |
| 4250 | * the array on the top of the stack. |
| 4251 | */ |
| 4252 | lua_pushstring(L, "headers"); |
| 4253 | htxn.s = s; |
| 4254 | htxn.p = px; |
| 4255 | htxn.dir = SMP_OPT_DIR_REQ; |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 4256 | if (!hlua_http_get_headers(L, &htxn.s->txn->req)) |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4257 | return 0; |
| 4258 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4259 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4260 | path = http_get_path(htx_sl_req_uri(sl)); |
Tim Duesterhus | ed52637 | 2020-03-05 17:56:33 +0100 | [diff] [blame] | 4261 | if (isttest(path)) { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4262 | char *p, *q, *end; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4263 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4264 | p = path.ptr; |
| 4265 | end = path.ptr + path.len; |
| 4266 | q = p; |
| 4267 | while (q < end && *q != '?') |
| 4268 | q++; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4269 | |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 4270 | /* Stores the request path. */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4271 | lua_pushstring(L, "path"); |
| 4272 | lua_pushlstring(L, p, q - p); |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 4273 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4274 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4275 | /* Stores the query string. */ |
| 4276 | lua_pushstring(L, "qs"); |
| 4277 | if (*q == '?') |
| 4278 | q++; |
| 4279 | lua_pushlstring(L, q, end - q); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4280 | lua_settable(L, -3); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4281 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4282 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4283 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 4284 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 4285 | enum htx_blk_type type = htx_get_blk_type(blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4286 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 4287 | if (type == HTX_BLK_TLR || type == HTX_BLK_EOT) |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4288 | break; |
| 4289 | if (type == HTX_BLK_DATA) |
| 4290 | len += htx_get_blksz(blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4291 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4292 | if (htx->extra != ULLONG_MAX) |
| 4293 | len += htx->extra; |
| 4294 | |
| 4295 | /* Stores the request path. */ |
| 4296 | lua_pushstring(L, "length"); |
| 4297 | lua_pushinteger(L, len); |
| 4298 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4299 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4300 | /* Create an empty array of HTTP request headers. */ |
| 4301 | lua_pushstring(L, "response"); |
| 4302 | lua_newtable(L); |
| 4303 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4304 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4305 | /* Pop a class stream metatable and affect it to the table. */ |
| 4306 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref); |
| 4307 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4308 | |
| 4309 | return 1; |
| 4310 | } |
| 4311 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4312 | __LJMP static int hlua_applet_http_set_var(lua_State *L) |
| 4313 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4314 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4315 | struct stream *s; |
| 4316 | const char *name; |
| 4317 | size_t len; |
| 4318 | struct sample smp; |
| 4319 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 4320 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 4321 | 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] | 4322 | |
| 4323 | /* It is useles to retrieve the stream, but this function |
| 4324 | * runs only in a stream context. |
| 4325 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4326 | luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4327 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4328 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4329 | |
| 4330 | /* Converts the third argument in a sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 4331 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4332 | hlua_lua2smp(L, 3, &smp); |
| 4333 | |
Aurelien DARRAGON | d39fb74 | 2023-05-17 16:06:11 +0200 | [diff] [blame] | 4334 | /* Store the sample in a variable. We don't need to dup the smp, vars API |
| 4335 | * already takes care of duplicating dynamic var data. |
| 4336 | */ |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4337 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 4338 | |
| 4339 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 4340 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 4341 | else |
| 4342 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 4343 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 4344 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4345 | } |
| 4346 | |
| 4347 | __LJMP static int hlua_applet_http_unset_var(lua_State *L) |
| 4348 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4349 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4350 | struct stream *s; |
| 4351 | const char *name; |
| 4352 | size_t len; |
| 4353 | struct sample smp; |
| 4354 | |
| 4355 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 4356 | |
| 4357 | /* It is useles to retrieve the stream, but this function |
| 4358 | * runs only in a stream context. |
| 4359 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4360 | luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4361 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4362 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4363 | |
| 4364 | /* Unset the variable. */ |
| 4365 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 4366 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 4367 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4368 | } |
| 4369 | |
| 4370 | __LJMP static int hlua_applet_http_get_var(lua_State *L) |
| 4371 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4372 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4373 | struct stream *s; |
| 4374 | const char *name; |
| 4375 | size_t len; |
| 4376 | struct sample smp; |
| 4377 | |
| 4378 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 4379 | |
| 4380 | /* It is useles to retrieve the stream, but this function |
| 4381 | * runs only in a stream context. |
| 4382 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4383 | luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4384 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4385 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4386 | |
| 4387 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 4388 | if (!vars_get_by_name(name, len, &smp)) { |
| 4389 | lua_pushnil(L); |
| 4390 | return 1; |
| 4391 | } |
| 4392 | |
| 4393 | return hlua_smp2lua(L, &smp); |
| 4394 | } |
| 4395 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4396 | __LJMP static int hlua_applet_http_set_priv(lua_State *L) |
| 4397 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4398 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4399 | struct stream *s = luactx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4400 | struct hlua *hlua; |
| 4401 | |
| 4402 | /* Note that this hlua struct is from the session and not from the applet. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 4403 | if (!s->hlua) |
| 4404 | return 0; |
| 4405 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4406 | |
| 4407 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 4408 | |
| 4409 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 4410 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4411 | |
| 4412 | /* Get and store new value. */ |
| 4413 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 4414 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 4415 | |
| 4416 | return 0; |
| 4417 | } |
| 4418 | |
| 4419 | __LJMP static int hlua_applet_http_get_priv(lua_State *L) |
| 4420 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4421 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4422 | struct stream *s = luactx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4423 | struct hlua *hlua; |
| 4424 | |
| 4425 | /* Note that this hlua struct is from the session and not from the applet. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 4426 | if (!s->hlua) { |
| 4427 | lua_pushnil(L); |
| 4428 | return 1; |
| 4429 | } |
| 4430 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4431 | |
| 4432 | /* Push configuration index in the stack. */ |
| 4433 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 4434 | |
| 4435 | return 1; |
| 4436 | } |
| 4437 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4438 | /* If expected data not yet available, it returns a yield. This function |
| 4439 | * consumes the data in the buffer. It returns a string containing the |
| 4440 | * data. This string can be empty. |
| 4441 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4442 | __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] | 4443 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4444 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4445 | struct stream_interface *si = luactx->appctx->owner; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4446 | struct channel *req = si_oc(si); |
| 4447 | struct htx *htx; |
| 4448 | struct htx_blk *blk; |
| 4449 | size_t count; |
| 4450 | int stop = 0; |
| 4451 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4452 | htx = htx_from_buf(&req->buf); |
| 4453 | count = co_data(req); |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 4454 | blk = htx_get_first_blk(htx); |
Christopher Faulet | cc26b13 | 2018-12-18 21:20:57 +0100 | [diff] [blame] | 4455 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4456 | while (count && !stop && blk) { |
| 4457 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4458 | uint32_t sz = htx_get_blksz(blk); |
| 4459 | struct ist v; |
| 4460 | uint32_t vlen; |
| 4461 | char *nl; |
| 4462 | |
| 4463 | vlen = sz; |
| 4464 | if (vlen > count) { |
| 4465 | if (type != HTX_BLK_DATA) |
| 4466 | break; |
| 4467 | vlen = count; |
| 4468 | } |
| 4469 | |
| 4470 | switch (type) { |
| 4471 | case HTX_BLK_UNUSED: |
| 4472 | break; |
| 4473 | |
| 4474 | case HTX_BLK_DATA: |
| 4475 | v = htx_get_blk_value(htx, blk); |
| 4476 | v.len = vlen; |
| 4477 | nl = istchr(v, '\n'); |
| 4478 | if (nl != NULL) { |
| 4479 | stop = 1; |
| 4480 | vlen = nl - v.ptr + 1; |
| 4481 | } |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4482 | luaL_addlstring(&luactx->b, v.ptr, vlen); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4483 | break; |
| 4484 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4485 | case HTX_BLK_TLR: |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 4486 | case HTX_BLK_EOT: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4487 | stop = 1; |
| 4488 | break; |
| 4489 | |
| 4490 | default: |
| 4491 | break; |
| 4492 | } |
| 4493 | |
| 4494 | co_set_data(req, co_data(req) - vlen); |
| 4495 | count -= vlen; |
| 4496 | if (sz == vlen) |
| 4497 | blk = htx_remove_blk(htx, blk); |
| 4498 | else { |
| 4499 | htx_cut_data_blk(htx, blk, vlen); |
| 4500 | break; |
| 4501 | } |
| 4502 | } |
| 4503 | |
Christopher Faulet | eccb31c | 2021-04-02 14:24:56 +0200 | [diff] [blame] | 4504 | /* The message was fully consumed and no more data are expected |
| 4505 | * (EOM flag set). |
| 4506 | */ |
Christopher Faulet | a5a25b1 | 2022-08-29 15:37:16 +0200 | [diff] [blame] | 4507 | if (htx_is_empty(htx) && (req->flags & CF_EOI)) |
Christopher Faulet | eccb31c | 2021-04-02 14:24:56 +0200 | [diff] [blame] | 4508 | stop = 1; |
| 4509 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4510 | htx_to_buf(htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4511 | if (!stop) { |
| 4512 | si_cant_get(si); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4513 | 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] | 4514 | } |
| 4515 | |
| 4516 | /* return the result. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4517 | luaL_pushresult(&luactx->b); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4518 | return 1; |
| 4519 | } |
| 4520 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4521 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4522 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4523 | __LJMP static int hlua_applet_http_getline(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4524 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4525 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4526 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4527 | /* Initialise the string catenation. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4528 | luaL_buffinit(L, &luactx->b); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4529 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4530 | return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4531 | } |
| 4532 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4533 | /* If expected data not yet available, it returns a yield. This function |
| 4534 | * consumes the data in the buffer. It returns a string containing the |
| 4535 | * data. This string can be empty. |
| 4536 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4537 | __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] | 4538 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4539 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4540 | struct stream_interface *si = luactx->appctx->owner; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4541 | struct channel *req = si_oc(si); |
| 4542 | struct htx *htx; |
| 4543 | struct htx_blk *blk; |
| 4544 | size_t count; |
| 4545 | int len; |
| 4546 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4547 | htx = htx_from_buf(&req->buf); |
| 4548 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4549 | count = co_data(req); |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 4550 | blk = htx_get_head_blk(htx); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4551 | while (count && len && blk) { |
| 4552 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4553 | uint32_t sz = htx_get_blksz(blk); |
| 4554 | struct ist v; |
| 4555 | uint32_t vlen; |
| 4556 | |
| 4557 | vlen = sz; |
| 4558 | if (len > 0 && vlen > len) |
| 4559 | vlen = len; |
| 4560 | if (vlen > count) { |
| 4561 | if (type != HTX_BLK_DATA) |
| 4562 | break; |
| 4563 | vlen = count; |
| 4564 | } |
| 4565 | |
| 4566 | switch (type) { |
| 4567 | case HTX_BLK_UNUSED: |
| 4568 | break; |
| 4569 | |
| 4570 | case HTX_BLK_DATA: |
| 4571 | v = htx_get_blk_value(htx, blk); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4572 | luaL_addlstring(&luactx->b, v.ptr, vlen); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4573 | break; |
| 4574 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4575 | case HTX_BLK_TLR: |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 4576 | case HTX_BLK_EOT: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4577 | len = 0; |
| 4578 | break; |
| 4579 | |
| 4580 | default: |
| 4581 | break; |
| 4582 | } |
| 4583 | |
| 4584 | co_set_data(req, co_data(req) - vlen); |
| 4585 | count -= vlen; |
| 4586 | if (len > 0) |
| 4587 | len -= vlen; |
| 4588 | if (sz == vlen) |
| 4589 | blk = htx_remove_blk(htx, blk); |
| 4590 | else { |
| 4591 | htx_cut_data_blk(htx, blk, vlen); |
| 4592 | break; |
| 4593 | } |
| 4594 | } |
| 4595 | |
Christopher Faulet | eccb31c | 2021-04-02 14:24:56 +0200 | [diff] [blame] | 4596 | /* The message was fully consumed and no more data are expected |
| 4597 | * (EOM flag set). |
| 4598 | */ |
Christopher Faulet | a5a25b1 | 2022-08-29 15:37:16 +0200 | [diff] [blame] | 4599 | if (htx_is_empty(htx) && (req->flags & CF_EOI)) |
Christopher Faulet | eccb31c | 2021-04-02 14:24:56 +0200 | [diff] [blame] | 4600 | len = 0; |
| 4601 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4602 | htx_to_buf(htx, &req->buf); |
| 4603 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4604 | /* If we are no other data available, yield waiting for new data. */ |
| 4605 | if (len) { |
| 4606 | if (len > 0) { |
| 4607 | lua_pushinteger(L, len); |
| 4608 | lua_replace(L, 2); |
| 4609 | } |
| 4610 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4611 | 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] | 4612 | } |
| 4613 | |
| 4614 | /* return the result. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4615 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4616 | return 1; |
| 4617 | } |
| 4618 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4619 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4620 | __LJMP static int hlua_applet_http_recv(lua_State *L) |
| 4621 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4622 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4623 | int len = -1; |
| 4624 | |
| 4625 | /* Check arguments. */ |
| 4626 | if (lua_gettop(L) > 2) |
| 4627 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 4628 | if (lua_gettop(L) >= 2) { |
| 4629 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4630 | lua_pop(L, 1); |
| 4631 | } |
| 4632 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4633 | lua_pushinteger(L, len); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4634 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4635 | /* Initialise the string catenation. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4636 | luaL_buffinit(L, &luactx->b); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4637 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4638 | return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4639 | } |
| 4640 | |
| 4641 | /* Append data in the output side of the buffer. This data is immediately |
| 4642 | * sent. The function returns the amount of data written. If the buffer |
| 4643 | * cannot contain the data, the function yields. The function returns -1 |
| 4644 | * if the channel is closed. |
| 4645 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4646 | __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] | 4647 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4648 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4649 | struct stream_interface *si = luactx->appctx->owner; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4650 | struct channel *res = si_ic(si); |
| 4651 | struct htx *htx = htx_from_buf(&res->buf); |
| 4652 | const char *data; |
| 4653 | size_t len; |
| 4654 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 4655 | int max; |
| 4656 | |
Christopher Faulet | 9060fc0 | 2019-07-03 11:39:30 +0200 | [diff] [blame] | 4657 | max = htx_get_max_blksz(htx, channel_htx_recv_max(res, htx)); |
Christopher Faulet | 4b0e9b2 | 2019-01-09 12:16:58 +0100 | [diff] [blame] | 4658 | if (!max) |
| 4659 | goto snd_yield; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4660 | |
| 4661 | data = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4662 | |
| 4663 | /* Get the max amount of data which can write as input in the channel. */ |
| 4664 | if (max > (len - l)) |
| 4665 | max = len - l; |
| 4666 | |
| 4667 | /* Copy data. */ |
Willy Tarreau | 0a7ef02 | 2019-05-28 10:30:11 +0200 | [diff] [blame] | 4668 | max = htx_add_data(htx, ist2(data + l, max)); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 4669 | channel_add_input(res, max); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4670 | |
| 4671 | /* update counters. */ |
| 4672 | l += max; |
| 4673 | lua_pop(L, 1); |
| 4674 | lua_pushinteger(L, l); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4675 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4676 | /* If some data is not send, declares the situation to the |
| 4677 | * applet, and returns a yield. |
| 4678 | */ |
| 4679 | if (l < len) { |
Christopher Faulet | 4b0e9b2 | 2019-01-09 12:16:58 +0100 | [diff] [blame] | 4680 | snd_yield: |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4681 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4682 | si_rx_room_blk(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4683 | 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] | 4684 | } |
| 4685 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4686 | htx_to_buf(htx, &res->buf); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4687 | return 1; |
| 4688 | } |
| 4689 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4690 | /* Just a wrapper of "hlua_applet_send_yield". This wrapper permits |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4691 | * yield the LUA process, and resume it without checking the |
| 4692 | * input arguments. |
| 4693 | */ |
| 4694 | __LJMP static int hlua_applet_http_send(lua_State *L) |
| 4695 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4696 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4697 | |
| 4698 | /* We want to send some data. Headers must be sent. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4699 | if (!(luactx->appctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4700 | hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data."); |
| 4701 | WILL_LJMP(lua_error(L)); |
| 4702 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4703 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4704 | /* This integer is used for followinf the amount of data sent. */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4705 | lua_pushinteger(L, 0); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4706 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4707 | return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4708 | } |
| 4709 | |
| 4710 | __LJMP static int hlua_applet_http_addheader(lua_State *L) |
| 4711 | { |
| 4712 | const char *name; |
| 4713 | int ret; |
| 4714 | |
| 4715 | MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4716 | name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 4717 | MAY_LJMP(luaL_checkstring(L, 3)); |
| 4718 | |
| 4719 | /* Push in the stack the "response" entry. */ |
| 4720 | ret = lua_getfield(L, 1, "response"); |
| 4721 | if (ret != LUA_TTABLE) { |
| 4722 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] " |
| 4723 | "is expected as an array. %s found", lua_typename(L, ret)); |
| 4724 | WILL_LJMP(lua_error(L)); |
| 4725 | } |
| 4726 | |
| 4727 | /* check if the header is already registered if it is not |
| 4728 | * the case, register it. |
| 4729 | */ |
| 4730 | ret = lua_getfield(L, -1, name); |
| 4731 | if (ret == LUA_TNIL) { |
| 4732 | |
| 4733 | /* Entry not found. */ |
| 4734 | lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */ |
| 4735 | |
| 4736 | /* Insert the new header name in the array in the top of the stack. |
| 4737 | * It left the new array in the top of the stack. |
| 4738 | */ |
| 4739 | lua_newtable(L); |
| 4740 | lua_pushvalue(L, 2); |
| 4741 | lua_pushvalue(L, -2); |
| 4742 | lua_settable(L, -4); |
| 4743 | |
| 4744 | } else if (ret != LUA_TTABLE) { |
| 4745 | |
| 4746 | /* corruption error. */ |
| 4747 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] " |
| 4748 | "is expected as an array. %s found", name, lua_typename(L, ret)); |
| 4749 | WILL_LJMP(lua_error(L)); |
| 4750 | } |
| 4751 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 4752 | /* Now the top of thestack is an array of values. We push |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4753 | * the header value as new entry. |
| 4754 | */ |
| 4755 | lua_pushvalue(L, 3); |
| 4756 | ret = lua_rawlen(L, -2); |
| 4757 | lua_rawseti(L, -2, ret + 1); |
| 4758 | lua_pushboolean(L, 1); |
| 4759 | return 1; |
| 4760 | } |
| 4761 | |
| 4762 | __LJMP static int hlua_applet_http_status(lua_State *L) |
| 4763 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4764 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4765 | int status = MAY_LJMP(luaL_checkinteger(L, 2)); |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4766 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4767 | |
| 4768 | if (status < 100 || status > 599) { |
| 4769 | lua_pushboolean(L, 0); |
| 4770 | return 1; |
| 4771 | } |
| 4772 | |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4773 | luactx->appctx->ctx.hlua_apphttp.status = status; |
| 4774 | luactx->appctx->ctx.hlua_apphttp.reason = reason; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4775 | lua_pushboolean(L, 1); |
| 4776 | return 1; |
| 4777 | } |
| 4778 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4779 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4780 | __LJMP static int hlua_applet_http_send_response(lua_State *L) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4781 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4782 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4783 | struct stream_interface *si = luactx->appctx->owner; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4784 | struct channel *res = si_ic(si); |
| 4785 | struct htx *htx; |
| 4786 | struct htx_sl *sl; |
| 4787 | struct h1m h1m; |
| 4788 | const char *status, *reason; |
| 4789 | const char *name, *value; |
| 4790 | size_t nlen, vlen; |
| 4791 | unsigned int flags; |
| 4792 | |
| 4793 | /* Send the message at once. */ |
| 4794 | htx = htx_from_buf(&res->buf); |
| 4795 | h1m_init_res(&h1m); |
| 4796 | |
| 4797 | /* Use the same http version than the request. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4798 | status = ultoa_r(luactx->appctx->ctx.hlua_apphttp.status, trash.area, trash.size); |
| 4799 | reason = luactx->appctx->ctx.hlua_apphttp.reason; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4800 | if (reason == NULL) |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4801 | reason = http_get_reason(luactx->appctx->ctx.hlua_apphttp.status); |
| 4802 | if (luactx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4803 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11); |
| 4804 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist(status), ist(reason)); |
| 4805 | } |
| 4806 | else { |
| 4807 | flags = HTX_SL_F_IS_RESP; |
| 4808 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), ist(status), ist(reason)); |
| 4809 | } |
| 4810 | if (!sl) { |
| 4811 | hlua_pusherror(L, "Lua applet http '%s': Failed to create response.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4812 | luactx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4813 | WILL_LJMP(lua_error(L)); |
| 4814 | } |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4815 | sl->info.res.status = luactx->appctx->ctx.hlua_apphttp.status; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4816 | |
| 4817 | /* Get the array associated to the field "response" in the object AppletHTTP. */ |
| 4818 | lua_pushvalue(L, 0); |
| 4819 | if (lua_getfield(L, 1, "response") != LUA_TTABLE) { |
| 4820 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4821 | luactx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4822 | WILL_LJMP(lua_error(L)); |
| 4823 | } |
| 4824 | |
| 4825 | /* Browse the list of headers. */ |
| 4826 | lua_pushnil(L); |
| 4827 | while(lua_next(L, -2) != 0) { |
| 4828 | /* We expect a string as -2. */ |
| 4829 | if (lua_type(L, -2) != LUA_TSTRING) { |
| 4830 | 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] | 4831 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4832 | lua_typename(L, lua_type(L, -2))); |
| 4833 | WILL_LJMP(lua_error(L)); |
| 4834 | } |
| 4835 | name = lua_tolstring(L, -2, &nlen); |
| 4836 | |
| 4837 | /* We expect an array as -1. */ |
| 4838 | if (lua_type(L, -1) != LUA_TTABLE) { |
| 4839 | 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] | 4840 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4841 | name, |
| 4842 | lua_typename(L, lua_type(L, -1))); |
| 4843 | WILL_LJMP(lua_error(L)); |
| 4844 | } |
| 4845 | |
| 4846 | /* Browse the table who is on the top of the stack. */ |
| 4847 | lua_pushnil(L); |
| 4848 | while(lua_next(L, -2) != 0) { |
| 4849 | int id; |
| 4850 | |
| 4851 | /* We expect a number as -2. */ |
| 4852 | if (lua_type(L, -2) != LUA_TNUMBER) { |
| 4853 | 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] | 4854 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4855 | name, |
| 4856 | lua_typename(L, lua_type(L, -2))); |
| 4857 | WILL_LJMP(lua_error(L)); |
| 4858 | } |
| 4859 | id = lua_tointeger(L, -2); |
| 4860 | |
| 4861 | /* We expect a string as -2. */ |
| 4862 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 4863 | 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] | 4864 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4865 | name, id, |
| 4866 | lua_typename(L, lua_type(L, -1))); |
| 4867 | WILL_LJMP(lua_error(L)); |
| 4868 | } |
| 4869 | value = lua_tolstring(L, -1, &vlen); |
| 4870 | |
| 4871 | /* Simple Protocol checks. */ |
| 4872 | if (isteqi(ist2(name, nlen), ist("transfer-encoding"))) |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 4873 | h1_parse_xfer_enc_header(&h1m, ist2(value, vlen)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4874 | else if (isteqi(ist2(name, nlen), ist("content-length"))) { |
| 4875 | struct ist v = ist2(value, vlen); |
| 4876 | int ret; |
| 4877 | |
| 4878 | ret = h1_parse_cont_len_header(&h1m, &v); |
| 4879 | if (ret < 0) { |
| 4880 | hlua_pusherror(L, "Lua applet http '%s': Invalid '%s' header.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4881 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4882 | name); |
| 4883 | WILL_LJMP(lua_error(L)); |
| 4884 | } |
| 4885 | else if (ret == 0) |
| 4886 | goto next; /* Skip it */ |
| 4887 | } |
| 4888 | |
| 4889 | /* Add a new header */ |
| 4890 | if (!htx_add_header(htx, ist2(name, nlen), ist2(value, vlen))) { |
| 4891 | 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] | 4892 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4893 | name); |
| 4894 | WILL_LJMP(lua_error(L)); |
| 4895 | } |
| 4896 | next: |
| 4897 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 4898 | lua_pop(L, 1); |
| 4899 | } |
| 4900 | |
| 4901 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 4902 | lua_pop(L, 1); |
| 4903 | } |
| 4904 | |
| 4905 | if (h1m.flags & H1_MF_CHNK) |
| 4906 | h1m.flags &= ~H1_MF_CLEN; |
| 4907 | if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK)) |
| 4908 | h1m.flags |= H1_MF_XFER_LEN; |
| 4909 | |
| 4910 | /* Uset HTX start-line flags */ |
| 4911 | if (h1m.flags & H1_MF_XFER_ENC) |
| 4912 | flags |= HTX_SL_F_XFER_ENC; |
| 4913 | if (h1m.flags & H1_MF_XFER_LEN) { |
| 4914 | flags |= HTX_SL_F_XFER_LEN; |
| 4915 | if (h1m.flags & H1_MF_CHNK) |
| 4916 | flags |= HTX_SL_F_CHNK; |
| 4917 | else if (h1m.flags & H1_MF_CLEN) |
| 4918 | flags |= HTX_SL_F_CLEN; |
| 4919 | if (h1m.body_len == 0) |
| 4920 | flags |= HTX_SL_F_BODYLESS; |
| 4921 | } |
| 4922 | sl->flags |= flags; |
| 4923 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 4924 | /* 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] | 4925 | * and the status code implies the presence of a message body, we must |
| 4926 | * announce a transfer encoding chunked. This is required by haproxy |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4927 | * for the keepalive compliance. If the applet announces a transfer-encoding |
| 4928 | * chunked itself, don't do anything. |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4929 | */ |
| 4930 | 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] | 4931 | luactx->appctx->ctx.hlua_apphttp.status >= 200 && |
| 4932 | luactx->appctx->ctx.hlua_apphttp.status != 204 && |
| 4933 | luactx->appctx->ctx.hlua_apphttp.status != 304) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4934 | /* Add a new header */ |
| 4935 | sl->flags |= (HTX_SL_F_XFER_ENC|H1_MF_CHNK|H1_MF_XFER_LEN); |
| 4936 | if (!htx_add_header(htx, ist("transfer-encoding"), ist("chunked"))) { |
| 4937 | 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] | 4938 | luactx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4939 | WILL_LJMP(lua_error(L)); |
| 4940 | } |
| 4941 | } |
| 4942 | |
| 4943 | /* Finalize headers. */ |
| 4944 | if (!htx_add_endof(htx, HTX_BLK_EOH)) { |
| 4945 | hlua_pusherror(L, "Lua applet http '%s': Failed create the response.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4946 | luactx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4947 | WILL_LJMP(lua_error(L)); |
| 4948 | } |
| 4949 | |
| 4950 | if (htx_used_space(htx) > b_size(&res->buf) - global.tune.maxrewrite) { |
| 4951 | b_reset(&res->buf); |
| 4952 | hlua_pusherror(L, "Lua: 'start_response': response header block too big"); |
| 4953 | WILL_LJMP(lua_error(L)); |
| 4954 | } |
| 4955 | |
| 4956 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 4957 | channel_add_input(res, htx->data); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4958 | |
| 4959 | /* Headers sent, set the flag. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4960 | luactx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4961 | return 0; |
| 4962 | |
| 4963 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4964 | /* We will build the status line and the headers of the HTTP response. |
| 4965 | * We will try send at once if its not possible, we give back the hand |
| 4966 | * waiting for more room. |
| 4967 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4968 | __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] | 4969 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4970 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4971 | struct stream_interface *si = luactx->appctx->owner; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4972 | struct channel *res = si_ic(si); |
| 4973 | |
| 4974 | if (co_data(res)) { |
| 4975 | si_rx_room_blk(si); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4976 | 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] | 4977 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4978 | return MAY_LJMP(hlua_applet_http_send_response(L)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4979 | } |
| 4980 | |
| 4981 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4982 | __LJMP static int hlua_applet_http_start_response(lua_State *L) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4983 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4984 | return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4985 | } |
| 4986 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4987 | /* |
| 4988 | * |
| 4989 | * |
| 4990 | * Class HTTP |
| 4991 | * |
| 4992 | * |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4993 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4994 | |
| 4995 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 4996 | * a class stream, otherwise it throws an error. |
| 4997 | */ |
| 4998 | __LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4999 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5000 | return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref)); |
| 5001 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5002 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5003 | /* This function creates and push in the stack a HTTP object |
| 5004 | * according with a current TXN. |
| 5005 | */ |
| 5006 | static int hlua_http_new(lua_State *L, struct hlua_txn *txn) |
| 5007 | { |
| 5008 | struct hlua_txn *htxn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5009 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5010 | /* Check stack size. */ |
| 5011 | if (!lua_checkstack(L, 3)) |
| 5012 | return 0; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5013 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5014 | /* Create the object: obj[0] = userdata. |
| 5015 | * Note that the base of the Converters object is the |
| 5016 | * same than the TXN object. |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5017 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5018 | lua_newtable(L); |
| 5019 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
| 5020 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5021 | |
| 5022 | htxn->s = txn->s; |
| 5023 | htxn->p = txn->p; |
Christopher Faulet | 256b69a | 2019-05-23 11:14:21 +0200 | [diff] [blame] | 5024 | htxn->dir = txn->dir; |
| 5025 | htxn->flags = txn->flags; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5026 | |
| 5027 | /* Pop a class stream metatable and affect it to the table. */ |
| 5028 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref); |
| 5029 | lua_setmetatable(L, -2); |
| 5030 | |
| 5031 | return 1; |
| 5032 | } |
| 5033 | |
| 5034 | /* This function creates ans returns an array of HTTP headers. |
| 5035 | * This function does not fails. It is used as wrapper with the |
| 5036 | * 2 following functions. |
| 5037 | */ |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 5038 | __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] | 5039 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5040 | struct htx *htx; |
| 5041 | int32_t pos; |
| 5042 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5043 | /* Create the table. */ |
| 5044 | lua_newtable(L); |
| 5045 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5046 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5047 | htx = htxbuf(&msg->chn->buf); |
| 5048 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 5049 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 5050 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 5051 | struct ist n, v; |
| 5052 | int len; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5053 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5054 | if (type == HTX_BLK_HDR) { |
| 5055 | n = htx_get_blk_name(htx,blk); |
| 5056 | v = htx_get_blk_value(htx, blk); |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5057 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5058 | else if (type == HTX_BLK_EOH) |
| 5059 | break; |
| 5060 | else |
| 5061 | continue; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5062 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5063 | /* Check for existing entry: |
| 5064 | * assume that the table is on the top of the stack, and |
| 5065 | * push the key in the stack, the function lua_gettable() |
| 5066 | * perform the lookup. |
| 5067 | */ |
| 5068 | lua_pushlstring(L, n.ptr, n.len); |
| 5069 | lua_gettable(L, -2); |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5070 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5071 | switch (lua_type(L, -1)) { |
| 5072 | case LUA_TNIL: |
| 5073 | /* Table not found, create it. */ |
| 5074 | lua_pop(L, 1); /* remove the nil value. */ |
| 5075 | lua_pushlstring(L, n.ptr, n.len); /* push the header name as key. */ |
| 5076 | lua_newtable(L); /* create and push empty table. */ |
| 5077 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 5078 | lua_rawseti(L, -2, 0); /* index header value (pop it). */ |
| 5079 | 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] | 5080 | break; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5081 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5082 | case LUA_TTABLE: |
| 5083 | /* Entry found: push the value in the table. */ |
| 5084 | len = lua_rawlen(L, -1); |
| 5085 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 5086 | lua_rawseti(L, -2, len+1); /* index header value (pop it). */ |
| 5087 | lua_pop(L, 1); /* remove the table (it is stored in the main table). */ |
| 5088 | break; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5089 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5090 | default: |
| 5091 | /* Other cases are errors. */ |
| 5092 | hlua_pusherror(L, "internal error during the parsing of headers."); |
| 5093 | WILL_LJMP(lua_error(L)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5094 | } |
| 5095 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5096 | return 1; |
| 5097 | } |
| 5098 | |
| 5099 | __LJMP static int hlua_http_req_get_headers(lua_State *L) |
| 5100 | { |
| 5101 | struct hlua_txn *htxn; |
| 5102 | |
| 5103 | MAY_LJMP(check_args(L, 1, "req_get_headers")); |
| 5104 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5105 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5106 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5107 | WILL_LJMP(lua_error(L)); |
| 5108 | |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 5109 | return hlua_http_get_headers(L, &htxn->s->txn->req); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5110 | } |
| 5111 | |
| 5112 | __LJMP static int hlua_http_res_get_headers(lua_State *L) |
| 5113 | { |
| 5114 | struct hlua_txn *htxn; |
| 5115 | |
| 5116 | MAY_LJMP(check_args(L, 1, "res_get_headers")); |
| 5117 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5118 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5119 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5120 | WILL_LJMP(lua_error(L)); |
| 5121 | |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 5122 | return hlua_http_get_headers(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5123 | } |
| 5124 | |
| 5125 | /* This function replace full header, or just a value in |
| 5126 | * the request or in the response. It is a wrapper fir the |
| 5127 | * 4 following functions. |
| 5128 | */ |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5129 | __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] | 5130 | { |
| 5131 | size_t name_len; |
| 5132 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5133 | const char *reg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 5134 | const char *value = MAY_LJMP(luaL_checkstring(L, 4)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5135 | struct htx *htx; |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 5136 | struct my_regex *re; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5137 | |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 5138 | if (!(re = regex_comp(reg, 1, 1, NULL))) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5139 | WILL_LJMP(luaL_argerror(L, 3, "invalid regex")); |
| 5140 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5141 | htx = htxbuf(&msg->chn->buf); |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5142 | 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] | 5143 | regex_free(re); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5144 | return 0; |
| 5145 | } |
| 5146 | |
| 5147 | __LJMP static int hlua_http_req_rep_hdr(lua_State *L) |
| 5148 | { |
| 5149 | struct hlua_txn *htxn; |
| 5150 | |
| 5151 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 5152 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5153 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5154 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5155 | WILL_LJMP(lua_error(L)); |
| 5156 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5157 | 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] | 5158 | } |
| 5159 | |
| 5160 | __LJMP static int hlua_http_res_rep_hdr(lua_State *L) |
| 5161 | { |
| 5162 | struct hlua_txn *htxn; |
| 5163 | |
| 5164 | MAY_LJMP(check_args(L, 4, "res_rep_hdr")); |
| 5165 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5166 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5167 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5168 | WILL_LJMP(lua_error(L)); |
| 5169 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5170 | 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] | 5171 | } |
| 5172 | |
| 5173 | __LJMP static int hlua_http_req_rep_val(lua_State *L) |
| 5174 | { |
| 5175 | struct hlua_txn *htxn; |
| 5176 | |
| 5177 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 5178 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5179 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5180 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5181 | WILL_LJMP(lua_error(L)); |
| 5182 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5183 | 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] | 5184 | } |
| 5185 | |
| 5186 | __LJMP static int hlua_http_res_rep_val(lua_State *L) |
| 5187 | { |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5188 | struct hlua_txn *htxn; |
| 5189 | |
| 5190 | MAY_LJMP(check_args(L, 4, "res_rep_val")); |
| 5191 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5192 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5193 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5194 | WILL_LJMP(lua_error(L)); |
| 5195 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5196 | 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] | 5197 | } |
| 5198 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5199 | /* This function deletes all the occurrences of an header. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5200 | * It is a wrapper for the 2 following functions. |
| 5201 | */ |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5202 | __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] | 5203 | { |
| 5204 | size_t len; |
| 5205 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5206 | struct htx *htx = htxbuf(&msg->chn->buf); |
| 5207 | struct http_hdr_ctx ctx; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5208 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5209 | ctx.blk = NULL; |
| 5210 | while (http_find_header(htx, ist2(name, len), &ctx, 1)) |
| 5211 | http_remove_header(htx, &ctx); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5212 | return 0; |
| 5213 | } |
| 5214 | |
| 5215 | __LJMP static int hlua_http_req_del_hdr(lua_State *L) |
| 5216 | { |
| 5217 | struct hlua_txn *htxn; |
| 5218 | |
| 5219 | MAY_LJMP(check_args(L, 2, "req_del_hdr")); |
| 5220 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5221 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5222 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5223 | WILL_LJMP(lua_error(L)); |
| 5224 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5225 | return hlua_http_del_hdr(L, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5226 | } |
| 5227 | |
| 5228 | __LJMP static int hlua_http_res_del_hdr(lua_State *L) |
| 5229 | { |
| 5230 | struct hlua_txn *htxn; |
| 5231 | |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5232 | MAY_LJMP(check_args(L, 2, "res_del_hdr")); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5233 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5234 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5235 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5236 | WILL_LJMP(lua_error(L)); |
| 5237 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5238 | return hlua_http_del_hdr(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5239 | } |
| 5240 | |
| 5241 | /* This function adds an header. It is a wrapper used by |
| 5242 | * the 2 following functions. |
| 5243 | */ |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5244 | __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] | 5245 | { |
| 5246 | size_t name_len; |
| 5247 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5248 | size_t value_len; |
| 5249 | const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5250 | struct htx *htx = htxbuf(&msg->chn->buf); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5251 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5252 | lua_pushboolean(L, http_add_header(htx, ist2(name, name_len), |
| 5253 | ist2(value, value_len))); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5254 | return 0; |
| 5255 | } |
| 5256 | |
| 5257 | __LJMP static int hlua_http_req_add_hdr(lua_State *L) |
| 5258 | { |
| 5259 | struct hlua_txn *htxn; |
| 5260 | |
| 5261 | MAY_LJMP(check_args(L, 3, "req_add_hdr")); |
| 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 | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5267 | return hlua_http_add_hdr(L, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5268 | } |
| 5269 | |
| 5270 | __LJMP static int hlua_http_res_add_hdr(lua_State *L) |
| 5271 | { |
| 5272 | struct hlua_txn *htxn; |
| 5273 | |
| 5274 | MAY_LJMP(check_args(L, 3, "res_add_hdr")); |
| 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 | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5280 | return hlua_http_add_hdr(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5281 | } |
| 5282 | |
| 5283 | static int hlua_http_req_set_hdr(lua_State *L) |
| 5284 | { |
| 5285 | struct hlua_txn *htxn; |
| 5286 | |
| 5287 | MAY_LJMP(check_args(L, 3, "req_set_hdr")); |
| 5288 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5289 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5290 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5291 | WILL_LJMP(lua_error(L)); |
| 5292 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5293 | hlua_http_del_hdr(L, &htxn->s->txn->req); |
| 5294 | return hlua_http_add_hdr(L, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5295 | } |
| 5296 | |
| 5297 | static int hlua_http_res_set_hdr(lua_State *L) |
| 5298 | { |
| 5299 | struct hlua_txn *htxn; |
| 5300 | |
| 5301 | MAY_LJMP(check_args(L, 3, "res_set_hdr")); |
| 5302 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5303 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5304 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5305 | WILL_LJMP(lua_error(L)); |
| 5306 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5307 | hlua_http_del_hdr(L, &htxn->s->txn->rsp); |
| 5308 | return hlua_http_add_hdr(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5309 | } |
| 5310 | |
| 5311 | /* This function set the method. */ |
| 5312 | static int hlua_http_req_set_meth(lua_State *L) |
| 5313 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5314 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5315 | size_t name_len; |
| 5316 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5317 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5318 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5319 | WILL_LJMP(lua_error(L)); |
| 5320 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5321 | 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] | 5322 | return 1; |
| 5323 | } |
| 5324 | |
| 5325 | /* This function set the method. */ |
| 5326 | static int hlua_http_req_set_path(lua_State *L) |
| 5327 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5328 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5329 | size_t name_len; |
| 5330 | 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] | 5331 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5332 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5333 | WILL_LJMP(lua_error(L)); |
| 5334 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5335 | 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] | 5336 | return 1; |
| 5337 | } |
| 5338 | |
| 5339 | /* This function set the query-string. */ |
| 5340 | static int hlua_http_req_set_query(lua_State *L) |
| 5341 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5342 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5343 | size_t name_len; |
| 5344 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5345 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5346 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5347 | WILL_LJMP(lua_error(L)); |
| 5348 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5349 | /* Check length. */ |
| 5350 | if (name_len > trash.size - 1) { |
| 5351 | lua_pushboolean(L, 0); |
| 5352 | return 1; |
| 5353 | } |
| 5354 | |
| 5355 | /* Add the mark question as prefix. */ |
| 5356 | chunk_reset(&trash); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5357 | trash.area[trash.data++] = '?'; |
| 5358 | memcpy(trash.area + trash.data, name, name_len); |
| 5359 | trash.data += name_len; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5360 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5361 | lua_pushboolean(L, |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5362 | 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] | 5363 | return 1; |
| 5364 | } |
| 5365 | |
| 5366 | /* This function set the uri. */ |
| 5367 | static int hlua_http_req_set_uri(lua_State *L) |
| 5368 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5369 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5370 | size_t name_len; |
| 5371 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5372 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5373 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5374 | WILL_LJMP(lua_error(L)); |
| 5375 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5376 | 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] | 5377 | return 1; |
| 5378 | } |
| 5379 | |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5380 | /* This function set the response code & optionally reason. */ |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5381 | static int hlua_http_res_set_status(lua_State *L) |
| 5382 | { |
| 5383 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5384 | unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2)); |
Christopher Faulet | 96bff76 | 2019-12-17 13:46:18 +0100 | [diff] [blame] | 5385 | const char *str = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
| 5386 | const struct ist reason = ist2(str, (str ? strlen(str) : 0)); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5387 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5388 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5389 | WILL_LJMP(lua_error(L)); |
| 5390 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5391 | http_res_set_status(code, reason, htxn->s); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5392 | return 0; |
| 5393 | } |
| 5394 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5395 | /* |
| 5396 | * |
| 5397 | * |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5398 | * Class TXN |
| 5399 | * |
| 5400 | * |
| 5401 | */ |
| 5402 | |
| 5403 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5404 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5405 | */ |
| 5406 | __LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud) |
| 5407 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 5408 | return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref)); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5409 | } |
| 5410 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5411 | __LJMP static int hlua_set_var(lua_State *L) |
| 5412 | { |
| 5413 | struct hlua_txn *htxn; |
| 5414 | const char *name; |
| 5415 | size_t len; |
| 5416 | struct sample smp; |
| 5417 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 5418 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 5419 | 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] | 5420 | |
| 5421 | /* It is useles to retrieve the stream, but this function |
| 5422 | * runs only in a stream context. |
| 5423 | */ |
| 5424 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5425 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5426 | |
| 5427 | /* Converts the third argument in a sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 5428 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5429 | hlua_lua2smp(L, 3, &smp); |
| 5430 | |
Aurelien DARRAGON | d39fb74 | 2023-05-17 16:06:11 +0200 | [diff] [blame] | 5431 | /* Store the sample in a variable. We don't need to dup the smp, vars API |
| 5432 | * already takes care of duplicating dynamic var data. |
| 5433 | */ |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 5434 | 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] | 5435 | |
| 5436 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 5437 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 5438 | else |
| 5439 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 5440 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 5441 | return 1; |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5442 | } |
| 5443 | |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 5444 | __LJMP static int hlua_unset_var(lua_State *L) |
| 5445 | { |
| 5446 | struct hlua_txn *htxn; |
| 5447 | const char *name; |
| 5448 | size_t len; |
| 5449 | struct sample smp; |
| 5450 | |
| 5451 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 5452 | |
| 5453 | /* It is useles to retrieve the stream, but this function |
| 5454 | * runs only in a stream context. |
| 5455 | */ |
| 5456 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5457 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5458 | |
| 5459 | /* Unset the variable. */ |
| 5460 | 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] | 5461 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 5462 | return 1; |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 5463 | } |
| 5464 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5465 | __LJMP static int hlua_get_var(lua_State *L) |
| 5466 | { |
| 5467 | struct hlua_txn *htxn; |
| 5468 | const char *name; |
| 5469 | size_t len; |
| 5470 | struct sample smp; |
| 5471 | |
| 5472 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 5473 | |
| 5474 | /* It is useles to retrieve the stream, but this function |
| 5475 | * runs only in a stream context. |
| 5476 | */ |
| 5477 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5478 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5479 | |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 5480 | 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] | 5481 | if (!vars_get_by_name(name, len, &smp)) { |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5482 | lua_pushnil(L); |
| 5483 | return 1; |
| 5484 | } |
| 5485 | |
| 5486 | return hlua_smp2lua(L, &smp); |
| 5487 | } |
| 5488 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5489 | __LJMP static int hlua_set_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5490 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5491 | struct hlua *hlua; |
| 5492 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5493 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 5494 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5495 | /* It is useles to retrieve the stream, but this function |
| 5496 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5497 | */ |
| 5498 | MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 5499 | |
| 5500 | /* Get hlua struct, or NULL if we execute from main lua state */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5501 | hlua = hlua_gethlua(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 5502 | if (!hlua) |
| 5503 | return 0; |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5504 | |
| 5505 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 5506 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5507 | |
| 5508 | /* Get and store new value. */ |
| 5509 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 5510 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 5511 | |
| 5512 | return 0; |
| 5513 | } |
| 5514 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5515 | __LJMP static int hlua_get_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5516 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5517 | struct hlua *hlua; |
| 5518 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5519 | MAY_LJMP(check_args(L, 1, "get_priv")); |
| 5520 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5521 | /* It is useles to retrieve the stream, but this function |
| 5522 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5523 | */ |
| 5524 | MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 5525 | |
| 5526 | /* Get hlua struct, or NULL if we execute from main lua state */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5527 | hlua = hlua_gethlua(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 5528 | if (!hlua) { |
| 5529 | lua_pushnil(L); |
| 5530 | return 1; |
| 5531 | } |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5532 | |
| 5533 | /* Push configuration index in the stack. */ |
| 5534 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 5535 | |
| 5536 | return 1; |
| 5537 | } |
| 5538 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5539 | /* Create stack entry containing a class TXN. This function |
| 5540 | * return 0 if the stack does not contains free slots, |
| 5541 | * otherwise it returns 1. |
| 5542 | */ |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5543 | 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] | 5544 | { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5545 | struct hlua_txn *htxn; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5546 | |
| 5547 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5548 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5549 | return 0; |
| 5550 | |
| 5551 | /* NOTE: The allocation never fails. The failure |
| 5552 | * throw an error, and the function never returns. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5553 | * if the throw is not available, the process is aborted. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5554 | */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5555 | /* Create the object: obj[0] = userdata. */ |
| 5556 | lua_newtable(L); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5557 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5558 | lua_rawseti(L, -2, 0); |
| 5559 | |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5560 | htxn->s = s; |
| 5561 | htxn->p = p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 5562 | htxn->dir = dir; |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5563 | htxn->flags = flags; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5564 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 5565 | /* Create the "f" field that contains a list of fetches. */ |
| 5566 | lua_pushstring(L, "f"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 5567 | if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5568 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5569 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5570 | |
| 5571 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 5572 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 5573 | 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] | 5574 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5575 | lua_rawset(L, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 5576 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5577 | /* Create the "c" field that contains a list of converters. */ |
| 5578 | lua_pushstring(L, "c"); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5579 | if (!hlua_converters_new(L, htxn, 0)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5580 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5581 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5582 | |
| 5583 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 5584 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 5585 | if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5586 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5587 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5588 | |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5589 | /* Create the "req" field that contains the request channel object. */ |
| 5590 | lua_pushstring(L, "req"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 5591 | if (!hlua_channel_new(L, &s->req)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5592 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5593 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5594 | |
| 5595 | /* Create the "res" field that contains the response channel object. */ |
| 5596 | lua_pushstring(L, "res"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 5597 | if (!hlua_channel_new(L, &s->res)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5598 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5599 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5600 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5601 | /* Creates the HTTP object is the current proxy allows http. */ |
| 5602 | lua_pushstring(L, "http"); |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 5603 | if (IS_HTX_STRM(s)) { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5604 | if (!hlua_http_new(L, htxn)) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5605 | return 0; |
| 5606 | } |
| 5607 | else |
| 5608 | lua_pushnil(L); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5609 | lua_rawset(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5610 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5611 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 5612 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref); |
| 5613 | lua_setmetatable(L, -2); |
| 5614 | |
| 5615 | return 1; |
| 5616 | } |
| 5617 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 5618 | __LJMP static int hlua_txn_deflog(lua_State *L) |
| 5619 | { |
| 5620 | const char *msg; |
| 5621 | struct hlua_txn *htxn; |
| 5622 | |
| 5623 | MAY_LJMP(check_args(L, 2, "deflog")); |
| 5624 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5625 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5626 | |
| 5627 | hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg); |
| 5628 | return 0; |
| 5629 | } |
| 5630 | |
| 5631 | __LJMP static int hlua_txn_log(lua_State *L) |
| 5632 | { |
| 5633 | int level; |
| 5634 | const char *msg; |
| 5635 | struct hlua_txn *htxn; |
| 5636 | |
| 5637 | MAY_LJMP(check_args(L, 3, "log")); |
| 5638 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5639 | level = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5640 | msg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 5641 | |
| 5642 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 5643 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 5644 | |
| 5645 | hlua_sendlog(htxn->s->be, level, msg); |
| 5646 | return 0; |
| 5647 | } |
| 5648 | |
| 5649 | __LJMP static int hlua_txn_log_debug(lua_State *L) |
| 5650 | { |
| 5651 | const char *msg; |
| 5652 | struct hlua_txn *htxn; |
| 5653 | |
| 5654 | MAY_LJMP(check_args(L, 2, "Debug")); |
| 5655 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5656 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5657 | hlua_sendlog(htxn->s->be, LOG_DEBUG, msg); |
| 5658 | return 0; |
| 5659 | } |
| 5660 | |
| 5661 | __LJMP static int hlua_txn_log_info(lua_State *L) |
| 5662 | { |
| 5663 | const char *msg; |
| 5664 | struct hlua_txn *htxn; |
| 5665 | |
| 5666 | MAY_LJMP(check_args(L, 2, "Info")); |
| 5667 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5668 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5669 | hlua_sendlog(htxn->s->be, LOG_INFO, msg); |
| 5670 | return 0; |
| 5671 | } |
| 5672 | |
| 5673 | __LJMP static int hlua_txn_log_warning(lua_State *L) |
| 5674 | { |
| 5675 | const char *msg; |
| 5676 | struct hlua_txn *htxn; |
| 5677 | |
| 5678 | MAY_LJMP(check_args(L, 2, "Warning")); |
| 5679 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5680 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5681 | hlua_sendlog(htxn->s->be, LOG_WARNING, msg); |
| 5682 | return 0; |
| 5683 | } |
| 5684 | |
| 5685 | __LJMP static int hlua_txn_log_alert(lua_State *L) |
| 5686 | { |
| 5687 | const char *msg; |
| 5688 | struct hlua_txn *htxn; |
| 5689 | |
| 5690 | MAY_LJMP(check_args(L, 2, "Alert")); |
| 5691 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5692 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5693 | hlua_sendlog(htxn->s->be, LOG_ALERT, msg); |
| 5694 | return 0; |
| 5695 | } |
| 5696 | |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5697 | __LJMP static int hlua_txn_set_loglevel(lua_State *L) |
| 5698 | { |
| 5699 | struct hlua_txn *htxn; |
| 5700 | int ll; |
| 5701 | |
| 5702 | MAY_LJMP(check_args(L, 2, "set_loglevel")); |
| 5703 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5704 | ll = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5705 | |
Christopher Faulet | 8263e94 | 2024-02-29 15:41:17 +0100 | [diff] [blame] | 5706 | if (ll < -1 || ll > NB_LOG_LEVELS) |
| 5707 | WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be one of the following value:" |
| 5708 | " core.silent(-1), core.emerg(0), core.alert(1), core.crit(2), core.error(3)," |
| 5709 | " 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] | 5710 | |
Christopher Faulet | 8263e94 | 2024-02-29 15:41:17 +0100 | [diff] [blame] | 5711 | htxn->s->logs.level = (ll == -1) ? ll : ll + 1; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5712 | return 0; |
| 5713 | } |
| 5714 | |
| 5715 | __LJMP static int hlua_txn_set_tos(lua_State *L) |
| 5716 | { |
| 5717 | struct hlua_txn *htxn; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5718 | int tos; |
| 5719 | |
| 5720 | MAY_LJMP(check_args(L, 2, "set_tos")); |
| 5721 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5722 | tos = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5723 | |
Willy Tarreau | 1a18b54 | 2018-12-11 16:37:42 +0100 | [diff] [blame] | 5724 | conn_set_tos(objt_conn(htxn->s->sess->origin), tos); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5725 | return 0; |
| 5726 | } |
| 5727 | |
| 5728 | __LJMP static int hlua_txn_set_mark(lua_State *L) |
| 5729 | { |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5730 | struct hlua_txn *htxn; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5731 | int mark; |
| 5732 | |
| 5733 | MAY_LJMP(check_args(L, 2, "set_mark")); |
| 5734 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5735 | mark = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5736 | |
Lukas Tribus | 579e3e3 | 2019-08-11 18:03:45 +0200 | [diff] [blame] | 5737 | conn_set_mark(objt_conn(htxn->s->sess->origin), mark); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5738 | return 0; |
| 5739 | } |
| 5740 | |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 5741 | __LJMP static int hlua_txn_set_priority_class(lua_State *L) |
| 5742 | { |
| 5743 | struct hlua_txn *htxn; |
| 5744 | |
| 5745 | MAY_LJMP(check_args(L, 2, "set_priority_class")); |
| 5746 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5747 | htxn->s->priority_class = queue_limit_class(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 5748 | return 0; |
| 5749 | } |
| 5750 | |
| 5751 | __LJMP static int hlua_txn_set_priority_offset(lua_State *L) |
| 5752 | { |
| 5753 | struct hlua_txn *htxn; |
| 5754 | |
| 5755 | MAY_LJMP(check_args(L, 2, "set_priority_offset")); |
| 5756 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5757 | htxn->s->priority_offset = queue_limit_offset(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 5758 | return 0; |
| 5759 | } |
| 5760 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5761 | /* 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] | 5762 | * 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] | 5763 | * message and terminate the transaction. It returns 1 on success and 0 on |
| 5764 | * error. The Reply must be on top of the stack. |
| 5765 | */ |
| 5766 | __LJMP static int hlua_txn_forward_reply(lua_State *L, struct stream *s) |
| 5767 | { |
| 5768 | struct htx *htx; |
| 5769 | struct htx_sl *sl; |
| 5770 | struct h1m h1m; |
| 5771 | const char *status, *reason, *body; |
| 5772 | size_t status_len, reason_len, body_len; |
| 5773 | int ret, code, flags; |
| 5774 | |
| 5775 | code = 200; |
| 5776 | status = "200"; |
| 5777 | status_len = 3; |
| 5778 | ret = lua_getfield(L, -1, "status"); |
| 5779 | if (ret == LUA_TNUMBER) { |
| 5780 | code = lua_tointeger(L, -1); |
| 5781 | status = lua_tolstring(L, -1, &status_len); |
| 5782 | } |
| 5783 | lua_pop(L, 1); |
| 5784 | |
| 5785 | reason = http_get_reason(code); |
| 5786 | reason_len = strlen(reason); |
| 5787 | ret = lua_getfield(L, -1, "reason"); |
| 5788 | if (ret == LUA_TSTRING) |
| 5789 | reason = lua_tolstring(L, -1, &reason_len); |
| 5790 | lua_pop(L, 1); |
| 5791 | |
| 5792 | body = NULL; |
| 5793 | body_len = 0; |
| 5794 | ret = lua_getfield(L, -1, "body"); |
| 5795 | if (ret == LUA_TSTRING) |
| 5796 | body = lua_tolstring(L, -1, &body_len); |
| 5797 | lua_pop(L, 1); |
| 5798 | |
| 5799 | /* Prepare the response before inserting the headers */ |
| 5800 | h1m_init_res(&h1m); |
| 5801 | htx = htx_from_buf(&s->res.buf); |
| 5802 | channel_htx_truncate(&s->res, htx); |
| 5803 | if (s->txn->req.flags & HTTP_MSGF_VER_11) { |
| 5804 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11); |
| 5805 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), |
| 5806 | ist2(status, status_len), ist2(reason, reason_len)); |
| 5807 | } |
| 5808 | else { |
| 5809 | flags = HTX_SL_F_IS_RESP; |
| 5810 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), |
| 5811 | ist2(status, status_len), ist2(reason, reason_len)); |
| 5812 | } |
| 5813 | if (!sl) |
| 5814 | goto fail; |
| 5815 | sl->info.res.status = code; |
| 5816 | |
| 5817 | /* Push in the stack the "headers" entry. */ |
| 5818 | ret = lua_getfield(L, -1, "headers"); |
| 5819 | if (ret != LUA_TTABLE) |
| 5820 | goto skip_headers; |
| 5821 | |
| 5822 | lua_pushnil(L); |
| 5823 | while (lua_next(L, -2) != 0) { |
| 5824 | struct ist name, value; |
| 5825 | const char *n, *v; |
| 5826 | size_t nlen, vlen; |
| 5827 | |
| 5828 | if (!lua_isstring(L, -2) || !lua_istable(L, -1)) { |
| 5829 | /* Skip element if the key is not a string or if the value is not a table */ |
| 5830 | goto next_hdr; |
| 5831 | } |
| 5832 | |
| 5833 | n = lua_tolstring(L, -2, &nlen); |
| 5834 | name = ist2(n, nlen); |
| 5835 | if (isteqi(name, ist("content-length"))) { |
| 5836 | /* Always skip content-length header. It will be added |
| 5837 | * later with the correct len |
| 5838 | */ |
| 5839 | goto next_hdr; |
| 5840 | } |
| 5841 | |
| 5842 | /* Loop on header's values */ |
| 5843 | lua_pushnil(L); |
| 5844 | while (lua_next(L, -2)) { |
| 5845 | if (!lua_isstring(L, -1)) { |
| 5846 | /* Skip the value if it is not a string */ |
| 5847 | goto next_value; |
| 5848 | } |
| 5849 | |
| 5850 | v = lua_tolstring(L, -1, &vlen); |
| 5851 | value = ist2(v, vlen); |
| 5852 | |
| 5853 | if (isteqi(name, ist("transfer-encoding"))) |
| 5854 | h1_parse_xfer_enc_header(&h1m, value); |
| 5855 | if (!htx_add_header(htx, ist2(n, nlen), ist2(v, vlen))) |
| 5856 | goto fail; |
| 5857 | |
| 5858 | next_value: |
| 5859 | lua_pop(L, 1); |
| 5860 | } |
| 5861 | |
| 5862 | next_hdr: |
| 5863 | lua_pop(L, 1); |
| 5864 | } |
| 5865 | skip_headers: |
| 5866 | lua_pop(L, 1); |
| 5867 | |
| 5868 | /* Update h1m flags: CLEN is set if CHNK is not present */ |
| 5869 | if (!(h1m.flags & H1_MF_CHNK)) { |
| 5870 | const char *clen = ultoa(body_len); |
| 5871 | |
| 5872 | h1m.flags |= H1_MF_CLEN; |
| 5873 | if (!htx_add_header(htx, ist("content-length"), ist(clen))) |
| 5874 | goto fail; |
| 5875 | } |
| 5876 | if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK)) |
| 5877 | h1m.flags |= H1_MF_XFER_LEN; |
| 5878 | |
| 5879 | /* Update HTX start-line flags */ |
| 5880 | if (h1m.flags & H1_MF_XFER_ENC) |
| 5881 | flags |= HTX_SL_F_XFER_ENC; |
| 5882 | if (h1m.flags & H1_MF_XFER_LEN) { |
| 5883 | flags |= HTX_SL_F_XFER_LEN; |
| 5884 | if (h1m.flags & H1_MF_CHNK) |
| 5885 | flags |= HTX_SL_F_CHNK; |
| 5886 | else if (h1m.flags & H1_MF_CLEN) |
| 5887 | flags |= HTX_SL_F_CLEN; |
| 5888 | if (h1m.body_len == 0) |
| 5889 | flags |= HTX_SL_F_BODYLESS; |
| 5890 | } |
| 5891 | sl->flags |= flags; |
| 5892 | |
| 5893 | |
| 5894 | if (!htx_add_endof(htx, HTX_BLK_EOH) || |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 5895 | (body_len && !htx_add_data_atonce(htx, ist2(body, body_len)))) |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5896 | goto fail; |
| 5897 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 5898 | htx->flags |= HTX_FL_EOM; |
| 5899 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5900 | /* Now, forward the response and terminate the transaction */ |
| 5901 | s->txn->status = code; |
| 5902 | htx_to_buf(htx, &s->res.buf); |
| 5903 | if (!http_forward_proxy_resp(s, 1)) |
| 5904 | goto fail; |
| 5905 | |
| 5906 | return 1; |
| 5907 | |
| 5908 | fail: |
| 5909 | channel_htx_truncate(&s->res, htx); |
| 5910 | return 0; |
| 5911 | } |
| 5912 | |
| 5913 | /* Terminate a transaction if called from a lua action. For TCP streams, |
| 5914 | * processing is just aborted. Nothing is returned to the client and all |
| 5915 | * arguments are ignored. For HTTP streams, if a reply is passed as argument, it |
| 5916 | * is forwarded to the client before terminating the transaction. On success, |
| 5917 | * the function exits with ACT_RET_DONE code. If an error occurred, it exits |
| 5918 | * with ACT_RET_ERR code. If this function is not called from a lua action, it |
| 5919 | * just exits without any processing. |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5920 | */ |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 5921 | __LJMP static int hlua_txn_done(lua_State *L) |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5922 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 5923 | struct hlua_txn *htxn; |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5924 | struct stream *s; |
| 5925 | int finst; |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5926 | |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 5927 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5928 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5929 | /* If the flags NOTERM is set, we cannot terminate the session, so we |
| 5930 | * just end the execution of the current lua code. */ |
| 5931 | if (htxn->flags & HLUA_TXN_NOTERM) |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5932 | WILL_LJMP(hlua_done(L)); |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5933 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5934 | s = htxn->s; |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5935 | if (!IS_HTX_STRM(htxn->s)) { |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5936 | struct channel *req = &s->req; |
| 5937 | struct channel *res = &s->res; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 5938 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5939 | channel_auto_read(req); |
| 5940 | channel_abort(req); |
| 5941 | channel_auto_close(req); |
| 5942 | channel_erase(req); |
| 5943 | |
| 5944 | res->wex = tick_add_ifset(now_ms, res->wto); |
| 5945 | channel_auto_read(res); |
| 5946 | channel_auto_close(res); |
| 5947 | channel_shutr_now(res); |
| 5948 | |
| 5949 | finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_D); |
| 5950 | goto done; |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 5951 | } |
Thierry FOURNIER | 10ec214 | 2015-08-24 17:23:45 +0200 | [diff] [blame] | 5952 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5953 | if (lua_gettop(L) == 1 || !lua_istable(L, 2)) { |
| 5954 | /* No reply or invalid reply */ |
| 5955 | s->txn->status = 0; |
| 5956 | http_reply_and_close(s, 0, NULL); |
| 5957 | } |
| 5958 | else { |
| 5959 | /* Remove extra args to have the reply on top of the stack */ |
| 5960 | if (lua_gettop(L) > 2) |
| 5961 | lua_pop(L, lua_gettop(L) - 2); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5962 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5963 | if (!hlua_txn_forward_reply(L, s)) { |
| 5964 | if (!(s->flags & SF_ERR_MASK)) |
| 5965 | s->flags |= SF_ERR_PRXCOND; |
| 5966 | lua_pushinteger(L, ACT_RET_ERR); |
| 5967 | WILL_LJMP(hlua_done(L)); |
| 5968 | return 0; /* Never reached */ |
| 5969 | } |
Christopher Faulet | 4d0e263 | 2019-07-16 10:52:40 +0200 | [diff] [blame] | 5970 | } |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 5971 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5972 | finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_H); |
| 5973 | if (htxn->dir == SMP_OPT_DIR_REQ) { |
| 5974 | /* let's log the request time */ |
| 5975 | s->logs.tv_request = now; |
| 5976 | 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] | 5977 | _HA_ATOMIC_INC(&s->sess->fe->fe_counters.intercepted_req); |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5978 | } |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 5979 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5980 | done: |
| 5981 | if (!(s->flags & SF_ERR_MASK)) |
| 5982 | s->flags |= SF_ERR_LOCAL; |
| 5983 | if (!(s->flags & SF_FINST_MASK)) |
| 5984 | s->flags |= finst; |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 5985 | |
Christopher Faulet | 4ad7310 | 2020-03-05 11:07:31 +0100 | [diff] [blame] | 5986 | lua_pushinteger(L, ACT_RET_ABRT); |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 5987 | WILL_LJMP(hlua_done(L)); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 5988 | return 0; |
| 5989 | } |
| 5990 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5991 | /* |
| 5992 | * |
| 5993 | * |
| 5994 | * Class REPLY |
| 5995 | * |
| 5996 | * |
| 5997 | */ |
| 5998 | |
| 5999 | /* Pushes the TXN reply onto the top of the stack. If the stask does not have a |
| 6000 | * free slots, the function fails and returns 0; |
| 6001 | */ |
| 6002 | static int hlua_txn_reply_new(lua_State *L) |
| 6003 | { |
| 6004 | struct hlua_txn *htxn; |
| 6005 | const char *reason, *body = NULL; |
| 6006 | int ret, status; |
| 6007 | |
| 6008 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 6009 | if (!IS_HTX_STRM(htxn->s)) { |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 6010 | hlua_pusherror(L, "txn object is not an HTTP transaction."); |
| 6011 | WILL_LJMP(lua_error(L)); |
| 6012 | } |
| 6013 | |
| 6014 | /* Default value */ |
| 6015 | status = 200; |
| 6016 | reason = http_get_reason(status); |
| 6017 | |
| 6018 | if (lua_istable(L, 2)) { |
| 6019 | /* load status and reason from the table argument at index 2 */ |
| 6020 | ret = lua_getfield(L, 2, "status"); |
| 6021 | if (ret == LUA_TNIL) |
| 6022 | goto reason; |
| 6023 | else if (ret != LUA_TNUMBER) { |
| 6024 | /* invalid status: ignore the reason */ |
| 6025 | goto body; |
| 6026 | } |
| 6027 | status = lua_tointeger(L, -1); |
| 6028 | |
| 6029 | reason: |
| 6030 | lua_pop(L, 1); /* restore the stack: remove status */ |
| 6031 | ret = lua_getfield(L, 2, "reason"); |
| 6032 | if (ret == LUA_TSTRING) |
| 6033 | reason = lua_tostring(L, -1); |
| 6034 | |
| 6035 | body: |
| 6036 | lua_pop(L, 1); /* restore the stack: remove invalid status or reason */ |
| 6037 | ret = lua_getfield(L, 2, "body"); |
| 6038 | if (ret == LUA_TSTRING) |
| 6039 | body = lua_tostring(L, -1); |
| 6040 | lua_pop(L, 1); /* restore the stack: remove body */ |
| 6041 | } |
| 6042 | |
| 6043 | /* Create the Reply table */ |
| 6044 | lua_newtable(L); |
| 6045 | |
| 6046 | /* Add status element */ |
| 6047 | lua_pushstring(L, "status"); |
| 6048 | lua_pushinteger(L, status); |
| 6049 | lua_settable(L, -3); |
| 6050 | |
| 6051 | /* Add reason element */ |
| 6052 | reason = http_get_reason(status); |
| 6053 | lua_pushstring(L, "reason"); |
| 6054 | lua_pushstring(L, reason); |
| 6055 | lua_settable(L, -3); |
| 6056 | |
| 6057 | /* Add body element, nil if undefined */ |
| 6058 | lua_pushstring(L, "body"); |
| 6059 | if (body) |
| 6060 | lua_pushstring(L, body); |
| 6061 | else |
| 6062 | lua_pushnil(L); |
| 6063 | lua_settable(L, -3); |
| 6064 | |
| 6065 | /* Add headers element */ |
| 6066 | lua_pushstring(L, "headers"); |
| 6067 | lua_newtable(L); |
| 6068 | |
| 6069 | /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */ |
| 6070 | if (lua_istable(L, 2)) { |
| 6071 | /* load headers from the table argument at index 2. If it is a table, copy it. */ |
| 6072 | ret = lua_getfield(L, 2, "headers"); |
| 6073 | if (ret == LUA_TTABLE) { |
| 6074 | /* stack: [ ... <headers:table>, <table> ] */ |
| 6075 | lua_pushnil(L); |
| 6076 | while (lua_next(L, -2) != 0) { |
| 6077 | /* stack: [ ... <headers:table>, <table>, k, v] */ |
| 6078 | if (!lua_isstring(L, -1) && !lua_istable(L, -1)) { |
| 6079 | /* invalid value type, skip it */ |
| 6080 | lua_pop(L, 1); |
| 6081 | continue; |
| 6082 | } |
| 6083 | |
| 6084 | |
| 6085 | /* Duplicate the key and swap it with the value. */ |
| 6086 | lua_pushvalue(L, -2); |
| 6087 | lua_insert(L, -2); |
| 6088 | /* stack: [ ... <headers:table>, <table>, k, k, v ] */ |
| 6089 | |
| 6090 | lua_newtable(L); |
| 6091 | lua_insert(L, -2); |
| 6092 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, v ] */ |
| 6093 | |
| 6094 | if (lua_isstring(L, -1)) { |
| 6095 | /* push the value in the inner table */ |
| 6096 | lua_rawseti(L, -2, 1); |
| 6097 | } |
| 6098 | else { /* table */ |
| 6099 | lua_pushnil(L); |
| 6100 | while (lua_next(L, -2) != 0) { |
| 6101 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2, v2 ] */ |
| 6102 | if (!lua_isstring(L, -1)) { |
| 6103 | /* invalid value type, skip it*/ |
| 6104 | lua_pop(L, 1); |
| 6105 | continue; |
| 6106 | } |
| 6107 | /* push the value in the inner table */ |
| 6108 | lua_rawseti(L, -4, lua_rawlen(L, -4) + 1); |
| 6109 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2 ] */ |
| 6110 | } |
| 6111 | lua_pop(L, 1); |
| 6112 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table> ] */ |
| 6113 | } |
| 6114 | |
| 6115 | /* push (k,v) on the stack in the headers table: |
| 6116 | * stack: [ ... <headers:table>, <table>, k, k, v ] |
| 6117 | */ |
| 6118 | lua_settable(L, -5); |
| 6119 | /* stack: [ ... <headers:table>, <table>, k ] */ |
| 6120 | } |
| 6121 | } |
| 6122 | lua_pop(L, 1); |
| 6123 | } |
| 6124 | /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */ |
| 6125 | lua_settable(L, -3); |
| 6126 | /* stack: [ txn, <Arg:table>, <Reply:table> ] */ |
| 6127 | |
| 6128 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 6129 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_reply_ref); |
| 6130 | lua_setmetatable(L, -2); |
| 6131 | return 1; |
| 6132 | } |
| 6133 | |
| 6134 | /* Set the reply status code, and optionally the reason. If no reason is |
| 6135 | * provided, the default one corresponding to the status code is used. |
| 6136 | */ |
| 6137 | __LJMP static int hlua_txn_reply_set_status(lua_State *L) |
| 6138 | { |
| 6139 | int status = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 6140 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
| 6141 | |
| 6142 | /* First argument (self) must be a table */ |
| 6143 | luaL_checktype(L, 1, LUA_TTABLE); |
| 6144 | |
| 6145 | if (status < 100 || status > 599) { |
| 6146 | lua_pushboolean(L, 0); |
| 6147 | return 1; |
| 6148 | } |
| 6149 | if (!reason) |
| 6150 | reason = http_get_reason(status); |
| 6151 | |
| 6152 | lua_pushinteger(L, status); |
| 6153 | lua_setfield(L, 1, "status"); |
| 6154 | |
| 6155 | lua_pushstring(L, reason); |
| 6156 | lua_setfield(L, 1, "reason"); |
| 6157 | |
| 6158 | lua_pushboolean(L, 1); |
| 6159 | return 1; |
| 6160 | } |
| 6161 | |
| 6162 | /* Add a header into the reply object. Each header name is associated to an |
| 6163 | * array of values in the "headers" table. If the header name is not found, a |
| 6164 | * new entry is created. |
| 6165 | */ |
| 6166 | __LJMP static int hlua_txn_reply_add_header(lua_State *L) |
| 6167 | { |
| 6168 | const char *name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 6169 | const char *value = MAY_LJMP(luaL_checkstring(L, 3)); |
| 6170 | int ret; |
| 6171 | |
| 6172 | /* First argument (self) must be a table */ |
| 6173 | luaL_checktype(L, 1, LUA_TTABLE); |
| 6174 | |
| 6175 | /* Push in the stack the "headers" entry. */ |
| 6176 | ret = lua_getfield(L, 1, "headers"); |
| 6177 | if (ret != LUA_TTABLE) { |
| 6178 | hlua_pusherror(L, "Reply['headers'] is expected to a an array. %s found", lua_typename(L, ret)); |
| 6179 | WILL_LJMP(lua_error(L)); |
| 6180 | } |
| 6181 | |
| 6182 | /* check if the header is already registered. If not, register it. */ |
| 6183 | ret = lua_getfield(L, -1, name); |
| 6184 | if (ret == LUA_TNIL) { |
| 6185 | /* Entry not found. */ |
| 6186 | lua_pop(L, 1); /* remove the nil. The "headers" table is the top of the stack. */ |
| 6187 | |
| 6188 | /* Insert the new header name in the array in the top of the stack. |
| 6189 | * It left the new array in the top of the stack. |
| 6190 | */ |
| 6191 | lua_newtable(L); |
| 6192 | lua_pushstring(L, name); |
| 6193 | lua_pushvalue(L, -2); |
| 6194 | lua_settable(L, -4); |
| 6195 | } |
| 6196 | else if (ret != LUA_TTABLE) { |
| 6197 | hlua_pusherror(L, "Reply['headers']['%s'] is expected to be an array. %s found", name, lua_typename(L, ret)); |
| 6198 | WILL_LJMP(lua_error(L)); |
| 6199 | } |
| 6200 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 6201 | /* Now the top of thestack is an array of values. We push |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 6202 | * the header value as new entry. |
| 6203 | */ |
| 6204 | lua_pushstring(L, value); |
| 6205 | ret = lua_rawlen(L, -2); |
| 6206 | lua_rawseti(L, -2, ret + 1); |
| 6207 | |
| 6208 | lua_pushboolean(L, 1); |
| 6209 | return 1; |
| 6210 | } |
| 6211 | |
| 6212 | /* Remove all occurrences of a given header name. */ |
| 6213 | __LJMP static int hlua_txn_reply_del_header(lua_State *L) |
| 6214 | { |
| 6215 | const char *name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 6216 | int ret; |
| 6217 | |
| 6218 | /* First argument (self) must be a table */ |
| 6219 | luaL_checktype(L, 1, LUA_TTABLE); |
| 6220 | |
| 6221 | /* Push in the stack the "headers" entry. */ |
| 6222 | ret = lua_getfield(L, 1, "headers"); |
| 6223 | if (ret != LUA_TTABLE) { |
| 6224 | hlua_pusherror(L, "Reply['headers'] is expected to be an array. %s found", lua_typename(L, ret)); |
| 6225 | WILL_LJMP(lua_error(L)); |
| 6226 | } |
| 6227 | |
| 6228 | lua_pushstring(L, name); |
| 6229 | lua_pushnil(L); |
| 6230 | lua_settable(L, -3); |
| 6231 | |
| 6232 | lua_pushboolean(L, 1); |
| 6233 | return 1; |
| 6234 | } |
| 6235 | |
| 6236 | /* Set the reply's body. Overwrite any existing entry. */ |
| 6237 | __LJMP static int hlua_txn_reply_set_body(lua_State *L) |
| 6238 | { |
| 6239 | const char *payload = MAY_LJMP(luaL_checkstring(L, 2)); |
| 6240 | |
| 6241 | /* First argument (self) must be a table */ |
| 6242 | luaL_checktype(L, 1, LUA_TTABLE); |
| 6243 | |
| 6244 | lua_pushstring(L, payload); |
| 6245 | lua_setfield(L, 1, "body"); |
| 6246 | |
| 6247 | lua_pushboolean(L, 1); |
| 6248 | return 1; |
| 6249 | } |
| 6250 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 6251 | __LJMP static int hlua_log(lua_State *L) |
| 6252 | { |
| 6253 | int level; |
| 6254 | const char *msg; |
| 6255 | |
| 6256 | MAY_LJMP(check_args(L, 2, "log")); |
| 6257 | level = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 6258 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 6259 | |
| 6260 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 6261 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 6262 | |
| 6263 | hlua_sendlog(NULL, level, msg); |
| 6264 | return 0; |
| 6265 | } |
| 6266 | |
| 6267 | __LJMP static int hlua_log_debug(lua_State *L) |
| 6268 | { |
| 6269 | const char *msg; |
| 6270 | |
| 6271 | MAY_LJMP(check_args(L, 1, "debug")); |
| 6272 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6273 | hlua_sendlog(NULL, LOG_DEBUG, msg); |
| 6274 | return 0; |
| 6275 | } |
| 6276 | |
| 6277 | __LJMP static int hlua_log_info(lua_State *L) |
| 6278 | { |
| 6279 | const char *msg; |
| 6280 | |
| 6281 | MAY_LJMP(check_args(L, 1, "info")); |
| 6282 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6283 | hlua_sendlog(NULL, LOG_INFO, msg); |
| 6284 | return 0; |
| 6285 | } |
| 6286 | |
| 6287 | __LJMP static int hlua_log_warning(lua_State *L) |
| 6288 | { |
| 6289 | const char *msg; |
| 6290 | |
| 6291 | MAY_LJMP(check_args(L, 1, "warning")); |
| 6292 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6293 | hlua_sendlog(NULL, LOG_WARNING, msg); |
| 6294 | return 0; |
| 6295 | } |
| 6296 | |
| 6297 | __LJMP static int hlua_log_alert(lua_State *L) |
| 6298 | { |
| 6299 | const char *msg; |
| 6300 | |
| 6301 | MAY_LJMP(check_args(L, 1, "alert")); |
| 6302 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6303 | hlua_sendlog(NULL, LOG_ALERT, msg); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 6304 | return 0; |
| 6305 | } |
| 6306 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 6307 | __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] | 6308 | { |
| 6309 | int wakeup_ms = lua_tointeger(L, -1); |
Willy Tarreau | 9cc2e4c | 2021-09-30 16:12:31 +0200 | [diff] [blame] | 6310 | if (!tick_is_expired(wakeup_ms, now_ms)) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 6311 | 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] | 6312 | return 0; |
| 6313 | } |
| 6314 | |
| 6315 | __LJMP static int hlua_sleep(lua_State *L) |
| 6316 | { |
| 6317 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6318 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6319 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6320 | MAY_LJMP(check_args(L, 1, "sleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6321 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 6322 | delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6323 | wakeup_ms = tick_add(now_ms, delay); |
| 6324 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6325 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 6326 | 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] | 6327 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6328 | } |
| 6329 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6330 | __LJMP static int hlua_msleep(lua_State *L) |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6331 | { |
| 6332 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6333 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6334 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6335 | MAY_LJMP(check_args(L, 1, "msleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6336 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 6337 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6338 | wakeup_ms = tick_add(now_ms, delay); |
| 6339 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6340 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 6341 | 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] | 6342 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6343 | } |
| 6344 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 6345 | /* This functionis an LUA binding. it permits to give back |
| 6346 | * the hand at the HAProxy scheduler. It is used when the |
| 6347 | * LUA processing consumes a lot of time. |
| 6348 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 6349 | __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] | 6350 | { |
| 6351 | return 0; |
| 6352 | } |
| 6353 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 6354 | __LJMP static int hlua_yield(lua_State *L) |
| 6355 | { |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 6356 | 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] | 6357 | return 0; |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 6358 | } |
| 6359 | |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6360 | /* This function change the nice of the currently executed |
| 6361 | * task. It is used set low or high priority at the current |
| 6362 | * task. |
| 6363 | */ |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 6364 | __LJMP static int hlua_set_nice(lua_State *L) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6365 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6366 | struct hlua *hlua; |
| 6367 | int nice; |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6368 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6369 | MAY_LJMP(check_args(L, 1, "set_nice")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6370 | nice = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6371 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 6372 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 6373 | hlua = hlua_gethlua(L); |
| 6374 | |
| 6375 | /* If the task is not set, I'm in a start mode. */ |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6376 | if (!hlua || !hlua->task) |
| 6377 | return 0; |
| 6378 | |
| 6379 | if (nice < -1024) |
| 6380 | nice = -1024; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6381 | else if (nice > 1024) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6382 | nice = 1024; |
| 6383 | |
| 6384 | hlua->task->nice = nice; |
| 6385 | return 0; |
| 6386 | } |
| 6387 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6388 | /* 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] | 6389 | * HAProxy task subsystem when the task is awaked. The LUA runtime can |
| 6390 | * return an E_AGAIN signal, the emmiter of this signal must set a |
| 6391 | * signal to wake the task. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6392 | * |
| 6393 | * Task wrapper are longjmp safe because the only one Lua code |
| 6394 | * executed is the safe hlua_ctx_resume(); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6395 | */ |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 6396 | 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] | 6397 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 6398 | struct hlua *hlua = context; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6399 | enum hlua_exec status; |
| 6400 | |
Christopher Faulet | 5bc9972 | 2018-04-25 10:34:45 +0200 | [diff] [blame] | 6401 | if (task->thread_mask == MAX_THREADS_MASK) |
| 6402 | task_set_affinity(task, tid_bit); |
| 6403 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6404 | /* If it is the first call to the task, we must initialize the |
| 6405 | * execution timeouts. |
| 6406 | */ |
| 6407 | if (!HLUA_IS_RUNNING(hlua)) |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 6408 | hlua->max_time = hlua_timeout_task; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6409 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6410 | /* Execute the Lua code. */ |
| 6411 | status = hlua_ctx_resume(hlua, 1); |
| 6412 | |
| 6413 | switch (status) { |
| 6414 | /* finished or yield */ |
| 6415 | case HLUA_E_OK: |
| 6416 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 6417 | task_destroy(task); |
Tim Duesterhus | cd235c6 | 2018-04-24 13:56:01 +0200 | [diff] [blame] | 6418 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6419 | break; |
| 6420 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6421 | case HLUA_E_AGAIN: /* co process or timeout wake me later. */ |
Thierry FOURNIER | cb14688 | 2017-12-10 17:10:57 +0100 | [diff] [blame] | 6422 | notification_gc(&hlua->com); |
PiBa-NL | fe971b3 | 2018-05-02 22:27:14 +0200 | [diff] [blame] | 6423 | task->expire = hlua->wake_time; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6424 | break; |
| 6425 | |
| 6426 | /* finished with error. */ |
| 6427 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6428 | SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1)); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6429 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 6430 | task_destroy(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 6431 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6432 | break; |
| 6433 | |
| 6434 | case HLUA_E_ERR: |
| 6435 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6436 | SEND_ERR(NULL, "Lua task: unknown error.\n"); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6437 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 6438 | task_destroy(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 6439 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6440 | break; |
| 6441 | } |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 6442 | return task; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6443 | } |
| 6444 | |
Aurelien DARRAGON | 4a7a5d8 | 2023-08-09 14:56:19 +0200 | [diff] [blame] | 6445 | /* Helper function to prepare the lua ctx for a given stream |
| 6446 | * |
Aurelien DARRAGON | 16226c8 | 2023-08-09 15:19:56 +0200 | [diff] [blame] | 6447 | * ctx will be enforced in <state_id> parent stack on initial creation. |
| 6448 | * If s->hlua->state_id differs from <state_id>, which may happen at |
| 6449 | * runtime since existing stream hlua ctx will be reused for other |
| 6450 | * "independent" (but stream-related) lua executions, hlua will be |
| 6451 | * recreated with the expected state id. |
Aurelien DARRAGON | 4a7a5d8 | 2023-08-09 14:56:19 +0200 | [diff] [blame] | 6452 | * |
| 6453 | * Returns 1 for success and 0 for failure |
| 6454 | */ |
| 6455 | static int hlua_stream_ctx_prepare(struct stream *s, int state_id) |
| 6456 | { |
| 6457 | /* In the execution wrappers linked with a stream, the |
| 6458 | * Lua context can be not initialized. This behavior |
| 6459 | * permits to save performances because a systematic |
| 6460 | * Lua initialization cause 5% performances loss. |
| 6461 | */ |
Aurelien DARRAGON | 16226c8 | 2023-08-09 15:19:56 +0200 | [diff] [blame] | 6462 | ctx_renew: |
Aurelien DARRAGON | 4a7a5d8 | 2023-08-09 14:56:19 +0200 | [diff] [blame] | 6463 | if (!s->hlua) { |
| 6464 | struct hlua *hlua; |
| 6465 | |
| 6466 | hlua = pool_alloc(pool_head_hlua); |
| 6467 | if (!hlua) |
| 6468 | return 0; |
| 6469 | HLUA_INIT(hlua); |
| 6470 | if (!hlua_ctx_init(hlua, state_id, s->task)) { |
| 6471 | pool_free(pool_head_hlua, hlua); |
| 6472 | return 0; |
| 6473 | } |
| 6474 | s->hlua = hlua; |
| 6475 | } |
Aurelien DARRAGON | 16226c8 | 2023-08-09 15:19:56 +0200 | [diff] [blame] | 6476 | else if (s->hlua->state_id != state_id) { |
| 6477 | /* ctx already created, but not in proper state. |
| 6478 | * It should only happen after the previous execution is |
| 6479 | * finished, otherwise it's probably a bug since we don't |
| 6480 | * want to abort unfinished job.. |
| 6481 | */ |
| 6482 | BUG_ON(HLUA_IS_RUNNING(s->hlua)); |
| 6483 | hlua_ctx_destroy(s->hlua); |
| 6484 | s->hlua = NULL; |
| 6485 | goto ctx_renew; |
| 6486 | } |
Aurelien DARRAGON | 4a7a5d8 | 2023-08-09 14:56:19 +0200 | [diff] [blame] | 6487 | return 1; |
| 6488 | } |
| 6489 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6490 | /* This function is an LUA binding that register LUA function to be |
| 6491 | * executed after the HAProxy configuration parsing and before the |
| 6492 | * HAProxy scheduler starts. This function expect only one LUA |
| 6493 | * argument that is a function. This function returns nothing, but |
| 6494 | * throws if an error is encountered. |
| 6495 | */ |
| 6496 | __LJMP static int hlua_register_init(lua_State *L) |
| 6497 | { |
| 6498 | struct hlua_init_function *init; |
| 6499 | int ref; |
| 6500 | |
| 6501 | MAY_LJMP(check_args(L, 1, "register_init")); |
| 6502 | |
Aurelien DARRAGON | 8985ab8 | 2023-02-24 09:43:54 +0100 | [diff] [blame] | 6503 | if (hlua_gethlua(L)) { |
| 6504 | /* runtime processing */ |
| 6505 | WILL_LJMP(luaL_error(L, "register_init: not available outside of body context")); |
| 6506 | } |
| 6507 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6508 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 6509 | |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6510 | init = calloc(1, sizeof(*init)); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6511 | if (!init) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6512 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6513 | |
| 6514 | init->function_ref = ref; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 6515 | LIST_APPEND(&hlua_init_functions[hlua_state_id], &init->l); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6516 | return 0; |
| 6517 | } |
| 6518 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6519 | /* This functio is an LUA binding. It permits to register a task |
| 6520 | * executed in parallel of the main HAroxy activity. The task is |
| 6521 | * created and it is set in the HAProxy scheduler. It can be called |
| 6522 | * from the "init" section, "post init" or during the runtime. |
| 6523 | * |
| 6524 | * Lua prototype: |
| 6525 | * |
| 6526 | * <none> core.register_task(<function>) |
| 6527 | */ |
| 6528 | static int hlua_register_task(lua_State *L) |
| 6529 | { |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 6530 | struct hlua *hlua = NULL; |
| 6531 | struct task *task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6532 | int ref; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 6533 | int state_id; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6534 | |
| 6535 | MAY_LJMP(check_args(L, 1, "register_task")); |
| 6536 | |
| 6537 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 6538 | |
Thierry Fournier | 75fc029 | 2020-11-28 13:18:56 +0100 | [diff] [blame] | 6539 | /* Get the reference state. If the reference is NULL, L is the master |
| 6540 | * state, otherwise hlua->T is. |
| 6541 | */ |
| 6542 | hlua = hlua_gethlua(L); |
| 6543 | if (hlua) |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 6544 | /* we are in runtime processing */ |
| 6545 | state_id = hlua->state_id; |
Thierry Fournier | 75fc029 | 2020-11-28 13:18:56 +0100 | [diff] [blame] | 6546 | else |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 6547 | /* we are in initialization mode */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 6548 | state_id = hlua_state_id; |
Thierry Fournier | 75fc029 | 2020-11-28 13:18:56 +0100 | [diff] [blame] | 6549 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6550 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6551 | if (!hlua) |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 6552 | goto alloc_error; |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 6553 | HLUA_INIT(hlua); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6554 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 6555 | /* We are in the common lua state, execute the task anywhere, |
| 6556 | * otherwise, inherit the current thread identifier |
| 6557 | */ |
| 6558 | if (state_id == 0) |
| 6559 | task = task_new(MAX_THREADS_MASK); |
| 6560 | else |
| 6561 | task = task_new(tid_bit); |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 6562 | if (!task) |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 6563 | goto alloc_error; |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 6564 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6565 | task->context = hlua; |
| 6566 | task->process = hlua_process_task; |
| 6567 | |
Aurelien DARRAGON | 5483bde | 2023-03-21 14:02:16 +0100 | [diff] [blame] | 6568 | if (!hlua_ctx_init(hlua, state_id, task)) |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 6569 | goto alloc_error; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6570 | |
| 6571 | /* Restore the function in the stack. */ |
| 6572 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref); |
Aurelien DARRAGON | b6aade3 | 2023-03-13 14:09:21 +0100 | [diff] [blame] | 6573 | /* function ref not needed anymore since it was pushed to the substack */ |
| 6574 | hlua_unref(L, ref); |
| 6575 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6576 | hlua->nargs = 0; |
| 6577 | |
| 6578 | /* Schedule task. */ |
Willy Tarreau | 790810f | 2021-09-30 16:17:37 +0200 | [diff] [blame] | 6579 | task_wakeup(task, TASK_WOKEN_INIT); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6580 | |
| 6581 | return 0; |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 6582 | |
| 6583 | alloc_error: |
| 6584 | task_destroy(task); |
| 6585 | hlua_ctx_destroy(hlua); |
| 6586 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 6587 | return 0; /* Never reached */ |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6588 | } |
| 6589 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6590 | /* Wrapper called by HAProxy to execute an LUA converter. This wrapper |
| 6591 | * doesn't allow "yield" functions because the HAProxy engine cannot |
| 6592 | * resume converters. |
| 6593 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6594 | 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] | 6595 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 6596 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6597 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6598 | const char *error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6599 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6600 | if (!stream) |
| 6601 | return 0; |
| 6602 | |
Aurelien DARRAGON | 4a7a5d8 | 2023-08-09 14:56:19 +0200 | [diff] [blame] | 6603 | if (!hlua_stream_ctx_prepare(stream, fcn_ref_to_stack_id(fcn))) { |
| 6604 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 6605 | return 0; |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6606 | } |
| 6607 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6608 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6609 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6610 | |
| 6611 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 6612 | if (!SET_SAFE_LJMP(stream->hlua)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6613 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 6614 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6615 | else |
| 6616 | error = "critical error"; |
| 6617 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6618 | return 0; |
| 6619 | } |
| 6620 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6621 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6622 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6623 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 6624 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6625 | return 0; |
| 6626 | } |
| 6627 | |
| 6628 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 6629 | lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref[stream->hlua->state_id]); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6630 | |
| 6631 | /* convert input sample and pust-it in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6632 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6633 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 6634 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6635 | return 0; |
| 6636 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6637 | hlua_smp2lua(stream->hlua->T, smp); |
| 6638 | stream->hlua->nargs = 1; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6639 | |
| 6640 | /* push keywords in the stack. */ |
| 6641 | if (arg_p) { |
| 6642 | for (; arg_p->type != ARGT_STOP; arg_p++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6643 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6644 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 6645 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6646 | return 0; |
| 6647 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6648 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 6649 | stream->hlua->nargs++; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6650 | } |
| 6651 | } |
| 6652 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6653 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6654 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6655 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6656 | /* At this point the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 6657 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6658 | } |
| 6659 | |
| 6660 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6661 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6662 | /* finished. */ |
| 6663 | case HLUA_E_OK: |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 6664 | /* If the stack is empty, the function fails. */ |
| 6665 | if (lua_gettop(stream->hlua->T) <= 0) |
| 6666 | return 0; |
| 6667 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6668 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6669 | hlua_lua2smp(stream->hlua->T, -1, smp); |
Aurelien DARRAGON | d39fb74 | 2023-05-17 16:06:11 +0200 | [diff] [blame] | 6670 | /* dup the smp before popping the related lua value and |
| 6671 | * returning it to haproxy |
| 6672 | */ |
| 6673 | smp_dup(smp); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6674 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6675 | return 1; |
| 6676 | |
| 6677 | /* yield. */ |
| 6678 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6679 | 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] | 6680 | return 0; |
| 6681 | |
| 6682 | /* finished with error. */ |
| 6683 | case HLUA_E_ERRMSG: |
| 6684 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6685 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6686 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 6687 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6688 | return 0; |
| 6689 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6690 | case HLUA_E_ETMOUT: |
| 6691 | SEND_ERR(stream->be, "Lua converter '%s': execution timeout.\n", fcn->name); |
| 6692 | return 0; |
| 6693 | |
| 6694 | case HLUA_E_NOMEM: |
| 6695 | SEND_ERR(stream->be, "Lua converter '%s': out of memory error.\n", fcn->name); |
| 6696 | return 0; |
| 6697 | |
| 6698 | case HLUA_E_YIELD: |
| 6699 | SEND_ERR(stream->be, "Lua converter '%s': yield functions like core.tcp() or core.sleep() are not allowed.\n", fcn->name); |
| 6700 | return 0; |
| 6701 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6702 | case HLUA_E_ERR: |
| 6703 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6704 | 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] | 6705 | /* fall through */ |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6706 | |
| 6707 | default: |
| 6708 | return 0; |
| 6709 | } |
| 6710 | } |
| 6711 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6712 | /* Wrapper called by HAProxy to execute a sample-fetch. this wrapper |
| 6713 | * doesn't allow "yield" functions because the HAProxy engine cannot |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6714 | * resume sample-fetches. This function will be called by the sample |
| 6715 | * fetch engine to call lua-based fetch operations. |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6716 | */ |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6717 | static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp, |
| 6718 | const char *kw, void *private) |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6719 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 6720 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6721 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6722 | const char *error; |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6723 | unsigned int hflags = HLUA_TXN_NOTERM; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6724 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6725 | if (!stream) |
| 6726 | return 0; |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 6727 | |
Aurelien DARRAGON | 4a7a5d8 | 2023-08-09 14:56:19 +0200 | [diff] [blame] | 6728 | if (!hlua_stream_ctx_prepare(stream, fcn_ref_to_stack_id(fcn))) { |
| 6729 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 6730 | return 0; |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6731 | } |
| 6732 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6733 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6734 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6735 | |
| 6736 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 6737 | if (!SET_SAFE_LJMP(stream->hlua)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6738 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 6739 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6740 | else |
| 6741 | error = "critical error"; |
| 6742 | SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", fcn->name, error); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6743 | return 0; |
| 6744 | } |
| 6745 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6746 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6747 | if (!lua_checkstack(stream->hlua->T, 2)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6748 | SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 6749 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6750 | return 0; |
| 6751 | } |
| 6752 | |
| 6753 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 6754 | lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref[stream->hlua->state_id]); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6755 | |
| 6756 | /* push arguments in the stack. */ |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6757 | if (!hlua_txn_new(stream->hlua->T, stream, smp->px, smp->opt & SMP_OPT_DIR, hflags)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6758 | SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 6759 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6760 | return 0; |
| 6761 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6762 | stream->hlua->nargs = 1; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6763 | |
| 6764 | /* push keywords in the stack. */ |
| 6765 | for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) { |
| 6766 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6767 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6768 | SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 6769 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6770 | return 0; |
| 6771 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6772 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 6773 | stream->hlua->nargs++; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6774 | } |
| 6775 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6776 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6777 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6778 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6779 | /* At this point the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 6780 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6781 | } |
| 6782 | |
| 6783 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6784 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6785 | /* finished. */ |
| 6786 | case HLUA_E_OK: |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 6787 | /* If the stack is empty, the function fails. */ |
| 6788 | if (lua_gettop(stream->hlua->T) <= 0) |
| 6789 | return 0; |
| 6790 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6791 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6792 | hlua_lua2smp(stream->hlua->T, -1, smp); |
Aurelien DARRAGON | d39fb74 | 2023-05-17 16:06:11 +0200 | [diff] [blame] | 6793 | /* dup the smp before popping the related lua value and |
| 6794 | * returning it to haproxy |
| 6795 | */ |
| 6796 | smp_dup(smp); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6797 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6798 | |
| 6799 | /* Set the end of execution flag. */ |
| 6800 | smp->flags &= ~SMP_F_MAY_CHANGE; |
| 6801 | return 1; |
| 6802 | |
| 6803 | /* yield. */ |
| 6804 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6805 | 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] | 6806 | return 0; |
| 6807 | |
| 6808 | /* finished with error. */ |
| 6809 | case HLUA_E_ERRMSG: |
| 6810 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6811 | SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6812 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 6813 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6814 | return 0; |
| 6815 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6816 | case HLUA_E_ETMOUT: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6817 | SEND_ERR(smp->px, "Lua sample-fetch '%s': execution timeout.\n", fcn->name); |
| 6818 | return 0; |
| 6819 | |
| 6820 | case HLUA_E_NOMEM: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6821 | SEND_ERR(smp->px, "Lua sample-fetch '%s': out of memory error.\n", fcn->name); |
| 6822 | return 0; |
| 6823 | |
| 6824 | case HLUA_E_YIELD: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6825 | SEND_ERR(smp->px, "Lua sample-fetch '%s': yield not allowed.\n", fcn->name); |
| 6826 | return 0; |
| 6827 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6828 | case HLUA_E_ERR: |
| 6829 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6830 | 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] | 6831 | /* fall through */ |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6832 | |
| 6833 | default: |
| 6834 | return 0; |
| 6835 | } |
| 6836 | } |
| 6837 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6838 | /* This function is an LUA binding used for registering |
| 6839 | * "sample-conv" functions. It expects a converter name used |
| 6840 | * in the haproxy configuration file, and an LUA function. |
| 6841 | */ |
| 6842 | __LJMP static int hlua_register_converters(lua_State *L) |
| 6843 | { |
| 6844 | struct sample_conv_kw_list *sck; |
| 6845 | const char *name; |
| 6846 | int ref; |
| 6847 | int len; |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 6848 | struct hlua_function *fcn = NULL; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 6849 | struct sample_conv *sc; |
| 6850 | struct buffer *trash; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6851 | |
| 6852 | MAY_LJMP(check_args(L, 2, "register_converters")); |
| 6853 | |
Aurelien DARRAGON | 8985ab8 | 2023-02-24 09:43:54 +0100 | [diff] [blame] | 6854 | if (hlua_gethlua(L)) { |
| 6855 | /* runtime processing */ |
| 6856 | WILL_LJMP(luaL_error(L, "register_converters: not available outside of body context")); |
| 6857 | } |
| 6858 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6859 | /* First argument : converter name. */ |
| 6860 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6861 | |
| 6862 | /* Second argument : lua function. */ |
| 6863 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 6864 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 6865 | /* Check if the converter is already registered */ |
| 6866 | trash = get_trash_chunk(); |
| 6867 | chunk_printf(trash, "lua.%s", name); |
| 6868 | sc = find_sample_conv(trash->area, trash->data); |
| 6869 | if (sc != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 6870 | fcn = sc->private; |
| 6871 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 6872 | ha_warning("Trying to register converter 'lua.%s' more than once. " |
| 6873 | "This will become a hard error in version 2.5.\n", name); |
| 6874 | } |
| 6875 | fcn->function_ref[hlua_state_id] = ref; |
| 6876 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 6877 | } |
| 6878 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6879 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6880 | sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6881 | if (!sck) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 6882 | goto alloc_error; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 6883 | fcn = new_hlua_function(); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6884 | if (!fcn) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 6885 | goto alloc_error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6886 | |
| 6887 | /* Fill fcn. */ |
| 6888 | fcn->name = strdup(name); |
| 6889 | if (!fcn->name) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 6890 | goto alloc_error; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 6891 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6892 | |
| 6893 | /* List head */ |
| 6894 | sck->list.n = sck->list.p = NULL; |
| 6895 | |
| 6896 | /* converter keyword. */ |
| 6897 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6898 | sck->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6899 | if (!sck->kw[0].kw) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 6900 | goto alloc_error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6901 | |
| 6902 | snprintf((char *)sck->kw[0].kw, len, "lua.%s", name); |
| 6903 | sck->kw[0].process = hlua_sample_conv_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 6904 | 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] | 6905 | sck->kw[0].val_args = NULL; |
| 6906 | sck->kw[0].in_type = SMP_T_STR; |
| 6907 | sck->kw[0].out_type = SMP_T_STR; |
| 6908 | sck->kw[0].private = fcn; |
| 6909 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6910 | /* Register this new converter */ |
| 6911 | sample_register_convs(sck); |
| 6912 | |
| 6913 | return 0; |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 6914 | |
| 6915 | alloc_error: |
| 6916 | release_hlua_function(fcn); |
| 6917 | ha_free(&sck); |
| 6918 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 6919 | return 0; /* Never reached */ |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6920 | } |
| 6921 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6922 | /* This function is an LUA binding used for registering |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6923 | * "sample-fetch" functions. It expects a converter name used |
| 6924 | * in the haproxy configuration file, and an LUA function. |
| 6925 | */ |
| 6926 | __LJMP static int hlua_register_fetches(lua_State *L) |
| 6927 | { |
| 6928 | const char *name; |
| 6929 | int ref; |
| 6930 | int len; |
| 6931 | struct sample_fetch_kw_list *sfk; |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 6932 | struct hlua_function *fcn = NULL; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 6933 | struct sample_fetch *sf; |
| 6934 | struct buffer *trash; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6935 | |
| 6936 | MAY_LJMP(check_args(L, 2, "register_fetches")); |
| 6937 | |
Aurelien DARRAGON | 8985ab8 | 2023-02-24 09:43:54 +0100 | [diff] [blame] | 6938 | if (hlua_gethlua(L)) { |
| 6939 | /* runtime processing */ |
| 6940 | WILL_LJMP(luaL_error(L, "register_fetches: not available outside of body context")); |
| 6941 | } |
| 6942 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6943 | /* First argument : sample-fetch name. */ |
| 6944 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6945 | |
| 6946 | /* Second argument : lua function. */ |
| 6947 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 6948 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 6949 | /* Check if the sample-fetch is already registered */ |
| 6950 | trash = get_trash_chunk(); |
| 6951 | chunk_printf(trash, "lua.%s", name); |
| 6952 | sf = find_sample_fetch(trash->area, trash->data); |
| 6953 | if (sf != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 6954 | fcn = sf->private; |
| 6955 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 6956 | ha_warning("Trying to register sample-fetch 'lua.%s' more than once. " |
| 6957 | "This will become a hard error in version 2.5.\n", name); |
| 6958 | } |
| 6959 | fcn->function_ref[hlua_state_id] = ref; |
| 6960 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 6961 | } |
| 6962 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6963 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6964 | sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6965 | if (!sfk) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 6966 | goto alloc_error; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 6967 | fcn = new_hlua_function(); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6968 | if (!fcn) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 6969 | goto alloc_error; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6970 | |
| 6971 | /* Fill fcn. */ |
| 6972 | fcn->name = strdup(name); |
| 6973 | if (!fcn->name) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 6974 | goto alloc_error; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 6975 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6976 | |
| 6977 | /* List head */ |
| 6978 | sfk->list.n = sfk->list.p = NULL; |
| 6979 | |
| 6980 | /* sample-fetch keyword. */ |
| 6981 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6982 | sfk->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6983 | if (!sfk->kw[0].kw) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 6984 | goto alloc_error; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6985 | |
| 6986 | snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name); |
| 6987 | sfk->kw[0].process = hlua_sample_fetch_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 6988 | 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] | 6989 | sfk->kw[0].val_args = NULL; |
| 6990 | sfk->kw[0].out_type = SMP_T_STR; |
| 6991 | sfk->kw[0].use = SMP_USE_HTTP_ANY; |
| 6992 | sfk->kw[0].val = 0; |
| 6993 | sfk->kw[0].private = fcn; |
| 6994 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6995 | /* Register this new fetch. */ |
| 6996 | sample_register_fetches(sfk); |
| 6997 | |
| 6998 | return 0; |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 6999 | |
| 7000 | alloc_error: |
| 7001 | release_hlua_function(fcn); |
| 7002 | ha_free(&sfk); |
| 7003 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 7004 | return 0; /* Never reached */ |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 7005 | } |
| 7006 | |
Christopher Faulet | 501465d | 2020-02-26 14:54:16 +0100 | [diff] [blame] | 7007 | /* This function is a lua binding to set the wake_time. |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 7008 | */ |
Christopher Faulet | 501465d | 2020-02-26 14:54:16 +0100 | [diff] [blame] | 7009 | __LJMP static int hlua_set_wake_time(lua_State *L) |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 7010 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 7011 | struct hlua *hlua; |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 7012 | unsigned int delay; |
| 7013 | unsigned int wakeup_ms; |
| 7014 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 7015 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 7016 | hlua = hlua_gethlua(L); |
| 7017 | if (!hlua) { |
| 7018 | return 0; |
| 7019 | } |
| 7020 | |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 7021 | MAY_LJMP(check_args(L, 1, "wake_time")); |
| 7022 | |
| 7023 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 7024 | wakeup_ms = tick_add(now_ms, delay); |
| 7025 | hlua->wake_time = wakeup_ms; |
| 7026 | return 0; |
| 7027 | } |
| 7028 | |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7029 | /* This function is a wrapper to execute each LUA function declared as an action |
| 7030 | * wrapper during the initialisation period. This function may return any |
| 7031 | * ACT_RET_* value. On error ACT_RET_CONT is returned and the action is |
| 7032 | * ignored. If the lua action yields, ACT_RET_YIELD is returned. On success, the |
| 7033 | * return value is the first element on the stack. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7034 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7035 | 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] | 7036 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7037 | { |
| 7038 | char **arg; |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 7039 | unsigned int hflags = 0; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7040 | int dir, act_ret = ACT_RET_CONT; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7041 | const char *error; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7042 | |
| 7043 | switch (rule->from) { |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 7044 | case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break; |
| 7045 | case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break; |
| 7046 | case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break; |
| 7047 | case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7048 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 7049 | SEND_ERR(px, "Lua: internal error while execute action.\n"); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7050 | goto end; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7051 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7052 | |
Aurelien DARRAGON | 4a7a5d8 | 2023-08-09 14:56:19 +0200 | [diff] [blame] | 7053 | if (!hlua_stream_ctx_prepare(s, fcn_ref_to_stack_id(rule->arg.hlua_rule->fcn))) { |
| 7054 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
| 7055 | rule->arg.hlua_rule->fcn->name); |
| 7056 | goto end; |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 7057 | } |
| 7058 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7059 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 7060 | if (!HLUA_IS_RUNNING(s->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7061 | |
| 7062 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7063 | if (!SET_SAFE_LJMP(s->hlua)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 7064 | if (lua_type(s->hlua->T, -1) == LUA_TSTRING) |
| 7065 | error = lua_tostring(s->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7066 | else |
| 7067 | error = "critical error"; |
| 7068 | SEND_ERR(px, "Lua function '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7069 | rule->arg.hlua_rule->fcn->name, error); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7070 | goto end; |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7071 | } |
| 7072 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7073 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 7074 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 7075 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7076 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7077 | RESET_SAFE_LJMP(s->hlua); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7078 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7079 | } |
| 7080 | |
| 7081 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 7082 | lua_rawgeti(s->hlua->T, LUA_REGISTRYINDEX, rule->arg.hlua_rule->fcn->function_ref[s->hlua->state_id]); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7083 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7084 | /* Create and and push object stream in the stack. */ |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 7085 | if (!hlua_txn_new(s->hlua->T, s, px, dir, hflags)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 7086 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7087 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7088 | RESET_SAFE_LJMP(s->hlua); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7089 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7090 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 7091 | s->hlua->nargs = 1; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7092 | |
| 7093 | /* push keywords in the stack. */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7094 | for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 7095 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 7096 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7097 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7098 | RESET_SAFE_LJMP(s->hlua); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7099 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7100 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 7101 | lua_pushstring(s->hlua->T, *arg); |
| 7102 | s->hlua->nargs++; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7103 | } |
| 7104 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7105 | /* Now the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7106 | RESET_SAFE_LJMP(s->hlua); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7107 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 7108 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 7109 | s->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7110 | } |
| 7111 | |
| 7112 | /* Execute the function. */ |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 7113 | switch (hlua_ctx_resume(s->hlua, !(flags & ACT_OPT_FINAL))) { |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7114 | /* finished. */ |
| 7115 | case HLUA_E_OK: |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7116 | /* Catch the return value */ |
| 7117 | if (lua_gettop(s->hlua->T) > 0) |
| 7118 | act_ret = lua_tointeger(s->hlua->T, -1); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7119 | |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 7120 | /* Set timeout in the required channel. */ |
Christopher Faulet | 498c483 | 2020-07-28 11:59:58 +0200 | [diff] [blame] | 7121 | if (act_ret == ACT_RET_YIELD) { |
| 7122 | if (flags & ACT_OPT_FINAL) |
| 7123 | goto err_yield; |
| 7124 | |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 7125 | if (dir == SMP_OPT_DIR_REQ) |
| 7126 | s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp), |
| 7127 | s->hlua->wake_time); |
| 7128 | else |
| 7129 | s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp), |
| 7130 | s->hlua->wake_time); |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 7131 | } |
| 7132 | goto end; |
| 7133 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7134 | /* yield. */ |
| 7135 | case HLUA_E_AGAIN: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 7136 | /* Set timeout in the required channel. */ |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 7137 | if (dir == SMP_OPT_DIR_REQ) |
| 7138 | s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp), |
| 7139 | s->hlua->wake_time); |
| 7140 | else |
| 7141 | s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp), |
| 7142 | s->hlua->wake_time); |
| 7143 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7144 | /* Some actions can be wake up when a "write" event |
| 7145 | * is detected on a response channel. This is useful |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7146 | * only for actions targeted on the requests. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7147 | */ |
Christopher Faulet | 51fa358 | 2019-07-26 14:54:52 +0200 | [diff] [blame] | 7148 | if (HLUA_IS_WAKERESWR(s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 7149 | s->res.flags |= CF_WAKE_WRITE; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 7150 | if (HLUA_IS_WAKEREQWR(s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 7151 | s->req.flags |= CF_WAKE_WRITE; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7152 | act_ret = ACT_RET_YIELD; |
| 7153 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7154 | |
| 7155 | /* finished with error. */ |
| 7156 | case HLUA_E_ERRMSG: |
| 7157 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 7158 | SEND_ERR(px, "Lua function '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7159 | rule->arg.hlua_rule->fcn->name, lua_tostring(s->hlua->T, -1)); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 7160 | lua_pop(s->hlua->T, 1); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7161 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7162 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7163 | case HLUA_E_ETMOUT: |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7164 | 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] | 7165 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7166 | |
| 7167 | case HLUA_E_NOMEM: |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7168 | 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] | 7169 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7170 | |
| 7171 | case HLUA_E_YIELD: |
Christopher Faulet | 498c483 | 2020-07-28 11:59:58 +0200 | [diff] [blame] | 7172 | err_yield: |
| 7173 | act_ret = ACT_RET_CONT; |
Aurelien DARRAGON | 602c4af | 2023-08-31 21:45:21 +0200 | [diff] [blame] | 7174 | SEND_ERR(px, "Lua function '%s': yield not allowed.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7175 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7176 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7177 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7178 | case HLUA_E_ERR: |
| 7179 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 7180 | SEND_ERR(px, "Lua function '%s' return an unknown error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7181 | rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7182 | |
| 7183 | default: |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7184 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7185 | } |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7186 | |
| 7187 | end: |
Christopher Faulet | 2361fd9 | 2020-07-30 10:40:58 +0200 | [diff] [blame] | 7188 | if (act_ret != ACT_RET_YIELD && s->hlua) |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 7189 | s->hlua->wake_time = TICK_ETERNITY; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7190 | return act_ret; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7191 | } |
| 7192 | |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 7193 | 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] | 7194 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 7195 | struct appctx *ctx = context; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7196 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7197 | appctx_wakeup(ctx); |
Willy Tarreau | d958741 | 2017-08-23 16:07:33 +0200 | [diff] [blame] | 7198 | t->expire = TICK_ETERNITY; |
Willy Tarreau | d1aa41f | 2017-07-21 16:41:56 +0200 | [diff] [blame] | 7199 | return t; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7200 | } |
| 7201 | |
| 7202 | static int hlua_applet_tcp_init(struct appctx *ctx, struct proxy *px, struct stream *strm) |
| 7203 | { |
| 7204 | struct stream_interface *si = ctx->owner; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7205 | struct hlua *hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7206 | struct task *task; |
| 7207 | char **arg; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7208 | const char *error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7209 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7210 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7211 | if (!hlua) { |
| 7212 | SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7213 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7214 | return 0; |
| 7215 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7216 | HLUA_INIT(hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7217 | ctx->ctx.hlua_apptcp.hlua = hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7218 | ctx->ctx.hlua_apptcp.flags = 0; |
| 7219 | |
| 7220 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 7221 | task = task_new(tid_bit); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7222 | if (!task) { |
| 7223 | SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7224 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7225 | return 0; |
| 7226 | } |
| 7227 | task->nice = 0; |
| 7228 | task->context = ctx; |
| 7229 | task->process = hlua_applet_wakeup; |
| 7230 | ctx->ctx.hlua_apptcp.task = task; |
| 7231 | |
| 7232 | /* In the execution wrappers linked with a stream, the |
| 7233 | * Lua context can be not initialized. This behavior |
| 7234 | * permits to save performances because a systematic |
| 7235 | * Lua initialization cause 5% performances loss. |
| 7236 | */ |
Aurelien DARRAGON | 5483bde | 2023-03-21 14:02:16 +0100 | [diff] [blame] | 7237 | 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] | 7238 | 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] | 7239 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7240 | return 0; |
| 7241 | } |
| 7242 | |
| 7243 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 7244 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7245 | |
| 7246 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7247 | if (!SET_SAFE_LJMP(hlua)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7248 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 7249 | error = lua_tostring(hlua->T, -1); |
| 7250 | else |
| 7251 | error = "critical error"; |
| 7252 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7253 | ctx->rule->arg.hlua_rule->fcn->name, error); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7254 | return 0; |
| 7255 | } |
| 7256 | |
| 7257 | /* Check stack available size. */ |
| 7258 | if (!lua_checkstack(hlua->T, 1)) { |
| 7259 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7260 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7261 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7262 | return 0; |
| 7263 | } |
| 7264 | |
| 7265 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 7266 | 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] | 7267 | |
| 7268 | /* Create and and push object stream in the stack. */ |
| 7269 | if (!hlua_applet_tcp_new(hlua->T, ctx)) { |
| 7270 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7271 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7272 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7273 | return 0; |
| 7274 | } |
| 7275 | hlua->nargs = 1; |
| 7276 | |
| 7277 | /* push keywords in the stack. */ |
| 7278 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 7279 | if (!lua_checkstack(hlua->T, 1)) { |
| 7280 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7281 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7282 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7283 | return 0; |
| 7284 | } |
| 7285 | lua_pushstring(hlua->T, *arg); |
| 7286 | hlua->nargs++; |
| 7287 | } |
| 7288 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7289 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7290 | |
| 7291 | /* Wakeup the applet ASAP. */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 7292 | si_cant_get(si); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 7293 | si_rx_endp_more(si); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7294 | |
| 7295 | return 1; |
| 7296 | } |
| 7297 | |
Willy Tarreau | 60409db | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 7298 | void hlua_applet_tcp_fct(struct appctx *ctx) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7299 | { |
| 7300 | struct stream_interface *si = ctx->owner; |
| 7301 | struct stream *strm = si_strm(si); |
| 7302 | struct channel *res = si_ic(si); |
| 7303 | struct act_rule *rule = ctx->rule; |
| 7304 | struct proxy *px = strm->be; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7305 | struct hlua *hlua = ctx->ctx.hlua_apptcp.hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7306 | |
| 7307 | /* The applet execution is already done. */ |
Olivier Houchard | 594c8c5 | 2018-08-28 14:41:31 +0200 | [diff] [blame] | 7308 | if (ctx->ctx.hlua_apptcp.flags & APPLET_DONE) { |
| 7309 | /* eat the whole request */ |
| 7310 | co_skip(si_oc(si), co_data(si_oc(si))); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7311 | return; |
Olivier Houchard | 594c8c5 | 2018-08-28 14:41:31 +0200 | [diff] [blame] | 7312 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7313 | |
| 7314 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 7315 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 7316 | return; |
| 7317 | |
| 7318 | /* Execute the function. */ |
| 7319 | switch (hlua_ctx_resume(hlua, 1)) { |
| 7320 | /* finished. */ |
| 7321 | case HLUA_E_OK: |
| 7322 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 7323 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7324 | /* eat the whole request */ |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 7325 | co_skip(si_oc(si), co_data(si_oc(si))); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7326 | res->flags |= CF_READ_NULL; |
| 7327 | si_shutr(si); |
| 7328 | return; |
| 7329 | |
| 7330 | /* yield. */ |
| 7331 | case HLUA_E_AGAIN: |
Thierry Fournier | 0164f20 | 2016-02-20 17:47:43 +0100 | [diff] [blame] | 7332 | if (hlua->wake_time != TICK_ETERNITY) |
| 7333 | task_schedule(ctx->ctx.hlua_apptcp.task, hlua->wake_time); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7334 | return; |
| 7335 | |
| 7336 | /* finished with error. */ |
| 7337 | case HLUA_E_ERRMSG: |
| 7338 | /* Display log. */ |
| 7339 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7340 | rule->arg.hlua_rule->fcn->name, lua_tostring(hlua->T, -1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7341 | lua_pop(hlua->T, 1); |
| 7342 | goto error; |
| 7343 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7344 | case HLUA_E_ETMOUT: |
| 7345 | SEND_ERR(px, "Lua applet tcp '%s': execution timeout.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7346 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7347 | goto error; |
| 7348 | |
| 7349 | case HLUA_E_NOMEM: |
| 7350 | SEND_ERR(px, "Lua applet tcp '%s': out of memory error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7351 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7352 | goto error; |
| 7353 | |
| 7354 | case HLUA_E_YIELD: /* unexpected */ |
| 7355 | SEND_ERR(px, "Lua applet tcp '%s': yield not allowed.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7356 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7357 | goto error; |
| 7358 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7359 | case HLUA_E_ERR: |
| 7360 | /* Display log. */ |
| 7361 | SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7362 | rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7363 | goto error; |
| 7364 | |
| 7365 | default: |
| 7366 | goto error; |
| 7367 | } |
| 7368 | |
| 7369 | error: |
| 7370 | |
| 7371 | /* For all other cases, just close the stream. */ |
| 7372 | si_shutw(si); |
| 7373 | si_shutr(si); |
| 7374 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 7375 | } |
| 7376 | |
| 7377 | static void hlua_applet_tcp_release(struct appctx *ctx) |
| 7378 | { |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 7379 | task_destroy(ctx->ctx.hlua_apptcp.task); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7380 | ctx->ctx.hlua_apptcp.task = NULL; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7381 | hlua_ctx_destroy(ctx->ctx.hlua_apptcp.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7382 | ctx->ctx.hlua_apptcp.hlua = NULL; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7383 | } |
| 7384 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7385 | /* The function returns 1 if the initialisation is complete, 0 if |
| 7386 | * an errors occurs and -1 if more data are required for initializing |
| 7387 | * the applet. |
| 7388 | */ |
| 7389 | static int hlua_applet_http_init(struct appctx *ctx, struct proxy *px, struct stream *strm) |
| 7390 | { |
| 7391 | struct stream_interface *si = ctx->owner; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7392 | struct http_txn *txn; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7393 | struct hlua *hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7394 | char **arg; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7395 | struct task *task; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7396 | const char *error; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7397 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7398 | txn = strm->txn; |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7399 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7400 | if (!hlua) { |
| 7401 | SEND_ERR(px, "Lua applet http '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7402 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7403 | return 0; |
| 7404 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7405 | HLUA_INIT(hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7406 | ctx->ctx.hlua_apphttp.hlua = hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7407 | ctx->ctx.hlua_apphttp.left_bytes = -1; |
| 7408 | ctx->ctx.hlua_apphttp.flags = 0; |
| 7409 | |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 7410 | if (txn->req.flags & HTTP_MSGF_VER_11) |
| 7411 | ctx->ctx.hlua_apphttp.flags |= APPLET_HTTP11; |
| 7412 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7413 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 7414 | task = task_new(tid_bit); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7415 | if (!task) { |
| 7416 | SEND_ERR(px, "Lua applet http '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7417 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7418 | return 0; |
| 7419 | } |
| 7420 | task->nice = 0; |
| 7421 | task->context = ctx; |
| 7422 | task->process = hlua_applet_wakeup; |
| 7423 | ctx->ctx.hlua_apphttp.task = task; |
| 7424 | |
| 7425 | /* In the execution wrappers linked with a stream, the |
| 7426 | * Lua context can be not initialized. This behavior |
| 7427 | * permits to save performances because a systematic |
| 7428 | * Lua initialization cause 5% performances loss. |
| 7429 | */ |
Aurelien DARRAGON | 5483bde | 2023-03-21 14:02:16 +0100 | [diff] [blame] | 7430 | 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] | 7431 | 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] | 7432 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7433 | return 0; |
| 7434 | } |
| 7435 | |
| 7436 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 7437 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7438 | |
| 7439 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7440 | if (!SET_SAFE_LJMP(hlua)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7441 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 7442 | error = lua_tostring(hlua->T, -1); |
| 7443 | else |
| 7444 | error = "critical error"; |
| 7445 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7446 | ctx->rule->arg.hlua_rule->fcn->name, error); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7447 | return 0; |
| 7448 | } |
| 7449 | |
| 7450 | /* Check stack available size. */ |
| 7451 | if (!lua_checkstack(hlua->T, 1)) { |
| 7452 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7453 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7454 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7455 | return 0; |
| 7456 | } |
| 7457 | |
| 7458 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 7459 | 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] | 7460 | |
| 7461 | /* Create and and push object stream in the stack. */ |
| 7462 | if (!hlua_applet_http_new(hlua->T, ctx)) { |
| 7463 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7464 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7465 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7466 | return 0; |
| 7467 | } |
| 7468 | hlua->nargs = 1; |
| 7469 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7470 | /* push keywords in the stack. */ |
| 7471 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 7472 | if (!lua_checkstack(hlua->T, 1)) { |
| 7473 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7474 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7475 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7476 | return 0; |
| 7477 | } |
| 7478 | lua_pushstring(hlua->T, *arg); |
| 7479 | hlua->nargs++; |
| 7480 | } |
| 7481 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7482 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7483 | |
| 7484 | /* Wakeup the applet when data is ready for read. */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 7485 | si_cant_get(si); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7486 | |
| 7487 | return 1; |
| 7488 | } |
| 7489 | |
Willy Tarreau | 60409db | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 7490 | void hlua_applet_http_fct(struct appctx *ctx) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7491 | { |
| 7492 | struct stream_interface *si = ctx->owner; |
| 7493 | struct stream *strm = si_strm(si); |
| 7494 | struct channel *req = si_oc(si); |
| 7495 | struct channel *res = si_ic(si); |
| 7496 | struct act_rule *rule = ctx->rule; |
| 7497 | struct proxy *px = strm->be; |
| 7498 | struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua; |
| 7499 | struct htx *req_htx, *res_htx; |
| 7500 | |
| 7501 | res_htx = htx_from_buf(&res->buf); |
| 7502 | |
| 7503 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 7504 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 7505 | goto out; |
| 7506 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 7507 | /* Check if the input buffer is available. */ |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7508 | if (!b_size(&res->buf)) { |
| 7509 | si_rx_room_blk(si); |
| 7510 | goto out; |
| 7511 | } |
| 7512 | /* check that the output is not closed */ |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7513 | if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR)) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7514 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 7515 | |
| 7516 | /* Set the currently running flag. */ |
| 7517 | if (!HLUA_IS_RUNNING(hlua) && |
| 7518 | !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
Christopher Faulet | bd878d2 | 2021-04-28 10:50:21 +0200 | [diff] [blame] | 7519 | if (!co_data(req)) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7520 | si_cant_get(si); |
| 7521 | goto out; |
| 7522 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7523 | } |
| 7524 | |
| 7525 | /* Executes The applet if it is not done. */ |
| 7526 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
| 7527 | |
| 7528 | /* Execute the function. */ |
| 7529 | switch (hlua_ctx_resume(hlua, 1)) { |
| 7530 | /* finished. */ |
| 7531 | case HLUA_E_OK: |
| 7532 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 7533 | break; |
| 7534 | |
| 7535 | /* yield. */ |
| 7536 | case HLUA_E_AGAIN: |
| 7537 | if (hlua->wake_time != TICK_ETERNITY) |
| 7538 | task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time); |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7539 | goto out; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7540 | |
| 7541 | /* finished with error. */ |
| 7542 | case HLUA_E_ERRMSG: |
| 7543 | /* Display log. */ |
| 7544 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7545 | rule->arg.hlua_rule->fcn->name, lua_tostring(hlua->T, -1)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7546 | lua_pop(hlua->T, 1); |
| 7547 | goto error; |
| 7548 | |
| 7549 | case HLUA_E_ETMOUT: |
| 7550 | SEND_ERR(px, "Lua applet http '%s': execution timeout.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7551 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7552 | goto error; |
| 7553 | |
| 7554 | case HLUA_E_NOMEM: |
| 7555 | SEND_ERR(px, "Lua applet http '%s': out of memory error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7556 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7557 | goto error; |
| 7558 | |
| 7559 | case HLUA_E_YIELD: /* unexpected */ |
| 7560 | SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7561 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7562 | goto error; |
| 7563 | |
| 7564 | case HLUA_E_ERR: |
| 7565 | /* Display log. */ |
| 7566 | SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7567 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7568 | goto error; |
| 7569 | |
| 7570 | default: |
| 7571 | goto error; |
| 7572 | } |
| 7573 | } |
| 7574 | |
| 7575 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7576 | if (ctx->ctx.hlua_apphttp.flags & APPLET_RSP_SENT) |
| 7577 | goto done; |
| 7578 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7579 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) |
| 7580 | goto error; |
| 7581 | |
Christopher Faulet | 868b3b1 | 2022-04-07 10:07:18 +0200 | [diff] [blame] | 7582 | /* no more data are expected. If the response buffer is empty |
| 7583 | * for a chunked message, be sure to add something (EOT block in |
| 7584 | * this case) to have something to send. It is important to be |
| 7585 | * sure the EOM flags will be handled by the endpoint. |
| 7586 | */ |
| 7587 | if (htx_is_empty(res_htx) && (strm->txn->rsp.flags & (HTTP_MSGF_XFER_LEN|HTTP_MSGF_CNT_LEN)) == HTTP_MSGF_XFER_LEN) { |
| 7588 | if (!htx_add_endof(res_htx, HTX_BLK_EOT)) { |
| 7589 | si_rx_room_blk(si); |
| 7590 | goto out; |
| 7591 | } |
| 7592 | channel_add_input(res, 1); |
| 7593 | } |
| 7594 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 7595 | res_htx->flags |= HTX_FL_EOM; |
Christopher Faulet | 79c0fc7 | 2022-03-07 15:50:54 +0100 | [diff] [blame] | 7596 | res->flags |= CF_EOI; |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7597 | strm->txn->status = ctx->ctx.hlua_apphttp.status; |
| 7598 | ctx->ctx.hlua_apphttp.flags |= APPLET_RSP_SENT; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7599 | } |
| 7600 | |
| 7601 | done: |
| 7602 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7603 | if (!(res->flags & CF_SHUTR)) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7604 | res->flags |= CF_READ_NULL; |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7605 | si_shutr(si); |
| 7606 | } |
| 7607 | |
| 7608 | /* eat the whole request */ |
| 7609 | if (co_data(req)) { |
| 7610 | req_htx = htx_from_buf(&req->buf); |
| 7611 | co_htx_skip(req, req_htx, co_data(req)); |
| 7612 | htx_to_buf(req_htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7613 | } |
| 7614 | } |
| 7615 | |
| 7616 | out: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7617 | htx_to_buf(res_htx, &res->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7618 | return; |
| 7619 | |
| 7620 | error: |
| 7621 | |
| 7622 | /* If we are in HTTP mode, and we are not send any |
| 7623 | * data, return a 500 server error in best effort: |
| 7624 | * if there is no room available in the buffer, |
| 7625 | * just close the connection. |
| 7626 | */ |
| 7627 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) { |
Christopher Faulet | f734638 | 2019-07-17 22:02:08 +0200 | [diff] [blame] | 7628 | struct buffer *err = &http_err_chunks[HTTP_ERR_500]; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7629 | |
| 7630 | channel_erase(res); |
| 7631 | res->buf.data = b_data(err); |
| 7632 | memcpy(res->buf.area, b_head(err), b_data(err)); |
| 7633 | res_htx = htx_from_buf(&res->buf); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 7634 | channel_add_input(res, res_htx->data); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7635 | } |
| 7636 | if (!(strm->flags & SF_ERR_MASK)) |
| 7637 | strm->flags |= SF_ERR_RESOURCE; |
| 7638 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 7639 | goto done; |
| 7640 | } |
| 7641 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7642 | static void hlua_applet_http_release(struct appctx *ctx) |
| 7643 | { |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 7644 | task_destroy(ctx->ctx.hlua_apphttp.task); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7645 | ctx->ctx.hlua_apphttp.task = NULL; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7646 | hlua_ctx_destroy(ctx->ctx.hlua_apphttp.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7647 | ctx->ctx.hlua_apphttp.hlua = NULL; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7648 | } |
| 7649 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7650 | /* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 7651 | * success case, else return ACT_RET_PRS_ERR. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7652 | * |
| 7653 | * This function can fail with an abort() due to an Lua critical error. |
| 7654 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 7655 | * tolerated. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7656 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7657 | static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px, |
| 7658 | struct act_rule *rule, char **err) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7659 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7660 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7661 | int i; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7662 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7663 | /* Memory for the rule. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7664 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7665 | if (!rule->arg.hlua_rule) { |
| 7666 | memprintf(err, "out of memory error"); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 7667 | goto error; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7668 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7669 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7670 | /* Memory for arguments. */ |
Tim Duesterhus | e52b6e5 | 2020-09-12 20:26:43 +0200 | [diff] [blame] | 7671 | rule->arg.hlua_rule->args = calloc(fcn->nargs + 1, |
| 7672 | sizeof(*rule->arg.hlua_rule->args)); |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7673 | if (!rule->arg.hlua_rule->args) { |
| 7674 | memprintf(err, "out of memory error"); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 7675 | goto error; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7676 | } |
| 7677 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7678 | /* Reference the Lua function and store the reference. */ |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7679 | rule->arg.hlua_rule->fcn = fcn; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7680 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7681 | /* Expect some arguments */ |
| 7682 | for (i = 0; i < fcn->nargs; i++) { |
Thierry FOURNIER | 1725c2e | 2019-01-06 19:38:49 +0100 | [diff] [blame] | 7683 | if (*args[*cur_arg] == '\0') { |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7684 | memprintf(err, "expect %d arguments", fcn->nargs); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 7685 | goto error; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7686 | } |
Thierry FOURNIER | 1725c2e | 2019-01-06 19:38:49 +0100 | [diff] [blame] | 7687 | rule->arg.hlua_rule->args[i] = strdup(args[*cur_arg]); |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7688 | if (!rule->arg.hlua_rule->args[i]) { |
| 7689 | memprintf(err, "out of memory error"); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 7690 | goto error; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7691 | } |
| 7692 | (*cur_arg)++; |
| 7693 | } |
| 7694 | rule->arg.hlua_rule->args[i] = NULL; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7695 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 7696 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7697 | rule->action_ptr = hlua_action; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 7698 | return ACT_RET_PRS_OK; |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 7699 | |
| 7700 | error: |
| 7701 | if (rule->arg.hlua_rule) { |
| 7702 | if (rule->arg.hlua_rule->args) { |
| 7703 | for (i = 0; i < fcn->nargs; i++) |
| 7704 | ha_free(&rule->arg.hlua_rule->args[i]); |
| 7705 | ha_free(&rule->arg.hlua_rule->args); |
| 7706 | } |
| 7707 | ha_free(&rule->arg.hlua_rule); |
| 7708 | } |
| 7709 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7710 | } |
| 7711 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7712 | static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px, |
| 7713 | struct act_rule *rule, char **err) |
| 7714 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7715 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7716 | |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 7717 | /* HTTP applets are forbidden in tcp-request rules. |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 7718 | * HTTP applet request requires everything initialized by |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 7719 | * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER). |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 7720 | * The applet will be immediately initialized, but its before |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 7721 | * the call of this analyzer. |
| 7722 | */ |
| 7723 | if (rule->from != ACT_F_HTTP_REQ) { |
| 7724 | memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets"); |
| 7725 | return ACT_RET_PRS_ERR; |
| 7726 | } |
| 7727 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7728 | /* Memory for the rule. */ |
| 7729 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 7730 | if (!rule->arg.hlua_rule) { |
| 7731 | memprintf(err, "out of memory error"); |
| 7732 | return ACT_RET_PRS_ERR; |
| 7733 | } |
| 7734 | |
| 7735 | /* Reference the Lua function and store the reference. */ |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7736 | rule->arg.hlua_rule->fcn = fcn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7737 | |
| 7738 | /* TODO: later accept arguments. */ |
| 7739 | rule->arg.hlua_rule->args = NULL; |
| 7740 | |
| 7741 | /* Add applet pointer in the rule. */ |
| 7742 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 7743 | rule->applet.name = fcn->name; |
| 7744 | rule->applet.init = hlua_applet_http_init; |
| 7745 | rule->applet.fct = hlua_applet_http_fct; |
| 7746 | rule->applet.release = hlua_applet_http_release; |
| 7747 | rule->applet.timeout = hlua_timeout_applet; |
| 7748 | |
| 7749 | return ACT_RET_PRS_OK; |
| 7750 | } |
| 7751 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7752 | /* This function is an LUA binding used for registering |
| 7753 | * "sample-conv" functions. It expects a converter name used |
| 7754 | * in the haproxy configuration file, and an LUA function. |
| 7755 | */ |
| 7756 | __LJMP static int hlua_register_action(lua_State *L) |
| 7757 | { |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 7758 | struct action_kw_list *akl = NULL; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7759 | const char *name; |
| 7760 | int ref; |
| 7761 | int len; |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 7762 | struct hlua_function *fcn = NULL; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7763 | int nargs; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 7764 | struct buffer *trash; |
| 7765 | struct action_kw *akw; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7766 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7767 | /* Initialise the number of expected arguments at 0. */ |
| 7768 | nargs = 0; |
| 7769 | |
| 7770 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 7771 | 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] | 7772 | |
Aurelien DARRAGON | 8985ab8 | 2023-02-24 09:43:54 +0100 | [diff] [blame] | 7773 | if (hlua_gethlua(L)) { |
| 7774 | /* runtime processing */ |
| 7775 | WILL_LJMP(luaL_error(L, "register_action: not available outside of body context")); |
| 7776 | } |
| 7777 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7778 | /* First argument : converter name. */ |
| 7779 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 7780 | |
| 7781 | /* Second argument : environment. */ |
| 7782 | if (lua_type(L, 2) != LUA_TTABLE) |
| 7783 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 7784 | |
| 7785 | /* Third argument : lua function. */ |
| 7786 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 7787 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7788 | /* 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] | 7789 | if (lua_gettop(L) >= 4) |
| 7790 | nargs = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 7791 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7792 | /* browse the second argument as an array. */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7793 | lua_pushnil(L); |
| 7794 | while (lua_next(L, 2) != 0) { |
| 7795 | if (lua_type(L, -1) != LUA_TSTRING) |
| 7796 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 7797 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 7798 | /* Check if action exists */ |
| 7799 | trash = get_trash_chunk(); |
| 7800 | chunk_printf(trash, "lua.%s", name); |
| 7801 | if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) { |
| 7802 | akw = tcp_req_cont_action(trash->area); |
| 7803 | } else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) { |
| 7804 | akw = tcp_res_cont_action(trash->area); |
| 7805 | } else if (strcmp(lua_tostring(L, -1), "http-req") == 0) { |
| 7806 | akw = action_http_req_custom(trash->area); |
| 7807 | } else if (strcmp(lua_tostring(L, -1), "http-res") == 0) { |
| 7808 | akw = action_http_res_custom(trash->area); |
| 7809 | } else { |
| 7810 | akw = NULL; |
| 7811 | } |
| 7812 | if (akw != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 7813 | fcn = akw->private; |
| 7814 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 7815 | ha_warning("Trying to register action 'lua.%s' more than once. " |
| 7816 | "This will become a hard error in version 2.5.\n", name); |
| 7817 | } |
| 7818 | fcn->function_ref[hlua_state_id] = ref; |
| 7819 | |
| 7820 | /* pop the environment string. */ |
| 7821 | lua_pop(L, 1); |
| 7822 | continue; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 7823 | } |
| 7824 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7825 | /* Check required environment. Only accepted "http" or "tcp". */ |
| 7826 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7827 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7828 | if (!akl) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 7829 | goto alloc_error;; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 7830 | fcn = new_hlua_function(); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7831 | if (!fcn) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 7832 | goto alloc_error; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7833 | |
| 7834 | /* Fill fcn. */ |
| 7835 | fcn->name = strdup(name); |
| 7836 | if (!fcn->name) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 7837 | goto alloc_error; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 7838 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7839 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 7840 | /* Set the expected number of arguments. */ |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7841 | fcn->nargs = nargs; |
| 7842 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7843 | /* List head */ |
| 7844 | akl->list.n = akl->list.p = NULL; |
| 7845 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7846 | /* action keyword. */ |
| 7847 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7848 | akl->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7849 | if (!akl->kw[0].kw) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 7850 | goto alloc_error; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7851 | |
| 7852 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 7853 | |
Amaury Denoyelle | e4a617c | 2021-05-06 15:33:09 +0200 | [diff] [blame] | 7854 | akl->kw[0].flags = 0; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7855 | akl->kw[0].private = fcn; |
| 7856 | akl->kw[0].parse = action_register_lua; |
| 7857 | |
| 7858 | /* select the action registering point. */ |
| 7859 | if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) |
| 7860 | tcp_req_cont_keywords_register(akl); |
| 7861 | else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) |
| 7862 | tcp_res_cont_keywords_register(akl); |
| 7863 | else if (strcmp(lua_tostring(L, -1), "http-req") == 0) |
| 7864 | http_req_keywords_register(akl); |
| 7865 | else if (strcmp(lua_tostring(L, -1), "http-res") == 0) |
| 7866 | http_res_keywords_register(akl); |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 7867 | else { |
| 7868 | release_hlua_function(fcn); |
| 7869 | if (akl) |
| 7870 | ha_free((char **)&(akl->kw[0].kw)); |
| 7871 | ha_free(&akl); |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7872 | WILL_LJMP(luaL_error(L, "Lua action environment '%s' is unknown. " |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7873 | "'tcp-req', 'tcp-res', 'http-req' or 'http-res' " |
| 7874 | "are expected.", lua_tostring(L, -1))); |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 7875 | } |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7876 | |
| 7877 | /* pop the environment string. */ |
| 7878 | lua_pop(L, 1); |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 7879 | |
| 7880 | /* reset for next loop */ |
| 7881 | akl = NULL; |
| 7882 | fcn = NULL; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7883 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7884 | return ACT_RET_PRS_OK; |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 7885 | |
| 7886 | alloc_error: |
| 7887 | release_hlua_function(fcn); |
| 7888 | ha_free(&akl); |
| 7889 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 7890 | return 0; /* Never reached */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7891 | } |
| 7892 | |
| 7893 | static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px, |
| 7894 | struct act_rule *rule, char **err) |
| 7895 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7896 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7897 | |
Christopher Faulet | 280f85b | 2019-07-15 15:02:04 +0200 | [diff] [blame] | 7898 | if (px->mode == PR_MODE_HTTP) { |
| 7899 | memprintf(err, "Lua TCP services cannot be used on HTTP proxies"); |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 7900 | return ACT_RET_PRS_ERR; |
| 7901 | } |
| 7902 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7903 | /* Memory for the rule. */ |
| 7904 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 7905 | if (!rule->arg.hlua_rule) { |
| 7906 | memprintf(err, "out of memory error"); |
| 7907 | return ACT_RET_PRS_ERR; |
| 7908 | } |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7909 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7910 | /* Reference the Lua function and store the reference. */ |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7911 | rule->arg.hlua_rule->fcn = fcn; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7912 | |
| 7913 | /* TODO: later accept arguments. */ |
| 7914 | rule->arg.hlua_rule->args = NULL; |
| 7915 | |
| 7916 | /* Add applet pointer in the rule. */ |
| 7917 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 7918 | rule->applet.name = fcn->name; |
| 7919 | rule->applet.init = hlua_applet_tcp_init; |
| 7920 | rule->applet.fct = hlua_applet_tcp_fct; |
| 7921 | rule->applet.release = hlua_applet_tcp_release; |
| 7922 | rule->applet.timeout = hlua_timeout_applet; |
| 7923 | |
| 7924 | return 0; |
| 7925 | } |
| 7926 | |
| 7927 | /* This function is an LUA binding used for registering |
| 7928 | * "sample-conv" functions. It expects a converter name used |
| 7929 | * in the haproxy configuration file, and an LUA function. |
| 7930 | */ |
| 7931 | __LJMP static int hlua_register_service(lua_State *L) |
| 7932 | { |
| 7933 | struct action_kw_list *akl; |
| 7934 | const char *name; |
| 7935 | const char *env; |
| 7936 | int ref; |
| 7937 | int len; |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 7938 | struct hlua_function *fcn = NULL; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 7939 | struct buffer *trash; |
| 7940 | struct action_kw *akw; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7941 | |
| 7942 | MAY_LJMP(check_args(L, 3, "register_service")); |
| 7943 | |
Aurelien DARRAGON | 8985ab8 | 2023-02-24 09:43:54 +0100 | [diff] [blame] | 7944 | if (hlua_gethlua(L)) { |
| 7945 | /* runtime processing */ |
| 7946 | WILL_LJMP(luaL_error(L, "register_service: not available outside of body context")); |
| 7947 | } |
| 7948 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7949 | /* First argument : converter name. */ |
| 7950 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 7951 | |
| 7952 | /* Second argument : environment. */ |
| 7953 | env = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7954 | |
| 7955 | /* Third argument : lua function. */ |
| 7956 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 7957 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 7958 | /* Check for service already registered */ |
| 7959 | trash = get_trash_chunk(); |
| 7960 | chunk_printf(trash, "lua.%s", name); |
| 7961 | akw = service_find(trash->area); |
| 7962 | if (akw != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 7963 | fcn = akw->private; |
| 7964 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 7965 | ha_warning("Trying to register service 'lua.%s' more than once. " |
| 7966 | "This will become a hard error in version 2.5.\n", name); |
| 7967 | } |
| 7968 | fcn->function_ref[hlua_state_id] = ref; |
| 7969 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 7970 | } |
| 7971 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7972 | /* Allocate and fill the sample fetch keyword struct. */ |
| 7973 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
| 7974 | if (!akl) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 7975 | goto alloc_error; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 7976 | fcn = new_hlua_function(); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7977 | if (!fcn) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 7978 | goto alloc_error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7979 | |
| 7980 | /* Fill fcn. */ |
| 7981 | len = strlen("<lua.>") + strlen(name) + 1; |
| 7982 | fcn->name = calloc(1, len); |
| 7983 | if (!fcn->name) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 7984 | goto alloc_error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7985 | snprintf((char *)fcn->name, len, "<lua.%s>", name); |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 7986 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7987 | |
| 7988 | /* List head */ |
| 7989 | akl->list.n = akl->list.p = NULL; |
| 7990 | |
| 7991 | /* converter keyword. */ |
| 7992 | len = strlen("lua.") + strlen(name) + 1; |
| 7993 | akl->kw[0].kw = calloc(1, len); |
| 7994 | if (!akl->kw[0].kw) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 7995 | goto alloc_error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7996 | |
| 7997 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 7998 | |
Thierry FOURNIER / OZON.IO | 02564fd | 2016-11-12 11:07:05 +0100 | [diff] [blame] | 7999 | /* Check required environment. Only accepted "http" or "tcp". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8000 | if (strcmp(env, "tcp") == 0) |
| 8001 | akl->kw[0].parse = action_register_service_tcp; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8002 | else if (strcmp(env, "http") == 0) |
| 8003 | akl->kw[0].parse = action_register_service_http; |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 8004 | else { |
| 8005 | release_hlua_function(fcn); |
| 8006 | if (akl) |
| 8007 | ha_free((char **)&(akl->kw[0].kw)); |
| 8008 | ha_free(&akl); |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8009 | WILL_LJMP(luaL_error(L, "Lua service environment '%s' is unknown. " |
Eric Salama | fe7456f | 2017-12-21 14:30:07 +0100 | [diff] [blame] | 8010 | "'tcp' or 'http' are expected.", env)); |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 8011 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8012 | |
Amaury Denoyelle | e4a617c | 2021-05-06 15:33:09 +0200 | [diff] [blame] | 8013 | akl->kw[0].flags = 0; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8014 | akl->kw[0].private = fcn; |
| 8015 | |
| 8016 | /* End of array. */ |
| 8017 | memset(&akl->kw[1], 0, sizeof(*akl->kw)); |
| 8018 | |
| 8019 | /* Register this new converter */ |
| 8020 | service_keywords_register(akl); |
| 8021 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 8022 | return 0; |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 8023 | |
| 8024 | alloc_error: |
| 8025 | release_hlua_function(fcn); |
| 8026 | ha_free(&akl); |
| 8027 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 8028 | return 0; /* Never reached */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 8029 | } |
| 8030 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8031 | /* This function initialises Lua cli handler. It copies the |
| 8032 | * arguments in the Lua stack and create channel IO objects. |
| 8033 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 8034 | 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] | 8035 | { |
| 8036 | struct hlua *hlua; |
| 8037 | struct hlua_function *fcn; |
| 8038 | int i; |
| 8039 | const char *error; |
| 8040 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8041 | fcn = private; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 8042 | appctx->ctx.hlua_cli.fcn = private; |
| 8043 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 8044 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 8045 | if (!hlua) { |
| 8046 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 8047 | return 1; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 8048 | } |
| 8049 | HLUA_INIT(hlua); |
| 8050 | appctx->ctx.hlua_cli.hlua = hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8051 | |
| 8052 | /* Create task used by signal to wakeup applets. |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8053 | * 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] | 8054 | * applet_http. It is absolutely compatible. |
| 8055 | */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 8056 | appctx->ctx.hlua_cli.task = task_new(tid_bit); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8057 | if (!appctx->ctx.hlua_cli.task) { |
Thierry FOURNIER | ffbf569 | 2016-12-16 11:14:06 +0100 | [diff] [blame] | 8058 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 8059 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8060 | } |
| 8061 | appctx->ctx.hlua_cli.task->nice = 0; |
| 8062 | appctx->ctx.hlua_cli.task->context = appctx; |
| 8063 | appctx->ctx.hlua_cli.task->process = hlua_applet_wakeup; |
| 8064 | |
| 8065 | /* Initialises the Lua context */ |
Aurelien DARRAGON | 5483bde | 2023-03-21 14:02:16 +0100 | [diff] [blame] | 8066 | 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] | 8067 | 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] | 8068 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8069 | } |
| 8070 | |
| 8071 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8072 | if (!SET_SAFE_LJMP(hlua)) { |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8073 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 8074 | error = lua_tostring(hlua->T, -1); |
| 8075 | else |
| 8076 | error = "critical error"; |
| 8077 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error); |
| 8078 | goto error; |
| 8079 | } |
| 8080 | |
| 8081 | /* Check stack available size. */ |
| 8082 | if (!lua_checkstack(hlua->T, 2)) { |
| 8083 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 8084 | goto error; |
| 8085 | } |
| 8086 | |
| 8087 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8088 | 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] | 8089 | |
| 8090 | /* Once the arguments parsed, the CLI is like an AppletTCP, |
| 8091 | * so push AppletTCP in the stack. |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8092 | */ |
| 8093 | if (!hlua_applet_tcp_new(hlua->T, appctx)) { |
| 8094 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 8095 | goto error; |
| 8096 | } |
| 8097 | hlua->nargs = 1; |
| 8098 | |
| 8099 | /* push keywords in the stack. */ |
| 8100 | for (i = 0; *args[i]; i++) { |
| 8101 | /* Check stack available size. */ |
| 8102 | if (!lua_checkstack(hlua->T, 1)) { |
| 8103 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 8104 | goto error; |
| 8105 | } |
| 8106 | lua_pushstring(hlua->T, args[i]); |
| 8107 | hlua->nargs++; |
| 8108 | } |
| 8109 | |
| 8110 | /* We must initialize the execution timeouts. */ |
| 8111 | hlua->max_time = hlua_timeout_session; |
| 8112 | |
| 8113 | /* At this point the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8114 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8115 | |
| 8116 | /* It's ok */ |
| 8117 | return 0; |
| 8118 | |
| 8119 | /* It's not ok. */ |
| 8120 | error: |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8121 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8122 | hlua_ctx_destroy(hlua); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 8123 | appctx->ctx.hlua_cli.hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8124 | return 1; |
| 8125 | } |
| 8126 | |
| 8127 | static int hlua_cli_io_handler_fct(struct appctx *appctx) |
| 8128 | { |
| 8129 | struct hlua *hlua; |
| 8130 | struct stream_interface *si; |
| 8131 | struct hlua_function *fcn; |
| 8132 | |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 8133 | hlua = appctx->ctx.hlua_cli.hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8134 | si = appctx->owner; |
Willy Tarreau | 8ae4f75 | 2016-12-14 15:41:45 +0100 | [diff] [blame] | 8135 | fcn = appctx->ctx.hlua_cli.fcn; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8136 | |
| 8137 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 8138 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 8139 | return 1; |
| 8140 | |
| 8141 | /* Execute the function. */ |
| 8142 | switch (hlua_ctx_resume(hlua, 1)) { |
| 8143 | |
| 8144 | /* finished. */ |
| 8145 | case HLUA_E_OK: |
| 8146 | return 1; |
| 8147 | |
| 8148 | /* yield. */ |
| 8149 | case HLUA_E_AGAIN: |
| 8150 | /* We want write. */ |
| 8151 | if (HLUA_IS_WAKERESWR(hlua)) |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 8152 | si_rx_room_blk(si); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8153 | /* Set the timeout. */ |
| 8154 | if (hlua->wake_time != TICK_ETERNITY) |
| 8155 | task_schedule(hlua->task, hlua->wake_time); |
| 8156 | return 0; |
| 8157 | |
| 8158 | /* finished with error. */ |
| 8159 | case HLUA_E_ERRMSG: |
| 8160 | /* Display log. */ |
| 8161 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", |
| 8162 | fcn->name, lua_tostring(hlua->T, -1)); |
| 8163 | lua_pop(hlua->T, 1); |
| 8164 | return 1; |
| 8165 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 8166 | case HLUA_E_ETMOUT: |
| 8167 | SEND_ERR(NULL, "Lua converter '%s': execution timeout.\n", |
| 8168 | fcn->name); |
| 8169 | return 1; |
| 8170 | |
| 8171 | case HLUA_E_NOMEM: |
| 8172 | SEND_ERR(NULL, "Lua converter '%s': out of memory error.\n", |
| 8173 | fcn->name); |
| 8174 | return 1; |
| 8175 | |
| 8176 | case HLUA_E_YIELD: /* unexpected */ |
| 8177 | SEND_ERR(NULL, "Lua converter '%s': yield not allowed.\n", |
| 8178 | fcn->name); |
| 8179 | return 1; |
| 8180 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8181 | case HLUA_E_ERR: |
| 8182 | /* Display log. */ |
| 8183 | SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n", |
| 8184 | fcn->name); |
| 8185 | return 1; |
| 8186 | |
| 8187 | default: |
| 8188 | return 1; |
| 8189 | } |
| 8190 | |
| 8191 | return 1; |
| 8192 | } |
| 8193 | |
| 8194 | static void hlua_cli_io_release_fct(struct appctx *appctx) |
| 8195 | { |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 8196 | hlua_ctx_destroy(appctx->ctx.hlua_cli.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 8197 | appctx->ctx.hlua_cli.hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8198 | } |
| 8199 | |
| 8200 | /* This function is an LUA binding used for registering |
| 8201 | * new keywords in the cli. It expects a list of keywords |
| 8202 | * which are the "path". It is limited to 5 keywords. A |
| 8203 | * description of the command, a function to be executed |
| 8204 | * for the parsing and a function for io handlers. |
| 8205 | */ |
| 8206 | __LJMP static int hlua_register_cli(lua_State *L) |
| 8207 | { |
| 8208 | struct cli_kw_list *cli_kws; |
| 8209 | const char *message; |
| 8210 | int ref_io; |
| 8211 | int len; |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 8212 | struct hlua_function *fcn = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8213 | int index; |
| 8214 | int i; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8215 | struct buffer *trash; |
| 8216 | const char *kw[5]; |
| 8217 | struct cli_kw *cli_kw; |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 8218 | const char *errmsg; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8219 | |
| 8220 | MAY_LJMP(check_args(L, 3, "register_cli")); |
| 8221 | |
Aurelien DARRAGON | 8985ab8 | 2023-02-24 09:43:54 +0100 | [diff] [blame] | 8222 | if (hlua_gethlua(L)) { |
| 8223 | /* runtime processing */ |
| 8224 | WILL_LJMP(luaL_error(L, "register_cli: not available outside of body context")); |
| 8225 | } |
| 8226 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8227 | /* First argument : an array of maximum 5 keywords. */ |
| 8228 | if (!lua_istable(L, 1)) |
| 8229 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table")); |
| 8230 | |
| 8231 | /* Second argument : string with contextual message. */ |
| 8232 | message = MAY_LJMP(luaL_checkstring(L, 2)); |
| 8233 | |
| 8234 | /* Third and fourth argument : lua function. */ |
| 8235 | ref_io = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 8236 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8237 | /* Check for CLI service already registered */ |
| 8238 | trash = get_trash_chunk(); |
| 8239 | index = 0; |
| 8240 | lua_pushnil(L); |
| 8241 | memset(kw, 0, sizeof(kw)); |
| 8242 | while (lua_next(L, 1) != 0) { |
| 8243 | if (index >= CLI_PREFIX_KW_NB) |
| 8244 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries")); |
| 8245 | if (lua_type(L, -1) != LUA_TSTRING) |
| 8246 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings")); |
| 8247 | kw[index] = lua_tostring(L, -1); |
| 8248 | if (index == 0) |
| 8249 | chunk_printf(trash, "%s", kw[index]); |
| 8250 | else |
| 8251 | chunk_appendf(trash, " %s", kw[index]); |
| 8252 | index++; |
| 8253 | lua_pop(L, 1); |
| 8254 | } |
| 8255 | cli_kw = cli_find_kw_exact((char **)kw); |
| 8256 | if (cli_kw != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8257 | fcn = cli_kw->private; |
| 8258 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 8259 | ha_warning("Trying to register CLI keyword 'lua.%s' more than once. " |
| 8260 | "This will become a hard error in version 2.5.\n", trash->area); |
| 8261 | } |
| 8262 | fcn->function_ref[hlua_state_id] = ref_io; |
| 8263 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8264 | } |
| 8265 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8266 | /* Allocate and fill the sample fetch keyword struct. */ |
| 8267 | cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 8268 | if (!cli_kws) { |
| 8269 | errmsg = "Lua out of memory error."; |
| 8270 | goto error; |
| 8271 | } |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 8272 | fcn = new_hlua_function(); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 8273 | if (!fcn) { |
| 8274 | errmsg = "Lua out of memory error."; |
| 8275 | goto error; |
| 8276 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8277 | |
| 8278 | /* Fill path. */ |
| 8279 | index = 0; |
| 8280 | lua_pushnil(L); |
| 8281 | while(lua_next(L, 1) != 0) { |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 8282 | if (index >= 5) { |
| 8283 | errmsg = "1st argument must be a table with a maximum of 5 entries"; |
| 8284 | goto error; |
| 8285 | } |
| 8286 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 8287 | errmsg = "1st argument must be a table filled with strings"; |
| 8288 | goto error; |
| 8289 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8290 | cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1)); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 8291 | if (!cli_kws->kw[0].str_kw[index]) { |
| 8292 | errmsg = "Lua out of memory error."; |
| 8293 | goto error; |
| 8294 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8295 | index++; |
| 8296 | lua_pop(L, 1); |
| 8297 | } |
| 8298 | |
| 8299 | /* Copy help message. */ |
| 8300 | cli_kws->kw[0].usage = strdup(message); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 8301 | if (!cli_kws->kw[0].usage) { |
| 8302 | errmsg = "Lua out of memory error."; |
| 8303 | goto error; |
| 8304 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8305 | |
| 8306 | /* Fill fcn io handler. */ |
| 8307 | len = strlen("<lua.cli>") + 1; |
| 8308 | for (i = 0; i < index; i++) |
| 8309 | len += strlen(cli_kws->kw[0].str_kw[i]) + 1; |
| 8310 | fcn->name = calloc(1, len); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 8311 | if (!fcn->name) { |
| 8312 | errmsg = "Lua out of memory error."; |
| 8313 | goto error; |
| 8314 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8315 | strncat((char *)fcn->name, "<lua.cli", len); |
| 8316 | for (i = 0; i < index; i++) { |
| 8317 | strncat((char *)fcn->name, ".", len); |
| 8318 | strncat((char *)fcn->name, cli_kws->kw[0].str_kw[i], len); |
| 8319 | } |
| 8320 | strncat((char *)fcn->name, ">", len); |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8321 | fcn->function_ref[hlua_state_id] = ref_io; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8322 | |
| 8323 | /* Fill last entries. */ |
| 8324 | cli_kws->kw[0].private = fcn; |
| 8325 | cli_kws->kw[0].parse = hlua_cli_parse_fct; |
| 8326 | cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct; |
| 8327 | cli_kws->kw[0].io_release = hlua_cli_io_release_fct; |
| 8328 | |
| 8329 | /* Register this new converter */ |
| 8330 | cli_register_kw(cli_kws); |
| 8331 | |
| 8332 | return 0; |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 8333 | |
| 8334 | error: |
| 8335 | release_hlua_function(fcn); |
| 8336 | if (cli_kws) { |
| 8337 | for (i = 0; i < index; i++) |
| 8338 | ha_free((char **)&(cli_kws->kw[0].str_kw[i])); |
| 8339 | ha_free((char **)&(cli_kws->kw[0].usage)); |
| 8340 | } |
| 8341 | ha_free(&cli_kws); |
| 8342 | WILL_LJMP(luaL_error(L, errmsg)); |
| 8343 | return 0; /* Never reached */ |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8344 | } |
| 8345 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8346 | 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] | 8347 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8348 | char **err, unsigned int *timeout) |
| 8349 | { |
| 8350 | const char *error; |
| 8351 | |
| 8352 | error = parse_time_err(args[1], timeout, TIME_UNIT_MS); |
Willy Tarreau | 9faebe3 | 2019-06-07 19:00:37 +0200 | [diff] [blame] | 8353 | if (error == PARSE_TIME_OVER) { |
| 8354 | memprintf(err, "timer overflow in argument <%s> to <%s> (maximum value is 2147483647 ms or ~24.8 days)", |
| 8355 | args[1], args[0]); |
| 8356 | return -1; |
| 8357 | } |
| 8358 | else if (error == PARSE_TIME_UNDER) { |
| 8359 | memprintf(err, "timer underflow in argument <%s> to <%s> (minimum non-null value is 1 ms)", |
| 8360 | args[1], args[0]); |
| 8361 | return -1; |
| 8362 | } |
| 8363 | else if (error) { |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8364 | memprintf(err, "%s: invalid timeout", args[0]); |
| 8365 | return -1; |
| 8366 | } |
| 8367 | return 0; |
| 8368 | } |
| 8369 | |
| 8370 | 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] | 8371 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8372 | char **err) |
| 8373 | { |
| 8374 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 8375 | file, line, err, &hlua_timeout_session); |
| 8376 | } |
| 8377 | |
| 8378 | 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] | 8379 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8380 | char **err) |
| 8381 | { |
| 8382 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 8383 | file, line, err, &hlua_timeout_task); |
| 8384 | } |
| 8385 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8386 | 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] | 8387 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8388 | char **err) |
| 8389 | { |
| 8390 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 8391 | file, line, err, &hlua_timeout_applet); |
| 8392 | } |
| 8393 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 8394 | 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] | 8395 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 8396 | char **err) |
| 8397 | { |
| 8398 | char *error; |
| 8399 | |
| 8400 | hlua_nb_instruction = strtoll(args[1], &error, 10); |
| 8401 | if (*error != '\0') { |
| 8402 | memprintf(err, "%s: invalid number", args[0]); |
| 8403 | return -1; |
| 8404 | } |
| 8405 | return 0; |
| 8406 | } |
| 8407 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8408 | 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] | 8409 | const struct proxy *defpx, const char *file, int line, |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8410 | char **err) |
| 8411 | { |
| 8412 | char *error; |
| 8413 | |
| 8414 | if (*(args[1]) == 0) { |
| 8415 | memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]); |
| 8416 | return -1; |
| 8417 | } |
| 8418 | hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L; |
| 8419 | if (*error != '\0') { |
| 8420 | memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error); |
| 8421 | return -1; |
| 8422 | } |
| 8423 | return 0; |
| 8424 | } |
| 8425 | |
| 8426 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8427 | /* This function is called by the main configuration key "lua-load". It loads and |
| 8428 | * execute an lua file during the parsing of the HAProxy configuration file. It is |
| 8429 | * the main lua entry point. |
| 8430 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 8431 | * This function runs with the HAProxy keywords API. It returns -1 if an error |
| 8432 | * occurs, otherwise it returns 0. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8433 | * |
| 8434 | * In some error case, LUA set an error message in top of the stack. This function |
| 8435 | * 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] | 8436 | * |
| 8437 | * This function can fail with an abort() due to an Lua critical error. |
| 8438 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 8439 | * tolerated. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8440 | */ |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8441 | static int hlua_load_state(char *filename, lua_State *L, char **err) |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8442 | { |
| 8443 | int error; |
| 8444 | |
| 8445 | /* Just load and compile the file. */ |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8446 | error = luaL_loadfile(L, filename); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8447 | if (error) { |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8448 | memprintf(err, "error in Lua file '%s': %s", filename, lua_tostring(L, -1)); |
| 8449 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8450 | return -1; |
| 8451 | } |
| 8452 | |
| 8453 | /* If no syntax error where detected, execute the code. */ |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8454 | error = lua_pcall(L, 0, LUA_MULTRET, 0); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8455 | switch (error) { |
| 8456 | case LUA_OK: |
| 8457 | break; |
| 8458 | case LUA_ERRRUN: |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8459 | memprintf(err, "Lua runtime error: %s\n", lua_tostring(L, -1)); |
| 8460 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8461 | return -1; |
| 8462 | case LUA_ERRMEM: |
Thierry Fournier | de6145f | 2020-11-29 00:55:53 +0100 | [diff] [blame] | 8463 | memprintf(err, "Lua out of memory error\n"); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8464 | return -1; |
| 8465 | case LUA_ERRERR: |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8466 | memprintf(err, "Lua message handler error: %s\n", lua_tostring(L, -1)); |
| 8467 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8468 | return -1; |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 8469 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM <= 503 |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8470 | case LUA_ERRGCMM: |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8471 | memprintf(err, "Lua garbage collector error: %s\n", lua_tostring(L, -1)); |
| 8472 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8473 | return -1; |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 8474 | #endif |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8475 | default: |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8476 | memprintf(err, "Lua unknown error: %s\n", lua_tostring(L, -1)); |
| 8477 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8478 | return -1; |
| 8479 | } |
| 8480 | |
| 8481 | return 0; |
| 8482 | } |
| 8483 | |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8484 | static int hlua_load(char **args, int section_type, struct proxy *curpx, |
Willy Tarreau | 0182516 | 2021-03-09 09:53:46 +0100 | [diff] [blame] | 8485 | const struct proxy *defpx, const char *file, int line, |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8486 | char **err) |
| 8487 | { |
| 8488 | if (*(args[1]) == 0) { |
| 8489 | memprintf(err, "'%s' expects a file name as parameter.\n", args[0]); |
| 8490 | return -1; |
| 8491 | } |
| 8492 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8493 | /* loading for global state */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8494 | hlua_state_id = 0; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8495 | ha_set_tid(0); |
Thierry Fournier | afc63e2 | 2020-11-28 17:06:51 +0100 | [diff] [blame] | 8496 | return hlua_load_state(args[1], hlua_states[0], err); |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8497 | } |
| 8498 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8499 | 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] | 8500 | const struct proxy *defpx, const char *file, int line, |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8501 | char **err) |
| 8502 | { |
| 8503 | int len; |
| 8504 | |
| 8505 | if (*(args[1]) == 0) { |
| 8506 | memprintf(err, "'%s' expects a file as parameter.\n", args[0]); |
| 8507 | return -1; |
| 8508 | } |
| 8509 | |
| 8510 | if (per_thread_load == NULL) { |
| 8511 | /* allocate the first entry large enough to store the final NULL */ |
| 8512 | per_thread_load = calloc(1, sizeof(*per_thread_load)); |
| 8513 | if (per_thread_load == NULL) { |
| 8514 | memprintf(err, "out of memory error"); |
| 8515 | return -1; |
| 8516 | } |
| 8517 | } |
| 8518 | |
| 8519 | /* count used entries */ |
| 8520 | for (len = 0; per_thread_load[len] != NULL; len++) |
| 8521 | ; |
| 8522 | |
| 8523 | per_thread_load = realloc(per_thread_load, (len + 2) * sizeof(*per_thread_load)); |
| 8524 | if (per_thread_load == NULL) { |
| 8525 | memprintf(err, "out of memory error"); |
| 8526 | return -1; |
| 8527 | } |
| 8528 | |
| 8529 | per_thread_load[len] = strdup(args[1]); |
| 8530 | per_thread_load[len + 1] = NULL; |
| 8531 | |
| 8532 | if (per_thread_load[len] == NULL) { |
| 8533 | memprintf(err, "out of memory error"); |
| 8534 | return -1; |
| 8535 | } |
| 8536 | |
| 8537 | /* loading for thread 1 only */ |
| 8538 | hlua_state_id = 1; |
| 8539 | ha_set_tid(0); |
| 8540 | return hlua_load_state(args[1], hlua_states[1], err); |
| 8541 | } |
| 8542 | |
Tim Duesterhus | c9fc9f2 | 2020-01-12 13:55:39 +0100 | [diff] [blame] | 8543 | /* Prepend the given <path> followed by a semicolon to the `package.<type>` variable |
| 8544 | * in the given <ctx>. |
| 8545 | */ |
Thierry Fournier | 3fb9e51 | 2020-11-28 10:13:12 +0100 | [diff] [blame] | 8546 | static int hlua_prepend_path(lua_State *L, char *type, char *path) |
Tim Duesterhus | c9fc9f2 | 2020-01-12 13:55:39 +0100 | [diff] [blame] | 8547 | { |
Thierry Fournier | 3fb9e51 | 2020-11-28 10:13:12 +0100 | [diff] [blame] | 8548 | lua_getglobal(L, "package"); /* push package variable */ |
| 8549 | lua_pushstring(L, path); /* push given path */ |
| 8550 | lua_pushstring(L, ";"); /* push semicolon */ |
| 8551 | lua_getfield(L, -3, type); /* push old path */ |
| 8552 | lua_concat(L, 3); /* concatenate to new path */ |
| 8553 | lua_setfield(L, -2, type); /* store new path */ |
| 8554 | lua_pop(L, 1); /* pop package variable */ |
Tim Duesterhus | c9fc9f2 | 2020-01-12 13:55:39 +0100 | [diff] [blame] | 8555 | |
| 8556 | return 0; |
| 8557 | } |
| 8558 | |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8559 | 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] | 8560 | const struct proxy *defpx, const char *file, int line, |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8561 | char **err) |
| 8562 | { |
| 8563 | char *path; |
| 8564 | char *type = "path"; |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 8565 | struct prepend_path *p = NULL; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8566 | |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8567 | if (too_many_args(2, args, err, NULL)) { |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 8568 | goto err; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8569 | } |
| 8570 | |
| 8571 | if (!(*args[1])) { |
| 8572 | memprintf(err, "'%s' expects to receive a <path> as argument", args[0]); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 8573 | goto err; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8574 | } |
| 8575 | path = args[1]; |
| 8576 | |
| 8577 | if (*args[2]) { |
| 8578 | if (strcmp(args[2], "path") != 0 && strcmp(args[2], "cpath") != 0) { |
| 8579 | 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] | 8580 | goto err; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8581 | } |
| 8582 | type = args[2]; |
| 8583 | } |
| 8584 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8585 | p = calloc(1, sizeof(*p)); |
| 8586 | if (p == NULL) { |
Tim Duesterhus | f89d43a | 2021-01-03 20:04:37 +0100 | [diff] [blame] | 8587 | memprintf(err, "memory allocation failed"); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 8588 | goto err; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8589 | } |
| 8590 | p->path = strdup(path); |
| 8591 | if (p->path == NULL) { |
Tim Duesterhus | f89d43a | 2021-01-03 20:04:37 +0100 | [diff] [blame] | 8592 | memprintf(err, "memory allocation failed"); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 8593 | goto err2; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8594 | } |
| 8595 | p->type = strdup(type); |
| 8596 | if (p->type == NULL) { |
Tim Duesterhus | f89d43a | 2021-01-03 20:04:37 +0100 | [diff] [blame] | 8597 | memprintf(err, "memory allocation failed"); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 8598 | goto err2; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8599 | } |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 8600 | LIST_APPEND(&prepend_path_list, &p->l); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8601 | |
| 8602 | hlua_prepend_path(hlua_states[0], type, path); |
| 8603 | hlua_prepend_path(hlua_states[1], type, path); |
| 8604 | return 0; |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 8605 | |
| 8606 | err2: |
| 8607 | free(p->type); |
| 8608 | free(p->path); |
| 8609 | err: |
| 8610 | free(p); |
| 8611 | return -1; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8612 | } |
| 8613 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8614 | /* configuration keywords declaration */ |
| 8615 | static struct cfg_kw_list cfg_kws = {{ },{ |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8616 | { CFG_GLOBAL, "lua-prepend-path", hlua_config_prepend_path }, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8617 | { CFG_GLOBAL, "lua-load", hlua_load }, |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8618 | { CFG_GLOBAL, "lua-load-per-thread", hlua_load_per_thread }, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8619 | { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout }, |
| 8620 | { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout }, |
Thierry FOURNIER | 56da101 | 2015-10-01 08:42:31 +0200 | [diff] [blame] | 8621 | { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout }, |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 8622 | { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield }, |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8623 | { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem }, |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8624 | { 0, NULL, NULL }, |
| 8625 | }}; |
| 8626 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 8627 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |
| 8628 | |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 8629 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8630 | /* This function can fail with an abort() due to an Lua critical error. |
| 8631 | * We are in the initialisation process of HAProxy, this abort() is |
| 8632 | * tolerated. |
| 8633 | */ |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8634 | int hlua_post_init_state(lua_State *L) |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8635 | { |
| 8636 | struct hlua_init_function *init; |
| 8637 | const char *msg; |
| 8638 | enum hlua_exec ret; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8639 | const char *error; |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8640 | const char *kind; |
| 8641 | const char *trace; |
| 8642 | int return_status = 1; |
| 8643 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
| 8644 | int nres; |
| 8645 | #endif |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8646 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 8647 | /* disable memory limit checks if limit is not set */ |
| 8648 | if (!hlua_global_allocator.limit) |
| 8649 | hlua_global_allocator.limit = ~hlua_global_allocator.limit; |
| 8650 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 8651 | /* Call post initialisation function in safe environment. */ |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 8652 | if (setjmp(safe_ljmp_env) != 0) { |
| 8653 | lua_atpanic(L, hlua_panic_safe); |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8654 | if (lua_type(L, -1) == LUA_TSTRING) |
| 8655 | error = lua_tostring(L, -1); |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 8656 | else |
| 8657 | error = "critical error"; |
| 8658 | fprintf(stderr, "Lua post-init: %s.\n", error); |
| 8659 | exit(1); |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 8660 | } else { |
| 8661 | lua_atpanic(L, hlua_panic_ljmp); |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 8662 | } |
Frédéric Lécaille | 54f2bcf | 2018-08-29 13:46:24 +0200 | [diff] [blame] | 8663 | |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8664 | hlua_fcn_post_init(L); |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 8665 | |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8666 | list_for_each_entry(init, &hlua_init_functions[hlua_state_id], l) { |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8667 | lua_rawgeti(L, LUA_REGISTRYINDEX, init->function_ref); |
Aurelien DARRAGON | 93591b4 | 2023-03-20 16:29:55 +0100 | [diff] [blame] | 8668 | /* function ref should be released right away since it was pushed |
| 8669 | * on the stack and will not be used anymore |
| 8670 | */ |
| 8671 | hlua_unref(L, init->function_ref); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8672 | |
| 8673 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
Aurelien DARRAGON | 2dec950 | 2023-09-08 19:29:08 +0200 | [diff] [blame] | 8674 | ret = lua_resume(L, NULL, 0, &nres); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8675 | #else |
Aurelien DARRAGON | 2dec950 | 2023-09-08 19:29:08 +0200 | [diff] [blame] | 8676 | ret = lua_resume(L, NULL, 0); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8677 | #endif |
| 8678 | kind = NULL; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8679 | switch (ret) { |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8680 | |
| 8681 | case LUA_OK: |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8682 | lua_pop(L, -1); |
Thierry Fournier | 13d08b7 | 2020-11-28 11:02:58 +0100 | [diff] [blame] | 8683 | break; |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8684 | |
| 8685 | case LUA_ERRERR: |
| 8686 | kind = "message handler error"; |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 8687 | /* Fall through */ |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8688 | case LUA_ERRRUN: |
| 8689 | if (!kind) |
| 8690 | kind = "runtime error"; |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8691 | msg = lua_tostring(L, -1); |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 8692 | trace = hlua_traceback(L, ", "); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8693 | if (msg) |
| 8694 | ha_alert("Lua init: %s: '%s' from %s\n", kind, msg, trace); |
| 8695 | else |
| 8696 | ha_alert("Lua init: unknown %s from %s\n", kind, trace); |
Aurelien DARRAGON | adac932 | 2024-03-01 19:54:16 +0100 | [diff] [blame^] | 8697 | |
| 8698 | lua_settop(L, 0); /* Empty the stack. */ |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8699 | return_status = 0; |
| 8700 | break; |
| 8701 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8702 | default: |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8703 | /* Unknown error */ |
| 8704 | kind = "Unknown error"; |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 8705 | /* Fall through */ |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8706 | case LUA_YIELD: |
| 8707 | /* yield is not configured at this step, this state doesn't happen */ |
| 8708 | if (!kind) |
| 8709 | kind = "yield not allowed"; |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 8710 | /* Fall through */ |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8711 | case LUA_ERRMEM: |
| 8712 | if (!kind) |
| 8713 | kind = "out of memory error"; |
Aurelien DARRAGON | 93f5d8d | 2023-08-09 10:11:49 +0200 | [diff] [blame] | 8714 | lua_settop(L, 0); /* Empty the stack. */ |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 8715 | trace = hlua_traceback(L, ", "); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8716 | ha_alert("Lua init: %s: %s\n", kind, trace); |
| 8717 | return_status = 0; |
| 8718 | break; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8719 | } |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8720 | if (!return_status) |
| 8721 | break; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8722 | } |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 8723 | |
| 8724 | lua_atpanic(L, hlua_panic_safe); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8725 | return return_status; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8726 | } |
| 8727 | |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8728 | int hlua_post_init() |
| 8729 | { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8730 | int ret; |
| 8731 | int i; |
| 8732 | int errors; |
| 8733 | char *err = NULL; |
| 8734 | struct hlua_function *fcn; |
| 8735 | |
Willy Tarreau | 42afc59 | 2021-08-30 09:35:18 +0200 | [diff] [blame] | 8736 | #if defined(USE_OPENSSL) |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8737 | /* Initialize SSL server. */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 8738 | if (socket_ssl->xprt->prepare_srv) { |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8739 | int saved_used_backed = global.ssl_used_backend; |
| 8740 | // don't affect maxconn automatic computation |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 8741 | socket_ssl->xprt->prepare_srv(socket_ssl); |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8742 | global.ssl_used_backend = saved_used_backed; |
| 8743 | } |
| 8744 | #endif |
| 8745 | |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8746 | /* Perform post init of common thread */ |
| 8747 | hlua_state_id = 0; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8748 | ha_set_tid(0); |
| 8749 | ret = hlua_post_init_state(hlua_states[hlua_state_id]); |
| 8750 | if (ret == 0) |
| 8751 | return 0; |
| 8752 | |
| 8753 | /* init remaining lua states and load files */ |
| 8754 | for (hlua_state_id = 2; hlua_state_id < global.nbthread + 1; hlua_state_id++) { |
| 8755 | |
| 8756 | /* set thread context */ |
| 8757 | ha_set_tid(hlua_state_id - 1); |
| 8758 | |
| 8759 | /* Init lua state */ |
| 8760 | hlua_states[hlua_state_id] = hlua_init_state(hlua_state_id); |
| 8761 | |
| 8762 | /* Load lua files */ |
| 8763 | for (i = 0; per_thread_load && per_thread_load[i]; i++) { |
| 8764 | ret = hlua_load_state(per_thread_load[i], hlua_states[hlua_state_id], &err); |
| 8765 | if (ret != 0) { |
| 8766 | ha_alert("Lua init: %s\n", err); |
| 8767 | return 0; |
| 8768 | } |
| 8769 | } |
| 8770 | } |
| 8771 | |
| 8772 | /* Reset thread context */ |
| 8773 | ha_set_tid(0); |
| 8774 | |
| 8775 | /* Execute post init for all states */ |
| 8776 | for (hlua_state_id = 1; hlua_state_id < global.nbthread + 1; hlua_state_id++) { |
| 8777 | |
| 8778 | /* set thread context */ |
| 8779 | ha_set_tid(hlua_state_id - 1); |
| 8780 | |
| 8781 | /* run post init */ |
| 8782 | ret = hlua_post_init_state(hlua_states[hlua_state_id]); |
| 8783 | if (ret == 0) |
| 8784 | return 0; |
| 8785 | } |
| 8786 | |
| 8787 | /* Reset thread context */ |
| 8788 | ha_set_tid(0); |
| 8789 | |
| 8790 | /* control functions registering. Each function must have: |
| 8791 | * - only the function_ref[0] set positive and all other to -1 |
| 8792 | * - only the function_ref[0] set to -1 and all other positive |
| 8793 | * This ensure a same reference is not used both in shared |
| 8794 | * lua state and thread dedicated lua state. Note: is the case |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 8795 | * reach, the shared state is priority, but the bug will be |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8796 | * complicated to found for the end user. |
| 8797 | */ |
| 8798 | errors = 0; |
| 8799 | list_for_each_entry(fcn, &referenced_functions, l) { |
| 8800 | ret = 0; |
| 8801 | for (i = 1; i < global.nbthread + 1; i++) { |
| 8802 | if (fcn->function_ref[i] == -1) |
| 8803 | ret--; |
| 8804 | else |
| 8805 | ret++; |
| 8806 | } |
| 8807 | if (abs(ret) != global.nbthread) { |
| 8808 | ha_alert("Lua function '%s' is not referenced in all thread. " |
| 8809 | "Expect function in all thread or in none thread.\n", fcn->name); |
| 8810 | errors++; |
| 8811 | continue; |
| 8812 | } |
| 8813 | |
| 8814 | if ((fcn->function_ref[0] == -1) == (ret < 0)) { |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 8815 | ha_alert("Lua function '%s' is referenced both ins shared Lua context (through lua-load) " |
| 8816 | "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] | 8817 | "exclusive.\n", fcn->name); |
| 8818 | errors++; |
| 8819 | } |
| 8820 | } |
| 8821 | |
| 8822 | if (errors > 0) |
| 8823 | return 0; |
| 8824 | |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 8825 | /* 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] | 8826 | * -1 in order to have probably a segfault if someone use it |
| 8827 | */ |
| 8828 | hlua_state_id = -1; |
| 8829 | |
| 8830 | return 1; |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8831 | } |
| 8832 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8833 | /* The memory allocator used by the Lua stack. <ud> is a pointer to the |
| 8834 | * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize> |
| 8835 | * 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] | 8836 | * allocation. <nsize> is the requested new size. A new allocation is |
| 8837 | * indicated by <ptr> being NULL. A free is indicated by <nsize> being |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 8838 | * zero. This one verifies that the limits are respected but is optimized |
| 8839 | * 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] | 8840 | * |
| 8841 | * Warning: while this API ressembles glibc's realloc() a lot, glibc surpasses |
| 8842 | * POSIX by making realloc(ptr,0) an effective free(), but others do not do |
| 8843 | * that and will simply allocate zero as if it were the result of malloc(0), |
| 8844 | * so mapping this onto realloc() will lead to memory leaks on non-glibc |
| 8845 | * systems. |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8846 | */ |
| 8847 | static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize) |
| 8848 | { |
| 8849 | struct hlua_mem_allocator *zone = ud; |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 8850 | size_t limit, old, new; |
| 8851 | |
Tim Duesterhus | 2258652 | 2021-01-08 10:35:33 +0100 | [diff] [blame] | 8852 | if (unlikely(!ptr && !nsize)) |
| 8853 | return NULL; |
| 8854 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 8855 | /* a limit of ~0 means unlimited and boot complete, so there's no need |
| 8856 | * for accounting anymore. |
| 8857 | */ |
Willy Tarreau | e00ad05 | 2021-10-22 16:00:02 +0200 | [diff] [blame] | 8858 | if (likely(~zone->limit == 0)) { |
| 8859 | if (!nsize) |
| 8860 | ha_free(&ptr); |
| 8861 | else |
| 8862 | ptr = realloc(ptr, nsize); |
| 8863 | return ptr; |
| 8864 | } |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8865 | |
Willy Tarreau | d36c7fa | 2020-12-02 12:26:29 +0100 | [diff] [blame] | 8866 | if (!ptr) |
| 8867 | osize = 0; |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8868 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 8869 | /* enforce strict limits across all threads */ |
| 8870 | limit = zone->limit; |
| 8871 | old = _HA_ATOMIC_LOAD(&zone->allocated); |
| 8872 | do { |
| 8873 | new = old + nsize - osize; |
| 8874 | if (unlikely(nsize && limit && new > limit)) |
| 8875 | return NULL; |
| 8876 | } while (!_HA_ATOMIC_CAS(&zone->allocated, &old, new)); |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8877 | |
Willy Tarreau | e00ad05 | 2021-10-22 16:00:02 +0200 | [diff] [blame] | 8878 | if (!nsize) |
| 8879 | ha_free(&ptr); |
| 8880 | else |
| 8881 | ptr = realloc(ptr, nsize); |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 8882 | |
| 8883 | if (unlikely(!ptr && nsize)) // failed |
| 8884 | _HA_ATOMIC_SUB(&zone->allocated, nsize - osize); |
| 8885 | |
| 8886 | __ha_barrier_atomic_store(); |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8887 | return ptr; |
| 8888 | } |
| 8889 | |
Thierry Fournier | ecb83c2 | 2020-11-28 15:49:44 +0100 | [diff] [blame] | 8890 | /* 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] | 8891 | * We are in the initialisation process of HAProxy, this abort() is |
| 8892 | * tolerated. |
| 8893 | */ |
Thierry Fournier | ecb83c2 | 2020-11-28 15:49:44 +0100 | [diff] [blame] | 8894 | lua_State *hlua_init_state(int thread_num) |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 8895 | { |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8896 | int i; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8897 | int idx; |
| 8898 | struct sample_fetch *sf; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8899 | struct sample_conv *sc; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8900 | char *p; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8901 | const char *error_msg; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 8902 | void **context; |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8903 | lua_State *L; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8904 | struct prepend_path *pp; |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8905 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 8906 | /* Init main lua stack. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8907 | L = lua_newstate(hlua_alloc, &hlua_global_allocator); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 8908 | |
firexinghe | bdd336c | 2023-07-13 11:03:34 +0800 | [diff] [blame] | 8909 | if (!L) { |
| 8910 | fprintf(stderr, |
| 8911 | "Lua init: critical error: lua_newstate() returned NULL." |
| 8912 | " This may possibly be caused by a memory allocation error.\n"); |
| 8913 | exit(1); |
| 8914 | } |
| 8915 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 8916 | /* Initialise Lua context to NULL */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8917 | context = lua_getextraspace(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 8918 | *context = NULL; |
| 8919 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 8920 | /* From this point, until the end of the initialisation function, |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8921 | * the Lua function can fail with an abort. We are in the initialisation |
| 8922 | * process of HAProxy, this abort() is tolerated. |
| 8923 | */ |
| 8924 | |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 8925 | /* Call post initialisation function in safe environment. */ |
| 8926 | if (setjmp(safe_ljmp_env) != 0) { |
| 8927 | lua_atpanic(L, hlua_panic_safe); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8928 | if (lua_type(L, -1) == LUA_TSTRING) |
| 8929 | error_msg = lua_tostring(L, -1); |
Thierry Fournier | 2f05cc6 | 2020-11-28 16:08:02 +0100 | [diff] [blame] | 8930 | else |
| 8931 | error_msg = "critical error"; |
| 8932 | fprintf(stderr, "Lua init: %s.\n", error_msg); |
| 8933 | exit(1); |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 8934 | } else { |
| 8935 | lua_atpanic(L, hlua_panic_ljmp); |
Thierry Fournier | 2f05cc6 | 2020-11-28 16:08:02 +0100 | [diff] [blame] | 8936 | } |
| 8937 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 8938 | /* Initialise lua. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8939 | luaL_openlibs(L); |
Tim Duesterhus | 541fe1e | 2020-01-12 13:55:41 +0100 | [diff] [blame] | 8940 | #define HLUA_PREPEND_PATH_TOSTRING1(x) #x |
| 8941 | #define HLUA_PREPEND_PATH_TOSTRING(x) HLUA_PREPEND_PATH_TOSTRING1(x) |
| 8942 | #ifdef HLUA_PREPEND_PATH |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8943 | hlua_prepend_path(L, "path", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_PATH)); |
Tim Duesterhus | 541fe1e | 2020-01-12 13:55:41 +0100 | [diff] [blame] | 8944 | #endif |
| 8945 | #ifdef HLUA_PREPEND_CPATH |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8946 | hlua_prepend_path(L, "cpath", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_CPATH)); |
Tim Duesterhus | 541fe1e | 2020-01-12 13:55:41 +0100 | [diff] [blame] | 8947 | #endif |
| 8948 | #undef HLUA_PREPEND_PATH_TOSTRING |
| 8949 | #undef HLUA_PREPEND_PATH_TOSTRING1 |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8950 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8951 | /* Apply configured prepend path */ |
| 8952 | list_for_each_entry(pp, &prepend_path_list, l) |
| 8953 | hlua_prepend_path(L, pp->type, pp->path); |
| 8954 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8955 | /* |
| 8956 | * |
| 8957 | * Create "core" object. |
| 8958 | * |
| 8959 | */ |
| 8960 | |
Thierry FOURNIER | a2d8c65 | 2015-03-11 17:29:39 +0100 | [diff] [blame] | 8961 | /* This table entry is the object "core" base. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8962 | lua_newtable(L); |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8963 | |
Thierry Fournier | ecb83c2 | 2020-11-28 15:49:44 +0100 | [diff] [blame] | 8964 | /* set the thread id */ |
| 8965 | hlua_class_const_int(L, "thread", thread_num); |
| 8966 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8967 | /* Push the loglevel constants. */ |
Christopher Faulet | 8263e94 | 2024-02-29 15:41:17 +0100 | [diff] [blame] | 8968 | hlua_class_const_int(L, "silent", -1); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8969 | for (i = 0; i < NB_LOG_LEVELS; i++) |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8970 | hlua_class_const_int(L, log_levels[i], i); |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8971 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8972 | /* Register special functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8973 | hlua_class_function(L, "register_init", hlua_register_init); |
| 8974 | hlua_class_function(L, "register_task", hlua_register_task); |
| 8975 | hlua_class_function(L, "register_fetches", hlua_register_fetches); |
| 8976 | hlua_class_function(L, "register_converters", hlua_register_converters); |
| 8977 | hlua_class_function(L, "register_action", hlua_register_action); |
| 8978 | hlua_class_function(L, "register_service", hlua_register_service); |
| 8979 | hlua_class_function(L, "register_cli", hlua_register_cli); |
| 8980 | hlua_class_function(L, "yield", hlua_yield); |
| 8981 | hlua_class_function(L, "set_nice", hlua_set_nice); |
| 8982 | hlua_class_function(L, "sleep", hlua_sleep); |
| 8983 | hlua_class_function(L, "msleep", hlua_msleep); |
| 8984 | hlua_class_function(L, "add_acl", hlua_add_acl); |
| 8985 | hlua_class_function(L, "del_acl", hlua_del_acl); |
| 8986 | hlua_class_function(L, "set_map", hlua_set_map); |
| 8987 | hlua_class_function(L, "del_map", hlua_del_map); |
| 8988 | hlua_class_function(L, "tcp", hlua_socket_new); |
| 8989 | hlua_class_function(L, "log", hlua_log); |
| 8990 | hlua_class_function(L, "Debug", hlua_log_debug); |
| 8991 | hlua_class_function(L, "Info", hlua_log_info); |
| 8992 | hlua_class_function(L, "Warning", hlua_log_warning); |
| 8993 | hlua_class_function(L, "Alert", hlua_log_alert); |
| 8994 | hlua_class_function(L, "done", hlua_done); |
| 8995 | hlua_fcn_reg_core_fcn(L); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8996 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8997 | lua_setglobal(L, "core"); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 8998 | |
| 8999 | /* |
| 9000 | * |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 9001 | * Create "act" object. |
| 9002 | * |
| 9003 | */ |
| 9004 | |
| 9005 | /* This table entry is the object "act" base. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9006 | lua_newtable(L); |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 9007 | |
| 9008 | /* push action return constants */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9009 | hlua_class_const_int(L, "CONTINUE", ACT_RET_CONT); |
| 9010 | hlua_class_const_int(L, "STOP", ACT_RET_STOP); |
| 9011 | hlua_class_const_int(L, "YIELD", ACT_RET_YIELD); |
| 9012 | hlua_class_const_int(L, "ERROR", ACT_RET_ERR); |
| 9013 | hlua_class_const_int(L, "DONE", ACT_RET_DONE); |
| 9014 | hlua_class_const_int(L, "DENY", ACT_RET_DENY); |
| 9015 | hlua_class_const_int(L, "ABORT", ACT_RET_ABRT); |
| 9016 | hlua_class_const_int(L, "INVALID", ACT_RET_INV); |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 9017 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9018 | hlua_class_function(L, "wake_time", hlua_set_wake_time); |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 9019 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9020 | lua_setglobal(L, "act"); |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 9021 | |
| 9022 | /* |
| 9023 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 9024 | * Register class Map |
| 9025 | * |
| 9026 | */ |
| 9027 | |
| 9028 | /* This table entry is the object "Map" base. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9029 | lua_newtable(L); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 9030 | |
| 9031 | /* register pattern types. */ |
| 9032 | for (i=0; i<PAT_MATCH_NUM; i++) |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9033 | hlua_class_const_int(L, pat_match_names[i], i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 9034 | for (i=0; i<PAT_MATCH_NUM; i++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 9035 | snprintf(trash.area, trash.size, "_%s", pat_match_names[i]); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9036 | hlua_class_const_int(L, trash.area, i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 9037 | } |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 9038 | |
| 9039 | /* register constructor. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9040 | hlua_class_function(L, "new", hlua_map_new); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 9041 | |
| 9042 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9043 | lua_newtable(L); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 9044 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 9045 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9046 | lua_pushstring(L, "__index"); |
| 9047 | lua_newtable(L); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 9048 | |
| 9049 | /* Register . */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9050 | hlua_class_function(L, "lookup", hlua_map_lookup); |
| 9051 | hlua_class_function(L, "slookup", hlua_map_slookup); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 9052 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9053 | lua_rawset(L, -3); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 9054 | |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 9055 | /* Register previous table in the registry with reference and named entry. |
| 9056 | * The function hlua_register_metatable() pops the stack, so we |
| 9057 | * previously create a copy of the table. |
| 9058 | */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9059 | lua_pushvalue(L, -1); /* Copy the -1 entry and push it on the stack. */ |
| 9060 | class_map_ref = hlua_register_metatable(L, CLASS_MAP); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 9061 | |
| 9062 | /* Assign the metatable to the mai Map object. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9063 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 9064 | |
| 9065 | /* Set a name to the table. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9066 | lua_setglobal(L, "Map"); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 9067 | |
| 9068 | /* |
| 9069 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 9070 | * Register class Channel |
| 9071 | * |
| 9072 | */ |
| 9073 | |
| 9074 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9075 | lua_newtable(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 9076 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 9077 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9078 | lua_pushstring(L, "__index"); |
| 9079 | lua_newtable(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 9080 | |
| 9081 | /* Register . */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9082 | hlua_class_function(L, "get", hlua_channel_get); |
| 9083 | hlua_class_function(L, "dup", hlua_channel_dup); |
| 9084 | hlua_class_function(L, "getline", hlua_channel_getline); |
| 9085 | hlua_class_function(L, "set", hlua_channel_set); |
| 9086 | hlua_class_function(L, "append", hlua_channel_append); |
| 9087 | hlua_class_function(L, "send", hlua_channel_send); |
| 9088 | hlua_class_function(L, "forward", hlua_channel_forward); |
| 9089 | hlua_class_function(L, "get_in_len", hlua_channel_get_in_len); |
| 9090 | hlua_class_function(L, "get_out_len", hlua_channel_get_out_len); |
| 9091 | hlua_class_function(L, "is_full", hlua_channel_is_full); |
| 9092 | hlua_class_function(L, "is_resp", hlua_channel_is_resp); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 9093 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9094 | lua_rawset(L, -3); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 9095 | |
| 9096 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9097 | class_channel_ref = hlua_register_metatable(L, CLASS_CHANNEL); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 9098 | |
| 9099 | /* |
| 9100 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 9101 | * Register class Fetches |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 9102 | * |
| 9103 | */ |
| 9104 | |
| 9105 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9106 | lua_newtable(L); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 9107 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 9108 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9109 | lua_pushstring(L, "__index"); |
| 9110 | lua_newtable(L); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 9111 | |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 9112 | /* Browse existing fetches and create the associated |
| 9113 | * object method. |
| 9114 | */ |
| 9115 | sf = NULL; |
| 9116 | while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) { |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 9117 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 9118 | * by an underscore. |
| 9119 | */ |
Willy Tarreau | fa92f36 | 2021-08-26 16:48:53 +0200 | [diff] [blame] | 9120 | strlcpy2(trash.area, sf->kw, trash.size); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 9121 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 9122 | if (*p == '.' || *p == '-' || *p == '+') |
| 9123 | *p = '_'; |
| 9124 | |
| 9125 | /* Register the function. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9126 | lua_pushstring(L, trash.area); |
| 9127 | lua_pushlightuserdata(L, sf); |
| 9128 | lua_pushcclosure(L, hlua_run_sample_fetch, 1); |
| 9129 | lua_rawset(L, -3); |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 9130 | } |
| 9131 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9132 | lua_rawset(L, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 9133 | |
| 9134 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9135 | class_fetches_ref = hlua_register_metatable(L, CLASS_FETCHES); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 9136 | |
| 9137 | /* |
| 9138 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 9139 | * Register class Converters |
| 9140 | * |
| 9141 | */ |
| 9142 | |
| 9143 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9144 | lua_newtable(L); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 9145 | |
| 9146 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9147 | lua_pushstring(L, "__index"); |
| 9148 | lua_newtable(L); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 9149 | |
| 9150 | /* Browse existing converters and create the associated |
| 9151 | * object method. |
| 9152 | */ |
| 9153 | sc = NULL; |
| 9154 | while ((sc = sample_conv_getnext(sc, &idx)) != NULL) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 9155 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 9156 | * by an underscore. |
| 9157 | */ |
Willy Tarreau | fa92f36 | 2021-08-26 16:48:53 +0200 | [diff] [blame] | 9158 | strlcpy2(trash.area, sc->kw, trash.size); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 9159 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 9160 | if (*p == '.' || *p == '-' || *p == '+') |
| 9161 | *p = '_'; |
| 9162 | |
| 9163 | /* Register the function. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9164 | lua_pushstring(L, trash.area); |
| 9165 | lua_pushlightuserdata(L, sc); |
| 9166 | lua_pushcclosure(L, hlua_run_sample_conv, 1); |
| 9167 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 9168 | } |
| 9169 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9170 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 9171 | |
| 9172 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9173 | class_converters_ref = hlua_register_metatable(L, CLASS_CONVERTERS); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 9174 | |
| 9175 | /* |
| 9176 | * |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 9177 | * Register class HTTP |
| 9178 | * |
| 9179 | */ |
| 9180 | |
| 9181 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9182 | lua_newtable(L); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 9183 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 9184 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9185 | lua_pushstring(L, "__index"); |
| 9186 | lua_newtable(L); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 9187 | |
| 9188 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9189 | hlua_class_function(L, "req_get_headers",hlua_http_req_get_headers); |
| 9190 | hlua_class_function(L, "req_del_header", hlua_http_req_del_hdr); |
| 9191 | hlua_class_function(L, "req_rep_header", hlua_http_req_rep_hdr); |
| 9192 | hlua_class_function(L, "req_rep_value", hlua_http_req_rep_val); |
| 9193 | hlua_class_function(L, "req_add_header", hlua_http_req_add_hdr); |
| 9194 | hlua_class_function(L, "req_set_header", hlua_http_req_set_hdr); |
| 9195 | hlua_class_function(L, "req_set_method", hlua_http_req_set_meth); |
| 9196 | hlua_class_function(L, "req_set_path", hlua_http_req_set_path); |
| 9197 | hlua_class_function(L, "req_set_query", hlua_http_req_set_query); |
| 9198 | hlua_class_function(L, "req_set_uri", hlua_http_req_set_uri); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 9199 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9200 | hlua_class_function(L, "res_get_headers",hlua_http_res_get_headers); |
| 9201 | hlua_class_function(L, "res_del_header", hlua_http_res_del_hdr); |
| 9202 | hlua_class_function(L, "res_rep_header", hlua_http_res_rep_hdr); |
| 9203 | hlua_class_function(L, "res_rep_value", hlua_http_res_rep_val); |
| 9204 | hlua_class_function(L, "res_add_header", hlua_http_res_add_hdr); |
| 9205 | hlua_class_function(L, "res_set_header", hlua_http_res_set_hdr); |
| 9206 | hlua_class_function(L, "res_set_status", hlua_http_res_set_status); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 9207 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9208 | lua_rawset(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 9209 | |
| 9210 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9211 | class_http_ref = hlua_register_metatable(L, CLASS_HTTP); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 9212 | |
| 9213 | /* |
| 9214 | * |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9215 | * Register class AppletTCP |
| 9216 | * |
| 9217 | */ |
| 9218 | |
| 9219 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9220 | lua_newtable(L); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9221 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 9222 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9223 | lua_pushstring(L, "__index"); |
| 9224 | lua_newtable(L); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9225 | |
| 9226 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9227 | hlua_class_function(L, "getline", hlua_applet_tcp_getline); |
| 9228 | hlua_class_function(L, "receive", hlua_applet_tcp_recv); |
| 9229 | hlua_class_function(L, "send", hlua_applet_tcp_send); |
| 9230 | hlua_class_function(L, "set_priv", hlua_applet_tcp_set_priv); |
| 9231 | hlua_class_function(L, "get_priv", hlua_applet_tcp_get_priv); |
| 9232 | hlua_class_function(L, "set_var", hlua_applet_tcp_set_var); |
| 9233 | hlua_class_function(L, "unset_var", hlua_applet_tcp_unset_var); |
| 9234 | hlua_class_function(L, "get_var", hlua_applet_tcp_get_var); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9235 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9236 | lua_settable(L, -3); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9237 | |
| 9238 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9239 | class_applet_tcp_ref = hlua_register_metatable(L, CLASS_APPLET_TCP); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9240 | |
| 9241 | /* |
| 9242 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9243 | * Register class AppletHTTP |
| 9244 | * |
| 9245 | */ |
| 9246 | |
| 9247 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9248 | lua_newtable(L); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9249 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 9250 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9251 | lua_pushstring(L, "__index"); |
| 9252 | lua_newtable(L); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9253 | |
| 9254 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9255 | hlua_class_function(L, "set_priv", hlua_applet_http_set_priv); |
| 9256 | hlua_class_function(L, "get_priv", hlua_applet_http_get_priv); |
| 9257 | hlua_class_function(L, "set_var", hlua_applet_http_set_var); |
| 9258 | hlua_class_function(L, "unset_var", hlua_applet_http_unset_var); |
| 9259 | hlua_class_function(L, "get_var", hlua_applet_http_get_var); |
| 9260 | hlua_class_function(L, "getline", hlua_applet_http_getline); |
| 9261 | hlua_class_function(L, "receive", hlua_applet_http_recv); |
| 9262 | hlua_class_function(L, "send", hlua_applet_http_send); |
| 9263 | hlua_class_function(L, "add_header", hlua_applet_http_addheader); |
| 9264 | hlua_class_function(L, "set_status", hlua_applet_http_status); |
| 9265 | hlua_class_function(L, "start_response", hlua_applet_http_start_response); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9266 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9267 | lua_settable(L, -3); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9268 | |
| 9269 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9270 | class_applet_http_ref = hlua_register_metatable(L, CLASS_APPLET_HTTP); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9271 | |
| 9272 | /* |
| 9273 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 9274 | * Register class TXN |
| 9275 | * |
| 9276 | */ |
| 9277 | |
| 9278 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9279 | lua_newtable(L); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 9280 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 9281 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9282 | lua_pushstring(L, "__index"); |
| 9283 | lua_newtable(L); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 9284 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 9285 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9286 | hlua_class_function(L, "set_priv", hlua_set_priv); |
| 9287 | hlua_class_function(L, "get_priv", hlua_get_priv); |
| 9288 | hlua_class_function(L, "set_var", hlua_set_var); |
| 9289 | hlua_class_function(L, "unset_var", hlua_unset_var); |
| 9290 | hlua_class_function(L, "get_var", hlua_get_var); |
| 9291 | hlua_class_function(L, "done", hlua_txn_done); |
| 9292 | hlua_class_function(L, "reply", hlua_txn_reply_new); |
| 9293 | hlua_class_function(L, "set_loglevel", hlua_txn_set_loglevel); |
| 9294 | hlua_class_function(L, "set_tos", hlua_txn_set_tos); |
| 9295 | hlua_class_function(L, "set_mark", hlua_txn_set_mark); |
| 9296 | hlua_class_function(L, "set_priority_class", hlua_txn_set_priority_class); |
| 9297 | hlua_class_function(L, "set_priority_offset", hlua_txn_set_priority_offset); |
| 9298 | hlua_class_function(L, "deflog", hlua_txn_deflog); |
| 9299 | hlua_class_function(L, "log", hlua_txn_log); |
| 9300 | hlua_class_function(L, "Debug", hlua_txn_log_debug); |
| 9301 | hlua_class_function(L, "Info", hlua_txn_log_info); |
| 9302 | hlua_class_function(L, "Warning", hlua_txn_log_warning); |
| 9303 | hlua_class_function(L, "Alert", hlua_txn_log_alert); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 9304 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9305 | lua_rawset(L, -3); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 9306 | |
| 9307 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9308 | class_txn_ref = hlua_register_metatable(L, CLASS_TXN); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9309 | |
| 9310 | /* |
| 9311 | * |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 9312 | * Register class reply |
| 9313 | * |
| 9314 | */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9315 | lua_newtable(L); |
| 9316 | lua_pushstring(L, "__index"); |
| 9317 | lua_newtable(L); |
| 9318 | hlua_class_function(L, "set_status", hlua_txn_reply_set_status); |
| 9319 | hlua_class_function(L, "add_header", hlua_txn_reply_add_header); |
| 9320 | hlua_class_function(L, "del_header", hlua_txn_reply_del_header); |
| 9321 | hlua_class_function(L, "set_body", hlua_txn_reply_set_body); |
| 9322 | lua_settable(L, -3); /* Sets the __index entry. */ |
| 9323 | class_txn_reply_ref = luaL_ref(L, LUA_REGISTRYINDEX); |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 9324 | |
| 9325 | |
| 9326 | /* |
| 9327 | * |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9328 | * Register class Socket |
| 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 | 7e7ac32 | 2015-02-16 19:27:16 +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 | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9338 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 9339 | #ifdef USE_OPENSSL |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9340 | hlua_class_function(L, "connect_ssl", hlua_socket_connect_ssl); |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 9341 | #endif |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9342 | hlua_class_function(L, "connect", hlua_socket_connect); |
| 9343 | hlua_class_function(L, "send", hlua_socket_send); |
| 9344 | hlua_class_function(L, "receive", hlua_socket_receive); |
| 9345 | hlua_class_function(L, "close", hlua_socket_close); |
| 9346 | hlua_class_function(L, "getpeername", hlua_socket_getpeername); |
| 9347 | hlua_class_function(L, "getsockname", hlua_socket_getsockname); |
| 9348 | hlua_class_function(L, "setoption", hlua_socket_setoption); |
| 9349 | hlua_class_function(L, "settimeout", hlua_socket_settimeout); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9350 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9351 | 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] | 9352 | |
| 9353 | /* Register the garbage collector entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9354 | lua_pushstring(L, "__gc"); |
| 9355 | lua_pushcclosure(L, hlua_socket_gc, 0); |
| 9356 | 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] | 9357 | |
| 9358 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9359 | class_socket_ref = hlua_register_metatable(L, CLASS_SOCKET); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9360 | |
Thierry Fournier | aafc777 | 2020-12-04 11:47:47 +0100 | [diff] [blame] | 9361 | lua_atpanic(L, hlua_panic_safe); |
| 9362 | |
| 9363 | return L; |
| 9364 | } |
| 9365 | |
| 9366 | void hlua_init(void) { |
| 9367 | int i; |
Amaury Denoyelle | 239fdbf | 2021-03-24 10:22:03 +0100 | [diff] [blame] | 9368 | char *errmsg; |
Thierry Fournier | aafc777 | 2020-12-04 11:47:47 +0100 | [diff] [blame] | 9369 | #ifdef USE_OPENSSL |
| 9370 | struct srv_kw *kw; |
| 9371 | int tmp_error; |
| 9372 | char *error; |
| 9373 | char *args[] = { /* SSL client configuration. */ |
| 9374 | "ssl", |
| 9375 | "verify", |
| 9376 | "none", |
| 9377 | NULL |
| 9378 | }; |
| 9379 | #endif |
| 9380 | |
| 9381 | /* Init post init function list head */ |
| 9382 | for (i = 0; i < MAX_THREADS + 1; i++) |
| 9383 | LIST_INIT(&hlua_init_functions[i]); |
| 9384 | |
| 9385 | /* Init state for common/shared lua parts */ |
| 9386 | hlua_state_id = 0; |
| 9387 | ha_set_tid(0); |
| 9388 | hlua_states[0] = hlua_init_state(0); |
| 9389 | |
| 9390 | /* Init state 1 for thread 0. We have at least one thread. */ |
| 9391 | hlua_state_id = 1; |
| 9392 | ha_set_tid(0); |
| 9393 | hlua_states[1] = hlua_init_state(1); |
| 9394 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9395 | /* Proxy and server configuration initialisation. */ |
Amaury Denoyelle | 239fdbf | 2021-03-24 10:22:03 +0100 | [diff] [blame] | 9396 | socket_proxy = alloc_new_proxy("LUA-SOCKET", PR_CAP_FE|PR_CAP_BE|PR_CAP_LUA, &errmsg); |
| 9397 | if (!socket_proxy) { |
| 9398 | fprintf(stderr, "Lua init: %s\n", errmsg); |
| 9399 | exit(1); |
| 9400 | } |
| 9401 | proxy_preset_defaults(socket_proxy); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9402 | |
| 9403 | /* Init TCP server: unchanged parameters */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 9404 | socket_tcp = new_server(socket_proxy); |
| 9405 | if (!socket_tcp) { |
| 9406 | fprintf(stderr, "Lua init: failed to allocate tcp server socket\n"); |
| 9407 | exit(1); |
| 9408 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9409 | |
| 9410 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9411 | /* Init TCP server: unchanged parameters */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 9412 | socket_ssl = new_server(socket_proxy); |
| 9413 | if (!socket_ssl) { |
| 9414 | fprintf(stderr, "Lua init: failed to allocate ssl server socket\n"); |
| 9415 | exit(1); |
| 9416 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9417 | |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 9418 | socket_ssl->use_ssl = 1; |
| 9419 | socket_ssl->xprt = xprt_get(XPRT_SSL); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9420 | |
Bertrand Jacquin | 80839ff | 2021-01-21 19:14:46 +0000 | [diff] [blame] | 9421 | for (i = 0; args[i] != NULL; i++) { |
| 9422 | 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] | 9423 | /* |
| 9424 | * |
| 9425 | * If the keyword is not known, we can search in the registered |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 9426 | * server keywords. This is useful to configure special SSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9427 | * features like client certificates and ssl_verify. |
| 9428 | * |
| 9429 | */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 9430 | tmp_error = kw->parse(args, &i, socket_proxy, socket_ssl, &error); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9431 | if (tmp_error != 0) { |
| 9432 | fprintf(stderr, "INTERNAL ERROR: %s\n", error); |
| 9433 | abort(); /* This must be never arrives because the command line |
| 9434 | not editable by the user. */ |
| 9435 | } |
Bertrand Jacquin | 80839ff | 2021-01-21 19:14:46 +0000 | [diff] [blame] | 9436 | i += kw->skip; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9437 | } |
| 9438 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9439 | #endif |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 9440 | |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 9441 | } |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 9442 | |
Tim Duesterhus | d0c0ca2 | 2020-07-04 11:53:26 +0200 | [diff] [blame] | 9443 | static void hlua_deinit() |
| 9444 | { |
Willy Tarreau | 186f376 | 2020-12-04 11:48:12 +0100 | [diff] [blame] | 9445 | int thr; |
| 9446 | |
| 9447 | for (thr = 0; thr < MAX_THREADS+1; thr++) { |
| 9448 | if (hlua_states[thr]) |
| 9449 | lua_close(hlua_states[thr]); |
| 9450 | } |
Willy Tarreau | 430bf4a | 2021-03-04 09:45:32 +0100 | [diff] [blame] | 9451 | |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 9452 | free_server(socket_tcp); |
Willy Tarreau | 430bf4a | 2021-03-04 09:45:32 +0100 | [diff] [blame] | 9453 | |
Willy Tarreau | 0f143af | 2021-03-05 10:41:48 +0100 | [diff] [blame] | 9454 | #ifdef USE_OPENSSL |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 9455 | free_server(socket_ssl); |
Willy Tarreau | 0f143af | 2021-03-05 10:41:48 +0100 | [diff] [blame] | 9456 | #endif |
Amaury Denoyelle | 239fdbf | 2021-03-24 10:22:03 +0100 | [diff] [blame] | 9457 | |
| 9458 | free_proxy(socket_proxy); |
Tim Duesterhus | d0c0ca2 | 2020-07-04 11:53:26 +0200 | [diff] [blame] | 9459 | } |
| 9460 | |
| 9461 | REGISTER_POST_DEINIT(hlua_deinit); |
| 9462 | |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 9463 | static void hlua_register_build_options(void) |
| 9464 | { |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 9465 | char *ptr = NULL; |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 9466 | |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 9467 | memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE); |
| 9468 | hap_register_build_opts(ptr, 1); |
| 9469 | } |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 9470 | |
| 9471 | INITCALL0(STG_REGISTER, hlua_register_build_options); |