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); |
| 1445 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 1446 | trace = hlua_traceback(lua->T, ", "); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1447 | if (msg) |
Thierry Fournier | 46278ff | 2020-11-29 11:48:12 +0100 | [diff] [blame] | 1448 | 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] | 1449 | else |
Thierry Fournier | 46278ff | 2020-11-29 11:48:12 +0100 | [diff] [blame] | 1450 | lua_pushfstring(lua->T, "[state-id %d] unknown runtime error from %s", lua->state_id, trace); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1451 | ret = HLUA_E_ERRMSG; |
| 1452 | break; |
| 1453 | |
| 1454 | case LUA_ERRMEM: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1455 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1456 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1457 | ret = HLUA_E_NOMEM; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1458 | break; |
| 1459 | |
| 1460 | case LUA_ERRERR: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1461 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1462 | if (!lua_checkstack(lua->T, 1)) { |
| 1463 | ret = HLUA_E_ERR; |
| 1464 | break; |
| 1465 | } |
| 1466 | msg = lua_tostring(lua->T, -1); |
| 1467 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1468 | if (msg) |
Thierry Fournier | 46278ff | 2020-11-29 11:48:12 +0100 | [diff] [blame] | 1469 | 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] | 1470 | else |
Thierry Fournier | 46278ff | 2020-11-29 11:48:12 +0100 | [diff] [blame] | 1471 | lua_pushfstring(lua->T, "[state-id %d] message handler error", lua->state_id); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1472 | ret = HLUA_E_ERRMSG; |
| 1473 | break; |
| 1474 | |
| 1475 | default: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1476 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1477 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1478 | ret = HLUA_E_ERR; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1479 | break; |
| 1480 | } |
| 1481 | |
| 1482 | switch (ret) { |
| 1483 | case HLUA_E_AGAIN: |
| 1484 | break; |
| 1485 | |
| 1486 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1487 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1488 | hlua_ctx_renew(lua, 1); |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1489 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1490 | break; |
| 1491 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1492 | case HLUA_E_ETMOUT: |
| 1493 | case HLUA_E_NOMEM: |
| 1494 | case HLUA_E_YIELD: |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1495 | case HLUA_E_ERR: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1496 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1497 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1498 | hlua_ctx_renew(lua, 0); |
| 1499 | break; |
| 1500 | |
| 1501 | case HLUA_E_OK: |
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 | break; |
| 1505 | } |
| 1506 | |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1507 | /* This is the main exit point, remove the Lua lock. */ |
Aurelien DARRAGON | e00bf5d | 2023-03-21 13:22:33 +0100 | [diff] [blame] | 1508 | hlua_unlock(lua); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1509 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1510 | return ret; |
| 1511 | } |
| 1512 | |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1513 | /* This function exit the current code. */ |
| 1514 | __LJMP static int hlua_done(lua_State *L) |
| 1515 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 1516 | struct hlua *hlua; |
| 1517 | |
| 1518 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 1519 | hlua = hlua_gethlua(L); |
| 1520 | if (!hlua) |
| 1521 | return 0; |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1522 | |
| 1523 | hlua->flags |= HLUA_EXIT; |
| 1524 | WILL_LJMP(lua_error(L)); |
| 1525 | |
| 1526 | return 0; |
| 1527 | } |
| 1528 | |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1529 | /* This function is an LUA binding. It provides a function |
| 1530 | * for deleting ACL from a referenced ACL file. |
| 1531 | */ |
| 1532 | __LJMP static int hlua_del_acl(lua_State *L) |
| 1533 | { |
| 1534 | const char *name; |
| 1535 | const char *key; |
| 1536 | struct pat_ref *ref; |
| 1537 | |
| 1538 | MAY_LJMP(check_args(L, 2, "del_acl")); |
| 1539 | |
| 1540 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1541 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1542 | |
| 1543 | ref = pat_ref_lookup(name); |
| 1544 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1545 | WILL_LJMP(luaL_error(L, "'del_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1546 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1547 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1548 | pat_ref_delete(ref, key); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1549 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1550 | return 0; |
| 1551 | } |
| 1552 | |
| 1553 | /* This function is an LUA binding. It provides a function |
| 1554 | * for deleting map entry from a referenced map file. |
| 1555 | */ |
| 1556 | static int hlua_del_map(lua_State *L) |
| 1557 | { |
| 1558 | const char *name; |
| 1559 | const char *key; |
| 1560 | struct pat_ref *ref; |
| 1561 | |
| 1562 | MAY_LJMP(check_args(L, 2, "del_map")); |
| 1563 | |
| 1564 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1565 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1566 | |
| 1567 | ref = pat_ref_lookup(name); |
| 1568 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1569 | WILL_LJMP(luaL_error(L, "'del_map': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1570 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1571 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1572 | pat_ref_delete(ref, key); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1573 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1574 | return 0; |
| 1575 | } |
| 1576 | |
| 1577 | /* This function is an LUA binding. It provides a function |
| 1578 | * for adding ACL pattern from a referenced ACL file. |
| 1579 | */ |
| 1580 | static int hlua_add_acl(lua_State *L) |
| 1581 | { |
| 1582 | const char *name; |
| 1583 | const char *key; |
| 1584 | struct pat_ref *ref; |
| 1585 | |
| 1586 | MAY_LJMP(check_args(L, 2, "add_acl")); |
| 1587 | |
| 1588 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1589 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1590 | |
| 1591 | ref = pat_ref_lookup(name); |
| 1592 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1593 | WILL_LJMP(luaL_error(L, "'add_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1594 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1595 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1596 | if (pat_ref_find_elt(ref, key) == NULL) |
| 1597 | pat_ref_add(ref, key, NULL, NULL); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1598 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1599 | return 0; |
| 1600 | } |
| 1601 | |
| 1602 | /* This function is an LUA binding. It provides a function |
| 1603 | * for setting map pattern and sample from a referenced map |
| 1604 | * file. |
| 1605 | */ |
| 1606 | static int hlua_set_map(lua_State *L) |
| 1607 | { |
| 1608 | const char *name; |
| 1609 | const char *key; |
| 1610 | const char *value; |
| 1611 | struct pat_ref *ref; |
| 1612 | |
| 1613 | MAY_LJMP(check_args(L, 3, "set_map")); |
| 1614 | |
| 1615 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1616 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1617 | value = MAY_LJMP(luaL_checkstring(L, 3)); |
| 1618 | |
| 1619 | ref = pat_ref_lookup(name); |
| 1620 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1621 | WILL_LJMP(luaL_error(L, "'set_map': unknown map file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1622 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1623 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1624 | if (pat_ref_find_elt(ref, key) != NULL) |
| 1625 | pat_ref_set(ref, key, value, NULL); |
| 1626 | else |
| 1627 | pat_ref_add(ref, key, value, NULL); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1628 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1629 | return 0; |
| 1630 | } |
| 1631 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1632 | /* A class is a lot of memory that contain data. This data can be a table, |
| 1633 | * 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] | 1634 | * metatable have an original version registered in the global context with |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1635 | * the name of the object (_G[<name>] = <metable> ). |
| 1636 | * |
| 1637 | * A metable is a table that modify the standard behavior of a standard |
| 1638 | * access to the associated data. The entries of this new metatable are |
| 1639 | * defined as is: |
| 1640 | * |
| 1641 | * http://lua-users.org/wiki/MetatableEvents |
| 1642 | * |
| 1643 | * __index |
| 1644 | * |
| 1645 | * we access an absent field in a table, the result is nil. This is |
| 1646 | * true, but it is not the whole truth. Actually, such access triggers |
| 1647 | * the interpreter to look for an __index metamethod: If there is no |
| 1648 | * such method, as usually happens, then the access results in nil; |
| 1649 | * otherwise, the metamethod will provide the result. |
| 1650 | * |
| 1651 | * Control 'prototype' inheritance. When accessing "myTable[key]" and |
| 1652 | * the key does not appear in the table, but the metatable has an __index |
| 1653 | * property: |
| 1654 | * |
| 1655 | * - if the value is a function, the function is called, passing in the |
| 1656 | * table and the key; the return value of that function is returned as |
| 1657 | * the result. |
| 1658 | * |
| 1659 | * - if the value is another table, the value of the key in that table is |
| 1660 | * asked for and returned (and if it doesn't exist in that table, but that |
| 1661 | * table's metatable has an __index property, then it continues on up) |
| 1662 | * |
| 1663 | * - Use "rawget(myTable,key)" to skip this metamethod. |
| 1664 | * |
| 1665 | * http://www.lua.org/pil/13.4.1.html |
| 1666 | * |
| 1667 | * __newindex |
| 1668 | * |
| 1669 | * Like __index, but control property assignment. |
| 1670 | * |
| 1671 | * __mode - Control weak references. A string value with one or both |
| 1672 | * of the characters 'k' and 'v' which specifies that the the |
| 1673 | * keys and/or values in the table are weak references. |
| 1674 | * |
| 1675 | * __call - Treat a table like a function. When a table is followed by |
| 1676 | * parenthesis such as "myTable( 'foo' )" and the metatable has |
| 1677 | * a __call key pointing to a function, that function is invoked |
| 1678 | * (passing any specified arguments) and the return value is |
| 1679 | * returned. |
| 1680 | * |
| 1681 | * __metatable - Hide the metatable. When "getmetatable( myTable )" is |
| 1682 | * called, if the metatable for myTable has a __metatable |
| 1683 | * key, the value of that key is returned instead of the |
| 1684 | * actual metatable. |
| 1685 | * |
| 1686 | * __tostring - Control string representation. When the builtin |
| 1687 | * "tostring( myTable )" function is called, if the metatable |
| 1688 | * for myTable has a __tostring property set to a function, |
| 1689 | * that function is invoked (passing myTable to it) and the |
| 1690 | * return value is used as the string representation. |
| 1691 | * |
| 1692 | * __len - Control table length. When the table length is requested using |
| 1693 | * the length operator ( '#' ), if the metatable for myTable has |
| 1694 | * a __len key pointing to a function, that function is invoked |
| 1695 | * (passing myTable to it) and the return value used as the value |
| 1696 | * of "#myTable". |
| 1697 | * |
| 1698 | * __gc - Userdata finalizer code. When userdata is set to be garbage |
| 1699 | * collected, if the metatable has a __gc field pointing to a |
| 1700 | * function, that function is first invoked, passing the userdata |
| 1701 | * to it. The __gc metamethod is not called for tables. |
| 1702 | * (See http://lua-users.org/lists/lua-l/2006-11/msg00508.html) |
| 1703 | * |
| 1704 | * Special metamethods for redefining standard operators: |
| 1705 | * http://www.lua.org/pil/13.1.html |
| 1706 | * |
| 1707 | * __add "+" |
| 1708 | * __sub "-" |
| 1709 | * __mul "*" |
| 1710 | * __div "/" |
| 1711 | * __unm "!" |
| 1712 | * __pow "^" |
| 1713 | * __concat ".." |
| 1714 | * |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1715 | * Special methods for redefining standard relations |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1716 | * http://www.lua.org/pil/13.2.html |
| 1717 | * |
| 1718 | * __eq "==" |
| 1719 | * __lt "<" |
| 1720 | * __le "<=" |
| 1721 | */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1722 | |
| 1723 | /* |
| 1724 | * |
| 1725 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1726 | * Class Map |
| 1727 | * |
| 1728 | * |
| 1729 | */ |
| 1730 | |
| 1731 | /* Returns a struct hlua_map if the stack entry "ud" is |
| 1732 | * a class session, otherwise it throws an error. |
| 1733 | */ |
| 1734 | __LJMP static struct map_descriptor *hlua_checkmap(lua_State *L, int ud) |
| 1735 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1736 | return MAY_LJMP(hlua_checkudata(L, ud, class_map_ref)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1737 | } |
| 1738 | |
| 1739 | /* This function is the map constructor. It don't need |
| 1740 | * the class Map object. It creates and return a new Map |
| 1741 | * object. It must be called only during "body" or "init" |
| 1742 | * context because it process some filesystem accesses. |
| 1743 | */ |
| 1744 | __LJMP static int hlua_map_new(struct lua_State *L) |
| 1745 | { |
| 1746 | const char *fn; |
| 1747 | int match = PAT_MATCH_STR; |
| 1748 | struct sample_conv conv; |
| 1749 | const char *file = ""; |
| 1750 | int line = 0; |
| 1751 | lua_Debug ar; |
| 1752 | char *err = NULL; |
| 1753 | struct arg args[2]; |
| 1754 | |
| 1755 | if (lua_gettop(L) < 1 || lua_gettop(L) > 2) |
| 1756 | WILL_LJMP(luaL_error(L, "'new' needs at least 1 argument.")); |
| 1757 | |
| 1758 | fn = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1759 | |
| 1760 | if (lua_gettop(L) >= 2) { |
| 1761 | match = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 1762 | if (match < 0 || match >= PAT_MATCH_NUM) |
| 1763 | WILL_LJMP(luaL_error(L, "'new' needs a valid match method.")); |
| 1764 | } |
| 1765 | |
| 1766 | /* Get Lua filename and line number. */ |
| 1767 | if (lua_getstack(L, 1, &ar)) { /* check function at level */ |
| 1768 | lua_getinfo(L, "Sl", &ar); /* get info about it */ |
| 1769 | if (ar.currentline > 0) { /* is there info? */ |
| 1770 | file = ar.short_src; |
| 1771 | line = ar.currentline; |
| 1772 | } |
| 1773 | } |
| 1774 | |
| 1775 | /* fill fake sample_conv struct. */ |
| 1776 | conv.kw = ""; /* unused. */ |
| 1777 | conv.process = NULL; /* unused. */ |
| 1778 | conv.arg_mask = 0; /* unused. */ |
| 1779 | conv.val_args = NULL; /* unused. */ |
| 1780 | conv.out_type = SMP_T_STR; |
| 1781 | conv.private = (void *)(long)match; |
| 1782 | switch (match) { |
| 1783 | case PAT_MATCH_STR: conv.in_type = SMP_T_STR; break; |
| 1784 | case PAT_MATCH_BEG: conv.in_type = SMP_T_STR; break; |
| 1785 | case PAT_MATCH_SUB: conv.in_type = SMP_T_STR; break; |
| 1786 | case PAT_MATCH_DIR: conv.in_type = SMP_T_STR; break; |
| 1787 | case PAT_MATCH_DOM: conv.in_type = SMP_T_STR; break; |
| 1788 | case PAT_MATCH_END: conv.in_type = SMP_T_STR; break; |
| 1789 | case PAT_MATCH_REG: conv.in_type = SMP_T_STR; break; |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1790 | case PAT_MATCH_INT: conv.in_type = SMP_T_SINT; break; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1791 | case PAT_MATCH_IP: conv.in_type = SMP_T_ADDR; break; |
| 1792 | default: |
| 1793 | WILL_LJMP(luaL_error(L, "'new' doesn't support this match mode.")); |
| 1794 | } |
| 1795 | |
| 1796 | /* fill fake args. */ |
| 1797 | args[0].type = ARGT_STR; |
Christopher Faulet | 73292e9 | 2020-08-06 08:40:09 +0200 | [diff] [blame] | 1798 | args[0].data.str.area = strdup(fn); |
| 1799 | args[0].data.str.data = strlen(fn); |
| 1800 | args[0].data.str.size = args[0].data.str.data+1; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1801 | args[1].type = ARGT_STOP; |
| 1802 | |
| 1803 | /* load the map. */ |
| 1804 | if (!sample_load_map(args, &conv, file, line, &err)) { |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 1805 | /* error case: we can't use luaL_error because we must |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1806 | * free the err variable. |
| 1807 | */ |
| 1808 | luaL_where(L, 1); |
| 1809 | lua_pushfstring(L, "'new': %s.", err); |
| 1810 | lua_concat(L, 2); |
| 1811 | free(err); |
Christopher Faulet | 6ad7df4 | 2020-08-07 11:45:18 +0200 | [diff] [blame] | 1812 | chunk_destroy(&args[0].data.str); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1813 | WILL_LJMP(lua_error(L)); |
| 1814 | } |
| 1815 | |
| 1816 | /* create the lua object. */ |
| 1817 | lua_newtable(L); |
| 1818 | lua_pushlightuserdata(L, args[0].data.map); |
| 1819 | lua_rawseti(L, -2, 0); |
| 1820 | |
| 1821 | /* Pop a class Map metatable and affect it to the userdata. */ |
| 1822 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_map_ref); |
| 1823 | lua_setmetatable(L, -2); |
| 1824 | |
| 1825 | |
| 1826 | return 1; |
| 1827 | } |
| 1828 | |
| 1829 | __LJMP static inline int _hlua_map_lookup(struct lua_State *L, int str) |
| 1830 | { |
| 1831 | struct map_descriptor *desc; |
| 1832 | struct pattern *pat; |
| 1833 | struct sample smp; |
| 1834 | |
| 1835 | MAY_LJMP(check_args(L, 2, "lookup")); |
| 1836 | desc = MAY_LJMP(hlua_checkmap(L, 1)); |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1837 | if (desc->pat.expect_type == SMP_T_SINT) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1838 | smp.data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1839 | smp.data.u.sint = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1840 | } |
| 1841 | else { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1842 | smp.data.type = SMP_T_STR; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1843 | smp.flags = SMP_F_CONST; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1844 | 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] | 1845 | smp.data.u.str.size = smp.data.u.str.data + 1; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1846 | } |
| 1847 | |
| 1848 | pat = pattern_exec_match(&desc->pat, &smp, 1); |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1849 | if (!pat || !pat->data) { |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1850 | if (str) |
| 1851 | lua_pushstring(L, ""); |
| 1852 | else |
| 1853 | lua_pushnil(L); |
| 1854 | return 1; |
| 1855 | } |
| 1856 | |
| 1857 | /* 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] | 1858 | 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] | 1859 | return 1; |
| 1860 | } |
| 1861 | |
| 1862 | __LJMP static int hlua_map_lookup(struct lua_State *L) |
| 1863 | { |
| 1864 | return _hlua_map_lookup(L, 0); |
| 1865 | } |
| 1866 | |
| 1867 | __LJMP static int hlua_map_slookup(struct lua_State *L) |
| 1868 | { |
| 1869 | return _hlua_map_lookup(L, 1); |
| 1870 | } |
| 1871 | |
| 1872 | /* |
| 1873 | * |
| 1874 | * |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1875 | * Class Socket |
| 1876 | * |
| 1877 | * |
| 1878 | */ |
| 1879 | |
| 1880 | __LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud) |
| 1881 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1882 | return MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1883 | } |
| 1884 | |
| 1885 | /* 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] | 1886 | * connection. It is used for notify space available to send or data |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1887 | * received. |
| 1888 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1889 | static void hlua_socket_handler(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1890 | { |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1891 | struct stream_interface *si = appctx->owner; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1892 | |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1893 | if (appctx->ctx.hlua_cosocket.die) { |
| 1894 | si_shutw(si); |
| 1895 | si_shutr(si); |
| 1896 | si_ic(si)->flags |= CF_READ_NULL; |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1897 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1898 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1899 | stream_shutdown(si_strm(si), SF_ERR_KILLED); |
| 1900 | } |
| 1901 | |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 1902 | /* If we can't write, wakeup the pending write signals. */ |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1903 | if (channel_output_closed(si_ic(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1904 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1905 | |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 1906 | /* If we can't read, wakeup the pending read signals. */ |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1907 | if (channel_input_closed(si_oc(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1908 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1909 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 1910 | /* if the connection is not established, inform the stream that we want |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 1911 | * to be notified whenever the connection completes. |
| 1912 | */ |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 1913 | if (si_opposite(si)->state < SI_ST_EST) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 1914 | si_cant_get(si); |
Willy Tarreau | 12c2423 | 2018-12-06 15:29:50 +0100 | [diff] [blame] | 1915 | si_rx_conn_blk(si); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1916 | si_rx_endp_more(si); |
Willy Tarreau | d4da196 | 2015-04-20 01:31:23 +0200 | [diff] [blame] | 1917 | return; |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 1918 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1919 | |
| 1920 | /* This function is called after the connect. */ |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 1921 | appctx->ctx.hlua_cosocket.connected = 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1922 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1923 | /* 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] | 1924 | if (channel_may_recv(si_ic(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1925 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1926 | |
| 1927 | /* Wake the tasks which wants to read if the buffer contains data. */ |
Thierry FOURNIER | eba6f64 | 2015-09-26 22:01:07 +0200 | [diff] [blame] | 1928 | if (!channel_is_empty(si_oc(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1929 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1930 | |
| 1931 | /* Some data were injected in the buffer, notify the stream |
| 1932 | * interface. |
| 1933 | */ |
| 1934 | if (!channel_is_empty(si_ic(si))) |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 1935 | si_update(si); |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1936 | |
| 1937 | /* If write notifications are registered, we considers we want |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1938 | * to write, so we clear the blocking flag. |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1939 | */ |
| 1940 | if (notification_registered(&appctx->ctx.hlua_cosocket.wake_on_write)) |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1941 | si_rx_endp_more(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1942 | } |
| 1943 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1944 | /* This function is called when the "struct stream" is destroyed. |
| 1945 | * Remove the link from the object to this stream. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1946 | * Wake all the pending signals. |
| 1947 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1948 | static void hlua_socket_release(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1949 | { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1950 | struct xref *peer; |
| 1951 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1952 | /* Remove my link in the original object. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1953 | peer = xref_get_peer_and_lock(&appctx->ctx.hlua_cosocket.xref); |
| 1954 | if (peer) |
| 1955 | xref_disconnect(&appctx->ctx.hlua_cosocket.xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1956 | |
| 1957 | /* Wake all the task waiting for me. */ |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1958 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1959 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1960 | } |
| 1961 | |
| 1962 | /* If the garbage collectio of the object is launch, nobody |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1963 | * uses this object. If the stream does not exists, just quit. |
| 1964 | * Send the shutdown signal to the stream. In some cases, |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1965 | * pending signal can rest in the read and write lists. destroy |
| 1966 | * it. |
| 1967 | */ |
| 1968 | __LJMP static int hlua_socket_gc(lua_State *L) |
| 1969 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1970 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1971 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1972 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1973 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1974 | MAY_LJMP(check_args(L, 1, "__gc")); |
| 1975 | |
| 1976 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1977 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1978 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1979 | return 0; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1980 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1981 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1982 | /* Set the flag which destroy the session. */ |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1983 | appctx->ctx.hlua_cosocket.die = 1; |
| 1984 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1985 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1986 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1987 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1988 | return 0; |
| 1989 | } |
| 1990 | |
| 1991 | /* The close function send shutdown signal and break the |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1992 | * links between the stream and the object. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1993 | */ |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 1994 | __LJMP static int hlua_socket_close_helper(lua_State *L) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1995 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1996 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1997 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1998 | struct xref *peer; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 1999 | struct hlua *hlua; |
| 2000 | |
| 2001 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 2002 | hlua = hlua_gethlua(L); |
| 2003 | if (!hlua) |
| 2004 | return 0; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2005 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2006 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2007 | |
| 2008 | /* Check if we run on the same thread than the xreator thread. |
| 2009 | * We cannot access to the socket if the thread is different. |
| 2010 | */ |
| 2011 | if (socket->tid != tid) |
| 2012 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2013 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2014 | peer = xref_get_peer_and_lock(&socket->xref); |
| 2015 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2016 | return 0; |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 2017 | |
| 2018 | hlua->gc_count--; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2019 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2020 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2021 | /* Set the flag which destroy the session. */ |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 2022 | appctx->ctx.hlua_cosocket.die = 1; |
| 2023 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2024 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2025 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2026 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2027 | return 0; |
| 2028 | } |
| 2029 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 2030 | /* The close function calls close_helper. |
| 2031 | */ |
| 2032 | __LJMP static int hlua_socket_close(lua_State *L) |
| 2033 | { |
| 2034 | MAY_LJMP(check_args(L, 1, "close")); |
| 2035 | return hlua_socket_close_helper(L); |
| 2036 | } |
| 2037 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2038 | /* This Lua function assumes that the stack contain three parameters. |
| 2039 | * 1 - USERDATA containing a struct socket |
| 2040 | * 2 - INTEGER with values of the macro defined below |
| 2041 | * If the integer is -1, we must read at most one line. |
| 2042 | * If the integer is -2, we ust read all the data until the |
| 2043 | * end of the stream. |
| 2044 | * If the integer is positive value, we must read a number of |
| 2045 | * bytes corresponding to this value. |
| 2046 | */ |
| 2047 | #define HLSR_READ_LINE (-1) |
| 2048 | #define HLSR_READ_ALL (-2) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2049 | __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] | 2050 | { |
| 2051 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2052 | int wanted = lua_tointeger(L, 2); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2053 | struct hlua *hlua; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2054 | struct appctx *appctx; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2055 | size_t len; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2056 | int nblk; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 2057 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2058 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 2059 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2060 | size_t len2; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 2061 | int skip_at_end = 0; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 2062 | struct channel *oc; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2063 | struct stream_interface *si; |
| 2064 | struct stream *s; |
| 2065 | struct xref *peer; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 2066 | int missing_bytes; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2067 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2068 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 2069 | hlua = hlua_gethlua(L); |
| 2070 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2071 | /* Check if this lua stack is schedulable. */ |
| 2072 | if (!hlua || !hlua->task) |
| 2073 | WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in " |
| 2074 | "'frontend', 'backend' or 'task'")); |
| 2075 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2076 | /* Check if we run on the same thread than the xreator thread. |
| 2077 | * We cannot access to the socket if the thread is different. |
| 2078 | */ |
| 2079 | if (socket->tid != tid) |
| 2080 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2081 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2082 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2083 | peer = xref_get_peer_and_lock(&socket->xref); |
| 2084 | if (!peer) |
| 2085 | goto no_peer; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2086 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2087 | si = appctx->owner; |
| 2088 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2089 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2090 | oc = &s->res; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2091 | if (wanted == HLSR_READ_LINE) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2092 | /* Read line. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2093 | nblk = co_getline_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2094 | if (nblk < 0) /* Connection close. */ |
| 2095 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2096 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2097 | goto connection_empty; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 2098 | |
| 2099 | /* remove final \r\n. */ |
| 2100 | if (nblk == 1) { |
| 2101 | if (blk1[len1-1] == '\n') { |
| 2102 | len1--; |
| 2103 | skip_at_end++; |
| 2104 | if (blk1[len1-1] == '\r') { |
| 2105 | len1--; |
| 2106 | skip_at_end++; |
| 2107 | } |
| 2108 | } |
| 2109 | } |
| 2110 | else { |
| 2111 | if (blk2[len2-1] == '\n') { |
| 2112 | len2--; |
| 2113 | skip_at_end++; |
| 2114 | if (blk2[len2-1] == '\r') { |
| 2115 | len2--; |
| 2116 | skip_at_end++; |
| 2117 | } |
| 2118 | } |
| 2119 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2120 | } |
| 2121 | |
| 2122 | else if (wanted == HLSR_READ_ALL) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2123 | /* Read all the available data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2124 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2125 | if (nblk < 0) /* Connection close. */ |
| 2126 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2127 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2128 | goto connection_empty; |
| 2129 | } |
| 2130 | |
| 2131 | else { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2132 | /* Read a block of data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2133 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2134 | if (nblk < 0) /* Connection close. */ |
| 2135 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2136 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2137 | goto connection_empty; |
| 2138 | |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 2139 | missing_bytes = wanted - socket->b.n; |
| 2140 | if (len1 > missing_bytes) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2141 | nblk = 1; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 2142 | len1 = missing_bytes; |
| 2143 | } if (nblk == 2 && len1 + len2 > missing_bytes) |
| 2144 | len2 = missing_bytes - len1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2145 | } |
| 2146 | |
| 2147 | len = len1; |
| 2148 | |
| 2149 | luaL_addlstring(&socket->b, blk1, len1); |
| 2150 | if (nblk == 2) { |
| 2151 | len += len2; |
| 2152 | luaL_addlstring(&socket->b, blk2, len2); |
| 2153 | } |
| 2154 | |
| 2155 | /* Consume data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2156 | co_skip(oc, len + skip_at_end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2157 | |
| 2158 | /* Don't wait anything. */ |
Thierry FOURNIER | 7e4ee47 | 2018-05-25 15:03:50 +0200 | [diff] [blame] | 2159 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2160 | |
| 2161 | /* If the pattern reclaim to read all the data |
| 2162 | * in the connection, got out. |
| 2163 | */ |
| 2164 | if (wanted == HLSR_READ_ALL) |
| 2165 | goto connection_empty; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 2166 | else if (wanted >= 0 && socket->b.n < wanted) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2167 | goto connection_empty; |
| 2168 | |
| 2169 | /* Return result. */ |
| 2170 | luaL_pushresult(&socket->b); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2171 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2172 | return 1; |
| 2173 | |
| 2174 | connection_closed: |
| 2175 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2176 | xref_unlock(&socket->xref, peer); |
| 2177 | |
| 2178 | no_peer: |
| 2179 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2180 | /* If the buffer containds data. */ |
| 2181 | if (socket->b.n > 0) { |
| 2182 | luaL_pushresult(&socket->b); |
| 2183 | return 1; |
| 2184 | } |
| 2185 | lua_pushnil(L); |
| 2186 | lua_pushstring(L, "connection closed."); |
| 2187 | return 2; |
| 2188 | |
| 2189 | connection_empty: |
| 2190 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2191 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_read, hlua->task)) { |
| 2192 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2193 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2194 | } |
| 2195 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2196 | 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] | 2197 | return 0; |
| 2198 | } |
| 2199 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2200 | /* This Lua function gets two parameters. The first one can be string |
| 2201 | * or a number. If the string is "*l", the user requires one line. If |
| 2202 | * 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] | 2203 | * If the value is a number, the user require a number of bytes equal |
| 2204 | * to the value. The default value is "*l" (a line). |
| 2205 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2206 | * This parameter with a variable type is converted in integer. This |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2207 | * integer takes this values: |
| 2208 | * -1 : read a line |
| 2209 | * -2 : read all the stream |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2210 | * >0 : amount of bytes. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2211 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2212 | * The second parameter is optional. It contains a string that must be |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2213 | * concatenated with the read data. |
| 2214 | */ |
| 2215 | __LJMP static int hlua_socket_receive(struct lua_State *L) |
| 2216 | { |
| 2217 | int wanted = HLSR_READ_LINE; |
| 2218 | const char *pattern; |
Christopher Faulet | c31b200 | 2021-05-03 10:11:13 +0200 | [diff] [blame] | 2219 | int lastarg, type; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2220 | char *error; |
| 2221 | size_t len; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2222 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2223 | |
| 2224 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 2225 | WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments.")); |
| 2226 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2227 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2228 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2229 | /* Check if we run on the same thread than the xreator thread. |
| 2230 | * We cannot access to the socket if the thread is different. |
| 2231 | */ |
| 2232 | if (socket->tid != tid) |
| 2233 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2234 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2235 | /* check for pattern. */ |
| 2236 | if (lua_gettop(L) >= 2) { |
| 2237 | type = lua_type(L, 2); |
| 2238 | if (type == LUA_TSTRING) { |
| 2239 | pattern = lua_tostring(L, 2); |
| 2240 | if (strcmp(pattern, "*a") == 0) |
| 2241 | wanted = HLSR_READ_ALL; |
| 2242 | else if (strcmp(pattern, "*l") == 0) |
| 2243 | wanted = HLSR_READ_LINE; |
| 2244 | else { |
| 2245 | wanted = strtoll(pattern, &error, 10); |
| 2246 | if (*error != '\0') |
| 2247 | WILL_LJMP(luaL_error(L, "Unsupported pattern.")); |
| 2248 | } |
| 2249 | } |
| 2250 | else if (type == LUA_TNUMBER) { |
| 2251 | wanted = lua_tointeger(L, 2); |
| 2252 | if (wanted < 0) |
| 2253 | WILL_LJMP(luaL_error(L, "Unsupported size.")); |
| 2254 | } |
| 2255 | } |
| 2256 | |
| 2257 | /* Set pattern. */ |
| 2258 | lua_pushinteger(L, wanted); |
Tim Duesterhus | c6e377e | 2018-01-04 19:32:13 +0100 | [diff] [blame] | 2259 | |
| 2260 | /* Check if we would replace the top by itself. */ |
| 2261 | if (lua_gettop(L) != 2) |
| 2262 | lua_replace(L, 2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2263 | |
Christopher Faulet | c31b200 | 2021-05-03 10:11:13 +0200 | [diff] [blame] | 2264 | /* Save index of the top of the stack because since buffers are used, it |
| 2265 | * may change |
| 2266 | */ |
| 2267 | lastarg = lua_gettop(L); |
| 2268 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2269 | /* init buffer, and fill it with prefix. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2270 | luaL_buffinit(L, &socket->b); |
| 2271 | |
| 2272 | /* Check prefix. */ |
Christopher Faulet | c31b200 | 2021-05-03 10:11:13 +0200 | [diff] [blame] | 2273 | if (lastarg >= 3) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2274 | if (lua_type(L, 3) != LUA_TSTRING) |
| 2275 | WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix")); |
| 2276 | pattern = lua_tolstring(L, 3, &len); |
| 2277 | luaL_addlstring(&socket->b, pattern, len); |
| 2278 | } |
| 2279 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2280 | return __LJMP(hlua_socket_receive_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2281 | } |
| 2282 | |
| 2283 | /* Write the Lua input string in the output buffer. |
Mark Lakes | 22154b4 | 2018-01-29 14:38:40 -0800 | [diff] [blame] | 2284 | * This function returns a yield if no space is available. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2285 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2286 | 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] | 2287 | { |
| 2288 | struct hlua_socket *socket; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2289 | struct hlua *hlua; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2290 | struct appctx *appctx; |
| 2291 | size_t buf_len; |
| 2292 | const char *buf; |
| 2293 | int len; |
| 2294 | int send_len; |
| 2295 | int sent; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2296 | struct xref *peer; |
| 2297 | struct stream_interface *si; |
| 2298 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2299 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2300 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 2301 | hlua = hlua_gethlua(L); |
| 2302 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2303 | /* Check if this lua stack is schedulable. */ |
| 2304 | if (!hlua || !hlua->task) |
| 2305 | WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in " |
| 2306 | "'frontend', 'backend' or 'task'")); |
| 2307 | |
| 2308 | /* Get object */ |
| 2309 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2310 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2311 | sent = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2312 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2313 | /* Check if we run on the same thread than the xreator thread. |
| 2314 | * We cannot access to the socket if the thread is different. |
| 2315 | */ |
| 2316 | if (socket->tid != tid) |
| 2317 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2318 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2319 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2320 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2321 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2322 | lua_pushinteger(L, -1); |
| 2323 | return 1; |
| 2324 | } |
| 2325 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2326 | si = appctx->owner; |
| 2327 | s = si_strm(si); |
| 2328 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2329 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2330 | if (channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2331 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2332 | lua_pushinteger(L, -1); |
| 2333 | return 1; |
| 2334 | } |
| 2335 | |
| 2336 | /* Update the input buffer data. */ |
| 2337 | buf += sent; |
| 2338 | send_len = buf_len - sent; |
| 2339 | |
| 2340 | /* All the data are sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2341 | if (sent >= buf_len) { |
| 2342 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2343 | return 1; /* Implicitly return the length sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2344 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2345 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2346 | /* Check if the buffer is available because HAProxy doesn't allocate |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 2347 | * the request buffer if its not required. |
| 2348 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2349 | if (s->req.buf.size == 0) { |
Willy Tarreau | 581abd3 | 2018-10-25 10:21:41 +0200 | [diff] [blame] | 2350 | if (!si_alloc_ibuf(si, &appctx->buffer_wait)) |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2351 | goto hlua_socket_write_yield_return; |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 2352 | } |
| 2353 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2354 | /* Check for available space. */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2355 | len = b_room(&s->req.buf); |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2356 | if (len <= 0) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2357 | goto hlua_socket_write_yield_return; |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2358 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2359 | |
| 2360 | /* send data */ |
| 2361 | if (len < send_len) |
| 2362 | send_len = len; |
Thierry FOURNIER | 66b8919 | 2018-05-27 01:14:47 +0200 | [diff] [blame] | 2363 | len = ci_putblk(&s->req, buf, send_len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2364 | |
| 2365 | /* "Not enough space" (-1), "Buffer too little to contain |
| 2366 | * the data" (-2) are not expected because the available length |
| 2367 | * is tested. |
| 2368 | * Other unknown error are also not expected. |
| 2369 | */ |
| 2370 | if (len <= 0) { |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2371 | if (len == -1) |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2372 | s->req.flags |= CF_WAKE_WRITE; |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2373 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 2374 | MAY_LJMP(hlua_socket_close_helper(L)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2375 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2376 | lua_pushinteger(L, -1); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2377 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2378 | return 1; |
| 2379 | } |
| 2380 | |
| 2381 | /* update buffers. */ |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 2382 | appctx_wakeup(appctx); |
Willy Tarreau | de70fa1 | 2015-09-26 11:25:05 +0200 | [diff] [blame] | 2383 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2384 | s->req.rex = TICK_ETERNITY; |
| 2385 | s->res.wex = TICK_ETERNITY; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2386 | |
| 2387 | /* Update length sent. */ |
| 2388 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2389 | lua_pushinteger(L, sent + len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2390 | |
| 2391 | /* All the data buffer is sent ? */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2392 | if (sent + len >= buf_len) { |
| 2393 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2394 | return 1; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2395 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2396 | |
| 2397 | hlua_socket_write_yield_return: |
Thierry FOURNIER | ba42fcd | 2018-05-27 00:59:48 +0200 | [diff] [blame] | 2398 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2399 | xref_unlock(&socket->xref, peer); |
| 2400 | WILL_LJMP(luaL_error(L, "out of memory")); |
| 2401 | } |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2402 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2403 | 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] | 2404 | return 0; |
| 2405 | } |
| 2406 | |
| 2407 | /* This function initiate the send of data. It just check the input |
| 2408 | * parameters and push an integer in the Lua stack that contain the |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2409 | * 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] | 2410 | * "hlua_socket_write_yield" that can yield. |
| 2411 | * |
| 2412 | * The Lua function gets between 3 and 4 parameters. The first one is |
| 2413 | * the associated object. The second is a string buffer. The third is |
| 2414 | * a facultative integer that represents where is the buffer position |
| 2415 | * of the start of the data that can send. The first byte is the |
| 2416 | * position "1". The default value is "1". The fourth argument is a |
| 2417 | * facultative integer that represents where is the buffer position |
| 2418 | * of the end of the data that can send. The default is the last byte. |
| 2419 | */ |
| 2420 | static int hlua_socket_send(struct lua_State *L) |
| 2421 | { |
| 2422 | int i; |
| 2423 | int j; |
| 2424 | const char *buf; |
| 2425 | size_t buf_len; |
| 2426 | |
| 2427 | /* Check number of arguments. */ |
| 2428 | if (lua_gettop(L) < 2 || lua_gettop(L) > 4) |
| 2429 | WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments")); |
| 2430 | |
| 2431 | /* Get the string. */ |
| 2432 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
| 2433 | |
| 2434 | /* Get and check j. */ |
| 2435 | if (lua_gettop(L) == 4) { |
| 2436 | j = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 2437 | if (j < 0) |
| 2438 | j = buf_len + j + 1; |
| 2439 | if (j > buf_len) |
| 2440 | j = buf_len + 1; |
| 2441 | lua_pop(L, 1); |
| 2442 | } |
| 2443 | else |
| 2444 | j = buf_len; |
| 2445 | |
| 2446 | /* Get and check i. */ |
| 2447 | if (lua_gettop(L) == 3) { |
| 2448 | i = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2449 | if (i < 0) |
| 2450 | i = buf_len + i + 1; |
| 2451 | if (i > buf_len) |
| 2452 | i = buf_len + 1; |
| 2453 | lua_pop(L, 1); |
| 2454 | } else |
| 2455 | i = 1; |
| 2456 | |
| 2457 | /* Check bth i and j. */ |
| 2458 | if (i > j) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2459 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2460 | return 1; |
| 2461 | } |
| 2462 | if (i == 0 && j == 0) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2463 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2464 | return 1; |
| 2465 | } |
| 2466 | if (i == 0) |
| 2467 | i = 1; |
| 2468 | if (j == 0) |
| 2469 | j = 1; |
| 2470 | |
| 2471 | /* Pop the string. */ |
| 2472 | lua_pop(L, 1); |
| 2473 | |
| 2474 | /* Update the buffer length. */ |
| 2475 | buf += i - 1; |
| 2476 | buf_len = j - i + 1; |
| 2477 | lua_pushlstring(L, buf, buf_len); |
| 2478 | |
| 2479 | /* This unsigned is used to remember the amount of sent data. */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2480 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2481 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2482 | return MAY_LJMP(hlua_socket_write_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2483 | } |
| 2484 | |
Willy Tarreau | 22b0a68 | 2015-06-17 19:43:49 +0200 | [diff] [blame] | 2485 | #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] | 2486 | __LJMP static inline int hlua_socket_info(struct lua_State *L, struct sockaddr_storage *addr) |
| 2487 | { |
| 2488 | static char buffer[SOCKET_INFO_MAX_LEN]; |
| 2489 | int ret; |
| 2490 | int len; |
| 2491 | char *p; |
| 2492 | |
| 2493 | ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1); |
| 2494 | if (ret <= 0) { |
| 2495 | lua_pushnil(L); |
| 2496 | return 1; |
| 2497 | } |
| 2498 | |
| 2499 | if (ret == AF_UNIX) { |
| 2500 | lua_pushstring(L, buffer+1); |
| 2501 | return 1; |
| 2502 | } |
| 2503 | else if (ret == AF_INET6) { |
| 2504 | buffer[0] = '['; |
| 2505 | len = strlen(buffer); |
| 2506 | buffer[len] = ']'; |
| 2507 | len++; |
| 2508 | buffer[len] = ':'; |
| 2509 | len++; |
| 2510 | p = buffer; |
| 2511 | } |
| 2512 | else if (ret == AF_INET) { |
| 2513 | p = buffer + 1; |
| 2514 | len = strlen(p); |
| 2515 | p[len] = ':'; |
| 2516 | len++; |
| 2517 | } |
| 2518 | else { |
| 2519 | lua_pushnil(L); |
| 2520 | return 1; |
| 2521 | } |
| 2522 | |
| 2523 | if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) { |
| 2524 | lua_pushnil(L); |
| 2525 | return 1; |
| 2526 | } |
| 2527 | |
| 2528 | lua_pushstring(L, p); |
| 2529 | return 1; |
| 2530 | } |
| 2531 | |
| 2532 | /* Returns information about the peer of the connection. */ |
| 2533 | __LJMP static int hlua_socket_getpeername(struct lua_State *L) |
| 2534 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2535 | struct hlua_socket *socket; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2536 | struct xref *peer; |
| 2537 | struct appctx *appctx; |
| 2538 | struct stream_interface *si; |
| 2539 | struct stream *s; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2540 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2541 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2542 | MAY_LJMP(check_args(L, 1, "getpeername")); |
| 2543 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2544 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2545 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2546 | /* Check if we run on the same thread than the xreator thread. |
| 2547 | * We cannot access to the socket if the thread is different. |
| 2548 | */ |
| 2549 | if (socket->tid != tid) |
| 2550 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2551 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2552 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2553 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2554 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2555 | lua_pushnil(L); |
| 2556 | return 1; |
| 2557 | } |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2558 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2559 | si = appctx->owner; |
| 2560 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2561 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2562 | if (!s->target_addr) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2563 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2564 | lua_pushnil(L); |
| 2565 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2566 | } |
| 2567 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2568 | ret = MAY_LJMP(hlua_socket_info(L, s->target_addr)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2569 | xref_unlock(&socket->xref, peer); |
| 2570 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2571 | } |
| 2572 | |
| 2573 | /* Returns information about my connection side. */ |
| 2574 | static int hlua_socket_getsockname(struct lua_State *L) |
| 2575 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2576 | struct hlua_socket *socket; |
| 2577 | struct connection *conn; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2578 | struct appctx *appctx; |
| 2579 | struct xref *peer; |
| 2580 | struct stream_interface *si; |
| 2581 | struct stream *s; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2582 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2583 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2584 | MAY_LJMP(check_args(L, 1, "getsockname")); |
| 2585 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2586 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2587 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2588 | /* Check if we run on the same thread than the xreator thread. |
| 2589 | * We cannot access to the socket if the thread is different. |
| 2590 | */ |
| 2591 | if (socket->tid != tid) |
| 2592 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2593 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2594 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2595 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2596 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2597 | lua_pushnil(L); |
| 2598 | return 1; |
| 2599 | } |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2600 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2601 | si = appctx->owner; |
| 2602 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2603 | |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 2604 | conn = cs_conn(objt_cs(s->si[1].end)); |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2605 | if (!conn || !conn_get_src(conn)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2606 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2607 | lua_pushnil(L); |
| 2608 | return 1; |
| 2609 | } |
| 2610 | |
Willy Tarreau | 9da9a6f | 2019-07-17 14:49:44 +0200 | [diff] [blame] | 2611 | ret = hlua_socket_info(L, conn->src); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2612 | xref_unlock(&socket->xref, peer); |
| 2613 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2614 | } |
| 2615 | |
| 2616 | /* This struct define the applet. */ |
Willy Tarreau | 3057645 | 2015-04-13 13:50:30 +0200 | [diff] [blame] | 2617 | static struct applet update_applet = { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2618 | .obj_type = OBJ_TYPE_APPLET, |
| 2619 | .name = "<LUA_TCP>", |
| 2620 | .fct = hlua_socket_handler, |
| 2621 | .release = hlua_socket_release, |
| 2622 | }; |
| 2623 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2624 | __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] | 2625 | { |
| 2626 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2627 | struct hlua *hlua; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2628 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2629 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2630 | struct stream_interface *si; |
| 2631 | struct stream *s; |
| 2632 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2633 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 2634 | hlua = hlua_gethlua(L); |
| 2635 | if (!hlua) |
| 2636 | return 0; |
| 2637 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2638 | /* Check if we run on the same thread than the xreator thread. |
| 2639 | * We cannot access to the socket if the thread is different. |
| 2640 | */ |
| 2641 | if (socket->tid != tid) |
| 2642 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2643 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2644 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2645 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2646 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2647 | lua_pushnil(L); |
| 2648 | lua_pushstring(L, "Can't connect"); |
| 2649 | return 2; |
| 2650 | } |
| 2651 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2652 | si = appctx->owner; |
| 2653 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2654 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2655 | /* Check if we run on the same thread than the xreator thread. |
| 2656 | * We cannot access to the socket if the thread is different. |
| 2657 | */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2658 | if (socket->tid != tid) { |
| 2659 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2660 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2661 | } |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2662 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2663 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2664 | if (!hlua || channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2665 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2666 | lua_pushnil(L); |
| 2667 | lua_pushstring(L, "Can't connect"); |
| 2668 | return 2; |
| 2669 | } |
| 2670 | |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 2671 | appctx = __objt_appctx(s->si[0].end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2672 | |
| 2673 | /* Check for connection established. */ |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2674 | if (appctx->ctx.hlua_cosocket.connected) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2675 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2676 | lua_pushinteger(L, 1); |
| 2677 | return 1; |
| 2678 | } |
| 2679 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2680 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2681 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2682 | WILL_LJMP(luaL_error(L, "out of memory error")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2683 | } |
| 2684 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2685 | 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] | 2686 | return 0; |
| 2687 | } |
| 2688 | |
| 2689 | /* This function fail or initite the connection. */ |
| 2690 | __LJMP static int hlua_socket_connect(struct lua_State *L) |
| 2691 | { |
| 2692 | struct hlua_socket *socket; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2693 | int port = -1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2694 | const char *ip; |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2695 | struct hlua *hlua; |
| 2696 | struct appctx *appctx; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2697 | int low, high; |
| 2698 | struct sockaddr_storage *addr; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2699 | struct xref *peer; |
| 2700 | struct stream_interface *si; |
| 2701 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2702 | |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2703 | if (lua_gettop(L) < 2) |
| 2704 | WILL_LJMP(luaL_error(L, "connect: need at least 2 arguments")); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2705 | |
| 2706 | /* Get args. */ |
| 2707 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2708 | |
| 2709 | /* Check if we run on the same thread than the xreator thread. |
| 2710 | * We cannot access to the socket if the thread is different. |
| 2711 | */ |
| 2712 | if (socket->tid != tid) |
| 2713 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2714 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2715 | ip = MAY_LJMP(luaL_checkstring(L, 2)); |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 2716 | if (lua_gettop(L) >= 3) { |
| 2717 | luaL_Buffer b; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2718 | port = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2719 | |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 2720 | /* Force the ip to end with a colon, to support IPv6 addresses |
| 2721 | * that are not enclosed within square brackets. |
| 2722 | */ |
| 2723 | if (port > 0) { |
| 2724 | luaL_buffinit(L, &b); |
| 2725 | luaL_addstring(&b, ip); |
| 2726 | luaL_addchar(&b, ':'); |
| 2727 | luaL_pushresult(&b); |
| 2728 | ip = lua_tolstring(L, lua_gettop(L), NULL); |
| 2729 | } |
| 2730 | } |
| 2731 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2732 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2733 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2734 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2735 | lua_pushnil(L); |
| 2736 | return 1; |
| 2737 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2738 | |
| 2739 | /* Parse ip address. */ |
Willy Tarreau | 5fc9328 | 2020-09-16 18:25:03 +0200 | [diff] [blame] | 2740 | 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] | 2741 | if (!addr) { |
| 2742 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2743 | WILL_LJMP(luaL_error(L, "connect: cannot parse destination address '%s'", ip)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2744 | } |
Willy Tarreau | 9da9a6f | 2019-07-17 14:49:44 +0200 | [diff] [blame] | 2745 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2746 | /* Set port. */ |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2747 | if (low == 0) { |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2748 | if (addr->ss_family == AF_INET) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2749 | if (port == -1) { |
| 2750 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2751 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2752 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2753 | ((struct sockaddr_in *)addr)->sin_port = htons(port); |
| 2754 | } else if (addr->ss_family == AF_INET6) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2755 | if (port == -1) { |
| 2756 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2757 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2758 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2759 | ((struct sockaddr_in6 *)addr)->sin6_port = htons(port); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2760 | } |
| 2761 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2762 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2763 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2764 | si = appctx->owner; |
| 2765 | s = si_strm(si); |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2766 | |
Willy Tarreau | 9b7587a | 2020-10-15 07:32:10 +0200 | [diff] [blame] | 2767 | if (!sockaddr_alloc(&s->target_addr, addr, sizeof(*addr))) { |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2768 | xref_unlock(&socket->xref, peer); |
| 2769 | WILL_LJMP(luaL_error(L, "connect: internal error")); |
| 2770 | } |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2771 | s->flags |= SF_ADDR_SET; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2772 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2773 | /* Get hlua struct, or NULL if we execute from main lua state */ |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2774 | hlua = hlua_gethlua(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2775 | if (!hlua) |
| 2776 | return 0; |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2777 | |
| 2778 | /* inform the stream that we want to be notified whenever the |
| 2779 | * connection completes. |
| 2780 | */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 2781 | si_cant_get(&s->si[0]); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 2782 | si_rx_endp_more(&s->si[0]); |
Thierry FOURNIER | 8c8fbbe | 2015-09-26 17:02:35 +0200 | [diff] [blame] | 2783 | appctx_wakeup(appctx); |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2784 | |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 2785 | hlua->gc_count++; |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 2786 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2787 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2788 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2789 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2790 | } |
| 2791 | xref_unlock(&socket->xref, peer); |
PiBa-NL | 706d5ee | 2018-05-05 23:51:42 +0200 | [diff] [blame] | 2792 | |
| 2793 | task_wakeup(s->task, TASK_WOKEN_INIT); |
| 2794 | /* Return yield waiting for connection. */ |
| 2795 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2796 | 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] | 2797 | |
| 2798 | return 0; |
| 2799 | } |
| 2800 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2801 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2802 | __LJMP static int hlua_socket_connect_ssl(struct lua_State *L) |
| 2803 | { |
| 2804 | struct hlua_socket *socket; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2805 | struct xref *peer; |
| 2806 | struct appctx *appctx; |
| 2807 | struct stream_interface *si; |
| 2808 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2809 | |
| 2810 | MAY_LJMP(check_args(L, 3, "connect_ssl")); |
| 2811 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2812 | |
| 2813 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2814 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2815 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2816 | lua_pushnil(L); |
| 2817 | return 1; |
| 2818 | } |
| 2819 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2820 | si = appctx->owner; |
| 2821 | s = si_strm(si); |
| 2822 | |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 2823 | s->target = &socket_ssl->obj_type; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2824 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2825 | return MAY_LJMP(hlua_socket_connect(L)); |
| 2826 | } |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2827 | #endif |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2828 | |
| 2829 | __LJMP static int hlua_socket_setoption(struct lua_State *L) |
| 2830 | { |
| 2831 | return 0; |
| 2832 | } |
| 2833 | |
| 2834 | __LJMP static int hlua_socket_settimeout(struct lua_State *L) |
| 2835 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2836 | struct hlua_socket *socket; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2837 | int tmout; |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2838 | double dtmout; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2839 | struct xref *peer; |
| 2840 | struct appctx *appctx; |
| 2841 | struct stream_interface *si; |
| 2842 | struct stream *s; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2843 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2844 | MAY_LJMP(check_args(L, 2, "settimeout")); |
| 2845 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2846 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2847 | |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2848 | /* convert the timeout to millis */ |
| 2849 | dtmout = MAY_LJMP(luaL_checknumber(L, 2)) * 1000; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2850 | |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 2851 | /* Check for negative values */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2852 | if (dtmout < 0) |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 2853 | WILL_LJMP(luaL_error(L, "settimeout: cannot set negatives values")); |
| 2854 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2855 | if (dtmout > INT_MAX) /* overflow check */ |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2856 | 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] | 2857 | |
| 2858 | tmout = MS_TO_TICKS((int)dtmout); |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2859 | if (tmout == 0) |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 2860 | tmout++; /* very small timeouts are adjusted to a minimum of 1ms */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2861 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2862 | /* Check if we run on the same thread than the xreator thread. |
| 2863 | * We cannot access to the socket if the thread is different. |
| 2864 | */ |
| 2865 | if (socket->tid != tid) |
| 2866 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2867 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2868 | /* check for connection break. If some data were read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2869 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2870 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2871 | hlua_pusherror(L, "socket: not yet initialised, you can't set timeouts."); |
| 2872 | WILL_LJMP(lua_error(L)); |
| 2873 | return 0; |
| 2874 | } |
| 2875 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2876 | si = appctx->owner; |
| 2877 | s = si_strm(si); |
| 2878 | |
Cyril Bonté | 7bb6345 | 2018-08-17 23:51:02 +0200 | [diff] [blame] | 2879 | s->sess->fe->timeout.connect = tmout; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2880 | s->req.rto = tmout; |
| 2881 | s->req.wto = tmout; |
| 2882 | s->res.rto = tmout; |
| 2883 | s->res.wto = tmout; |
Cyril Bonté | 7bb6345 | 2018-08-17 23:51:02 +0200 | [diff] [blame] | 2884 | s->req.rex = tick_add_ifset(now_ms, tmout); |
| 2885 | s->req.wex = tick_add_ifset(now_ms, tmout); |
| 2886 | s->res.rex = tick_add_ifset(now_ms, tmout); |
| 2887 | s->res.wex = tick_add_ifset(now_ms, tmout); |
| 2888 | |
| 2889 | s->task->expire = tick_add_ifset(now_ms, tmout); |
| 2890 | task_queue(s->task); |
| 2891 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2892 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2893 | |
Thierry Fournier | e9636f1 | 2018-03-08 09:54:32 +0100 | [diff] [blame] | 2894 | lua_pushinteger(L, 1); |
Tim Duesterhus | 119a5f1 | 2018-01-06 19:16:25 +0100 | [diff] [blame] | 2895 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2896 | } |
| 2897 | |
| 2898 | __LJMP static int hlua_socket_new(lua_State *L) |
| 2899 | { |
| 2900 | struct hlua_socket *socket; |
| 2901 | struct appctx *appctx; |
Willy Tarreau | 15b5e14 | 2015-04-04 14:38:25 +0200 | [diff] [blame] | 2902 | struct session *sess; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2903 | struct stream *strm; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2904 | |
| 2905 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2906 | if (!lua_checkstack(L, 3)) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2907 | hlua_pusherror(L, "socket: full stack"); |
| 2908 | goto out_fail_conf; |
| 2909 | } |
| 2910 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2911 | /* Create the object: obj[0] = userdata. */ |
| 2912 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2913 | socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket))); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2914 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2915 | memset(socket, 0, sizeof(*socket)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2916 | socket->tid = tid; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2917 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 2918 | /* Check if the various memory pools are initialized. */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 2919 | if (!pool_head_stream || !pool_head_buffer) { |
Thierry FOURNIER | 4a6170c | 2015-03-09 17:07:10 +0100 | [diff] [blame] | 2920 | hlua_pusherror(L, "socket: uninitialized pools."); |
| 2921 | goto out_fail_conf; |
| 2922 | } |
| 2923 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2924 | /* Pop a class stream metatable and affect it to the userdata. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2925 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref); |
| 2926 | lua_setmetatable(L, -2); |
| 2927 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2928 | /* Create the applet context */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 2929 | appctx = appctx_new(&update_applet, tid_bit); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2930 | if (!appctx) { |
| 2931 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | feb7640 | 2015-04-03 14:10:06 +0200 | [diff] [blame] | 2932 | goto out_fail_conf; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2933 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2934 | |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2935 | appctx->ctx.hlua_cosocket.connected = 0; |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 2936 | appctx->ctx.hlua_cosocket.die = 0; |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2937 | LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_write); |
| 2938 | LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_read); |
Willy Tarreau | b2bf833 | 2015-04-04 15:58:58 +0200 | [diff] [blame] | 2939 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2940 | /* Now create a session, task and stream for this applet */ |
Amaury Denoyelle | 239fdbf | 2021-03-24 10:22:03 +0100 | [diff] [blame] | 2941 | sess = session_new(socket_proxy, NULL, &appctx->obj_type); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2942 | if (!sess) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2943 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2944 | goto out_fail_sess; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2945 | } |
| 2946 | |
Christopher Faulet | 26256f8 | 2020-09-14 11:40:13 +0200 | [diff] [blame] | 2947 | strm = stream_new(sess, &appctx->obj_type, &BUF_NULL); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2948 | if (!strm) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2949 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2950 | goto out_fail_stream; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2951 | } |
| 2952 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2953 | /* Initialise cross reference between stream and Lua socket object. */ |
| 2954 | xref_create(&socket->xref, &appctx->ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2955 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2956 | /* Configure "right" stream interface. this "si" is used to connect |
| 2957 | * and retrieve data from the server. The connection is initialized |
| 2958 | * with the "struct server". |
| 2959 | */ |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2960 | si_set_state(&strm->si[1], SI_ST_ASS); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2961 | |
| 2962 | /* Force destination server. */ |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2963 | strm->flags |= SF_DIRECT | SF_ASSIGNED | SF_BE_ASSIGNED; |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 2964 | strm->target = &socket_tcp->obj_type; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2965 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2966 | return 1; |
| 2967 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2968 | out_fail_stream: |
Willy Tarreau | 11c3624 | 2015-04-04 15:54:03 +0200 | [diff] [blame] | 2969 | session_free(sess); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2970 | out_fail_sess: |
| 2971 | appctx_free(appctx); |
| 2972 | out_fail_conf: |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2973 | WILL_LJMP(lua_error(L)); |
| 2974 | return 0; |
| 2975 | } |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 2976 | |
| 2977 | /* |
| 2978 | * |
| 2979 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2980 | * Class Channel |
| 2981 | * |
| 2982 | * |
| 2983 | */ |
| 2984 | |
| 2985 | /* Returns the struct hlua_channel join to the class channel in the |
| 2986 | * stack entry "ud" or throws an argument error. |
| 2987 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2988 | __LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2989 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 2990 | return MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2991 | } |
| 2992 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2993 | /* Pushes the channel onto the top of the stack. If the stask does not have a |
| 2994 | * free slots, the function fails and returns 0; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2995 | */ |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 2996 | static int hlua_channel_new(lua_State *L, struct channel *channel) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2997 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2998 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2999 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3000 | return 0; |
| 3001 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 3002 | lua_newtable(L); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3003 | lua_pushlightuserdata(L, channel); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 3004 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3005 | |
| 3006 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 3007 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref); |
| 3008 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3009 | return 1; |
| 3010 | } |
| 3011 | |
| 3012 | /* Duplicate all the data present in the input channel and put it |
| 3013 | * in a string LUA variables. Returns -1 and push a nil value in |
| 3014 | * the stack if the channel is closed and all the data are consumed, |
| 3015 | * returns 0 if no data are available, otherwise it returns the length |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 3016 | * of the built string. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3017 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3018 | static inline int _hlua_channel_dup(struct channel *chn, lua_State *L) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3019 | { |
| 3020 | char *blk1; |
| 3021 | char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3022 | size_t len1; |
| 3023 | size_t len2; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3024 | int ret; |
| 3025 | luaL_Buffer b; |
| 3026 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3027 | ret = ci_getblk_nc(chn, &blk1, &len1, &blk2, &len2); |
Christopher Faulet | 055310f | 2021-08-05 11:58:37 +0200 | [diff] [blame] | 3028 | if (unlikely(ret <= 0)) { |
| 3029 | if (ret < 0 || HLUA_CANT_YIELD(hlua_gethlua(L))) { |
| 3030 | lua_pushnil(L); |
| 3031 | return -1; |
| 3032 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3033 | return 0; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3034 | } |
| 3035 | |
| 3036 | luaL_buffinit(L, &b); |
| 3037 | luaL_addlstring(&b, blk1, len1); |
| 3038 | if (unlikely(ret == 2)) |
| 3039 | luaL_addlstring(&b, blk2, len2); |
| 3040 | luaL_pushresult(&b); |
| 3041 | |
| 3042 | if (unlikely(ret == 2)) |
| 3043 | return len1 + len2; |
| 3044 | return len1; |
| 3045 | } |
| 3046 | |
| 3047 | /* "_hlua_channel_dup" wrapper. If no data are available, it returns |
| 3048 | * a yield. This function keep the data in the buffer. |
| 3049 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3050 | __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] | 3051 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3052 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3053 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3054 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3055 | |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 3056 | if (IS_HTX_STRM(chn_strm(chn))) { |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3057 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3058 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3059 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3060 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3061 | if (_hlua_channel_dup(chn, L) == 0) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3062 | 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] | 3063 | return 1; |
| 3064 | } |
| 3065 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3066 | /* Check arguments for the function "hlua_channel_dup_yield". */ |
| 3067 | __LJMP static int hlua_channel_dup(lua_State *L) |
| 3068 | { |
| 3069 | MAY_LJMP(check_args(L, 1, "dup")); |
| 3070 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3071 | return MAY_LJMP(hlua_channel_dup_yield(L, 0, 0)); |
| 3072 | } |
| 3073 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3074 | /* "_hlua_channel_dup" wrapper. If no data are available, it returns |
| 3075 | * a yield. This function consumes the data in the buffer. It returns |
| 3076 | * a string containing the data or a nil pointer if no data are available |
| 3077 | * and the channel is closed. |
| 3078 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3079 | __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] | 3080 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3081 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3082 | int ret; |
| 3083 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3084 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3085 | |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 3086 | if (IS_HTX_STRM(chn_strm(chn))) { |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3087 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3088 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3089 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3090 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3091 | ret = _hlua_channel_dup(chn, L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3092 | if (unlikely(ret == 0)) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3093 | 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] | 3094 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3095 | if (unlikely(ret == -1)) |
| 3096 | return 1; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3097 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3098 | b_sub(&chn->buf, ret); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3099 | return 1; |
| 3100 | } |
| 3101 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3102 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3103 | __LJMP static int hlua_channel_get(lua_State *L) |
| 3104 | { |
| 3105 | MAY_LJMP(check_args(L, 1, "get")); |
| 3106 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3107 | return MAY_LJMP(hlua_channel_get_yield(L, 0, 0)); |
| 3108 | } |
| 3109 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3110 | /* This functions consumes and returns one line. If the channel is closed, |
| 3111 | * 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] | 3112 | * 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] | 3113 | * value. |
| 3114 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3115 | __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] | 3116 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3117 | char *blk1; |
| 3118 | char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3119 | size_t len1; |
| 3120 | size_t len2; |
| 3121 | size_t len; |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3122 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3123 | int ret; |
| 3124 | luaL_Buffer b; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3125 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3126 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3127 | |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 3128 | if (IS_HTX_STRM(chn_strm(chn))) { |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3129 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3130 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3131 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3132 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3133 | ret = ci_getline_nc(chn, &blk1, &len1, &blk2, &len2); |
Christopher Faulet | 055310f | 2021-08-05 11:58:37 +0200 | [diff] [blame] | 3134 | if (ret == 0) { |
| 3135 | if (HLUA_CANT_YIELD(hlua_gethlua(L))) { |
| 3136 | _hlua_channel_dup(chn, L); |
| 3137 | return 1; |
| 3138 | } |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3139 | 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] | 3140 | } |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3141 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3142 | if (ret == -1) { |
| 3143 | lua_pushnil(L); |
| 3144 | return 1; |
| 3145 | } |
| 3146 | |
| 3147 | luaL_buffinit(L, &b); |
| 3148 | luaL_addlstring(&b, blk1, len1); |
| 3149 | len = len1; |
| 3150 | if (unlikely(ret == 2)) { |
| 3151 | luaL_addlstring(&b, blk2, len2); |
| 3152 | len += len2; |
| 3153 | } |
| 3154 | luaL_pushresult(&b); |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3155 | 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] | 3156 | return 1; |
| 3157 | } |
| 3158 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3159 | /* Check arguments for the function "hlua_channel_getline_yield". */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3160 | __LJMP static int hlua_channel_getline(lua_State *L) |
| 3161 | { |
| 3162 | MAY_LJMP(check_args(L, 1, "getline")); |
| 3163 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3164 | return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0)); |
| 3165 | } |
| 3166 | |
Christopher Faulet | 3b7bc3e | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3167 | /* This function takes a string as argument, and append it at the input side of |
| 3168 | * channel. If data cannot be copied, because there is not enough space to do so |
| 3169 | * or because the channel is closed, it returns -1. Otherwise, it returns the |
| 3170 | * amount of data written (the string length). This function does not yield. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3171 | */ |
Christopher Faulet | 3b7bc3e | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3172 | __LJMP static int hlua_channel_append(lua_State *L) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3173 | { |
Christopher Faulet | 3b7bc3e | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3174 | struct channel *chn; |
| 3175 | const char *str; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3176 | size_t len; |
Christopher Faulet | 3b7bc3e | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3177 | |
| 3178 | MAY_LJMP(check_args(L, 2, "append")); |
| 3179 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3180 | str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3181 | |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 3182 | if (IS_HTX_STRM(chn_strm(chn))) { |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3183 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3184 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3185 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3186 | |
Christopher Faulet | 3b7bc3e | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3187 | if (len > c_room(chn) || ci_putblk(chn, str, len) < 0) |
| 3188 | lua_pushinteger(L, -1); |
| 3189 | else |
| 3190 | lua_pushinteger(L, len); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3191 | return 1; |
| 3192 | } |
| 3193 | |
Christopher Faulet | 3b7bc3e | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3194 | /* The function replaces input data from the channel by the string passed as |
| 3195 | * argument. Before clearing the buffer, we take care the copy will be |
| 3196 | * possible. It returns the amount of data written (the string length) on |
| 3197 | * 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] | 3198 | */ |
| 3199 | __LJMP static int hlua_channel_set(lua_State *L) |
| 3200 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3201 | struct channel *chn; |
Christopher Faulet | 3b7bc3e | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3202 | const char *str; |
| 3203 | size_t len; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3204 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3205 | MAY_LJMP(check_args(L, 2, "set")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3206 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 3b7bc3e | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3207 | str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3208 | |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 3209 | if (IS_HTX_STRM(chn_strm(chn))) { |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3210 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3211 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3212 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3213 | |
Christopher Faulet | 3b7bc3e | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3214 | /* Be sure we can copied the string once input data will be removed. */ |
| 3215 | if (len > ci_data(chn) || channel_input_closed(chn)) |
| 3216 | lua_pushinteger(L, -1); |
| 3217 | else { |
| 3218 | b_set_data(&chn->buf, co_data(chn)); |
| 3219 | if (ci_putblk(chn, str, len) < 0) |
| 3220 | lua_pushinteger(L, -1); |
| 3221 | else |
| 3222 | lua_pushinteger(L, len); |
| 3223 | } |
| 3224 | return -1; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3225 | } |
| 3226 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3227 | /* Append data in the output side of the buffer. This data is immediately |
| 3228 | * sent. The function returns the amount of data written. If the buffer |
| 3229 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3230 | * if the channel is closed. |
| 3231 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3232 | __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] | 3233 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3234 | struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3235 | size_t len; |
| 3236 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3237 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3238 | int max; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 3239 | struct hlua *hlua; |
| 3240 | |
| 3241 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 3242 | hlua = hlua_gethlua(L); |
| 3243 | if (!hlua) { |
| 3244 | lua_pushnil(L); |
| 3245 | return 1; |
| 3246 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3247 | |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 3248 | if (IS_HTX_STRM(chn_strm(chn))) { |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3249 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3250 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3251 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3252 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3253 | if (unlikely(channel_output_closed(chn))) { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3254 | lua_pushinteger(L, -1); |
| 3255 | return 1; |
| 3256 | } |
| 3257 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3258 | /* Check if the buffer is available because HAProxy doesn't allocate |
Thierry FOURNIER | 3e3a608 | 2015-03-05 17:06:12 +0100 | [diff] [blame] | 3259 | * the request buffer if its not required. |
| 3260 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3261 | if (chn->buf.size == 0) { |
Christopher Faulet | 055310f | 2021-08-05 11:58:37 +0200 | [diff] [blame] | 3262 | if (HLUA_CANT_YIELD(hlua_gethlua(L))) |
| 3263 | return 1; |
Willy Tarreau | 4b962a4 | 2018-11-15 11:03:21 +0100 | [diff] [blame] | 3264 | si_rx_buff_blk(chn_prod(chn)); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3265 | 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] | 3266 | } |
| 3267 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3268 | /* The written data will be immediately sent, so we can check |
| 3269 | * the available space without taking in account the reserve. |
| 3270 | * The reserve is guaranteed for the processing of incoming |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3271 | * data, because the buffer will be flushed. |
| 3272 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3273 | max = b_room(&chn->buf); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3274 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3275 | /* If there is no space available, and the output buffer is empty. |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3276 | * in this case, we cannot add more data, so we cannot yield, |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3277 | * we return the amount of copied data. |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3278 | */ |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3279 | if (max == 0 && co_data(chn) == 0) |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3280 | return 1; |
| 3281 | |
| 3282 | /* Adjust the real required length. */ |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3283 | if (max > len - l) |
| 3284 | max = len - l; |
| 3285 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3286 | /* The buffer available size may be not contiguous. This test |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3287 | * detects a non contiguous buffer and realign it. |
| 3288 | */ |
Willy Tarreau | 3f67999 | 2018-06-15 15:06:42 +0200 | [diff] [blame] | 3289 | if (ci_space_for_replace(chn) < max) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3290 | channel_slow_realign(chn, trash.area); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3291 | |
| 3292 | /* Copy input data in the buffer. */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3293 | 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] | 3294 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3295 | /* buffer replace considers that the input part is filled. |
| 3296 | * so, I must forward these new data in the output part. |
| 3297 | */ |
Willy Tarreau | bcbd393 | 2018-06-06 07:13:22 +0200 | [diff] [blame] | 3298 | c_adv(chn, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3299 | |
| 3300 | l += max; |
| 3301 | lua_pop(L, 1); |
| 3302 | lua_pushinteger(L, l); |
| 3303 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3304 | if (l < len) { |
Christopher Faulet | 055310f | 2021-08-05 11:58:37 +0200 | [diff] [blame] | 3305 | /* If there is no space available, and the output buffer is empty. |
| 3306 | * in this case, we cannot add more data, so we cannot yield, |
| 3307 | * we return the amount of copied data. |
| 3308 | */ |
| 3309 | max = b_room(&chn->buf); |
| 3310 | if ((max == 0 && co_data(chn) == 0) || HLUA_CANT_YIELD(hlua_gethlua(L))) |
| 3311 | return 1; |
| 3312 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3313 | /* If we are waiting for space in the response buffer, we |
| 3314 | * must set the flag WAKERESWR. This flag required the task |
| 3315 | * wake up if any activity is detected on the response buffer. |
| 3316 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3317 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3318 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3319 | else |
| 3320 | HLUA_SET_WAKEREQWR(hlua); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3321 | 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] | 3322 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3323 | |
| 3324 | return 1; |
| 3325 | } |
| 3326 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3327 | /* Just a wrapper of "_hlua_channel_send". This wrapper permits |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3328 | * yield the LUA process, and resume it without checking the |
| 3329 | * input arguments. |
| 3330 | */ |
| 3331 | __LJMP static int hlua_channel_send(lua_State *L) |
| 3332 | { |
| 3333 | MAY_LJMP(check_args(L, 2, "send")); |
| 3334 | lua_pushinteger(L, 0); |
| 3335 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3336 | return MAY_LJMP(hlua_channel_send_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3337 | } |
| 3338 | |
| 3339 | /* This function forward and amount of butes. The data pass from |
| 3340 | * the input side of the buffer to the output side, and can be |
| 3341 | * forwarded. This function never fails. |
| 3342 | * |
| 3343 | * The Lua function takes an amount of bytes to be forwarded in |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3344 | * input. It returns the number of bytes forwarded. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3345 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3346 | __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] | 3347 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3348 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3349 | int len; |
| 3350 | int l; |
| 3351 | int max; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 3352 | struct hlua *hlua; |
| 3353 | |
| 3354 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 3355 | hlua = hlua_gethlua(L); |
| 3356 | if (!hlua) |
| 3357 | return 1; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3358 | |
| 3359 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3360 | |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 3361 | if (IS_HTX_STRM(chn_strm(chn))) { |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3362 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3363 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3364 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3365 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3366 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3367 | l = MAY_LJMP(luaL_checkinteger(L, -1)); |
| 3368 | |
| 3369 | max = len - l; |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3370 | if (max > ci_data(chn)) |
| 3371 | max = ci_data(chn); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3372 | channel_forward(chn, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3373 | l += max; |
| 3374 | |
| 3375 | lua_pop(L, 1); |
| 3376 | lua_pushinteger(L, l); |
| 3377 | |
| 3378 | /* Check if it miss bytes to forward. */ |
| 3379 | if (l < len) { |
| 3380 | /* The the input channel or the output channel are closed, we |
| 3381 | * must return the amount of data forwarded. |
| 3382 | */ |
Christopher Faulet | 055310f | 2021-08-05 11:58:37 +0200 | [diff] [blame] | 3383 | 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] | 3384 | return 1; |
| 3385 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3386 | /* If we are waiting for space data in the response buffer, we |
| 3387 | * must set the flag WAKERESWR. This flag required the task |
| 3388 | * wake up if any activity is detected on the response buffer. |
| 3389 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3390 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3391 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3392 | else |
| 3393 | HLUA_SET_WAKEREQWR(hlua); |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3394 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3395 | /* Otherwise, we can yield waiting for new data in the inpout side. */ |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3396 | 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] | 3397 | } |
| 3398 | |
| 3399 | return 1; |
| 3400 | } |
| 3401 | |
| 3402 | /* Just check the input and prepare the stack for the previous |
| 3403 | * function "hlua_channel_forward_yield" |
| 3404 | */ |
| 3405 | __LJMP static int hlua_channel_forward(lua_State *L) |
| 3406 | { |
| 3407 | MAY_LJMP(check_args(L, 2, "forward")); |
| 3408 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3409 | MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3410 | |
| 3411 | lua_pushinteger(L, 0); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3412 | return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3413 | } |
| 3414 | |
| 3415 | /* Just returns the number of bytes available in the input |
| 3416 | * side of the buffer. This function never fails. |
| 3417 | */ |
| 3418 | __LJMP static int hlua_channel_get_in_len(lua_State *L) |
| 3419 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3420 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3421 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3422 | MAY_LJMP(check_args(L, 1, "get_in_len")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3423 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a3ceac1 | 2018-12-14 13:39:09 +0100 | [diff] [blame] | 3424 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3425 | struct htx *htx = htxbuf(&chn->buf); |
| 3426 | lua_pushinteger(L, htx->data - co_data(chn)); |
| 3427 | } |
| 3428 | else |
| 3429 | lua_pushinteger(L, ci_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3430 | return 1; |
| 3431 | } |
| 3432 | |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3433 | /* Returns true if the channel is full. */ |
| 3434 | __LJMP static int hlua_channel_is_full(lua_State *L) |
| 3435 | { |
| 3436 | struct channel *chn; |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3437 | |
| 3438 | MAY_LJMP(check_args(L, 1, "is_full")); |
| 3439 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 0ec740e | 2020-02-26 11:59:19 +0100 | [diff] [blame] | 3440 | /* ignore the reserve, we are not on a producer side (ie in an |
| 3441 | * applet). |
| 3442 | */ |
| 3443 | lua_pushboolean(L, channel_full(chn, 0)); |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3444 | return 1; |
| 3445 | } |
| 3446 | |
Christopher Faulet | 2ac9ba2 | 2020-02-25 10:15:50 +0100 | [diff] [blame] | 3447 | /* Returns true if the channel is the response channel. */ |
| 3448 | __LJMP static int hlua_channel_is_resp(lua_State *L) |
| 3449 | { |
| 3450 | struct channel *chn; |
| 3451 | |
| 3452 | MAY_LJMP(check_args(L, 1, "is_resp")); |
| 3453 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3454 | |
| 3455 | lua_pushboolean(L, !!(chn->flags & CF_ISRESP)); |
| 3456 | return 1; |
| 3457 | } |
| 3458 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3459 | /* Just returns the number of bytes available in the output |
| 3460 | * side of the buffer. This function never fails. |
| 3461 | */ |
| 3462 | __LJMP static int hlua_channel_get_out_len(lua_State *L) |
| 3463 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3464 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3465 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3466 | MAY_LJMP(check_args(L, 1, "get_out_len")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3467 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3468 | lua_pushinteger(L, co_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3469 | return 1; |
| 3470 | } |
| 3471 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3472 | /* |
| 3473 | * |
| 3474 | * |
| 3475 | * Class Fetches |
| 3476 | * |
| 3477 | * |
| 3478 | */ |
| 3479 | |
| 3480 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3481 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3482 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3483 | __LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud) |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3484 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3485 | return MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3486 | } |
| 3487 | |
| 3488 | /* This function creates and push in the stack a fetch object according |
| 3489 | * with a current TXN. |
| 3490 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3491 | 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] | 3492 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3493 | struct hlua_smp *hsmp; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3494 | |
| 3495 | /* Check stack size. */ |
| 3496 | if (!lua_checkstack(L, 3)) |
| 3497 | return 0; |
| 3498 | |
| 3499 | /* Create the object: obj[0] = userdata. |
| 3500 | * Note that the base of the Fetches object is the |
| 3501 | * transaction object. |
| 3502 | */ |
| 3503 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3504 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3505 | lua_rawseti(L, -2, 0); |
| 3506 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3507 | hsmp->s = txn->s; |
| 3508 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 3509 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3510 | hsmp->flags = flags; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3511 | |
| 3512 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 3513 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref); |
| 3514 | lua_setmetatable(L, -2); |
| 3515 | |
| 3516 | return 1; |
| 3517 | } |
| 3518 | |
| 3519 | /* This function is an LUA binding. It is called with each sample-fetch. |
| 3520 | * It uses closure argument to store the associated sample-fetch. It |
| 3521 | * returns only one argument or throws an error. An error is thrown |
| 3522 | * only if an error is encountered during the argument parsing. If |
| 3523 | * the "sample-fetch" function fails, nil is returned. |
| 3524 | */ |
| 3525 | __LJMP static int hlua_run_sample_fetch(lua_State *L) |
| 3526 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3527 | struct hlua_smp *hsmp; |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 3528 | struct sample_fetch *f; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 3529 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3530 | int i; |
| 3531 | struct sample smp; |
| 3532 | |
| 3533 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3534 | f = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3535 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3536 | /* Get traditional arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3537 | hsmp = MAY_LJMP(hlua_checkfetches(L, 1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3538 | |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 3539 | /* Check execution authorization. */ |
| 3540 | if (f->use & SMP_USE_HTTP_ANY && |
| 3541 | !(hsmp->flags & HLUA_F_MAY_USE_HTTP)) { |
| 3542 | lua_pushfstring(L, "the sample-fetch '%s' needs an HTTP parser which " |
| 3543 | "is not available in Lua services", f->kw); |
| 3544 | WILL_LJMP(lua_error(L)); |
| 3545 | } |
| 3546 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3547 | /* Get extra arguments. */ |
| 3548 | for (i = 0; i < lua_gettop(L) - 1; i++) { |
| 3549 | if (i >= ARGM_NBARGS) |
| 3550 | break; |
| 3551 | hlua_lua2arg(L, i + 2, &args[i]); |
| 3552 | } |
| 3553 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3554 | args[i].data.str.area = NULL; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3555 | |
| 3556 | /* Check arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3557 | 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] | 3558 | |
| 3559 | /* Run the special args checker. */ |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 3560 | if (f->val_args && !f->val_args(args, NULL)) { |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3561 | lua_pushfstring(L, "error in arguments"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3562 | goto error; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3563 | } |
| 3564 | |
| 3565 | /* Initialise the sample. */ |
| 3566 | memset(&smp, 0, sizeof(smp)); |
| 3567 | |
| 3568 | /* Run the sample fetch process. */ |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 3569 | 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] | 3570 | if (!f->process(args, &smp, f->kw, f->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3571 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3572 | lua_pushstring(L, ""); |
| 3573 | else |
| 3574 | lua_pushnil(L); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3575 | goto end; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3576 | } |
| 3577 | |
| 3578 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3579 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3580 | hlua_smp2lua_str(L, &smp); |
| 3581 | else |
| 3582 | hlua_smp2lua(L, &smp); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3583 | |
| 3584 | end: |
| 3585 | for (i = 0; args[i].type != ARGT_STOP; i++) { |
| 3586 | if (args[i].type == ARGT_STR) |
| 3587 | chunk_destroy(&args[i].data.str); |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 3588 | else if (args[i].type == ARGT_REG) |
| 3589 | regex_free(args[i].data.reg); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3590 | } |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3591 | return 1; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3592 | |
| 3593 | error: |
| 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 | } |
| 3600 | WILL_LJMP(lua_error(L)); |
| 3601 | return 0; /* Never reached */ |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3602 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3603 | |
| 3604 | /* |
| 3605 | * |
| 3606 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3607 | * Class Converters |
| 3608 | * |
| 3609 | * |
| 3610 | */ |
| 3611 | |
| 3612 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3613 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3614 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3615 | __LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3616 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3617 | return MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3618 | } |
| 3619 | |
| 3620 | /* This function creates and push in the stack a Converters object |
| 3621 | * according with a current TXN. |
| 3622 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3623 | 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] | 3624 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3625 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3626 | |
| 3627 | /* Check stack size. */ |
| 3628 | if (!lua_checkstack(L, 3)) |
| 3629 | return 0; |
| 3630 | |
| 3631 | /* Create the object: obj[0] = userdata. |
| 3632 | * Note that the base of the Converters object is the |
| 3633 | * same than the TXN object. |
| 3634 | */ |
| 3635 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3636 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3637 | lua_rawseti(L, -2, 0); |
| 3638 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3639 | hsmp->s = txn->s; |
| 3640 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 3641 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3642 | hsmp->flags = flags; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3643 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3644 | /* Pop a class stream metatable and affect it to the table. */ |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3645 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref); |
| 3646 | lua_setmetatable(L, -2); |
| 3647 | |
| 3648 | return 1; |
| 3649 | } |
| 3650 | |
| 3651 | /* This function is an LUA binding. It is called with each converter. |
| 3652 | * It uses closure argument to store the associated converter. It |
| 3653 | * returns only one argument or throws an error. An error is thrown |
| 3654 | * only if an error is encountered during the argument parsing. If |
| 3655 | * the converter function function fails, nil is returned. |
| 3656 | */ |
| 3657 | __LJMP static int hlua_run_sample_conv(lua_State *L) |
| 3658 | { |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3659 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3660 | struct sample_conv *conv; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 3661 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3662 | int i; |
| 3663 | struct sample smp; |
| 3664 | |
| 3665 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3666 | conv = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3667 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3668 | /* Get traditional arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3669 | hsmp = MAY_LJMP(hlua_checkconverters(L, 1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3670 | |
| 3671 | /* Get extra arguments. */ |
| 3672 | for (i = 0; i < lua_gettop(L) - 2; i++) { |
| 3673 | if (i >= ARGM_NBARGS) |
| 3674 | break; |
| 3675 | hlua_lua2arg(L, i + 3, &args[i]); |
| 3676 | } |
| 3677 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3678 | args[i].data.str.area = NULL; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3679 | |
| 3680 | /* Check arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3681 | 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] | 3682 | |
| 3683 | /* Run the special args checker. */ |
| 3684 | if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) { |
| 3685 | hlua_pusherror(L, "error in arguments"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3686 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3687 | } |
| 3688 | |
| 3689 | /* Initialise the sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 3690 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3691 | if (!hlua_lua2smp(L, 2, &smp)) { |
| 3692 | hlua_pusherror(L, "error in the input argument"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3693 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3694 | } |
| 3695 | |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 3696 | smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR); |
| 3697 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3698 | /* Apply expected cast. */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3699 | if (!sample_casts[smp.data.type][conv->in_type]) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3700 | hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'", |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3701 | smp_to_type[smp.data.type], smp_to_type[conv->in_type]); |
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 | } |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3704 | if (sample_casts[smp.data.type][conv->in_type] != c_none && |
| 3705 | !sample_casts[smp.data.type][conv->in_type](&smp)) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3706 | hlua_pusherror(L, "error during the input argument casting"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3707 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3708 | } |
| 3709 | |
| 3710 | /* Run the sample conversion process. */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 3711 | if (!conv->process(args, &smp, conv->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3712 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3713 | lua_pushstring(L, ""); |
| 3714 | else |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 3715 | lua_pushnil(L); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3716 | goto end; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3717 | } |
| 3718 | |
| 3719 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3720 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3721 | hlua_smp2lua_str(L, &smp); |
| 3722 | else |
| 3723 | hlua_smp2lua(L, &smp); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3724 | end: |
| 3725 | for (i = 0; args[i].type != ARGT_STOP; i++) { |
| 3726 | if (args[i].type == ARGT_STR) |
| 3727 | chunk_destroy(&args[i].data.str); |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 3728 | else if (args[i].type == ARGT_REG) |
| 3729 | regex_free(args[i].data.reg); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3730 | } |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 3731 | return 1; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3732 | |
| 3733 | error: |
| 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 | } |
| 3740 | WILL_LJMP(lua_error(L)); |
| 3741 | return 0; /* Never reached */ |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3742 | } |
| 3743 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3744 | /* |
| 3745 | * |
| 3746 | * |
| 3747 | * Class AppletTCP |
| 3748 | * |
| 3749 | * |
| 3750 | */ |
| 3751 | |
| 3752 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 3753 | * a class stream, otherwise it throws an error. |
| 3754 | */ |
| 3755 | __LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud) |
| 3756 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3757 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3758 | } |
| 3759 | |
| 3760 | /* This function creates and push in the stack an Applet object |
| 3761 | * according with a current TXN. |
| 3762 | */ |
| 3763 | static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx) |
| 3764 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3765 | struct hlua_appctx *luactx; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3766 | struct stream_interface *si = ctx->owner; |
| 3767 | struct stream *s = si_strm(si); |
| 3768 | struct proxy *p = s->be; |
| 3769 | |
| 3770 | /* Check stack size. */ |
| 3771 | if (!lua_checkstack(L, 3)) |
| 3772 | return 0; |
| 3773 | |
| 3774 | /* Create the object: obj[0] = userdata. |
| 3775 | * Note that the base of the Converters object is the |
| 3776 | * same than the TXN object. |
| 3777 | */ |
| 3778 | lua_newtable(L); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3779 | luactx = lua_newuserdata(L, sizeof(*luactx)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3780 | lua_rawseti(L, -2, 0); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3781 | luactx->appctx = ctx; |
| 3782 | luactx->htxn.s = s; |
| 3783 | luactx->htxn.p = p; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3784 | |
| 3785 | /* Create the "f" field that contains a list of fetches. */ |
| 3786 | lua_pushstring(L, "f"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3787 | if (!hlua_fetches_new(L, &luactx->htxn, 0)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3788 | return 0; |
| 3789 | lua_settable(L, -3); |
| 3790 | |
| 3791 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 3792 | lua_pushstring(L, "sf"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3793 | if (!hlua_fetches_new(L, &luactx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3794 | return 0; |
| 3795 | lua_settable(L, -3); |
| 3796 | |
| 3797 | /* Create the "c" field that contains a list of converters. */ |
| 3798 | lua_pushstring(L, "c"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3799 | if (!hlua_converters_new(L, &luactx->htxn, 0)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3800 | return 0; |
| 3801 | lua_settable(L, -3); |
| 3802 | |
| 3803 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 3804 | lua_pushstring(L, "sc"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3805 | if (!hlua_converters_new(L, &luactx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3806 | return 0; |
| 3807 | lua_settable(L, -3); |
| 3808 | |
| 3809 | /* Pop a class stream metatable and affect it to the table. */ |
| 3810 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref); |
| 3811 | lua_setmetatable(L, -2); |
| 3812 | |
| 3813 | return 1; |
| 3814 | } |
| 3815 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3816 | __LJMP static int hlua_applet_tcp_set_var(lua_State *L) |
| 3817 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3818 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3819 | struct stream *s; |
| 3820 | const char *name; |
| 3821 | size_t len; |
| 3822 | struct sample smp; |
| 3823 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 3824 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 3825 | 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] | 3826 | |
| 3827 | /* It is useles to retrieve the stream, but this function |
| 3828 | * runs only in a stream context. |
| 3829 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3830 | luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3831 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3832 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3833 | |
| 3834 | /* Converts the third argument in a sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 3835 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3836 | hlua_lua2smp(L, 3, &smp); |
| 3837 | |
Aurelien DARRAGON | d39fb74 | 2023-05-17 16:06:11 +0200 | [diff] [blame] | 3838 | /* Store the sample in a variable. We don't need to dup the smp, vars API |
| 3839 | * already takes care of duplicating dynamic var data. |
| 3840 | */ |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3841 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 3842 | |
| 3843 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 3844 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 3845 | else |
| 3846 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 3847 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 3848 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3849 | } |
| 3850 | |
| 3851 | __LJMP static int hlua_applet_tcp_unset_var(lua_State *L) |
| 3852 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3853 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3854 | struct stream *s; |
| 3855 | const char *name; |
| 3856 | size_t len; |
| 3857 | struct sample smp; |
| 3858 | |
| 3859 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 3860 | |
| 3861 | /* It is useles to retrieve the stream, but this function |
| 3862 | * runs only in a stream context. |
| 3863 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3864 | luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3865 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3866 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3867 | |
| 3868 | /* Unset the variable. */ |
| 3869 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 3870 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 3871 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3872 | } |
| 3873 | |
| 3874 | __LJMP static int hlua_applet_tcp_get_var(lua_State *L) |
| 3875 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3876 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3877 | struct stream *s; |
| 3878 | const char *name; |
| 3879 | size_t len; |
| 3880 | struct sample smp; |
| 3881 | |
| 3882 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 3883 | |
| 3884 | /* It is useles to retrieve the stream, but this function |
| 3885 | * runs only in a stream context. |
| 3886 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3887 | luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3888 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3889 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3890 | |
| 3891 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 3892 | if (!vars_get_by_name(name, len, &smp)) { |
| 3893 | lua_pushnil(L); |
| 3894 | return 1; |
| 3895 | } |
| 3896 | |
| 3897 | return hlua_smp2lua(L, &smp); |
| 3898 | } |
| 3899 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3900 | __LJMP static int hlua_applet_tcp_set_priv(lua_State *L) |
| 3901 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3902 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3903 | struct stream *s = luactx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 3904 | struct hlua *hlua; |
| 3905 | |
| 3906 | /* 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] | 3907 | if (!s->hlua) |
| 3908 | return 0; |
| 3909 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3910 | |
| 3911 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 3912 | |
| 3913 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 3914 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3915 | |
| 3916 | /* Get and store new value. */ |
| 3917 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 3918 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 3919 | |
| 3920 | return 0; |
| 3921 | } |
| 3922 | |
| 3923 | __LJMP static int hlua_applet_tcp_get_priv(lua_State *L) |
| 3924 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3925 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3926 | struct stream *s = luactx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 3927 | struct hlua *hlua; |
| 3928 | |
| 3929 | /* 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] | 3930 | if (!s->hlua) { |
| 3931 | lua_pushnil(L); |
| 3932 | return 1; |
| 3933 | } |
| 3934 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3935 | |
| 3936 | /* Push configuration index in the stack. */ |
| 3937 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 3938 | |
| 3939 | return 1; |
| 3940 | } |
| 3941 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3942 | /* If expected data not yet available, it returns a yield. This function |
| 3943 | * consumes the data in the buffer. It returns a string containing the |
| 3944 | * data. This string can be empty. |
| 3945 | */ |
| 3946 | __LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx) |
| 3947 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3948 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3949 | struct stream_interface *si = luactx->appctx->owner; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3950 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3951 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3952 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3953 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3954 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3955 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3956 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3957 | ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3958 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3959 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3960 | if (ret == 0) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3961 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3962 | 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] | 3963 | } |
| 3964 | |
| 3965 | /* End of data: commit the total strings and return. */ |
| 3966 | if (ret < 0) { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3967 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3968 | return 1; |
| 3969 | } |
| 3970 | |
| 3971 | /* Ensure that the block 2 length is usable. */ |
| 3972 | if (ret == 1) |
| 3973 | len2 = 0; |
| 3974 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 3975 | /* don't check the max length read and don't check. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3976 | luaL_addlstring(&luactx->b, blk1, len1); |
| 3977 | luaL_addlstring(&luactx->b, blk2, len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3978 | |
| 3979 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3980 | co_skip(si_oc(si), len1 + len2); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3981 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3982 | return 1; |
| 3983 | } |
| 3984 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3985 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3986 | __LJMP static int hlua_applet_tcp_getline(lua_State *L) |
| 3987 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3988 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3989 | |
| 3990 | /* Initialise the string catenation. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3991 | luaL_buffinit(L, &luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3992 | |
| 3993 | return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0)); |
| 3994 | } |
| 3995 | |
| 3996 | /* If expected data not yet available, it returns a yield. This function |
| 3997 | * consumes the data in the buffer. It returns a string containing the |
| 3998 | * data. This string can be empty. |
| 3999 | */ |
| 4000 | __LJMP static int hlua_applet_tcp_recv_yield(lua_State *L, int status, lua_KContext ctx) |
| 4001 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4002 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 4003 | struct stream_interface *si = luactx->appctx->owner; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4004 | size_t len = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4005 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4006 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4007 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4008 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4009 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4010 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4011 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4012 | ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4013 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4014 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4015 | if (ret == 0) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 4016 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4017 | 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] | 4018 | } |
| 4019 | |
| 4020 | /* End of data: commit the total strings and return. */ |
| 4021 | if (ret < 0) { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4022 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4023 | return 1; |
| 4024 | } |
| 4025 | |
| 4026 | /* Ensure that the block 2 length is usable. */ |
| 4027 | if (ret == 1) |
| 4028 | len2 = 0; |
| 4029 | |
| 4030 | if (len == -1) { |
| 4031 | |
| 4032 | /* If len == -1, catenate all the data avalaile and |
| 4033 | * yield because we want to get all the data until |
| 4034 | * the end of data stream. |
| 4035 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4036 | luaL_addlstring(&luactx->b, blk1, len1); |
| 4037 | luaL_addlstring(&luactx->b, blk2, len2); |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4038 | co_skip(si_oc(si), len1 + len2); |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 4039 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4040 | 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] | 4041 | |
| 4042 | } else { |
| 4043 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4044 | /* Copy the first block caping to the length required. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4045 | if (len1 > len) |
| 4046 | len1 = len; |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4047 | luaL_addlstring(&luactx->b, blk1, len1); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4048 | len -= len1; |
| 4049 | |
| 4050 | /* Copy the second block. */ |
| 4051 | if (len2 > len) |
| 4052 | len2 = len; |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4053 | luaL_addlstring(&luactx->b, blk2, len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4054 | len -= len2; |
| 4055 | |
| 4056 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4057 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4058 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4059 | /* If there is no other data available, yield waiting for new data. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4060 | if (len > 0) { |
| 4061 | lua_pushinteger(L, len); |
| 4062 | lua_replace(L, 2); |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 4063 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4064 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4065 | } |
| 4066 | |
| 4067 | /* return the result. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4068 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4069 | return 1; |
| 4070 | } |
| 4071 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4072 | /* we never execute this */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4073 | hlua_pusherror(L, "Lua: internal error"); |
| 4074 | WILL_LJMP(lua_error(L)); |
| 4075 | return 0; |
| 4076 | } |
| 4077 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4078 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4079 | __LJMP static int hlua_applet_tcp_recv(lua_State *L) |
| 4080 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4081 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4082 | int len = -1; |
| 4083 | |
| 4084 | if (lua_gettop(L) > 2) |
| 4085 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 4086 | if (lua_gettop(L) >= 2) { |
| 4087 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4088 | lua_pop(L, 1); |
| 4089 | } |
| 4090 | |
| 4091 | /* Confirm or set the required length */ |
| 4092 | lua_pushinteger(L, len); |
| 4093 | |
| 4094 | /* Initialise the string catenation. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4095 | luaL_buffinit(L, &luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4096 | |
| 4097 | return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0)); |
| 4098 | } |
| 4099 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4100 | /* Append data in the output side of the buffer. This data is immediately |
| 4101 | * sent. The function returns the amount of data written. If the buffer |
| 4102 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4103 | * if the channel is closed. |
| 4104 | */ |
| 4105 | __LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx) |
| 4106 | { |
| 4107 | size_t len; |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4108 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4109 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4110 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4111 | struct stream_interface *si = luactx->appctx->owner; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4112 | struct channel *chn = si_ic(si); |
| 4113 | int max; |
| 4114 | |
| 4115 | /* Get the max amount of data which can write as input in the channel. */ |
| 4116 | max = channel_recv_max(chn); |
| 4117 | if (max > (len - l)) |
| 4118 | max = len - l; |
| 4119 | |
| 4120 | /* Copy data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4121 | ci_putblk(chn, str + l, max); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4122 | |
| 4123 | /* update counters. */ |
| 4124 | l += max; |
| 4125 | lua_pop(L, 1); |
| 4126 | lua_pushinteger(L, l); |
| 4127 | |
| 4128 | /* If some data is not send, declares the situation to the |
| 4129 | * applet, and returns a yield. |
| 4130 | */ |
| 4131 | if (l < len) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 4132 | si_rx_room_blk(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4133 | 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] | 4134 | } |
| 4135 | |
| 4136 | return 1; |
| 4137 | } |
| 4138 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4139 | /* Just a wrapper of "hlua_applet_tcp_send_yield". This wrapper permits |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4140 | * yield the LUA process, and resume it without checking the |
| 4141 | * input arguments. |
| 4142 | */ |
| 4143 | __LJMP static int hlua_applet_tcp_send(lua_State *L) |
| 4144 | { |
| 4145 | MAY_LJMP(check_args(L, 2, "send")); |
| 4146 | lua_pushinteger(L, 0); |
| 4147 | |
| 4148 | return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0)); |
| 4149 | } |
| 4150 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4151 | /* |
| 4152 | * |
| 4153 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4154 | * Class AppletHTTP |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4155 | * |
| 4156 | * |
| 4157 | */ |
| 4158 | |
| 4159 | /* Returns a struct hlua_txn if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4160 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4161 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4162 | __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] | 4163 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4164 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_http_ref)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4165 | } |
| 4166 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4167 | /* This function creates and push in the stack an Applet object |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4168 | * according with a current TXN. |
| 4169 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4170 | static int hlua_applet_http_new(lua_State *L, struct appctx *ctx) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4171 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4172 | struct hlua_appctx *luactx; |
Thierry FOURNIER | 841475e | 2015-12-11 17:10:09 +0100 | [diff] [blame] | 4173 | struct hlua_txn htxn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4174 | struct stream_interface *si = ctx->owner; |
| 4175 | struct stream *s = si_strm(si); |
| 4176 | struct proxy *px = s->be; |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4177 | struct htx *htx; |
| 4178 | struct htx_blk *blk; |
| 4179 | struct htx_sl *sl; |
| 4180 | struct ist path; |
| 4181 | unsigned long long len = 0; |
| 4182 | int32_t pos; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4183 | |
| 4184 | /* Check stack size. */ |
| 4185 | if (!lua_checkstack(L, 3)) |
| 4186 | return 0; |
| 4187 | |
| 4188 | /* Create the object: obj[0] = userdata. |
| 4189 | * Note that the base of the Converters object is the |
| 4190 | * same than the TXN object. |
| 4191 | */ |
| 4192 | lua_newtable(L); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4193 | luactx = lua_newuserdata(L, sizeof(*luactx)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4194 | lua_rawseti(L, -2, 0); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4195 | luactx->appctx = ctx; |
| 4196 | luactx->appctx->ctx.hlua_apphttp.status = 200; /* Default status code returned. */ |
| 4197 | luactx->appctx->ctx.hlua_apphttp.reason = NULL; /* Use default reason based on status */ |
| 4198 | luactx->htxn.s = s; |
| 4199 | luactx->htxn.p = px; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4200 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4201 | /* Create the "f" field that contains a list of fetches. */ |
| 4202 | lua_pushstring(L, "f"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4203 | if (!hlua_fetches_new(L, &luactx->htxn, 0)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4204 | return 0; |
| 4205 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4206 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4207 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 4208 | lua_pushstring(L, "sf"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4209 | if (!hlua_fetches_new(L, &luactx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4210 | return 0; |
| 4211 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4212 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4213 | /* Create the "c" field that contains a list of converters. */ |
| 4214 | lua_pushstring(L, "c"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4215 | if (!hlua_converters_new(L, &luactx->htxn, 0)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4216 | return 0; |
| 4217 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4218 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4219 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 4220 | lua_pushstring(L, "sc"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4221 | if (!hlua_converters_new(L, &luactx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4222 | return 0; |
| 4223 | lua_settable(L, -3); |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4224 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4225 | htx = htxbuf(&s->req.buf); |
| 4226 | blk = htx_get_first_blk(htx); |
Christopher Faulet | ea00973 | 2019-11-18 15:50:25 +0100 | [diff] [blame] | 4227 | BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4228 | sl = htx_get_blk_ptr(htx, blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4229 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4230 | /* Stores the request method. */ |
| 4231 | lua_pushstring(L, "method"); |
| 4232 | lua_pushlstring(L, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); |
| 4233 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4234 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4235 | /* Stores the http version. */ |
| 4236 | lua_pushstring(L, "version"); |
| 4237 | lua_pushlstring(L, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); |
| 4238 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4239 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4240 | /* creates an array of headers. hlua_http_get_headers() crates and push |
| 4241 | * the array on the top of the stack. |
| 4242 | */ |
| 4243 | lua_pushstring(L, "headers"); |
| 4244 | htxn.s = s; |
| 4245 | htxn.p = px; |
| 4246 | htxn.dir = SMP_OPT_DIR_REQ; |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 4247 | if (!hlua_http_get_headers(L, &htxn.s->txn->req)) |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4248 | return 0; |
| 4249 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4250 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4251 | path = http_get_path(htx_sl_req_uri(sl)); |
Tim Duesterhus | ed52637 | 2020-03-05 17:56:33 +0100 | [diff] [blame] | 4252 | if (isttest(path)) { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4253 | char *p, *q, *end; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4254 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4255 | p = path.ptr; |
| 4256 | end = path.ptr + path.len; |
| 4257 | q = p; |
| 4258 | while (q < end && *q != '?') |
| 4259 | q++; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4260 | |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 4261 | /* Stores the request path. */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4262 | lua_pushstring(L, "path"); |
| 4263 | lua_pushlstring(L, p, q - p); |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 4264 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4265 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4266 | /* Stores the query string. */ |
| 4267 | lua_pushstring(L, "qs"); |
| 4268 | if (*q == '?') |
| 4269 | q++; |
| 4270 | lua_pushlstring(L, q, end - q); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4271 | lua_settable(L, -3); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4272 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4273 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4274 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 4275 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 4276 | enum htx_blk_type type = htx_get_blk_type(blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4277 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 4278 | if (type == HTX_BLK_TLR || type == HTX_BLK_EOT) |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4279 | break; |
| 4280 | if (type == HTX_BLK_DATA) |
| 4281 | len += htx_get_blksz(blk); |
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 | if (htx->extra != ULLONG_MAX) |
| 4284 | len += htx->extra; |
| 4285 | |
| 4286 | /* Stores the request path. */ |
| 4287 | lua_pushstring(L, "length"); |
| 4288 | lua_pushinteger(L, len); |
| 4289 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4290 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4291 | /* Create an empty array of HTTP request headers. */ |
| 4292 | lua_pushstring(L, "response"); |
| 4293 | lua_newtable(L); |
| 4294 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4295 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4296 | /* Pop a class stream metatable and affect it to the table. */ |
| 4297 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref); |
| 4298 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4299 | |
| 4300 | return 1; |
| 4301 | } |
| 4302 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4303 | __LJMP static int hlua_applet_http_set_var(lua_State *L) |
| 4304 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4305 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4306 | struct stream *s; |
| 4307 | const char *name; |
| 4308 | size_t len; |
| 4309 | struct sample smp; |
| 4310 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 4311 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 4312 | 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] | 4313 | |
| 4314 | /* It is useles to retrieve the stream, but this function |
| 4315 | * runs only in a stream context. |
| 4316 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4317 | luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4318 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4319 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4320 | |
| 4321 | /* Converts the third argument in a sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 4322 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4323 | hlua_lua2smp(L, 3, &smp); |
| 4324 | |
Aurelien DARRAGON | d39fb74 | 2023-05-17 16:06:11 +0200 | [diff] [blame] | 4325 | /* Store the sample in a variable. We don't need to dup the smp, vars API |
| 4326 | * already takes care of duplicating dynamic var data. |
| 4327 | */ |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4328 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 4329 | |
| 4330 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 4331 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 4332 | else |
| 4333 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 4334 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 4335 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4336 | } |
| 4337 | |
| 4338 | __LJMP static int hlua_applet_http_unset_var(lua_State *L) |
| 4339 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4340 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4341 | struct stream *s; |
| 4342 | const char *name; |
| 4343 | size_t len; |
| 4344 | struct sample smp; |
| 4345 | |
| 4346 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 4347 | |
| 4348 | /* It is useles to retrieve the stream, but this function |
| 4349 | * runs only in a stream context. |
| 4350 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4351 | luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4352 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4353 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4354 | |
| 4355 | /* Unset the variable. */ |
| 4356 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 4357 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 4358 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4359 | } |
| 4360 | |
| 4361 | __LJMP static int hlua_applet_http_get_var(lua_State *L) |
| 4362 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4363 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4364 | struct stream *s; |
| 4365 | const char *name; |
| 4366 | size_t len; |
| 4367 | struct sample smp; |
| 4368 | |
| 4369 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 4370 | |
| 4371 | /* It is useles to retrieve the stream, but this function |
| 4372 | * runs only in a stream context. |
| 4373 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4374 | luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4375 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4376 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4377 | |
| 4378 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 4379 | if (!vars_get_by_name(name, len, &smp)) { |
| 4380 | lua_pushnil(L); |
| 4381 | return 1; |
| 4382 | } |
| 4383 | |
| 4384 | return hlua_smp2lua(L, &smp); |
| 4385 | } |
| 4386 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4387 | __LJMP static int hlua_applet_http_set_priv(lua_State *L) |
| 4388 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4389 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4390 | struct stream *s = luactx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4391 | struct hlua *hlua; |
| 4392 | |
| 4393 | /* 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] | 4394 | if (!s->hlua) |
| 4395 | return 0; |
| 4396 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4397 | |
| 4398 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 4399 | |
| 4400 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 4401 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4402 | |
| 4403 | /* Get and store new value. */ |
| 4404 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 4405 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 4406 | |
| 4407 | return 0; |
| 4408 | } |
| 4409 | |
| 4410 | __LJMP static int hlua_applet_http_get_priv(lua_State *L) |
| 4411 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4412 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4413 | struct stream *s = luactx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4414 | struct hlua *hlua; |
| 4415 | |
| 4416 | /* 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] | 4417 | if (!s->hlua) { |
| 4418 | lua_pushnil(L); |
| 4419 | return 1; |
| 4420 | } |
| 4421 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4422 | |
| 4423 | /* Push configuration index in the stack. */ |
| 4424 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 4425 | |
| 4426 | return 1; |
| 4427 | } |
| 4428 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4429 | /* If expected data not yet available, it returns a yield. This function |
| 4430 | * consumes the data in the buffer. It returns a string containing the |
| 4431 | * data. This string can be empty. |
| 4432 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4433 | __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] | 4434 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4435 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4436 | struct stream_interface *si = luactx->appctx->owner; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4437 | struct channel *req = si_oc(si); |
| 4438 | struct htx *htx; |
| 4439 | struct htx_blk *blk; |
| 4440 | size_t count; |
| 4441 | int stop = 0; |
| 4442 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4443 | htx = htx_from_buf(&req->buf); |
| 4444 | count = co_data(req); |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 4445 | blk = htx_get_first_blk(htx); |
Christopher Faulet | cc26b13 | 2018-12-18 21:20:57 +0100 | [diff] [blame] | 4446 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4447 | while (count && !stop && blk) { |
| 4448 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4449 | uint32_t sz = htx_get_blksz(blk); |
| 4450 | struct ist v; |
| 4451 | uint32_t vlen; |
| 4452 | char *nl; |
| 4453 | |
| 4454 | vlen = sz; |
| 4455 | if (vlen > count) { |
| 4456 | if (type != HTX_BLK_DATA) |
| 4457 | break; |
| 4458 | vlen = count; |
| 4459 | } |
| 4460 | |
| 4461 | switch (type) { |
| 4462 | case HTX_BLK_UNUSED: |
| 4463 | break; |
| 4464 | |
| 4465 | case HTX_BLK_DATA: |
| 4466 | v = htx_get_blk_value(htx, blk); |
| 4467 | v.len = vlen; |
| 4468 | nl = istchr(v, '\n'); |
| 4469 | if (nl != NULL) { |
| 4470 | stop = 1; |
| 4471 | vlen = nl - v.ptr + 1; |
| 4472 | } |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4473 | luaL_addlstring(&luactx->b, v.ptr, vlen); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4474 | break; |
| 4475 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4476 | case HTX_BLK_TLR: |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 4477 | case HTX_BLK_EOT: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4478 | stop = 1; |
| 4479 | break; |
| 4480 | |
| 4481 | default: |
| 4482 | break; |
| 4483 | } |
| 4484 | |
| 4485 | co_set_data(req, co_data(req) - vlen); |
| 4486 | count -= vlen; |
| 4487 | if (sz == vlen) |
| 4488 | blk = htx_remove_blk(htx, blk); |
| 4489 | else { |
| 4490 | htx_cut_data_blk(htx, blk, vlen); |
| 4491 | break; |
| 4492 | } |
| 4493 | } |
| 4494 | |
Christopher Faulet | eccb31c | 2021-04-02 14:24:56 +0200 | [diff] [blame] | 4495 | /* The message was fully consumed and no more data are expected |
| 4496 | * (EOM flag set). |
| 4497 | */ |
Christopher Faulet | a5a25b1 | 2022-08-29 15:37:16 +0200 | [diff] [blame] | 4498 | if (htx_is_empty(htx) && (req->flags & CF_EOI)) |
Christopher Faulet | eccb31c | 2021-04-02 14:24:56 +0200 | [diff] [blame] | 4499 | stop = 1; |
| 4500 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4501 | htx_to_buf(htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4502 | if (!stop) { |
| 4503 | si_cant_get(si); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4504 | 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] | 4505 | } |
| 4506 | |
| 4507 | /* return the result. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4508 | luaL_pushresult(&luactx->b); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4509 | return 1; |
| 4510 | } |
| 4511 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4512 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4513 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4514 | __LJMP static int hlua_applet_http_getline(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4515 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4516 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4517 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4518 | /* Initialise the string catenation. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4519 | luaL_buffinit(L, &luactx->b); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4520 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4521 | return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4522 | } |
| 4523 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4524 | /* If expected data not yet available, it returns a yield. This function |
| 4525 | * consumes the data in the buffer. It returns a string containing the |
| 4526 | * data. This string can be empty. |
| 4527 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4528 | __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] | 4529 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4530 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4531 | struct stream_interface *si = luactx->appctx->owner; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4532 | struct channel *req = si_oc(si); |
| 4533 | struct htx *htx; |
| 4534 | struct htx_blk *blk; |
| 4535 | size_t count; |
| 4536 | int len; |
| 4537 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4538 | htx = htx_from_buf(&req->buf); |
| 4539 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4540 | count = co_data(req); |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 4541 | blk = htx_get_head_blk(htx); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4542 | while (count && len && blk) { |
| 4543 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4544 | uint32_t sz = htx_get_blksz(blk); |
| 4545 | struct ist v; |
| 4546 | uint32_t vlen; |
| 4547 | |
| 4548 | vlen = sz; |
| 4549 | if (len > 0 && vlen > len) |
| 4550 | vlen = len; |
| 4551 | if (vlen > count) { |
| 4552 | if (type != HTX_BLK_DATA) |
| 4553 | break; |
| 4554 | vlen = count; |
| 4555 | } |
| 4556 | |
| 4557 | switch (type) { |
| 4558 | case HTX_BLK_UNUSED: |
| 4559 | break; |
| 4560 | |
| 4561 | case HTX_BLK_DATA: |
| 4562 | v = htx_get_blk_value(htx, blk); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4563 | luaL_addlstring(&luactx->b, v.ptr, vlen); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4564 | break; |
| 4565 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4566 | case HTX_BLK_TLR: |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 4567 | case HTX_BLK_EOT: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4568 | len = 0; |
| 4569 | break; |
| 4570 | |
| 4571 | default: |
| 4572 | break; |
| 4573 | } |
| 4574 | |
| 4575 | co_set_data(req, co_data(req) - vlen); |
| 4576 | count -= vlen; |
| 4577 | if (len > 0) |
| 4578 | len -= vlen; |
| 4579 | if (sz == vlen) |
| 4580 | blk = htx_remove_blk(htx, blk); |
| 4581 | else { |
| 4582 | htx_cut_data_blk(htx, blk, vlen); |
| 4583 | break; |
| 4584 | } |
| 4585 | } |
| 4586 | |
Christopher Faulet | eccb31c | 2021-04-02 14:24:56 +0200 | [diff] [blame] | 4587 | /* The message was fully consumed and no more data are expected |
| 4588 | * (EOM flag set). |
| 4589 | */ |
Christopher Faulet | a5a25b1 | 2022-08-29 15:37:16 +0200 | [diff] [blame] | 4590 | if (htx_is_empty(htx) && (req->flags & CF_EOI)) |
Christopher Faulet | eccb31c | 2021-04-02 14:24:56 +0200 | [diff] [blame] | 4591 | len = 0; |
| 4592 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4593 | htx_to_buf(htx, &req->buf); |
| 4594 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4595 | /* If we are no other data available, yield waiting for new data. */ |
| 4596 | if (len) { |
| 4597 | if (len > 0) { |
| 4598 | lua_pushinteger(L, len); |
| 4599 | lua_replace(L, 2); |
| 4600 | } |
| 4601 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4602 | 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] | 4603 | } |
| 4604 | |
| 4605 | /* return the result. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4606 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4607 | return 1; |
| 4608 | } |
| 4609 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4610 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4611 | __LJMP static int hlua_applet_http_recv(lua_State *L) |
| 4612 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4613 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4614 | int len = -1; |
| 4615 | |
| 4616 | /* Check arguments. */ |
| 4617 | if (lua_gettop(L) > 2) |
| 4618 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 4619 | if (lua_gettop(L) >= 2) { |
| 4620 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4621 | lua_pop(L, 1); |
| 4622 | } |
| 4623 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4624 | lua_pushinteger(L, len); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4625 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4626 | /* Initialise the string catenation. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4627 | luaL_buffinit(L, &luactx->b); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4628 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4629 | return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4630 | } |
| 4631 | |
| 4632 | /* Append data in the output side of the buffer. This data is immediately |
| 4633 | * sent. The function returns the amount of data written. If the buffer |
| 4634 | * cannot contain the data, the function yields. The function returns -1 |
| 4635 | * if the channel is closed. |
| 4636 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4637 | __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] | 4638 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4639 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4640 | struct stream_interface *si = luactx->appctx->owner; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4641 | struct channel *res = si_ic(si); |
| 4642 | struct htx *htx = htx_from_buf(&res->buf); |
| 4643 | const char *data; |
| 4644 | size_t len; |
| 4645 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 4646 | int max; |
| 4647 | |
Christopher Faulet | 9060fc0 | 2019-07-03 11:39:30 +0200 | [diff] [blame] | 4648 | max = htx_get_max_blksz(htx, channel_htx_recv_max(res, htx)); |
Christopher Faulet | 4b0e9b2 | 2019-01-09 12:16:58 +0100 | [diff] [blame] | 4649 | if (!max) |
| 4650 | goto snd_yield; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4651 | |
| 4652 | data = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4653 | |
| 4654 | /* Get the max amount of data which can write as input in the channel. */ |
| 4655 | if (max > (len - l)) |
| 4656 | max = len - l; |
| 4657 | |
| 4658 | /* Copy data. */ |
Willy Tarreau | 0a7ef02 | 2019-05-28 10:30:11 +0200 | [diff] [blame] | 4659 | max = htx_add_data(htx, ist2(data + l, max)); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 4660 | channel_add_input(res, max); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4661 | |
| 4662 | /* update counters. */ |
| 4663 | l += max; |
| 4664 | lua_pop(L, 1); |
| 4665 | lua_pushinteger(L, l); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4666 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4667 | /* If some data is not send, declares the situation to the |
| 4668 | * applet, and returns a yield. |
| 4669 | */ |
| 4670 | if (l < len) { |
Christopher Faulet | 4b0e9b2 | 2019-01-09 12:16:58 +0100 | [diff] [blame] | 4671 | snd_yield: |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4672 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4673 | si_rx_room_blk(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4674 | 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] | 4675 | } |
| 4676 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4677 | htx_to_buf(htx, &res->buf); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4678 | return 1; |
| 4679 | } |
| 4680 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4681 | /* Just a wrapper of "hlua_applet_send_yield". This wrapper permits |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4682 | * yield the LUA process, and resume it without checking the |
| 4683 | * input arguments. |
| 4684 | */ |
| 4685 | __LJMP static int hlua_applet_http_send(lua_State *L) |
| 4686 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4687 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4688 | |
| 4689 | /* We want to send some data. Headers must be sent. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4690 | if (!(luactx->appctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4691 | hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data."); |
| 4692 | WILL_LJMP(lua_error(L)); |
| 4693 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4694 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4695 | /* This integer is used for followinf the amount of data sent. */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4696 | lua_pushinteger(L, 0); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4697 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4698 | return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4699 | } |
| 4700 | |
| 4701 | __LJMP static int hlua_applet_http_addheader(lua_State *L) |
| 4702 | { |
| 4703 | const char *name; |
| 4704 | int ret; |
| 4705 | |
| 4706 | MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4707 | name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 4708 | MAY_LJMP(luaL_checkstring(L, 3)); |
| 4709 | |
| 4710 | /* Push in the stack the "response" entry. */ |
| 4711 | ret = lua_getfield(L, 1, "response"); |
| 4712 | if (ret != LUA_TTABLE) { |
| 4713 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] " |
| 4714 | "is expected as an array. %s found", lua_typename(L, ret)); |
| 4715 | WILL_LJMP(lua_error(L)); |
| 4716 | } |
| 4717 | |
| 4718 | /* check if the header is already registered if it is not |
| 4719 | * the case, register it. |
| 4720 | */ |
| 4721 | ret = lua_getfield(L, -1, name); |
| 4722 | if (ret == LUA_TNIL) { |
| 4723 | |
| 4724 | /* Entry not found. */ |
| 4725 | lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */ |
| 4726 | |
| 4727 | /* Insert the new header name in the array in the top of the stack. |
| 4728 | * It left the new array in the top of the stack. |
| 4729 | */ |
| 4730 | lua_newtable(L); |
| 4731 | lua_pushvalue(L, 2); |
| 4732 | lua_pushvalue(L, -2); |
| 4733 | lua_settable(L, -4); |
| 4734 | |
| 4735 | } else if (ret != LUA_TTABLE) { |
| 4736 | |
| 4737 | /* corruption error. */ |
| 4738 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] " |
| 4739 | "is expected as an array. %s found", name, lua_typename(L, ret)); |
| 4740 | WILL_LJMP(lua_error(L)); |
| 4741 | } |
| 4742 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 4743 | /* Now the top of thestack is an array of values. We push |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4744 | * the header value as new entry. |
| 4745 | */ |
| 4746 | lua_pushvalue(L, 3); |
| 4747 | ret = lua_rawlen(L, -2); |
| 4748 | lua_rawseti(L, -2, ret + 1); |
| 4749 | lua_pushboolean(L, 1); |
| 4750 | return 1; |
| 4751 | } |
| 4752 | |
| 4753 | __LJMP static int hlua_applet_http_status(lua_State *L) |
| 4754 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4755 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4756 | int status = MAY_LJMP(luaL_checkinteger(L, 2)); |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4757 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4758 | |
| 4759 | if (status < 100 || status > 599) { |
| 4760 | lua_pushboolean(L, 0); |
| 4761 | return 1; |
| 4762 | } |
| 4763 | |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4764 | luactx->appctx->ctx.hlua_apphttp.status = status; |
| 4765 | luactx->appctx->ctx.hlua_apphttp.reason = reason; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4766 | lua_pushboolean(L, 1); |
| 4767 | return 1; |
| 4768 | } |
| 4769 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4770 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4771 | __LJMP static int hlua_applet_http_send_response(lua_State *L) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4772 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4773 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4774 | struct stream_interface *si = luactx->appctx->owner; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4775 | struct channel *res = si_ic(si); |
| 4776 | struct htx *htx; |
| 4777 | struct htx_sl *sl; |
| 4778 | struct h1m h1m; |
| 4779 | const char *status, *reason; |
| 4780 | const char *name, *value; |
| 4781 | size_t nlen, vlen; |
| 4782 | unsigned int flags; |
| 4783 | |
| 4784 | /* Send the message at once. */ |
| 4785 | htx = htx_from_buf(&res->buf); |
| 4786 | h1m_init_res(&h1m); |
| 4787 | |
| 4788 | /* Use the same http version than the request. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4789 | status = ultoa_r(luactx->appctx->ctx.hlua_apphttp.status, trash.area, trash.size); |
| 4790 | reason = luactx->appctx->ctx.hlua_apphttp.reason; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4791 | if (reason == NULL) |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4792 | reason = http_get_reason(luactx->appctx->ctx.hlua_apphttp.status); |
| 4793 | if (luactx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4794 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11); |
| 4795 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist(status), ist(reason)); |
| 4796 | } |
| 4797 | else { |
| 4798 | flags = HTX_SL_F_IS_RESP; |
| 4799 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), ist(status), ist(reason)); |
| 4800 | } |
| 4801 | if (!sl) { |
| 4802 | hlua_pusherror(L, "Lua applet http '%s': Failed to create response.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4803 | luactx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4804 | WILL_LJMP(lua_error(L)); |
| 4805 | } |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4806 | sl->info.res.status = luactx->appctx->ctx.hlua_apphttp.status; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4807 | |
| 4808 | /* Get the array associated to the field "response" in the object AppletHTTP. */ |
| 4809 | lua_pushvalue(L, 0); |
| 4810 | if (lua_getfield(L, 1, "response") != LUA_TTABLE) { |
| 4811 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\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 | } |
| 4815 | |
| 4816 | /* Browse the list of headers. */ |
| 4817 | lua_pushnil(L); |
| 4818 | while(lua_next(L, -2) != 0) { |
| 4819 | /* We expect a string as -2. */ |
| 4820 | if (lua_type(L, -2) != LUA_TSTRING) { |
| 4821 | 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] | 4822 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4823 | lua_typename(L, lua_type(L, -2))); |
| 4824 | WILL_LJMP(lua_error(L)); |
| 4825 | } |
| 4826 | name = lua_tolstring(L, -2, &nlen); |
| 4827 | |
| 4828 | /* We expect an array as -1. */ |
| 4829 | if (lua_type(L, -1) != LUA_TTABLE) { |
| 4830 | 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] | 4831 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4832 | name, |
| 4833 | lua_typename(L, lua_type(L, -1))); |
| 4834 | WILL_LJMP(lua_error(L)); |
| 4835 | } |
| 4836 | |
| 4837 | /* Browse the table who is on the top of the stack. */ |
| 4838 | lua_pushnil(L); |
| 4839 | while(lua_next(L, -2) != 0) { |
| 4840 | int id; |
| 4841 | |
| 4842 | /* We expect a number as -2. */ |
| 4843 | if (lua_type(L, -2) != LUA_TNUMBER) { |
| 4844 | 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] | 4845 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4846 | name, |
| 4847 | lua_typename(L, lua_type(L, -2))); |
| 4848 | WILL_LJMP(lua_error(L)); |
| 4849 | } |
| 4850 | id = lua_tointeger(L, -2); |
| 4851 | |
| 4852 | /* We expect a string as -2. */ |
| 4853 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 4854 | 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] | 4855 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4856 | name, id, |
| 4857 | lua_typename(L, lua_type(L, -1))); |
| 4858 | WILL_LJMP(lua_error(L)); |
| 4859 | } |
| 4860 | value = lua_tolstring(L, -1, &vlen); |
| 4861 | |
| 4862 | /* Simple Protocol checks. */ |
| 4863 | if (isteqi(ist2(name, nlen), ist("transfer-encoding"))) |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 4864 | h1_parse_xfer_enc_header(&h1m, ist2(value, vlen)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4865 | else if (isteqi(ist2(name, nlen), ist("content-length"))) { |
| 4866 | struct ist v = ist2(value, vlen); |
| 4867 | int ret; |
| 4868 | |
| 4869 | ret = h1_parse_cont_len_header(&h1m, &v); |
| 4870 | if (ret < 0) { |
| 4871 | hlua_pusherror(L, "Lua applet http '%s': Invalid '%s' header.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4872 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4873 | name); |
| 4874 | WILL_LJMP(lua_error(L)); |
| 4875 | } |
| 4876 | else if (ret == 0) |
| 4877 | goto next; /* Skip it */ |
| 4878 | } |
| 4879 | |
| 4880 | /* Add a new header */ |
| 4881 | if (!htx_add_header(htx, ist2(name, nlen), ist2(value, vlen))) { |
| 4882 | 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] | 4883 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4884 | name); |
| 4885 | WILL_LJMP(lua_error(L)); |
| 4886 | } |
| 4887 | next: |
| 4888 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 4889 | lua_pop(L, 1); |
| 4890 | } |
| 4891 | |
| 4892 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 4893 | lua_pop(L, 1); |
| 4894 | } |
| 4895 | |
| 4896 | if (h1m.flags & H1_MF_CHNK) |
| 4897 | h1m.flags &= ~H1_MF_CLEN; |
| 4898 | if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK)) |
| 4899 | h1m.flags |= H1_MF_XFER_LEN; |
| 4900 | |
| 4901 | /* Uset HTX start-line flags */ |
| 4902 | if (h1m.flags & H1_MF_XFER_ENC) |
| 4903 | flags |= HTX_SL_F_XFER_ENC; |
| 4904 | if (h1m.flags & H1_MF_XFER_LEN) { |
| 4905 | flags |= HTX_SL_F_XFER_LEN; |
| 4906 | if (h1m.flags & H1_MF_CHNK) |
| 4907 | flags |= HTX_SL_F_CHNK; |
| 4908 | else if (h1m.flags & H1_MF_CLEN) |
| 4909 | flags |= HTX_SL_F_CLEN; |
| 4910 | if (h1m.body_len == 0) |
| 4911 | flags |= HTX_SL_F_BODYLESS; |
| 4912 | } |
| 4913 | sl->flags |= flags; |
| 4914 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 4915 | /* 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] | 4916 | * and the status code implies the presence of a message body, we must |
| 4917 | * announce a transfer encoding chunked. This is required by haproxy |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4918 | * for the keepalive compliance. If the applet announces a transfer-encoding |
| 4919 | * chunked itself, don't do anything. |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4920 | */ |
| 4921 | 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] | 4922 | luactx->appctx->ctx.hlua_apphttp.status >= 200 && |
| 4923 | luactx->appctx->ctx.hlua_apphttp.status != 204 && |
| 4924 | luactx->appctx->ctx.hlua_apphttp.status != 304) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4925 | /* Add a new header */ |
| 4926 | sl->flags |= (HTX_SL_F_XFER_ENC|H1_MF_CHNK|H1_MF_XFER_LEN); |
| 4927 | if (!htx_add_header(htx, ist("transfer-encoding"), ist("chunked"))) { |
| 4928 | 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] | 4929 | luactx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4930 | WILL_LJMP(lua_error(L)); |
| 4931 | } |
| 4932 | } |
| 4933 | |
| 4934 | /* Finalize headers. */ |
| 4935 | if (!htx_add_endof(htx, HTX_BLK_EOH)) { |
| 4936 | hlua_pusherror(L, "Lua applet http '%s': Failed create the response.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4937 | luactx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4938 | WILL_LJMP(lua_error(L)); |
| 4939 | } |
| 4940 | |
| 4941 | if (htx_used_space(htx) > b_size(&res->buf) - global.tune.maxrewrite) { |
| 4942 | b_reset(&res->buf); |
| 4943 | hlua_pusherror(L, "Lua: 'start_response': response header block too big"); |
| 4944 | WILL_LJMP(lua_error(L)); |
| 4945 | } |
| 4946 | |
| 4947 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 4948 | channel_add_input(res, htx->data); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4949 | |
| 4950 | /* Headers sent, set the flag. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4951 | luactx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4952 | return 0; |
| 4953 | |
| 4954 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4955 | /* We will build the status line and the headers of the HTTP response. |
| 4956 | * We will try send at once if its not possible, we give back the hand |
| 4957 | * waiting for more room. |
| 4958 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4959 | __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] | 4960 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4961 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4962 | struct stream_interface *si = luactx->appctx->owner; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4963 | struct channel *res = si_ic(si); |
| 4964 | |
| 4965 | if (co_data(res)) { |
| 4966 | si_rx_room_blk(si); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4967 | 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] | 4968 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4969 | return MAY_LJMP(hlua_applet_http_send_response(L)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4970 | } |
| 4971 | |
| 4972 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4973 | __LJMP static int hlua_applet_http_start_response(lua_State *L) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4974 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4975 | return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4976 | } |
| 4977 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4978 | /* |
| 4979 | * |
| 4980 | * |
| 4981 | * Class HTTP |
| 4982 | * |
| 4983 | * |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4984 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4985 | |
| 4986 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 4987 | * a class stream, otherwise it throws an error. |
| 4988 | */ |
| 4989 | __LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4990 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4991 | return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref)); |
| 4992 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4993 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4994 | /* This function creates and push in the stack a HTTP object |
| 4995 | * according with a current TXN. |
| 4996 | */ |
| 4997 | static int hlua_http_new(lua_State *L, struct hlua_txn *txn) |
| 4998 | { |
| 4999 | struct hlua_txn *htxn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5000 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5001 | /* Check stack size. */ |
| 5002 | if (!lua_checkstack(L, 3)) |
| 5003 | return 0; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5004 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5005 | /* Create the object: obj[0] = userdata. |
| 5006 | * Note that the base of the Converters object is the |
| 5007 | * same than the TXN object. |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5008 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5009 | lua_newtable(L); |
| 5010 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
| 5011 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5012 | |
| 5013 | htxn->s = txn->s; |
| 5014 | htxn->p = txn->p; |
Christopher Faulet | 256b69a | 2019-05-23 11:14:21 +0200 | [diff] [blame] | 5015 | htxn->dir = txn->dir; |
| 5016 | htxn->flags = txn->flags; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5017 | |
| 5018 | /* Pop a class stream metatable and affect it to the table. */ |
| 5019 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref); |
| 5020 | lua_setmetatable(L, -2); |
| 5021 | |
| 5022 | return 1; |
| 5023 | } |
| 5024 | |
| 5025 | /* This function creates ans returns an array of HTTP headers. |
| 5026 | * This function does not fails. It is used as wrapper with the |
| 5027 | * 2 following functions. |
| 5028 | */ |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 5029 | __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] | 5030 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5031 | struct htx *htx; |
| 5032 | int32_t pos; |
| 5033 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5034 | /* Create the table. */ |
| 5035 | lua_newtable(L); |
| 5036 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5037 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5038 | htx = htxbuf(&msg->chn->buf); |
| 5039 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 5040 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 5041 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 5042 | struct ist n, v; |
| 5043 | int len; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5044 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5045 | if (type == HTX_BLK_HDR) { |
| 5046 | n = htx_get_blk_name(htx,blk); |
| 5047 | v = htx_get_blk_value(htx, blk); |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5048 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5049 | else if (type == HTX_BLK_EOH) |
| 5050 | break; |
| 5051 | else |
| 5052 | continue; |
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 | /* Check for existing entry: |
| 5055 | * assume that the table is on the top of the stack, and |
| 5056 | * push the key in the stack, the function lua_gettable() |
| 5057 | * perform the lookup. |
| 5058 | */ |
| 5059 | lua_pushlstring(L, n.ptr, n.len); |
| 5060 | lua_gettable(L, -2); |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5061 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5062 | switch (lua_type(L, -1)) { |
| 5063 | case LUA_TNIL: |
| 5064 | /* Table not found, create it. */ |
| 5065 | lua_pop(L, 1); /* remove the nil value. */ |
| 5066 | lua_pushlstring(L, n.ptr, n.len); /* push the header name as key. */ |
| 5067 | lua_newtable(L); /* create and push empty table. */ |
| 5068 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 5069 | lua_rawseti(L, -2, 0); /* index header value (pop it). */ |
| 5070 | 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] | 5071 | break; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5072 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5073 | case LUA_TTABLE: |
| 5074 | /* Entry found: push the value in the table. */ |
| 5075 | len = lua_rawlen(L, -1); |
| 5076 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 5077 | lua_rawseti(L, -2, len+1); /* index header value (pop it). */ |
| 5078 | lua_pop(L, 1); /* remove the table (it is stored in the main table). */ |
| 5079 | break; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5080 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5081 | default: |
| 5082 | /* Other cases are errors. */ |
| 5083 | hlua_pusherror(L, "internal error during the parsing of headers."); |
| 5084 | WILL_LJMP(lua_error(L)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5085 | } |
| 5086 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5087 | return 1; |
| 5088 | } |
| 5089 | |
| 5090 | __LJMP static int hlua_http_req_get_headers(lua_State *L) |
| 5091 | { |
| 5092 | struct hlua_txn *htxn; |
| 5093 | |
| 5094 | MAY_LJMP(check_args(L, 1, "req_get_headers")); |
| 5095 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5096 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5097 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5098 | WILL_LJMP(lua_error(L)); |
| 5099 | |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 5100 | return hlua_http_get_headers(L, &htxn->s->txn->req); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5101 | } |
| 5102 | |
| 5103 | __LJMP static int hlua_http_res_get_headers(lua_State *L) |
| 5104 | { |
| 5105 | struct hlua_txn *htxn; |
| 5106 | |
| 5107 | MAY_LJMP(check_args(L, 1, "res_get_headers")); |
| 5108 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5109 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5110 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5111 | WILL_LJMP(lua_error(L)); |
| 5112 | |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 5113 | return hlua_http_get_headers(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5114 | } |
| 5115 | |
| 5116 | /* This function replace full header, or just a value in |
| 5117 | * the request or in the response. It is a wrapper fir the |
| 5118 | * 4 following functions. |
| 5119 | */ |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5120 | __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] | 5121 | { |
| 5122 | size_t name_len; |
| 5123 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5124 | const char *reg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 5125 | const char *value = MAY_LJMP(luaL_checkstring(L, 4)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5126 | struct htx *htx; |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 5127 | struct my_regex *re; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5128 | |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 5129 | if (!(re = regex_comp(reg, 1, 1, NULL))) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5130 | WILL_LJMP(luaL_argerror(L, 3, "invalid regex")); |
| 5131 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5132 | htx = htxbuf(&msg->chn->buf); |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5133 | 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] | 5134 | regex_free(re); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5135 | return 0; |
| 5136 | } |
| 5137 | |
| 5138 | __LJMP static int hlua_http_req_rep_hdr(lua_State *L) |
| 5139 | { |
| 5140 | struct hlua_txn *htxn; |
| 5141 | |
| 5142 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 5143 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5144 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5145 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5146 | WILL_LJMP(lua_error(L)); |
| 5147 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5148 | 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] | 5149 | } |
| 5150 | |
| 5151 | __LJMP static int hlua_http_res_rep_hdr(lua_State *L) |
| 5152 | { |
| 5153 | struct hlua_txn *htxn; |
| 5154 | |
| 5155 | MAY_LJMP(check_args(L, 4, "res_rep_hdr")); |
| 5156 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5157 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5158 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5159 | WILL_LJMP(lua_error(L)); |
| 5160 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5161 | 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] | 5162 | } |
| 5163 | |
| 5164 | __LJMP static int hlua_http_req_rep_val(lua_State *L) |
| 5165 | { |
| 5166 | struct hlua_txn *htxn; |
| 5167 | |
| 5168 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 5169 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5170 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5171 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5172 | WILL_LJMP(lua_error(L)); |
| 5173 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5174 | 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] | 5175 | } |
| 5176 | |
| 5177 | __LJMP static int hlua_http_res_rep_val(lua_State *L) |
| 5178 | { |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5179 | struct hlua_txn *htxn; |
| 5180 | |
| 5181 | MAY_LJMP(check_args(L, 4, "res_rep_val")); |
| 5182 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5183 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5184 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5185 | WILL_LJMP(lua_error(L)); |
| 5186 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5187 | 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] | 5188 | } |
| 5189 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5190 | /* This function deletes all the occurrences of an header. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5191 | * It is a wrapper for the 2 following functions. |
| 5192 | */ |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5193 | __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] | 5194 | { |
| 5195 | size_t len; |
| 5196 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5197 | struct htx *htx = htxbuf(&msg->chn->buf); |
| 5198 | struct http_hdr_ctx ctx; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5199 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5200 | ctx.blk = NULL; |
| 5201 | while (http_find_header(htx, ist2(name, len), &ctx, 1)) |
| 5202 | http_remove_header(htx, &ctx); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5203 | return 0; |
| 5204 | } |
| 5205 | |
| 5206 | __LJMP static int hlua_http_req_del_hdr(lua_State *L) |
| 5207 | { |
| 5208 | struct hlua_txn *htxn; |
| 5209 | |
| 5210 | MAY_LJMP(check_args(L, 2, "req_del_hdr")); |
| 5211 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5212 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5213 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5214 | WILL_LJMP(lua_error(L)); |
| 5215 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5216 | return hlua_http_del_hdr(L, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5217 | } |
| 5218 | |
| 5219 | __LJMP static int hlua_http_res_del_hdr(lua_State *L) |
| 5220 | { |
| 5221 | struct hlua_txn *htxn; |
| 5222 | |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5223 | MAY_LJMP(check_args(L, 2, "res_del_hdr")); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5224 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5225 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5226 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5227 | WILL_LJMP(lua_error(L)); |
| 5228 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5229 | return hlua_http_del_hdr(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5230 | } |
| 5231 | |
| 5232 | /* This function adds an header. It is a wrapper used by |
| 5233 | * the 2 following functions. |
| 5234 | */ |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5235 | __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] | 5236 | { |
| 5237 | size_t name_len; |
| 5238 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5239 | size_t value_len; |
| 5240 | const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5241 | struct htx *htx = htxbuf(&msg->chn->buf); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5242 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5243 | lua_pushboolean(L, http_add_header(htx, ist2(name, name_len), |
| 5244 | ist2(value, value_len))); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5245 | return 0; |
| 5246 | } |
| 5247 | |
| 5248 | __LJMP static int hlua_http_req_add_hdr(lua_State *L) |
| 5249 | { |
| 5250 | struct hlua_txn *htxn; |
| 5251 | |
| 5252 | MAY_LJMP(check_args(L, 3, "req_add_hdr")); |
| 5253 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5254 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5255 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5256 | WILL_LJMP(lua_error(L)); |
| 5257 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5258 | return hlua_http_add_hdr(L, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5259 | } |
| 5260 | |
| 5261 | __LJMP static int hlua_http_res_add_hdr(lua_State *L) |
| 5262 | { |
| 5263 | struct hlua_txn *htxn; |
| 5264 | |
| 5265 | MAY_LJMP(check_args(L, 3, "res_add_hdr")); |
| 5266 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5267 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5268 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5269 | WILL_LJMP(lua_error(L)); |
| 5270 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5271 | return hlua_http_add_hdr(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5272 | } |
| 5273 | |
| 5274 | static int hlua_http_req_set_hdr(lua_State *L) |
| 5275 | { |
| 5276 | struct hlua_txn *htxn; |
| 5277 | |
| 5278 | MAY_LJMP(check_args(L, 3, "req_set_hdr")); |
| 5279 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5280 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5281 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5282 | WILL_LJMP(lua_error(L)); |
| 5283 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5284 | hlua_http_del_hdr(L, &htxn->s->txn->req); |
| 5285 | return hlua_http_add_hdr(L, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5286 | } |
| 5287 | |
| 5288 | static int hlua_http_res_set_hdr(lua_State *L) |
| 5289 | { |
| 5290 | struct hlua_txn *htxn; |
| 5291 | |
| 5292 | MAY_LJMP(check_args(L, 3, "res_set_hdr")); |
| 5293 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5294 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5295 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5296 | WILL_LJMP(lua_error(L)); |
| 5297 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5298 | hlua_http_del_hdr(L, &htxn->s->txn->rsp); |
| 5299 | return hlua_http_add_hdr(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5300 | } |
| 5301 | |
| 5302 | /* This function set the method. */ |
| 5303 | static int hlua_http_req_set_meth(lua_State *L) |
| 5304 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5305 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5306 | size_t name_len; |
| 5307 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5308 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5309 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5310 | WILL_LJMP(lua_error(L)); |
| 5311 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5312 | 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] | 5313 | return 1; |
| 5314 | } |
| 5315 | |
| 5316 | /* This function set the method. */ |
| 5317 | static int hlua_http_req_set_path(lua_State *L) |
| 5318 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5319 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5320 | size_t name_len; |
| 5321 | 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] | 5322 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5323 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5324 | WILL_LJMP(lua_error(L)); |
| 5325 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5326 | 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] | 5327 | return 1; |
| 5328 | } |
| 5329 | |
| 5330 | /* This function set the query-string. */ |
| 5331 | static int hlua_http_req_set_query(lua_State *L) |
| 5332 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5333 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5334 | size_t name_len; |
| 5335 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5336 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5337 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5338 | WILL_LJMP(lua_error(L)); |
| 5339 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5340 | /* Check length. */ |
| 5341 | if (name_len > trash.size - 1) { |
| 5342 | lua_pushboolean(L, 0); |
| 5343 | return 1; |
| 5344 | } |
| 5345 | |
| 5346 | /* Add the mark question as prefix. */ |
| 5347 | chunk_reset(&trash); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5348 | trash.area[trash.data++] = '?'; |
| 5349 | memcpy(trash.area + trash.data, name, name_len); |
| 5350 | trash.data += name_len; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5351 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5352 | lua_pushboolean(L, |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5353 | 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] | 5354 | return 1; |
| 5355 | } |
| 5356 | |
| 5357 | /* This function set the uri. */ |
| 5358 | static int hlua_http_req_set_uri(lua_State *L) |
| 5359 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5360 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5361 | size_t name_len; |
| 5362 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5363 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5364 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5365 | WILL_LJMP(lua_error(L)); |
| 5366 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5367 | 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] | 5368 | return 1; |
| 5369 | } |
| 5370 | |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5371 | /* This function set the response code & optionally reason. */ |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5372 | static int hlua_http_res_set_status(lua_State *L) |
| 5373 | { |
| 5374 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5375 | unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2)); |
Christopher Faulet | 96bff76 | 2019-12-17 13:46:18 +0100 | [diff] [blame] | 5376 | const char *str = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
| 5377 | const struct ist reason = ist2(str, (str ? strlen(str) : 0)); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5378 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5379 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5380 | WILL_LJMP(lua_error(L)); |
| 5381 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5382 | http_res_set_status(code, reason, htxn->s); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5383 | return 0; |
| 5384 | } |
| 5385 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5386 | /* |
| 5387 | * |
| 5388 | * |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5389 | * Class TXN |
| 5390 | * |
| 5391 | * |
| 5392 | */ |
| 5393 | |
| 5394 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5395 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5396 | */ |
| 5397 | __LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud) |
| 5398 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 5399 | return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref)); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5400 | } |
| 5401 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5402 | __LJMP static int hlua_set_var(lua_State *L) |
| 5403 | { |
| 5404 | struct hlua_txn *htxn; |
| 5405 | const char *name; |
| 5406 | size_t len; |
| 5407 | struct sample smp; |
| 5408 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 5409 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 5410 | 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] | 5411 | |
| 5412 | /* It is useles to retrieve the stream, but this function |
| 5413 | * runs only in a stream context. |
| 5414 | */ |
| 5415 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5416 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5417 | |
| 5418 | /* Converts the third argument in a sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 5419 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5420 | hlua_lua2smp(L, 3, &smp); |
| 5421 | |
Aurelien DARRAGON | d39fb74 | 2023-05-17 16:06:11 +0200 | [diff] [blame] | 5422 | /* Store the sample in a variable. We don't need to dup the smp, vars API |
| 5423 | * already takes care of duplicating dynamic var data. |
| 5424 | */ |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 5425 | 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] | 5426 | |
| 5427 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 5428 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 5429 | else |
| 5430 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 5431 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 5432 | return 1; |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5433 | } |
| 5434 | |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 5435 | __LJMP static int hlua_unset_var(lua_State *L) |
| 5436 | { |
| 5437 | struct hlua_txn *htxn; |
| 5438 | const char *name; |
| 5439 | size_t len; |
| 5440 | struct sample smp; |
| 5441 | |
| 5442 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 5443 | |
| 5444 | /* It is useles to retrieve the stream, but this function |
| 5445 | * runs only in a stream context. |
| 5446 | */ |
| 5447 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5448 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5449 | |
| 5450 | /* Unset the variable. */ |
| 5451 | 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] | 5452 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 5453 | return 1; |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 5454 | } |
| 5455 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5456 | __LJMP static int hlua_get_var(lua_State *L) |
| 5457 | { |
| 5458 | struct hlua_txn *htxn; |
| 5459 | const char *name; |
| 5460 | size_t len; |
| 5461 | struct sample smp; |
| 5462 | |
| 5463 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 5464 | |
| 5465 | /* It is useles to retrieve the stream, but this function |
| 5466 | * runs only in a stream context. |
| 5467 | */ |
| 5468 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5469 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5470 | |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 5471 | 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] | 5472 | if (!vars_get_by_name(name, len, &smp)) { |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5473 | lua_pushnil(L); |
| 5474 | return 1; |
| 5475 | } |
| 5476 | |
| 5477 | return hlua_smp2lua(L, &smp); |
| 5478 | } |
| 5479 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5480 | __LJMP static int hlua_set_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5481 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5482 | struct hlua *hlua; |
| 5483 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5484 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 5485 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5486 | /* It is useles to retrieve the stream, but this function |
| 5487 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5488 | */ |
| 5489 | MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 5490 | |
| 5491 | /* Get hlua struct, or NULL if we execute from main lua state */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5492 | hlua = hlua_gethlua(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 5493 | if (!hlua) |
| 5494 | return 0; |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5495 | |
| 5496 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 5497 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5498 | |
| 5499 | /* Get and store new value. */ |
| 5500 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 5501 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 5502 | |
| 5503 | return 0; |
| 5504 | } |
| 5505 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5506 | __LJMP static int hlua_get_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5507 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5508 | struct hlua *hlua; |
| 5509 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5510 | MAY_LJMP(check_args(L, 1, "get_priv")); |
| 5511 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5512 | /* It is useles to retrieve the stream, but this function |
| 5513 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5514 | */ |
| 5515 | MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 5516 | |
| 5517 | /* Get hlua struct, or NULL if we execute from main lua state */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5518 | hlua = hlua_gethlua(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 5519 | if (!hlua) { |
| 5520 | lua_pushnil(L); |
| 5521 | return 1; |
| 5522 | } |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5523 | |
| 5524 | /* Push configuration index in the stack. */ |
| 5525 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 5526 | |
| 5527 | return 1; |
| 5528 | } |
| 5529 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5530 | /* Create stack entry containing a class TXN. This function |
| 5531 | * return 0 if the stack does not contains free slots, |
| 5532 | * otherwise it returns 1. |
| 5533 | */ |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5534 | 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] | 5535 | { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5536 | struct hlua_txn *htxn; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5537 | |
| 5538 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5539 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5540 | return 0; |
| 5541 | |
| 5542 | /* NOTE: The allocation never fails. The failure |
| 5543 | * throw an error, and the function never returns. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5544 | * if the throw is not available, the process is aborted. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5545 | */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5546 | /* Create the object: obj[0] = userdata. */ |
| 5547 | lua_newtable(L); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5548 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5549 | lua_rawseti(L, -2, 0); |
| 5550 | |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5551 | htxn->s = s; |
| 5552 | htxn->p = p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 5553 | htxn->dir = dir; |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5554 | htxn->flags = flags; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5555 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 5556 | /* Create the "f" field that contains a list of fetches. */ |
| 5557 | lua_pushstring(L, "f"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 5558 | if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5559 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5560 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5561 | |
| 5562 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 5563 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 5564 | 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] | 5565 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5566 | lua_rawset(L, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 5567 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5568 | /* Create the "c" field that contains a list of converters. */ |
| 5569 | lua_pushstring(L, "c"); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5570 | if (!hlua_converters_new(L, htxn, 0)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5571 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5572 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5573 | |
| 5574 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 5575 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 5576 | if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5577 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5578 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5579 | |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5580 | /* Create the "req" field that contains the request channel object. */ |
| 5581 | lua_pushstring(L, "req"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 5582 | if (!hlua_channel_new(L, &s->req)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5583 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5584 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5585 | |
| 5586 | /* Create the "res" field that contains the response channel object. */ |
| 5587 | lua_pushstring(L, "res"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 5588 | if (!hlua_channel_new(L, &s->res)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5589 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5590 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5591 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5592 | /* Creates the HTTP object is the current proxy allows http. */ |
| 5593 | lua_pushstring(L, "http"); |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 5594 | if (IS_HTX_STRM(s)) { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5595 | if (!hlua_http_new(L, htxn)) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5596 | return 0; |
| 5597 | } |
| 5598 | else |
| 5599 | lua_pushnil(L); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5600 | lua_rawset(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5601 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5602 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 5603 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref); |
| 5604 | lua_setmetatable(L, -2); |
| 5605 | |
| 5606 | return 1; |
| 5607 | } |
| 5608 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 5609 | __LJMP static int hlua_txn_deflog(lua_State *L) |
| 5610 | { |
| 5611 | const char *msg; |
| 5612 | struct hlua_txn *htxn; |
| 5613 | |
| 5614 | MAY_LJMP(check_args(L, 2, "deflog")); |
| 5615 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5616 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5617 | |
| 5618 | hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg); |
| 5619 | return 0; |
| 5620 | } |
| 5621 | |
| 5622 | __LJMP static int hlua_txn_log(lua_State *L) |
| 5623 | { |
| 5624 | int level; |
| 5625 | const char *msg; |
| 5626 | struct hlua_txn *htxn; |
| 5627 | |
| 5628 | MAY_LJMP(check_args(L, 3, "log")); |
| 5629 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5630 | level = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5631 | msg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 5632 | |
| 5633 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 5634 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 5635 | |
| 5636 | hlua_sendlog(htxn->s->be, level, msg); |
| 5637 | return 0; |
| 5638 | } |
| 5639 | |
| 5640 | __LJMP static int hlua_txn_log_debug(lua_State *L) |
| 5641 | { |
| 5642 | const char *msg; |
| 5643 | struct hlua_txn *htxn; |
| 5644 | |
| 5645 | MAY_LJMP(check_args(L, 2, "Debug")); |
| 5646 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5647 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5648 | hlua_sendlog(htxn->s->be, LOG_DEBUG, msg); |
| 5649 | return 0; |
| 5650 | } |
| 5651 | |
| 5652 | __LJMP static int hlua_txn_log_info(lua_State *L) |
| 5653 | { |
| 5654 | const char *msg; |
| 5655 | struct hlua_txn *htxn; |
| 5656 | |
| 5657 | MAY_LJMP(check_args(L, 2, "Info")); |
| 5658 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5659 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5660 | hlua_sendlog(htxn->s->be, LOG_INFO, msg); |
| 5661 | return 0; |
| 5662 | } |
| 5663 | |
| 5664 | __LJMP static int hlua_txn_log_warning(lua_State *L) |
| 5665 | { |
| 5666 | const char *msg; |
| 5667 | struct hlua_txn *htxn; |
| 5668 | |
| 5669 | MAY_LJMP(check_args(L, 2, "Warning")); |
| 5670 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5671 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5672 | hlua_sendlog(htxn->s->be, LOG_WARNING, msg); |
| 5673 | return 0; |
| 5674 | } |
| 5675 | |
| 5676 | __LJMP static int hlua_txn_log_alert(lua_State *L) |
| 5677 | { |
| 5678 | const char *msg; |
| 5679 | struct hlua_txn *htxn; |
| 5680 | |
| 5681 | MAY_LJMP(check_args(L, 2, "Alert")); |
| 5682 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5683 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5684 | hlua_sendlog(htxn->s->be, LOG_ALERT, msg); |
| 5685 | return 0; |
| 5686 | } |
| 5687 | |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5688 | __LJMP static int hlua_txn_set_loglevel(lua_State *L) |
| 5689 | { |
| 5690 | struct hlua_txn *htxn; |
| 5691 | int ll; |
| 5692 | |
| 5693 | MAY_LJMP(check_args(L, 2, "set_loglevel")); |
| 5694 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5695 | ll = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5696 | |
| 5697 | if (ll < 0 || ll > 7) |
| 5698 | WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7")); |
| 5699 | |
| 5700 | htxn->s->logs.level = ll; |
| 5701 | return 0; |
| 5702 | } |
| 5703 | |
| 5704 | __LJMP static int hlua_txn_set_tos(lua_State *L) |
| 5705 | { |
| 5706 | struct hlua_txn *htxn; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5707 | int tos; |
| 5708 | |
| 5709 | MAY_LJMP(check_args(L, 2, "set_tos")); |
| 5710 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5711 | tos = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5712 | |
Willy Tarreau | 1a18b54 | 2018-12-11 16:37:42 +0100 | [diff] [blame] | 5713 | conn_set_tos(objt_conn(htxn->s->sess->origin), tos); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5714 | return 0; |
| 5715 | } |
| 5716 | |
| 5717 | __LJMP static int hlua_txn_set_mark(lua_State *L) |
| 5718 | { |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5719 | struct hlua_txn *htxn; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5720 | int mark; |
| 5721 | |
| 5722 | MAY_LJMP(check_args(L, 2, "set_mark")); |
| 5723 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5724 | mark = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5725 | |
Lukas Tribus | 579e3e3 | 2019-08-11 18:03:45 +0200 | [diff] [blame] | 5726 | conn_set_mark(objt_conn(htxn->s->sess->origin), mark); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5727 | return 0; |
| 5728 | } |
| 5729 | |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 5730 | __LJMP static int hlua_txn_set_priority_class(lua_State *L) |
| 5731 | { |
| 5732 | struct hlua_txn *htxn; |
| 5733 | |
| 5734 | MAY_LJMP(check_args(L, 2, "set_priority_class")); |
| 5735 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5736 | htxn->s->priority_class = queue_limit_class(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 5737 | return 0; |
| 5738 | } |
| 5739 | |
| 5740 | __LJMP static int hlua_txn_set_priority_offset(lua_State *L) |
| 5741 | { |
| 5742 | struct hlua_txn *htxn; |
| 5743 | |
| 5744 | MAY_LJMP(check_args(L, 2, "set_priority_offset")); |
| 5745 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5746 | htxn->s->priority_offset = queue_limit_offset(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 5747 | return 0; |
| 5748 | } |
| 5749 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5750 | /* 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] | 5751 | * 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] | 5752 | * message and terminate the transaction. It returns 1 on success and 0 on |
| 5753 | * error. The Reply must be on top of the stack. |
| 5754 | */ |
| 5755 | __LJMP static int hlua_txn_forward_reply(lua_State *L, struct stream *s) |
| 5756 | { |
| 5757 | struct htx *htx; |
| 5758 | struct htx_sl *sl; |
| 5759 | struct h1m h1m; |
| 5760 | const char *status, *reason, *body; |
| 5761 | size_t status_len, reason_len, body_len; |
| 5762 | int ret, code, flags; |
| 5763 | |
| 5764 | code = 200; |
| 5765 | status = "200"; |
| 5766 | status_len = 3; |
| 5767 | ret = lua_getfield(L, -1, "status"); |
| 5768 | if (ret == LUA_TNUMBER) { |
| 5769 | code = lua_tointeger(L, -1); |
| 5770 | status = lua_tolstring(L, -1, &status_len); |
| 5771 | } |
| 5772 | lua_pop(L, 1); |
| 5773 | |
| 5774 | reason = http_get_reason(code); |
| 5775 | reason_len = strlen(reason); |
| 5776 | ret = lua_getfield(L, -1, "reason"); |
| 5777 | if (ret == LUA_TSTRING) |
| 5778 | reason = lua_tolstring(L, -1, &reason_len); |
| 5779 | lua_pop(L, 1); |
| 5780 | |
| 5781 | body = NULL; |
| 5782 | body_len = 0; |
| 5783 | ret = lua_getfield(L, -1, "body"); |
| 5784 | if (ret == LUA_TSTRING) |
| 5785 | body = lua_tolstring(L, -1, &body_len); |
| 5786 | lua_pop(L, 1); |
| 5787 | |
| 5788 | /* Prepare the response before inserting the headers */ |
| 5789 | h1m_init_res(&h1m); |
| 5790 | htx = htx_from_buf(&s->res.buf); |
| 5791 | channel_htx_truncate(&s->res, htx); |
| 5792 | if (s->txn->req.flags & HTTP_MSGF_VER_11) { |
| 5793 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11); |
| 5794 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), |
| 5795 | ist2(status, status_len), ist2(reason, reason_len)); |
| 5796 | } |
| 5797 | else { |
| 5798 | flags = HTX_SL_F_IS_RESP; |
| 5799 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), |
| 5800 | ist2(status, status_len), ist2(reason, reason_len)); |
| 5801 | } |
| 5802 | if (!sl) |
| 5803 | goto fail; |
| 5804 | sl->info.res.status = code; |
| 5805 | |
| 5806 | /* Push in the stack the "headers" entry. */ |
| 5807 | ret = lua_getfield(L, -1, "headers"); |
| 5808 | if (ret != LUA_TTABLE) |
| 5809 | goto skip_headers; |
| 5810 | |
| 5811 | lua_pushnil(L); |
| 5812 | while (lua_next(L, -2) != 0) { |
| 5813 | struct ist name, value; |
| 5814 | const char *n, *v; |
| 5815 | size_t nlen, vlen; |
| 5816 | |
| 5817 | if (!lua_isstring(L, -2) || !lua_istable(L, -1)) { |
| 5818 | /* Skip element if the key is not a string or if the value is not a table */ |
| 5819 | goto next_hdr; |
| 5820 | } |
| 5821 | |
| 5822 | n = lua_tolstring(L, -2, &nlen); |
| 5823 | name = ist2(n, nlen); |
| 5824 | if (isteqi(name, ist("content-length"))) { |
| 5825 | /* Always skip content-length header. It will be added |
| 5826 | * later with the correct len |
| 5827 | */ |
| 5828 | goto next_hdr; |
| 5829 | } |
| 5830 | |
| 5831 | /* Loop on header's values */ |
| 5832 | lua_pushnil(L); |
| 5833 | while (lua_next(L, -2)) { |
| 5834 | if (!lua_isstring(L, -1)) { |
| 5835 | /* Skip the value if it is not a string */ |
| 5836 | goto next_value; |
| 5837 | } |
| 5838 | |
| 5839 | v = lua_tolstring(L, -1, &vlen); |
| 5840 | value = ist2(v, vlen); |
| 5841 | |
| 5842 | if (isteqi(name, ist("transfer-encoding"))) |
| 5843 | h1_parse_xfer_enc_header(&h1m, value); |
| 5844 | if (!htx_add_header(htx, ist2(n, nlen), ist2(v, vlen))) |
| 5845 | goto fail; |
| 5846 | |
| 5847 | next_value: |
| 5848 | lua_pop(L, 1); |
| 5849 | } |
| 5850 | |
| 5851 | next_hdr: |
| 5852 | lua_pop(L, 1); |
| 5853 | } |
| 5854 | skip_headers: |
| 5855 | lua_pop(L, 1); |
| 5856 | |
| 5857 | /* Update h1m flags: CLEN is set if CHNK is not present */ |
| 5858 | if (!(h1m.flags & H1_MF_CHNK)) { |
| 5859 | const char *clen = ultoa(body_len); |
| 5860 | |
| 5861 | h1m.flags |= H1_MF_CLEN; |
| 5862 | if (!htx_add_header(htx, ist("content-length"), ist(clen))) |
| 5863 | goto fail; |
| 5864 | } |
| 5865 | if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK)) |
| 5866 | h1m.flags |= H1_MF_XFER_LEN; |
| 5867 | |
| 5868 | /* Update HTX start-line flags */ |
| 5869 | if (h1m.flags & H1_MF_XFER_ENC) |
| 5870 | flags |= HTX_SL_F_XFER_ENC; |
| 5871 | if (h1m.flags & H1_MF_XFER_LEN) { |
| 5872 | flags |= HTX_SL_F_XFER_LEN; |
| 5873 | if (h1m.flags & H1_MF_CHNK) |
| 5874 | flags |= HTX_SL_F_CHNK; |
| 5875 | else if (h1m.flags & H1_MF_CLEN) |
| 5876 | flags |= HTX_SL_F_CLEN; |
| 5877 | if (h1m.body_len == 0) |
| 5878 | flags |= HTX_SL_F_BODYLESS; |
| 5879 | } |
| 5880 | sl->flags |= flags; |
| 5881 | |
| 5882 | |
| 5883 | if (!htx_add_endof(htx, HTX_BLK_EOH) || |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 5884 | (body_len && !htx_add_data_atonce(htx, ist2(body, body_len)))) |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5885 | goto fail; |
| 5886 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 5887 | htx->flags |= HTX_FL_EOM; |
| 5888 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5889 | /* Now, forward the response and terminate the transaction */ |
| 5890 | s->txn->status = code; |
| 5891 | htx_to_buf(htx, &s->res.buf); |
| 5892 | if (!http_forward_proxy_resp(s, 1)) |
| 5893 | goto fail; |
| 5894 | |
| 5895 | return 1; |
| 5896 | |
| 5897 | fail: |
| 5898 | channel_htx_truncate(&s->res, htx); |
| 5899 | return 0; |
| 5900 | } |
| 5901 | |
| 5902 | /* Terminate a transaction if called from a lua action. For TCP streams, |
| 5903 | * processing is just aborted. Nothing is returned to the client and all |
| 5904 | * arguments are ignored. For HTTP streams, if a reply is passed as argument, it |
| 5905 | * is forwarded to the client before terminating the transaction. On success, |
| 5906 | * the function exits with ACT_RET_DONE code. If an error occurred, it exits |
| 5907 | * with ACT_RET_ERR code. If this function is not called from a lua action, it |
| 5908 | * just exits without any processing. |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5909 | */ |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 5910 | __LJMP static int hlua_txn_done(lua_State *L) |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5911 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 5912 | struct hlua_txn *htxn; |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5913 | struct stream *s; |
| 5914 | int finst; |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5915 | |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 5916 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5917 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5918 | /* If the flags NOTERM is set, we cannot terminate the session, so we |
| 5919 | * just end the execution of the current lua code. */ |
| 5920 | if (htxn->flags & HLUA_TXN_NOTERM) |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5921 | WILL_LJMP(hlua_done(L)); |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5922 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5923 | s = htxn->s; |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5924 | if (!IS_HTX_STRM(htxn->s)) { |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5925 | struct channel *req = &s->req; |
| 5926 | struct channel *res = &s->res; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 5927 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5928 | channel_auto_read(req); |
| 5929 | channel_abort(req); |
| 5930 | channel_auto_close(req); |
| 5931 | channel_erase(req); |
| 5932 | |
| 5933 | res->wex = tick_add_ifset(now_ms, res->wto); |
| 5934 | channel_auto_read(res); |
| 5935 | channel_auto_close(res); |
| 5936 | channel_shutr_now(res); |
| 5937 | |
| 5938 | finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_D); |
| 5939 | goto done; |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 5940 | } |
Thierry FOURNIER | 10ec214 | 2015-08-24 17:23:45 +0200 | [diff] [blame] | 5941 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5942 | if (lua_gettop(L) == 1 || !lua_istable(L, 2)) { |
| 5943 | /* No reply or invalid reply */ |
| 5944 | s->txn->status = 0; |
| 5945 | http_reply_and_close(s, 0, NULL); |
| 5946 | } |
| 5947 | else { |
| 5948 | /* Remove extra args to have the reply on top of the stack */ |
| 5949 | if (lua_gettop(L) > 2) |
| 5950 | lua_pop(L, lua_gettop(L) - 2); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5951 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5952 | if (!hlua_txn_forward_reply(L, s)) { |
| 5953 | if (!(s->flags & SF_ERR_MASK)) |
| 5954 | s->flags |= SF_ERR_PRXCOND; |
| 5955 | lua_pushinteger(L, ACT_RET_ERR); |
| 5956 | WILL_LJMP(hlua_done(L)); |
| 5957 | return 0; /* Never reached */ |
| 5958 | } |
Christopher Faulet | 4d0e263 | 2019-07-16 10:52:40 +0200 | [diff] [blame] | 5959 | } |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 5960 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5961 | finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_H); |
| 5962 | if (htxn->dir == SMP_OPT_DIR_REQ) { |
| 5963 | /* let's log the request time */ |
| 5964 | s->logs.tv_request = now; |
| 5965 | 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] | 5966 | _HA_ATOMIC_INC(&s->sess->fe->fe_counters.intercepted_req); |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5967 | } |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 5968 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5969 | done: |
| 5970 | if (!(s->flags & SF_ERR_MASK)) |
| 5971 | s->flags |= SF_ERR_LOCAL; |
| 5972 | if (!(s->flags & SF_FINST_MASK)) |
| 5973 | s->flags |= finst; |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 5974 | |
Christopher Faulet | 4ad7310 | 2020-03-05 11:07:31 +0100 | [diff] [blame] | 5975 | lua_pushinteger(L, ACT_RET_ABRT); |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 5976 | WILL_LJMP(hlua_done(L)); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 5977 | return 0; |
| 5978 | } |
| 5979 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5980 | /* |
| 5981 | * |
| 5982 | * |
| 5983 | * Class REPLY |
| 5984 | * |
| 5985 | * |
| 5986 | */ |
| 5987 | |
| 5988 | /* Pushes the TXN reply onto the top of the stack. If the stask does not have a |
| 5989 | * free slots, the function fails and returns 0; |
| 5990 | */ |
| 5991 | static int hlua_txn_reply_new(lua_State *L) |
| 5992 | { |
| 5993 | struct hlua_txn *htxn; |
| 5994 | const char *reason, *body = NULL; |
| 5995 | int ret, status; |
| 5996 | |
| 5997 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5998 | if (!IS_HTX_STRM(htxn->s)) { |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5999 | hlua_pusherror(L, "txn object is not an HTTP transaction."); |
| 6000 | WILL_LJMP(lua_error(L)); |
| 6001 | } |
| 6002 | |
| 6003 | /* Default value */ |
| 6004 | status = 200; |
| 6005 | reason = http_get_reason(status); |
| 6006 | |
| 6007 | if (lua_istable(L, 2)) { |
| 6008 | /* load status and reason from the table argument at index 2 */ |
| 6009 | ret = lua_getfield(L, 2, "status"); |
| 6010 | if (ret == LUA_TNIL) |
| 6011 | goto reason; |
| 6012 | else if (ret != LUA_TNUMBER) { |
| 6013 | /* invalid status: ignore the reason */ |
| 6014 | goto body; |
| 6015 | } |
| 6016 | status = lua_tointeger(L, -1); |
| 6017 | |
| 6018 | reason: |
| 6019 | lua_pop(L, 1); /* restore the stack: remove status */ |
| 6020 | ret = lua_getfield(L, 2, "reason"); |
| 6021 | if (ret == LUA_TSTRING) |
| 6022 | reason = lua_tostring(L, -1); |
| 6023 | |
| 6024 | body: |
| 6025 | lua_pop(L, 1); /* restore the stack: remove invalid status or reason */ |
| 6026 | ret = lua_getfield(L, 2, "body"); |
| 6027 | if (ret == LUA_TSTRING) |
| 6028 | body = lua_tostring(L, -1); |
| 6029 | lua_pop(L, 1); /* restore the stack: remove body */ |
| 6030 | } |
| 6031 | |
| 6032 | /* Create the Reply table */ |
| 6033 | lua_newtable(L); |
| 6034 | |
| 6035 | /* Add status element */ |
| 6036 | lua_pushstring(L, "status"); |
| 6037 | lua_pushinteger(L, status); |
| 6038 | lua_settable(L, -3); |
| 6039 | |
| 6040 | /* Add reason element */ |
| 6041 | reason = http_get_reason(status); |
| 6042 | lua_pushstring(L, "reason"); |
| 6043 | lua_pushstring(L, reason); |
| 6044 | lua_settable(L, -3); |
| 6045 | |
| 6046 | /* Add body element, nil if undefined */ |
| 6047 | lua_pushstring(L, "body"); |
| 6048 | if (body) |
| 6049 | lua_pushstring(L, body); |
| 6050 | else |
| 6051 | lua_pushnil(L); |
| 6052 | lua_settable(L, -3); |
| 6053 | |
| 6054 | /* Add headers element */ |
| 6055 | lua_pushstring(L, "headers"); |
| 6056 | lua_newtable(L); |
| 6057 | |
| 6058 | /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */ |
| 6059 | if (lua_istable(L, 2)) { |
| 6060 | /* load headers from the table argument at index 2. If it is a table, copy it. */ |
| 6061 | ret = lua_getfield(L, 2, "headers"); |
| 6062 | if (ret == LUA_TTABLE) { |
| 6063 | /* stack: [ ... <headers:table>, <table> ] */ |
| 6064 | lua_pushnil(L); |
| 6065 | while (lua_next(L, -2) != 0) { |
| 6066 | /* stack: [ ... <headers:table>, <table>, k, v] */ |
| 6067 | if (!lua_isstring(L, -1) && !lua_istable(L, -1)) { |
| 6068 | /* invalid value type, skip it */ |
| 6069 | lua_pop(L, 1); |
| 6070 | continue; |
| 6071 | } |
| 6072 | |
| 6073 | |
| 6074 | /* Duplicate the key and swap it with the value. */ |
| 6075 | lua_pushvalue(L, -2); |
| 6076 | lua_insert(L, -2); |
| 6077 | /* stack: [ ... <headers:table>, <table>, k, k, v ] */ |
| 6078 | |
| 6079 | lua_newtable(L); |
| 6080 | lua_insert(L, -2); |
| 6081 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, v ] */ |
| 6082 | |
| 6083 | if (lua_isstring(L, -1)) { |
| 6084 | /* push the value in the inner table */ |
| 6085 | lua_rawseti(L, -2, 1); |
| 6086 | } |
| 6087 | else { /* table */ |
| 6088 | lua_pushnil(L); |
| 6089 | while (lua_next(L, -2) != 0) { |
| 6090 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2, v2 ] */ |
| 6091 | if (!lua_isstring(L, -1)) { |
| 6092 | /* invalid value type, skip it*/ |
| 6093 | lua_pop(L, 1); |
| 6094 | continue; |
| 6095 | } |
| 6096 | /* push the value in the inner table */ |
| 6097 | lua_rawseti(L, -4, lua_rawlen(L, -4) + 1); |
| 6098 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2 ] */ |
| 6099 | } |
| 6100 | lua_pop(L, 1); |
| 6101 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table> ] */ |
| 6102 | } |
| 6103 | |
| 6104 | /* push (k,v) on the stack in the headers table: |
| 6105 | * stack: [ ... <headers:table>, <table>, k, k, v ] |
| 6106 | */ |
| 6107 | lua_settable(L, -5); |
| 6108 | /* stack: [ ... <headers:table>, <table>, k ] */ |
| 6109 | } |
| 6110 | } |
| 6111 | lua_pop(L, 1); |
| 6112 | } |
| 6113 | /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */ |
| 6114 | lua_settable(L, -3); |
| 6115 | /* stack: [ txn, <Arg:table>, <Reply:table> ] */ |
| 6116 | |
| 6117 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 6118 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_reply_ref); |
| 6119 | lua_setmetatable(L, -2); |
| 6120 | return 1; |
| 6121 | } |
| 6122 | |
| 6123 | /* Set the reply status code, and optionally the reason. If no reason is |
| 6124 | * provided, the default one corresponding to the status code is used. |
| 6125 | */ |
| 6126 | __LJMP static int hlua_txn_reply_set_status(lua_State *L) |
| 6127 | { |
| 6128 | int status = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 6129 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
| 6130 | |
| 6131 | /* First argument (self) must be a table */ |
| 6132 | luaL_checktype(L, 1, LUA_TTABLE); |
| 6133 | |
| 6134 | if (status < 100 || status > 599) { |
| 6135 | lua_pushboolean(L, 0); |
| 6136 | return 1; |
| 6137 | } |
| 6138 | if (!reason) |
| 6139 | reason = http_get_reason(status); |
| 6140 | |
| 6141 | lua_pushinteger(L, status); |
| 6142 | lua_setfield(L, 1, "status"); |
| 6143 | |
| 6144 | lua_pushstring(L, reason); |
| 6145 | lua_setfield(L, 1, "reason"); |
| 6146 | |
| 6147 | lua_pushboolean(L, 1); |
| 6148 | return 1; |
| 6149 | } |
| 6150 | |
| 6151 | /* Add a header into the reply object. Each header name is associated to an |
| 6152 | * array of values in the "headers" table. If the header name is not found, a |
| 6153 | * new entry is created. |
| 6154 | */ |
| 6155 | __LJMP static int hlua_txn_reply_add_header(lua_State *L) |
| 6156 | { |
| 6157 | const char *name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 6158 | const char *value = MAY_LJMP(luaL_checkstring(L, 3)); |
| 6159 | int ret; |
| 6160 | |
| 6161 | /* First argument (self) must be a table */ |
| 6162 | luaL_checktype(L, 1, LUA_TTABLE); |
| 6163 | |
| 6164 | /* Push in the stack the "headers" entry. */ |
| 6165 | ret = lua_getfield(L, 1, "headers"); |
| 6166 | if (ret != LUA_TTABLE) { |
| 6167 | hlua_pusherror(L, "Reply['headers'] is expected to a an array. %s found", lua_typename(L, ret)); |
| 6168 | WILL_LJMP(lua_error(L)); |
| 6169 | } |
| 6170 | |
| 6171 | /* check if the header is already registered. If not, register it. */ |
| 6172 | ret = lua_getfield(L, -1, name); |
| 6173 | if (ret == LUA_TNIL) { |
| 6174 | /* Entry not found. */ |
| 6175 | lua_pop(L, 1); /* remove the nil. The "headers" table is the top of the stack. */ |
| 6176 | |
| 6177 | /* Insert the new header name in the array in the top of the stack. |
| 6178 | * It left the new array in the top of the stack. |
| 6179 | */ |
| 6180 | lua_newtable(L); |
| 6181 | lua_pushstring(L, name); |
| 6182 | lua_pushvalue(L, -2); |
| 6183 | lua_settable(L, -4); |
| 6184 | } |
| 6185 | else if (ret != LUA_TTABLE) { |
| 6186 | hlua_pusherror(L, "Reply['headers']['%s'] is expected to be an array. %s found", name, lua_typename(L, ret)); |
| 6187 | WILL_LJMP(lua_error(L)); |
| 6188 | } |
| 6189 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 6190 | /* Now the top of thestack is an array of values. We push |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 6191 | * the header value as new entry. |
| 6192 | */ |
| 6193 | lua_pushstring(L, value); |
| 6194 | ret = lua_rawlen(L, -2); |
| 6195 | lua_rawseti(L, -2, ret + 1); |
| 6196 | |
| 6197 | lua_pushboolean(L, 1); |
| 6198 | return 1; |
| 6199 | } |
| 6200 | |
| 6201 | /* Remove all occurrences of a given header name. */ |
| 6202 | __LJMP static int hlua_txn_reply_del_header(lua_State *L) |
| 6203 | { |
| 6204 | const char *name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 6205 | int ret; |
| 6206 | |
| 6207 | /* First argument (self) must be a table */ |
| 6208 | luaL_checktype(L, 1, LUA_TTABLE); |
| 6209 | |
| 6210 | /* Push in the stack the "headers" entry. */ |
| 6211 | ret = lua_getfield(L, 1, "headers"); |
| 6212 | if (ret != LUA_TTABLE) { |
| 6213 | hlua_pusherror(L, "Reply['headers'] is expected to be an array. %s found", lua_typename(L, ret)); |
| 6214 | WILL_LJMP(lua_error(L)); |
| 6215 | } |
| 6216 | |
| 6217 | lua_pushstring(L, name); |
| 6218 | lua_pushnil(L); |
| 6219 | lua_settable(L, -3); |
| 6220 | |
| 6221 | lua_pushboolean(L, 1); |
| 6222 | return 1; |
| 6223 | } |
| 6224 | |
| 6225 | /* Set the reply's body. Overwrite any existing entry. */ |
| 6226 | __LJMP static int hlua_txn_reply_set_body(lua_State *L) |
| 6227 | { |
| 6228 | const char *payload = MAY_LJMP(luaL_checkstring(L, 2)); |
| 6229 | |
| 6230 | /* First argument (self) must be a table */ |
| 6231 | luaL_checktype(L, 1, LUA_TTABLE); |
| 6232 | |
| 6233 | lua_pushstring(L, payload); |
| 6234 | lua_setfield(L, 1, "body"); |
| 6235 | |
| 6236 | lua_pushboolean(L, 1); |
| 6237 | return 1; |
| 6238 | } |
| 6239 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 6240 | __LJMP static int hlua_log(lua_State *L) |
| 6241 | { |
| 6242 | int level; |
| 6243 | const char *msg; |
| 6244 | |
| 6245 | MAY_LJMP(check_args(L, 2, "log")); |
| 6246 | level = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 6247 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 6248 | |
| 6249 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 6250 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 6251 | |
| 6252 | hlua_sendlog(NULL, level, msg); |
| 6253 | return 0; |
| 6254 | } |
| 6255 | |
| 6256 | __LJMP static int hlua_log_debug(lua_State *L) |
| 6257 | { |
| 6258 | const char *msg; |
| 6259 | |
| 6260 | MAY_LJMP(check_args(L, 1, "debug")); |
| 6261 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6262 | hlua_sendlog(NULL, LOG_DEBUG, msg); |
| 6263 | return 0; |
| 6264 | } |
| 6265 | |
| 6266 | __LJMP static int hlua_log_info(lua_State *L) |
| 6267 | { |
| 6268 | const char *msg; |
| 6269 | |
| 6270 | MAY_LJMP(check_args(L, 1, "info")); |
| 6271 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6272 | hlua_sendlog(NULL, LOG_INFO, msg); |
| 6273 | return 0; |
| 6274 | } |
| 6275 | |
| 6276 | __LJMP static int hlua_log_warning(lua_State *L) |
| 6277 | { |
| 6278 | const char *msg; |
| 6279 | |
| 6280 | MAY_LJMP(check_args(L, 1, "warning")); |
| 6281 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6282 | hlua_sendlog(NULL, LOG_WARNING, msg); |
| 6283 | return 0; |
| 6284 | } |
| 6285 | |
| 6286 | __LJMP static int hlua_log_alert(lua_State *L) |
| 6287 | { |
| 6288 | const char *msg; |
| 6289 | |
| 6290 | MAY_LJMP(check_args(L, 1, "alert")); |
| 6291 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6292 | hlua_sendlog(NULL, LOG_ALERT, msg); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 6293 | return 0; |
| 6294 | } |
| 6295 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 6296 | __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] | 6297 | { |
| 6298 | int wakeup_ms = lua_tointeger(L, -1); |
Willy Tarreau | 9cc2e4c | 2021-09-30 16:12:31 +0200 | [diff] [blame] | 6299 | if (!tick_is_expired(wakeup_ms, now_ms)) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 6300 | 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] | 6301 | return 0; |
| 6302 | } |
| 6303 | |
| 6304 | __LJMP static int hlua_sleep(lua_State *L) |
| 6305 | { |
| 6306 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6307 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6308 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6309 | MAY_LJMP(check_args(L, 1, "sleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6310 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 6311 | delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6312 | wakeup_ms = tick_add(now_ms, delay); |
| 6313 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6314 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 6315 | 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] | 6316 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6317 | } |
| 6318 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6319 | __LJMP static int hlua_msleep(lua_State *L) |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6320 | { |
| 6321 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6322 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6323 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6324 | MAY_LJMP(check_args(L, 1, "msleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6325 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 6326 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6327 | wakeup_ms = tick_add(now_ms, delay); |
| 6328 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6329 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 6330 | 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] | 6331 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6332 | } |
| 6333 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 6334 | /* This functionis an LUA binding. it permits to give back |
| 6335 | * the hand at the HAProxy scheduler. It is used when the |
| 6336 | * LUA processing consumes a lot of time. |
| 6337 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 6338 | __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] | 6339 | { |
| 6340 | return 0; |
| 6341 | } |
| 6342 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 6343 | __LJMP static int hlua_yield(lua_State *L) |
| 6344 | { |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 6345 | 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] | 6346 | return 0; |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 6347 | } |
| 6348 | |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6349 | /* This function change the nice of the currently executed |
| 6350 | * task. It is used set low or high priority at the current |
| 6351 | * task. |
| 6352 | */ |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 6353 | __LJMP static int hlua_set_nice(lua_State *L) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6354 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6355 | struct hlua *hlua; |
| 6356 | int nice; |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6357 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6358 | MAY_LJMP(check_args(L, 1, "set_nice")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6359 | nice = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6360 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 6361 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 6362 | hlua = hlua_gethlua(L); |
| 6363 | |
| 6364 | /* If the task is not set, I'm in a start mode. */ |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6365 | if (!hlua || !hlua->task) |
| 6366 | return 0; |
| 6367 | |
| 6368 | if (nice < -1024) |
| 6369 | nice = -1024; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6370 | else if (nice > 1024) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6371 | nice = 1024; |
| 6372 | |
| 6373 | hlua->task->nice = nice; |
| 6374 | return 0; |
| 6375 | } |
| 6376 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6377 | /* 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] | 6378 | * HAProxy task subsystem when the task is awaked. The LUA runtime can |
| 6379 | * return an E_AGAIN signal, the emmiter of this signal must set a |
| 6380 | * signal to wake the task. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6381 | * |
| 6382 | * Task wrapper are longjmp safe because the only one Lua code |
| 6383 | * executed is the safe hlua_ctx_resume(); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6384 | */ |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 6385 | 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] | 6386 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 6387 | struct hlua *hlua = context; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6388 | enum hlua_exec status; |
| 6389 | |
Christopher Faulet | 5bc9972 | 2018-04-25 10:34:45 +0200 | [diff] [blame] | 6390 | if (task->thread_mask == MAX_THREADS_MASK) |
| 6391 | task_set_affinity(task, tid_bit); |
| 6392 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6393 | /* If it is the first call to the task, we must initialize the |
| 6394 | * execution timeouts. |
| 6395 | */ |
| 6396 | if (!HLUA_IS_RUNNING(hlua)) |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 6397 | hlua->max_time = hlua_timeout_task; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6398 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6399 | /* Execute the Lua code. */ |
| 6400 | status = hlua_ctx_resume(hlua, 1); |
| 6401 | |
| 6402 | switch (status) { |
| 6403 | /* finished or yield */ |
| 6404 | case HLUA_E_OK: |
| 6405 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 6406 | task_destroy(task); |
Tim Duesterhus | cd235c6 | 2018-04-24 13:56:01 +0200 | [diff] [blame] | 6407 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6408 | break; |
| 6409 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6410 | case HLUA_E_AGAIN: /* co process or timeout wake me later. */ |
Thierry FOURNIER | cb14688 | 2017-12-10 17:10:57 +0100 | [diff] [blame] | 6411 | notification_gc(&hlua->com); |
PiBa-NL | fe971b3 | 2018-05-02 22:27:14 +0200 | [diff] [blame] | 6412 | task->expire = hlua->wake_time; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6413 | break; |
| 6414 | |
| 6415 | /* finished with error. */ |
| 6416 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6417 | SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1)); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6418 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 6419 | task_destroy(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 6420 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6421 | break; |
| 6422 | |
| 6423 | case HLUA_E_ERR: |
| 6424 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6425 | SEND_ERR(NULL, "Lua task: unknown error.\n"); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6426 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 6427 | task_destroy(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 6428 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6429 | break; |
| 6430 | } |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 6431 | return task; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6432 | } |
| 6433 | |
Aurelien DARRAGON | 4a7a5d8 | 2023-08-09 14:56:19 +0200 | [diff] [blame] | 6434 | /* Helper function to prepare the lua ctx for a given stream |
| 6435 | * |
Aurelien DARRAGON | 16226c8 | 2023-08-09 15:19:56 +0200 | [diff] [blame] | 6436 | * ctx will be enforced in <state_id> parent stack on initial creation. |
| 6437 | * If s->hlua->state_id differs from <state_id>, which may happen at |
| 6438 | * runtime since existing stream hlua ctx will be reused for other |
| 6439 | * "independent" (but stream-related) lua executions, hlua will be |
| 6440 | * recreated with the expected state id. |
Aurelien DARRAGON | 4a7a5d8 | 2023-08-09 14:56:19 +0200 | [diff] [blame] | 6441 | * |
| 6442 | * Returns 1 for success and 0 for failure |
| 6443 | */ |
| 6444 | static int hlua_stream_ctx_prepare(struct stream *s, int state_id) |
| 6445 | { |
| 6446 | /* In the execution wrappers linked with a stream, the |
| 6447 | * Lua context can be not initialized. This behavior |
| 6448 | * permits to save performances because a systematic |
| 6449 | * Lua initialization cause 5% performances loss. |
| 6450 | */ |
Aurelien DARRAGON | 16226c8 | 2023-08-09 15:19:56 +0200 | [diff] [blame] | 6451 | ctx_renew: |
Aurelien DARRAGON | 4a7a5d8 | 2023-08-09 14:56:19 +0200 | [diff] [blame] | 6452 | if (!s->hlua) { |
| 6453 | struct hlua *hlua; |
| 6454 | |
| 6455 | hlua = pool_alloc(pool_head_hlua); |
| 6456 | if (!hlua) |
| 6457 | return 0; |
| 6458 | HLUA_INIT(hlua); |
| 6459 | if (!hlua_ctx_init(hlua, state_id, s->task)) { |
| 6460 | pool_free(pool_head_hlua, hlua); |
| 6461 | return 0; |
| 6462 | } |
| 6463 | s->hlua = hlua; |
| 6464 | } |
Aurelien DARRAGON | 16226c8 | 2023-08-09 15:19:56 +0200 | [diff] [blame] | 6465 | else if (s->hlua->state_id != state_id) { |
| 6466 | /* ctx already created, but not in proper state. |
| 6467 | * It should only happen after the previous execution is |
| 6468 | * finished, otherwise it's probably a bug since we don't |
| 6469 | * want to abort unfinished job.. |
| 6470 | */ |
| 6471 | BUG_ON(HLUA_IS_RUNNING(s->hlua)); |
| 6472 | hlua_ctx_destroy(s->hlua); |
| 6473 | s->hlua = NULL; |
| 6474 | goto ctx_renew; |
| 6475 | } |
Aurelien DARRAGON | 4a7a5d8 | 2023-08-09 14:56:19 +0200 | [diff] [blame] | 6476 | return 1; |
| 6477 | } |
| 6478 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6479 | /* This function is an LUA binding that register LUA function to be |
| 6480 | * executed after the HAProxy configuration parsing and before the |
| 6481 | * HAProxy scheduler starts. This function expect only one LUA |
| 6482 | * argument that is a function. This function returns nothing, but |
| 6483 | * throws if an error is encountered. |
| 6484 | */ |
| 6485 | __LJMP static int hlua_register_init(lua_State *L) |
| 6486 | { |
| 6487 | struct hlua_init_function *init; |
| 6488 | int ref; |
| 6489 | |
| 6490 | MAY_LJMP(check_args(L, 1, "register_init")); |
| 6491 | |
Aurelien DARRAGON | 8985ab8 | 2023-02-24 09:43:54 +0100 | [diff] [blame] | 6492 | if (hlua_gethlua(L)) { |
| 6493 | /* runtime processing */ |
| 6494 | WILL_LJMP(luaL_error(L, "register_init: not available outside of body context")); |
| 6495 | } |
| 6496 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6497 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 6498 | |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6499 | init = calloc(1, sizeof(*init)); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6500 | if (!init) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6501 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6502 | |
| 6503 | init->function_ref = ref; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 6504 | LIST_APPEND(&hlua_init_functions[hlua_state_id], &init->l); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6505 | return 0; |
| 6506 | } |
| 6507 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6508 | /* This functio is an LUA binding. It permits to register a task |
| 6509 | * executed in parallel of the main HAroxy activity. The task is |
| 6510 | * created and it is set in the HAProxy scheduler. It can be called |
| 6511 | * from the "init" section, "post init" or during the runtime. |
| 6512 | * |
| 6513 | * Lua prototype: |
| 6514 | * |
| 6515 | * <none> core.register_task(<function>) |
| 6516 | */ |
| 6517 | static int hlua_register_task(lua_State *L) |
| 6518 | { |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 6519 | struct hlua *hlua = NULL; |
| 6520 | struct task *task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6521 | int ref; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 6522 | int state_id; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6523 | |
| 6524 | MAY_LJMP(check_args(L, 1, "register_task")); |
| 6525 | |
| 6526 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 6527 | |
Thierry Fournier | 75fc029 | 2020-11-28 13:18:56 +0100 | [diff] [blame] | 6528 | /* Get the reference state. If the reference is NULL, L is the master |
| 6529 | * state, otherwise hlua->T is. |
| 6530 | */ |
| 6531 | hlua = hlua_gethlua(L); |
| 6532 | if (hlua) |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 6533 | /* we are in runtime processing */ |
| 6534 | state_id = hlua->state_id; |
Thierry Fournier | 75fc029 | 2020-11-28 13:18:56 +0100 | [diff] [blame] | 6535 | else |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 6536 | /* we are in initialization mode */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 6537 | state_id = hlua_state_id; |
Thierry Fournier | 75fc029 | 2020-11-28 13:18:56 +0100 | [diff] [blame] | 6538 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6539 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6540 | if (!hlua) |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 6541 | goto alloc_error; |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 6542 | HLUA_INIT(hlua); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6543 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 6544 | /* We are in the common lua state, execute the task anywhere, |
| 6545 | * otherwise, inherit the current thread identifier |
| 6546 | */ |
| 6547 | if (state_id == 0) |
| 6548 | task = task_new(MAX_THREADS_MASK); |
| 6549 | else |
| 6550 | task = task_new(tid_bit); |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 6551 | if (!task) |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 6552 | goto alloc_error; |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 6553 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6554 | task->context = hlua; |
| 6555 | task->process = hlua_process_task; |
| 6556 | |
Aurelien DARRAGON | 5483bde | 2023-03-21 14:02:16 +0100 | [diff] [blame] | 6557 | if (!hlua_ctx_init(hlua, state_id, task)) |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 6558 | goto alloc_error; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6559 | |
| 6560 | /* Restore the function in the stack. */ |
| 6561 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref); |
Aurelien DARRAGON | b6aade3 | 2023-03-13 14:09:21 +0100 | [diff] [blame] | 6562 | /* function ref not needed anymore since it was pushed to the substack */ |
| 6563 | hlua_unref(L, ref); |
| 6564 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6565 | hlua->nargs = 0; |
| 6566 | |
| 6567 | /* Schedule task. */ |
Willy Tarreau | 790810f | 2021-09-30 16:17:37 +0200 | [diff] [blame] | 6568 | task_wakeup(task, TASK_WOKEN_INIT); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6569 | |
| 6570 | return 0; |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 6571 | |
| 6572 | alloc_error: |
| 6573 | task_destroy(task); |
| 6574 | hlua_ctx_destroy(hlua); |
| 6575 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 6576 | return 0; /* Never reached */ |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6577 | } |
| 6578 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6579 | /* Wrapper called by HAProxy to execute an LUA converter. This wrapper |
| 6580 | * doesn't allow "yield" functions because the HAProxy engine cannot |
| 6581 | * resume converters. |
| 6582 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6583 | 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] | 6584 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 6585 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6586 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6587 | const char *error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6588 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6589 | if (!stream) |
| 6590 | return 0; |
| 6591 | |
Aurelien DARRAGON | 4a7a5d8 | 2023-08-09 14:56:19 +0200 | [diff] [blame] | 6592 | if (!hlua_stream_ctx_prepare(stream, fcn_ref_to_stack_id(fcn))) { |
| 6593 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 6594 | return 0; |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6595 | } |
| 6596 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6597 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6598 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6599 | |
| 6600 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 6601 | if (!SET_SAFE_LJMP(stream->hlua)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6602 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 6603 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6604 | else |
| 6605 | error = "critical error"; |
| 6606 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6607 | return 0; |
| 6608 | } |
| 6609 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6610 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6611 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6612 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 6613 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6614 | return 0; |
| 6615 | } |
| 6616 | |
| 6617 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 6618 | 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] | 6619 | |
| 6620 | /* convert input sample and pust-it in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6621 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6622 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 6623 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6624 | return 0; |
| 6625 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6626 | hlua_smp2lua(stream->hlua->T, smp); |
| 6627 | stream->hlua->nargs = 1; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6628 | |
| 6629 | /* push keywords in the stack. */ |
| 6630 | if (arg_p) { |
| 6631 | for (; arg_p->type != ARGT_STOP; arg_p++) { |
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_arg2lua(stream->hlua->T, arg_p); |
| 6638 | stream->hlua->nargs++; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6639 | } |
| 6640 | } |
| 6641 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6642 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6643 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6644 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6645 | /* At this point the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 6646 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6647 | } |
| 6648 | |
| 6649 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6650 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6651 | /* finished. */ |
| 6652 | case HLUA_E_OK: |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 6653 | /* If the stack is empty, the function fails. */ |
| 6654 | if (lua_gettop(stream->hlua->T) <= 0) |
| 6655 | return 0; |
| 6656 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6657 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6658 | hlua_lua2smp(stream->hlua->T, -1, smp); |
Aurelien DARRAGON | d39fb74 | 2023-05-17 16:06:11 +0200 | [diff] [blame] | 6659 | /* dup the smp before popping the related lua value and |
| 6660 | * returning it to haproxy |
| 6661 | */ |
| 6662 | smp_dup(smp); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6663 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6664 | return 1; |
| 6665 | |
| 6666 | /* yield. */ |
| 6667 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6668 | 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] | 6669 | return 0; |
| 6670 | |
| 6671 | /* finished with error. */ |
| 6672 | case HLUA_E_ERRMSG: |
| 6673 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6674 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6675 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 6676 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6677 | return 0; |
| 6678 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6679 | case HLUA_E_ETMOUT: |
| 6680 | SEND_ERR(stream->be, "Lua converter '%s': execution timeout.\n", fcn->name); |
| 6681 | return 0; |
| 6682 | |
| 6683 | case HLUA_E_NOMEM: |
| 6684 | SEND_ERR(stream->be, "Lua converter '%s': out of memory error.\n", fcn->name); |
| 6685 | return 0; |
| 6686 | |
| 6687 | case HLUA_E_YIELD: |
| 6688 | SEND_ERR(stream->be, "Lua converter '%s': yield functions like core.tcp() or core.sleep() are not allowed.\n", fcn->name); |
| 6689 | return 0; |
| 6690 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6691 | case HLUA_E_ERR: |
| 6692 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6693 | 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] | 6694 | /* fall through */ |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6695 | |
| 6696 | default: |
| 6697 | return 0; |
| 6698 | } |
| 6699 | } |
| 6700 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6701 | /* Wrapper called by HAProxy to execute a sample-fetch. this wrapper |
| 6702 | * doesn't allow "yield" functions because the HAProxy engine cannot |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6703 | * resume sample-fetches. This function will be called by the sample |
| 6704 | * fetch engine to call lua-based fetch operations. |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6705 | */ |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6706 | static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp, |
| 6707 | const char *kw, void *private) |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6708 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 6709 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6710 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6711 | const char *error; |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6712 | unsigned int hflags = HLUA_TXN_NOTERM; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6713 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6714 | if (!stream) |
| 6715 | return 0; |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 6716 | |
Aurelien DARRAGON | 4a7a5d8 | 2023-08-09 14:56:19 +0200 | [diff] [blame] | 6717 | if (!hlua_stream_ctx_prepare(stream, fcn_ref_to_stack_id(fcn))) { |
| 6718 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 6719 | return 0; |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6720 | } |
| 6721 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6722 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6723 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6724 | |
| 6725 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 6726 | if (!SET_SAFE_LJMP(stream->hlua)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6727 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 6728 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6729 | else |
| 6730 | error = "critical error"; |
| 6731 | 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] | 6732 | return 0; |
| 6733 | } |
| 6734 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6735 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6736 | if (!lua_checkstack(stream->hlua->T, 2)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6737 | 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] | 6738 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6739 | return 0; |
| 6740 | } |
| 6741 | |
| 6742 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 6743 | 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] | 6744 | |
| 6745 | /* push arguments in the stack. */ |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6746 | 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] | 6747 | 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] | 6748 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6749 | return 0; |
| 6750 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6751 | stream->hlua->nargs = 1; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6752 | |
| 6753 | /* push keywords in the stack. */ |
| 6754 | for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) { |
| 6755 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6756 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6757 | 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] | 6758 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6759 | return 0; |
| 6760 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6761 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 6762 | stream->hlua->nargs++; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6763 | } |
| 6764 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6765 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6766 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6767 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6768 | /* At this point the execution is safe. */ |
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 | } |
| 6771 | |
| 6772 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6773 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6774 | /* finished. */ |
| 6775 | case HLUA_E_OK: |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 6776 | /* If the stack is empty, the function fails. */ |
| 6777 | if (lua_gettop(stream->hlua->T) <= 0) |
| 6778 | return 0; |
| 6779 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6780 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6781 | hlua_lua2smp(stream->hlua->T, -1, smp); |
Aurelien DARRAGON | d39fb74 | 2023-05-17 16:06:11 +0200 | [diff] [blame] | 6782 | /* dup the smp before popping the related lua value and |
| 6783 | * returning it to haproxy |
| 6784 | */ |
| 6785 | smp_dup(smp); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6786 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6787 | |
| 6788 | /* Set the end of execution flag. */ |
| 6789 | smp->flags &= ~SMP_F_MAY_CHANGE; |
| 6790 | return 1; |
| 6791 | |
| 6792 | /* yield. */ |
| 6793 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6794 | 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] | 6795 | return 0; |
| 6796 | |
| 6797 | /* finished with error. */ |
| 6798 | case HLUA_E_ERRMSG: |
| 6799 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6800 | SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6801 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 6802 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6803 | return 0; |
| 6804 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6805 | case HLUA_E_ETMOUT: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6806 | SEND_ERR(smp->px, "Lua sample-fetch '%s': execution timeout.\n", fcn->name); |
| 6807 | return 0; |
| 6808 | |
| 6809 | case HLUA_E_NOMEM: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6810 | SEND_ERR(smp->px, "Lua sample-fetch '%s': out of memory error.\n", fcn->name); |
| 6811 | return 0; |
| 6812 | |
| 6813 | case HLUA_E_YIELD: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6814 | SEND_ERR(smp->px, "Lua sample-fetch '%s': yield not allowed.\n", fcn->name); |
| 6815 | return 0; |
| 6816 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6817 | case HLUA_E_ERR: |
| 6818 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6819 | 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] | 6820 | /* fall through */ |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6821 | |
| 6822 | default: |
| 6823 | return 0; |
| 6824 | } |
| 6825 | } |
| 6826 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6827 | /* This function is an LUA binding used for registering |
| 6828 | * "sample-conv" functions. It expects a converter name used |
| 6829 | * in the haproxy configuration file, and an LUA function. |
| 6830 | */ |
| 6831 | __LJMP static int hlua_register_converters(lua_State *L) |
| 6832 | { |
| 6833 | struct sample_conv_kw_list *sck; |
| 6834 | const char *name; |
| 6835 | int ref; |
| 6836 | int len; |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 6837 | struct hlua_function *fcn = NULL; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 6838 | struct sample_conv *sc; |
| 6839 | struct buffer *trash; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6840 | |
| 6841 | MAY_LJMP(check_args(L, 2, "register_converters")); |
| 6842 | |
Aurelien DARRAGON | 8985ab8 | 2023-02-24 09:43:54 +0100 | [diff] [blame] | 6843 | if (hlua_gethlua(L)) { |
| 6844 | /* runtime processing */ |
| 6845 | WILL_LJMP(luaL_error(L, "register_converters: not available outside of body context")); |
| 6846 | } |
| 6847 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6848 | /* First argument : converter name. */ |
| 6849 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6850 | |
| 6851 | /* Second argument : lua function. */ |
| 6852 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 6853 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 6854 | /* Check if the converter is already registered */ |
| 6855 | trash = get_trash_chunk(); |
| 6856 | chunk_printf(trash, "lua.%s", name); |
| 6857 | sc = find_sample_conv(trash->area, trash->data); |
| 6858 | if (sc != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 6859 | fcn = sc->private; |
| 6860 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 6861 | ha_warning("Trying to register converter 'lua.%s' more than once. " |
| 6862 | "This will become a hard error in version 2.5.\n", name); |
| 6863 | } |
| 6864 | fcn->function_ref[hlua_state_id] = ref; |
| 6865 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 6866 | } |
| 6867 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6868 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6869 | sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6870 | if (!sck) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 6871 | goto alloc_error; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 6872 | fcn = new_hlua_function(); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6873 | if (!fcn) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 6874 | goto alloc_error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6875 | |
| 6876 | /* Fill fcn. */ |
| 6877 | fcn->name = strdup(name); |
| 6878 | if (!fcn->name) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 6879 | goto alloc_error; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 6880 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6881 | |
| 6882 | /* List head */ |
| 6883 | sck->list.n = sck->list.p = NULL; |
| 6884 | |
| 6885 | /* converter keyword. */ |
| 6886 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6887 | sck->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6888 | if (!sck->kw[0].kw) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 6889 | goto alloc_error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6890 | |
| 6891 | snprintf((char *)sck->kw[0].kw, len, "lua.%s", name); |
| 6892 | sck->kw[0].process = hlua_sample_conv_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 6893 | 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] | 6894 | sck->kw[0].val_args = NULL; |
| 6895 | sck->kw[0].in_type = SMP_T_STR; |
| 6896 | sck->kw[0].out_type = SMP_T_STR; |
| 6897 | sck->kw[0].private = fcn; |
| 6898 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6899 | /* Register this new converter */ |
| 6900 | sample_register_convs(sck); |
| 6901 | |
| 6902 | return 0; |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 6903 | |
| 6904 | alloc_error: |
| 6905 | release_hlua_function(fcn); |
| 6906 | ha_free(&sck); |
| 6907 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 6908 | return 0; /* Never reached */ |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6909 | } |
| 6910 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6911 | /* This function is an LUA binding used for registering |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6912 | * "sample-fetch" functions. It expects a converter name used |
| 6913 | * in the haproxy configuration file, and an LUA function. |
| 6914 | */ |
| 6915 | __LJMP static int hlua_register_fetches(lua_State *L) |
| 6916 | { |
| 6917 | const char *name; |
| 6918 | int ref; |
| 6919 | int len; |
| 6920 | struct sample_fetch_kw_list *sfk; |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 6921 | struct hlua_function *fcn = NULL; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 6922 | struct sample_fetch *sf; |
| 6923 | struct buffer *trash; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6924 | |
| 6925 | MAY_LJMP(check_args(L, 2, "register_fetches")); |
| 6926 | |
Aurelien DARRAGON | 8985ab8 | 2023-02-24 09:43:54 +0100 | [diff] [blame] | 6927 | if (hlua_gethlua(L)) { |
| 6928 | /* runtime processing */ |
| 6929 | WILL_LJMP(luaL_error(L, "register_fetches: not available outside of body context")); |
| 6930 | } |
| 6931 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6932 | /* First argument : sample-fetch name. */ |
| 6933 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6934 | |
| 6935 | /* Second argument : lua function. */ |
| 6936 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 6937 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 6938 | /* Check if the sample-fetch is already registered */ |
| 6939 | trash = get_trash_chunk(); |
| 6940 | chunk_printf(trash, "lua.%s", name); |
| 6941 | sf = find_sample_fetch(trash->area, trash->data); |
| 6942 | if (sf != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 6943 | fcn = sf->private; |
| 6944 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 6945 | ha_warning("Trying to register sample-fetch 'lua.%s' more than once. " |
| 6946 | "This will become a hard error in version 2.5.\n", name); |
| 6947 | } |
| 6948 | fcn->function_ref[hlua_state_id] = ref; |
| 6949 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 6950 | } |
| 6951 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6952 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6953 | sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6954 | if (!sfk) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 6955 | goto alloc_error; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 6956 | fcn = new_hlua_function(); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6957 | if (!fcn) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 6958 | goto alloc_error; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6959 | |
| 6960 | /* Fill fcn. */ |
| 6961 | fcn->name = strdup(name); |
| 6962 | if (!fcn->name) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 6963 | goto alloc_error; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 6964 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6965 | |
| 6966 | /* List head */ |
| 6967 | sfk->list.n = sfk->list.p = NULL; |
| 6968 | |
| 6969 | /* sample-fetch keyword. */ |
| 6970 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6971 | sfk->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6972 | if (!sfk->kw[0].kw) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 6973 | goto alloc_error; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6974 | |
| 6975 | snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name); |
| 6976 | sfk->kw[0].process = hlua_sample_fetch_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 6977 | 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] | 6978 | sfk->kw[0].val_args = NULL; |
| 6979 | sfk->kw[0].out_type = SMP_T_STR; |
| 6980 | sfk->kw[0].use = SMP_USE_HTTP_ANY; |
| 6981 | sfk->kw[0].val = 0; |
| 6982 | sfk->kw[0].private = fcn; |
| 6983 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6984 | /* Register this new fetch. */ |
| 6985 | sample_register_fetches(sfk); |
| 6986 | |
| 6987 | return 0; |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 6988 | |
| 6989 | alloc_error: |
| 6990 | release_hlua_function(fcn); |
| 6991 | ha_free(&sfk); |
| 6992 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 6993 | return 0; /* Never reached */ |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6994 | } |
| 6995 | |
Christopher Faulet | 501465d | 2020-02-26 14:54:16 +0100 | [diff] [blame] | 6996 | /* This function is a lua binding to set the wake_time. |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 6997 | */ |
Christopher Faulet | 501465d | 2020-02-26 14:54:16 +0100 | [diff] [blame] | 6998 | __LJMP static int hlua_set_wake_time(lua_State *L) |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 6999 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 7000 | struct hlua *hlua; |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 7001 | unsigned int delay; |
| 7002 | unsigned int wakeup_ms; |
| 7003 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 7004 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 7005 | hlua = hlua_gethlua(L); |
| 7006 | if (!hlua) { |
| 7007 | return 0; |
| 7008 | } |
| 7009 | |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 7010 | MAY_LJMP(check_args(L, 1, "wake_time")); |
| 7011 | |
| 7012 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 7013 | wakeup_ms = tick_add(now_ms, delay); |
| 7014 | hlua->wake_time = wakeup_ms; |
| 7015 | return 0; |
| 7016 | } |
| 7017 | |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7018 | /* This function is a wrapper to execute each LUA function declared as an action |
| 7019 | * wrapper during the initialisation period. This function may return any |
| 7020 | * ACT_RET_* value. On error ACT_RET_CONT is returned and the action is |
| 7021 | * ignored. If the lua action yields, ACT_RET_YIELD is returned. On success, the |
| 7022 | * return value is the first element on the stack. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7023 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7024 | 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] | 7025 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7026 | { |
| 7027 | char **arg; |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 7028 | unsigned int hflags = 0; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7029 | int dir, act_ret = ACT_RET_CONT; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7030 | const char *error; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7031 | |
| 7032 | switch (rule->from) { |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 7033 | case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break; |
| 7034 | case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break; |
| 7035 | case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break; |
| 7036 | case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7037 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 7038 | SEND_ERR(px, "Lua: internal error while execute action.\n"); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7039 | goto end; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7040 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7041 | |
Aurelien DARRAGON | 4a7a5d8 | 2023-08-09 14:56:19 +0200 | [diff] [blame] | 7042 | if (!hlua_stream_ctx_prepare(s, fcn_ref_to_stack_id(rule->arg.hlua_rule->fcn))) { |
| 7043 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
| 7044 | rule->arg.hlua_rule->fcn->name); |
| 7045 | goto end; |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 7046 | } |
| 7047 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7048 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 7049 | if (!HLUA_IS_RUNNING(s->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7050 | |
| 7051 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7052 | if (!SET_SAFE_LJMP(s->hlua)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 7053 | if (lua_type(s->hlua->T, -1) == LUA_TSTRING) |
| 7054 | error = lua_tostring(s->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7055 | else |
| 7056 | error = "critical error"; |
| 7057 | SEND_ERR(px, "Lua function '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7058 | rule->arg.hlua_rule->fcn->name, error); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7059 | goto end; |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7060 | } |
| 7061 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7062 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 7063 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 7064 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7065 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7066 | RESET_SAFE_LJMP(s->hlua); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7067 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7068 | } |
| 7069 | |
| 7070 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 7071 | 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] | 7072 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7073 | /* Create and and push object stream in the stack. */ |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 7074 | if (!hlua_txn_new(s->hlua->T, s, px, dir, hflags)) { |
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 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 7080 | s->hlua->nargs = 1; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7081 | |
| 7082 | /* push keywords in the stack. */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7083 | for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 7084 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 7085 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7086 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7087 | RESET_SAFE_LJMP(s->hlua); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7088 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7089 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 7090 | lua_pushstring(s->hlua->T, *arg); |
| 7091 | s->hlua->nargs++; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7092 | } |
| 7093 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7094 | /* Now the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7095 | RESET_SAFE_LJMP(s->hlua); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7096 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 7097 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 7098 | s->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7099 | } |
| 7100 | |
| 7101 | /* Execute the function. */ |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 7102 | switch (hlua_ctx_resume(s->hlua, !(flags & ACT_OPT_FINAL))) { |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7103 | /* finished. */ |
| 7104 | case HLUA_E_OK: |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7105 | /* Catch the return value */ |
| 7106 | if (lua_gettop(s->hlua->T) > 0) |
| 7107 | act_ret = lua_tointeger(s->hlua->T, -1); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7108 | |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 7109 | /* Set timeout in the required channel. */ |
Christopher Faulet | 498c483 | 2020-07-28 11:59:58 +0200 | [diff] [blame] | 7110 | if (act_ret == ACT_RET_YIELD) { |
| 7111 | if (flags & ACT_OPT_FINAL) |
| 7112 | goto err_yield; |
| 7113 | |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 7114 | if (dir == SMP_OPT_DIR_REQ) |
| 7115 | s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp), |
| 7116 | s->hlua->wake_time); |
| 7117 | else |
| 7118 | s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp), |
| 7119 | s->hlua->wake_time); |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 7120 | } |
| 7121 | goto end; |
| 7122 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7123 | /* yield. */ |
| 7124 | case HLUA_E_AGAIN: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 7125 | /* Set timeout in the required channel. */ |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 7126 | if (dir == SMP_OPT_DIR_REQ) |
| 7127 | s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp), |
| 7128 | s->hlua->wake_time); |
| 7129 | else |
| 7130 | s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp), |
| 7131 | s->hlua->wake_time); |
| 7132 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7133 | /* Some actions can be wake up when a "write" event |
| 7134 | * is detected on a response channel. This is useful |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7135 | * only for actions targeted on the requests. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7136 | */ |
Christopher Faulet | 51fa358 | 2019-07-26 14:54:52 +0200 | [diff] [blame] | 7137 | if (HLUA_IS_WAKERESWR(s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 7138 | s->res.flags |= CF_WAKE_WRITE; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 7139 | if (HLUA_IS_WAKEREQWR(s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 7140 | s->req.flags |= CF_WAKE_WRITE; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7141 | act_ret = ACT_RET_YIELD; |
| 7142 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7143 | |
| 7144 | /* finished with error. */ |
| 7145 | case HLUA_E_ERRMSG: |
| 7146 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 7147 | SEND_ERR(px, "Lua function '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7148 | rule->arg.hlua_rule->fcn->name, lua_tostring(s->hlua->T, -1)); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 7149 | lua_pop(s->hlua->T, 1); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7150 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7151 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7152 | case HLUA_E_ETMOUT: |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7153 | 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] | 7154 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7155 | |
| 7156 | case HLUA_E_NOMEM: |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7157 | 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] | 7158 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7159 | |
| 7160 | case HLUA_E_YIELD: |
Christopher Faulet | 498c483 | 2020-07-28 11:59:58 +0200 | [diff] [blame] | 7161 | err_yield: |
| 7162 | act_ret = ACT_RET_CONT; |
Aurelien DARRAGON | 602c4af | 2023-08-31 21:45:21 +0200 | [diff] [blame] | 7163 | SEND_ERR(px, "Lua function '%s': yield not allowed.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7164 | 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 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7167 | case HLUA_E_ERR: |
| 7168 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 7169 | SEND_ERR(px, "Lua function '%s' return an unknown error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7170 | rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7171 | |
| 7172 | default: |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7173 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7174 | } |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7175 | |
| 7176 | end: |
Christopher Faulet | 2361fd9 | 2020-07-30 10:40:58 +0200 | [diff] [blame] | 7177 | if (act_ret != ACT_RET_YIELD && s->hlua) |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 7178 | s->hlua->wake_time = TICK_ETERNITY; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7179 | return act_ret; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7180 | } |
| 7181 | |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 7182 | 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] | 7183 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 7184 | struct appctx *ctx = context; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7185 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7186 | appctx_wakeup(ctx); |
Willy Tarreau | d958741 | 2017-08-23 16:07:33 +0200 | [diff] [blame] | 7187 | t->expire = TICK_ETERNITY; |
Willy Tarreau | d1aa41f | 2017-07-21 16:41:56 +0200 | [diff] [blame] | 7188 | return t; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7189 | } |
| 7190 | |
| 7191 | static int hlua_applet_tcp_init(struct appctx *ctx, struct proxy *px, struct stream *strm) |
| 7192 | { |
| 7193 | struct stream_interface *si = ctx->owner; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7194 | struct hlua *hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7195 | struct task *task; |
| 7196 | char **arg; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7197 | const char *error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7198 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7199 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7200 | if (!hlua) { |
| 7201 | SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7202 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7203 | return 0; |
| 7204 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7205 | HLUA_INIT(hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7206 | ctx->ctx.hlua_apptcp.hlua = hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7207 | ctx->ctx.hlua_apptcp.flags = 0; |
| 7208 | |
| 7209 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 7210 | task = task_new(tid_bit); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7211 | if (!task) { |
| 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 | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7214 | return 0; |
| 7215 | } |
| 7216 | task->nice = 0; |
| 7217 | task->context = ctx; |
| 7218 | task->process = hlua_applet_wakeup; |
| 7219 | ctx->ctx.hlua_apptcp.task = task; |
| 7220 | |
| 7221 | /* In the execution wrappers linked with a stream, the |
| 7222 | * Lua context can be not initialized. This behavior |
| 7223 | * permits to save performances because a systematic |
| 7224 | * Lua initialization cause 5% performances loss. |
| 7225 | */ |
Aurelien DARRAGON | 5483bde | 2023-03-21 14:02:16 +0100 | [diff] [blame] | 7226 | 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] | 7227 | 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] | 7228 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7229 | return 0; |
| 7230 | } |
| 7231 | |
| 7232 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 7233 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7234 | |
| 7235 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7236 | if (!SET_SAFE_LJMP(hlua)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7237 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 7238 | error = lua_tostring(hlua->T, -1); |
| 7239 | else |
| 7240 | error = "critical error"; |
| 7241 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7242 | ctx->rule->arg.hlua_rule->fcn->name, error); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7243 | return 0; |
| 7244 | } |
| 7245 | |
| 7246 | /* Check stack available size. */ |
| 7247 | if (!lua_checkstack(hlua->T, 1)) { |
| 7248 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7249 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7250 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7251 | return 0; |
| 7252 | } |
| 7253 | |
| 7254 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 7255 | 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] | 7256 | |
| 7257 | /* Create and and push object stream in the stack. */ |
| 7258 | if (!hlua_applet_tcp_new(hlua->T, ctx)) { |
| 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 | hlua->nargs = 1; |
| 7265 | |
| 7266 | /* push keywords in the stack. */ |
| 7267 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 7268 | if (!lua_checkstack(hlua->T, 1)) { |
| 7269 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7270 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7271 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7272 | return 0; |
| 7273 | } |
| 7274 | lua_pushstring(hlua->T, *arg); |
| 7275 | hlua->nargs++; |
| 7276 | } |
| 7277 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7278 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7279 | |
| 7280 | /* Wakeup the applet ASAP. */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 7281 | si_cant_get(si); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 7282 | si_rx_endp_more(si); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7283 | |
| 7284 | return 1; |
| 7285 | } |
| 7286 | |
Willy Tarreau | 60409db | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 7287 | void hlua_applet_tcp_fct(struct appctx *ctx) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7288 | { |
| 7289 | struct stream_interface *si = ctx->owner; |
| 7290 | struct stream *strm = si_strm(si); |
| 7291 | struct channel *res = si_ic(si); |
| 7292 | struct act_rule *rule = ctx->rule; |
| 7293 | struct proxy *px = strm->be; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7294 | struct hlua *hlua = ctx->ctx.hlua_apptcp.hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7295 | |
| 7296 | /* The applet execution is already done. */ |
Olivier Houchard | 594c8c5 | 2018-08-28 14:41:31 +0200 | [diff] [blame] | 7297 | if (ctx->ctx.hlua_apptcp.flags & APPLET_DONE) { |
| 7298 | /* eat the whole request */ |
| 7299 | co_skip(si_oc(si), co_data(si_oc(si))); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7300 | return; |
Olivier Houchard | 594c8c5 | 2018-08-28 14:41:31 +0200 | [diff] [blame] | 7301 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7302 | |
| 7303 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 7304 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 7305 | return; |
| 7306 | |
| 7307 | /* Execute the function. */ |
| 7308 | switch (hlua_ctx_resume(hlua, 1)) { |
| 7309 | /* finished. */ |
| 7310 | case HLUA_E_OK: |
| 7311 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 7312 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7313 | /* eat the whole request */ |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 7314 | co_skip(si_oc(si), co_data(si_oc(si))); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7315 | res->flags |= CF_READ_NULL; |
| 7316 | si_shutr(si); |
| 7317 | return; |
| 7318 | |
| 7319 | /* yield. */ |
| 7320 | case HLUA_E_AGAIN: |
Thierry Fournier | 0164f20 | 2016-02-20 17:47:43 +0100 | [diff] [blame] | 7321 | if (hlua->wake_time != TICK_ETERNITY) |
| 7322 | task_schedule(ctx->ctx.hlua_apptcp.task, hlua->wake_time); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7323 | return; |
| 7324 | |
| 7325 | /* finished with error. */ |
| 7326 | case HLUA_E_ERRMSG: |
| 7327 | /* Display log. */ |
| 7328 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7329 | rule->arg.hlua_rule->fcn->name, lua_tostring(hlua->T, -1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7330 | lua_pop(hlua->T, 1); |
| 7331 | goto error; |
| 7332 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7333 | case HLUA_E_ETMOUT: |
| 7334 | SEND_ERR(px, "Lua applet tcp '%s': execution timeout.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7335 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7336 | goto error; |
| 7337 | |
| 7338 | case HLUA_E_NOMEM: |
| 7339 | SEND_ERR(px, "Lua applet tcp '%s': out of memory error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7340 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7341 | goto error; |
| 7342 | |
| 7343 | case HLUA_E_YIELD: /* unexpected */ |
| 7344 | SEND_ERR(px, "Lua applet tcp '%s': yield not allowed.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7345 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7346 | goto error; |
| 7347 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7348 | case HLUA_E_ERR: |
| 7349 | /* Display log. */ |
| 7350 | SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7351 | rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7352 | goto error; |
| 7353 | |
| 7354 | default: |
| 7355 | goto error; |
| 7356 | } |
| 7357 | |
| 7358 | error: |
| 7359 | |
| 7360 | /* For all other cases, just close the stream. */ |
| 7361 | si_shutw(si); |
| 7362 | si_shutr(si); |
| 7363 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 7364 | } |
| 7365 | |
| 7366 | static void hlua_applet_tcp_release(struct appctx *ctx) |
| 7367 | { |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 7368 | task_destroy(ctx->ctx.hlua_apptcp.task); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7369 | ctx->ctx.hlua_apptcp.task = NULL; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7370 | hlua_ctx_destroy(ctx->ctx.hlua_apptcp.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7371 | ctx->ctx.hlua_apptcp.hlua = NULL; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7372 | } |
| 7373 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7374 | /* The function returns 1 if the initialisation is complete, 0 if |
| 7375 | * an errors occurs and -1 if more data are required for initializing |
| 7376 | * the applet. |
| 7377 | */ |
| 7378 | static int hlua_applet_http_init(struct appctx *ctx, struct proxy *px, struct stream *strm) |
| 7379 | { |
| 7380 | struct stream_interface *si = ctx->owner; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7381 | struct http_txn *txn; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7382 | struct hlua *hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7383 | char **arg; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7384 | struct task *task; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7385 | const char *error; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7386 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7387 | txn = strm->txn; |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7388 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7389 | if (!hlua) { |
| 7390 | SEND_ERR(px, "Lua applet http '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7391 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7392 | return 0; |
| 7393 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7394 | HLUA_INIT(hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7395 | ctx->ctx.hlua_apphttp.hlua = hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7396 | ctx->ctx.hlua_apphttp.left_bytes = -1; |
| 7397 | ctx->ctx.hlua_apphttp.flags = 0; |
| 7398 | |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 7399 | if (txn->req.flags & HTTP_MSGF_VER_11) |
| 7400 | ctx->ctx.hlua_apphttp.flags |= APPLET_HTTP11; |
| 7401 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7402 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 7403 | task = task_new(tid_bit); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7404 | if (!task) { |
| 7405 | SEND_ERR(px, "Lua applet http '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7406 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7407 | return 0; |
| 7408 | } |
| 7409 | task->nice = 0; |
| 7410 | task->context = ctx; |
| 7411 | task->process = hlua_applet_wakeup; |
| 7412 | ctx->ctx.hlua_apphttp.task = task; |
| 7413 | |
| 7414 | /* In the execution wrappers linked with a stream, the |
| 7415 | * Lua context can be not initialized. This behavior |
| 7416 | * permits to save performances because a systematic |
| 7417 | * Lua initialization cause 5% performances loss. |
| 7418 | */ |
Aurelien DARRAGON | 5483bde | 2023-03-21 14:02:16 +0100 | [diff] [blame] | 7419 | 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] | 7420 | 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] | 7421 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7422 | return 0; |
| 7423 | } |
| 7424 | |
| 7425 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 7426 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7427 | |
| 7428 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7429 | if (!SET_SAFE_LJMP(hlua)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7430 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 7431 | error = lua_tostring(hlua->T, -1); |
| 7432 | else |
| 7433 | error = "critical error"; |
| 7434 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7435 | ctx->rule->arg.hlua_rule->fcn->name, error); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7436 | return 0; |
| 7437 | } |
| 7438 | |
| 7439 | /* Check stack available size. */ |
| 7440 | if (!lua_checkstack(hlua->T, 1)) { |
| 7441 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7442 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7443 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7444 | return 0; |
| 7445 | } |
| 7446 | |
| 7447 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 7448 | 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] | 7449 | |
| 7450 | /* Create and and push object stream in the stack. */ |
| 7451 | if (!hlua_applet_http_new(hlua->T, ctx)) { |
| 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 | hlua->nargs = 1; |
| 7458 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7459 | /* push keywords in the stack. */ |
| 7460 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 7461 | if (!lua_checkstack(hlua->T, 1)) { |
| 7462 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7463 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7464 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7465 | return 0; |
| 7466 | } |
| 7467 | lua_pushstring(hlua->T, *arg); |
| 7468 | hlua->nargs++; |
| 7469 | } |
| 7470 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7471 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7472 | |
| 7473 | /* Wakeup the applet when data is ready for read. */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 7474 | si_cant_get(si); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7475 | |
| 7476 | return 1; |
| 7477 | } |
| 7478 | |
Willy Tarreau | 60409db | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 7479 | void hlua_applet_http_fct(struct appctx *ctx) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7480 | { |
| 7481 | struct stream_interface *si = ctx->owner; |
| 7482 | struct stream *strm = si_strm(si); |
| 7483 | struct channel *req = si_oc(si); |
| 7484 | struct channel *res = si_ic(si); |
| 7485 | struct act_rule *rule = ctx->rule; |
| 7486 | struct proxy *px = strm->be; |
| 7487 | struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua; |
| 7488 | struct htx *req_htx, *res_htx; |
| 7489 | |
| 7490 | res_htx = htx_from_buf(&res->buf); |
| 7491 | |
| 7492 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 7493 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 7494 | goto out; |
| 7495 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 7496 | /* Check if the input buffer is available. */ |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7497 | if (!b_size(&res->buf)) { |
| 7498 | si_rx_room_blk(si); |
| 7499 | goto out; |
| 7500 | } |
| 7501 | /* check that the output is not closed */ |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7502 | if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR)) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7503 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 7504 | |
| 7505 | /* Set the currently running flag. */ |
| 7506 | if (!HLUA_IS_RUNNING(hlua) && |
| 7507 | !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
Christopher Faulet | bd878d2 | 2021-04-28 10:50:21 +0200 | [diff] [blame] | 7508 | if (!co_data(req)) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7509 | si_cant_get(si); |
| 7510 | goto out; |
| 7511 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7512 | } |
| 7513 | |
| 7514 | /* Executes The applet if it is not done. */ |
| 7515 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
| 7516 | |
| 7517 | /* Execute the function. */ |
| 7518 | switch (hlua_ctx_resume(hlua, 1)) { |
| 7519 | /* finished. */ |
| 7520 | case HLUA_E_OK: |
| 7521 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 7522 | break; |
| 7523 | |
| 7524 | /* yield. */ |
| 7525 | case HLUA_E_AGAIN: |
| 7526 | if (hlua->wake_time != TICK_ETERNITY) |
| 7527 | task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time); |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7528 | goto out; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7529 | |
| 7530 | /* finished with error. */ |
| 7531 | case HLUA_E_ERRMSG: |
| 7532 | /* Display log. */ |
| 7533 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7534 | rule->arg.hlua_rule->fcn->name, lua_tostring(hlua->T, -1)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7535 | lua_pop(hlua->T, 1); |
| 7536 | goto error; |
| 7537 | |
| 7538 | case HLUA_E_ETMOUT: |
| 7539 | SEND_ERR(px, "Lua applet http '%s': execution timeout.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7540 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7541 | goto error; |
| 7542 | |
| 7543 | case HLUA_E_NOMEM: |
| 7544 | SEND_ERR(px, "Lua applet http '%s': out of memory error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7545 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7546 | goto error; |
| 7547 | |
| 7548 | case HLUA_E_YIELD: /* unexpected */ |
| 7549 | SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7550 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7551 | goto error; |
| 7552 | |
| 7553 | case HLUA_E_ERR: |
| 7554 | /* Display log. */ |
| 7555 | SEND_ERR(px, "Lua applet http '%s' return an unknown 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 | default: |
| 7560 | goto error; |
| 7561 | } |
| 7562 | } |
| 7563 | |
| 7564 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7565 | if (ctx->ctx.hlua_apphttp.flags & APPLET_RSP_SENT) |
| 7566 | goto done; |
| 7567 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7568 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) |
| 7569 | goto error; |
| 7570 | |
Christopher Faulet | 868b3b1 | 2022-04-07 10:07:18 +0200 | [diff] [blame] | 7571 | /* no more data are expected. If the response buffer is empty |
| 7572 | * for a chunked message, be sure to add something (EOT block in |
| 7573 | * this case) to have something to send. It is important to be |
| 7574 | * sure the EOM flags will be handled by the endpoint. |
| 7575 | */ |
| 7576 | if (htx_is_empty(res_htx) && (strm->txn->rsp.flags & (HTTP_MSGF_XFER_LEN|HTTP_MSGF_CNT_LEN)) == HTTP_MSGF_XFER_LEN) { |
| 7577 | if (!htx_add_endof(res_htx, HTX_BLK_EOT)) { |
| 7578 | si_rx_room_blk(si); |
| 7579 | goto out; |
| 7580 | } |
| 7581 | channel_add_input(res, 1); |
| 7582 | } |
| 7583 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 7584 | res_htx->flags |= HTX_FL_EOM; |
Christopher Faulet | 79c0fc7 | 2022-03-07 15:50:54 +0100 | [diff] [blame] | 7585 | res->flags |= CF_EOI; |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7586 | strm->txn->status = ctx->ctx.hlua_apphttp.status; |
| 7587 | ctx->ctx.hlua_apphttp.flags |= APPLET_RSP_SENT; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7588 | } |
| 7589 | |
| 7590 | done: |
| 7591 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7592 | if (!(res->flags & CF_SHUTR)) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7593 | res->flags |= CF_READ_NULL; |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7594 | si_shutr(si); |
| 7595 | } |
| 7596 | |
| 7597 | /* eat the whole request */ |
| 7598 | if (co_data(req)) { |
| 7599 | req_htx = htx_from_buf(&req->buf); |
| 7600 | co_htx_skip(req, req_htx, co_data(req)); |
| 7601 | htx_to_buf(req_htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7602 | } |
| 7603 | } |
| 7604 | |
| 7605 | out: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7606 | htx_to_buf(res_htx, &res->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7607 | return; |
| 7608 | |
| 7609 | error: |
| 7610 | |
| 7611 | /* If we are in HTTP mode, and we are not send any |
| 7612 | * data, return a 500 server error in best effort: |
| 7613 | * if there is no room available in the buffer, |
| 7614 | * just close the connection. |
| 7615 | */ |
| 7616 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) { |
Christopher Faulet | f734638 | 2019-07-17 22:02:08 +0200 | [diff] [blame] | 7617 | struct buffer *err = &http_err_chunks[HTTP_ERR_500]; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7618 | |
| 7619 | channel_erase(res); |
| 7620 | res->buf.data = b_data(err); |
| 7621 | memcpy(res->buf.area, b_head(err), b_data(err)); |
| 7622 | res_htx = htx_from_buf(&res->buf); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 7623 | channel_add_input(res, res_htx->data); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7624 | } |
| 7625 | if (!(strm->flags & SF_ERR_MASK)) |
| 7626 | strm->flags |= SF_ERR_RESOURCE; |
| 7627 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 7628 | goto done; |
| 7629 | } |
| 7630 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7631 | static void hlua_applet_http_release(struct appctx *ctx) |
| 7632 | { |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 7633 | task_destroy(ctx->ctx.hlua_apphttp.task); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7634 | ctx->ctx.hlua_apphttp.task = NULL; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7635 | hlua_ctx_destroy(ctx->ctx.hlua_apphttp.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7636 | ctx->ctx.hlua_apphttp.hlua = NULL; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7637 | } |
| 7638 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7639 | /* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 7640 | * success case, else return ACT_RET_PRS_ERR. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7641 | * |
| 7642 | * This function can fail with an abort() due to an Lua critical error. |
| 7643 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 7644 | * tolerated. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7645 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7646 | static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px, |
| 7647 | struct act_rule *rule, char **err) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7648 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7649 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7650 | int i; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7651 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7652 | /* Memory for the rule. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7653 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7654 | if (!rule->arg.hlua_rule) { |
| 7655 | memprintf(err, "out of memory error"); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 7656 | goto error; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7657 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7658 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7659 | /* Memory for arguments. */ |
Tim Duesterhus | e52b6e5 | 2020-09-12 20:26:43 +0200 | [diff] [blame] | 7660 | rule->arg.hlua_rule->args = calloc(fcn->nargs + 1, |
| 7661 | sizeof(*rule->arg.hlua_rule->args)); |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7662 | if (!rule->arg.hlua_rule->args) { |
| 7663 | memprintf(err, "out of memory error"); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 7664 | goto error; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7665 | } |
| 7666 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7667 | /* Reference the Lua function and store the reference. */ |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7668 | rule->arg.hlua_rule->fcn = fcn; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7669 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7670 | /* Expect some arguments */ |
| 7671 | for (i = 0; i < fcn->nargs; i++) { |
Thierry FOURNIER | 1725c2e | 2019-01-06 19:38:49 +0100 | [diff] [blame] | 7672 | if (*args[*cur_arg] == '\0') { |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7673 | memprintf(err, "expect %d arguments", fcn->nargs); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 7674 | goto error; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7675 | } |
Thierry FOURNIER | 1725c2e | 2019-01-06 19:38:49 +0100 | [diff] [blame] | 7676 | rule->arg.hlua_rule->args[i] = strdup(args[*cur_arg]); |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7677 | if (!rule->arg.hlua_rule->args[i]) { |
| 7678 | memprintf(err, "out of memory error"); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 7679 | goto error; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7680 | } |
| 7681 | (*cur_arg)++; |
| 7682 | } |
| 7683 | rule->arg.hlua_rule->args[i] = NULL; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7684 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 7685 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7686 | rule->action_ptr = hlua_action; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 7687 | return ACT_RET_PRS_OK; |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 7688 | |
| 7689 | error: |
| 7690 | if (rule->arg.hlua_rule) { |
| 7691 | if (rule->arg.hlua_rule->args) { |
| 7692 | for (i = 0; i < fcn->nargs; i++) |
| 7693 | ha_free(&rule->arg.hlua_rule->args[i]); |
| 7694 | ha_free(&rule->arg.hlua_rule->args); |
| 7695 | } |
| 7696 | ha_free(&rule->arg.hlua_rule); |
| 7697 | } |
| 7698 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7699 | } |
| 7700 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7701 | static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px, |
| 7702 | struct act_rule *rule, char **err) |
| 7703 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7704 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7705 | |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 7706 | /* HTTP applets are forbidden in tcp-request rules. |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 7707 | * HTTP applet request requires everything initialized by |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 7708 | * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER). |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 7709 | * The applet will be immediately initialized, but its before |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 7710 | * the call of this analyzer. |
| 7711 | */ |
| 7712 | if (rule->from != ACT_F_HTTP_REQ) { |
| 7713 | memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets"); |
| 7714 | return ACT_RET_PRS_ERR; |
| 7715 | } |
| 7716 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7717 | /* Memory for the rule. */ |
| 7718 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 7719 | if (!rule->arg.hlua_rule) { |
| 7720 | memprintf(err, "out of memory error"); |
| 7721 | return ACT_RET_PRS_ERR; |
| 7722 | } |
| 7723 | |
| 7724 | /* Reference the Lua function and store the reference. */ |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7725 | rule->arg.hlua_rule->fcn = fcn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7726 | |
| 7727 | /* TODO: later accept arguments. */ |
| 7728 | rule->arg.hlua_rule->args = NULL; |
| 7729 | |
| 7730 | /* Add applet pointer in the rule. */ |
| 7731 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 7732 | rule->applet.name = fcn->name; |
| 7733 | rule->applet.init = hlua_applet_http_init; |
| 7734 | rule->applet.fct = hlua_applet_http_fct; |
| 7735 | rule->applet.release = hlua_applet_http_release; |
| 7736 | rule->applet.timeout = hlua_timeout_applet; |
| 7737 | |
| 7738 | return ACT_RET_PRS_OK; |
| 7739 | } |
| 7740 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7741 | /* This function is an LUA binding used for registering |
| 7742 | * "sample-conv" functions. It expects a converter name used |
| 7743 | * in the haproxy configuration file, and an LUA function. |
| 7744 | */ |
| 7745 | __LJMP static int hlua_register_action(lua_State *L) |
| 7746 | { |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 7747 | struct action_kw_list *akl = NULL; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7748 | const char *name; |
| 7749 | int ref; |
| 7750 | int len; |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 7751 | struct hlua_function *fcn = NULL; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7752 | int nargs; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 7753 | struct buffer *trash; |
| 7754 | struct action_kw *akw; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7755 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7756 | /* Initialise the number of expected arguments at 0. */ |
| 7757 | nargs = 0; |
| 7758 | |
| 7759 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 7760 | 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] | 7761 | |
Aurelien DARRAGON | 8985ab8 | 2023-02-24 09:43:54 +0100 | [diff] [blame] | 7762 | if (hlua_gethlua(L)) { |
| 7763 | /* runtime processing */ |
| 7764 | WILL_LJMP(luaL_error(L, "register_action: not available outside of body context")); |
| 7765 | } |
| 7766 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7767 | /* First argument : converter name. */ |
| 7768 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 7769 | |
| 7770 | /* Second argument : environment. */ |
| 7771 | if (lua_type(L, 2) != LUA_TTABLE) |
| 7772 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 7773 | |
| 7774 | /* Third argument : lua function. */ |
| 7775 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 7776 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7777 | /* 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] | 7778 | if (lua_gettop(L) >= 4) |
| 7779 | nargs = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 7780 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7781 | /* browse the second argument as an array. */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7782 | lua_pushnil(L); |
| 7783 | while (lua_next(L, 2) != 0) { |
| 7784 | if (lua_type(L, -1) != LUA_TSTRING) |
| 7785 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 7786 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 7787 | /* Check if action exists */ |
| 7788 | trash = get_trash_chunk(); |
| 7789 | chunk_printf(trash, "lua.%s", name); |
| 7790 | if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) { |
| 7791 | akw = tcp_req_cont_action(trash->area); |
| 7792 | } else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) { |
| 7793 | akw = tcp_res_cont_action(trash->area); |
| 7794 | } else if (strcmp(lua_tostring(L, -1), "http-req") == 0) { |
| 7795 | akw = action_http_req_custom(trash->area); |
| 7796 | } else if (strcmp(lua_tostring(L, -1), "http-res") == 0) { |
| 7797 | akw = action_http_res_custom(trash->area); |
| 7798 | } else { |
| 7799 | akw = NULL; |
| 7800 | } |
| 7801 | if (akw != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 7802 | fcn = akw->private; |
| 7803 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 7804 | ha_warning("Trying to register action 'lua.%s' more than once. " |
| 7805 | "This will become a hard error in version 2.5.\n", name); |
| 7806 | } |
| 7807 | fcn->function_ref[hlua_state_id] = ref; |
| 7808 | |
| 7809 | /* pop the environment string. */ |
| 7810 | lua_pop(L, 1); |
| 7811 | continue; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 7812 | } |
| 7813 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7814 | /* Check required environment. Only accepted "http" or "tcp". */ |
| 7815 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7816 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7817 | if (!akl) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 7818 | goto alloc_error;; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 7819 | fcn = new_hlua_function(); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7820 | if (!fcn) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 7821 | goto alloc_error; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7822 | |
| 7823 | /* Fill fcn. */ |
| 7824 | fcn->name = strdup(name); |
| 7825 | if (!fcn->name) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 7826 | goto alloc_error; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 7827 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7828 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 7829 | /* Set the expected number of arguments. */ |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7830 | fcn->nargs = nargs; |
| 7831 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7832 | /* List head */ |
| 7833 | akl->list.n = akl->list.p = NULL; |
| 7834 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7835 | /* action keyword. */ |
| 7836 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7837 | akl->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7838 | if (!akl->kw[0].kw) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 7839 | goto alloc_error; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7840 | |
| 7841 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 7842 | |
Amaury Denoyelle | e4a617c | 2021-05-06 15:33:09 +0200 | [diff] [blame] | 7843 | akl->kw[0].flags = 0; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7844 | akl->kw[0].private = fcn; |
| 7845 | akl->kw[0].parse = action_register_lua; |
| 7846 | |
| 7847 | /* select the action registering point. */ |
| 7848 | if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) |
| 7849 | tcp_req_cont_keywords_register(akl); |
| 7850 | else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) |
| 7851 | tcp_res_cont_keywords_register(akl); |
| 7852 | else if (strcmp(lua_tostring(L, -1), "http-req") == 0) |
| 7853 | http_req_keywords_register(akl); |
| 7854 | else if (strcmp(lua_tostring(L, -1), "http-res") == 0) |
| 7855 | http_res_keywords_register(akl); |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 7856 | else { |
| 7857 | release_hlua_function(fcn); |
| 7858 | if (akl) |
| 7859 | ha_free((char **)&(akl->kw[0].kw)); |
| 7860 | ha_free(&akl); |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7861 | WILL_LJMP(luaL_error(L, "Lua action environment '%s' is unknown. " |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7862 | "'tcp-req', 'tcp-res', 'http-req' or 'http-res' " |
| 7863 | "are expected.", lua_tostring(L, -1))); |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 7864 | } |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7865 | |
| 7866 | /* pop the environment string. */ |
| 7867 | lua_pop(L, 1); |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 7868 | |
| 7869 | /* reset for next loop */ |
| 7870 | akl = NULL; |
| 7871 | fcn = NULL; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7872 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7873 | return ACT_RET_PRS_OK; |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 7874 | |
| 7875 | alloc_error: |
| 7876 | release_hlua_function(fcn); |
| 7877 | ha_free(&akl); |
| 7878 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 7879 | return 0; /* Never reached */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7880 | } |
| 7881 | |
| 7882 | static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px, |
| 7883 | struct act_rule *rule, char **err) |
| 7884 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7885 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7886 | |
Christopher Faulet | 280f85b | 2019-07-15 15:02:04 +0200 | [diff] [blame] | 7887 | if (px->mode == PR_MODE_HTTP) { |
| 7888 | memprintf(err, "Lua TCP services cannot be used on HTTP proxies"); |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 7889 | return ACT_RET_PRS_ERR; |
| 7890 | } |
| 7891 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7892 | /* Memory for the rule. */ |
| 7893 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 7894 | if (!rule->arg.hlua_rule) { |
| 7895 | memprintf(err, "out of memory error"); |
| 7896 | return ACT_RET_PRS_ERR; |
| 7897 | } |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7898 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7899 | /* Reference the Lua function and store the reference. */ |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7900 | rule->arg.hlua_rule->fcn = fcn; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7901 | |
| 7902 | /* TODO: later accept arguments. */ |
| 7903 | rule->arg.hlua_rule->args = NULL; |
| 7904 | |
| 7905 | /* Add applet pointer in the rule. */ |
| 7906 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 7907 | rule->applet.name = fcn->name; |
| 7908 | rule->applet.init = hlua_applet_tcp_init; |
| 7909 | rule->applet.fct = hlua_applet_tcp_fct; |
| 7910 | rule->applet.release = hlua_applet_tcp_release; |
| 7911 | rule->applet.timeout = hlua_timeout_applet; |
| 7912 | |
| 7913 | return 0; |
| 7914 | } |
| 7915 | |
| 7916 | /* This function is an LUA binding used for registering |
| 7917 | * "sample-conv" functions. It expects a converter name used |
| 7918 | * in the haproxy configuration file, and an LUA function. |
| 7919 | */ |
| 7920 | __LJMP static int hlua_register_service(lua_State *L) |
| 7921 | { |
| 7922 | struct action_kw_list *akl; |
| 7923 | const char *name; |
| 7924 | const char *env; |
| 7925 | int ref; |
| 7926 | int len; |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 7927 | struct hlua_function *fcn = NULL; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 7928 | struct buffer *trash; |
| 7929 | struct action_kw *akw; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7930 | |
| 7931 | MAY_LJMP(check_args(L, 3, "register_service")); |
| 7932 | |
Aurelien DARRAGON | 8985ab8 | 2023-02-24 09:43:54 +0100 | [diff] [blame] | 7933 | if (hlua_gethlua(L)) { |
| 7934 | /* runtime processing */ |
| 7935 | WILL_LJMP(luaL_error(L, "register_service: not available outside of body context")); |
| 7936 | } |
| 7937 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7938 | /* First argument : converter name. */ |
| 7939 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 7940 | |
| 7941 | /* Second argument : environment. */ |
| 7942 | env = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7943 | |
| 7944 | /* Third argument : lua function. */ |
| 7945 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 7946 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 7947 | /* Check for service already registered */ |
| 7948 | trash = get_trash_chunk(); |
| 7949 | chunk_printf(trash, "lua.%s", name); |
| 7950 | akw = service_find(trash->area); |
| 7951 | if (akw != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 7952 | fcn = akw->private; |
| 7953 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 7954 | ha_warning("Trying to register service 'lua.%s' more than once. " |
| 7955 | "This will become a hard error in version 2.5.\n", name); |
| 7956 | } |
| 7957 | fcn->function_ref[hlua_state_id] = ref; |
| 7958 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 7959 | } |
| 7960 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7961 | /* Allocate and fill the sample fetch keyword struct. */ |
| 7962 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
| 7963 | if (!akl) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 7964 | goto alloc_error; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 7965 | fcn = new_hlua_function(); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7966 | if (!fcn) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 7967 | goto alloc_error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7968 | |
| 7969 | /* Fill fcn. */ |
| 7970 | len = strlen("<lua.>") + strlen(name) + 1; |
| 7971 | fcn->name = calloc(1, len); |
| 7972 | if (!fcn->name) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 7973 | goto alloc_error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7974 | snprintf((char *)fcn->name, len, "<lua.%s>", name); |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 7975 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7976 | |
| 7977 | /* List head */ |
| 7978 | akl->list.n = akl->list.p = NULL; |
| 7979 | |
| 7980 | /* converter keyword. */ |
| 7981 | len = strlen("lua.") + strlen(name) + 1; |
| 7982 | akl->kw[0].kw = calloc(1, len); |
| 7983 | if (!akl->kw[0].kw) |
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 | |
| 7986 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 7987 | |
Thierry FOURNIER / OZON.IO | 02564fd | 2016-11-12 11:07:05 +0100 | [diff] [blame] | 7988 | /* Check required environment. Only accepted "http" or "tcp". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7989 | if (strcmp(env, "tcp") == 0) |
| 7990 | akl->kw[0].parse = action_register_service_tcp; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7991 | else if (strcmp(env, "http") == 0) |
| 7992 | akl->kw[0].parse = action_register_service_http; |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 7993 | else { |
| 7994 | release_hlua_function(fcn); |
| 7995 | if (akl) |
| 7996 | ha_free((char **)&(akl->kw[0].kw)); |
| 7997 | ha_free(&akl); |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7998 | WILL_LJMP(luaL_error(L, "Lua service environment '%s' is unknown. " |
Eric Salama | fe7456f | 2017-12-21 14:30:07 +0100 | [diff] [blame] | 7999 | "'tcp' or 'http' are expected.", env)); |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 8000 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8001 | |
Amaury Denoyelle | e4a617c | 2021-05-06 15:33:09 +0200 | [diff] [blame] | 8002 | akl->kw[0].flags = 0; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8003 | akl->kw[0].private = fcn; |
| 8004 | |
| 8005 | /* End of array. */ |
| 8006 | memset(&akl->kw[1], 0, sizeof(*akl->kw)); |
| 8007 | |
| 8008 | /* Register this new converter */ |
| 8009 | service_keywords_register(akl); |
| 8010 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 8011 | return 0; |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 8012 | |
| 8013 | alloc_error: |
| 8014 | release_hlua_function(fcn); |
| 8015 | ha_free(&akl); |
| 8016 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 8017 | return 0; /* Never reached */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 8018 | } |
| 8019 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8020 | /* This function initialises Lua cli handler. It copies the |
| 8021 | * arguments in the Lua stack and create channel IO objects. |
| 8022 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 8023 | 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] | 8024 | { |
| 8025 | struct hlua *hlua; |
| 8026 | struct hlua_function *fcn; |
| 8027 | int i; |
| 8028 | const char *error; |
| 8029 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8030 | fcn = private; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 8031 | appctx->ctx.hlua_cli.fcn = private; |
| 8032 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 8033 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 8034 | if (!hlua) { |
| 8035 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 8036 | return 1; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 8037 | } |
| 8038 | HLUA_INIT(hlua); |
| 8039 | appctx->ctx.hlua_cli.hlua = hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8040 | |
| 8041 | /* Create task used by signal to wakeup applets. |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8042 | * 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] | 8043 | * applet_http. It is absolutely compatible. |
| 8044 | */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 8045 | appctx->ctx.hlua_cli.task = task_new(tid_bit); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8046 | if (!appctx->ctx.hlua_cli.task) { |
Thierry FOURNIER | ffbf569 | 2016-12-16 11:14:06 +0100 | [diff] [blame] | 8047 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 8048 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8049 | } |
| 8050 | appctx->ctx.hlua_cli.task->nice = 0; |
| 8051 | appctx->ctx.hlua_cli.task->context = appctx; |
| 8052 | appctx->ctx.hlua_cli.task->process = hlua_applet_wakeup; |
| 8053 | |
| 8054 | /* Initialises the Lua context */ |
Aurelien DARRAGON | 5483bde | 2023-03-21 14:02:16 +0100 | [diff] [blame] | 8055 | 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] | 8056 | 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] | 8057 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8058 | } |
| 8059 | |
| 8060 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8061 | if (!SET_SAFE_LJMP(hlua)) { |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8062 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 8063 | error = lua_tostring(hlua->T, -1); |
| 8064 | else |
| 8065 | error = "critical error"; |
| 8066 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error); |
| 8067 | goto error; |
| 8068 | } |
| 8069 | |
| 8070 | /* Check stack available size. */ |
| 8071 | if (!lua_checkstack(hlua->T, 2)) { |
| 8072 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 8073 | goto error; |
| 8074 | } |
| 8075 | |
| 8076 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8077 | 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] | 8078 | |
| 8079 | /* Once the arguments parsed, the CLI is like an AppletTCP, |
| 8080 | * so push AppletTCP in the stack. |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8081 | */ |
| 8082 | if (!hlua_applet_tcp_new(hlua->T, appctx)) { |
| 8083 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 8084 | goto error; |
| 8085 | } |
| 8086 | hlua->nargs = 1; |
| 8087 | |
| 8088 | /* push keywords in the stack. */ |
| 8089 | for (i = 0; *args[i]; i++) { |
| 8090 | /* Check stack available size. */ |
| 8091 | if (!lua_checkstack(hlua->T, 1)) { |
| 8092 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 8093 | goto error; |
| 8094 | } |
| 8095 | lua_pushstring(hlua->T, args[i]); |
| 8096 | hlua->nargs++; |
| 8097 | } |
| 8098 | |
| 8099 | /* We must initialize the execution timeouts. */ |
| 8100 | hlua->max_time = hlua_timeout_session; |
| 8101 | |
| 8102 | /* At this point the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8103 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8104 | |
| 8105 | /* It's ok */ |
| 8106 | return 0; |
| 8107 | |
| 8108 | /* It's not ok. */ |
| 8109 | error: |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8110 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8111 | hlua_ctx_destroy(hlua); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 8112 | appctx->ctx.hlua_cli.hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8113 | return 1; |
| 8114 | } |
| 8115 | |
| 8116 | static int hlua_cli_io_handler_fct(struct appctx *appctx) |
| 8117 | { |
| 8118 | struct hlua *hlua; |
| 8119 | struct stream_interface *si; |
| 8120 | struct hlua_function *fcn; |
| 8121 | |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 8122 | hlua = appctx->ctx.hlua_cli.hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8123 | si = appctx->owner; |
Willy Tarreau | 8ae4f75 | 2016-12-14 15:41:45 +0100 | [diff] [blame] | 8124 | fcn = appctx->ctx.hlua_cli.fcn; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8125 | |
| 8126 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 8127 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 8128 | return 1; |
| 8129 | |
| 8130 | /* Execute the function. */ |
| 8131 | switch (hlua_ctx_resume(hlua, 1)) { |
| 8132 | |
| 8133 | /* finished. */ |
| 8134 | case HLUA_E_OK: |
| 8135 | return 1; |
| 8136 | |
| 8137 | /* yield. */ |
| 8138 | case HLUA_E_AGAIN: |
| 8139 | /* We want write. */ |
| 8140 | if (HLUA_IS_WAKERESWR(hlua)) |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 8141 | si_rx_room_blk(si); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8142 | /* Set the timeout. */ |
| 8143 | if (hlua->wake_time != TICK_ETERNITY) |
| 8144 | task_schedule(hlua->task, hlua->wake_time); |
| 8145 | return 0; |
| 8146 | |
| 8147 | /* finished with error. */ |
| 8148 | case HLUA_E_ERRMSG: |
| 8149 | /* Display log. */ |
| 8150 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", |
| 8151 | fcn->name, lua_tostring(hlua->T, -1)); |
| 8152 | lua_pop(hlua->T, 1); |
| 8153 | return 1; |
| 8154 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 8155 | case HLUA_E_ETMOUT: |
| 8156 | SEND_ERR(NULL, "Lua converter '%s': execution timeout.\n", |
| 8157 | fcn->name); |
| 8158 | return 1; |
| 8159 | |
| 8160 | case HLUA_E_NOMEM: |
| 8161 | SEND_ERR(NULL, "Lua converter '%s': out of memory error.\n", |
| 8162 | fcn->name); |
| 8163 | return 1; |
| 8164 | |
| 8165 | case HLUA_E_YIELD: /* unexpected */ |
| 8166 | SEND_ERR(NULL, "Lua converter '%s': yield not allowed.\n", |
| 8167 | fcn->name); |
| 8168 | return 1; |
| 8169 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8170 | case HLUA_E_ERR: |
| 8171 | /* Display log. */ |
| 8172 | SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n", |
| 8173 | fcn->name); |
| 8174 | return 1; |
| 8175 | |
| 8176 | default: |
| 8177 | return 1; |
| 8178 | } |
| 8179 | |
| 8180 | return 1; |
| 8181 | } |
| 8182 | |
| 8183 | static void hlua_cli_io_release_fct(struct appctx *appctx) |
| 8184 | { |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 8185 | hlua_ctx_destroy(appctx->ctx.hlua_cli.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 8186 | appctx->ctx.hlua_cli.hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8187 | } |
| 8188 | |
| 8189 | /* This function is an LUA binding used for registering |
| 8190 | * new keywords in the cli. It expects a list of keywords |
| 8191 | * which are the "path". It is limited to 5 keywords. A |
| 8192 | * description of the command, a function to be executed |
| 8193 | * for the parsing and a function for io handlers. |
| 8194 | */ |
| 8195 | __LJMP static int hlua_register_cli(lua_State *L) |
| 8196 | { |
| 8197 | struct cli_kw_list *cli_kws; |
| 8198 | const char *message; |
| 8199 | int ref_io; |
| 8200 | int len; |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 8201 | struct hlua_function *fcn = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8202 | int index; |
| 8203 | int i; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8204 | struct buffer *trash; |
| 8205 | const char *kw[5]; |
| 8206 | struct cli_kw *cli_kw; |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 8207 | const char *errmsg; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8208 | |
| 8209 | MAY_LJMP(check_args(L, 3, "register_cli")); |
| 8210 | |
Aurelien DARRAGON | 8985ab8 | 2023-02-24 09:43:54 +0100 | [diff] [blame] | 8211 | if (hlua_gethlua(L)) { |
| 8212 | /* runtime processing */ |
| 8213 | WILL_LJMP(luaL_error(L, "register_cli: not available outside of body context")); |
| 8214 | } |
| 8215 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8216 | /* First argument : an array of maximum 5 keywords. */ |
| 8217 | if (!lua_istable(L, 1)) |
| 8218 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table")); |
| 8219 | |
| 8220 | /* Second argument : string with contextual message. */ |
| 8221 | message = MAY_LJMP(luaL_checkstring(L, 2)); |
| 8222 | |
| 8223 | /* Third and fourth argument : lua function. */ |
| 8224 | ref_io = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 8225 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8226 | /* Check for CLI service already registered */ |
| 8227 | trash = get_trash_chunk(); |
| 8228 | index = 0; |
| 8229 | lua_pushnil(L); |
| 8230 | memset(kw, 0, sizeof(kw)); |
| 8231 | while (lua_next(L, 1) != 0) { |
| 8232 | if (index >= CLI_PREFIX_KW_NB) |
| 8233 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries")); |
| 8234 | if (lua_type(L, -1) != LUA_TSTRING) |
| 8235 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings")); |
| 8236 | kw[index] = lua_tostring(L, -1); |
| 8237 | if (index == 0) |
| 8238 | chunk_printf(trash, "%s", kw[index]); |
| 8239 | else |
| 8240 | chunk_appendf(trash, " %s", kw[index]); |
| 8241 | index++; |
| 8242 | lua_pop(L, 1); |
| 8243 | } |
| 8244 | cli_kw = cli_find_kw_exact((char **)kw); |
| 8245 | if (cli_kw != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8246 | fcn = cli_kw->private; |
| 8247 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 8248 | ha_warning("Trying to register CLI keyword 'lua.%s' more than once. " |
| 8249 | "This will become a hard error in version 2.5.\n", trash->area); |
| 8250 | } |
| 8251 | fcn->function_ref[hlua_state_id] = ref_io; |
| 8252 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8253 | } |
| 8254 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8255 | /* Allocate and fill the sample fetch keyword struct. */ |
| 8256 | cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 8257 | if (!cli_kws) { |
| 8258 | errmsg = "Lua out of memory error."; |
| 8259 | goto error; |
| 8260 | } |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 8261 | fcn = new_hlua_function(); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 8262 | if (!fcn) { |
| 8263 | errmsg = "Lua out of memory error."; |
| 8264 | goto error; |
| 8265 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8266 | |
| 8267 | /* Fill path. */ |
| 8268 | index = 0; |
| 8269 | lua_pushnil(L); |
| 8270 | while(lua_next(L, 1) != 0) { |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 8271 | if (index >= 5) { |
| 8272 | errmsg = "1st argument must be a table with a maximum of 5 entries"; |
| 8273 | goto error; |
| 8274 | } |
| 8275 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 8276 | errmsg = "1st argument must be a table filled with strings"; |
| 8277 | goto error; |
| 8278 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8279 | cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1)); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 8280 | if (!cli_kws->kw[0].str_kw[index]) { |
| 8281 | errmsg = "Lua out of memory error."; |
| 8282 | goto error; |
| 8283 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8284 | index++; |
| 8285 | lua_pop(L, 1); |
| 8286 | } |
| 8287 | |
| 8288 | /* Copy help message. */ |
| 8289 | cli_kws->kw[0].usage = strdup(message); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 8290 | if (!cli_kws->kw[0].usage) { |
| 8291 | errmsg = "Lua out of memory error."; |
| 8292 | goto error; |
| 8293 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8294 | |
| 8295 | /* Fill fcn io handler. */ |
| 8296 | len = strlen("<lua.cli>") + 1; |
| 8297 | for (i = 0; i < index; i++) |
| 8298 | len += strlen(cli_kws->kw[0].str_kw[i]) + 1; |
| 8299 | fcn->name = calloc(1, len); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 8300 | if (!fcn->name) { |
| 8301 | errmsg = "Lua out of memory error."; |
| 8302 | goto error; |
| 8303 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8304 | strncat((char *)fcn->name, "<lua.cli", len); |
| 8305 | for (i = 0; i < index; i++) { |
| 8306 | strncat((char *)fcn->name, ".", len); |
| 8307 | strncat((char *)fcn->name, cli_kws->kw[0].str_kw[i], len); |
| 8308 | } |
| 8309 | strncat((char *)fcn->name, ">", len); |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8310 | fcn->function_ref[hlua_state_id] = ref_io; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8311 | |
| 8312 | /* Fill last entries. */ |
| 8313 | cli_kws->kw[0].private = fcn; |
| 8314 | cli_kws->kw[0].parse = hlua_cli_parse_fct; |
| 8315 | cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct; |
| 8316 | cli_kws->kw[0].io_release = hlua_cli_io_release_fct; |
| 8317 | |
| 8318 | /* Register this new converter */ |
| 8319 | cli_register_kw(cli_kws); |
| 8320 | |
| 8321 | return 0; |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 8322 | |
| 8323 | error: |
| 8324 | release_hlua_function(fcn); |
| 8325 | if (cli_kws) { |
| 8326 | for (i = 0; i < index; i++) |
| 8327 | ha_free((char **)&(cli_kws->kw[0].str_kw[i])); |
| 8328 | ha_free((char **)&(cli_kws->kw[0].usage)); |
| 8329 | } |
| 8330 | ha_free(&cli_kws); |
| 8331 | WILL_LJMP(luaL_error(L, errmsg)); |
| 8332 | return 0; /* Never reached */ |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8333 | } |
| 8334 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8335 | 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] | 8336 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8337 | char **err, unsigned int *timeout) |
| 8338 | { |
| 8339 | const char *error; |
| 8340 | |
| 8341 | error = parse_time_err(args[1], timeout, TIME_UNIT_MS); |
Willy Tarreau | 9faebe3 | 2019-06-07 19:00:37 +0200 | [diff] [blame] | 8342 | if (error == PARSE_TIME_OVER) { |
| 8343 | memprintf(err, "timer overflow in argument <%s> to <%s> (maximum value is 2147483647 ms or ~24.8 days)", |
| 8344 | args[1], args[0]); |
| 8345 | return -1; |
| 8346 | } |
| 8347 | else if (error == PARSE_TIME_UNDER) { |
| 8348 | memprintf(err, "timer underflow in argument <%s> to <%s> (minimum non-null value is 1 ms)", |
| 8349 | args[1], args[0]); |
| 8350 | return -1; |
| 8351 | } |
| 8352 | else if (error) { |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8353 | memprintf(err, "%s: invalid timeout", args[0]); |
| 8354 | return -1; |
| 8355 | } |
| 8356 | return 0; |
| 8357 | } |
| 8358 | |
| 8359 | 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] | 8360 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8361 | char **err) |
| 8362 | { |
| 8363 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 8364 | file, line, err, &hlua_timeout_session); |
| 8365 | } |
| 8366 | |
| 8367 | 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] | 8368 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8369 | char **err) |
| 8370 | { |
| 8371 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 8372 | file, line, err, &hlua_timeout_task); |
| 8373 | } |
| 8374 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8375 | 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] | 8376 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8377 | char **err) |
| 8378 | { |
| 8379 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 8380 | file, line, err, &hlua_timeout_applet); |
| 8381 | } |
| 8382 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 8383 | 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] | 8384 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 8385 | char **err) |
| 8386 | { |
| 8387 | char *error; |
| 8388 | |
| 8389 | hlua_nb_instruction = strtoll(args[1], &error, 10); |
| 8390 | if (*error != '\0') { |
| 8391 | memprintf(err, "%s: invalid number", args[0]); |
| 8392 | return -1; |
| 8393 | } |
| 8394 | return 0; |
| 8395 | } |
| 8396 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8397 | 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] | 8398 | const struct proxy *defpx, const char *file, int line, |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8399 | char **err) |
| 8400 | { |
| 8401 | char *error; |
| 8402 | |
| 8403 | if (*(args[1]) == 0) { |
| 8404 | memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]); |
| 8405 | return -1; |
| 8406 | } |
| 8407 | hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L; |
| 8408 | if (*error != '\0') { |
| 8409 | memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error); |
| 8410 | return -1; |
| 8411 | } |
| 8412 | return 0; |
| 8413 | } |
| 8414 | |
| 8415 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8416 | /* This function is called by the main configuration key "lua-load". It loads and |
| 8417 | * execute an lua file during the parsing of the HAProxy configuration file. It is |
| 8418 | * the main lua entry point. |
| 8419 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 8420 | * This function runs with the HAProxy keywords API. It returns -1 if an error |
| 8421 | * occurs, otherwise it returns 0. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8422 | * |
| 8423 | * In some error case, LUA set an error message in top of the stack. This function |
| 8424 | * 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] | 8425 | * |
| 8426 | * This function can fail with an abort() due to an Lua critical error. |
| 8427 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 8428 | * tolerated. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8429 | */ |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8430 | static int hlua_load_state(char *filename, lua_State *L, char **err) |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8431 | { |
| 8432 | int error; |
| 8433 | |
| 8434 | /* Just load and compile the file. */ |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8435 | error = luaL_loadfile(L, filename); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8436 | if (error) { |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8437 | memprintf(err, "error in Lua file '%s': %s", filename, lua_tostring(L, -1)); |
| 8438 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8439 | return -1; |
| 8440 | } |
| 8441 | |
| 8442 | /* If no syntax error where detected, execute the code. */ |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8443 | error = lua_pcall(L, 0, LUA_MULTRET, 0); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8444 | switch (error) { |
| 8445 | case LUA_OK: |
| 8446 | break; |
| 8447 | case LUA_ERRRUN: |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8448 | memprintf(err, "Lua runtime error: %s\n", 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 | case LUA_ERRMEM: |
Thierry Fournier | de6145f | 2020-11-29 00:55:53 +0100 | [diff] [blame] | 8452 | memprintf(err, "Lua out of memory error\n"); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8453 | return -1; |
| 8454 | case LUA_ERRERR: |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8455 | memprintf(err, "Lua message handler error: %s\n", lua_tostring(L, -1)); |
| 8456 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8457 | return -1; |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 8458 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM <= 503 |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8459 | case LUA_ERRGCMM: |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8460 | memprintf(err, "Lua garbage collector error: %s\n", lua_tostring(L, -1)); |
| 8461 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8462 | return -1; |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 8463 | #endif |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8464 | default: |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8465 | memprintf(err, "Lua unknown error: %s\n", lua_tostring(L, -1)); |
| 8466 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8467 | return -1; |
| 8468 | } |
| 8469 | |
| 8470 | return 0; |
| 8471 | } |
| 8472 | |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8473 | static int hlua_load(char **args, int section_type, struct proxy *curpx, |
Willy Tarreau | 0182516 | 2021-03-09 09:53:46 +0100 | [diff] [blame] | 8474 | const struct proxy *defpx, const char *file, int line, |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8475 | char **err) |
| 8476 | { |
| 8477 | if (*(args[1]) == 0) { |
| 8478 | memprintf(err, "'%s' expects a file name as parameter.\n", args[0]); |
| 8479 | return -1; |
| 8480 | } |
| 8481 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8482 | /* loading for global state */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8483 | hlua_state_id = 0; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8484 | ha_set_tid(0); |
Thierry Fournier | afc63e2 | 2020-11-28 17:06:51 +0100 | [diff] [blame] | 8485 | return hlua_load_state(args[1], hlua_states[0], err); |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8486 | } |
| 8487 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8488 | 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] | 8489 | const struct proxy *defpx, const char *file, int line, |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8490 | char **err) |
| 8491 | { |
| 8492 | int len; |
| 8493 | |
| 8494 | if (*(args[1]) == 0) { |
| 8495 | memprintf(err, "'%s' expects a file as parameter.\n", args[0]); |
| 8496 | return -1; |
| 8497 | } |
| 8498 | |
| 8499 | if (per_thread_load == NULL) { |
| 8500 | /* allocate the first entry large enough to store the final NULL */ |
| 8501 | per_thread_load = calloc(1, sizeof(*per_thread_load)); |
| 8502 | if (per_thread_load == NULL) { |
| 8503 | memprintf(err, "out of memory error"); |
| 8504 | return -1; |
| 8505 | } |
| 8506 | } |
| 8507 | |
| 8508 | /* count used entries */ |
| 8509 | for (len = 0; per_thread_load[len] != NULL; len++) |
| 8510 | ; |
| 8511 | |
| 8512 | per_thread_load = realloc(per_thread_load, (len + 2) * sizeof(*per_thread_load)); |
| 8513 | if (per_thread_load == NULL) { |
| 8514 | memprintf(err, "out of memory error"); |
| 8515 | return -1; |
| 8516 | } |
| 8517 | |
| 8518 | per_thread_load[len] = strdup(args[1]); |
| 8519 | per_thread_load[len + 1] = NULL; |
| 8520 | |
| 8521 | if (per_thread_load[len] == NULL) { |
| 8522 | memprintf(err, "out of memory error"); |
| 8523 | return -1; |
| 8524 | } |
| 8525 | |
| 8526 | /* loading for thread 1 only */ |
| 8527 | hlua_state_id = 1; |
| 8528 | ha_set_tid(0); |
| 8529 | return hlua_load_state(args[1], hlua_states[1], err); |
| 8530 | } |
| 8531 | |
Tim Duesterhus | c9fc9f2 | 2020-01-12 13:55:39 +0100 | [diff] [blame] | 8532 | /* Prepend the given <path> followed by a semicolon to the `package.<type>` variable |
| 8533 | * in the given <ctx>. |
| 8534 | */ |
Thierry Fournier | 3fb9e51 | 2020-11-28 10:13:12 +0100 | [diff] [blame] | 8535 | static int hlua_prepend_path(lua_State *L, char *type, char *path) |
Tim Duesterhus | c9fc9f2 | 2020-01-12 13:55:39 +0100 | [diff] [blame] | 8536 | { |
Thierry Fournier | 3fb9e51 | 2020-11-28 10:13:12 +0100 | [diff] [blame] | 8537 | lua_getglobal(L, "package"); /* push package variable */ |
| 8538 | lua_pushstring(L, path); /* push given path */ |
| 8539 | lua_pushstring(L, ";"); /* push semicolon */ |
| 8540 | lua_getfield(L, -3, type); /* push old path */ |
| 8541 | lua_concat(L, 3); /* concatenate to new path */ |
| 8542 | lua_setfield(L, -2, type); /* store new path */ |
| 8543 | lua_pop(L, 1); /* pop package variable */ |
Tim Duesterhus | c9fc9f2 | 2020-01-12 13:55:39 +0100 | [diff] [blame] | 8544 | |
| 8545 | return 0; |
| 8546 | } |
| 8547 | |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8548 | 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] | 8549 | const struct proxy *defpx, const char *file, int line, |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8550 | char **err) |
| 8551 | { |
| 8552 | char *path; |
| 8553 | char *type = "path"; |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 8554 | struct prepend_path *p = NULL; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8555 | |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8556 | if (too_many_args(2, args, err, NULL)) { |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 8557 | goto err; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8558 | } |
| 8559 | |
| 8560 | if (!(*args[1])) { |
| 8561 | memprintf(err, "'%s' expects to receive a <path> as argument", args[0]); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 8562 | goto err; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8563 | } |
| 8564 | path = args[1]; |
| 8565 | |
| 8566 | if (*args[2]) { |
| 8567 | if (strcmp(args[2], "path") != 0 && strcmp(args[2], "cpath") != 0) { |
| 8568 | 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] | 8569 | goto err; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8570 | } |
| 8571 | type = args[2]; |
| 8572 | } |
| 8573 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8574 | p = calloc(1, sizeof(*p)); |
| 8575 | if (p == NULL) { |
Tim Duesterhus | f89d43a | 2021-01-03 20:04:37 +0100 | [diff] [blame] | 8576 | memprintf(err, "memory allocation failed"); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 8577 | goto err; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8578 | } |
| 8579 | p->path = strdup(path); |
| 8580 | if (p->path == NULL) { |
Tim Duesterhus | f89d43a | 2021-01-03 20:04:37 +0100 | [diff] [blame] | 8581 | memprintf(err, "memory allocation failed"); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 8582 | goto err2; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8583 | } |
| 8584 | p->type = strdup(type); |
| 8585 | if (p->type == NULL) { |
Tim Duesterhus | f89d43a | 2021-01-03 20:04:37 +0100 | [diff] [blame] | 8586 | memprintf(err, "memory allocation failed"); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 8587 | goto err2; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8588 | } |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 8589 | LIST_APPEND(&prepend_path_list, &p->l); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8590 | |
| 8591 | hlua_prepend_path(hlua_states[0], type, path); |
| 8592 | hlua_prepend_path(hlua_states[1], type, path); |
| 8593 | return 0; |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 8594 | |
| 8595 | err2: |
| 8596 | free(p->type); |
| 8597 | free(p->path); |
| 8598 | err: |
| 8599 | free(p); |
| 8600 | return -1; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8601 | } |
| 8602 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8603 | /* configuration keywords declaration */ |
| 8604 | static struct cfg_kw_list cfg_kws = {{ },{ |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8605 | { CFG_GLOBAL, "lua-prepend-path", hlua_config_prepend_path }, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8606 | { CFG_GLOBAL, "lua-load", hlua_load }, |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8607 | { CFG_GLOBAL, "lua-load-per-thread", hlua_load_per_thread }, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8608 | { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout }, |
| 8609 | { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout }, |
Thierry FOURNIER | 56da101 | 2015-10-01 08:42:31 +0200 | [diff] [blame] | 8610 | { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout }, |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 8611 | { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield }, |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8612 | { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem }, |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8613 | { 0, NULL, NULL }, |
| 8614 | }}; |
| 8615 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 8616 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |
| 8617 | |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 8618 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8619 | /* This function can fail with an abort() due to an Lua critical error. |
| 8620 | * We are in the initialisation process of HAProxy, this abort() is |
| 8621 | * tolerated. |
| 8622 | */ |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8623 | int hlua_post_init_state(lua_State *L) |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8624 | { |
| 8625 | struct hlua_init_function *init; |
| 8626 | const char *msg; |
| 8627 | enum hlua_exec ret; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8628 | const char *error; |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8629 | const char *kind; |
| 8630 | const char *trace; |
| 8631 | int return_status = 1; |
| 8632 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
| 8633 | int nres; |
| 8634 | #endif |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8635 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 8636 | /* disable memory limit checks if limit is not set */ |
| 8637 | if (!hlua_global_allocator.limit) |
| 8638 | hlua_global_allocator.limit = ~hlua_global_allocator.limit; |
| 8639 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 8640 | /* Call post initialisation function in safe environment. */ |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 8641 | if (setjmp(safe_ljmp_env) != 0) { |
| 8642 | lua_atpanic(L, hlua_panic_safe); |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8643 | if (lua_type(L, -1) == LUA_TSTRING) |
| 8644 | error = lua_tostring(L, -1); |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 8645 | else |
| 8646 | error = "critical error"; |
| 8647 | fprintf(stderr, "Lua post-init: %s.\n", error); |
| 8648 | exit(1); |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 8649 | } else { |
| 8650 | lua_atpanic(L, hlua_panic_ljmp); |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 8651 | } |
Frédéric Lécaille | 54f2bcf | 2018-08-29 13:46:24 +0200 | [diff] [blame] | 8652 | |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8653 | hlua_fcn_post_init(L); |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 8654 | |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8655 | list_for_each_entry(init, &hlua_init_functions[hlua_state_id], l) { |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8656 | lua_rawgeti(L, LUA_REGISTRYINDEX, init->function_ref); |
Aurelien DARRAGON | 93591b4 | 2023-03-20 16:29:55 +0100 | [diff] [blame] | 8657 | /* function ref should be released right away since it was pushed |
| 8658 | * on the stack and will not be used anymore |
| 8659 | */ |
| 8660 | hlua_unref(L, init->function_ref); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8661 | |
| 8662 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
Aurelien DARRAGON | 2dec950 | 2023-09-08 19:29:08 +0200 | [diff] [blame] | 8663 | ret = lua_resume(L, NULL, 0, &nres); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8664 | #else |
Aurelien DARRAGON | 2dec950 | 2023-09-08 19:29:08 +0200 | [diff] [blame] | 8665 | ret = lua_resume(L, NULL, 0); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8666 | #endif |
| 8667 | kind = NULL; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8668 | switch (ret) { |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8669 | |
| 8670 | case LUA_OK: |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8671 | lua_pop(L, -1); |
Thierry Fournier | 13d08b7 | 2020-11-28 11:02:58 +0100 | [diff] [blame] | 8672 | break; |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8673 | |
| 8674 | case LUA_ERRERR: |
| 8675 | kind = "message handler error"; |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 8676 | /* Fall through */ |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8677 | case LUA_ERRRUN: |
| 8678 | if (!kind) |
| 8679 | kind = "runtime error"; |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8680 | msg = lua_tostring(L, -1); |
| 8681 | lua_settop(L, 0); /* Empty the stack. */ |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 8682 | trace = hlua_traceback(L, ", "); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8683 | if (msg) |
| 8684 | ha_alert("Lua init: %s: '%s' from %s\n", kind, msg, trace); |
| 8685 | else |
| 8686 | ha_alert("Lua init: unknown %s from %s\n", kind, trace); |
| 8687 | return_status = 0; |
| 8688 | break; |
| 8689 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8690 | default: |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8691 | /* Unknown error */ |
| 8692 | kind = "Unknown error"; |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 8693 | /* Fall through */ |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8694 | case LUA_YIELD: |
| 8695 | /* yield is not configured at this step, this state doesn't happen */ |
| 8696 | if (!kind) |
| 8697 | kind = "yield not allowed"; |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 8698 | /* Fall through */ |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8699 | case LUA_ERRMEM: |
| 8700 | if (!kind) |
| 8701 | kind = "out of memory error"; |
Aurelien DARRAGON | 93f5d8d | 2023-08-09 10:11:49 +0200 | [diff] [blame] | 8702 | lua_settop(L, 0); /* Empty the stack. */ |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 8703 | trace = hlua_traceback(L, ", "); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8704 | ha_alert("Lua init: %s: %s\n", kind, trace); |
| 8705 | return_status = 0; |
| 8706 | break; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8707 | } |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8708 | if (!return_status) |
| 8709 | break; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8710 | } |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 8711 | |
| 8712 | lua_atpanic(L, hlua_panic_safe); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8713 | return return_status; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8714 | } |
| 8715 | |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8716 | int hlua_post_init() |
| 8717 | { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8718 | int ret; |
| 8719 | int i; |
| 8720 | int errors; |
| 8721 | char *err = NULL; |
| 8722 | struct hlua_function *fcn; |
| 8723 | |
Willy Tarreau | 42afc59 | 2021-08-30 09:35:18 +0200 | [diff] [blame] | 8724 | #if defined(USE_OPENSSL) |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8725 | /* Initialize SSL server. */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 8726 | if (socket_ssl->xprt->prepare_srv) { |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8727 | int saved_used_backed = global.ssl_used_backend; |
| 8728 | // don't affect maxconn automatic computation |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 8729 | socket_ssl->xprt->prepare_srv(socket_ssl); |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8730 | global.ssl_used_backend = saved_used_backed; |
| 8731 | } |
| 8732 | #endif |
| 8733 | |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8734 | /* Perform post init of common thread */ |
| 8735 | hlua_state_id = 0; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8736 | ha_set_tid(0); |
| 8737 | ret = hlua_post_init_state(hlua_states[hlua_state_id]); |
| 8738 | if (ret == 0) |
| 8739 | return 0; |
| 8740 | |
| 8741 | /* init remaining lua states and load files */ |
| 8742 | for (hlua_state_id = 2; hlua_state_id < global.nbthread + 1; hlua_state_id++) { |
| 8743 | |
| 8744 | /* set thread context */ |
| 8745 | ha_set_tid(hlua_state_id - 1); |
| 8746 | |
| 8747 | /* Init lua state */ |
| 8748 | hlua_states[hlua_state_id] = hlua_init_state(hlua_state_id); |
| 8749 | |
| 8750 | /* Load lua files */ |
| 8751 | for (i = 0; per_thread_load && per_thread_load[i]; i++) { |
| 8752 | ret = hlua_load_state(per_thread_load[i], hlua_states[hlua_state_id], &err); |
| 8753 | if (ret != 0) { |
| 8754 | ha_alert("Lua init: %s\n", err); |
| 8755 | return 0; |
| 8756 | } |
| 8757 | } |
| 8758 | } |
| 8759 | |
| 8760 | /* Reset thread context */ |
| 8761 | ha_set_tid(0); |
| 8762 | |
| 8763 | /* Execute post init for all states */ |
| 8764 | for (hlua_state_id = 1; hlua_state_id < global.nbthread + 1; hlua_state_id++) { |
| 8765 | |
| 8766 | /* set thread context */ |
| 8767 | ha_set_tid(hlua_state_id - 1); |
| 8768 | |
| 8769 | /* run post init */ |
| 8770 | ret = hlua_post_init_state(hlua_states[hlua_state_id]); |
| 8771 | if (ret == 0) |
| 8772 | return 0; |
| 8773 | } |
| 8774 | |
| 8775 | /* Reset thread context */ |
| 8776 | ha_set_tid(0); |
| 8777 | |
| 8778 | /* control functions registering. Each function must have: |
| 8779 | * - only the function_ref[0] set positive and all other to -1 |
| 8780 | * - only the function_ref[0] set to -1 and all other positive |
| 8781 | * This ensure a same reference is not used both in shared |
| 8782 | * lua state and thread dedicated lua state. Note: is the case |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 8783 | * reach, the shared state is priority, but the bug will be |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8784 | * complicated to found for the end user. |
| 8785 | */ |
| 8786 | errors = 0; |
| 8787 | list_for_each_entry(fcn, &referenced_functions, l) { |
| 8788 | ret = 0; |
| 8789 | for (i = 1; i < global.nbthread + 1; i++) { |
| 8790 | if (fcn->function_ref[i] == -1) |
| 8791 | ret--; |
| 8792 | else |
| 8793 | ret++; |
| 8794 | } |
| 8795 | if (abs(ret) != global.nbthread) { |
| 8796 | ha_alert("Lua function '%s' is not referenced in all thread. " |
| 8797 | "Expect function in all thread or in none thread.\n", fcn->name); |
| 8798 | errors++; |
| 8799 | continue; |
| 8800 | } |
| 8801 | |
| 8802 | if ((fcn->function_ref[0] == -1) == (ret < 0)) { |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 8803 | ha_alert("Lua function '%s' is referenced both ins shared Lua context (through lua-load) " |
| 8804 | "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] | 8805 | "exclusive.\n", fcn->name); |
| 8806 | errors++; |
| 8807 | } |
| 8808 | } |
| 8809 | |
| 8810 | if (errors > 0) |
| 8811 | return 0; |
| 8812 | |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 8813 | /* 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] | 8814 | * -1 in order to have probably a segfault if someone use it |
| 8815 | */ |
| 8816 | hlua_state_id = -1; |
| 8817 | |
| 8818 | return 1; |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8819 | } |
| 8820 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8821 | /* The memory allocator used by the Lua stack. <ud> is a pointer to the |
| 8822 | * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize> |
| 8823 | * 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] | 8824 | * allocation. <nsize> is the requested new size. A new allocation is |
| 8825 | * indicated by <ptr> being NULL. A free is indicated by <nsize> being |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 8826 | * zero. This one verifies that the limits are respected but is optimized |
| 8827 | * 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] | 8828 | * |
| 8829 | * Warning: while this API ressembles glibc's realloc() a lot, glibc surpasses |
| 8830 | * POSIX by making realloc(ptr,0) an effective free(), but others do not do |
| 8831 | * that and will simply allocate zero as if it were the result of malloc(0), |
| 8832 | * so mapping this onto realloc() will lead to memory leaks on non-glibc |
| 8833 | * systems. |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8834 | */ |
| 8835 | static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize) |
| 8836 | { |
| 8837 | struct hlua_mem_allocator *zone = ud; |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 8838 | size_t limit, old, new; |
| 8839 | |
Tim Duesterhus | 2258652 | 2021-01-08 10:35:33 +0100 | [diff] [blame] | 8840 | if (unlikely(!ptr && !nsize)) |
| 8841 | return NULL; |
| 8842 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 8843 | /* a limit of ~0 means unlimited and boot complete, so there's no need |
| 8844 | * for accounting anymore. |
| 8845 | */ |
Willy Tarreau | e00ad05 | 2021-10-22 16:00:02 +0200 | [diff] [blame] | 8846 | if (likely(~zone->limit == 0)) { |
| 8847 | if (!nsize) |
| 8848 | ha_free(&ptr); |
| 8849 | else |
| 8850 | ptr = realloc(ptr, nsize); |
| 8851 | return ptr; |
| 8852 | } |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8853 | |
Willy Tarreau | d36c7fa | 2020-12-02 12:26:29 +0100 | [diff] [blame] | 8854 | if (!ptr) |
| 8855 | osize = 0; |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8856 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 8857 | /* enforce strict limits across all threads */ |
| 8858 | limit = zone->limit; |
| 8859 | old = _HA_ATOMIC_LOAD(&zone->allocated); |
| 8860 | do { |
| 8861 | new = old + nsize - osize; |
| 8862 | if (unlikely(nsize && limit && new > limit)) |
| 8863 | return NULL; |
| 8864 | } while (!_HA_ATOMIC_CAS(&zone->allocated, &old, new)); |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8865 | |
Willy Tarreau | e00ad05 | 2021-10-22 16:00:02 +0200 | [diff] [blame] | 8866 | if (!nsize) |
| 8867 | ha_free(&ptr); |
| 8868 | else |
| 8869 | ptr = realloc(ptr, nsize); |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 8870 | |
| 8871 | if (unlikely(!ptr && nsize)) // failed |
| 8872 | _HA_ATOMIC_SUB(&zone->allocated, nsize - osize); |
| 8873 | |
| 8874 | __ha_barrier_atomic_store(); |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8875 | return ptr; |
| 8876 | } |
| 8877 | |
Thierry Fournier | ecb83c2 | 2020-11-28 15:49:44 +0100 | [diff] [blame] | 8878 | /* 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] | 8879 | * We are in the initialisation process of HAProxy, this abort() is |
| 8880 | * tolerated. |
| 8881 | */ |
Thierry Fournier | ecb83c2 | 2020-11-28 15:49:44 +0100 | [diff] [blame] | 8882 | lua_State *hlua_init_state(int thread_num) |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 8883 | { |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8884 | int i; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8885 | int idx; |
| 8886 | struct sample_fetch *sf; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8887 | struct sample_conv *sc; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8888 | char *p; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8889 | const char *error_msg; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 8890 | void **context; |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8891 | lua_State *L; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8892 | struct prepend_path *pp; |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8893 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 8894 | /* Init main lua stack. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8895 | L = lua_newstate(hlua_alloc, &hlua_global_allocator); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 8896 | |
firexinghe | bdd336c | 2023-07-13 11:03:34 +0800 | [diff] [blame] | 8897 | if (!L) { |
| 8898 | fprintf(stderr, |
| 8899 | "Lua init: critical error: lua_newstate() returned NULL." |
| 8900 | " This may possibly be caused by a memory allocation error.\n"); |
| 8901 | exit(1); |
| 8902 | } |
| 8903 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 8904 | /* Initialise Lua context to NULL */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8905 | context = lua_getextraspace(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 8906 | *context = NULL; |
| 8907 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 8908 | /* From this point, until the end of the initialisation function, |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8909 | * the Lua function can fail with an abort. We are in the initialisation |
| 8910 | * process of HAProxy, this abort() is tolerated. |
| 8911 | */ |
| 8912 | |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 8913 | /* Call post initialisation function in safe environment. */ |
| 8914 | if (setjmp(safe_ljmp_env) != 0) { |
| 8915 | lua_atpanic(L, hlua_panic_safe); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8916 | if (lua_type(L, -1) == LUA_TSTRING) |
| 8917 | error_msg = lua_tostring(L, -1); |
Thierry Fournier | 2f05cc6 | 2020-11-28 16:08:02 +0100 | [diff] [blame] | 8918 | else |
| 8919 | error_msg = "critical error"; |
| 8920 | fprintf(stderr, "Lua init: %s.\n", error_msg); |
| 8921 | exit(1); |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 8922 | } else { |
| 8923 | lua_atpanic(L, hlua_panic_ljmp); |
Thierry Fournier | 2f05cc6 | 2020-11-28 16:08:02 +0100 | [diff] [blame] | 8924 | } |
| 8925 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 8926 | /* Initialise lua. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8927 | luaL_openlibs(L); |
Tim Duesterhus | 541fe1e | 2020-01-12 13:55:41 +0100 | [diff] [blame] | 8928 | #define HLUA_PREPEND_PATH_TOSTRING1(x) #x |
| 8929 | #define HLUA_PREPEND_PATH_TOSTRING(x) HLUA_PREPEND_PATH_TOSTRING1(x) |
| 8930 | #ifdef HLUA_PREPEND_PATH |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8931 | hlua_prepend_path(L, "path", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_PATH)); |
Tim Duesterhus | 541fe1e | 2020-01-12 13:55:41 +0100 | [diff] [blame] | 8932 | #endif |
| 8933 | #ifdef HLUA_PREPEND_CPATH |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8934 | hlua_prepend_path(L, "cpath", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_CPATH)); |
Tim Duesterhus | 541fe1e | 2020-01-12 13:55:41 +0100 | [diff] [blame] | 8935 | #endif |
| 8936 | #undef HLUA_PREPEND_PATH_TOSTRING |
| 8937 | #undef HLUA_PREPEND_PATH_TOSTRING1 |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8938 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8939 | /* Apply configured prepend path */ |
| 8940 | list_for_each_entry(pp, &prepend_path_list, l) |
| 8941 | hlua_prepend_path(L, pp->type, pp->path); |
| 8942 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8943 | /* |
| 8944 | * |
| 8945 | * Create "core" object. |
| 8946 | * |
| 8947 | */ |
| 8948 | |
Thierry FOURNIER | a2d8c65 | 2015-03-11 17:29:39 +0100 | [diff] [blame] | 8949 | /* This table entry is the object "core" base. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8950 | lua_newtable(L); |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8951 | |
Thierry Fournier | ecb83c2 | 2020-11-28 15:49:44 +0100 | [diff] [blame] | 8952 | /* set the thread id */ |
| 8953 | hlua_class_const_int(L, "thread", thread_num); |
| 8954 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8955 | /* Push the loglevel constants. */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8956 | for (i = 0; i < NB_LOG_LEVELS; i++) |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8957 | hlua_class_const_int(L, log_levels[i], i); |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8958 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8959 | /* Register special functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8960 | hlua_class_function(L, "register_init", hlua_register_init); |
| 8961 | hlua_class_function(L, "register_task", hlua_register_task); |
| 8962 | hlua_class_function(L, "register_fetches", hlua_register_fetches); |
| 8963 | hlua_class_function(L, "register_converters", hlua_register_converters); |
| 8964 | hlua_class_function(L, "register_action", hlua_register_action); |
| 8965 | hlua_class_function(L, "register_service", hlua_register_service); |
| 8966 | hlua_class_function(L, "register_cli", hlua_register_cli); |
| 8967 | hlua_class_function(L, "yield", hlua_yield); |
| 8968 | hlua_class_function(L, "set_nice", hlua_set_nice); |
| 8969 | hlua_class_function(L, "sleep", hlua_sleep); |
| 8970 | hlua_class_function(L, "msleep", hlua_msleep); |
| 8971 | hlua_class_function(L, "add_acl", hlua_add_acl); |
| 8972 | hlua_class_function(L, "del_acl", hlua_del_acl); |
| 8973 | hlua_class_function(L, "set_map", hlua_set_map); |
| 8974 | hlua_class_function(L, "del_map", hlua_del_map); |
| 8975 | hlua_class_function(L, "tcp", hlua_socket_new); |
| 8976 | hlua_class_function(L, "log", hlua_log); |
| 8977 | hlua_class_function(L, "Debug", hlua_log_debug); |
| 8978 | hlua_class_function(L, "Info", hlua_log_info); |
| 8979 | hlua_class_function(L, "Warning", hlua_log_warning); |
| 8980 | hlua_class_function(L, "Alert", hlua_log_alert); |
| 8981 | hlua_class_function(L, "done", hlua_done); |
| 8982 | hlua_fcn_reg_core_fcn(L); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8983 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8984 | lua_setglobal(L, "core"); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 8985 | |
| 8986 | /* |
| 8987 | * |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 8988 | * Create "act" object. |
| 8989 | * |
| 8990 | */ |
| 8991 | |
| 8992 | /* This table entry is the object "act" base. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8993 | lua_newtable(L); |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 8994 | |
| 8995 | /* push action return constants */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8996 | hlua_class_const_int(L, "CONTINUE", ACT_RET_CONT); |
| 8997 | hlua_class_const_int(L, "STOP", ACT_RET_STOP); |
| 8998 | hlua_class_const_int(L, "YIELD", ACT_RET_YIELD); |
| 8999 | hlua_class_const_int(L, "ERROR", ACT_RET_ERR); |
| 9000 | hlua_class_const_int(L, "DONE", ACT_RET_DONE); |
| 9001 | hlua_class_const_int(L, "DENY", ACT_RET_DENY); |
| 9002 | hlua_class_const_int(L, "ABORT", ACT_RET_ABRT); |
| 9003 | hlua_class_const_int(L, "INVALID", ACT_RET_INV); |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 9004 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9005 | hlua_class_function(L, "wake_time", hlua_set_wake_time); |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 9006 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9007 | lua_setglobal(L, "act"); |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 9008 | |
| 9009 | /* |
| 9010 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 9011 | * Register class Map |
| 9012 | * |
| 9013 | */ |
| 9014 | |
| 9015 | /* This table entry is the object "Map" base. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9016 | lua_newtable(L); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 9017 | |
| 9018 | /* register pattern types. */ |
| 9019 | for (i=0; i<PAT_MATCH_NUM; i++) |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9020 | hlua_class_const_int(L, pat_match_names[i], i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 9021 | for (i=0; i<PAT_MATCH_NUM; i++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 9022 | snprintf(trash.area, trash.size, "_%s", pat_match_names[i]); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9023 | hlua_class_const_int(L, trash.area, i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 9024 | } |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 9025 | |
| 9026 | /* register constructor. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9027 | hlua_class_function(L, "new", hlua_map_new); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 9028 | |
| 9029 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9030 | lua_newtable(L); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 9031 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 9032 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9033 | lua_pushstring(L, "__index"); |
| 9034 | lua_newtable(L); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 9035 | |
| 9036 | /* Register . */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9037 | hlua_class_function(L, "lookup", hlua_map_lookup); |
| 9038 | hlua_class_function(L, "slookup", hlua_map_slookup); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 9039 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9040 | lua_rawset(L, -3); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 9041 | |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 9042 | /* Register previous table in the registry with reference and named entry. |
| 9043 | * The function hlua_register_metatable() pops the stack, so we |
| 9044 | * previously create a copy of the table. |
| 9045 | */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9046 | lua_pushvalue(L, -1); /* Copy the -1 entry and push it on the stack. */ |
| 9047 | class_map_ref = hlua_register_metatable(L, CLASS_MAP); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 9048 | |
| 9049 | /* Assign the metatable to the mai Map object. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9050 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 9051 | |
| 9052 | /* Set a name to the table. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9053 | lua_setglobal(L, "Map"); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 9054 | |
| 9055 | /* |
| 9056 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 9057 | * Register class Channel |
| 9058 | * |
| 9059 | */ |
| 9060 | |
| 9061 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9062 | lua_newtable(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 9063 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 9064 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9065 | lua_pushstring(L, "__index"); |
| 9066 | lua_newtable(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 9067 | |
| 9068 | /* Register . */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9069 | hlua_class_function(L, "get", hlua_channel_get); |
| 9070 | hlua_class_function(L, "dup", hlua_channel_dup); |
| 9071 | hlua_class_function(L, "getline", hlua_channel_getline); |
| 9072 | hlua_class_function(L, "set", hlua_channel_set); |
| 9073 | hlua_class_function(L, "append", hlua_channel_append); |
| 9074 | hlua_class_function(L, "send", hlua_channel_send); |
| 9075 | hlua_class_function(L, "forward", hlua_channel_forward); |
| 9076 | hlua_class_function(L, "get_in_len", hlua_channel_get_in_len); |
| 9077 | hlua_class_function(L, "get_out_len", hlua_channel_get_out_len); |
| 9078 | hlua_class_function(L, "is_full", hlua_channel_is_full); |
| 9079 | hlua_class_function(L, "is_resp", hlua_channel_is_resp); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 9080 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9081 | lua_rawset(L, -3); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 9082 | |
| 9083 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9084 | class_channel_ref = hlua_register_metatable(L, CLASS_CHANNEL); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 9085 | |
| 9086 | /* |
| 9087 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 9088 | * Register class Fetches |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 9089 | * |
| 9090 | */ |
| 9091 | |
| 9092 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9093 | lua_newtable(L); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 9094 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 9095 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9096 | lua_pushstring(L, "__index"); |
| 9097 | lua_newtable(L); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 9098 | |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 9099 | /* Browse existing fetches and create the associated |
| 9100 | * object method. |
| 9101 | */ |
| 9102 | sf = NULL; |
| 9103 | while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) { |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 9104 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 9105 | * by an underscore. |
| 9106 | */ |
Willy Tarreau | fa92f36 | 2021-08-26 16:48:53 +0200 | [diff] [blame] | 9107 | strlcpy2(trash.area, sf->kw, trash.size); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 9108 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 9109 | if (*p == '.' || *p == '-' || *p == '+') |
| 9110 | *p = '_'; |
| 9111 | |
| 9112 | /* Register the function. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9113 | lua_pushstring(L, trash.area); |
| 9114 | lua_pushlightuserdata(L, sf); |
| 9115 | lua_pushcclosure(L, hlua_run_sample_fetch, 1); |
| 9116 | lua_rawset(L, -3); |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 9117 | } |
| 9118 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9119 | lua_rawset(L, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 9120 | |
| 9121 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9122 | class_fetches_ref = hlua_register_metatable(L, CLASS_FETCHES); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 9123 | |
| 9124 | /* |
| 9125 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 9126 | * Register class Converters |
| 9127 | * |
| 9128 | */ |
| 9129 | |
| 9130 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9131 | lua_newtable(L); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 9132 | |
| 9133 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9134 | lua_pushstring(L, "__index"); |
| 9135 | lua_newtable(L); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 9136 | |
| 9137 | /* Browse existing converters and create the associated |
| 9138 | * object method. |
| 9139 | */ |
| 9140 | sc = NULL; |
| 9141 | while ((sc = sample_conv_getnext(sc, &idx)) != NULL) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 9142 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 9143 | * by an underscore. |
| 9144 | */ |
Willy Tarreau | fa92f36 | 2021-08-26 16:48:53 +0200 | [diff] [blame] | 9145 | strlcpy2(trash.area, sc->kw, trash.size); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 9146 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 9147 | if (*p == '.' || *p == '-' || *p == '+') |
| 9148 | *p = '_'; |
| 9149 | |
| 9150 | /* Register the function. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9151 | lua_pushstring(L, trash.area); |
| 9152 | lua_pushlightuserdata(L, sc); |
| 9153 | lua_pushcclosure(L, hlua_run_sample_conv, 1); |
| 9154 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 9155 | } |
| 9156 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9157 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 9158 | |
| 9159 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9160 | class_converters_ref = hlua_register_metatable(L, CLASS_CONVERTERS); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 9161 | |
| 9162 | /* |
| 9163 | * |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 9164 | * Register class HTTP |
| 9165 | * |
| 9166 | */ |
| 9167 | |
| 9168 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9169 | lua_newtable(L); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 9170 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 9171 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9172 | lua_pushstring(L, "__index"); |
| 9173 | lua_newtable(L); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 9174 | |
| 9175 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9176 | hlua_class_function(L, "req_get_headers",hlua_http_req_get_headers); |
| 9177 | hlua_class_function(L, "req_del_header", hlua_http_req_del_hdr); |
| 9178 | hlua_class_function(L, "req_rep_header", hlua_http_req_rep_hdr); |
| 9179 | hlua_class_function(L, "req_rep_value", hlua_http_req_rep_val); |
| 9180 | hlua_class_function(L, "req_add_header", hlua_http_req_add_hdr); |
| 9181 | hlua_class_function(L, "req_set_header", hlua_http_req_set_hdr); |
| 9182 | hlua_class_function(L, "req_set_method", hlua_http_req_set_meth); |
| 9183 | hlua_class_function(L, "req_set_path", hlua_http_req_set_path); |
| 9184 | hlua_class_function(L, "req_set_query", hlua_http_req_set_query); |
| 9185 | hlua_class_function(L, "req_set_uri", hlua_http_req_set_uri); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 9186 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9187 | hlua_class_function(L, "res_get_headers",hlua_http_res_get_headers); |
| 9188 | hlua_class_function(L, "res_del_header", hlua_http_res_del_hdr); |
| 9189 | hlua_class_function(L, "res_rep_header", hlua_http_res_rep_hdr); |
| 9190 | hlua_class_function(L, "res_rep_value", hlua_http_res_rep_val); |
| 9191 | hlua_class_function(L, "res_add_header", hlua_http_res_add_hdr); |
| 9192 | hlua_class_function(L, "res_set_header", hlua_http_res_set_hdr); |
| 9193 | hlua_class_function(L, "res_set_status", hlua_http_res_set_status); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 9194 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9195 | lua_rawset(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 9196 | |
| 9197 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9198 | class_http_ref = hlua_register_metatable(L, CLASS_HTTP); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 9199 | |
| 9200 | /* |
| 9201 | * |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9202 | * Register class AppletTCP |
| 9203 | * |
| 9204 | */ |
| 9205 | |
| 9206 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9207 | lua_newtable(L); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9208 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 9209 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9210 | lua_pushstring(L, "__index"); |
| 9211 | lua_newtable(L); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9212 | |
| 9213 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9214 | hlua_class_function(L, "getline", hlua_applet_tcp_getline); |
| 9215 | hlua_class_function(L, "receive", hlua_applet_tcp_recv); |
| 9216 | hlua_class_function(L, "send", hlua_applet_tcp_send); |
| 9217 | hlua_class_function(L, "set_priv", hlua_applet_tcp_set_priv); |
| 9218 | hlua_class_function(L, "get_priv", hlua_applet_tcp_get_priv); |
| 9219 | hlua_class_function(L, "set_var", hlua_applet_tcp_set_var); |
| 9220 | hlua_class_function(L, "unset_var", hlua_applet_tcp_unset_var); |
| 9221 | hlua_class_function(L, "get_var", hlua_applet_tcp_get_var); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9222 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9223 | lua_settable(L, -3); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9224 | |
| 9225 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9226 | class_applet_tcp_ref = hlua_register_metatable(L, CLASS_APPLET_TCP); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9227 | |
| 9228 | /* |
| 9229 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9230 | * Register class AppletHTTP |
| 9231 | * |
| 9232 | */ |
| 9233 | |
| 9234 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9235 | lua_newtable(L); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9236 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 9237 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9238 | lua_pushstring(L, "__index"); |
| 9239 | lua_newtable(L); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9240 | |
| 9241 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9242 | hlua_class_function(L, "set_priv", hlua_applet_http_set_priv); |
| 9243 | hlua_class_function(L, "get_priv", hlua_applet_http_get_priv); |
| 9244 | hlua_class_function(L, "set_var", hlua_applet_http_set_var); |
| 9245 | hlua_class_function(L, "unset_var", hlua_applet_http_unset_var); |
| 9246 | hlua_class_function(L, "get_var", hlua_applet_http_get_var); |
| 9247 | hlua_class_function(L, "getline", hlua_applet_http_getline); |
| 9248 | hlua_class_function(L, "receive", hlua_applet_http_recv); |
| 9249 | hlua_class_function(L, "send", hlua_applet_http_send); |
| 9250 | hlua_class_function(L, "add_header", hlua_applet_http_addheader); |
| 9251 | hlua_class_function(L, "set_status", hlua_applet_http_status); |
| 9252 | hlua_class_function(L, "start_response", hlua_applet_http_start_response); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9253 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9254 | lua_settable(L, -3); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9255 | |
| 9256 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9257 | class_applet_http_ref = hlua_register_metatable(L, CLASS_APPLET_HTTP); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9258 | |
| 9259 | /* |
| 9260 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 9261 | * Register class TXN |
| 9262 | * |
| 9263 | */ |
| 9264 | |
| 9265 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9266 | lua_newtable(L); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 9267 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 9268 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9269 | lua_pushstring(L, "__index"); |
| 9270 | lua_newtable(L); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 9271 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 9272 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9273 | hlua_class_function(L, "set_priv", hlua_set_priv); |
| 9274 | hlua_class_function(L, "get_priv", hlua_get_priv); |
| 9275 | hlua_class_function(L, "set_var", hlua_set_var); |
| 9276 | hlua_class_function(L, "unset_var", hlua_unset_var); |
| 9277 | hlua_class_function(L, "get_var", hlua_get_var); |
| 9278 | hlua_class_function(L, "done", hlua_txn_done); |
| 9279 | hlua_class_function(L, "reply", hlua_txn_reply_new); |
| 9280 | hlua_class_function(L, "set_loglevel", hlua_txn_set_loglevel); |
| 9281 | hlua_class_function(L, "set_tos", hlua_txn_set_tos); |
| 9282 | hlua_class_function(L, "set_mark", hlua_txn_set_mark); |
| 9283 | hlua_class_function(L, "set_priority_class", hlua_txn_set_priority_class); |
| 9284 | hlua_class_function(L, "set_priority_offset", hlua_txn_set_priority_offset); |
| 9285 | hlua_class_function(L, "deflog", hlua_txn_deflog); |
| 9286 | hlua_class_function(L, "log", hlua_txn_log); |
| 9287 | hlua_class_function(L, "Debug", hlua_txn_log_debug); |
| 9288 | hlua_class_function(L, "Info", hlua_txn_log_info); |
| 9289 | hlua_class_function(L, "Warning", hlua_txn_log_warning); |
| 9290 | hlua_class_function(L, "Alert", hlua_txn_log_alert); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 9291 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9292 | lua_rawset(L, -3); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 9293 | |
| 9294 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9295 | class_txn_ref = hlua_register_metatable(L, CLASS_TXN); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9296 | |
| 9297 | /* |
| 9298 | * |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 9299 | * Register class reply |
| 9300 | * |
| 9301 | */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9302 | lua_newtable(L); |
| 9303 | lua_pushstring(L, "__index"); |
| 9304 | lua_newtable(L); |
| 9305 | hlua_class_function(L, "set_status", hlua_txn_reply_set_status); |
| 9306 | hlua_class_function(L, "add_header", hlua_txn_reply_add_header); |
| 9307 | hlua_class_function(L, "del_header", hlua_txn_reply_del_header); |
| 9308 | hlua_class_function(L, "set_body", hlua_txn_reply_set_body); |
| 9309 | lua_settable(L, -3); /* Sets the __index entry. */ |
| 9310 | class_txn_reply_ref = luaL_ref(L, LUA_REGISTRYINDEX); |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 9311 | |
| 9312 | |
| 9313 | /* |
| 9314 | * |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9315 | * Register class Socket |
| 9316 | * |
| 9317 | */ |
| 9318 | |
| 9319 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9320 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9321 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 9322 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9323 | lua_pushstring(L, "__index"); |
| 9324 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9325 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 9326 | #ifdef USE_OPENSSL |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9327 | hlua_class_function(L, "connect_ssl", hlua_socket_connect_ssl); |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 9328 | #endif |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9329 | hlua_class_function(L, "connect", hlua_socket_connect); |
| 9330 | hlua_class_function(L, "send", hlua_socket_send); |
| 9331 | hlua_class_function(L, "receive", hlua_socket_receive); |
| 9332 | hlua_class_function(L, "close", hlua_socket_close); |
| 9333 | hlua_class_function(L, "getpeername", hlua_socket_getpeername); |
| 9334 | hlua_class_function(L, "getsockname", hlua_socket_getsockname); |
| 9335 | hlua_class_function(L, "setoption", hlua_socket_setoption); |
| 9336 | hlua_class_function(L, "settimeout", hlua_socket_settimeout); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9337 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9338 | 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] | 9339 | |
| 9340 | /* Register the garbage collector entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9341 | lua_pushstring(L, "__gc"); |
| 9342 | lua_pushcclosure(L, hlua_socket_gc, 0); |
| 9343 | 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] | 9344 | |
| 9345 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9346 | class_socket_ref = hlua_register_metatable(L, CLASS_SOCKET); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9347 | |
Thierry Fournier | aafc777 | 2020-12-04 11:47:47 +0100 | [diff] [blame] | 9348 | lua_atpanic(L, hlua_panic_safe); |
| 9349 | |
| 9350 | return L; |
| 9351 | } |
| 9352 | |
| 9353 | void hlua_init(void) { |
| 9354 | int i; |
Amaury Denoyelle | 239fdbf | 2021-03-24 10:22:03 +0100 | [diff] [blame] | 9355 | char *errmsg; |
Thierry Fournier | aafc777 | 2020-12-04 11:47:47 +0100 | [diff] [blame] | 9356 | #ifdef USE_OPENSSL |
| 9357 | struct srv_kw *kw; |
| 9358 | int tmp_error; |
| 9359 | char *error; |
| 9360 | char *args[] = { /* SSL client configuration. */ |
| 9361 | "ssl", |
| 9362 | "verify", |
| 9363 | "none", |
| 9364 | NULL |
| 9365 | }; |
| 9366 | #endif |
| 9367 | |
| 9368 | /* Init post init function list head */ |
| 9369 | for (i = 0; i < MAX_THREADS + 1; i++) |
| 9370 | LIST_INIT(&hlua_init_functions[i]); |
| 9371 | |
| 9372 | /* Init state for common/shared lua parts */ |
| 9373 | hlua_state_id = 0; |
| 9374 | ha_set_tid(0); |
| 9375 | hlua_states[0] = hlua_init_state(0); |
| 9376 | |
| 9377 | /* Init state 1 for thread 0. We have at least one thread. */ |
| 9378 | hlua_state_id = 1; |
| 9379 | ha_set_tid(0); |
| 9380 | hlua_states[1] = hlua_init_state(1); |
| 9381 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9382 | /* Proxy and server configuration initialisation. */ |
Amaury Denoyelle | 239fdbf | 2021-03-24 10:22:03 +0100 | [diff] [blame] | 9383 | socket_proxy = alloc_new_proxy("LUA-SOCKET", PR_CAP_FE|PR_CAP_BE|PR_CAP_LUA, &errmsg); |
| 9384 | if (!socket_proxy) { |
| 9385 | fprintf(stderr, "Lua init: %s\n", errmsg); |
| 9386 | exit(1); |
| 9387 | } |
| 9388 | proxy_preset_defaults(socket_proxy); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9389 | |
| 9390 | /* Init TCP server: unchanged parameters */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 9391 | socket_tcp = new_server(socket_proxy); |
| 9392 | if (!socket_tcp) { |
| 9393 | fprintf(stderr, "Lua init: failed to allocate tcp server socket\n"); |
| 9394 | exit(1); |
| 9395 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9396 | |
| 9397 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9398 | /* Init TCP server: unchanged parameters */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 9399 | socket_ssl = new_server(socket_proxy); |
| 9400 | if (!socket_ssl) { |
| 9401 | fprintf(stderr, "Lua init: failed to allocate ssl server socket\n"); |
| 9402 | exit(1); |
| 9403 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9404 | |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 9405 | socket_ssl->use_ssl = 1; |
| 9406 | socket_ssl->xprt = xprt_get(XPRT_SSL); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9407 | |
Bertrand Jacquin | 80839ff | 2021-01-21 19:14:46 +0000 | [diff] [blame] | 9408 | for (i = 0; args[i] != NULL; i++) { |
| 9409 | 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] | 9410 | /* |
| 9411 | * |
| 9412 | * If the keyword is not known, we can search in the registered |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 9413 | * server keywords. This is useful to configure special SSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9414 | * features like client certificates and ssl_verify. |
| 9415 | * |
| 9416 | */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 9417 | tmp_error = kw->parse(args, &i, socket_proxy, socket_ssl, &error); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9418 | if (tmp_error != 0) { |
| 9419 | fprintf(stderr, "INTERNAL ERROR: %s\n", error); |
| 9420 | abort(); /* This must be never arrives because the command line |
| 9421 | not editable by the user. */ |
| 9422 | } |
Bertrand Jacquin | 80839ff | 2021-01-21 19:14:46 +0000 | [diff] [blame] | 9423 | i += kw->skip; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9424 | } |
| 9425 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9426 | #endif |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 9427 | |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 9428 | } |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 9429 | |
Tim Duesterhus | d0c0ca2 | 2020-07-04 11:53:26 +0200 | [diff] [blame] | 9430 | static void hlua_deinit() |
| 9431 | { |
Willy Tarreau | 186f376 | 2020-12-04 11:48:12 +0100 | [diff] [blame] | 9432 | int thr; |
| 9433 | |
| 9434 | for (thr = 0; thr < MAX_THREADS+1; thr++) { |
| 9435 | if (hlua_states[thr]) |
| 9436 | lua_close(hlua_states[thr]); |
| 9437 | } |
Willy Tarreau | 430bf4a | 2021-03-04 09:45:32 +0100 | [diff] [blame] | 9438 | |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 9439 | free_server(socket_tcp); |
Willy Tarreau | 430bf4a | 2021-03-04 09:45:32 +0100 | [diff] [blame] | 9440 | |
Willy Tarreau | 0f143af | 2021-03-05 10:41:48 +0100 | [diff] [blame] | 9441 | #ifdef USE_OPENSSL |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 9442 | free_server(socket_ssl); |
Willy Tarreau | 0f143af | 2021-03-05 10:41:48 +0100 | [diff] [blame] | 9443 | #endif |
Amaury Denoyelle | 239fdbf | 2021-03-24 10:22:03 +0100 | [diff] [blame] | 9444 | |
| 9445 | free_proxy(socket_proxy); |
Tim Duesterhus | d0c0ca2 | 2020-07-04 11:53:26 +0200 | [diff] [blame] | 9446 | } |
| 9447 | |
| 9448 | REGISTER_POST_DEINIT(hlua_deinit); |
| 9449 | |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 9450 | static void hlua_register_build_options(void) |
| 9451 | { |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 9452 | char *ptr = NULL; |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 9453 | |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 9454 | memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE); |
| 9455 | hap_register_build_opts(ptr, 1); |
| 9456 | } |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 9457 | |
| 9458 | INITCALL0(STG_REGISTER, hlua_register_build_options); |