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 | 5554264 | 2021-10-08 09:33:24 +0200 | [diff] [blame] | 35 | #include <haproxy/clock.h> |
Willy Tarreau | 7ea393d | 2020-06-04 18:02:10 +0200 | [diff] [blame] | 36 | #include <haproxy/connection.h> |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 37 | #include <haproxy/filters.h> |
Willy Tarreau | 5413a87 | 2020-06-02 19:33:08 +0200 | [diff] [blame] | 38 | #include <haproxy/h1.h> |
Willy Tarreau | 8641605 | 2020-06-04 09:20:54 +0200 | [diff] [blame] | 39 | #include <haproxy/hlua.h> |
Willy Tarreau | 8c79400 | 2020-06-04 10:05:25 +0200 | [diff] [blame] | 40 | #include <haproxy/hlua_fcn.h> |
Willy Tarreau | c2b1ff0 | 2020-06-04 21:21:03 +0200 | [diff] [blame] | 41 | #include <haproxy/http_ana.h> |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 42 | #include <haproxy/http_client.h> |
Willy Tarreau | 126ba3a | 2020-06-04 18:26:43 +0200 | [diff] [blame] | 43 | #include <haproxy/http_fetch.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 44 | #include <haproxy/http_htx.h> |
Willy Tarreau | c761f84 | 2020-06-04 11:40:28 +0200 | [diff] [blame] | 45 | #include <haproxy/http_rules.h> |
Willy Tarreau | 36979d9 | 2020-06-05 17:27:29 +0200 | [diff] [blame] | 46 | #include <haproxy/log.h> |
Willy Tarreau | 2cd5809 | 2020-06-04 15:10:43 +0200 | [diff] [blame] | 47 | #include <haproxy/map.h> |
Willy Tarreau | 8efbdfb | 2020-06-04 11:29:21 +0200 | [diff] [blame] | 48 | #include <haproxy/obj_type.h> |
Willy Tarreau | 225a90a | 2020-06-04 15:06:28 +0200 | [diff] [blame] | 49 | #include <haproxy/pattern.h> |
Willy Tarreau | 469509b | 2020-06-04 15:13:30 +0200 | [diff] [blame] | 50 | #include <haproxy/payload.h> |
Willy Tarreau | 3d6ee40 | 2021-05-08 20:28:07 +0200 | [diff] [blame] | 51 | #include <haproxy/proxy.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 52 | #include <haproxy/regex.h> |
Willy Tarreau | e6ce10b | 2020-06-04 15:33:47 +0200 | [diff] [blame] | 53 | #include <haproxy/sample.h> |
Willy Tarreau | 5edca2f | 2022-05-27 09:25:10 +0200 | [diff] [blame] | 54 | #include <haproxy/sc_strm.h> |
Willy Tarreau | 198e92a | 2021-03-05 10:23:32 +0100 | [diff] [blame] | 55 | #include <haproxy/server.h> |
Willy Tarreau | 48d25b3 | 2020-06-04 18:58:52 +0200 | [diff] [blame] | 56 | #include <haproxy/session.h> |
William Lallemand | 30fcca1 | 2022-03-30 12:03:12 +0200 | [diff] [blame] | 57 | #include <haproxy/ssl_ckch.h> |
| 58 | #include <haproxy/ssl_sock.h> |
Willy Tarreau | 2eec9b5 | 2020-06-04 19:58:55 +0200 | [diff] [blame] | 59 | #include <haproxy/stats-t.h> |
Willy Tarreau | cb086c6 | 2022-05-27 09:47:12 +0200 | [diff] [blame] | 60 | #include <haproxy/stconn.h> |
Willy Tarreau | dfd3de8 | 2020-06-04 23:46:14 +0200 | [diff] [blame] | 61 | #include <haproxy/stream.h> |
Willy Tarreau | cea0e1b | 2020-06-04 17:25:40 +0200 | [diff] [blame] | 62 | #include <haproxy/task.h> |
Willy Tarreau | 8b550af | 2020-06-04 17:42:48 +0200 | [diff] [blame] | 63 | #include <haproxy/tcp_rules.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 64 | #include <haproxy/thread.h> |
Willy Tarreau | 48fbcae | 2020-06-03 18:09:46 +0200 | [diff] [blame] | 65 | #include <haproxy/tools.h> |
Willy Tarreau | a171892 | 2020-06-04 16:25:31 +0200 | [diff] [blame] | 66 | #include <haproxy/vars.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 67 | #include <haproxy/xref.h> |
Aurelien DARRAGON | c84899c | 2023-02-20 18:18:59 +0100 | [diff] [blame] | 68 | #include <haproxy/event_hdl.h> |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 69 | |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 70 | /* Lua uses longjmp to perform yield or throwing errors. This |
| 71 | * macro is used only for identifying the function that can |
| 72 | * not return because a longjmp is executed. |
| 73 | * __LJMP marks a prototype of hlua file that can use longjmp. |
| 74 | * WILL_LJMP() marks an lua function that will use longjmp. |
| 75 | * MAY_LJMP() marks an lua function that may use longjmp. |
| 76 | */ |
| 77 | #define __LJMP |
Willy Tarreau | 4e7cc33 | 2018-10-20 17:45:48 +0200 | [diff] [blame] | 78 | #define WILL_LJMP(func) do { func; my_unreachable(); } while(0) |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 79 | #define MAY_LJMP(func) func |
| 80 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 81 | /* This couple of function executes securely some Lua calls outside of |
| 82 | * the lua runtime environment. Each Lua call can return a longjmp |
| 83 | * if it encounter a memory error. |
| 84 | * |
| 85 | * Lua documentation extract: |
| 86 | * |
| 87 | * If an error happens outside any protected environment, Lua calls |
| 88 | * a panic function (see lua_atpanic) and then calls abort, thus |
| 89 | * exiting the host application. Your panic function can avoid this |
| 90 | * exit by never returning (e.g., doing a long jump to your own |
| 91 | * recovery point outside Lua). |
| 92 | * |
| 93 | * The panic function runs as if it were a message handler (see |
Willy Tarreau | 3dfb7da | 2022-03-02 22:33:39 +0100 | [diff] [blame] | 94 | * #2.3); in particular, the error message is at the top of the |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 95 | * stack. However, there is no guarantee about stack space. To push |
| 96 | * anything on the stack, the panic function must first check the |
Willy Tarreau | 3dfb7da | 2022-03-02 22:33:39 +0100 | [diff] [blame] | 97 | * available space (see #4.2). |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 98 | * |
| 99 | * We must check all the Lua entry point. This includes: |
| 100 | * - The include/proto/hlua.h exported functions |
| 101 | * - the task wrapper function |
| 102 | * - The action wrapper function |
| 103 | * - The converters wrapper function |
| 104 | * - The sample-fetch wrapper functions |
| 105 | * |
Ilya Shipitsin | 46a030c | 2020-07-05 16:36:08 +0500 | [diff] [blame] | 106 | * It is tolerated that the initialisation function returns an abort. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 107 | * Before each Lua abort, an error message is written on stderr. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 108 | * |
| 109 | * The macro SET_SAFE_LJMP initialise the longjmp. The Macro |
| 110 | * RESET_SAFE_LJMP reset the longjmp. These function must be macro |
| 111 | * because they must be exists in the program stack when the longjmp |
| 112 | * is called. |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 113 | * |
| 114 | * Note that the Lua processing is not really thread safe. It provides |
| 115 | * heavy system which consists to add our own lock function in the Lua |
| 116 | * code and recompile the library. This system will probably not accepted |
| 117 | * by maintainers of various distribs. |
| 118 | * |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 119 | * 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] | 120 | * quick looking on the Lua sources displays a lua_lock() a the start |
| 121 | * of function and a lua_unlock() at the end of the function. So I |
| 122 | * conclude that the Lua thread safe mode just perform a mutex around |
| 123 | * all execution. So I prefer to do this in the HAProxy code, it will be |
| 124 | * easier for distro maintainers. |
| 125 | * |
| 126 | * Note that the HAProxy lua functions rounded by the macro SET_SAFE_LJMP |
| 127 | * and RESET_SAFE_LJMP manipulates the Lua stack, so it will be careful |
| 128 | * to set mutex around these functions. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 129 | */ |
Willy Tarreau | 86abe44 | 2018-11-25 20:12:18 +0100 | [diff] [blame] | 130 | __decl_spinlock(hlua_global_lock); |
Thierry FOURNIER | ffbad79 | 2017-07-12 11:39:04 +0200 | [diff] [blame] | 131 | THREAD_LOCAL jmp_buf safe_ljmp_env; |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 132 | static int hlua_panic_safe(lua_State *L) { return 0; } |
Willy Tarreau | 6a51090 | 2021-07-14 19:41:25 +0200 | [diff] [blame] | 133 | 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] | 134 | |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 135 | /* This is the chained list of struct hlua_function referenced |
| 136 | * for haproxy action, sample-fetches, converters, cli and |
| 137 | * applet bindings. It is used for a post-initialisation control. |
| 138 | */ |
| 139 | static struct list referenced_functions = LIST_HEAD_INIT(referenced_functions); |
| 140 | |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 141 | /* This variable is used only during initialization to identify the Lua state |
| 142 | * currently being initialized. 0 is the common lua state, 1 to n are the Lua |
| 143 | * states dedicated to each thread (in this case hlua_state_id==tid+1). |
| 144 | */ |
| 145 | static int hlua_state_id; |
| 146 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 147 | /* This is a NULL-terminated list of lua file which are referenced to load per thread */ |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 148 | static char ***per_thread_load = NULL; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 149 | |
| 150 | lua_State *hlua_init_state(int thread_id); |
| 151 | |
Willy Tarreau | 1e7bef1 | 2021-08-20 15:47:25 +0200 | [diff] [blame] | 152 | /* This function takes the Lua global lock. Keep this function's visibility |
| 153 | * global so that it can appear in stack dumps and performance profiles! |
| 154 | */ |
Aurelien DARRAGON | e36f803 | 2023-03-21 13:22:33 +0100 | [diff] [blame] | 155 | static inline void lua_take_global_lock() |
Willy Tarreau | 1e7bef1 | 2021-08-20 15:47:25 +0200 | [diff] [blame] | 156 | { |
| 157 | HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock); |
| 158 | } |
| 159 | |
| 160 | static inline void lua_drop_global_lock() |
| 161 | { |
| 162 | HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); |
| 163 | } |
| 164 | |
Aurelien DARRAGON | 6b0b9bd | 2023-03-21 14:02:16 +0100 | [diff] [blame] | 165 | /* lua lock helpers: only lock when required |
| 166 | * |
| 167 | * state_id == 0: we're operating on the main lua stack (shared between |
| 168 | * os threads), so we need to acquire the main lock |
| 169 | * |
| 170 | * If the thread already owns the lock (_hlua_locked != 0), skip the lock |
| 171 | * attempt. This could happen if we run under protected lua environment. |
| 172 | * Not doing this could result in deadlocks because of nested locking |
| 173 | * attempts from the same thread |
| 174 | */ |
| 175 | static THREAD_LOCAL int _hlua_locked = 0; |
Aurelien DARRAGON | e36f803 | 2023-03-21 13:22:33 +0100 | [diff] [blame] | 176 | static inline void hlua_lock(struct hlua *hlua) |
| 177 | { |
Aurelien DARRAGON | 6b0b9bd | 2023-03-21 14:02:16 +0100 | [diff] [blame] | 178 | if (hlua->state_id != 0) |
| 179 | return; |
| 180 | if (!_hlua_locked) |
Aurelien DARRAGON | e36f803 | 2023-03-21 13:22:33 +0100 | [diff] [blame] | 181 | lua_take_global_lock(); |
Aurelien DARRAGON | 6b0b9bd | 2023-03-21 14:02:16 +0100 | [diff] [blame] | 182 | _hlua_locked += 1; |
Aurelien DARRAGON | e36f803 | 2023-03-21 13:22:33 +0100 | [diff] [blame] | 183 | } |
| 184 | static inline void hlua_unlock(struct hlua *hlua) |
| 185 | { |
Aurelien DARRAGON | 6b0b9bd | 2023-03-21 14:02:16 +0100 | [diff] [blame] | 186 | if (hlua->state_id != 0) |
| 187 | return; |
| 188 | BUG_ON(_hlua_locked <= 0); |
| 189 | _hlua_locked--; |
| 190 | /* drop the lock once the lock count reaches 0 */ |
| 191 | if (!_hlua_locked) |
Aurelien DARRAGON | e36f803 | 2023-03-21 13:22:33 +0100 | [diff] [blame] | 192 | lua_drop_global_lock(); |
| 193 | } |
| 194 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 195 | #define SET_SAFE_LJMP_L(__L, __HLUA) \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 196 | ({ \ |
| 197 | int ret; \ |
Aurelien DARRAGON | e36f803 | 2023-03-21 13:22:33 +0100 | [diff] [blame] | 198 | hlua_lock(__HLUA); \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 199 | if (setjmp(safe_ljmp_env) != 0) { \ |
| 200 | lua_atpanic(__L, hlua_panic_safe); \ |
| 201 | ret = 0; \ |
Aurelien DARRAGON | e36f803 | 2023-03-21 13:22:33 +0100 | [diff] [blame] | 202 | hlua_unlock(__HLUA); \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 203 | } else { \ |
| 204 | lua_atpanic(__L, hlua_panic_ljmp); \ |
| 205 | ret = 1; \ |
| 206 | } \ |
| 207 | ret; \ |
| 208 | }) |
| 209 | |
| 210 | /* If we are the last function catching Lua errors, we |
| 211 | * must reset the panic function. |
| 212 | */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 213 | #define RESET_SAFE_LJMP_L(__L, __HLUA) \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 214 | do { \ |
| 215 | lua_atpanic(__L, hlua_panic_safe); \ |
Aurelien DARRAGON | e36f803 | 2023-03-21 13:22:33 +0100 | [diff] [blame] | 216 | hlua_unlock(__HLUA); \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 217 | } while(0) |
| 218 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 219 | #define SET_SAFE_LJMP(__HLUA) \ |
| 220 | SET_SAFE_LJMP_L((__HLUA)->T, __HLUA) |
| 221 | |
| 222 | #define RESET_SAFE_LJMP(__HLUA) \ |
| 223 | RESET_SAFE_LJMP_L((__HLUA)->T, __HLUA) |
| 224 | |
| 225 | #define SET_SAFE_LJMP_PARENT(__HLUA) \ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 226 | SET_SAFE_LJMP_L(hlua_states[(__HLUA)->state_id], __HLUA) |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 227 | |
| 228 | #define RESET_SAFE_LJMP_PARENT(__HLUA) \ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 229 | RESET_SAFE_LJMP_L(hlua_states[(__HLUA)->state_id], __HLUA) |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 230 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 231 | /* Applet status flags */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 232 | #define APPLET_DONE 0x01 /* applet processing is done. */ |
Christopher Faulet | 18c2e8d | 2019-03-01 12:02:08 +0100 | [diff] [blame] | 233 | /* unused: 0x02 */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 234 | #define APPLET_HDR_SENT 0x04 /* Response header sent. */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 235 | /* unused: 0x08, 0x10 */ |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 236 | #define APPLET_HTTP11 0x20 /* Last chunk sent. */ |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 237 | #define APPLET_RSP_SENT 0x40 /* The response was fully sent */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 238 | |
Thierry Fournier | afc63e2 | 2020-11-28 17:06:51 +0100 | [diff] [blame] | 239 | /* The main Lua execution context. The 0 index is the |
| 240 | * common state shared by all threads. |
| 241 | */ |
Willy Tarreau | 186f376 | 2020-12-04 11:48:12 +0100 | [diff] [blame] | 242 | static lua_State *hlua_states[MAX_THREADS + 1]; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 243 | |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 244 | #define HLUA_FLT_CB_FINAL 0x00000001 |
| 245 | #define HLUA_FLT_CB_RETVAL 0x00000002 |
| 246 | #define HLUA_FLT_CB_ARG_CHN 0x00000004 |
| 247 | #define HLUA_FLT_CB_ARG_HTTP_MSG 0x00000008 |
| 248 | |
Christopher Faulet | 9f55a50 | 2020-02-25 15:21:02 +0100 | [diff] [blame] | 249 | #define HLUA_FLT_CTX_FL_PAYLOAD 0x00000001 |
| 250 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 251 | struct hlua_reg_filter { |
| 252 | char *name; |
| 253 | int flt_ref[MAX_THREADS + 1]; |
| 254 | int fun_ref[MAX_THREADS + 1]; |
| 255 | struct list l; |
| 256 | }; |
| 257 | |
| 258 | struct hlua_flt_config { |
| 259 | struct hlua_reg_filter *reg; |
| 260 | int ref[MAX_THREADS + 1]; |
| 261 | char **args; |
| 262 | }; |
| 263 | |
| 264 | struct hlua_flt_ctx { |
| 265 | int ref; /* ref to the filter lua object */ |
| 266 | struct hlua *hlua[2]; /* lua runtime context (0: request, 1: response) */ |
| 267 | unsigned int cur_off[2]; /* current offset (0: request, 1: response) */ |
| 268 | unsigned int cur_len[2]; /* current forwardable length (0: request, 1: response) */ |
| 269 | unsigned int flags; /* HLUA_FLT_CTX_FL_* */ |
| 270 | }; |
| 271 | |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 272 | /* appctx context used by the cosockets */ |
| 273 | struct hlua_csk_ctx { |
| 274 | int connected; |
| 275 | struct xref xref; /* cross reference with the Lua object owner. */ |
| 276 | struct list wake_on_read; |
| 277 | struct list wake_on_write; |
| 278 | struct appctx *appctx; |
| 279 | int die; |
| 280 | }; |
| 281 | |
Willy Tarreau | e23f33b | 2022-05-06 14:07:13 +0200 | [diff] [blame] | 282 | /* appctx context used by TCP services */ |
| 283 | struct hlua_tcp_ctx { |
| 284 | struct hlua *hlua; |
| 285 | int flags; |
| 286 | struct task *task; |
| 287 | }; |
| 288 | |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 289 | /* appctx context used by HTTP services */ |
| 290 | struct hlua_http_ctx { |
| 291 | struct hlua *hlua; |
| 292 | int left_bytes; /* The max amount of bytes that we can read. */ |
| 293 | int flags; |
| 294 | int status; |
| 295 | const char *reason; |
| 296 | struct task *task; |
| 297 | }; |
| 298 | |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 299 | /* used by registered CLI keywords */ |
| 300 | struct hlua_cli_ctx { |
| 301 | struct hlua *hlua; |
| 302 | struct task *task; |
| 303 | struct hlua_function *fcn; |
| 304 | }; |
| 305 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 306 | DECLARE_STATIC_POOL(pool_head_hlua_flt_ctx, "hlua_flt_ctx", sizeof(struct hlua_flt_ctx)); |
| 307 | |
Christopher Faulet | 9f55a50 | 2020-02-25 15:21:02 +0100 | [diff] [blame] | 308 | static int hlua_filter_from_payload(struct filter *filter); |
| 309 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 310 | /* This is the chained list of struct hlua_flt referenced |
| 311 | * for haproxy filters. It is used for a post-initialisation control. |
| 312 | */ |
| 313 | static struct list referenced_filters = LIST_HEAD_INIT(referenced_filters); |
| 314 | |
| 315 | |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 316 | /* This is the memory pool containing struct lua for applets |
| 317 | * (including cli). |
| 318 | */ |
Willy Tarreau | 8ceae72 | 2018-11-26 11:58:30 +0100 | [diff] [blame] | 319 | DECLARE_STATIC_POOL(pool_head_hlua, "hlua", sizeof(struct hlua)); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 320 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 321 | /* Used for Socket connection. */ |
Amaury Denoyelle | 239fdbf | 2021-03-24 10:22:03 +0100 | [diff] [blame] | 322 | static struct proxy *socket_proxy; |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 323 | static struct server *socket_tcp; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 324 | #ifdef USE_OPENSSL |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 325 | static struct server *socket_ssl; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 326 | #endif |
| 327 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 328 | /* List head of the function called at the initialisation time. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 329 | struct list hlua_init_functions[MAX_THREADS + 1]; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 330 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 331 | /* The following variables contains the reference of the different |
| 332 | * Lua classes. These references are useful for identify metadata |
| 333 | * associated with an object. |
| 334 | */ |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 335 | static int class_txn_ref; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 336 | static int class_socket_ref; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 337 | static int class_channel_ref; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 338 | static int class_fetches_ref; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 339 | static int class_converters_ref; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 340 | static int class_http_ref; |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 341 | static int class_http_msg_ref; |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 342 | static int class_httpclient_ref; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 343 | static int class_map_ref; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 344 | static int class_applet_tcp_ref; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 345 | static int class_applet_http_ref; |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 346 | static int class_txn_reply_ref; |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 347 | |
Aurelien DARRAGON | da9503c | 2022-11-25 09:10:07 +0100 | [diff] [blame] | 348 | /* Lua max execution timeouts. By default, stream-related |
| 349 | * lua coroutines (e.g.: actions) have a short timeout. |
| 350 | * On the other hand tasks coroutines don't have a timeout because |
| 351 | * a task may remain alive during all the haproxy execution. |
| 352 | * |
| 353 | * Timeouts are expressed in milliseconds, they are meant to be used |
| 354 | * with hlua timer's API exclusively. |
| 355 | * 0 means no timeout |
| 356 | */ |
Aurelien DARRAGON | 58e36e5 | 2023-04-06 22:51:56 +0200 | [diff] [blame] | 357 | static uint32_t hlua_timeout_burst = 1000; /* burst timeout. */ |
Aurelien DARRAGON | da9503c | 2022-11-25 09:10:07 +0100 | [diff] [blame] | 358 | static uint32_t hlua_timeout_session = 4000; /* session timeout. */ |
| 359 | static uint32_t hlua_timeout_task = 0; /* task timeout. */ |
| 360 | static uint32_t hlua_timeout_applet = 4000; /* applet timeout. */ |
| 361 | |
| 362 | /* hlua multipurpose timer: |
| 363 | * used to compute burst lua time (within a single hlua_ctx_resume()) |
| 364 | * and cumulative lua time for a given coroutine, and to check |
| 365 | * the lua coroutine against the configured timeouts |
| 366 | */ |
| 367 | |
| 368 | /* fetch per-thread cpu_time with ms precision (may wrap) */ |
| 369 | static inline uint32_t _hlua_time_ms() |
| 370 | { |
| 371 | /* We're interested in the current cpu time in ms, which will be returned |
| 372 | * as a uint32_t to save some space. |
| 373 | * We must take the following into account: |
| 374 | * |
| 375 | * - now_cpu_time_fast() which returns the time in nanoseconds as a uint64_t |
| 376 | * will wrap every 585 years. |
| 377 | * - uint32_t may only contain 4294967295ms (~=49.7 days), so _hlua_time_ms() |
| 378 | * itself will also wrap every 49.7 days. |
| 379 | * |
| 380 | * While we can safely ignore the now_cpu_time_fast() wrap, we must |
| 381 | * take care of the uint32_t wrap by making sure to exclusively |
| 382 | * manipulate the time using uint32_t everywhere _hlua_time_ms() |
| 383 | * is involved. |
| 384 | */ |
| 385 | return (uint32_t)(now_cpu_time_fast() / 1000000ULL); |
| 386 | } |
| 387 | |
| 388 | /* computes time spent in a single lua execution (in ms) */ |
| 389 | static inline uint32_t _hlua_time_burst(const struct hlua_timer *timer) |
| 390 | { |
| 391 | uint32_t burst_ms; |
| 392 | |
| 393 | /* wrapping is expected and properly |
| 394 | * handled thanks to _hlua_time_ms() and burst_ms |
| 395 | * being of the same type |
| 396 | */ |
| 397 | burst_ms = _hlua_time_ms() - timer->start; |
| 398 | return burst_ms; |
| 399 | } |
| 400 | |
| 401 | static inline void hlua_timer_init(struct hlua_timer *timer, unsigned int max) |
| 402 | { |
| 403 | timer->cumulative = 0; |
| 404 | timer->burst = 0; |
| 405 | timer->max = max; |
| 406 | } |
| 407 | |
| 408 | /* reset the timer ctx between 2 yields */ |
| 409 | static inline void hlua_timer_reset(struct hlua_timer *timer) |
| 410 | { |
| 411 | timer->cumulative += timer->burst; |
| 412 | timer->burst = 0; |
| 413 | } |
| 414 | |
| 415 | /* start the timer right before a new execution */ |
| 416 | static inline void hlua_timer_start(struct hlua_timer *timer) |
| 417 | { |
| 418 | timer->start = _hlua_time_ms(); |
| 419 | } |
| 420 | |
| 421 | /* update hlua timer when finishing an execution */ |
| 422 | static inline void hlua_timer_stop(struct hlua_timer *timer) |
| 423 | { |
| 424 | timer->burst += _hlua_time_burst(timer); |
| 425 | } |
| 426 | |
Aurelien DARRAGON | 58e36e5 | 2023-04-06 22:51:56 +0200 | [diff] [blame] | 427 | /* check the timers for current hlua context: |
| 428 | * - first check for burst timeout (max execution time for the current |
| 429 | hlua resume, ie: time between effective yields) |
| 430 | * - then check for yield cumulative timeout |
| 431 | * |
Aurelien DARRAGON | da9503c | 2022-11-25 09:10:07 +0100 | [diff] [blame] | 432 | * Returns 1 if the check succeeded and 0 if it failed |
| 433 | * (ie: timeout exceeded) |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 434 | */ |
Aurelien DARRAGON | da9503c | 2022-11-25 09:10:07 +0100 | [diff] [blame] | 435 | static inline int hlua_timer_check(const struct hlua_timer *timer) |
| 436 | { |
| 437 | uint32_t pburst = _hlua_time_burst(timer); /* pending burst time in ms */ |
| 438 | |
Aurelien DARRAGON | 58e36e5 | 2023-04-06 22:51:56 +0200 | [diff] [blame] | 439 | if (hlua_timeout_burst && (timer->burst + pburst) > hlua_timeout_burst) |
| 440 | return 0; /* burst timeout exceeded */ |
Aurelien DARRAGON | da9503c | 2022-11-25 09:10:07 +0100 | [diff] [blame] | 441 | if (timer->max && (timer->cumulative + timer->burst + pburst) > timer->max) |
| 442 | return 0; /* cumulative timeout exceeded */ |
| 443 | return 1; /* ok */ |
| 444 | } |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 445 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 446 | /* Interrupts the Lua processing each "hlua_nb_instruction" instructions. |
| 447 | * it is used for preventing infinite loops. |
| 448 | * |
| 449 | * I test the scheer with an infinite loop containing one incrementation |
| 450 | * and one test. I run this loop between 10 seconds, I raise a ceil of |
| 451 | * 710M loops from one interrupt each 9000 instructions, so I fix the value |
| 452 | * to one interrupt each 10 000 instructions. |
| 453 | * |
| 454 | * configured | Number of |
| 455 | * instructions | loops executed |
| 456 | * between two | in milions |
| 457 | * forced yields | |
| 458 | * ---------------+--------------- |
| 459 | * 10 | 160 |
| 460 | * 500 | 670 |
| 461 | * 1000 | 680 |
| 462 | * 5000 | 700 |
| 463 | * 7000 | 700 |
| 464 | * 8000 | 700 |
| 465 | * 9000 | 710 <- ceil |
| 466 | * 10000 | 710 |
| 467 | * 100000 | 710 |
| 468 | * 1000000 | 710 |
| 469 | * |
| 470 | */ |
| 471 | static unsigned int hlua_nb_instruction = 10000; |
| 472 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 473 | /* Descriptor for the memory allocation state. The limit is pre-initialised to |
| 474 | * 0 until it is replaced by "tune.lua.maxmem" during the config parsing, or it |
| 475 | * is replaced with ~0 during post_init after everything was loaded. This way |
| 476 | * it is guaranteed that if limit is ~0 the boot is complete and that if it's |
| 477 | * zero it's not yet limited and proper accounting is required. |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 478 | */ |
| 479 | struct hlua_mem_allocator { |
| 480 | size_t allocated; |
| 481 | size_t limit; |
| 482 | }; |
| 483 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 484 | static struct hlua_mem_allocator hlua_global_allocator THREAD_ALIGNED(64); |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 485 | |
Aurelien DARRAGON | c84899c | 2023-02-20 18:18:59 +0100 | [diff] [blame] | 486 | /* hlua event subscription */ |
| 487 | struct hlua_event_sub { |
| 488 | int fcn_ref; |
| 489 | int state_id; |
| 490 | struct hlua *hlua; |
| 491 | struct task *task; |
| 492 | event_hdl_async_equeue equeue; |
| 493 | struct event_hdl_sub *sub; |
| 494 | uint8_t paused; |
| 495 | }; |
| 496 | |
| 497 | /* This is the memory pool containing struct hlua_event_sub |
| 498 | * for event subscriptions from lua |
| 499 | */ |
| 500 | DECLARE_STATIC_POOL(pool_head_hlua_event_sub, "hlua_esub", sizeof(struct hlua_event_sub)); |
| 501 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 502 | /* These functions converts types between HAProxy internal args or |
| 503 | * sample and LUA types. Another function permits to check if the |
| 504 | * LUA stack contains arguments according with an required ARG_T |
| 505 | * format. |
| 506 | */ |
| 507 | static int hlua_arg2lua(lua_State *L, const struct arg *arg); |
| 508 | static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 509 | __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] | 510 | uint64_t mask, struct proxy *p); |
Willy Tarreau | 5eadada | 2015-03-10 17:28:54 +0100 | [diff] [blame] | 511 | static int hlua_smp2lua(lua_State *L, struct sample *smp); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 512 | static int hlua_smp2lua_str(lua_State *L, struct sample *smp); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 513 | static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp); |
| 514 | |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 515 | __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] | 516 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 517 | struct prepend_path { |
| 518 | struct list l; |
| 519 | char *type; |
| 520 | char *path; |
| 521 | }; |
| 522 | |
| 523 | static struct list prepend_path_list = LIST_HEAD_INIT(prepend_path_list); |
| 524 | |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 525 | #define SEND_ERR(__be, __fmt, __args...) \ |
| 526 | do { \ |
| 527 | send_log(__be, LOG_ERR, __fmt, ## __args); \ |
| 528 | if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) \ |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 529 | ha_alert(__fmt, ## __args); \ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 530 | } while (0) |
| 531 | |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 532 | static inline struct hlua_function *new_hlua_function() |
| 533 | { |
| 534 | struct hlua_function *fcn; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 535 | int i; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 536 | |
| 537 | fcn = calloc(1, sizeof(*fcn)); |
| 538 | if (!fcn) |
| 539 | return NULL; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 540 | LIST_APPEND(&referenced_functions, &fcn->l); |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 541 | for (i = 0; i < MAX_THREADS + 1; i++) |
| 542 | fcn->function_ref[i] = -1; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 543 | return fcn; |
| 544 | } |
| 545 | |
Christopher Faulet | dda4444 | 2021-04-12 14:05:43 +0200 | [diff] [blame] | 546 | static inline void release_hlua_function(struct hlua_function *fcn) |
| 547 | { |
| 548 | if (!fcn) |
| 549 | return; |
| 550 | if (fcn->name) |
| 551 | ha_free(&fcn->name); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 552 | LIST_DELETE(&fcn->l); |
Christopher Faulet | dda4444 | 2021-04-12 14:05:43 +0200 | [diff] [blame] | 553 | ha_free(&fcn); |
| 554 | } |
| 555 | |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 556 | /* If the common state is set, the stack id is 0, otherwise it is the tid + 1 */ |
| 557 | static inline int fcn_ref_to_stack_id(struct hlua_function *fcn) |
| 558 | { |
| 559 | if (fcn->function_ref[0] == -1) |
| 560 | return tid + 1; |
| 561 | return 0; |
| 562 | } |
| 563 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 564 | /* Create a new registered filter. Only its name is filled */ |
| 565 | static inline struct hlua_reg_filter *new_hlua_reg_filter(const char *name) |
| 566 | { |
| 567 | struct hlua_reg_filter *reg_flt; |
| 568 | int i; |
| 569 | |
| 570 | reg_flt = calloc(1, sizeof(*reg_flt)); |
| 571 | if (!reg_flt) |
| 572 | return NULL; |
| 573 | reg_flt->name = strdup(name); |
| 574 | if (!reg_flt->name) { |
| 575 | free(reg_flt); |
| 576 | return NULL; |
| 577 | } |
| 578 | LIST_APPEND(&referenced_filters, ®_flt->l); |
| 579 | for (i = 0; i < MAX_THREADS + 1; i++) { |
| 580 | reg_flt->flt_ref[i] = -1; |
| 581 | reg_flt->fun_ref[i] = -1; |
| 582 | } |
| 583 | return reg_flt; |
| 584 | } |
| 585 | |
| 586 | /* Release a registered filter */ |
| 587 | static inline void release_hlua_reg_filter(struct hlua_reg_filter *reg_flt) |
| 588 | { |
| 589 | if (!reg_flt) |
| 590 | return; |
| 591 | if (reg_flt->name) |
| 592 | ha_free(®_flt->name); |
| 593 | LIST_DELETE(®_flt->l); |
| 594 | ha_free(®_flt); |
| 595 | } |
| 596 | |
| 597 | /* If the common state is set, the stack id is 0, otherwise it is the tid + 1 */ |
| 598 | static inline int reg_flt_to_stack_id(struct hlua_reg_filter *reg_flt) |
| 599 | { |
| 600 | if (reg_flt->fun_ref[0] == -1) |
| 601 | return tid + 1; |
| 602 | return 0; |
| 603 | } |
| 604 | |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 605 | /* Used to check an Lua function type in the stack. It creates and |
| 606 | * returns a reference of the function. This function throws an |
Aurelien DARRAGON | f8f8a2b | 2023-03-02 17:50:49 +0100 | [diff] [blame] | 607 | * error if the argument is not a "function". |
| 608 | * When no longer used, the ref must be released with hlua_unref() |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 609 | */ |
Aurelien DARRAGON | 9ee0d04 | 2023-03-20 18:36:08 +0100 | [diff] [blame] | 610 | __LJMP int hlua_checkfunction(lua_State *L, int argno) |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 611 | { |
| 612 | if (!lua_isfunction(L, argno)) { |
Thierry FOURNIER | fd1e955 | 2018-02-23 18:41:18 +0100 | [diff] [blame] | 613 | 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] | 614 | WILL_LJMP(luaL_argerror(L, argno, msg)); |
| 615 | } |
| 616 | lua_pushvalue(L, argno); |
| 617 | return luaL_ref(L, LUA_REGISTRYINDEX); |
| 618 | } |
| 619 | |
Christopher Faulet | ba9e21d | 2020-02-25 10:20:04 +0100 | [diff] [blame] | 620 | /* Used to check an Lua table type in the stack. It creates and |
| 621 | * returns a reference of the table. This function throws an |
Aurelien DARRAGON | f8f8a2b | 2023-03-02 17:50:49 +0100 | [diff] [blame] | 622 | * error if the argument is not a "table". |
| 623 | * When no longer used, the ref must be released with hlua_unref() |
Christopher Faulet | ba9e21d | 2020-02-25 10:20:04 +0100 | [diff] [blame] | 624 | */ |
Aurelien DARRAGON | 9ee0d04 | 2023-03-20 18:36:08 +0100 | [diff] [blame] | 625 | __LJMP int hlua_checktable(lua_State *L, int argno) |
Christopher Faulet | ba9e21d | 2020-02-25 10:20:04 +0100 | [diff] [blame] | 626 | { |
| 627 | if (!lua_istable(L, argno)) { |
| 628 | const char *msg = lua_pushfstring(L, "table expected, got %s", luaL_typename(L, argno)); |
| 629 | WILL_LJMP(luaL_argerror(L, argno, msg)); |
| 630 | } |
| 631 | lua_pushvalue(L, argno); |
| 632 | return luaL_ref(L, LUA_REGISTRYINDEX); |
| 633 | } |
| 634 | |
Aurelien DARRAGON | f8f8a2b | 2023-03-02 17:50:49 +0100 | [diff] [blame] | 635 | /* Get a reference to the object that is at the top of the stack |
| 636 | * The referenced object will be popped from the stack |
| 637 | * |
| 638 | * The function returns the reference to the object which must |
| 639 | * be cleared using hlua_unref() when no longer used |
| 640 | */ |
| 641 | __LJMP int hlua_ref(lua_State *L) |
| 642 | { |
| 643 | return MAY_LJMP(luaL_ref(L, LUA_REGISTRYINDEX)); |
| 644 | } |
| 645 | |
| 646 | /* Pushes a reference previously created using luaL_ref(L, LUA_REGISTRYINDEX) |
| 647 | * on <L> stack |
| 648 | * (ie: hlua_checkfunction(), hlua_checktable() or hlua_ref()) |
| 649 | * |
| 650 | * When the reference is no longer used, it should be released by calling |
| 651 | * hlua_unref() |
| 652 | * |
| 653 | * <L> can be from any co-routine as long as it belongs to the same lua |
| 654 | * parent state that the one used to get the reference. |
| 655 | */ |
| 656 | void hlua_pushref(lua_State *L, int ref) |
| 657 | { |
| 658 | lua_rawgeti(L, LUA_REGISTRYINDEX, ref); |
| 659 | } |
| 660 | |
| 661 | /* Releases a reference previously created using luaL_ref(L, LUA_REGISTRYINDEX) |
| 662 | * (ie: hlua_checkfunction(), hlua_checktable() or hlua_ref()) |
| 663 | * |
| 664 | * This will allow the reference to be reused and the referred object |
| 665 | * to be garbage collected. |
| 666 | * |
| 667 | * <L> can be from any co-routine as long as it belongs to the same lua |
| 668 | * parent state that the one used to get the reference. |
| 669 | */ |
| 670 | void hlua_unref(lua_State *L, int ref) |
| 671 | { |
| 672 | luaL_unref(L, LUA_REGISTRYINDEX, ref); |
| 673 | } |
| 674 | |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 675 | __LJMP const char *hlua_traceback(lua_State *L, const char* sep) |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 676 | { |
| 677 | lua_Debug ar; |
| 678 | int level = 0; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 679 | struct buffer *msg = get_trash_chunk(); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 680 | |
| 681 | while (lua_getstack(L, level++, &ar)) { |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 682 | /* Fill fields: |
| 683 | * 'S': fills in the fields source, short_src, linedefined, lastlinedefined, and what; |
| 684 | * 'l': fills in the field currentline; |
| 685 | * 'n': fills in the field name and namewhat; |
| 686 | * 't': fills in the field istailcall; |
| 687 | */ |
| 688 | lua_getinfo(L, "Slnt", &ar); |
| 689 | |
Willy Tarreau | 5c14340 | 2022-06-19 17:35:53 +0200 | [diff] [blame] | 690 | /* skip these empty entries, usually they come from deep C functions */ |
| 691 | if (ar.currentline < 0 && *ar.what == 'C' && !*ar.namewhat && !ar.name) |
| 692 | continue; |
| 693 | |
| 694 | /* Add separator */ |
| 695 | if (b_data(msg)) |
| 696 | chunk_appendf(msg, "%s", sep); |
| 697 | |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 698 | /* Append code localisation */ |
| 699 | if (ar.currentline > 0) |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 700 | chunk_appendf(msg, "%s:%d: ", ar.short_src, ar.currentline); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 701 | else |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 702 | chunk_appendf(msg, "%s: ", ar.short_src); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 703 | |
| 704 | /* |
| 705 | * Get function name |
| 706 | * |
| 707 | * if namewhat is no empty, name is defined. |
| 708 | * what contains "Lua" for Lua function, "C" for C function, |
| 709 | * or "main" for main code. |
| 710 | */ |
| 711 | 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] | 712 | chunk_appendf(msg, "in %s '%s'", ar.namewhat, ar.name); /* use it */ |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 713 | |
| 714 | 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] | 715 | chunk_appendf(msg, "in main chunk"); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 716 | |
| 717 | else if (*ar.what != 'C') /* for Lua functions, use <file:line> */ |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 718 | chunk_appendf(msg, "in function line %d", ar.linedefined); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 719 | |
| 720 | else /* nothing left... */ |
| 721 | chunk_appendf(msg, "?"); |
| 722 | |
| 723 | |
| 724 | /* Display tailed call */ |
| 725 | if (ar.istailcall) |
| 726 | chunk_appendf(msg, " ..."); |
| 727 | } |
| 728 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 729 | return msg->area; |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 730 | } |
| 731 | |
| 732 | |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 733 | /* This function check the number of arguments available in the |
| 734 | * stack. If the number of arguments available is not the same |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 735 | * then <nb> an error is thrown. |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 736 | */ |
| 737 | __LJMP static inline void check_args(lua_State *L, int nb, char *fcn) |
| 738 | { |
| 739 | if (lua_gettop(L) == nb) |
| 740 | return; |
| 741 | WILL_LJMP(luaL_error(L, "'%s' needs %d arguments", fcn, nb)); |
| 742 | } |
| 743 | |
Mark Lakes | 22154b4 | 2018-01-29 14:38:40 -0800 | [diff] [blame] | 744 | /* This function pushes an error string prefixed by the file name |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 745 | * and the line number where the error is encountered. |
| 746 | */ |
| 747 | static int hlua_pusherror(lua_State *L, const char *fmt, ...) |
| 748 | { |
| 749 | va_list argp; |
| 750 | va_start(argp, fmt); |
| 751 | luaL_where(L, 1); |
| 752 | lua_pushvfstring(L, fmt, argp); |
| 753 | va_end(argp); |
| 754 | lua_concat(L, 2); |
| 755 | return 1; |
| 756 | } |
| 757 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 758 | /* This functions is used with sample fetch and converters. It |
| 759 | * converts the HAProxy configuration argument in a lua stack |
| 760 | * values. |
| 761 | * |
| 762 | * It takes an array of "arg", and each entry of the array is |
| 763 | * converted and pushed in the LUA stack. |
| 764 | */ |
| 765 | static int hlua_arg2lua(lua_State *L, const struct arg *arg) |
| 766 | { |
| 767 | switch (arg->type) { |
| 768 | case ARGT_SINT: |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 769 | case ARGT_TIME: |
| 770 | case ARGT_SIZE: |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 771 | lua_pushinteger(L, arg->data.sint); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 772 | break; |
| 773 | |
| 774 | case ARGT_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 775 | lua_pushlstring(L, arg->data.str.area, arg->data.str.data); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 776 | break; |
| 777 | |
| 778 | case ARGT_IPV4: |
| 779 | case ARGT_IPV6: |
| 780 | case ARGT_MSK4: |
| 781 | case ARGT_MSK6: |
| 782 | case ARGT_FE: |
| 783 | case ARGT_BE: |
| 784 | case ARGT_TAB: |
| 785 | case ARGT_SRV: |
| 786 | case ARGT_USR: |
| 787 | case ARGT_MAP: |
| 788 | default: |
| 789 | lua_pushnil(L); |
| 790 | break; |
| 791 | } |
| 792 | return 1; |
| 793 | } |
| 794 | |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 795 | /* 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] | 796 | * 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] | 797 | * with sample fetch wrappers. The input arguments are given to the |
| 798 | * lua wrapper and converted as arg list by the function. |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 799 | */ |
| 800 | static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg) |
| 801 | { |
| 802 | switch (lua_type(L, ud)) { |
| 803 | |
| 804 | case LUA_TNUMBER: |
| 805 | case LUA_TBOOLEAN: |
| 806 | arg->type = ARGT_SINT; |
| 807 | arg->data.sint = lua_tointeger(L, ud); |
| 808 | break; |
| 809 | |
| 810 | case LUA_TSTRING: |
| 811 | arg->type = ARGT_STR; |
Tim Duesterhus | 2e89dec | 2019-09-29 23:03:08 +0200 | [diff] [blame] | 812 | 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] | 813 | /* 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] | 814 | 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] | 815 | arg->data.str.head = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 816 | break; |
| 817 | |
| 818 | case LUA_TUSERDATA: |
| 819 | case LUA_TNIL: |
| 820 | case LUA_TTABLE: |
| 821 | case LUA_TFUNCTION: |
| 822 | case LUA_TTHREAD: |
| 823 | case LUA_TLIGHTUSERDATA: |
| 824 | arg->type = ARGT_SINT; |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 825 | arg->data.sint = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 826 | break; |
| 827 | } |
| 828 | return 1; |
| 829 | } |
| 830 | |
| 831 | /* the following functions are used to convert a struct sample |
| 832 | * in Lua type. This useful to convert the return of the |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 833 | * fetches or converters. |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 834 | */ |
Willy Tarreau | 5eadada | 2015-03-10 17:28:54 +0100 | [diff] [blame] | 835 | static int hlua_smp2lua(lua_State *L, struct sample *smp) |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 836 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 837 | switch (smp->data.type) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 838 | case SMP_T_SINT: |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 839 | case SMP_T_BOOL: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 840 | lua_pushinteger(L, smp->data.u.sint); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 841 | break; |
| 842 | |
| 843 | case SMP_T_BIN: |
| 844 | case SMP_T_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 845 | 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] | 846 | break; |
| 847 | |
| 848 | case SMP_T_METH: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 849 | switch (smp->data.u.meth.meth) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 850 | case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break; |
| 851 | case HTTP_METH_GET: lua_pushstring(L, "GET"); break; |
| 852 | case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break; |
| 853 | case HTTP_METH_POST: lua_pushstring(L, "POST"); break; |
| 854 | case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break; |
| 855 | case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break; |
| 856 | case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break; |
| 857 | case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break; |
| 858 | case HTTP_METH_OTHER: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 859 | 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] | 860 | break; |
| 861 | default: |
| 862 | lua_pushnil(L); |
| 863 | break; |
| 864 | } |
| 865 | break; |
| 866 | |
| 867 | case SMP_T_IPV4: |
| 868 | case SMP_T_IPV6: |
| 869 | 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] | 870 | if (sample_casts[smp->data.type][SMP_T_STR] && |
| 871 | sample_casts[smp->data.type][SMP_T_STR](smp)) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 872 | 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] | 873 | else |
| 874 | lua_pushnil(L); |
| 875 | break; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 876 | default: |
| 877 | lua_pushnil(L); |
| 878 | break; |
| 879 | } |
| 880 | return 1; |
| 881 | } |
| 882 | |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 883 | /* the following functions are used to convert a struct sample |
| 884 | * in Lua strings. This is useful to convert the return of the |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 885 | * fetches or converters. |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 886 | */ |
| 887 | static int hlua_smp2lua_str(lua_State *L, struct sample *smp) |
| 888 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 889 | switch (smp->data.type) { |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 890 | |
| 891 | case SMP_T_BIN: |
| 892 | case SMP_T_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 893 | 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] | 894 | break; |
| 895 | |
| 896 | case SMP_T_METH: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 897 | switch (smp->data.u.meth.meth) { |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 898 | case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break; |
| 899 | case HTTP_METH_GET: lua_pushstring(L, "GET"); break; |
| 900 | case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break; |
| 901 | case HTTP_METH_POST: lua_pushstring(L, "POST"); break; |
| 902 | case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break; |
| 903 | case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break; |
| 904 | case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break; |
| 905 | case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break; |
| 906 | case HTTP_METH_OTHER: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 907 | 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] | 908 | break; |
| 909 | default: |
| 910 | lua_pushstring(L, ""); |
| 911 | break; |
| 912 | } |
| 913 | break; |
| 914 | |
| 915 | case SMP_T_SINT: |
| 916 | case SMP_T_BOOL: |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 917 | case SMP_T_IPV4: |
| 918 | case SMP_T_IPV6: |
| 919 | 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] | 920 | if (sample_casts[smp->data.type][SMP_T_STR] && |
| 921 | sample_casts[smp->data.type][SMP_T_STR](smp)) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 922 | 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] | 923 | else |
| 924 | lua_pushstring(L, ""); |
| 925 | break; |
| 926 | default: |
| 927 | lua_pushstring(L, ""); |
| 928 | break; |
| 929 | } |
| 930 | return 1; |
| 931 | } |
| 932 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 933 | /* the following functions are used to convert an Lua type in a |
| 934 | * struct sample. This is useful to provide data from a converter |
| 935 | * to the LUA code. |
| 936 | */ |
| 937 | static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp) |
| 938 | { |
| 939 | switch (lua_type(L, ud)) { |
| 940 | |
| 941 | case LUA_TNUMBER: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 942 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 943 | smp->data.u.sint = lua_tointeger(L, ud); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 944 | break; |
| 945 | |
| 946 | |
| 947 | case LUA_TBOOLEAN: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 948 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 949 | smp->data.u.sint = lua_toboolean(L, ud); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 950 | break; |
| 951 | |
| 952 | case LUA_TSTRING: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 953 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 954 | smp->flags |= SMP_F_CONST; |
Tim Duesterhus | 2e89dec | 2019-09-29 23:03:08 +0200 | [diff] [blame] | 955 | 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] | 956 | /* 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] | 957 | 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] | 958 | smp->data.u.str.head = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 959 | break; |
| 960 | |
| 961 | case LUA_TUSERDATA: |
| 962 | case LUA_TNIL: |
| 963 | case LUA_TTABLE: |
| 964 | case LUA_TFUNCTION: |
| 965 | case LUA_TTHREAD: |
| 966 | case LUA_TLIGHTUSERDATA: |
Thierry FOURNIER | 93405e1 | 2015-08-26 14:19:03 +0200 | [diff] [blame] | 967 | case LUA_TNONE: |
| 968 | default: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 969 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 970 | smp->data.u.sint = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 971 | break; |
| 972 | } |
| 973 | return 1; |
| 974 | } |
| 975 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 976 | /* This function check the "argp" built by another conversion function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 977 | * 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] | 978 | * 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] | 979 | * |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 980 | * This function assumes that the argp argument contains ARGM_NBARGS + 1 |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 981 | * entries and that there is at least one stop at the last position. |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 982 | */ |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 983 | __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] | 984 | uint64_t mask, struct proxy *p) |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 985 | { |
| 986 | int min_arg; |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 987 | int idx; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 988 | struct proxy *px; |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 989 | struct userlist *ul; |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 990 | struct my_regex *reg; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 991 | const char *msg = NULL; |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 992 | char *sname, *pname, *err = NULL; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 993 | |
| 994 | idx = 0; |
| 995 | min_arg = ARGM(mask); |
| 996 | mask >>= ARGM_BITS; |
| 997 | |
| 998 | while (1) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 999 | struct buffer tmp = BUF_NULL; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 1000 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 1001 | /* Check for mandatory arguments. */ |
| 1002 | if (argp[idx].type == ARGT_STOP) { |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 1003 | if (idx < min_arg) { |
| 1004 | |
| 1005 | /* If miss other argument than the first one, we return an error. */ |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1006 | if (idx > 0) { |
| 1007 | msg = "Mandatory argument expected"; |
| 1008 | goto error; |
| 1009 | } |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 1010 | |
| 1011 | /* If first argument have a certain type, some default values |
| 1012 | * may be used. See the function smp_resolve_args(). |
| 1013 | */ |
| 1014 | switch (mask & ARGT_MASK) { |
| 1015 | |
| 1016 | case ARGT_FE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1017 | if (!(p->cap & PR_CAP_FE)) { |
| 1018 | msg = "Mandatory argument expected"; |
| 1019 | goto error; |
| 1020 | } |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 1021 | argp[idx].data.prx = p; |
| 1022 | argp[idx].type = ARGT_FE; |
| 1023 | argp[idx+1].type = ARGT_STOP; |
| 1024 | break; |
| 1025 | |
| 1026 | case ARGT_BE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1027 | if (!(p->cap & PR_CAP_BE)) { |
| 1028 | msg = "Mandatory argument expected"; |
| 1029 | goto error; |
| 1030 | } |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 1031 | argp[idx].data.prx = p; |
| 1032 | argp[idx].type = ARGT_BE; |
| 1033 | argp[idx+1].type = ARGT_STOP; |
| 1034 | break; |
| 1035 | |
| 1036 | case ARGT_TAB: |
Olivier Houchard | 14f6268 | 2022-09-13 00:35:53 +0200 | [diff] [blame] | 1037 | if (!p->table) { |
| 1038 | msg = "Mandatory argument expected"; |
| 1039 | goto error; |
| 1040 | } |
| 1041 | argp[idx].data.t = p->table; |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 1042 | argp[idx].type = ARGT_TAB; |
| 1043 | argp[idx+1].type = ARGT_STOP; |
| 1044 | break; |
| 1045 | |
| 1046 | default: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1047 | msg = "Mandatory argument expected"; |
| 1048 | goto error; |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 1049 | break; |
| 1050 | } |
| 1051 | } |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1052 | break; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 1053 | } |
| 1054 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1055 | /* Check for exceed the number of required argument. */ |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 1056 | if ((mask & ARGT_MASK) == ARGT_STOP && |
| 1057 | argp[idx].type != ARGT_STOP) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1058 | msg = "Last argument expected"; |
| 1059 | goto error; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 1060 | } |
| 1061 | |
| 1062 | if ((mask & ARGT_MASK) == ARGT_STOP && |
| 1063 | argp[idx].type == ARGT_STOP) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1064 | break; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 1065 | } |
| 1066 | |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1067 | /* Convert some argument types. All string in argp[] are for not |
| 1068 | * duplicated yet. |
| 1069 | */ |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 1070 | switch (mask & ARGT_MASK) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 1071 | case ARGT_SINT: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1072 | if (argp[idx].type != ARGT_SINT) { |
| 1073 | msg = "integer expected"; |
| 1074 | goto error; |
| 1075 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 1076 | argp[idx].type = ARGT_SINT; |
| 1077 | break; |
| 1078 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 1079 | case ARGT_TIME: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1080 | if (argp[idx].type != ARGT_SINT) { |
| 1081 | msg = "integer expected"; |
| 1082 | goto error; |
| 1083 | } |
Thierry FOURNIER | 29176f3 | 2015-07-07 00:41:29 +0200 | [diff] [blame] | 1084 | argp[idx].type = ARGT_TIME; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 1085 | break; |
| 1086 | |
| 1087 | case ARGT_SIZE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1088 | if (argp[idx].type != ARGT_SINT) { |
| 1089 | msg = "integer expected"; |
| 1090 | goto error; |
| 1091 | } |
Thierry FOURNIER | 29176f3 | 2015-07-07 00:41:29 +0200 | [diff] [blame] | 1092 | argp[idx].type = ARGT_SIZE; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 1093 | break; |
| 1094 | |
| 1095 | case ARGT_FE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1096 | if (argp[idx].type != ARGT_STR) { |
| 1097 | msg = "string expected"; |
| 1098 | goto error; |
| 1099 | } |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 1100 | 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] | 1101 | if (!argp[idx].data.prx) { |
| 1102 | msg = "frontend doesn't exist"; |
| 1103 | goto error; |
| 1104 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 1105 | argp[idx].type = ARGT_FE; |
| 1106 | break; |
| 1107 | |
| 1108 | case ARGT_BE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1109 | if (argp[idx].type != ARGT_STR) { |
| 1110 | msg = "string expected"; |
| 1111 | goto error; |
| 1112 | } |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 1113 | 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] | 1114 | if (!argp[idx].data.prx) { |
| 1115 | msg = "backend doesn't exist"; |
| 1116 | goto error; |
| 1117 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 1118 | argp[idx].type = ARGT_BE; |
| 1119 | break; |
| 1120 | |
| 1121 | case ARGT_TAB: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1122 | if (argp[idx].type != ARGT_STR) { |
| 1123 | msg = "string expected"; |
| 1124 | goto error; |
| 1125 | } |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 1126 | 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] | 1127 | if (!argp[idx].data.t) { |
| 1128 | msg = "table doesn't exist"; |
| 1129 | goto error; |
| 1130 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 1131 | argp[idx].type = ARGT_TAB; |
| 1132 | break; |
| 1133 | |
| 1134 | case ARGT_SRV: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1135 | if (argp[idx].type != ARGT_STR) { |
| 1136 | msg = "string expected"; |
| 1137 | goto error; |
| 1138 | } |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 1139 | sname = strrchr(argp[idx].data.str.area, '/'); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 1140 | if (sname) { |
| 1141 | *sname++ = '\0'; |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 1142 | pname = argp[idx].data.str.area; |
Willy Tarreau | 9e0bb10 | 2015-05-26 11:24:42 +0200 | [diff] [blame] | 1143 | px = proxy_be_by_name(pname); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1144 | if (!px) { |
| 1145 | msg = "backend doesn't exist"; |
| 1146 | goto error; |
| 1147 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 1148 | } |
| 1149 | else { |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 1150 | sname = argp[idx].data.str.area; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 1151 | px = p; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 1152 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 1153 | argp[idx].data.srv = findserver(px, sname); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1154 | if (!argp[idx].data.srv) { |
| 1155 | msg = "server doesn't exist"; |
| 1156 | goto error; |
| 1157 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 1158 | argp[idx].type = ARGT_SRV; |
| 1159 | break; |
| 1160 | |
| 1161 | case ARGT_IPV4: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1162 | if (argp[idx].type != ARGT_STR) { |
| 1163 | msg = "string expected"; |
| 1164 | goto error; |
| 1165 | } |
| 1166 | if (inet_pton(AF_INET, argp[idx].data.str.area, &argp[idx].data.ipv4)) { |
| 1167 | msg = "invalid IPv4 address"; |
| 1168 | goto error; |
| 1169 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 1170 | argp[idx].type = ARGT_IPV4; |
| 1171 | break; |
| 1172 | |
| 1173 | case ARGT_MSK4: |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 1174 | if (argp[idx].type == ARGT_SINT) |
| 1175 | len2mask4(argp[idx].data.sint, &argp[idx].data.ipv4); |
| 1176 | else if (argp[idx].type == ARGT_STR) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1177 | if (!str2mask(argp[idx].data.str.area, &argp[idx].data.ipv4)) { |
| 1178 | msg = "invalid IPv4 mask"; |
| 1179 | goto error; |
| 1180 | } |
| 1181 | } |
| 1182 | else { |
| 1183 | msg = "integer or string expected"; |
| 1184 | goto error; |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 1185 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 1186 | argp[idx].type = ARGT_MSK4; |
| 1187 | break; |
| 1188 | |
| 1189 | case ARGT_IPV6: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1190 | if (argp[idx].type != ARGT_STR) { |
| 1191 | msg = "string expected"; |
| 1192 | goto error; |
| 1193 | } |
| 1194 | if (inet_pton(AF_INET6, argp[idx].data.str.area, &argp[idx].data.ipv6)) { |
| 1195 | msg = "invalid IPv6 address"; |
| 1196 | goto error; |
| 1197 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 1198 | argp[idx].type = ARGT_IPV6; |
| 1199 | break; |
| 1200 | |
| 1201 | case ARGT_MSK6: |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 1202 | if (argp[idx].type == ARGT_SINT) |
| 1203 | len2mask6(argp[idx].data.sint, &argp[idx].data.ipv6); |
| 1204 | else if (argp[idx].type == ARGT_STR) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1205 | if (!str2mask6(argp[idx].data.str.area, &argp[idx].data.ipv6)) { |
| 1206 | msg = "invalid IPv6 mask"; |
| 1207 | goto error; |
| 1208 | } |
| 1209 | } |
| 1210 | else { |
| 1211 | msg = "integer or string expected"; |
| 1212 | goto error; |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 1213 | } |
Tim Duesterhus | b814da6 | 2018-01-25 16:24:50 +0100 | [diff] [blame] | 1214 | argp[idx].type = ARGT_MSK6; |
| 1215 | break; |
| 1216 | |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 1217 | case ARGT_REG: |
| 1218 | if (argp[idx].type != ARGT_STR) { |
| 1219 | msg = "string expected"; |
| 1220 | goto error; |
| 1221 | } |
| 1222 | reg = regex_comp(argp[idx].data.str.area, !(argp[idx].type_flags & ARGF_REG_ICASE), 1, &err); |
| 1223 | if (!reg) { |
| 1224 | msg = lua_pushfstring(L, "error compiling regex '%s' : '%s'", |
| 1225 | argp[idx].data.str.area, err); |
| 1226 | free(err); |
| 1227 | goto error; |
| 1228 | } |
| 1229 | argp[idx].type = ARGT_REG; |
| 1230 | argp[idx].data.reg = reg; |
| 1231 | break; |
| 1232 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 1233 | case ARGT_USR: |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 1234 | if (argp[idx].type != ARGT_STR) { |
| 1235 | msg = "string expected"; |
| 1236 | goto error; |
| 1237 | } |
| 1238 | if (p->uri_auth && p->uri_auth->userlist && |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 1239 | strcmp(p->uri_auth->userlist->name, argp[idx].data.str.area) == 0) |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 1240 | ul = p->uri_auth->userlist; |
| 1241 | else |
| 1242 | ul = auth_find_userlist(argp[idx].data.str.area); |
| 1243 | |
| 1244 | if (!ul) { |
| 1245 | msg = lua_pushfstring(L, "unable to find userlist '%s'", argp[idx].data.str.area); |
| 1246 | goto error; |
| 1247 | } |
| 1248 | argp[idx].type = ARGT_USR; |
| 1249 | argp[idx].data.usr = ul; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1250 | break; |
| 1251 | |
| 1252 | case ARGT_STR: |
| 1253 | if (!chunk_dup(&tmp, &argp[idx].data.str)) { |
| 1254 | msg = "unable to duplicate string arg"; |
| 1255 | goto error; |
| 1256 | } |
| 1257 | argp[idx].data.str = tmp; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 1258 | break; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1259 | |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 1260 | case ARGT_MAP: |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 1261 | msg = "type not yet supported"; |
| 1262 | goto error; |
| 1263 | break; |
| 1264 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 1265 | } |
| 1266 | |
| 1267 | /* Check for type of argument. */ |
| 1268 | if ((mask & ARGT_MASK) != argp[idx].type) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1269 | msg = lua_pushfstring(L, "'%s' expected, got '%s'", |
| 1270 | arg_type_names[(mask & ARGT_MASK)], |
| 1271 | arg_type_names[argp[idx].type & ARGT_MASK]); |
| 1272 | goto error; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 1273 | } |
| 1274 | |
| 1275 | /* Next argument. */ |
| 1276 | mask >>= ARGT_BITS; |
| 1277 | idx++; |
| 1278 | } |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1279 | return 0; |
| 1280 | |
| 1281 | error: |
Olivier Houchard | ca43161 | 2022-09-13 00:31:17 +0200 | [diff] [blame] | 1282 | argp[idx].type = ARGT_STOP; |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 1283 | free_args(argp); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1284 | WILL_LJMP(luaL_argerror(L, first + idx, msg)); |
| 1285 | return 0; /* Never reached */ |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 1286 | } |
| 1287 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1288 | /* |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 1289 | * The following functions are used to make correspondence between the the |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1290 | * executed lua pointer and the "struct hlua *" that contain the context. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1291 | * |
| 1292 | * - hlua_gethlua : return the hlua context associated with an lua_State. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1293 | * - hlua_sethlua : create the association between hlua context and lua_state. |
| 1294 | */ |
| 1295 | static inline struct hlua *hlua_gethlua(lua_State *L) |
| 1296 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 1297 | struct hlua **hlua = lua_getextraspace(L); |
| 1298 | return *hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1299 | } |
| 1300 | static inline void hlua_sethlua(struct hlua *hlua) |
| 1301 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 1302 | struct hlua **hlua_store = lua_getextraspace(hlua->T); |
| 1303 | *hlua_store = hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1304 | } |
| 1305 | |
Willy Tarreau | 0b7b639 | 2022-06-19 17:39:33 +0200 | [diff] [blame] | 1306 | /* Will return a non-NULL string indicating the Lua call trace if the caller |
| 1307 | * currently is executing from within a Lua function. One line per entry will |
| 1308 | * be emitted, and each extra line will be prefixed with <pfx>. If a current |
| 1309 | * Lua function is not detected, NULL is returned. |
| 1310 | */ |
| 1311 | const char *hlua_show_current_location(const char *pfx) |
| 1312 | { |
| 1313 | lua_State *L; |
| 1314 | lua_Debug ar; |
| 1315 | |
| 1316 | /* global or per-thread stack initializing ? */ |
| 1317 | if (hlua_state_id != -1 && (L = hlua_states[hlua_state_id]) && lua_getstack(L, 0, &ar)) |
| 1318 | return hlua_traceback(L, pfx); |
| 1319 | |
| 1320 | /* per-thread stack running ? */ |
| 1321 | if (hlua_states[tid + 1] && (L = hlua_states[tid + 1]) && lua_getstack(L, 0, &ar)) |
| 1322 | return hlua_traceback(L, pfx); |
| 1323 | |
| 1324 | /* global stack running ? */ |
| 1325 | if (hlua_states[0] && (L = hlua_states[0]) && lua_getstack(L, 0, &ar)) |
| 1326 | return hlua_traceback(L, pfx); |
| 1327 | |
| 1328 | return NULL; |
| 1329 | } |
| 1330 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1331 | /* This function is used to send logs. It try to send on screen (stderr) |
| 1332 | * and on the default syslog server. |
| 1333 | */ |
| 1334 | static inline void hlua_sendlog(struct proxy *px, int level, const char *msg) |
| 1335 | { |
| 1336 | struct tm tm; |
| 1337 | char *p; |
| 1338 | |
| 1339 | /* Cleanup the log message. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1340 | p = trash.area; |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1341 | for (; *msg != '\0'; msg++, p++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1342 | if (p >= trash.area + trash.size - 1) { |
Thierry FOURNIER | ccf0063 | 2015-09-16 12:47:03 +0200 | [diff] [blame] | 1343 | /* Break the message if exceed the buffer size. */ |
| 1344 | *(p-4) = ' '; |
| 1345 | *(p-3) = '.'; |
| 1346 | *(p-2) = '.'; |
| 1347 | *(p-1) = '.'; |
| 1348 | break; |
| 1349 | } |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 1350 | if (isprint((unsigned char)*msg)) |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1351 | *p = *msg; |
| 1352 | else |
| 1353 | *p = '.'; |
| 1354 | } |
| 1355 | *p = '\0'; |
| 1356 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1357 | send_log(px, level, "%s\n", trash.area); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1358 | if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) { |
Christopher Faulet | f98d821 | 2020-10-02 18:13:52 +0200 | [diff] [blame] | 1359 | if (level == LOG_DEBUG && !(global.mode & MODE_DEBUG)) |
| 1360 | return; |
| 1361 | |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 1362 | get_localtime(date.tv_sec, &tm); |
| 1363 | fprintf(stderr, "[%s] %03d/%02d%02d%02d (%d) : %s\n", |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1364 | 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] | 1365 | (int)getpid(), trash.area); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1366 | fflush(stderr); |
| 1367 | } |
| 1368 | } |
| 1369 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1370 | /* This function just ensure that the yield will be always |
| 1371 | * returned with a timeout and permit to set some flags |
Aurelien DARRAGON | 2a9764b | 2023-04-04 18:41:04 +0200 | [diff] [blame] | 1372 | * <timeout> is a tick value |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1373 | */ |
| 1374 | __LJMP void hlua_yieldk(lua_State *L, int nresults, int ctx, |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1375 | lua_KFunction k, int timeout, unsigned int flags) |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1376 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 1377 | struct hlua *hlua; |
| 1378 | |
| 1379 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 1380 | hlua = hlua_gethlua(L); |
| 1381 | if (!hlua) { |
| 1382 | return; |
| 1383 | } |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1384 | |
| 1385 | /* Set the wake timeout. If timeout is required, we set |
| 1386 | * the expiration time. |
| 1387 | */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1388 | hlua->wake_time = timeout; |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1389 | |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 1390 | hlua->flags |= flags; |
| 1391 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1392 | /* Process the yield. */ |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 1393 | MAY_LJMP(lua_yieldk(L, nresults, ctx, k)); |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1394 | } |
| 1395 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1396 | /* This function initialises the Lua environment stored in the stream. |
| 1397 | * 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] | 1398 | * 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] | 1399 | * |
| 1400 | * This function is particular. it initialises a new Lua thread. If the |
| 1401 | * initialisation fails (example: out of memory error), the lua function |
| 1402 | * throws an error (longjmp). |
| 1403 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1404 | * This function manipulates two Lua stacks: the main and the thread. Only |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 1405 | * the main stack can fail. The thread is not manipulated. This function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1406 | * MUST NOT manipulate the created thread stack state, because it is not |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1407 | * protected against errors thrown by the thread stack. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1408 | */ |
Aurelien DARRAGON | 6b0b9bd | 2023-03-21 14:02:16 +0100 | [diff] [blame] | 1409 | 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] | 1410 | { |
| 1411 | lua->Mref = LUA_REFNIL; |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1412 | lua->flags = 0; |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 1413 | lua->gc_count = 0; |
Christopher Faulet | bc275a9 | 2020-02-26 14:55:16 +0100 | [diff] [blame] | 1414 | lua->wake_time = TICK_ETERNITY; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1415 | lua->state_id = state_id; |
Aurelien DARRAGON | da9503c | 2022-11-25 09:10:07 +0100 | [diff] [blame] | 1416 | hlua_timer_init(&lua->timer, 0); /* default value, no timeout */ |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 1417 | LIST_INIT(&lua->com); |
Aurelien DARRAGON | 3ffbf38 | 2023-02-09 17:02:57 +0100 | [diff] [blame] | 1418 | MT_LIST_INIT(&lua->hc_list); |
Aurelien DARRAGON | 6b0b9bd | 2023-03-21 14:02:16 +0100 | [diff] [blame] | 1419 | if (!SET_SAFE_LJMP_PARENT(lua)) { |
| 1420 | lua->Tref = LUA_REFNIL; |
| 1421 | return 0; |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1422 | } |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1423 | lua->T = lua_newthread(hlua_states[state_id]); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1424 | if (!lua->T) { |
| 1425 | lua->Tref = LUA_REFNIL; |
Aurelien DARRAGON | 6b0b9bd | 2023-03-21 14:02:16 +0100 | [diff] [blame] | 1426 | RESET_SAFE_LJMP_PARENT(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1427 | return 0; |
| 1428 | } |
| 1429 | hlua_sethlua(lua); |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1430 | lua->Tref = luaL_ref(hlua_states[state_id], LUA_REGISTRYINDEX); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1431 | lua->task = task; |
Aurelien DARRAGON | 6b0b9bd | 2023-03-21 14:02:16 +0100 | [diff] [blame] | 1432 | RESET_SAFE_LJMP_PARENT(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1433 | return 1; |
| 1434 | } |
| 1435 | |
Aurelien DARRAGON | 3ffbf38 | 2023-02-09 17:02:57 +0100 | [diff] [blame] | 1436 | /* kill all associated httpclient to this hlua task |
| 1437 | * We must take extra precautions as we're manipulating lua-exposed |
| 1438 | * objects without the main lua lock. |
| 1439 | */ |
William Lallemand | bb58142 | 2022-10-20 10:57:28 +0200 | [diff] [blame] | 1440 | static void hlua_httpclient_destroy_all(struct hlua *hlua) |
| 1441 | { |
Aurelien DARRAGON | 3ffbf38 | 2023-02-09 17:02:57 +0100 | [diff] [blame] | 1442 | struct hlua_httpclient *hlua_hc; |
William Lallemand | bb58142 | 2022-10-20 10:57:28 +0200 | [diff] [blame] | 1443 | |
Aurelien DARRAGON | 3ffbf38 | 2023-02-09 17:02:57 +0100 | [diff] [blame] | 1444 | /* use thread-safe accessors for hc_list since GC cycle initiated by |
| 1445 | * another thread sharing the same main lua stack (lua coroutine) |
| 1446 | * could execute hlua_httpclient_gc() on the hlua->hc_list items |
| 1447 | * in parallel: Lua GC applies on the main stack, it is not limited to |
| 1448 | * a single coroutine stack, see Github issue #2037 for reference. |
| 1449 | * Remember, coroutines created using lua_newthread() are not meant to |
| 1450 | * be thread safe in Lua. (From lua co-author: |
| 1451 | * http://lua-users.org/lists/lua-l/2011-07/msg00072.html) |
| 1452 | * |
| 1453 | * This security measure is superfluous when 'lua-load-per-thread' is used |
| 1454 | * since in this case coroutines exclusively run on the same thread |
| 1455 | * (main stack is not shared between OS threads). |
| 1456 | */ |
| 1457 | while ((hlua_hc = MT_LIST_POP(&hlua->hc_list, typeof(hlua_hc), by_hlua))) { |
| 1458 | httpclient_stop_and_destroy(hlua_hc->hc); |
William Lallemand | bb58142 | 2022-10-20 10:57:28 +0200 | [diff] [blame] | 1459 | hlua_hc->hc = NULL; |
William Lallemand | bb58142 | 2022-10-20 10:57:28 +0200 | [diff] [blame] | 1460 | } |
| 1461 | } |
| 1462 | |
| 1463 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1464 | /* Used to destroy the Lua coroutine when the attached stream or task |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1465 | * is destroyed. The destroy also the memory context. The struct "lua" |
Aurelien DARRAGON | 60ab0f7 | 2023-03-01 16:45:50 +0100 | [diff] [blame] | 1466 | * will be freed. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1467 | */ |
| 1468 | void hlua_ctx_destroy(struct hlua *lua) |
| 1469 | { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 1470 | if (!lua) |
Thierry FOURNIER | a718b29 | 2015-03-04 16:48:34 +0100 | [diff] [blame] | 1471 | return; |
| 1472 | |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 1473 | if (!lua->T) |
| 1474 | goto end; |
| 1475 | |
William Lallemand | bb58142 | 2022-10-20 10:57:28 +0200 | [diff] [blame] | 1476 | /* clean all running httpclient */ |
| 1477 | hlua_httpclient_destroy_all(lua); |
| 1478 | |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 1479 | /* Purge all the pending signals. */ |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1480 | notification_purge(&lua->com); |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 1481 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1482 | if (!SET_SAFE_LJMP(lua)) |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 1483 | return; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1484 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1485 | RESET_SAFE_LJMP(lua); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 1486 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1487 | if (!SET_SAFE_LJMP_PARENT(lua)) |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 1488 | return; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1489 | luaL_unref(hlua_states[lua->state_id], LUA_REGISTRYINDEX, lua->Tref); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1490 | RESET_SAFE_LJMP_PARENT(lua); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 1491 | /* Forces a garbage collecting process. If the Lua program is finished |
| 1492 | * without error, we run the GC on the thread pointer. Its freed all |
| 1493 | * the unused memory. |
| 1494 | * If the thread is finnish with an error or is currently yielded, |
| 1495 | * it seems that the GC applied on the thread doesn't clean anything, |
| 1496 | * so e run the GC on the main thread. |
| 1497 | * NOTE: maybe this action locks all the Lua threads untiml the en of |
| 1498 | * the garbage collection. |
| 1499 | */ |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 1500 | if (lua->gc_count) { |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1501 | if (!SET_SAFE_LJMP_PARENT(lua)) |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 1502 | return; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1503 | lua_gc(hlua_states[lua->state_id], LUA_GCCOLLECT, 0); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1504 | RESET_SAFE_LJMP_PARENT(lua); |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 1505 | } |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 1506 | |
Thierry FOURNIER | a7b536b | 2015-09-21 22:50:24 +0200 | [diff] [blame] | 1507 | lua->T = NULL; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 1508 | |
| 1509 | end: |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 1510 | pool_free(pool_head_hlua, lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1511 | } |
| 1512 | |
| 1513 | /* This function is used to restore the Lua context when a coroutine |
| 1514 | * fails. This function copy the common memory between old coroutine |
| 1515 | * and the new coroutine. The old coroutine is destroyed, and its |
| 1516 | * replaced by the new coroutine. |
| 1517 | * If the flag "keep_msg" is set, the last entry of the old is assumed |
| 1518 | * as string error message and it is copied in the new stack. |
| 1519 | */ |
| 1520 | static int hlua_ctx_renew(struct hlua *lua, int keep_msg) |
| 1521 | { |
| 1522 | lua_State *T; |
| 1523 | int new_ref; |
| 1524 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1525 | /* New Lua coroutine. */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1526 | T = lua_newthread(hlua_states[lua->state_id]); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1527 | if (!T) |
| 1528 | return 0; |
| 1529 | |
| 1530 | /* Copy last error message. */ |
| 1531 | if (keep_msg) |
| 1532 | lua_xmove(lua->T, T, 1); |
| 1533 | |
| 1534 | /* Copy data between the coroutines. */ |
| 1535 | lua_rawgeti(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 1536 | lua_xmove(lua->T, T, 1); |
Ilya Shipitsin | 46a030c | 2020-07-05 16:36:08 +0500 | [diff] [blame] | 1537 | new_ref = luaL_ref(T, LUA_REGISTRYINDEX); /* Value popped. */ |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1538 | |
| 1539 | /* Destroy old data. */ |
| 1540 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 1541 | |
| 1542 | /* The thread is garbage collected by Lua. */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1543 | luaL_unref(hlua_states[lua->state_id], LUA_REGISTRYINDEX, lua->Tref); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1544 | |
| 1545 | /* Fill the struct with the new coroutine values. */ |
| 1546 | lua->Mref = new_ref; |
| 1547 | lua->T = T; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1548 | lua->Tref = luaL_ref(hlua_states[lua->state_id], LUA_REGISTRYINDEX); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1549 | |
| 1550 | /* Set context. */ |
| 1551 | hlua_sethlua(lua); |
| 1552 | |
| 1553 | return 1; |
| 1554 | } |
| 1555 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1556 | void hlua_hook(lua_State *L, lua_Debug *ar) |
| 1557 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 1558 | struct hlua *hlua; |
| 1559 | |
| 1560 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 1561 | hlua = hlua_gethlua(L); |
| 1562 | if (!hlua) |
| 1563 | return; |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1564 | |
Aurelien DARRAGON | cf0f792 | 2023-04-07 16:34:20 +0200 | [diff] [blame] | 1565 | if (hlua->T != L) { |
| 1566 | /* We don't want to enforce a yield on a sub coroutine, since |
| 1567 | * we have no guarantees that the yield will be handled properly. |
| 1568 | * Indeed, only the hlua->T coroutine is being handled through |
| 1569 | * hlua_ctx_resume() function. |
| 1570 | * |
| 1571 | * Instead, we simply check for timeouts and wait for the sub |
| 1572 | * coroutine to finish.. |
| 1573 | */ |
| 1574 | goto check_timeout; |
| 1575 | } |
| 1576 | |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1577 | /* Lua cannot yield when its returning from a function, |
| 1578 | * so, we can fix the interrupt hook to 1 instruction, |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1579 | * expecting that the function is finished. |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1580 | */ |
| 1581 | if (lua_gethookmask(L) & LUA_MASKRET) { |
| 1582 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, 1); |
| 1583 | return; |
| 1584 | } |
| 1585 | |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1586 | /* If we interrupt the Lua processing in yieldable state, we yield. |
| 1587 | * If the state is not yieldable, trying yield causes an error. |
| 1588 | */ |
Aurelien DARRAGON | 0ebd41f | 2022-11-24 09:51:40 +0100 | [diff] [blame] | 1589 | if (lua_isyieldable(L)) { |
| 1590 | /* note: for converters/fetches.. where yielding is not allowed |
| 1591 | * hlua_ctx_resume() will simply perform a goto resume_execution |
| 1592 | * instead of rescheduling hlua->task. |
| 1593 | * also: hlua_ctx_resume() will take care of checking execution |
| 1594 | * timeout and re-applying the hook as needed. |
| 1595 | */ |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 1596 | MAY_LJMP(hlua_yieldk(L, 0, 0, NULL, TICK_ETERNITY, HLUA_CTRLYIELD)); |
Aurelien DARRAGON | 0ebd41f | 2022-11-24 09:51:40 +0100 | [diff] [blame] | 1597 | /* lua docs says that the hook should return immediately after lua_yieldk |
| 1598 | * |
| 1599 | * From: https://www.lua.org/manual/5.3/manual.html#lua_yieldk |
| 1600 | * |
| 1601 | * Moreover, it seems that we don't want to continue after the yield |
| 1602 | * because the end of the function is about handling unyieldable function, |
| 1603 | * which is not the case here. |
| 1604 | * |
| 1605 | * ->if we don't return lua_sethook gets incorrectly set with MASKRET later |
| 1606 | * in the function. |
| 1607 | */ |
| 1608 | return; |
| 1609 | } |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1610 | |
Aurelien DARRAGON | cf0f792 | 2023-04-07 16:34:20 +0200 | [diff] [blame] | 1611 | check_timeout: |
Aurelien DARRAGON | da9503c | 2022-11-25 09:10:07 +0100 | [diff] [blame] | 1612 | /* If we cannot yield, check the timeout. */ |
| 1613 | if (!hlua_timer_check(&hlua->timer)) { |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1614 | lua_pushfstring(L, "execution timeout"); |
| 1615 | WILL_LJMP(lua_error(L)); |
| 1616 | } |
| 1617 | |
| 1618 | /* Try to interrupt the process at the end of the current |
| 1619 | * unyieldable function. |
| 1620 | */ |
| 1621 | 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] | 1622 | } |
| 1623 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1624 | /* This function start or resumes the Lua stack execution. If the flag |
| 1625 | * "yield_allowed" if no set and the LUA stack execution returns a yield |
| 1626 | * The function return an error. |
| 1627 | * |
| 1628 | * The function can returns 4 values: |
| 1629 | * - HLUA_E_OK : The execution is terminated without any errors. |
| 1630 | * - HLUA_E_AGAIN : The execution must continue at the next associated |
| 1631 | * task wakeup. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1632 | * - 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] | 1633 | * the top of the stack. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1634 | * - HLUA_E_ERR : An error has occurred without error message. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1635 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1636 | * 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] | 1637 | * LUA code. |
| 1638 | */ |
| 1639 | static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed) |
| 1640 | { |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1641 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
| 1642 | int nres; |
| 1643 | #endif |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1644 | int ret; |
| 1645 | const char *msg; |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1646 | const char *trace; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1647 | |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1648 | /* Lock the whole Lua execution. This lock must be before the |
| 1649 | * label "resume_execution". |
| 1650 | */ |
Aurelien DARRAGON | e36f803 | 2023-03-21 13:22:33 +0100 | [diff] [blame] | 1651 | hlua_lock(lua); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1652 | |
Aurelien DARRAGON | da9503c | 2022-11-25 09:10:07 +0100 | [diff] [blame] | 1653 | /* reset the timer as we might be re-entering the function to |
| 1654 | * resume the coroutine after a successful yield |
| 1655 | * (cumulative time will be updated) |
| 1656 | */ |
| 1657 | hlua_timer_reset(&lua->timer); |
| 1658 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1659 | resume_execution: |
| 1660 | |
| 1661 | /* This hook interrupts the Lua processing each 'hlua_nb_instruction' |
| 1662 | * instructions. it is used for preventing infinite loops. |
| 1663 | */ |
| 1664 | lua_sethook(lua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 1665 | |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1666 | /* Remove all flags except the running flags. */ |
Thierry FOURNIER | 2f3867f | 2015-09-28 01:02:01 +0200 | [diff] [blame] | 1667 | HLUA_SET_RUN(lua); |
| 1668 | HLUA_CLR_CTRLYIELD(lua); |
| 1669 | HLUA_CLR_WAKERESWR(lua); |
| 1670 | HLUA_CLR_WAKEREQWR(lua); |
Christopher Faulet | 1f43a34 | 2021-08-04 17:58:21 +0200 | [diff] [blame] | 1671 | HLUA_CLR_NOYIELD(lua); |
| 1672 | if (!yield_allowed) |
| 1673 | HLUA_SET_NOYIELD(lua); |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1674 | |
Aurelien DARRAGON | da9503c | 2022-11-25 09:10:07 +0100 | [diff] [blame] | 1675 | /* reset wake_time. */ |
Christopher Faulet | bc275a9 | 2020-02-26 14:55:16 +0100 | [diff] [blame] | 1676 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1677 | |
Aurelien DARRAGON | da9503c | 2022-11-25 09:10:07 +0100 | [diff] [blame] | 1678 | /* start the timer as we're about to start lua processing */ |
| 1679 | hlua_timer_start(&lua->timer); |
| 1680 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1681 | /* Call the function. */ |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1682 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1683 | 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] | 1684 | #else |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1685 | ret = lua_resume(lua->T, hlua_states[lua->state_id], lua->nargs); |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1686 | #endif |
Aurelien DARRAGON | da9503c | 2022-11-25 09:10:07 +0100 | [diff] [blame] | 1687 | |
| 1688 | /* out of lua processing, stop the timer */ |
| 1689 | hlua_timer_stop(&lua->timer); |
| 1690 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1691 | switch (ret) { |
| 1692 | |
| 1693 | case LUA_OK: |
| 1694 | ret = HLUA_E_OK; |
| 1695 | break; |
| 1696 | |
| 1697 | case LUA_YIELD: |
Aurelien DARRAGON | da9503c | 2022-11-25 09:10:07 +0100 | [diff] [blame] | 1698 | /* Check if the execution timeout is expired. If it is the case, we |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1699 | * break the Lua execution. |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1700 | */ |
Aurelien DARRAGON | da9503c | 2022-11-25 09:10:07 +0100 | [diff] [blame] | 1701 | if (!hlua_timer_check(&lua->timer)) { |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1702 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1703 | ret = HLUA_E_ETMOUT; |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1704 | break; |
| 1705 | } |
| 1706 | /* Process the forced yield. if the general yield is not allowed or |
| 1707 | * if no task were associated this the current Lua execution |
| 1708 | * coroutine, we resume the execution. Else we want to return in the |
| 1709 | * scheduler and we want to be waked up again, to continue the |
| 1710 | * current Lua execution. So we schedule our own task. |
| 1711 | */ |
| 1712 | if (HLUA_IS_CTRLYIELDING(lua)) { |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1713 | if (!yield_allowed || !lua->task) |
| 1714 | goto resume_execution; |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1715 | task_wakeup(lua->task, TASK_WOKEN_MSG); |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1716 | } |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1717 | if (!yield_allowed) { |
| 1718 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1719 | ret = HLUA_E_YIELD; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1720 | break; |
| 1721 | } |
| 1722 | ret = HLUA_E_AGAIN; |
| 1723 | break; |
| 1724 | |
| 1725 | case LUA_ERRRUN: |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1726 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1727 | /* Special exit case. The traditional exit is returned as an error |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1728 | * because the errors ares the only one mean to return immediately |
| 1729 | * from and lua execution. |
| 1730 | */ |
| 1731 | if (lua->flags & HLUA_EXIT) { |
| 1732 | ret = HLUA_E_OK; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 1733 | hlua_ctx_renew(lua, 1); |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1734 | break; |
| 1735 | } |
| 1736 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1737 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1738 | if (!lua_checkstack(lua->T, 1)) { |
| 1739 | ret = HLUA_E_ERR; |
| 1740 | break; |
| 1741 | } |
| 1742 | msg = lua_tostring(lua->T, -1); |
| 1743 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1744 | lua_pop(lua->T, 1); |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 1745 | trace = hlua_traceback(lua->T, ", "); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1746 | if (msg) |
Thierry Fournier | 46278ff | 2020-11-29 11:48:12 +0100 | [diff] [blame] | 1747 | 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] | 1748 | else |
Thierry Fournier | 46278ff | 2020-11-29 11:48:12 +0100 | [diff] [blame] | 1749 | 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] | 1750 | ret = HLUA_E_ERRMSG; |
| 1751 | break; |
| 1752 | |
| 1753 | case LUA_ERRMEM: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1754 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1755 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1756 | ret = HLUA_E_NOMEM; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1757 | break; |
| 1758 | |
| 1759 | case LUA_ERRERR: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1760 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1761 | if (!lua_checkstack(lua->T, 1)) { |
| 1762 | ret = HLUA_E_ERR; |
| 1763 | break; |
| 1764 | } |
| 1765 | msg = lua_tostring(lua->T, -1); |
| 1766 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1767 | lua_pop(lua->T, 1); |
| 1768 | if (msg) |
Thierry Fournier | 46278ff | 2020-11-29 11:48:12 +0100 | [diff] [blame] | 1769 | 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] | 1770 | else |
Thierry Fournier | 46278ff | 2020-11-29 11:48:12 +0100 | [diff] [blame] | 1771 | 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] | 1772 | ret = HLUA_E_ERRMSG; |
| 1773 | break; |
| 1774 | |
| 1775 | default: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1776 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1777 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1778 | ret = HLUA_E_ERR; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1779 | break; |
| 1780 | } |
| 1781 | |
| 1782 | switch (ret) { |
| 1783 | case HLUA_E_AGAIN: |
| 1784 | break; |
| 1785 | |
| 1786 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1787 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1788 | hlua_ctx_renew(lua, 1); |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1789 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1790 | break; |
| 1791 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1792 | case HLUA_E_ETMOUT: |
| 1793 | case HLUA_E_NOMEM: |
| 1794 | case HLUA_E_YIELD: |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1795 | case HLUA_E_ERR: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1796 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1797 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1798 | hlua_ctx_renew(lua, 0); |
| 1799 | break; |
| 1800 | |
| 1801 | case HLUA_E_OK: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1802 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1803 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1804 | break; |
| 1805 | } |
| 1806 | |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1807 | /* This is the main exit point, remove the Lua lock. */ |
Aurelien DARRAGON | e36f803 | 2023-03-21 13:22:33 +0100 | [diff] [blame] | 1808 | hlua_unlock(lua); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1809 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1810 | return ret; |
| 1811 | } |
| 1812 | |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1813 | /* This function exit the current code. */ |
| 1814 | __LJMP static int hlua_done(lua_State *L) |
| 1815 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 1816 | struct hlua *hlua; |
| 1817 | |
| 1818 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 1819 | hlua = hlua_gethlua(L); |
| 1820 | if (!hlua) |
| 1821 | return 0; |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1822 | |
| 1823 | hlua->flags |= HLUA_EXIT; |
| 1824 | WILL_LJMP(lua_error(L)); |
| 1825 | |
| 1826 | return 0; |
| 1827 | } |
| 1828 | |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1829 | /* This function is an LUA binding. It provides a function |
| 1830 | * for deleting ACL from a referenced ACL file. |
| 1831 | */ |
| 1832 | __LJMP static int hlua_del_acl(lua_State *L) |
| 1833 | { |
| 1834 | const char *name; |
| 1835 | const char *key; |
| 1836 | struct pat_ref *ref; |
| 1837 | |
| 1838 | MAY_LJMP(check_args(L, 2, "del_acl")); |
| 1839 | |
| 1840 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1841 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1842 | |
| 1843 | ref = pat_ref_lookup(name); |
| 1844 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1845 | WILL_LJMP(luaL_error(L, "'del_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1846 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1847 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1848 | pat_ref_delete(ref, key); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1849 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1850 | return 0; |
| 1851 | } |
| 1852 | |
| 1853 | /* This function is an LUA binding. It provides a function |
| 1854 | * for deleting map entry from a referenced map file. |
| 1855 | */ |
| 1856 | static int hlua_del_map(lua_State *L) |
| 1857 | { |
| 1858 | const char *name; |
| 1859 | const char *key; |
| 1860 | struct pat_ref *ref; |
| 1861 | |
| 1862 | MAY_LJMP(check_args(L, 2, "del_map")); |
| 1863 | |
| 1864 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1865 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1866 | |
| 1867 | ref = pat_ref_lookup(name); |
| 1868 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1869 | WILL_LJMP(luaL_error(L, "'del_map': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1870 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1871 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1872 | pat_ref_delete(ref, key); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1873 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1874 | return 0; |
| 1875 | } |
| 1876 | |
| 1877 | /* This function is an LUA binding. It provides a function |
| 1878 | * for adding ACL pattern from a referenced ACL file. |
| 1879 | */ |
| 1880 | static int hlua_add_acl(lua_State *L) |
| 1881 | { |
| 1882 | const char *name; |
| 1883 | const char *key; |
| 1884 | struct pat_ref *ref; |
| 1885 | |
| 1886 | MAY_LJMP(check_args(L, 2, "add_acl")); |
| 1887 | |
| 1888 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1889 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1890 | |
| 1891 | ref = pat_ref_lookup(name); |
| 1892 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1893 | WILL_LJMP(luaL_error(L, "'add_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1894 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1895 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1896 | if (pat_ref_find_elt(ref, key) == NULL) |
| 1897 | pat_ref_add(ref, key, NULL, NULL); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1898 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1899 | return 0; |
| 1900 | } |
| 1901 | |
| 1902 | /* This function is an LUA binding. It provides a function |
| 1903 | * for setting map pattern and sample from a referenced map |
| 1904 | * file. |
| 1905 | */ |
| 1906 | static int hlua_set_map(lua_State *L) |
| 1907 | { |
| 1908 | const char *name; |
| 1909 | const char *key; |
| 1910 | const char *value; |
| 1911 | struct pat_ref *ref; |
| 1912 | |
| 1913 | MAY_LJMP(check_args(L, 3, "set_map")); |
| 1914 | |
| 1915 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1916 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1917 | value = MAY_LJMP(luaL_checkstring(L, 3)); |
| 1918 | |
| 1919 | ref = pat_ref_lookup(name); |
| 1920 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1921 | WILL_LJMP(luaL_error(L, "'set_map': unknown map file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1922 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1923 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1924 | if (pat_ref_find_elt(ref, key) != NULL) |
| 1925 | pat_ref_set(ref, key, value, NULL); |
| 1926 | else |
| 1927 | pat_ref_add(ref, key, value, NULL); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1928 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1929 | return 0; |
| 1930 | } |
| 1931 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1932 | /* A class is a lot of memory that contain data. This data can be a table, |
| 1933 | * 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] | 1934 | * metatable have an original version registered in the global context with |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1935 | * the name of the object (_G[<name>] = <metable> ). |
| 1936 | * |
| 1937 | * A metable is a table that modify the standard behavior of a standard |
| 1938 | * access to the associated data. The entries of this new metatable are |
| 1939 | * defined as is: |
| 1940 | * |
| 1941 | * http://lua-users.org/wiki/MetatableEvents |
| 1942 | * |
| 1943 | * __index |
| 1944 | * |
| 1945 | * we access an absent field in a table, the result is nil. This is |
| 1946 | * true, but it is not the whole truth. Actually, such access triggers |
| 1947 | * the interpreter to look for an __index metamethod: If there is no |
| 1948 | * such method, as usually happens, then the access results in nil; |
| 1949 | * otherwise, the metamethod will provide the result. |
| 1950 | * |
| 1951 | * Control 'prototype' inheritance. When accessing "myTable[key]" and |
| 1952 | * the key does not appear in the table, but the metatable has an __index |
| 1953 | * property: |
| 1954 | * |
| 1955 | * - if the value is a function, the function is called, passing in the |
| 1956 | * table and the key; the return value of that function is returned as |
| 1957 | * the result. |
| 1958 | * |
| 1959 | * - if the value is another table, the value of the key in that table is |
| 1960 | * asked for and returned (and if it doesn't exist in that table, but that |
| 1961 | * table's metatable has an __index property, then it continues on up) |
| 1962 | * |
| 1963 | * - Use "rawget(myTable,key)" to skip this metamethod. |
| 1964 | * |
| 1965 | * http://www.lua.org/pil/13.4.1.html |
| 1966 | * |
| 1967 | * __newindex |
| 1968 | * |
| 1969 | * Like __index, but control property assignment. |
| 1970 | * |
| 1971 | * __mode - Control weak references. A string value with one or both |
| 1972 | * of the characters 'k' and 'v' which specifies that the the |
| 1973 | * keys and/or values in the table are weak references. |
| 1974 | * |
| 1975 | * __call - Treat a table like a function. When a table is followed by |
| 1976 | * parenthesis such as "myTable( 'foo' )" and the metatable has |
| 1977 | * a __call key pointing to a function, that function is invoked |
| 1978 | * (passing any specified arguments) and the return value is |
| 1979 | * returned. |
| 1980 | * |
| 1981 | * __metatable - Hide the metatable. When "getmetatable( myTable )" is |
| 1982 | * called, if the metatable for myTable has a __metatable |
| 1983 | * key, the value of that key is returned instead of the |
| 1984 | * actual metatable. |
| 1985 | * |
| 1986 | * __tostring - Control string representation. When the builtin |
| 1987 | * "tostring( myTable )" function is called, if the metatable |
| 1988 | * for myTable has a __tostring property set to a function, |
| 1989 | * that function is invoked (passing myTable to it) and the |
| 1990 | * return value is used as the string representation. |
| 1991 | * |
| 1992 | * __len - Control table length. When the table length is requested using |
| 1993 | * the length operator ( '#' ), if the metatable for myTable has |
| 1994 | * a __len key pointing to a function, that function is invoked |
| 1995 | * (passing myTable to it) and the return value used as the value |
| 1996 | * of "#myTable". |
| 1997 | * |
| 1998 | * __gc - Userdata finalizer code. When userdata is set to be garbage |
| 1999 | * collected, if the metatable has a __gc field pointing to a |
| 2000 | * function, that function is first invoked, passing the userdata |
| 2001 | * to it. The __gc metamethod is not called for tables. |
| 2002 | * (See http://lua-users.org/lists/lua-l/2006-11/msg00508.html) |
| 2003 | * |
| 2004 | * Special metamethods for redefining standard operators: |
| 2005 | * http://www.lua.org/pil/13.1.html |
| 2006 | * |
| 2007 | * __add "+" |
| 2008 | * __sub "-" |
| 2009 | * __mul "*" |
| 2010 | * __div "/" |
| 2011 | * __unm "!" |
| 2012 | * __pow "^" |
| 2013 | * __concat ".." |
| 2014 | * |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 2015 | * Special methods for redefining standard relations |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 2016 | * http://www.lua.org/pil/13.2.html |
| 2017 | * |
| 2018 | * __eq "==" |
| 2019 | * __lt "<" |
| 2020 | * __le "<=" |
| 2021 | */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2022 | |
| 2023 | /* |
| 2024 | * |
| 2025 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2026 | * Class Map |
| 2027 | * |
| 2028 | * |
| 2029 | */ |
| 2030 | |
| 2031 | /* Returns a struct hlua_map if the stack entry "ud" is |
| 2032 | * a class session, otherwise it throws an error. |
| 2033 | */ |
| 2034 | __LJMP static struct map_descriptor *hlua_checkmap(lua_State *L, int ud) |
| 2035 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 2036 | return MAY_LJMP(hlua_checkudata(L, ud, class_map_ref)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2037 | } |
| 2038 | |
| 2039 | /* This function is the map constructor. It don't need |
| 2040 | * the class Map object. It creates and return a new Map |
| 2041 | * object. It must be called only during "body" or "init" |
| 2042 | * context because it process some filesystem accesses. |
| 2043 | */ |
| 2044 | __LJMP static int hlua_map_new(struct lua_State *L) |
| 2045 | { |
| 2046 | const char *fn; |
| 2047 | int match = PAT_MATCH_STR; |
| 2048 | struct sample_conv conv; |
| 2049 | const char *file = ""; |
| 2050 | int line = 0; |
| 2051 | lua_Debug ar; |
| 2052 | char *err = NULL; |
| 2053 | struct arg args[2]; |
| 2054 | |
| 2055 | if (lua_gettop(L) < 1 || lua_gettop(L) > 2) |
| 2056 | WILL_LJMP(luaL_error(L, "'new' needs at least 1 argument.")); |
| 2057 | |
| 2058 | fn = MAY_LJMP(luaL_checkstring(L, 1)); |
| 2059 | |
| 2060 | if (lua_gettop(L) >= 2) { |
| 2061 | match = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 2062 | if (match < 0 || match >= PAT_MATCH_NUM) |
| 2063 | WILL_LJMP(luaL_error(L, "'new' needs a valid match method.")); |
| 2064 | } |
| 2065 | |
| 2066 | /* Get Lua filename and line number. */ |
| 2067 | if (lua_getstack(L, 1, &ar)) { /* check function at level */ |
| 2068 | lua_getinfo(L, "Sl", &ar); /* get info about it */ |
| 2069 | if (ar.currentline > 0) { /* is there info? */ |
| 2070 | file = ar.short_src; |
| 2071 | line = ar.currentline; |
| 2072 | } |
| 2073 | } |
| 2074 | |
| 2075 | /* fill fake sample_conv struct. */ |
| 2076 | conv.kw = ""; /* unused. */ |
| 2077 | conv.process = NULL; /* unused. */ |
| 2078 | conv.arg_mask = 0; /* unused. */ |
| 2079 | conv.val_args = NULL; /* unused. */ |
| 2080 | conv.out_type = SMP_T_STR; |
| 2081 | conv.private = (void *)(long)match; |
| 2082 | switch (match) { |
| 2083 | case PAT_MATCH_STR: conv.in_type = SMP_T_STR; break; |
| 2084 | case PAT_MATCH_BEG: conv.in_type = SMP_T_STR; break; |
| 2085 | case PAT_MATCH_SUB: conv.in_type = SMP_T_STR; break; |
| 2086 | case PAT_MATCH_DIR: conv.in_type = SMP_T_STR; break; |
| 2087 | case PAT_MATCH_DOM: conv.in_type = SMP_T_STR; break; |
| 2088 | case PAT_MATCH_END: conv.in_type = SMP_T_STR; break; |
| 2089 | case PAT_MATCH_REG: conv.in_type = SMP_T_STR; break; |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 2090 | case PAT_MATCH_INT: conv.in_type = SMP_T_SINT; break; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2091 | case PAT_MATCH_IP: conv.in_type = SMP_T_ADDR; break; |
| 2092 | default: |
| 2093 | WILL_LJMP(luaL_error(L, "'new' doesn't support this match mode.")); |
| 2094 | } |
| 2095 | |
| 2096 | /* fill fake args. */ |
| 2097 | args[0].type = ARGT_STR; |
Christopher Faulet | 73292e9 | 2020-08-06 08:40:09 +0200 | [diff] [blame] | 2098 | args[0].data.str.area = strdup(fn); |
| 2099 | args[0].data.str.data = strlen(fn); |
| 2100 | args[0].data.str.size = args[0].data.str.data+1; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2101 | args[1].type = ARGT_STOP; |
| 2102 | |
| 2103 | /* load the map. */ |
| 2104 | if (!sample_load_map(args, &conv, file, line, &err)) { |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 2105 | /* error case: we can't use luaL_error because we must |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2106 | * free the err variable. |
| 2107 | */ |
| 2108 | luaL_where(L, 1); |
| 2109 | lua_pushfstring(L, "'new': %s.", err); |
| 2110 | lua_concat(L, 2); |
| 2111 | free(err); |
Christopher Faulet | 6ad7df4 | 2020-08-07 11:45:18 +0200 | [diff] [blame] | 2112 | chunk_destroy(&args[0].data.str); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2113 | WILL_LJMP(lua_error(L)); |
| 2114 | } |
| 2115 | |
| 2116 | /* create the lua object. */ |
| 2117 | lua_newtable(L); |
| 2118 | lua_pushlightuserdata(L, args[0].data.map); |
| 2119 | lua_rawseti(L, -2, 0); |
| 2120 | |
| 2121 | /* Pop a class Map metatable and affect it to the userdata. */ |
| 2122 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_map_ref); |
| 2123 | lua_setmetatable(L, -2); |
| 2124 | |
| 2125 | |
| 2126 | return 1; |
| 2127 | } |
| 2128 | |
| 2129 | __LJMP static inline int _hlua_map_lookup(struct lua_State *L, int str) |
| 2130 | { |
| 2131 | struct map_descriptor *desc; |
| 2132 | struct pattern *pat; |
| 2133 | struct sample smp; |
| 2134 | |
| 2135 | MAY_LJMP(check_args(L, 2, "lookup")); |
| 2136 | desc = MAY_LJMP(hlua_checkmap(L, 1)); |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 2137 | if (desc->pat.expect_type == SMP_T_SINT) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 2138 | smp.data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 2139 | smp.data.u.sint = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2140 | } |
| 2141 | else { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 2142 | smp.data.type = SMP_T_STR; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2143 | smp.flags = SMP_F_CONST; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2144 | 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] | 2145 | smp.data.u.str.size = smp.data.u.str.data + 1; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2146 | } |
| 2147 | |
| 2148 | pat = pattern_exec_match(&desc->pat, &smp, 1); |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 2149 | if (!pat || !pat->data) { |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2150 | if (str) |
| 2151 | lua_pushstring(L, ""); |
| 2152 | else |
| 2153 | lua_pushnil(L); |
| 2154 | return 1; |
| 2155 | } |
| 2156 | |
| 2157 | /* 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] | 2158 | 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] | 2159 | return 1; |
| 2160 | } |
| 2161 | |
| 2162 | __LJMP static int hlua_map_lookup(struct lua_State *L) |
| 2163 | { |
| 2164 | return _hlua_map_lookup(L, 0); |
| 2165 | } |
| 2166 | |
| 2167 | __LJMP static int hlua_map_slookup(struct lua_State *L) |
| 2168 | { |
| 2169 | return _hlua_map_lookup(L, 1); |
| 2170 | } |
| 2171 | |
| 2172 | /* |
| 2173 | * |
| 2174 | * |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2175 | * Class Socket |
| 2176 | * |
| 2177 | * |
| 2178 | */ |
| 2179 | |
| 2180 | __LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud) |
| 2181 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 2182 | return MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2183 | } |
| 2184 | |
| 2185 | /* 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] | 2186 | * connection. It is used for notify space available to send or data |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2187 | * received. |
| 2188 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 2189 | static void hlua_socket_handler(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2190 | { |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2191 | struct hlua_csk_ctx *ctx = appctx->svcctx; |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 2192 | struct stconn *sc = appctx_sc(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2193 | |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 2194 | if (unlikely(se_fl_test(appctx->sedesc, (SE_FL_EOS|SE_FL_ERROR|SE_FL_SHR|SE_FL_SHW)))) { |
| 2195 | notification_wake(&ctx->wake_on_read); |
| 2196 | notification_wake(&ctx->wake_on_write); |
| 2197 | return; |
| 2198 | } |
| 2199 | |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2200 | if (ctx->die) { |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 2201 | se_fl_set(appctx->sedesc, SE_FL_EOI|SE_FL_EOS); |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2202 | notification_wake(&ctx->wake_on_read); |
| 2203 | notification_wake(&ctx->wake_on_write); |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 2204 | return; |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 2205 | } |
| 2206 | |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 2207 | /* If we can't write, wakeup the pending write signals. */ |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 2208 | if (channel_output_closed(sc_ic(sc))) |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2209 | notification_wake(&ctx->wake_on_write); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 2210 | |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 2211 | /* If we can't read, wakeup the pending read signals. */ |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 2212 | if (channel_input_closed(sc_oc(sc))) |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2213 | notification_wake(&ctx->wake_on_read); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 2214 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2215 | /* if the connection is not established, inform the stream that we want |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 2216 | * to be notified whenever the connection completes. |
| 2217 | */ |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 2218 | if (sc_opposite(sc)->state < SC_ST_EST) { |
Willy Tarreau | 90e8b45 | 2022-05-25 18:21:43 +0200 | [diff] [blame] | 2219 | applet_need_more_data(appctx); |
Willy Tarreau | b23edc8 | 2022-05-24 16:49:03 +0200 | [diff] [blame] | 2220 | se_need_remote_conn(appctx->sedesc); |
Willy Tarreau | 4164eb9 | 2022-05-25 15:42:03 +0200 | [diff] [blame] | 2221 | applet_have_more_data(appctx); |
Willy Tarreau | d4da196 | 2015-04-20 01:31:23 +0200 | [diff] [blame] | 2222 | return; |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 2223 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2224 | |
| 2225 | /* This function is called after the connect. */ |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2226 | ctx->connected = 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2227 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2228 | /* Wake the tasks which wants to write if the buffer have available space. */ |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 2229 | if (channel_may_recv(sc_ic(sc))) |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2230 | notification_wake(&ctx->wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2231 | |
| 2232 | /* Wake the tasks which wants to read if the buffer contains data. */ |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 2233 | if (!channel_is_empty(sc_oc(sc))) |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2234 | notification_wake(&ctx->wake_on_read); |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 2235 | |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 2236 | /* If write notifications are registered, we considers we want |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 2237 | * to write, so we clear the blocking flag. |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 2238 | */ |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2239 | if (notification_registered(&ctx->wake_on_write)) |
Willy Tarreau | 4164eb9 | 2022-05-25 15:42:03 +0200 | [diff] [blame] | 2240 | applet_have_more_data(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2241 | } |
| 2242 | |
Christopher Faulet | 5f35a3e | 2022-05-12 15:31:05 +0200 | [diff] [blame] | 2243 | static int hlua_socket_init(struct appctx *appctx) |
| 2244 | { |
| 2245 | struct hlua_csk_ctx *ctx = appctx->svcctx; |
| 2246 | struct stream *s; |
| 2247 | |
| 2248 | if (appctx_finalize_startup(appctx, socket_proxy, &BUF_NULL) == -1) |
| 2249 | goto error; |
| 2250 | |
| 2251 | s = appctx_strm(appctx); |
| 2252 | |
Willy Tarreau | 4596fe2 | 2022-05-17 19:07:51 +0200 | [diff] [blame] | 2253 | /* Configure "right" stream connector. This stconn is used to connect |
Christopher Faulet | 5f35a3e | 2022-05-12 15:31:05 +0200 | [diff] [blame] | 2254 | * and retrieve data from the server. The connection is initialized |
| 2255 | * with the "struct server". |
| 2256 | */ |
Willy Tarreau | 74568cf | 2022-05-27 09:03:30 +0200 | [diff] [blame] | 2257 | sc_set_state(s->scb, SC_ST_ASS); |
Christopher Faulet | 5f35a3e | 2022-05-12 15:31:05 +0200 | [diff] [blame] | 2258 | |
| 2259 | /* Force destination server. */ |
| 2260 | s->flags |= SF_DIRECT | SF_ASSIGNED | SF_BE_ASSIGNED; |
| 2261 | s->target = &socket_tcp->obj_type; |
| 2262 | |
| 2263 | ctx->appctx = appctx; |
| 2264 | return 0; |
| 2265 | |
| 2266 | error: |
| 2267 | return -1; |
| 2268 | } |
| 2269 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2270 | /* This function is called when the "struct stream" is destroyed. |
| 2271 | * Remove the link from the object to this stream. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2272 | * Wake all the pending signals. |
| 2273 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 2274 | static void hlua_socket_release(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2275 | { |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2276 | struct hlua_csk_ctx *ctx = appctx->svcctx; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2277 | struct xref *peer; |
| 2278 | |
Christopher Faulet | 5f35a3e | 2022-05-12 15:31:05 +0200 | [diff] [blame] | 2279 | /* Remove my link in the original objects. */ |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2280 | peer = xref_get_peer_and_lock(&ctx->xref); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2281 | if (peer) |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2282 | xref_disconnect(&ctx->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2283 | |
| 2284 | /* Wake all the task waiting for me. */ |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2285 | notification_wake(&ctx->wake_on_read); |
| 2286 | notification_wake(&ctx->wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2287 | } |
| 2288 | |
| 2289 | /* If the garbage collectio of the object is launch, nobody |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2290 | * uses this object. If the stream does not exists, just quit. |
| 2291 | * Send the shutdown signal to the stream. In some cases, |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2292 | * pending signal can rest in the read and write lists. destroy |
| 2293 | * it. |
| 2294 | */ |
| 2295 | __LJMP static int hlua_socket_gc(lua_State *L) |
| 2296 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2297 | struct hlua_socket *socket; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2298 | struct hlua_csk_ctx *ctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2299 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2300 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2301 | MAY_LJMP(check_args(L, 1, "__gc")); |
| 2302 | |
| 2303 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2304 | peer = xref_get_peer_and_lock(&socket->xref); |
| 2305 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2306 | return 0; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2307 | |
| 2308 | ctx = container_of(peer, struct hlua_csk_ctx, xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2309 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2310 | /* Set the flag which destroy the session. */ |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2311 | ctx->die = 1; |
| 2312 | appctx_wakeup(ctx->appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2313 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2314 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2315 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2316 | return 0; |
| 2317 | } |
| 2318 | |
| 2319 | /* The close function send shutdown signal and break the |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2320 | * links between the stream and the object. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2321 | */ |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 2322 | __LJMP static int hlua_socket_close_helper(lua_State *L) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2323 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2324 | struct hlua_socket *socket; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2325 | struct hlua_csk_ctx *ctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2326 | struct xref *peer; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2327 | struct hlua *hlua; |
| 2328 | |
| 2329 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 2330 | hlua = hlua_gethlua(L); |
| 2331 | if (!hlua) |
| 2332 | return 0; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2333 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2334 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2335 | |
| 2336 | /* Check if we run on the same thread than the xreator thread. |
| 2337 | * We cannot access to the socket if the thread is different. |
| 2338 | */ |
| 2339 | if (socket->tid != tid) |
| 2340 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2341 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2342 | peer = xref_get_peer_and_lock(&socket->xref); |
| 2343 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2344 | return 0; |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 2345 | |
| 2346 | hlua->gc_count--; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2347 | ctx = container_of(peer, struct hlua_csk_ctx, xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2348 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2349 | /* Set the flag which destroy the session. */ |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2350 | ctx->die = 1; |
| 2351 | appctx_wakeup(ctx->appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2352 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2353 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2354 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2355 | return 0; |
| 2356 | } |
| 2357 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 2358 | /* The close function calls close_helper. |
| 2359 | */ |
| 2360 | __LJMP static int hlua_socket_close(lua_State *L) |
| 2361 | { |
| 2362 | MAY_LJMP(check_args(L, 1, "close")); |
| 2363 | return hlua_socket_close_helper(L); |
| 2364 | } |
| 2365 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2366 | /* This Lua function assumes that the stack contain three parameters. |
| 2367 | * 1 - USERDATA containing a struct socket |
| 2368 | * 2 - INTEGER with values of the macro defined below |
| 2369 | * If the integer is -1, we must read at most one line. |
| 2370 | * If the integer is -2, we ust read all the data until the |
| 2371 | * end of the stream. |
| 2372 | * If the integer is positive value, we must read a number of |
| 2373 | * bytes corresponding to this value. |
| 2374 | */ |
| 2375 | #define HLSR_READ_LINE (-1) |
| 2376 | #define HLSR_READ_ALL (-2) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2377 | __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] | 2378 | { |
| 2379 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2380 | int wanted = lua_tointeger(L, 2); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2381 | struct hlua *hlua; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2382 | struct hlua_csk_ctx *csk_ctx; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2383 | struct appctx *appctx; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2384 | size_t len; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2385 | int nblk; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 2386 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2387 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 2388 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2389 | size_t len2; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 2390 | int skip_at_end = 0; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 2391 | struct channel *oc; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2392 | struct stream *s; |
| 2393 | struct xref *peer; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 2394 | int missing_bytes; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2395 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2396 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 2397 | hlua = hlua_gethlua(L); |
| 2398 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2399 | /* Check if this lua stack is schedulable. */ |
| 2400 | if (!hlua || !hlua->task) |
| 2401 | WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in " |
| 2402 | "'frontend', 'backend' or 'task'")); |
| 2403 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2404 | /* Check if we run on the same thread than the xreator thread. |
| 2405 | * We cannot access to the socket if the thread is different. |
| 2406 | */ |
| 2407 | if (socket->tid != tid) |
| 2408 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2409 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2410 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2411 | peer = xref_get_peer_and_lock(&socket->xref); |
| 2412 | if (!peer) |
| 2413 | goto no_peer; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2414 | |
| 2415 | csk_ctx = container_of(peer, struct hlua_csk_ctx, xref); |
| 2416 | appctx = csk_ctx->appctx; |
Willy Tarreau | 0698c80 | 2022-05-11 14:09:57 +0200 | [diff] [blame] | 2417 | s = appctx_strm(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2418 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2419 | oc = &s->res; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2420 | if (wanted == HLSR_READ_LINE) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2421 | /* Read line. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2422 | nblk = co_getline_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2423 | if (nblk < 0) /* Connection close. */ |
| 2424 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2425 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2426 | goto connection_empty; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 2427 | |
| 2428 | /* remove final \r\n. */ |
| 2429 | if (nblk == 1) { |
| 2430 | if (blk1[len1-1] == '\n') { |
| 2431 | len1--; |
| 2432 | skip_at_end++; |
| 2433 | if (blk1[len1-1] == '\r') { |
| 2434 | len1--; |
| 2435 | skip_at_end++; |
| 2436 | } |
| 2437 | } |
| 2438 | } |
| 2439 | else { |
| 2440 | if (blk2[len2-1] == '\n') { |
| 2441 | len2--; |
| 2442 | skip_at_end++; |
| 2443 | if (blk2[len2-1] == '\r') { |
| 2444 | len2--; |
| 2445 | skip_at_end++; |
| 2446 | } |
| 2447 | } |
| 2448 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2449 | } |
| 2450 | |
| 2451 | else if (wanted == HLSR_READ_ALL) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2452 | /* Read all the available data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2453 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2454 | if (nblk < 0) /* Connection close. */ |
| 2455 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2456 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2457 | goto connection_empty; |
| 2458 | } |
| 2459 | |
| 2460 | else { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2461 | /* Read a block of data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2462 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2463 | if (nblk < 0) /* Connection close. */ |
| 2464 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2465 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2466 | goto connection_empty; |
| 2467 | |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 2468 | missing_bytes = wanted - socket->b.n; |
| 2469 | if (len1 > missing_bytes) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2470 | nblk = 1; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 2471 | len1 = missing_bytes; |
| 2472 | } if (nblk == 2 && len1 + len2 > missing_bytes) |
| 2473 | len2 = missing_bytes - len1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2474 | } |
| 2475 | |
| 2476 | len = len1; |
| 2477 | |
| 2478 | luaL_addlstring(&socket->b, blk1, len1); |
| 2479 | if (nblk == 2) { |
| 2480 | len += len2; |
| 2481 | luaL_addlstring(&socket->b, blk2, len2); |
| 2482 | } |
| 2483 | |
| 2484 | /* Consume data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2485 | co_skip(oc, len + skip_at_end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2486 | |
| 2487 | /* Don't wait anything. */ |
Thierry FOURNIER | 7e4ee47 | 2018-05-25 15:03:50 +0200 | [diff] [blame] | 2488 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2489 | |
| 2490 | /* If the pattern reclaim to read all the data |
| 2491 | * in the connection, got out. |
| 2492 | */ |
| 2493 | if (wanted == HLSR_READ_ALL) |
| 2494 | goto connection_empty; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 2495 | else if (wanted >= 0 && socket->b.n < wanted) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2496 | goto connection_empty; |
| 2497 | |
| 2498 | /* Return result. */ |
| 2499 | luaL_pushresult(&socket->b); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2500 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2501 | return 1; |
| 2502 | |
| 2503 | connection_closed: |
| 2504 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2505 | xref_unlock(&socket->xref, peer); |
| 2506 | |
| 2507 | no_peer: |
| 2508 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2509 | /* If the buffer containds data. */ |
| 2510 | if (socket->b.n > 0) { |
| 2511 | luaL_pushresult(&socket->b); |
| 2512 | return 1; |
| 2513 | } |
| 2514 | lua_pushnil(L); |
| 2515 | lua_pushstring(L, "connection closed."); |
| 2516 | return 2; |
| 2517 | |
| 2518 | connection_empty: |
| 2519 | |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2520 | if (!notification_new(&hlua->com, &csk_ctx->wake_on_read, hlua->task)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2521 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2522 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2523 | } |
| 2524 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2525 | 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] | 2526 | return 0; |
| 2527 | } |
| 2528 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2529 | /* This Lua function gets two parameters. The first one can be string |
| 2530 | * or a number. If the string is "*l", the user requires one line. If |
| 2531 | * 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] | 2532 | * If the value is a number, the user require a number of bytes equal |
| 2533 | * to the value. The default value is "*l" (a line). |
| 2534 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2535 | * This parameter with a variable type is converted in integer. This |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2536 | * integer takes this values: |
| 2537 | * -1 : read a line |
| 2538 | * -2 : read all the stream |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2539 | * >0 : amount of bytes. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2540 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2541 | * The second parameter is optional. It contains a string that must be |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2542 | * concatenated with the read data. |
| 2543 | */ |
| 2544 | __LJMP static int hlua_socket_receive(struct lua_State *L) |
| 2545 | { |
| 2546 | int wanted = HLSR_READ_LINE; |
| 2547 | const char *pattern; |
Christopher Faulet | c31b200 | 2021-05-03 10:11:13 +0200 | [diff] [blame] | 2548 | int lastarg, type; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2549 | char *error; |
| 2550 | size_t len; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2551 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2552 | |
| 2553 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 2554 | WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments.")); |
| 2555 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2556 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2557 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2558 | /* Check if we run on the same thread than the xreator thread. |
| 2559 | * We cannot access to the socket if the thread is different. |
| 2560 | */ |
| 2561 | if (socket->tid != tid) |
| 2562 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2563 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2564 | /* check for pattern. */ |
| 2565 | if (lua_gettop(L) >= 2) { |
| 2566 | type = lua_type(L, 2); |
| 2567 | if (type == LUA_TSTRING) { |
| 2568 | pattern = lua_tostring(L, 2); |
| 2569 | if (strcmp(pattern, "*a") == 0) |
| 2570 | wanted = HLSR_READ_ALL; |
| 2571 | else if (strcmp(pattern, "*l") == 0) |
| 2572 | wanted = HLSR_READ_LINE; |
| 2573 | else { |
| 2574 | wanted = strtoll(pattern, &error, 10); |
| 2575 | if (*error != '\0') |
| 2576 | WILL_LJMP(luaL_error(L, "Unsupported pattern.")); |
| 2577 | } |
| 2578 | } |
| 2579 | else if (type == LUA_TNUMBER) { |
| 2580 | wanted = lua_tointeger(L, 2); |
| 2581 | if (wanted < 0) |
| 2582 | WILL_LJMP(luaL_error(L, "Unsupported size.")); |
| 2583 | } |
| 2584 | } |
| 2585 | |
| 2586 | /* Set pattern. */ |
| 2587 | lua_pushinteger(L, wanted); |
Tim Duesterhus | c6e377e | 2018-01-04 19:32:13 +0100 | [diff] [blame] | 2588 | |
| 2589 | /* Check if we would replace the top by itself. */ |
| 2590 | if (lua_gettop(L) != 2) |
| 2591 | lua_replace(L, 2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2592 | |
Christopher Faulet | c31b200 | 2021-05-03 10:11:13 +0200 | [diff] [blame] | 2593 | /* Save index of the top of the stack because since buffers are used, it |
| 2594 | * may change |
| 2595 | */ |
| 2596 | lastarg = lua_gettop(L); |
| 2597 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2598 | /* init buffer, and fill it with prefix. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2599 | luaL_buffinit(L, &socket->b); |
| 2600 | |
| 2601 | /* Check prefix. */ |
Christopher Faulet | c31b200 | 2021-05-03 10:11:13 +0200 | [diff] [blame] | 2602 | if (lastarg >= 3) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2603 | if (lua_type(L, 3) != LUA_TSTRING) |
| 2604 | WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix")); |
| 2605 | pattern = lua_tolstring(L, 3, &len); |
| 2606 | luaL_addlstring(&socket->b, pattern, len); |
| 2607 | } |
| 2608 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2609 | return __LJMP(hlua_socket_receive_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2610 | } |
| 2611 | |
| 2612 | /* Write the Lua input string in the output buffer. |
Mark Lakes | 22154b4 | 2018-01-29 14:38:40 -0800 | [diff] [blame] | 2613 | * This function returns a yield if no space is available. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2614 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2615 | 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] | 2616 | { |
| 2617 | struct hlua_socket *socket; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2618 | struct hlua *hlua; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2619 | struct hlua_csk_ctx *csk_ctx; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2620 | struct appctx *appctx; |
| 2621 | size_t buf_len; |
| 2622 | const char *buf; |
| 2623 | int len; |
| 2624 | int send_len; |
| 2625 | int sent; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2626 | struct xref *peer; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2627 | struct stream *s; |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 2628 | struct stconn *sc; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2629 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2630 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 2631 | hlua = hlua_gethlua(L); |
| 2632 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2633 | /* Check if this lua stack is schedulable. */ |
| 2634 | if (!hlua || !hlua->task) |
| 2635 | WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in " |
| 2636 | "'frontend', 'backend' or 'task'")); |
| 2637 | |
| 2638 | /* Get object */ |
| 2639 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2640 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2641 | sent = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2642 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2643 | /* Check if we run on the same thread than the xreator thread. |
| 2644 | * We cannot access to the socket if the thread is different. |
| 2645 | */ |
| 2646 | if (socket->tid != tid) |
| 2647 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2648 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2649 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2650 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2651 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2652 | lua_pushinteger(L, -1); |
| 2653 | return 1; |
| 2654 | } |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2655 | |
| 2656 | csk_ctx = container_of(peer, struct hlua_csk_ctx, xref); |
| 2657 | appctx = csk_ctx->appctx; |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 2658 | sc = appctx_sc(appctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 2659 | s = __sc_strm(sc); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2660 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2661 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2662 | if (channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2663 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2664 | lua_pushinteger(L, -1); |
| 2665 | return 1; |
| 2666 | } |
| 2667 | |
| 2668 | /* Update the input buffer data. */ |
| 2669 | buf += sent; |
| 2670 | send_len = buf_len - sent; |
| 2671 | |
| 2672 | /* All the data are sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2673 | if (sent >= buf_len) { |
| 2674 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2675 | return 1; /* Implicitly return the length sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2676 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2677 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2678 | /* Check if the buffer is available because HAProxy doesn't allocate |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 2679 | * the request buffer if its not required. |
| 2680 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2681 | if (s->req.buf.size == 0) { |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 2682 | if (!sc_alloc_ibuf(sc, &appctx->buffer_wait)) |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2683 | goto hlua_socket_write_yield_return; |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 2684 | } |
| 2685 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2686 | /* Check for available space. */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2687 | len = b_room(&s->req.buf); |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2688 | if (len <= 0) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2689 | goto hlua_socket_write_yield_return; |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2690 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2691 | |
| 2692 | /* send data */ |
| 2693 | if (len < send_len) |
| 2694 | send_len = len; |
Thierry FOURNIER | 66b8919 | 2018-05-27 01:14:47 +0200 | [diff] [blame] | 2695 | len = ci_putblk(&s->req, buf, send_len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2696 | |
| 2697 | /* "Not enough space" (-1), "Buffer too little to contain |
| 2698 | * the data" (-2) are not expected because the available length |
| 2699 | * is tested. |
| 2700 | * Other unknown error are also not expected. |
| 2701 | */ |
| 2702 | if (len <= 0) { |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2703 | if (len == -1) |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2704 | s->req.flags |= CF_WAKE_WRITE; |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2705 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 2706 | MAY_LJMP(hlua_socket_close_helper(L)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2707 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2708 | lua_pushinteger(L, -1); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2709 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2710 | return 1; |
| 2711 | } |
| 2712 | |
| 2713 | /* update buffers. */ |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 2714 | appctx_wakeup(appctx); |
Willy Tarreau | de70fa1 | 2015-09-26 11:25:05 +0200 | [diff] [blame] | 2715 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2716 | /* Update length sent. */ |
| 2717 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2718 | lua_pushinteger(L, sent + len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2719 | |
| 2720 | /* All the data buffer is sent ? */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2721 | if (sent + len >= buf_len) { |
| 2722 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2723 | return 1; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2724 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2725 | |
| 2726 | hlua_socket_write_yield_return: |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2727 | if (!notification_new(&hlua->com, &csk_ctx->wake_on_write, hlua->task)) { |
Thierry FOURNIER | ba42fcd | 2018-05-27 00:59:48 +0200 | [diff] [blame] | 2728 | xref_unlock(&socket->xref, peer); |
| 2729 | WILL_LJMP(luaL_error(L, "out of memory")); |
| 2730 | } |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2731 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2732 | 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] | 2733 | return 0; |
| 2734 | } |
| 2735 | |
| 2736 | /* This function initiate the send of data. It just check the input |
| 2737 | * parameters and push an integer in the Lua stack that contain the |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2738 | * 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] | 2739 | * "hlua_socket_write_yield" that can yield. |
| 2740 | * |
| 2741 | * The Lua function gets between 3 and 4 parameters. The first one is |
| 2742 | * the associated object. The second is a string buffer. The third is |
| 2743 | * a facultative integer that represents where is the buffer position |
| 2744 | * of the start of the data that can send. The first byte is the |
| 2745 | * position "1". The default value is "1". The fourth argument is a |
| 2746 | * facultative integer that represents where is the buffer position |
| 2747 | * of the end of the data that can send. The default is the last byte. |
| 2748 | */ |
| 2749 | static int hlua_socket_send(struct lua_State *L) |
| 2750 | { |
| 2751 | int i; |
| 2752 | int j; |
| 2753 | const char *buf; |
| 2754 | size_t buf_len; |
| 2755 | |
| 2756 | /* Check number of arguments. */ |
| 2757 | if (lua_gettop(L) < 2 || lua_gettop(L) > 4) |
| 2758 | WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments")); |
| 2759 | |
| 2760 | /* Get the string. */ |
| 2761 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
| 2762 | |
| 2763 | /* Get and check j. */ |
| 2764 | if (lua_gettop(L) == 4) { |
| 2765 | j = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 2766 | if (j < 0) |
| 2767 | j = buf_len + j + 1; |
| 2768 | if (j > buf_len) |
| 2769 | j = buf_len + 1; |
| 2770 | lua_pop(L, 1); |
| 2771 | } |
| 2772 | else |
| 2773 | j = buf_len; |
| 2774 | |
| 2775 | /* Get and check i. */ |
| 2776 | if (lua_gettop(L) == 3) { |
| 2777 | i = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2778 | if (i < 0) |
| 2779 | i = buf_len + i + 1; |
| 2780 | if (i > buf_len) |
| 2781 | i = buf_len + 1; |
| 2782 | lua_pop(L, 1); |
| 2783 | } else |
| 2784 | i = 1; |
| 2785 | |
| 2786 | /* Check bth i and j. */ |
| 2787 | if (i > j) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2788 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2789 | return 1; |
| 2790 | } |
| 2791 | if (i == 0 && j == 0) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2792 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2793 | return 1; |
| 2794 | } |
| 2795 | if (i == 0) |
| 2796 | i = 1; |
| 2797 | if (j == 0) |
| 2798 | j = 1; |
| 2799 | |
| 2800 | /* Pop the string. */ |
| 2801 | lua_pop(L, 1); |
| 2802 | |
| 2803 | /* Update the buffer length. */ |
| 2804 | buf += i - 1; |
| 2805 | buf_len = j - i + 1; |
| 2806 | lua_pushlstring(L, buf, buf_len); |
| 2807 | |
| 2808 | /* This unsigned is used to remember the amount of sent data. */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2809 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2810 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2811 | return MAY_LJMP(hlua_socket_write_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2812 | } |
| 2813 | |
Willy Tarreau | 22b0a68 | 2015-06-17 19:43:49 +0200 | [diff] [blame] | 2814 | #define SOCKET_INFO_MAX_LEN sizeof("[0000:0000:0000:0000:0000:0000:0000:0000]:12345") |
Christopher Faulet | e6465b3 | 2021-10-22 15:36:08 +0200 | [diff] [blame] | 2815 | __LJMP static inline int hlua_socket_info(struct lua_State *L, const struct sockaddr_storage *addr) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2816 | { |
| 2817 | static char buffer[SOCKET_INFO_MAX_LEN]; |
| 2818 | int ret; |
| 2819 | int len; |
| 2820 | char *p; |
| 2821 | |
| 2822 | ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1); |
| 2823 | if (ret <= 0) { |
| 2824 | lua_pushnil(L); |
| 2825 | return 1; |
| 2826 | } |
| 2827 | |
| 2828 | if (ret == AF_UNIX) { |
| 2829 | lua_pushstring(L, buffer+1); |
| 2830 | return 1; |
| 2831 | } |
| 2832 | else if (ret == AF_INET6) { |
| 2833 | buffer[0] = '['; |
| 2834 | len = strlen(buffer); |
| 2835 | buffer[len] = ']'; |
| 2836 | len++; |
| 2837 | buffer[len] = ':'; |
| 2838 | len++; |
| 2839 | p = buffer; |
| 2840 | } |
| 2841 | else if (ret == AF_INET) { |
| 2842 | p = buffer + 1; |
| 2843 | len = strlen(p); |
| 2844 | p[len] = ':'; |
| 2845 | len++; |
| 2846 | } |
| 2847 | else { |
| 2848 | lua_pushnil(L); |
| 2849 | return 1; |
| 2850 | } |
| 2851 | |
| 2852 | if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) { |
| 2853 | lua_pushnil(L); |
| 2854 | return 1; |
| 2855 | } |
| 2856 | |
| 2857 | lua_pushstring(L, p); |
| 2858 | return 1; |
| 2859 | } |
| 2860 | |
| 2861 | /* Returns information about the peer of the connection. */ |
| 2862 | __LJMP static int hlua_socket_getpeername(struct lua_State *L) |
| 2863 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2864 | struct hlua_socket *socket; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2865 | struct xref *peer; |
| 2866 | struct appctx *appctx; |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 2867 | struct stconn *sc; |
Christopher Faulet | 16f16af | 2021-10-27 09:34:56 +0200 | [diff] [blame] | 2868 | const struct sockaddr_storage *dst; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2869 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2870 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2871 | MAY_LJMP(check_args(L, 1, "getpeername")); |
| 2872 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2873 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2874 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2875 | /* Check if we run on the same thread than the xreator thread. |
| 2876 | * We cannot access to the socket if the thread is different. |
| 2877 | */ |
| 2878 | if (socket->tid != tid) |
| 2879 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2880 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2881 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2882 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2883 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2884 | lua_pushnil(L); |
| 2885 | return 1; |
| 2886 | } |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2887 | |
| 2888 | appctx = container_of(peer, struct hlua_csk_ctx, xref)->appctx; |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 2889 | sc = appctx_sc(appctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 2890 | dst = sc_dst(sc_opposite(sc)); |
Christopher Faulet | 16f16af | 2021-10-27 09:34:56 +0200 | [diff] [blame] | 2891 | if (!dst) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2892 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2893 | lua_pushnil(L); |
| 2894 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2895 | } |
| 2896 | |
Christopher Faulet | 16f16af | 2021-10-27 09:34:56 +0200 | [diff] [blame] | 2897 | ret = MAY_LJMP(hlua_socket_info(L, dst)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2898 | xref_unlock(&socket->xref, peer); |
| 2899 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2900 | } |
| 2901 | |
| 2902 | /* Returns information about my connection side. */ |
| 2903 | static int hlua_socket_getsockname(struct lua_State *L) |
| 2904 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2905 | struct hlua_socket *socket; |
| 2906 | struct connection *conn; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2907 | struct appctx *appctx; |
| 2908 | struct xref *peer; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2909 | struct stream *s; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2910 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2911 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2912 | MAY_LJMP(check_args(L, 1, "getsockname")); |
| 2913 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2914 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2915 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2916 | /* Check if we run on the same thread than the xreator thread. |
| 2917 | * We cannot access to the socket if the thread is different. |
| 2918 | */ |
| 2919 | if (socket->tid != tid) |
| 2920 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2921 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2922 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2923 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2924 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2925 | lua_pushnil(L); |
| 2926 | return 1; |
| 2927 | } |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2928 | |
| 2929 | appctx = container_of(peer, struct hlua_csk_ctx, xref)->appctx; |
Willy Tarreau | 0698c80 | 2022-05-11 14:09:57 +0200 | [diff] [blame] | 2930 | s = appctx_strm(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2931 | |
Willy Tarreau | fd9417b | 2022-05-18 16:23:22 +0200 | [diff] [blame] | 2932 | conn = sc_conn(s->scb); |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2933 | if (!conn || !conn_get_src(conn)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2934 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2935 | lua_pushnil(L); |
| 2936 | return 1; |
| 2937 | } |
| 2938 | |
Willy Tarreau | 9da9a6f | 2019-07-17 14:49:44 +0200 | [diff] [blame] | 2939 | ret = hlua_socket_info(L, conn->src); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2940 | xref_unlock(&socket->xref, peer); |
| 2941 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2942 | } |
| 2943 | |
| 2944 | /* This struct define the applet. */ |
Willy Tarreau | 3057645 | 2015-04-13 13:50:30 +0200 | [diff] [blame] | 2945 | static struct applet update_applet = { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2946 | .obj_type = OBJ_TYPE_APPLET, |
| 2947 | .name = "<LUA_TCP>", |
| 2948 | .fct = hlua_socket_handler, |
Christopher Faulet | 5f35a3e | 2022-05-12 15:31:05 +0200 | [diff] [blame] | 2949 | .init = hlua_socket_init, |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2950 | .release = hlua_socket_release, |
| 2951 | }; |
| 2952 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2953 | __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] | 2954 | { |
| 2955 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2956 | struct hlua *hlua; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2957 | struct xref *peer; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2958 | struct hlua_csk_ctx *csk_ctx; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2959 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2960 | struct stream *s; |
| 2961 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2962 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 2963 | hlua = hlua_gethlua(L); |
| 2964 | if (!hlua) |
| 2965 | return 0; |
| 2966 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2967 | /* Check if we run on the same thread than the xreator thread. |
| 2968 | * We cannot access to the socket if the thread is different. |
| 2969 | */ |
| 2970 | if (socket->tid != tid) |
| 2971 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2972 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2973 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2974 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2975 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2976 | lua_pushnil(L); |
| 2977 | lua_pushstring(L, "Can't connect"); |
| 2978 | return 2; |
| 2979 | } |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2980 | |
| 2981 | csk_ctx = container_of(peer, struct hlua_csk_ctx, xref); |
| 2982 | appctx = csk_ctx->appctx; |
Willy Tarreau | 0698c80 | 2022-05-11 14:09:57 +0200 | [diff] [blame] | 2983 | s = appctx_strm(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2984 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2985 | /* Check if we run on the same thread than the xreator thread. |
| 2986 | * We cannot access to the socket if the thread is different. |
| 2987 | */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2988 | if (socket->tid != tid) { |
| 2989 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2990 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2991 | } |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2992 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2993 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2994 | if (!hlua || channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2995 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2996 | lua_pushnil(L); |
| 2997 | lua_pushstring(L, "Can't connect"); |
| 2998 | return 2; |
| 2999 | } |
| 3000 | |
Willy Tarreau | 8e7c6e6 | 2022-05-18 17:58:02 +0200 | [diff] [blame] | 3001 | appctx = __sc_appctx(s->scf); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3002 | |
| 3003 | /* Check for connection established. */ |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 3004 | if (csk_ctx->connected) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 3005 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3006 | lua_pushinteger(L, 1); |
| 3007 | return 1; |
| 3008 | } |
| 3009 | |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 3010 | if (!notification_new(&hlua->com, &csk_ctx->wake_on_write, hlua->task)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 3011 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3012 | WILL_LJMP(luaL_error(L, "out of memory error")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 3013 | } |
| 3014 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3015 | 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] | 3016 | return 0; |
| 3017 | } |
| 3018 | |
| 3019 | /* This function fail or initite the connection. */ |
| 3020 | __LJMP static int hlua_socket_connect(struct lua_State *L) |
| 3021 | { |
| 3022 | struct hlua_socket *socket; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 3023 | int port = -1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3024 | const char *ip; |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 3025 | struct hlua *hlua; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 3026 | struct hlua_csk_ctx *csk_ctx; |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 3027 | struct appctx *appctx; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 3028 | int low, high; |
| 3029 | struct sockaddr_storage *addr; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 3030 | struct xref *peer; |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 3031 | struct stconn *sc; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 3032 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3033 | |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 3034 | if (lua_gettop(L) < 2) |
| 3035 | WILL_LJMP(luaL_error(L, "connect: need at least 2 arguments")); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3036 | |
| 3037 | /* Get args. */ |
| 3038 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 3039 | |
| 3040 | /* Check if we run on the same thread than the xreator thread. |
| 3041 | * We cannot access to the socket if the thread is different. |
| 3042 | */ |
| 3043 | if (socket->tid != tid) |
| 3044 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 3045 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3046 | ip = MAY_LJMP(luaL_checkstring(L, 2)); |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 3047 | if (lua_gettop(L) >= 3) { |
| 3048 | luaL_Buffer b; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 3049 | port = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3050 | |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 3051 | /* Force the ip to end with a colon, to support IPv6 addresses |
| 3052 | * that are not enclosed within square brackets. |
| 3053 | */ |
| 3054 | if (port > 0) { |
| 3055 | luaL_buffinit(L, &b); |
| 3056 | luaL_addstring(&b, ip); |
| 3057 | luaL_addchar(&b, ':'); |
| 3058 | luaL_pushresult(&b); |
| 3059 | ip = lua_tolstring(L, lua_gettop(L), NULL); |
| 3060 | } |
| 3061 | } |
| 3062 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 3063 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 3064 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 3065 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 3066 | lua_pushnil(L); |
| 3067 | return 1; |
| 3068 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3069 | |
| 3070 | /* Parse ip address. */ |
Willy Tarreau | 5fc9328 | 2020-09-16 18:25:03 +0200 | [diff] [blame] | 3071 | 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] | 3072 | if (!addr) { |
| 3073 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 3074 | WILL_LJMP(luaL_error(L, "connect: cannot parse destination address '%s'", ip)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 3075 | } |
Willy Tarreau | 9da9a6f | 2019-07-17 14:49:44 +0200 | [diff] [blame] | 3076 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3077 | /* Set port. */ |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 3078 | if (low == 0) { |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 3079 | if (addr->ss_family == AF_INET) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 3080 | if (port == -1) { |
| 3081 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 3082 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 3083 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 3084 | ((struct sockaddr_in *)addr)->sin_port = htons(port); |
| 3085 | } else if (addr->ss_family == AF_INET6) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 3086 | if (port == -1) { |
| 3087 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 3088 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 3089 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 3090 | ((struct sockaddr_in6 *)addr)->sin6_port = htons(port); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 3091 | } |
| 3092 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 3093 | |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 3094 | csk_ctx = container_of(peer, struct hlua_csk_ctx, xref); |
| 3095 | appctx = csk_ctx->appctx; |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 3096 | sc = appctx_sc(appctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 3097 | s = __sc_strm(sc); |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 3098 | |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 3099 | if (!sockaddr_alloc(&sc_opposite(sc)->dst, addr, sizeof(*addr))) { |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 3100 | xref_unlock(&socket->xref, peer); |
| 3101 | WILL_LJMP(luaL_error(L, "connect: internal error")); |
| 3102 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3103 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 3104 | /* Get hlua struct, or NULL if we execute from main lua state */ |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 3105 | hlua = hlua_gethlua(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 3106 | if (!hlua) |
| 3107 | return 0; |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 3108 | |
| 3109 | /* inform the stream that we want to be notified whenever the |
| 3110 | * connection completes. |
| 3111 | */ |
Willy Tarreau | 90e8b45 | 2022-05-25 18:21:43 +0200 | [diff] [blame] | 3112 | applet_need_more_data(appctx); |
Willy Tarreau | 4164eb9 | 2022-05-25 15:42:03 +0200 | [diff] [blame] | 3113 | applet_have_more_data(appctx); |
Thierry FOURNIER | 8c8fbbe | 2015-09-26 17:02:35 +0200 | [diff] [blame] | 3114 | appctx_wakeup(appctx); |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 3115 | |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 3116 | hlua->gc_count++; |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 3117 | |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 3118 | if (!notification_new(&hlua->com, &csk_ctx->wake_on_write, hlua->task)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 3119 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 3120 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 3121 | } |
| 3122 | xref_unlock(&socket->xref, peer); |
PiBa-NL | 706d5ee | 2018-05-05 23:51:42 +0200 | [diff] [blame] | 3123 | |
| 3124 | task_wakeup(s->task, TASK_WOKEN_INIT); |
| 3125 | /* Return yield waiting for connection. */ |
| 3126 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3127 | 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] | 3128 | |
| 3129 | return 0; |
| 3130 | } |
| 3131 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 3132 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3133 | __LJMP static int hlua_socket_connect_ssl(struct lua_State *L) |
| 3134 | { |
| 3135 | struct hlua_socket *socket; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 3136 | struct xref *peer; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 3137 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3138 | |
| 3139 | MAY_LJMP(check_args(L, 3, "connect_ssl")); |
| 3140 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 3141 | |
| 3142 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 3143 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 3144 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 3145 | lua_pushnil(L); |
| 3146 | return 1; |
| 3147 | } |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 3148 | |
Willy Tarreau | 0698c80 | 2022-05-11 14:09:57 +0200 | [diff] [blame] | 3149 | s = appctx_strm(container_of(peer, struct hlua_csk_ctx, xref)->appctx); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 3150 | |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 3151 | s->target = &socket_ssl->obj_type; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 3152 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3153 | return MAY_LJMP(hlua_socket_connect(L)); |
| 3154 | } |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 3155 | #endif |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3156 | |
| 3157 | __LJMP static int hlua_socket_setoption(struct lua_State *L) |
| 3158 | { |
| 3159 | return 0; |
| 3160 | } |
| 3161 | |
| 3162 | __LJMP static int hlua_socket_settimeout(struct lua_State *L) |
| 3163 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3164 | struct hlua_socket *socket; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3165 | int tmout; |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 3166 | double dtmout; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 3167 | struct xref *peer; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 3168 | struct stream *s; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3169 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3170 | MAY_LJMP(check_args(L, 2, "settimeout")); |
| 3171 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3172 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 3173 | |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 3174 | /* convert the timeout to millis */ |
| 3175 | dtmout = MAY_LJMP(luaL_checknumber(L, 2)) * 1000; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3176 | |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 3177 | /* Check for negative values */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 3178 | if (dtmout < 0) |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 3179 | WILL_LJMP(luaL_error(L, "settimeout: cannot set negatives values")); |
| 3180 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 3181 | if (dtmout > INT_MAX) /* overflow check */ |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 3182 | 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] | 3183 | |
| 3184 | tmout = MS_TO_TICKS((int)dtmout); |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 3185 | if (tmout == 0) |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3186 | tmout++; /* very small timeouts are adjusted to a minimum of 1ms */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 3187 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 3188 | /* Check if we run on the same thread than the xreator thread. |
| 3189 | * We cannot access to the socket if the thread is different. |
| 3190 | */ |
| 3191 | if (socket->tid != tid) |
| 3192 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 3193 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 3194 | /* check for connection break. If some data were read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 3195 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 3196 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 3197 | hlua_pusherror(L, "socket: not yet initialised, you can't set timeouts."); |
| 3198 | WILL_LJMP(lua_error(L)); |
| 3199 | return 0; |
| 3200 | } |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 3201 | |
Willy Tarreau | 0698c80 | 2022-05-11 14:09:57 +0200 | [diff] [blame] | 3202 | s = appctx_strm(container_of(peer, struct hlua_csk_ctx, xref)->appctx); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 3203 | |
Cyril Bonté | 7bb6345 | 2018-08-17 23:51:02 +0200 | [diff] [blame] | 3204 | s->sess->fe->timeout.connect = tmout; |
Christopher Faulet | 5aaacfb | 2023-02-15 08:13:33 +0100 | [diff] [blame] | 3205 | s->scf->ioto = tmout; |
| 3206 | s->scb->ioto = tmout; |
Cyril Bonté | 7bb6345 | 2018-08-17 23:51:02 +0200 | [diff] [blame] | 3207 | |
| 3208 | s->task->expire = tick_add_ifset(now_ms, tmout); |
| 3209 | task_queue(s->task); |
| 3210 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 3211 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3212 | |
Thierry Fournier | e9636f1 | 2018-03-08 09:54:32 +0100 | [diff] [blame] | 3213 | lua_pushinteger(L, 1); |
Tim Duesterhus | 119a5f1 | 2018-01-06 19:16:25 +0100 | [diff] [blame] | 3214 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3215 | } |
| 3216 | |
| 3217 | __LJMP static int hlua_socket_new(lua_State *L) |
| 3218 | { |
| 3219 | struct hlua_socket *socket; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 3220 | struct hlua_csk_ctx *ctx; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3221 | struct appctx *appctx; |
| 3222 | |
| 3223 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 3224 | if (!lua_checkstack(L, 3)) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3225 | hlua_pusherror(L, "socket: full stack"); |
| 3226 | goto out_fail_conf; |
| 3227 | } |
| 3228 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 3229 | /* Create the object: obj[0] = userdata. */ |
| 3230 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3231 | socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket))); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 3232 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3233 | memset(socket, 0, sizeof(*socket)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 3234 | socket->tid = tid; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3235 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3236 | /* Check if the various memory pools are initialized. */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 3237 | if (!pool_head_stream || !pool_head_buffer) { |
Thierry FOURNIER | 4a6170c | 2015-03-09 17:07:10 +0100 | [diff] [blame] | 3238 | hlua_pusherror(L, "socket: uninitialized pools."); |
| 3239 | goto out_fail_conf; |
| 3240 | } |
| 3241 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3242 | /* Pop a class stream metatable and affect it to the userdata. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3243 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref); |
| 3244 | lua_setmetatable(L, -2); |
| 3245 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 3246 | /* Create the applet context */ |
Christopher Faulet | 6095d57 | 2022-05-16 17:09:48 +0200 | [diff] [blame] | 3247 | appctx = appctx_new_here(&update_applet, NULL); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 3248 | if (!appctx) { |
| 3249 | hlua_pusherror(L, "socket: out of memory"); |
Christopher Faulet | a9e8b39 | 2022-03-23 11:01:09 +0100 | [diff] [blame] | 3250 | goto out_fail_conf; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 3251 | } |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 3252 | ctx = applet_reserve_svcctx(appctx, sizeof(*ctx)); |
| 3253 | ctx->connected = 0; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 3254 | ctx->die = 0; |
| 3255 | LIST_INIT(&ctx->wake_on_write); |
| 3256 | LIST_INIT(&ctx->wake_on_read); |
Willy Tarreau | b2bf833 | 2015-04-04 15:58:58 +0200 | [diff] [blame] | 3257 | |
Christopher Faulet | 5f35a3e | 2022-05-12 15:31:05 +0200 | [diff] [blame] | 3258 | if (appctx_init(appctx) == -1) { |
| 3259 | hlua_pusherror(L, "socket: fail to init applet."); |
Christopher Faulet | 13a35e5 | 2021-12-20 15:34:16 +0100 | [diff] [blame] | 3260 | goto out_fail_appctx; |
| 3261 | } |
| 3262 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 3263 | /* Initialise cross reference between stream and Lua socket object. */ |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 3264 | xref_create(&socket->xref, &ctx->xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3265 | return 1; |
| 3266 | |
Christopher Faulet | 13a35e5 | 2021-12-20 15:34:16 +0100 | [diff] [blame] | 3267 | out_fail_appctx: |
Christopher Faulet | 5f35a3e | 2022-05-12 15:31:05 +0200 | [diff] [blame] | 3268 | appctx_free_on_early_error(appctx); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 3269 | out_fail_conf: |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3270 | WILL_LJMP(lua_error(L)); |
| 3271 | return 0; |
| 3272 | } |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 3273 | |
| 3274 | /* |
| 3275 | * |
| 3276 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3277 | * Class Channel |
| 3278 | * |
| 3279 | * |
| 3280 | */ |
| 3281 | |
| 3282 | /* Returns the struct hlua_channel join to the class channel in the |
| 3283 | * stack entry "ud" or throws an argument error. |
| 3284 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3285 | __LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3286 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3287 | return MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3288 | } |
| 3289 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3290 | /* Pushes the channel onto the top of the stack. If the stask does not have a |
| 3291 | * free slots, the function fails and returns 0; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3292 | */ |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 3293 | static int hlua_channel_new(lua_State *L, struct channel *channel) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3294 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3295 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 3296 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3297 | return 0; |
| 3298 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 3299 | lua_newtable(L); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3300 | lua_pushlightuserdata(L, channel); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 3301 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3302 | |
| 3303 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 3304 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref); |
| 3305 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3306 | return 1; |
| 3307 | } |
| 3308 | |
Christopher Faulet | 9f55a50 | 2020-02-25 15:21:02 +0100 | [diff] [blame] | 3309 | /* Helper function returning a filter attached to a channel at the position <ud> |
| 3310 | * in the stack, filling the current offset and length of the filter. If no |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 3311 | * filter is attached, NULL is returned and <offset> and <len> are not |
Christopher Faulet | 9f55a50 | 2020-02-25 15:21:02 +0100 | [diff] [blame] | 3312 | * initialized. |
| 3313 | */ |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3314 | static struct filter *hlua_channel_filter(lua_State *L, int ud, struct channel *chn, size_t *offset, size_t *len) |
Christopher Faulet | 9f55a50 | 2020-02-25 15:21:02 +0100 | [diff] [blame] | 3315 | { |
| 3316 | struct filter *filter = NULL; |
| 3317 | |
| 3318 | if (lua_getfield(L, ud, "__filter") == LUA_TLIGHTUSERDATA) { |
| 3319 | struct hlua_flt_ctx *flt_ctx; |
| 3320 | |
| 3321 | filter = lua_touserdata (L, -1); |
| 3322 | flt_ctx = filter->ctx; |
| 3323 | if (hlua_filter_from_payload(filter)) { |
| 3324 | *offset = flt_ctx->cur_off[CHN_IDX(chn)]; |
| 3325 | *len = flt_ctx->cur_len[CHN_IDX(chn)]; |
| 3326 | } |
| 3327 | } |
| 3328 | |
| 3329 | lua_pop(L, 1); |
| 3330 | return filter; |
| 3331 | } |
| 3332 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3333 | /* Copies <len> bytes of data present in the channel's buffer, starting at the |
| 3334 | * offset <offset>, and put it in a LUA string variable. It is the caller |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3335 | * responsibility to ensure <len> and <offset> are valid. It always return the |
| 3336 | * length of the built string. <len> may be 0, in this case, an empty string is |
| 3337 | * created and 0 is returned. |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3338 | */ |
| 3339 | static inline int _hlua_channel_dup(struct channel *chn, lua_State *L, size_t offset, size_t len) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3340 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3341 | size_t block1, block2; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3342 | luaL_Buffer b; |
| 3343 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3344 | block1 = len; |
| 3345 | if (block1 > b_contig_data(&chn->buf, b_peek_ofs(&chn->buf, offset))) |
| 3346 | block1 = b_contig_data(&chn->buf, b_peek_ofs(&chn->buf, offset)); |
| 3347 | block2 = len - block1; |
| 3348 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3349 | luaL_buffinit(L, &b); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3350 | luaL_addlstring(&b, b_peek(&chn->buf, offset), block1); |
| 3351 | if (block2) |
| 3352 | luaL_addlstring(&b, b_orig(&chn->buf), block2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3353 | luaL_pushresult(&b); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3354 | return len; |
| 3355 | } |
| 3356 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3357 | /* Inserts the string <str> to the channel's buffer at the offset <offset>. This |
| 3358 | * function returns -1 if data cannot be copied. Otherwise, it returns the |
| 3359 | * number of bytes copied. |
| 3360 | */ |
| 3361 | static int _hlua_channel_insert(struct channel *chn, lua_State *L, struct ist str, size_t offset) |
| 3362 | { |
| 3363 | int ret = 0; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3364 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3365 | /* Nothing to do, just return */ |
| 3366 | if (unlikely(istlen(str) == 0)) |
| 3367 | goto end; |
| 3368 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3369 | if (istlen(str) > c_room(chn)) { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3370 | ret = -1; |
| 3371 | goto end; |
| 3372 | } |
| 3373 | ret = b_insert_blk(&chn->buf, offset, istptr(str), istlen(str)); |
| 3374 | |
| 3375 | end: |
| 3376 | return ret; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3377 | } |
| 3378 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3379 | /* Removes <len> bytes of data at the absolute position <offset>. |
| 3380 | */ |
| 3381 | static void _hlua_channel_delete(struct channel *chn, size_t offset, size_t len) |
| 3382 | { |
| 3383 | size_t end = offset + len; |
| 3384 | |
| 3385 | if (b_peek(&chn->buf, end) != b_tail(&chn->buf)) |
| 3386 | b_move(&chn->buf, b_peek_ofs(&chn->buf, end), |
| 3387 | b_data(&chn->buf) - end, -len); |
| 3388 | b_sub(&chn->buf, len); |
| 3389 | } |
| 3390 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3391 | /* Copies input data in the channel's buffer. It is possible to set a specific |
| 3392 | * offset (0 by default) and a length (all remaining input data starting for the |
| 3393 | * offset by default). If there is not enough input data and more data can be |
| 3394 | * received, this function yields. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3395 | * |
| 3396 | * From an action, All input data are considered. For a filter, the offset and |
| 3397 | * the length of input data to consider are retrieved from the filter context. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3398 | */ |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3399 | __LJMP static int hlua_channel_get_data_yield(lua_State *L, int status, lua_KContext ctx) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3400 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3401 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3402 | struct filter *filter; |
| 3403 | size_t input, output; |
| 3404 | int offset, len; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3405 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3406 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3407 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3408 | output = co_data(chn); |
| 3409 | input = ci_data(chn); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3410 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3411 | filter = hlua_channel_filter(L, 1, chn, &output, &input); |
| 3412 | if (filter && !hlua_filter_from_payload(filter)) |
| 3413 | WILL_LJMP(lua_error(L)); |
| 3414 | |
| 3415 | offset = output; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3416 | if (lua_gettop(L) > 1) { |
| 3417 | offset = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3418 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 3419 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3420 | offset += output; |
| 3421 | if (offset < output || offset > input + output) { |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3422 | lua_pushfstring(L, "offset out of range."); |
| 3423 | WILL_LJMP(lua_error(L)); |
| 3424 | } |
| 3425 | } |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3426 | len = output + input - offset; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3427 | if (lua_gettop(L) == 3) { |
| 3428 | len = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3429 | if (!len) |
| 3430 | goto dup; |
| 3431 | if (len == -1) |
| 3432 | len = global.tune.bufsize; |
| 3433 | if (len < 0) { |
| 3434 | lua_pushfstring(L, "length out of range."); |
| 3435 | WILL_LJMP(lua_error(L)); |
| 3436 | } |
| 3437 | } |
| 3438 | |
Christopher Faulet | 0ae2e63 | 2023-01-10 15:29:54 +0100 | [diff] [blame] | 3439 | /* Wait for more data if possible if no length was specified and there |
| 3440 | * is no data or not enough data was received. |
| 3441 | */ |
| 3442 | if (!len || offset + len > output + input) { |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3443 | if (!HLUA_CANT_YIELD(hlua_gethlua(L)) && !channel_input_closed(chn) && channel_may_recv(chn)) { |
| 3444 | /* Yield waiting for more data, as requested */ |
| 3445 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_get_data_yield, TICK_ETERNITY, 0)); |
| 3446 | } |
Christopher Faulet | 0ae2e63 | 2023-01-10 15:29:54 +0100 | [diff] [blame] | 3447 | |
| 3448 | /* Return 'nil' if there is no data and the channel can't receive more data */ |
| 3449 | if (!len) { |
| 3450 | lua_pushnil(L); |
| 3451 | return -1; |
| 3452 | } |
| 3453 | |
| 3454 | /* Otherwise, return all data */ |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3455 | len = output + input - offset; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3456 | } |
| 3457 | |
| 3458 | dup: |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3459 | _hlua_channel_dup(chn, L, offset, len); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3460 | return 1; |
| 3461 | } |
| 3462 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3463 | /* Copies the first line (including the trailing LF) of input data in the |
| 3464 | * channel's buffer. It is possible to set a specific offset (0 by default) and |
| 3465 | * a length (all remaining input data starting for the offset by default). If |
| 3466 | * there is not enough input data and more data can be received, the function |
| 3467 | * yields. If a length is explicitly specified, no more data are |
| 3468 | * copied. Otherwise, if no LF is found and more data can be received, this |
| 3469 | * function yields. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3470 | * |
| 3471 | * From an action, All input data are considered. For a filter, the offset and |
| 3472 | * the length of input data to consider are retrieved from the filter context. |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3473 | */ |
| 3474 | __LJMP static int hlua_channel_get_line_yield(lua_State *L, int status, lua_KContext ctx) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3475 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3476 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3477 | struct filter *filter; |
| 3478 | size_t l, input, output; |
| 3479 | int offset, len; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3480 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3481 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3482 | output = co_data(chn); |
| 3483 | input = ci_data(chn); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3484 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3485 | filter = hlua_channel_filter(L, 1, chn, &output, &input); |
| 3486 | if (filter && !hlua_filter_from_payload(filter)) |
| 3487 | WILL_LJMP(lua_error(L)); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3488 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3489 | offset = output; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3490 | if (lua_gettop(L) > 1) { |
| 3491 | offset = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3492 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 3493 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3494 | offset += output; |
| 3495 | if (offset < output || offset > input + output) { |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3496 | lua_pushfstring(L, "offset out of range."); |
| 3497 | WILL_LJMP(lua_error(L)); |
| 3498 | } |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3499 | } |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3500 | |
| 3501 | len = output + input - offset; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3502 | if (lua_gettop(L) == 3) { |
| 3503 | len = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3504 | if (!len) |
| 3505 | goto dup; |
| 3506 | if (len == -1) |
| 3507 | len = global.tune.bufsize; |
| 3508 | if (len < 0) { |
| 3509 | lua_pushfstring(L, "length out of range."); |
| 3510 | WILL_LJMP(lua_error(L)); |
| 3511 | } |
| 3512 | } |
| 3513 | |
| 3514 | for (l = 0; l < len; l++) { |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3515 | if (l + offset >= output + input) |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3516 | break; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3517 | if (*(b_peek(&chn->buf, offset + l)) == '\n') { |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3518 | len = l+1; |
| 3519 | goto dup; |
| 3520 | } |
| 3521 | } |
| 3522 | |
Christopher Faulet | 0ae2e63 | 2023-01-10 15:29:54 +0100 | [diff] [blame] | 3523 | /* Wait for more data if possible if no line is found and no length was |
| 3524 | * specified or not enough data was received. |
| 3525 | */ |
| 3526 | if (lua_gettop(L) != 3 || offset + len > output + input) { |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3527 | if (!HLUA_CANT_YIELD(hlua_gethlua(L)) && !channel_input_closed(chn) && channel_may_recv(chn)) { |
| 3528 | /* Yield waiting for more data */ |
| 3529 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_get_line_yield, TICK_ETERNITY, 0)); |
| 3530 | } |
Christopher Faulet | 0ae2e63 | 2023-01-10 15:29:54 +0100 | [diff] [blame] | 3531 | |
| 3532 | /* Return 'nil' if there is no data and the channel can't receive more data */ |
| 3533 | if (!len) { |
| 3534 | lua_pushnil(L); |
| 3535 | return -1; |
| 3536 | } |
| 3537 | |
| 3538 | /* Otherwise, return all data */ |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3539 | len = output + input - offset; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3540 | } |
| 3541 | |
| 3542 | dup: |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3543 | _hlua_channel_dup(chn, L, offset, len); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3544 | return 1; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3545 | } |
| 3546 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3547 | /* [ DEPRECATED ] |
| 3548 | * |
| 3549 | * Duplicate all input data foud in the channel's buffer. The data are not |
| 3550 | * removed from the buffer. This function relies on _hlua_channel_dup(). |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3551 | * |
| 3552 | * From an action, All input data are considered. For a filter, the offset and |
| 3553 | * the length of input data to consider are retrieved from the filter context. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3554 | */ |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3555 | __LJMP static int hlua_channel_dup(lua_State *L) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3556 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3557 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3558 | struct filter *filter; |
| 3559 | size_t offset, len; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3560 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3561 | MAY_LJMP(check_args(L, 1, "dup")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3562 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3563 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3564 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3565 | WILL_LJMP(lua_error(L)); |
| 3566 | } |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3567 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3568 | offset = co_data(chn); |
| 3569 | len = ci_data(chn); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3570 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3571 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 3572 | if (filter && !hlua_filter_from_payload(filter)) |
| 3573 | WILL_LJMP(lua_error(L)); |
| 3574 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3575 | if (!ci_data(chn) && channel_input_closed(chn)) { |
| 3576 | lua_pushnil(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3577 | return 1; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3578 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3579 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3580 | _hlua_channel_dup(chn, L, offset, len); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3581 | return 1; |
| 3582 | } |
| 3583 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3584 | /* [ DEPRECATED ] |
| 3585 | * |
| 3586 | * Get all input data foud in the channel's buffer. The data are removed from |
| 3587 | * the buffer after the copy. This function relies on _hlua_channel_dup() and |
| 3588 | * _hlua_channel_delete(). |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3589 | * |
| 3590 | * From an action, All input data are considered. For a filter, the offset and |
| 3591 | * the length of input data to consider are retrieved from the filter context. |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3592 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3593 | __LJMP static int hlua_channel_get(lua_State *L) |
| 3594 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3595 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3596 | struct filter *filter; |
| 3597 | size_t offset, len; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3598 | int ret; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3599 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3600 | MAY_LJMP(check_args(L, 1, "get")); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3601 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3602 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3603 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3604 | WILL_LJMP(lua_error(L)); |
| 3605 | } |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3606 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3607 | offset = co_data(chn); |
| 3608 | len = ci_data(chn); |
| 3609 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3610 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 3611 | if (filter && !hlua_filter_from_payload(filter)) |
| 3612 | WILL_LJMP(lua_error(L)); |
| 3613 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3614 | if (!ci_data(chn) && channel_input_closed(chn)) { |
| 3615 | lua_pushnil(L); |
| 3616 | return 1; |
| 3617 | } |
| 3618 | |
| 3619 | ret = _hlua_channel_dup(chn, L, offset, len); |
| 3620 | _hlua_channel_delete(chn, offset, ret); |
| 3621 | return 1; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3622 | } |
| 3623 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3624 | /* This functions consumes and returns one line. If the channel is closed, |
| 3625 | * 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] | 3626 | * 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] | 3627 | * value. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3628 | * |
| 3629 | * From an action, All input data are considered. For a filter, the offset and |
| 3630 | * the length of input data to consider are retrieved from the filter context. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3631 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3632 | __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] | 3633 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3634 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3635 | struct filter *filter; |
| 3636 | size_t l, offset, len; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3637 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3638 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3639 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3640 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3641 | offset = co_data(chn); |
| 3642 | len = ci_data(chn); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3643 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3644 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 3645 | if (filter && !hlua_filter_from_payload(filter)) |
| 3646 | WILL_LJMP(lua_error(L)); |
| 3647 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3648 | if (!ci_data(chn) && channel_input_closed(chn)) { |
| 3649 | lua_pushnil(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3650 | return 1; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3651 | } |
| 3652 | |
| 3653 | for (l = 0; l < len; l++) { |
| 3654 | if (*(b_peek(&chn->buf, offset+l)) == '\n') { |
| 3655 | len = l+1; |
| 3656 | goto dup; |
| 3657 | } |
| 3658 | } |
| 3659 | |
| 3660 | if (!HLUA_CANT_YIELD(hlua_gethlua(L)) && !channel_input_closed(chn) && channel_may_recv(chn)) { |
| 3661 | /* Yield waiting for more data */ |
| 3662 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_getline_yield, TICK_ETERNITY, 0)); |
| 3663 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3664 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3665 | dup: |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3666 | ret = _hlua_channel_dup(chn, L, offset, len); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3667 | _hlua_channel_delete(chn, offset, ret); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3668 | return 1; |
| 3669 | } |
| 3670 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3671 | /* [ DEPRECATED ] |
| 3672 | * |
| 3673 | * Check arguments for the function "hlua_channel_getline_yield". |
| 3674 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3675 | __LJMP static int hlua_channel_getline(lua_State *L) |
| 3676 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3677 | struct channel *chn; |
| 3678 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3679 | MAY_LJMP(check_args(L, 1, "getline")); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3680 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3681 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3682 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3683 | WILL_LJMP(lua_error(L)); |
| 3684 | } |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3685 | return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0)); |
| 3686 | } |
| 3687 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3688 | /* Retrieves a given amount of input data at the given offset. By default all |
| 3689 | * available input data are returned. The offset may be negactive to start from |
| 3690 | * the end of input data. The length may be -1 to set it to the maximum buffer |
| 3691 | * size. |
| 3692 | */ |
| 3693 | __LJMP static int hlua_channel_get_data(lua_State *L) |
| 3694 | { |
| 3695 | struct channel *chn; |
| 3696 | |
| 3697 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 3698 | WILL_LJMP(luaL_error(L, "'data' expects at most 2 arguments")); |
| 3699 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3700 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3701 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3702 | WILL_LJMP(lua_error(L)); |
| 3703 | } |
| 3704 | return MAY_LJMP(hlua_channel_get_data_yield(L, 0, 0)); |
| 3705 | } |
| 3706 | |
| 3707 | /* Retrieves a given amount of input data at the given offset. By default all |
| 3708 | * available input data are returned. The offset may be negactive to start from |
| 3709 | * the end of input data. The length may be -1 to set it to the maximum buffer |
| 3710 | * size. |
| 3711 | */ |
| 3712 | __LJMP static int hlua_channel_get_line(lua_State *L) |
| 3713 | { |
| 3714 | struct channel *chn; |
| 3715 | |
| 3716 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 3717 | WILL_LJMP(luaL_error(L, "'line' expects at most 2 arguments")); |
| 3718 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3719 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3720 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3721 | WILL_LJMP(lua_error(L)); |
| 3722 | } |
| 3723 | return MAY_LJMP(hlua_channel_get_line_yield(L, 0, 0)); |
| 3724 | } |
| 3725 | |
| 3726 | /* Appends a string into the input side of channel. It returns the length of the |
| 3727 | * written string, or -1 if the channel is closed or if the buffer size is too |
| 3728 | * little for the data. 0 may be returned if nothing is copied. This function |
| 3729 | * does not yield. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3730 | * |
| 3731 | * For a filter, the context is updated on success. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3732 | */ |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3733 | __LJMP static int hlua_channel_append(lua_State *L) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3734 | { |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3735 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3736 | struct filter *filter; |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3737 | const char *str; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3738 | size_t sz, offset, len; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3739 | int ret; |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3740 | |
| 3741 | MAY_LJMP(check_args(L, 2, "append")); |
| 3742 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3743 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 3744 | if (IS_HTX_STRM(chn_strm(chn))) { |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3745 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3746 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3747 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3748 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3749 | offset = co_data(chn); |
| 3750 | len = ci_data(chn); |
| 3751 | |
| 3752 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 3753 | if (filter && !hlua_filter_from_payload(filter)) |
| 3754 | WILL_LJMP(lua_error(L)); |
| 3755 | |
| 3756 | ret = _hlua_channel_insert(chn, L, ist2(str, sz), offset); |
| 3757 | if (ret > 0 && filter) { |
| 3758 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 3759 | |
| 3760 | flt_update_offsets(filter, chn, ret); |
| 3761 | flt_ctx->cur_len[CHN_IDX(chn)] += ret; |
| 3762 | } |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3763 | lua_pushinteger(L, ret); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3764 | return 1; |
| 3765 | } |
| 3766 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3767 | /* Prepends a string into the input side of channel. It returns the length of the |
| 3768 | * written string, or -1 if the channel is closed or if the buffer size is too |
| 3769 | * little for the data. 0 may be returned if nothing is copied. This function |
| 3770 | * does not yield. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3771 | * |
| 3772 | * For a filter, the context is updated on success. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3773 | */ |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3774 | __LJMP static int hlua_channel_prepend(lua_State *L) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3775 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3776 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3777 | struct filter *filter; |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3778 | const char *str; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3779 | size_t sz, offset, len; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3780 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3781 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3782 | MAY_LJMP(check_args(L, 2, "prepend")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3783 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3784 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 3785 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3786 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3787 | WILL_LJMP(lua_error(L)); |
| 3788 | } |
| 3789 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3790 | offset = co_data(chn); |
| 3791 | len = ci_data(chn); |
| 3792 | |
| 3793 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 3794 | if (filter && !hlua_filter_from_payload(filter)) |
| 3795 | WILL_LJMP(lua_error(L)); |
| 3796 | |
| 3797 | ret = _hlua_channel_insert(chn, L, ist2(str, sz), offset); |
| 3798 | if (ret > 0 && filter) { |
| 3799 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 3800 | |
| 3801 | flt_update_offsets(filter, chn, ret); |
| 3802 | flt_ctx->cur_len[CHN_IDX(chn)] += ret; |
| 3803 | } |
| 3804 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3805 | lua_pushinteger(L, ret); |
| 3806 | return 1; |
| 3807 | } |
| 3808 | |
| 3809 | /* Inserts a given amount of input data at the given offset by a string |
Aurelien DARRAGON | afb7daf | 2022-10-04 12:16:05 +0200 | [diff] [blame] | 3810 | * content. By default the string is appended in front of input data. It |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3811 | * returns the length of the written string, or -1 if the channel is closed or |
| 3812 | * if the buffer size is too little for the data. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3813 | * |
| 3814 | * For a filter, the context is updated on success. |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3815 | */ |
| 3816 | __LJMP static int hlua_channel_insert_data(lua_State *L) |
| 3817 | { |
| 3818 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3819 | struct filter *filter; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3820 | const char *str; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3821 | size_t sz, input, output; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3822 | int ret, offset; |
| 3823 | |
| 3824 | if (lua_gettop(L) < 2 || lua_gettop(L) > 3) |
| 3825 | WILL_LJMP(luaL_error(L, "'insert' expects at least 1 argument and at most 2 arguments")); |
| 3826 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3827 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3828 | |
| 3829 | output = co_data(chn); |
| 3830 | input = ci_data(chn); |
| 3831 | |
| 3832 | filter = hlua_channel_filter(L, 1, chn, &output, &input); |
| 3833 | if (filter && !hlua_filter_from_payload(filter)) |
| 3834 | WILL_LJMP(lua_error(L)); |
| 3835 | |
Aurelien DARRAGON | afb7daf | 2022-10-04 12:16:05 +0200 | [diff] [blame] | 3836 | offset = output; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3837 | if (lua_gettop(L) > 2) { |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3838 | offset = MAY_LJMP(luaL_checkinteger(L, 3)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3839 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 3840 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3841 | offset += output; |
Aurelien DARRAGON | afb7daf | 2022-10-04 12:16:05 +0200 | [diff] [blame] | 3842 | if (offset > output + input) { |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3843 | lua_pushfstring(L, "offset out of range."); |
| 3844 | WILL_LJMP(lua_error(L)); |
| 3845 | } |
| 3846 | } |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3847 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3848 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3849 | WILL_LJMP(lua_error(L)); |
| 3850 | } |
| 3851 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3852 | ret = _hlua_channel_insert(chn, L, ist2(str, sz), offset); |
| 3853 | if (ret > 0 && filter) { |
| 3854 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3855 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3856 | flt_update_offsets(filter, chn, ret); |
| 3857 | flt_ctx->cur_len[CHN_IDX(chn)] += ret; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3858 | } |
| 3859 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3860 | lua_pushinteger(L, ret); |
| 3861 | return 1; |
| 3862 | } |
| 3863 | /* Replaces a given amount of input data at the given offset by a string |
| 3864 | * content. By default all remaining data are removed (offset = 0 and len = |
| 3865 | * -1). It returns the length of the written string, or -1 if the channel is |
| 3866 | * closed or if the buffer size is too little for the data. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3867 | * |
| 3868 | * For a filter, the context is updated on success. |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3869 | */ |
| 3870 | __LJMP static int hlua_channel_set_data(lua_State *L) |
| 3871 | { |
| 3872 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3873 | struct filter *filter; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3874 | const char *str; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3875 | size_t sz, input, output; |
| 3876 | int ret, offset, len; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3877 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3878 | if (lua_gettop(L) < 2 || lua_gettop(L) > 4) |
| 3879 | WILL_LJMP(luaL_error(L, "'set' expects at least 1 argument and at most 3 arguments")); |
| 3880 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3881 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3882 | |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 3883 | if (IS_HTX_STRM(chn_strm(chn))) { |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3884 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3885 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3886 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3887 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3888 | output = co_data(chn); |
| 3889 | input = ci_data(chn); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3890 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3891 | filter = hlua_channel_filter(L, 1, chn, &output, &input); |
| 3892 | if (filter && !hlua_filter_from_payload(filter)) |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3893 | WILL_LJMP(lua_error(L)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3894 | |
| 3895 | offset = output; |
| 3896 | if (lua_gettop(L) > 2) { |
| 3897 | offset = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3898 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 3899 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3900 | offset += output; |
| 3901 | if (offset < output || offset > input + output) { |
| 3902 | lua_pushfstring(L, "offset out of range."); |
| 3903 | WILL_LJMP(lua_error(L)); |
| 3904 | } |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3905 | } |
| 3906 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3907 | len = output + input - offset; |
| 3908 | if (lua_gettop(L) == 4) { |
| 3909 | len = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 3910 | if (!len) |
| 3911 | goto set; |
| 3912 | if (len == -1) |
| 3913 | len = output + input - offset; |
| 3914 | if (len < 0 || offset + len > output + input) { |
| 3915 | lua_pushfstring(L, "length out of range."); |
| 3916 | WILL_LJMP(lua_error(L)); |
| 3917 | } |
| 3918 | } |
| 3919 | |
| 3920 | set: |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3921 | /* Be sure we can copied the string once input data will be removed. */ |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3922 | if (sz > c_room(chn) + len) |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3923 | lua_pushinteger(L, -1); |
| 3924 | else { |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3925 | _hlua_channel_delete(chn, offset, len); |
| 3926 | ret = _hlua_channel_insert(chn, L, ist2(str, sz), offset); |
| 3927 | if (filter) { |
| 3928 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 3929 | |
| 3930 | len -= (ret > 0 ? ret : 0); |
| 3931 | flt_update_offsets(filter, chn, -len); |
| 3932 | flt_ctx->cur_len[CHN_IDX(chn)] -= len; |
| 3933 | } |
| 3934 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3935 | lua_pushinteger(L, ret); |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3936 | } |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3937 | return 1; |
| 3938 | } |
| 3939 | |
| 3940 | /* Removes a given amount of input data at the given offset. By default all |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3941 | * input data are removed (offset = 0 and len = -1). It returns the amount of |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3942 | * the removed data. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3943 | * |
| 3944 | * For a filter, the context is updated on success. |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3945 | */ |
| 3946 | __LJMP static int hlua_channel_del_data(lua_State *L) |
| 3947 | { |
| 3948 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3949 | struct filter *filter; |
| 3950 | size_t input, output; |
| 3951 | int offset, len; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3952 | |
| 3953 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 3954 | WILL_LJMP(luaL_error(L, "'remove' expects at most 2 arguments")); |
| 3955 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3956 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3957 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3958 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3959 | WILL_LJMP(lua_error(L)); |
| 3960 | } |
| 3961 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3962 | output = co_data(chn); |
| 3963 | input = ci_data(chn); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3964 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3965 | filter = hlua_channel_filter(L, 1, chn, &output, &input); |
| 3966 | if (filter && !hlua_filter_from_payload(filter)) |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3967 | WILL_LJMP(lua_error(L)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3968 | |
| 3969 | offset = output; |
Boyang Li | e0c5435 | 2022-05-10 17:47:23 +0000 | [diff] [blame] | 3970 | if (lua_gettop(L) > 1) { |
| 3971 | offset = MAY_LJMP(luaL_checkinteger(L, 2)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3972 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 3973 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3974 | offset += output; |
| 3975 | if (offset < output || offset > input + output) { |
| 3976 | lua_pushfstring(L, "offset out of range."); |
| 3977 | WILL_LJMP(lua_error(L)); |
| 3978 | } |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3979 | } |
| 3980 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3981 | len = output + input - offset; |
Boyang Li | e0c5435 | 2022-05-10 17:47:23 +0000 | [diff] [blame] | 3982 | if (lua_gettop(L) == 3) { |
| 3983 | len = MAY_LJMP(luaL_checkinteger(L, 3)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3984 | if (!len) |
| 3985 | goto end; |
| 3986 | if (len == -1) |
| 3987 | len = output + input - offset; |
| 3988 | if (len < 0 || offset + len > output + input) { |
| 3989 | lua_pushfstring(L, "length out of range."); |
| 3990 | WILL_LJMP(lua_error(L)); |
| 3991 | } |
| 3992 | } |
| 3993 | |
| 3994 | _hlua_channel_delete(chn, offset, len); |
| 3995 | if (filter) { |
| 3996 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 3997 | |
| 3998 | flt_update_offsets(filter, chn, -len); |
| 3999 | flt_ctx->cur_len[CHN_IDX(chn)] -= len; |
| 4000 | } |
| 4001 | |
| 4002 | end: |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 4003 | lua_pushinteger(L, len); |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 4004 | return 1; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4005 | } |
| 4006 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4007 | /* Append data in the output side of the buffer. This data is immediately |
| 4008 | * sent. The function returns the amount of data written. If the buffer |
| 4009 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4010 | * if the channel is closed. |
| 4011 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 4012 | __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] | 4013 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 4014 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4015 | struct filter *filter; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 4016 | const char *str; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4017 | size_t offset, len, sz; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 4018 | int l, ret; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 4019 | struct hlua *hlua; |
| 4020 | |
| 4021 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 4022 | hlua = hlua_gethlua(L); |
| 4023 | if (!hlua) { |
| 4024 | lua_pushnil(L); |
| 4025 | return 1; |
| 4026 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4027 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 4028 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 4029 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 4030 | l = MAY_LJMP(luaL_checkinteger(L, 3)); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 4031 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4032 | offset = co_data(chn); |
| 4033 | len = ci_data(chn); |
| 4034 | |
| 4035 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 4036 | if (filter && !hlua_filter_from_payload(filter)) |
| 4037 | WILL_LJMP(lua_error(L)); |
| 4038 | |
| 4039 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 4040 | if (unlikely(channel_output_closed(chn))) { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4041 | lua_pushinteger(L, -1); |
| 4042 | return 1; |
| 4043 | } |
| 4044 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 4045 | len = c_room(chn); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4046 | if (len > sz -l) { |
| 4047 | if (filter) { |
| 4048 | lua_pushinteger(L, -1); |
| 4049 | return 1; |
| 4050 | } |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 4051 | len = sz - l; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4052 | } |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 4053 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4054 | ret = _hlua_channel_insert(chn, L, ist2(str, len), offset); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 4055 | if (ret == -1) { |
| 4056 | lua_pop(L, 1); |
| 4057 | lua_pushinteger(L, -1); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 4058 | return 1; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 4059 | } |
| 4060 | if (ret) { |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4061 | if (filter) { |
| 4062 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 4063 | |
| 4064 | |
| 4065 | flt_update_offsets(filter, chn, ret); |
| 4066 | FLT_OFF(filter, chn) += ret; |
| 4067 | flt_ctx->cur_off[CHN_IDX(chn)] += ret; |
| 4068 | } |
| 4069 | else |
| 4070 | c_adv(chn, ret); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4071 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 4072 | l += ret; |
| 4073 | lua_pop(L, 1); |
| 4074 | lua_pushinteger(L, l); |
| 4075 | } |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4076 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 4077 | if (l < sz) { |
| 4078 | /* Yield only if the channel's output is not empty. |
| 4079 | * Otherwise it means we cannot add more data. */ |
| 4080 | if (co_data(chn) == 0 || HLUA_CANT_YIELD(hlua_gethlua(L))) |
Christopher Faulet | 2e60aa4 | 2021-08-05 11:58:37 +0200 | [diff] [blame] | 4081 | return 1; |
| 4082 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 4083 | /* If we are waiting for space in the response buffer, we |
| 4084 | * must set the flag WAKERESWR. This flag required the task |
| 4085 | * wake up if any activity is detected on the response buffer. |
| 4086 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 4087 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 4088 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 4089 | else |
| 4090 | HLUA_SET_WAKEREQWR(hlua); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4091 | 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] | 4092 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4093 | |
| 4094 | return 1; |
| 4095 | } |
| 4096 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4097 | /* Just a wrapper of "_hlua_channel_send". This wrapper permits |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4098 | * yield the LUA process, and resume it without checking the |
| 4099 | * input arguments. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4100 | * |
| 4101 | * This function cannot be called from a filter. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4102 | */ |
| 4103 | __LJMP static int hlua_channel_send(lua_State *L) |
| 4104 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 4105 | struct channel *chn; |
| 4106 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4107 | MAY_LJMP(check_args(L, 2, "send")); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 4108 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 4109 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 4110 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 4111 | WILL_LJMP(lua_error(L)); |
| 4112 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4113 | lua_pushinteger(L, 0); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 4114 | return MAY_LJMP(hlua_channel_send_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4115 | } |
| 4116 | |
| 4117 | /* This function forward and amount of butes. The data pass from |
| 4118 | * the input side of the buffer to the output side, and can be |
| 4119 | * forwarded. This function never fails. |
| 4120 | * |
| 4121 | * The Lua function takes an amount of bytes to be forwarded in |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4122 | * input. It returns the number of bytes forwarded. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4123 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 4124 | __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] | 4125 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 4126 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4127 | struct filter *filter; |
| 4128 | size_t offset, len, fwd; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 4129 | int l, max; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 4130 | struct hlua *hlua; |
| 4131 | |
| 4132 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 4133 | hlua = hlua_gethlua(L); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 4134 | if (!hlua) { |
| 4135 | lua_pushnil(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 4136 | return 1; |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 4137 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 4138 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 4139 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4140 | fwd = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4141 | l = MAY_LJMP(luaL_checkinteger(L, -1)); |
| 4142 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4143 | offset = co_data(chn); |
| 4144 | len = ci_data(chn); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 4145 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4146 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 4147 | if (filter && !hlua_filter_from_payload(filter)) |
| 4148 | WILL_LJMP(lua_error(L)); |
| 4149 | |
| 4150 | max = fwd - l; |
| 4151 | if (max > len) |
| 4152 | max = len; |
| 4153 | |
| 4154 | if (filter) { |
| 4155 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 4156 | |
| 4157 | FLT_OFF(filter, chn) += max; |
| 4158 | flt_ctx->cur_off[CHN_IDX(chn)] += max; |
| 4159 | flt_ctx->cur_len[CHN_IDX(chn)] -= max; |
| 4160 | } |
| 4161 | else |
| 4162 | channel_forward(chn, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4163 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 4164 | l += max; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4165 | lua_pop(L, 1); |
| 4166 | lua_pushinteger(L, l); |
| 4167 | |
| 4168 | /* Check if it miss bytes to forward. */ |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4169 | if (l < fwd) { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4170 | /* The the input channel or the output channel are closed, we |
| 4171 | * must return the amount of data forwarded. |
| 4172 | */ |
Christopher Faulet | 2e60aa4 | 2021-08-05 11:58:37 +0200 | [diff] [blame] | 4173 | 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] | 4174 | return 1; |
| 4175 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 4176 | /* If we are waiting for space data in the response buffer, we |
| 4177 | * must set the flag WAKERESWR. This flag required the task |
| 4178 | * wake up if any activity is detected on the response buffer. |
| 4179 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 4180 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 4181 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 4182 | else |
| 4183 | HLUA_SET_WAKEREQWR(hlua); |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 4184 | |
Ilya Shipitsin | 4a689da | 2022-10-29 09:34:32 +0500 | [diff] [blame] | 4185 | /* Otherwise, we can yield waiting for new data in the input side. */ |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4186 | 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] | 4187 | } |
| 4188 | |
| 4189 | return 1; |
| 4190 | } |
| 4191 | |
| 4192 | /* Just check the input and prepare the stack for the previous |
| 4193 | * function "hlua_channel_forward_yield" |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4194 | * |
| 4195 | * This function cannot be called from a filter. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4196 | */ |
| 4197 | __LJMP static int hlua_channel_forward(lua_State *L) |
| 4198 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 4199 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4200 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 4201 | MAY_LJMP(check_args(L, 2, "forward")); |
| 4202 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 4203 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 4204 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 4205 | WILL_LJMP(lua_error(L)); |
| 4206 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4207 | lua_pushinteger(L, 0); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 4208 | return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4209 | } |
| 4210 | |
| 4211 | /* Just returns the number of bytes available in the input |
| 4212 | * side of the buffer. This function never fails. |
| 4213 | */ |
| 4214 | __LJMP static int hlua_channel_get_in_len(lua_State *L) |
| 4215 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 4216 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4217 | struct filter *filter; |
| 4218 | size_t output, input; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 4219 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 4220 | MAY_LJMP(check_args(L, 1, "input")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 4221 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4222 | |
| 4223 | output = co_data(chn); |
| 4224 | input = ci_data(chn); |
| 4225 | filter = hlua_channel_filter(L, 1, chn, &output, &input); |
| 4226 | if (filter || !IS_HTX_STRM(chn_strm(chn))) |
| 4227 | lua_pushinteger(L, input); |
| 4228 | else { |
Christopher Faulet | a3ceac1 | 2018-12-14 13:39:09 +0100 | [diff] [blame] | 4229 | struct htx *htx = htxbuf(&chn->buf); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4230 | |
Christopher Faulet | a3ceac1 | 2018-12-14 13:39:09 +0100 | [diff] [blame] | 4231 | lua_pushinteger(L, htx->data - co_data(chn)); |
| 4232 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4233 | return 1; |
| 4234 | } |
| 4235 | |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 4236 | /* Returns true if the channel is full. */ |
| 4237 | __LJMP static int hlua_channel_is_full(lua_State *L) |
| 4238 | { |
| 4239 | struct channel *chn; |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 4240 | |
| 4241 | MAY_LJMP(check_args(L, 1, "is_full")); |
| 4242 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 0ec740e | 2020-02-26 11:59:19 +0100 | [diff] [blame] | 4243 | /* ignore the reserve, we are not on a producer side (ie in an |
| 4244 | * applet). |
| 4245 | */ |
| 4246 | lua_pushboolean(L, channel_full(chn, 0)); |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 4247 | return 1; |
| 4248 | } |
| 4249 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 4250 | /* Returns true if the channel may still receive data. */ |
| 4251 | __LJMP static int hlua_channel_may_recv(lua_State *L) |
| 4252 | { |
| 4253 | struct channel *chn; |
| 4254 | |
| 4255 | MAY_LJMP(check_args(L, 1, "may_recv")); |
| 4256 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 4257 | lua_pushboolean(L, (!channel_input_closed(chn) && channel_may_recv(chn))); |
| 4258 | return 1; |
| 4259 | } |
| 4260 | |
Christopher Faulet | 2ac9ba2 | 2020-02-25 10:15:50 +0100 | [diff] [blame] | 4261 | /* Returns true if the channel is the response channel. */ |
| 4262 | __LJMP static int hlua_channel_is_resp(lua_State *L) |
| 4263 | { |
| 4264 | struct channel *chn; |
| 4265 | |
| 4266 | MAY_LJMP(check_args(L, 1, "is_resp")); |
| 4267 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 4268 | |
| 4269 | lua_pushboolean(L, !!(chn->flags & CF_ISRESP)); |
| 4270 | return 1; |
| 4271 | } |
| 4272 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4273 | /* Just returns the number of bytes available in the output |
| 4274 | * side of the buffer. This function never fails. |
| 4275 | */ |
| 4276 | __LJMP static int hlua_channel_get_out_len(lua_State *L) |
| 4277 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 4278 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4279 | size_t output, input; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 4280 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 4281 | MAY_LJMP(check_args(L, 1, "output")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 4282 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4283 | |
| 4284 | output = co_data(chn); |
| 4285 | input = ci_data(chn); |
| 4286 | hlua_channel_filter(L, 1, chn, &output, &input); |
| 4287 | |
| 4288 | lua_pushinteger(L, output); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4289 | return 1; |
| 4290 | } |
| 4291 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4292 | /* |
| 4293 | * |
| 4294 | * |
| 4295 | * Class Fetches |
| 4296 | * |
| 4297 | * |
| 4298 | */ |
| 4299 | |
| 4300 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4301 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4302 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4303 | __LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud) |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4304 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4305 | return MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4306 | } |
| 4307 | |
| 4308 | /* This function creates and push in the stack a fetch object according |
| 4309 | * with a current TXN. |
| 4310 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4311 | 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] | 4312 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 4313 | struct hlua_smp *hsmp; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4314 | |
| 4315 | /* Check stack size. */ |
| 4316 | if (!lua_checkstack(L, 3)) |
| 4317 | return 0; |
| 4318 | |
| 4319 | /* Create the object: obj[0] = userdata. |
| 4320 | * Note that the base of the Fetches object is the |
| 4321 | * transaction object. |
| 4322 | */ |
| 4323 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 4324 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4325 | lua_rawseti(L, -2, 0); |
| 4326 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 4327 | hsmp->s = txn->s; |
| 4328 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 4329 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4330 | hsmp->flags = flags; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4331 | |
| 4332 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 4333 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref); |
| 4334 | lua_setmetatable(L, -2); |
| 4335 | |
| 4336 | return 1; |
| 4337 | } |
| 4338 | |
| 4339 | /* This function is an LUA binding. It is called with each sample-fetch. |
| 4340 | * It uses closure argument to store the associated sample-fetch. It |
| 4341 | * returns only one argument or throws an error. An error is thrown |
| 4342 | * only if an error is encountered during the argument parsing. If |
| 4343 | * the "sample-fetch" function fails, nil is returned. |
| 4344 | */ |
| 4345 | __LJMP static int hlua_run_sample_fetch(lua_State *L) |
| 4346 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 4347 | struct hlua_smp *hsmp; |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 4348 | struct sample_fetch *f; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 4349 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4350 | int i; |
| 4351 | struct sample smp; |
| 4352 | |
| 4353 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4354 | f = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4355 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4356 | /* Get traditional arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 4357 | hsmp = MAY_LJMP(hlua_checkfetches(L, 1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4358 | |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 4359 | /* Check execution authorization. */ |
| 4360 | if (f->use & SMP_USE_HTTP_ANY && |
| 4361 | !(hsmp->flags & HLUA_F_MAY_USE_HTTP)) { |
| 4362 | lua_pushfstring(L, "the sample-fetch '%s' needs an HTTP parser which " |
| 4363 | "is not available in Lua services", f->kw); |
| 4364 | WILL_LJMP(lua_error(L)); |
| 4365 | } |
| 4366 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4367 | /* Get extra arguments. */ |
| 4368 | for (i = 0; i < lua_gettop(L) - 1; i++) { |
| 4369 | if (i >= ARGM_NBARGS) |
| 4370 | break; |
| 4371 | hlua_lua2arg(L, i + 2, &args[i]); |
| 4372 | } |
| 4373 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4374 | args[i].data.str.area = NULL; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4375 | |
| 4376 | /* Check arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 4377 | 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] | 4378 | |
| 4379 | /* Run the special args checker. */ |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 4380 | if (f->val_args && !f->val_args(args, NULL)) { |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4381 | lua_pushfstring(L, "error in arguments"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4382 | goto error; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4383 | } |
| 4384 | |
| 4385 | /* Initialise the sample. */ |
| 4386 | memset(&smp, 0, sizeof(smp)); |
| 4387 | |
| 4388 | /* Run the sample fetch process. */ |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 4389 | 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] | 4390 | if (!f->process(args, &smp, f->kw, f->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4391 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4392 | lua_pushstring(L, ""); |
| 4393 | else |
| 4394 | lua_pushnil(L); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4395 | goto end; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4396 | } |
| 4397 | |
| 4398 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4399 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4400 | hlua_smp2lua_str(L, &smp); |
| 4401 | else |
| 4402 | hlua_smp2lua(L, &smp); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4403 | |
| 4404 | end: |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 4405 | free_args(args); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4406 | return 1; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4407 | |
| 4408 | error: |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 4409 | free_args(args); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4410 | WILL_LJMP(lua_error(L)); |
| 4411 | return 0; /* Never reached */ |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4412 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4413 | |
| 4414 | /* |
| 4415 | * |
| 4416 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4417 | * Class Converters |
| 4418 | * |
| 4419 | * |
| 4420 | */ |
| 4421 | |
| 4422 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4423 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4424 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4425 | __LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4426 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4427 | return MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4428 | } |
| 4429 | |
| 4430 | /* This function creates and push in the stack a Converters object |
| 4431 | * according with a current TXN. |
| 4432 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4433 | 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] | 4434 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 4435 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4436 | |
| 4437 | /* Check stack size. */ |
| 4438 | if (!lua_checkstack(L, 3)) |
| 4439 | return 0; |
| 4440 | |
| 4441 | /* Create the object: obj[0] = userdata. |
| 4442 | * Note that the base of the Converters object is the |
| 4443 | * same than the TXN object. |
| 4444 | */ |
| 4445 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 4446 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4447 | lua_rawseti(L, -2, 0); |
| 4448 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 4449 | hsmp->s = txn->s; |
| 4450 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 4451 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4452 | hsmp->flags = flags; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4453 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4454 | /* Pop a class stream metatable and affect it to the table. */ |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4455 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref); |
| 4456 | lua_setmetatable(L, -2); |
| 4457 | |
| 4458 | return 1; |
| 4459 | } |
| 4460 | |
| 4461 | /* This function is an LUA binding. It is called with each converter. |
| 4462 | * It uses closure argument to store the associated converter. It |
| 4463 | * returns only one argument or throws an error. An error is thrown |
| 4464 | * only if an error is encountered during the argument parsing. If |
| 4465 | * the converter function function fails, nil is returned. |
| 4466 | */ |
| 4467 | __LJMP static int hlua_run_sample_conv(lua_State *L) |
| 4468 | { |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 4469 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4470 | struct sample_conv *conv; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 4471 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4472 | int i; |
| 4473 | struct sample smp; |
| 4474 | |
| 4475 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4476 | conv = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4477 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4478 | /* Get traditional arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 4479 | hsmp = MAY_LJMP(hlua_checkconverters(L, 1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4480 | |
| 4481 | /* Get extra arguments. */ |
| 4482 | for (i = 0; i < lua_gettop(L) - 2; i++) { |
| 4483 | if (i >= ARGM_NBARGS) |
| 4484 | break; |
| 4485 | hlua_lua2arg(L, i + 3, &args[i]); |
| 4486 | } |
| 4487 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4488 | args[i].data.str.area = NULL; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4489 | |
| 4490 | /* Check arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 4491 | 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] | 4492 | |
| 4493 | /* Run the special args checker. */ |
| 4494 | if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) { |
| 4495 | hlua_pusherror(L, "error in arguments"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4496 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4497 | } |
| 4498 | |
| 4499 | /* Initialise the sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 4500 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4501 | if (!hlua_lua2smp(L, 2, &smp)) { |
| 4502 | hlua_pusherror(L, "error in the input argument"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4503 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4504 | } |
| 4505 | |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 4506 | smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR); |
| 4507 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4508 | /* Apply expected cast. */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 4509 | if (!sample_casts[smp.data.type][conv->in_type]) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4510 | hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'", |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 4511 | smp_to_type[smp.data.type], smp_to_type[conv->in_type]); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4512 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4513 | } |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 4514 | if (sample_casts[smp.data.type][conv->in_type] != c_none && |
| 4515 | !sample_casts[smp.data.type][conv->in_type](&smp)) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4516 | hlua_pusherror(L, "error during the input argument casting"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4517 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4518 | } |
| 4519 | |
| 4520 | /* Run the sample conversion process. */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 4521 | if (!conv->process(args, &smp, conv->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4522 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4523 | lua_pushstring(L, ""); |
| 4524 | else |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 4525 | lua_pushnil(L); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4526 | goto end; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4527 | } |
| 4528 | |
| 4529 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4530 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4531 | hlua_smp2lua_str(L, &smp); |
| 4532 | else |
| 4533 | hlua_smp2lua(L, &smp); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4534 | end: |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 4535 | free_args(args); |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 4536 | return 1; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4537 | |
| 4538 | error: |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 4539 | free_args(args); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4540 | WILL_LJMP(lua_error(L)); |
| 4541 | return 0; /* Never reached */ |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4542 | } |
| 4543 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4544 | /* |
| 4545 | * |
| 4546 | * |
| 4547 | * Class AppletTCP |
| 4548 | * |
| 4549 | * |
| 4550 | */ |
| 4551 | |
| 4552 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 4553 | * a class stream, otherwise it throws an error. |
| 4554 | */ |
| 4555 | __LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud) |
| 4556 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4557 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4558 | } |
| 4559 | |
| 4560 | /* This function creates and push in the stack an Applet object |
| 4561 | * according with a current TXN. |
| 4562 | */ |
| 4563 | static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx) |
| 4564 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4565 | struct hlua_appctx *luactx; |
Willy Tarreau | 0698c80 | 2022-05-11 14:09:57 +0200 | [diff] [blame] | 4566 | struct stream *s = appctx_strm(ctx); |
Christopher Faulet | 2da02ae | 2022-02-24 13:45:27 +0100 | [diff] [blame] | 4567 | struct proxy *p; |
| 4568 | |
| 4569 | ALREADY_CHECKED(s); |
| 4570 | p = s->be; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4571 | |
| 4572 | /* Check stack size. */ |
| 4573 | if (!lua_checkstack(L, 3)) |
| 4574 | return 0; |
| 4575 | |
| 4576 | /* Create the object: obj[0] = userdata. |
| 4577 | * Note that the base of the Converters object is the |
| 4578 | * same than the TXN object. |
| 4579 | */ |
| 4580 | lua_newtable(L); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4581 | luactx = lua_newuserdata(L, sizeof(*luactx)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4582 | lua_rawseti(L, -2, 0); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4583 | luactx->appctx = ctx; |
| 4584 | luactx->htxn.s = s; |
| 4585 | luactx->htxn.p = p; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4586 | |
| 4587 | /* Create the "f" field that contains a list of fetches. */ |
| 4588 | lua_pushstring(L, "f"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4589 | if (!hlua_fetches_new(L, &luactx->htxn, 0)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4590 | return 0; |
| 4591 | lua_settable(L, -3); |
| 4592 | |
| 4593 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 4594 | lua_pushstring(L, "sf"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4595 | if (!hlua_fetches_new(L, &luactx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4596 | return 0; |
| 4597 | lua_settable(L, -3); |
| 4598 | |
| 4599 | /* Create the "c" field that contains a list of converters. */ |
| 4600 | lua_pushstring(L, "c"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4601 | if (!hlua_converters_new(L, &luactx->htxn, 0)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4602 | return 0; |
| 4603 | lua_settable(L, -3); |
| 4604 | |
| 4605 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 4606 | lua_pushstring(L, "sc"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4607 | if (!hlua_converters_new(L, &luactx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4608 | return 0; |
| 4609 | lua_settable(L, -3); |
| 4610 | |
| 4611 | /* Pop a class stream metatable and affect it to the table. */ |
| 4612 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref); |
| 4613 | lua_setmetatable(L, -2); |
| 4614 | |
| 4615 | return 1; |
| 4616 | } |
| 4617 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4618 | __LJMP static int hlua_applet_tcp_set_var(lua_State *L) |
| 4619 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4620 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4621 | struct stream *s; |
| 4622 | const char *name; |
| 4623 | size_t len; |
| 4624 | struct sample smp; |
| 4625 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 4626 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 4627 | 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] | 4628 | |
| 4629 | /* It is useles to retrieve the stream, but this function |
| 4630 | * runs only in a stream context. |
| 4631 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4632 | luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4633 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4634 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4635 | |
| 4636 | /* Converts the third argument in a sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 4637 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4638 | hlua_lua2smp(L, 3, &smp); |
| 4639 | |
| 4640 | /* Store the sample in a variable. */ |
| 4641 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 4642 | |
| 4643 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 4644 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 4645 | else |
| 4646 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 4647 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 4648 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4649 | } |
| 4650 | |
| 4651 | __LJMP static int hlua_applet_tcp_unset_var(lua_State *L) |
| 4652 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4653 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4654 | struct stream *s; |
| 4655 | const char *name; |
| 4656 | size_t len; |
| 4657 | struct sample smp; |
| 4658 | |
| 4659 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 4660 | |
| 4661 | /* It is useles to retrieve the stream, but this function |
| 4662 | * runs only in a stream context. |
| 4663 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4664 | luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4665 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4666 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4667 | |
| 4668 | /* Unset the variable. */ |
| 4669 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 4670 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 4671 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4672 | } |
| 4673 | |
| 4674 | __LJMP static int hlua_applet_tcp_get_var(lua_State *L) |
| 4675 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4676 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4677 | struct stream *s; |
| 4678 | const char *name; |
| 4679 | size_t len; |
| 4680 | struct sample smp; |
| 4681 | |
| 4682 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 4683 | |
| 4684 | /* It is useles to retrieve the stream, but this function |
| 4685 | * runs only in a stream context. |
| 4686 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4687 | luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4688 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4689 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4690 | |
| 4691 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Willy Tarreau | e352b9d | 2021-09-03 11:52:38 +0200 | [diff] [blame] | 4692 | if (!vars_get_by_name(name, len, &smp, NULL)) { |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4693 | lua_pushnil(L); |
| 4694 | return 1; |
| 4695 | } |
| 4696 | |
| 4697 | return hlua_smp2lua(L, &smp); |
| 4698 | } |
| 4699 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4700 | __LJMP static int hlua_applet_tcp_set_priv(lua_State *L) |
| 4701 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4702 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 4703 | struct stream *s = luactx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4704 | struct hlua *hlua; |
| 4705 | |
| 4706 | /* 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] | 4707 | if (!s->hlua) |
| 4708 | return 0; |
| 4709 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4710 | |
| 4711 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 4712 | |
| 4713 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 4714 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4715 | |
| 4716 | /* Get and store new value. */ |
| 4717 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 4718 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 4719 | |
| 4720 | return 0; |
| 4721 | } |
| 4722 | |
| 4723 | __LJMP static int hlua_applet_tcp_get_priv(lua_State *L) |
| 4724 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4725 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 4726 | struct stream *s = luactx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4727 | struct hlua *hlua; |
| 4728 | |
| 4729 | /* 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] | 4730 | if (!s->hlua) { |
| 4731 | lua_pushnil(L); |
| 4732 | return 1; |
| 4733 | } |
| 4734 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4735 | |
| 4736 | /* Push configuration index in the stack. */ |
| 4737 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 4738 | |
| 4739 | return 1; |
| 4740 | } |
| 4741 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4742 | /* If expected data not yet available, it returns a yield. This function |
| 4743 | * consumes the data in the buffer. It returns a string containing the |
| 4744 | * data. This string can be empty. |
| 4745 | */ |
| 4746 | __LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx) |
| 4747 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4748 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 4749 | struct stconn *sc = appctx_sc(luactx->appctx); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4750 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4751 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4752 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4753 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4754 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4755 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4756 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 4757 | ret = co_getline_nc(sc_oc(sc), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4758 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4759 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4760 | if (ret == 0) { |
Willy Tarreau | 90e8b45 | 2022-05-25 18:21:43 +0200 | [diff] [blame] | 4761 | applet_need_more_data(luactx->appctx); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4762 | 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] | 4763 | } |
| 4764 | |
| 4765 | /* End of data: commit the total strings and return. */ |
| 4766 | if (ret < 0) { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4767 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4768 | return 1; |
| 4769 | } |
| 4770 | |
| 4771 | /* Ensure that the block 2 length is usable. */ |
| 4772 | if (ret == 1) |
| 4773 | len2 = 0; |
| 4774 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 4775 | /* don't check the max length read and don't check. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4776 | luaL_addlstring(&luactx->b, blk1, len1); |
| 4777 | luaL_addlstring(&luactx->b, blk2, len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4778 | |
| 4779 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 4780 | co_skip(sc_oc(sc), len1 + len2); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4781 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4782 | return 1; |
| 4783 | } |
| 4784 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4785 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4786 | __LJMP static int hlua_applet_tcp_getline(lua_State *L) |
| 4787 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4788 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4789 | |
| 4790 | /* Initialise the string catenation. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4791 | luaL_buffinit(L, &luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4792 | |
| 4793 | return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0)); |
| 4794 | } |
| 4795 | |
| 4796 | /* If expected data not yet available, it returns a yield. This function |
| 4797 | * consumes the data in the buffer. It returns a string containing the |
| 4798 | * data. This string can be empty. |
| 4799 | */ |
| 4800 | __LJMP static int hlua_applet_tcp_recv_yield(lua_State *L, int status, lua_KContext ctx) |
| 4801 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4802 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 4803 | struct stconn *sc = appctx_sc(luactx->appctx); |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4804 | size_t len = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4805 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4806 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4807 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4808 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4809 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4810 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4811 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 4812 | ret = co_getblk_nc(sc_oc(sc), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4813 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4814 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4815 | if (ret == 0) { |
Willy Tarreau | 90e8b45 | 2022-05-25 18:21:43 +0200 | [diff] [blame] | 4816 | applet_need_more_data(luactx->appctx); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4817 | 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] | 4818 | } |
| 4819 | |
| 4820 | /* End of data: commit the total strings and return. */ |
| 4821 | if (ret < 0) { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4822 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4823 | return 1; |
| 4824 | } |
| 4825 | |
| 4826 | /* Ensure that the block 2 length is usable. */ |
| 4827 | if (ret == 1) |
| 4828 | len2 = 0; |
| 4829 | |
| 4830 | if (len == -1) { |
| 4831 | |
| 4832 | /* If len == -1, catenate all the data avalaile and |
| 4833 | * yield because we want to get all the data until |
| 4834 | * the end of data stream. |
| 4835 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4836 | luaL_addlstring(&luactx->b, blk1, len1); |
| 4837 | luaL_addlstring(&luactx->b, blk2, len2); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 4838 | co_skip(sc_oc(sc), len1 + len2); |
Willy Tarreau | 90e8b45 | 2022-05-25 18:21:43 +0200 | [diff] [blame] | 4839 | applet_need_more_data(luactx->appctx); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4840 | 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] | 4841 | |
| 4842 | } else { |
| 4843 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4844 | /* Copy the first block caping to the length required. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4845 | if (len1 > len) |
| 4846 | len1 = len; |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4847 | luaL_addlstring(&luactx->b, blk1, len1); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4848 | len -= len1; |
| 4849 | |
| 4850 | /* Copy the second block. */ |
| 4851 | if (len2 > len) |
| 4852 | len2 = len; |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4853 | luaL_addlstring(&luactx->b, blk2, len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4854 | len -= len2; |
| 4855 | |
| 4856 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 4857 | co_skip(sc_oc(sc), len1 + len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4858 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4859 | /* If there is no other data available, yield waiting for new data. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4860 | if (len > 0) { |
| 4861 | lua_pushinteger(L, len); |
| 4862 | lua_replace(L, 2); |
Willy Tarreau | 90e8b45 | 2022-05-25 18:21:43 +0200 | [diff] [blame] | 4863 | applet_need_more_data(luactx->appctx); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4864 | 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] | 4865 | } |
| 4866 | |
| 4867 | /* return the result. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4868 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4869 | return 1; |
| 4870 | } |
| 4871 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4872 | /* we never execute this */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4873 | hlua_pusherror(L, "Lua: internal error"); |
| 4874 | WILL_LJMP(lua_error(L)); |
| 4875 | return 0; |
| 4876 | } |
| 4877 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4878 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4879 | __LJMP static int hlua_applet_tcp_recv(lua_State *L) |
| 4880 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4881 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4882 | int len = -1; |
| 4883 | |
| 4884 | if (lua_gettop(L) > 2) |
| 4885 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 4886 | if (lua_gettop(L) >= 2) { |
| 4887 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4888 | lua_pop(L, 1); |
| 4889 | } |
| 4890 | |
| 4891 | /* Confirm or set the required length */ |
| 4892 | lua_pushinteger(L, len); |
| 4893 | |
| 4894 | /* Initialise the string catenation. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4895 | luaL_buffinit(L, &luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4896 | |
| 4897 | return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0)); |
| 4898 | } |
| 4899 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4900 | /* Append data in the output side of the buffer. This data is immediately |
| 4901 | * sent. The function returns the amount of data written. If the buffer |
| 4902 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4903 | * if the channel is closed. |
| 4904 | */ |
| 4905 | __LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx) |
| 4906 | { |
| 4907 | size_t len; |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4908 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4909 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4910 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 4911 | struct stconn *sc = appctx_sc(luactx->appctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 4912 | struct channel *chn = sc_ic(sc); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4913 | int max; |
| 4914 | |
| 4915 | /* Get the max amount of data which can write as input in the channel. */ |
| 4916 | max = channel_recv_max(chn); |
| 4917 | if (max > (len - l)) |
| 4918 | max = len - l; |
| 4919 | |
| 4920 | /* Copy data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4921 | ci_putblk(chn, str + l, max); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4922 | |
| 4923 | /* update counters. */ |
| 4924 | l += max; |
| 4925 | lua_pop(L, 1); |
| 4926 | lua_pushinteger(L, l); |
| 4927 | |
| 4928 | /* If some data is not send, declares the situation to the |
| 4929 | * applet, and returns a yield. |
| 4930 | */ |
| 4931 | if (l < len) { |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 4932 | sc_need_room(sc); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4933 | 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] | 4934 | } |
| 4935 | |
| 4936 | return 1; |
| 4937 | } |
| 4938 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4939 | /* Just a wrapper of "hlua_applet_tcp_send_yield". This wrapper permits |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4940 | * yield the LUA process, and resume it without checking the |
| 4941 | * input arguments. |
| 4942 | */ |
| 4943 | __LJMP static int hlua_applet_tcp_send(lua_State *L) |
| 4944 | { |
| 4945 | MAY_LJMP(check_args(L, 2, "send")); |
| 4946 | lua_pushinteger(L, 0); |
| 4947 | |
| 4948 | return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0)); |
| 4949 | } |
| 4950 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4951 | /* |
| 4952 | * |
| 4953 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4954 | * Class AppletHTTP |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4955 | * |
| 4956 | * |
| 4957 | */ |
| 4958 | |
| 4959 | /* Returns a struct hlua_txn if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4960 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4961 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4962 | __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] | 4963 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4964 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_http_ref)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4965 | } |
| 4966 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4967 | /* This function creates and push in the stack an Applet object |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4968 | * according with a current TXN. |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 4969 | * It relies on the caller to have already reserved the room in ctx->svcctx |
| 4970 | * for the local storage of hlua_http_ctx. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4971 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4972 | static int hlua_applet_http_new(lua_State *L, struct appctx *ctx) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4973 | { |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 4974 | struct hlua_http_ctx *http_ctx = ctx->svcctx; |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4975 | struct hlua_appctx *luactx; |
Thierry FOURNIER | 841475e | 2015-12-11 17:10:09 +0100 | [diff] [blame] | 4976 | struct hlua_txn htxn; |
Willy Tarreau | 0698c80 | 2022-05-11 14:09:57 +0200 | [diff] [blame] | 4977 | struct stream *s = appctx_strm(ctx); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4978 | struct proxy *px = s->be; |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4979 | struct htx *htx; |
| 4980 | struct htx_blk *blk; |
| 4981 | struct htx_sl *sl; |
| 4982 | struct ist path; |
| 4983 | unsigned long long len = 0; |
| 4984 | int32_t pos; |
Amaury Denoyelle | c453f95 | 2021-07-06 11:40:12 +0200 | [diff] [blame] | 4985 | struct http_uri_parser parser; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4986 | |
| 4987 | /* Check stack size. */ |
| 4988 | if (!lua_checkstack(L, 3)) |
| 4989 | return 0; |
| 4990 | |
| 4991 | /* Create the object: obj[0] = userdata. |
| 4992 | * Note that the base of the Converters object is the |
| 4993 | * same than the TXN object. |
| 4994 | */ |
| 4995 | lua_newtable(L); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4996 | luactx = lua_newuserdata(L, sizeof(*luactx)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4997 | lua_rawseti(L, -2, 0); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4998 | luactx->appctx = ctx; |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 4999 | http_ctx->status = 200; /* Default status code returned. */ |
| 5000 | http_ctx->reason = NULL; /* Use default reason based on status */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5001 | luactx->htxn.s = s; |
| 5002 | luactx->htxn.p = px; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5003 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5004 | /* Create the "f" field that contains a list of fetches. */ |
| 5005 | lua_pushstring(L, "f"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5006 | if (!hlua_fetches_new(L, &luactx->htxn, 0)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5007 | return 0; |
| 5008 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5009 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5010 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 5011 | lua_pushstring(L, "sf"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5012 | if (!hlua_fetches_new(L, &luactx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5013 | return 0; |
| 5014 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5015 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5016 | /* Create the "c" field that contains a list of converters. */ |
| 5017 | lua_pushstring(L, "c"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5018 | if (!hlua_converters_new(L, &luactx->htxn, 0)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5019 | return 0; |
| 5020 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5021 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5022 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 5023 | lua_pushstring(L, "sc"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5024 | if (!hlua_converters_new(L, &luactx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5025 | return 0; |
| 5026 | lua_settable(L, -3); |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5027 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5028 | htx = htxbuf(&s->req.buf); |
| 5029 | blk = htx_get_first_blk(htx); |
Christopher Faulet | ea00973 | 2019-11-18 15:50:25 +0100 | [diff] [blame] | 5030 | BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5031 | sl = htx_get_blk_ptr(htx, blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5032 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5033 | /* Stores the request method. */ |
| 5034 | lua_pushstring(L, "method"); |
| 5035 | lua_pushlstring(L, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); |
| 5036 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5037 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5038 | /* Stores the http version. */ |
| 5039 | lua_pushstring(L, "version"); |
| 5040 | lua_pushlstring(L, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); |
| 5041 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5042 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5043 | /* creates an array of headers. hlua_http_get_headers() crates and push |
| 5044 | * the array on the top of the stack. |
| 5045 | */ |
| 5046 | lua_pushstring(L, "headers"); |
| 5047 | htxn.s = s; |
| 5048 | htxn.p = px; |
| 5049 | htxn.dir = SMP_OPT_DIR_REQ; |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 5050 | if (!hlua_http_get_headers(L, &htxn.s->txn->req)) |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5051 | return 0; |
| 5052 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5053 | |
Amaury Denoyelle | c453f95 | 2021-07-06 11:40:12 +0200 | [diff] [blame] | 5054 | parser = http_uri_parser_init(htx_sl_req_uri(sl)); |
| 5055 | path = http_parse_path(&parser); |
Tim Duesterhus | ed52637 | 2020-03-05 17:56:33 +0100 | [diff] [blame] | 5056 | if (isttest(path)) { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5057 | char *p, *q, *end; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5058 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5059 | p = path.ptr; |
Tim Duesterhus | 4c8f75f | 2021-11-06 15:14:44 +0100 | [diff] [blame] | 5060 | end = istend(path); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5061 | q = p; |
| 5062 | while (q < end && *q != '?') |
| 5063 | q++; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5064 | |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 5065 | /* Stores the request path. */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5066 | lua_pushstring(L, "path"); |
| 5067 | lua_pushlstring(L, p, q - p); |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 5068 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 5069 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5070 | /* Stores the query string. */ |
| 5071 | lua_pushstring(L, "qs"); |
| 5072 | if (*q == '?') |
| 5073 | q++; |
| 5074 | lua_pushlstring(L, q, end - q); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5075 | lua_settable(L, -3); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5076 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5077 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5078 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 5079 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 5080 | enum htx_blk_type type = htx_get_blk_type(blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5081 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 5082 | if (type == HTX_BLK_TLR || type == HTX_BLK_EOT) |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5083 | break; |
| 5084 | if (type == HTX_BLK_DATA) |
| 5085 | len += htx_get_blksz(blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5086 | } |
Christopher Faulet | 2e47e3a | 2023-01-13 11:40:24 +0100 | [diff] [blame] | 5087 | if (htx->extra != HTX_UNKOWN_PAYLOAD_LENGTH) |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5088 | len += htx->extra; |
| 5089 | |
| 5090 | /* Stores the request path. */ |
| 5091 | lua_pushstring(L, "length"); |
| 5092 | lua_pushinteger(L, len); |
| 5093 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 5094 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5095 | /* Create an empty array of HTTP request headers. */ |
| 5096 | lua_pushstring(L, "response"); |
| 5097 | lua_newtable(L); |
| 5098 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 5099 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5100 | /* Pop a class stream metatable and affect it to the table. */ |
| 5101 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref); |
| 5102 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5103 | |
| 5104 | return 1; |
| 5105 | } |
| 5106 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 5107 | __LJMP static int hlua_applet_http_set_var(lua_State *L) |
| 5108 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5109 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 5110 | struct stream *s; |
| 5111 | const char *name; |
| 5112 | size_t len; |
| 5113 | struct sample smp; |
| 5114 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 5115 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 5116 | 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] | 5117 | |
| 5118 | /* It is useles to retrieve the stream, but this function |
| 5119 | * runs only in a stream context. |
| 5120 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5121 | luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 5122 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5123 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 5124 | |
| 5125 | /* Converts the third argument in a sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 5126 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 5127 | hlua_lua2smp(L, 3, &smp); |
| 5128 | |
| 5129 | /* Store the sample in a variable. */ |
| 5130 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 5131 | |
| 5132 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 5133 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 5134 | else |
| 5135 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 5136 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 5137 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 5138 | } |
| 5139 | |
| 5140 | __LJMP static int hlua_applet_http_unset_var(lua_State *L) |
| 5141 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5142 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 5143 | struct stream *s; |
| 5144 | const char *name; |
| 5145 | size_t len; |
| 5146 | struct sample smp; |
| 5147 | |
| 5148 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 5149 | |
| 5150 | /* It is useles to retrieve the stream, but this function |
| 5151 | * runs only in a stream context. |
| 5152 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5153 | luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 5154 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5155 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 5156 | |
| 5157 | /* Unset the variable. */ |
| 5158 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 5159 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 5160 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 5161 | } |
| 5162 | |
| 5163 | __LJMP static int hlua_applet_http_get_var(lua_State *L) |
| 5164 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5165 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 5166 | struct stream *s; |
| 5167 | const char *name; |
| 5168 | size_t len; |
| 5169 | struct sample smp; |
| 5170 | |
| 5171 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 5172 | |
| 5173 | /* It is useles to retrieve the stream, but this function |
| 5174 | * runs only in a stream context. |
| 5175 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5176 | luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 5177 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5178 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 5179 | |
| 5180 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Willy Tarreau | e352b9d | 2021-09-03 11:52:38 +0200 | [diff] [blame] | 5181 | if (!vars_get_by_name(name, len, &smp, NULL)) { |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 5182 | lua_pushnil(L); |
| 5183 | return 1; |
| 5184 | } |
| 5185 | |
| 5186 | return hlua_smp2lua(L, &smp); |
| 5187 | } |
| 5188 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 5189 | __LJMP static int hlua_applet_http_set_priv(lua_State *L) |
| 5190 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5191 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 5192 | struct stream *s = luactx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 5193 | struct hlua *hlua; |
| 5194 | |
| 5195 | /* 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] | 5196 | if (!s->hlua) |
| 5197 | return 0; |
| 5198 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 5199 | |
| 5200 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 5201 | |
| 5202 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 5203 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 5204 | |
| 5205 | /* Get and store new value. */ |
| 5206 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 5207 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 5208 | |
| 5209 | return 0; |
| 5210 | } |
| 5211 | |
| 5212 | __LJMP static int hlua_applet_http_get_priv(lua_State *L) |
| 5213 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5214 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 5215 | struct stream *s = luactx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 5216 | struct hlua *hlua; |
| 5217 | |
| 5218 | /* 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] | 5219 | if (!s->hlua) { |
| 5220 | lua_pushnil(L); |
| 5221 | return 1; |
| 5222 | } |
| 5223 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 5224 | |
| 5225 | /* Push configuration index in the stack. */ |
| 5226 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 5227 | |
| 5228 | return 1; |
| 5229 | } |
| 5230 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5231 | /* If expected data not yet available, it returns a yield. This function |
| 5232 | * consumes the data in the buffer. It returns a string containing the |
| 5233 | * data. This string can be empty. |
| 5234 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5235 | __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] | 5236 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5237 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 5238 | struct stconn *sc = appctx_sc(luactx->appctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 5239 | struct channel *req = sc_oc(sc); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5240 | struct htx *htx; |
| 5241 | struct htx_blk *blk; |
| 5242 | size_t count; |
| 5243 | int stop = 0; |
| 5244 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5245 | htx = htx_from_buf(&req->buf); |
| 5246 | count = co_data(req); |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 5247 | blk = htx_get_first_blk(htx); |
Christopher Faulet | cc26b13 | 2018-12-18 21:20:57 +0100 | [diff] [blame] | 5248 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5249 | while (count && !stop && blk) { |
| 5250 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 5251 | uint32_t sz = htx_get_blksz(blk); |
| 5252 | struct ist v; |
| 5253 | uint32_t vlen; |
| 5254 | char *nl; |
| 5255 | |
| 5256 | vlen = sz; |
| 5257 | if (vlen > count) { |
| 5258 | if (type != HTX_BLK_DATA) |
| 5259 | break; |
| 5260 | vlen = count; |
| 5261 | } |
| 5262 | |
| 5263 | switch (type) { |
| 5264 | case HTX_BLK_UNUSED: |
| 5265 | break; |
| 5266 | |
| 5267 | case HTX_BLK_DATA: |
| 5268 | v = htx_get_blk_value(htx, blk); |
| 5269 | v.len = vlen; |
| 5270 | nl = istchr(v, '\n'); |
| 5271 | if (nl != NULL) { |
| 5272 | stop = 1; |
| 5273 | vlen = nl - v.ptr + 1; |
| 5274 | } |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5275 | luaL_addlstring(&luactx->b, v.ptr, vlen); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5276 | break; |
| 5277 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5278 | case HTX_BLK_TLR: |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 5279 | case HTX_BLK_EOT: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5280 | stop = 1; |
| 5281 | break; |
| 5282 | |
| 5283 | default: |
| 5284 | break; |
| 5285 | } |
| 5286 | |
Willy Tarreau | 8424004 | 2022-02-28 16:51:23 +0100 | [diff] [blame] | 5287 | c_rew(req, vlen); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5288 | count -= vlen; |
| 5289 | if (sz == vlen) |
| 5290 | blk = htx_remove_blk(htx, blk); |
| 5291 | else { |
| 5292 | htx_cut_data_blk(htx, blk, vlen); |
| 5293 | break; |
| 5294 | } |
| 5295 | } |
| 5296 | |
Christopher Faulet | eccb31c | 2021-04-02 14:24:56 +0200 | [diff] [blame] | 5297 | /* The message was fully consumed and no more data are expected |
| 5298 | * (EOM flag set). |
| 5299 | */ |
Christopher Faulet | 904763f | 2023-03-22 14:53:11 +0100 | [diff] [blame] | 5300 | if (htx_is_empty(htx) && (sc->flags & SC_FL_EOI)) |
Christopher Faulet | eccb31c | 2021-04-02 14:24:56 +0200 | [diff] [blame] | 5301 | stop = 1; |
| 5302 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 5303 | htx_to_buf(htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5304 | if (!stop) { |
Willy Tarreau | 90e8b45 | 2022-05-25 18:21:43 +0200 | [diff] [blame] | 5305 | applet_need_more_data(luactx->appctx); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5306 | 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] | 5307 | } |
| 5308 | |
| 5309 | /* return the result. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5310 | luaL_pushresult(&luactx->b); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5311 | return 1; |
| 5312 | } |
| 5313 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5314 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5315 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5316 | __LJMP static int hlua_applet_http_getline(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5317 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5318 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5319 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5320 | /* Initialise the string catenation. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5321 | luaL_buffinit(L, &luactx->b); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5322 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5323 | return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5324 | } |
| 5325 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5326 | /* If expected data not yet available, it returns a yield. This function |
| 5327 | * consumes the data in the buffer. It returns a string containing the |
| 5328 | * data. This string can be empty. |
| 5329 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5330 | __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] | 5331 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5332 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 5333 | struct stconn *sc = appctx_sc(luactx->appctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 5334 | struct channel *req = sc_oc(sc); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5335 | struct htx *htx; |
| 5336 | struct htx_blk *blk; |
| 5337 | size_t count; |
| 5338 | int len; |
| 5339 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5340 | htx = htx_from_buf(&req->buf); |
| 5341 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5342 | count = co_data(req); |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 5343 | blk = htx_get_head_blk(htx); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5344 | while (count && len && blk) { |
| 5345 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 5346 | uint32_t sz = htx_get_blksz(blk); |
| 5347 | struct ist v; |
| 5348 | uint32_t vlen; |
| 5349 | |
| 5350 | vlen = sz; |
| 5351 | if (len > 0 && vlen > len) |
| 5352 | vlen = len; |
| 5353 | if (vlen > count) { |
| 5354 | if (type != HTX_BLK_DATA) |
| 5355 | break; |
| 5356 | vlen = count; |
| 5357 | } |
| 5358 | |
| 5359 | switch (type) { |
| 5360 | case HTX_BLK_UNUSED: |
| 5361 | break; |
| 5362 | |
| 5363 | case HTX_BLK_DATA: |
| 5364 | v = htx_get_blk_value(htx, blk); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5365 | luaL_addlstring(&luactx->b, v.ptr, vlen); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5366 | break; |
| 5367 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5368 | case HTX_BLK_TLR: |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 5369 | case HTX_BLK_EOT: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5370 | len = 0; |
| 5371 | break; |
| 5372 | |
| 5373 | default: |
| 5374 | break; |
| 5375 | } |
| 5376 | |
Willy Tarreau | 8424004 | 2022-02-28 16:51:23 +0100 | [diff] [blame] | 5377 | c_rew(req, vlen); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5378 | count -= vlen; |
| 5379 | if (len > 0) |
| 5380 | len -= vlen; |
| 5381 | if (sz == vlen) |
| 5382 | blk = htx_remove_blk(htx, blk); |
| 5383 | else { |
| 5384 | htx_cut_data_blk(htx, blk, vlen); |
| 5385 | break; |
| 5386 | } |
| 5387 | } |
| 5388 | |
Christopher Faulet | eccb31c | 2021-04-02 14:24:56 +0200 | [diff] [blame] | 5389 | /* The message was fully consumed and no more data are expected |
| 5390 | * (EOM flag set). |
| 5391 | */ |
Christopher Faulet | 904763f | 2023-03-22 14:53:11 +0100 | [diff] [blame] | 5392 | if (htx_is_empty(htx) && (sc->flags & SC_FL_EOI)) |
Christopher Faulet | eccb31c | 2021-04-02 14:24:56 +0200 | [diff] [blame] | 5393 | len = 0; |
| 5394 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 5395 | htx_to_buf(htx, &req->buf); |
| 5396 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5397 | /* If we are no other data available, yield waiting for new data. */ |
| 5398 | if (len) { |
| 5399 | if (len > 0) { |
| 5400 | lua_pushinteger(L, len); |
| 5401 | lua_replace(L, 2); |
| 5402 | } |
Willy Tarreau | 90e8b45 | 2022-05-25 18:21:43 +0200 | [diff] [blame] | 5403 | applet_need_more_data(luactx->appctx); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 5404 | 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] | 5405 | } |
| 5406 | |
| 5407 | /* return the result. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5408 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5409 | return 1; |
| 5410 | } |
| 5411 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5412 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5413 | __LJMP static int hlua_applet_http_recv(lua_State *L) |
| 5414 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5415 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5416 | int len = -1; |
| 5417 | |
| 5418 | /* Check arguments. */ |
| 5419 | if (lua_gettop(L) > 2) |
| 5420 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 5421 | if (lua_gettop(L) >= 2) { |
| 5422 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5423 | lua_pop(L, 1); |
| 5424 | } |
| 5425 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5426 | lua_pushinteger(L, len); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5427 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5428 | /* Initialise the string catenation. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5429 | luaL_buffinit(L, &luactx->b); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5430 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5431 | return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5432 | } |
| 5433 | |
| 5434 | /* Append data in the output side of the buffer. This data is immediately |
| 5435 | * sent. The function returns the amount of data written. If the buffer |
| 5436 | * cannot contain the data, the function yields. The function returns -1 |
| 5437 | * if the channel is closed. |
| 5438 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5439 | __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] | 5440 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5441 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 5442 | struct stconn *sc = appctx_sc(luactx->appctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 5443 | struct channel *res = sc_ic(sc); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5444 | struct htx *htx = htx_from_buf(&res->buf); |
| 5445 | const char *data; |
| 5446 | size_t len; |
| 5447 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 5448 | int max; |
| 5449 | |
Christopher Faulet | 9060fc0 | 2019-07-03 11:39:30 +0200 | [diff] [blame] | 5450 | max = htx_get_max_blksz(htx, channel_htx_recv_max(res, htx)); |
Christopher Faulet | 4b0e9b2 | 2019-01-09 12:16:58 +0100 | [diff] [blame] | 5451 | if (!max) |
| 5452 | goto snd_yield; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5453 | |
| 5454 | data = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5455 | |
| 5456 | /* Get the max amount of data which can write as input in the channel. */ |
| 5457 | if (max > (len - l)) |
| 5458 | max = len - l; |
| 5459 | |
| 5460 | /* Copy data. */ |
Willy Tarreau | 0a7ef02 | 2019-05-28 10:30:11 +0200 | [diff] [blame] | 5461 | max = htx_add_data(htx, ist2(data + l, max)); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 5462 | channel_add_input(res, max); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5463 | |
| 5464 | /* update counters. */ |
| 5465 | l += max; |
| 5466 | lua_pop(L, 1); |
| 5467 | lua_pushinteger(L, l); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5468 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5469 | /* If some data is not send, declares the situation to the |
| 5470 | * applet, and returns a yield. |
| 5471 | */ |
| 5472 | if (l < len) { |
Christopher Faulet | 4b0e9b2 | 2019-01-09 12:16:58 +0100 | [diff] [blame] | 5473 | snd_yield: |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 5474 | htx_to_buf(htx, &res->buf); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 5475 | sc_need_room(sc); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 5476 | 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] | 5477 | } |
| 5478 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5479 | htx_to_buf(htx, &res->buf); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5480 | return 1; |
| 5481 | } |
| 5482 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 5483 | /* Just a wrapper of "hlua_applet_send_yield". This wrapper permits |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5484 | * yield the LUA process, and resume it without checking the |
| 5485 | * input arguments. |
| 5486 | */ |
| 5487 | __LJMP static int hlua_applet_http_send(lua_State *L) |
| 5488 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5489 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 5490 | struct hlua_http_ctx *http_ctx = luactx->appctx->svcctx; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5491 | |
| 5492 | /* We want to send some data. Headers must be sent. */ |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 5493 | if (!(http_ctx->flags & APPLET_HDR_SENT)) { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5494 | hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data."); |
| 5495 | WILL_LJMP(lua_error(L)); |
| 5496 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5497 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 5498 | /* This integer is used for followinf the amount of data sent. */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5499 | lua_pushinteger(L, 0); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5500 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5501 | return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5502 | } |
| 5503 | |
| 5504 | __LJMP static int hlua_applet_http_addheader(lua_State *L) |
| 5505 | { |
| 5506 | const char *name; |
| 5507 | int ret; |
| 5508 | |
| 5509 | MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 5510 | name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5511 | MAY_LJMP(luaL_checkstring(L, 3)); |
| 5512 | |
| 5513 | /* Push in the stack the "response" entry. */ |
| 5514 | ret = lua_getfield(L, 1, "response"); |
| 5515 | if (ret != LUA_TTABLE) { |
| 5516 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] " |
| 5517 | "is expected as an array. %s found", lua_typename(L, ret)); |
| 5518 | WILL_LJMP(lua_error(L)); |
| 5519 | } |
| 5520 | |
| 5521 | /* check if the header is already registered if it is not |
| 5522 | * the case, register it. |
| 5523 | */ |
| 5524 | ret = lua_getfield(L, -1, name); |
| 5525 | if (ret == LUA_TNIL) { |
| 5526 | |
| 5527 | /* Entry not found. */ |
| 5528 | lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */ |
| 5529 | |
| 5530 | /* Insert the new header name in the array in the top of the stack. |
| 5531 | * It left the new array in the top of the stack. |
| 5532 | */ |
| 5533 | lua_newtable(L); |
| 5534 | lua_pushvalue(L, 2); |
| 5535 | lua_pushvalue(L, -2); |
| 5536 | lua_settable(L, -4); |
| 5537 | |
| 5538 | } else if (ret != LUA_TTABLE) { |
| 5539 | |
| 5540 | /* corruption error. */ |
| 5541 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] " |
| 5542 | "is expected as an array. %s found", name, lua_typename(L, ret)); |
| 5543 | WILL_LJMP(lua_error(L)); |
| 5544 | } |
| 5545 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 5546 | /* Now the top of thestack is an array of values. We push |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5547 | * the header value as new entry. |
| 5548 | */ |
| 5549 | lua_pushvalue(L, 3); |
| 5550 | ret = lua_rawlen(L, -2); |
| 5551 | lua_rawseti(L, -2, ret + 1); |
| 5552 | lua_pushboolean(L, 1); |
| 5553 | return 1; |
| 5554 | } |
| 5555 | |
| 5556 | __LJMP static int hlua_applet_http_status(lua_State *L) |
| 5557 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5558 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5559 | int status = MAY_LJMP(luaL_checkinteger(L, 2)); |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5560 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 5561 | struct hlua_http_ctx *http_ctx = luactx->appctx->svcctx; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5562 | |
| 5563 | if (status < 100 || status > 599) { |
| 5564 | lua_pushboolean(L, 0); |
| 5565 | return 1; |
| 5566 | } |
| 5567 | |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 5568 | http_ctx->status = status; |
| 5569 | http_ctx->reason = reason; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5570 | lua_pushboolean(L, 1); |
| 5571 | return 1; |
| 5572 | } |
| 5573 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5574 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5575 | __LJMP static int hlua_applet_http_send_response(lua_State *L) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5576 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5577 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 5578 | struct hlua_http_ctx *http_ctx = luactx->appctx->svcctx; |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 5579 | struct stconn *sc = appctx_sc(luactx->appctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 5580 | struct channel *res = sc_ic(sc); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5581 | struct htx *htx; |
| 5582 | struct htx_sl *sl; |
| 5583 | struct h1m h1m; |
| 5584 | const char *status, *reason; |
| 5585 | const char *name, *value; |
| 5586 | size_t nlen, vlen; |
| 5587 | unsigned int flags; |
| 5588 | |
| 5589 | /* Send the message at once. */ |
| 5590 | htx = htx_from_buf(&res->buf); |
| 5591 | h1m_init_res(&h1m); |
| 5592 | |
| 5593 | /* Use the same http version than the request. */ |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 5594 | status = ultoa_r(http_ctx->status, trash.area, trash.size); |
| 5595 | reason = http_ctx->reason; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5596 | if (reason == NULL) |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 5597 | reason = http_get_reason(http_ctx->status); |
| 5598 | if (http_ctx->flags & APPLET_HTTP11) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5599 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11); |
| 5600 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist(status), ist(reason)); |
| 5601 | } |
| 5602 | else { |
| 5603 | flags = HTX_SL_F_IS_RESP; |
| 5604 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), ist(status), ist(reason)); |
| 5605 | } |
| 5606 | if (!sl) { |
| 5607 | hlua_pusherror(L, "Lua applet http '%s': Failed to create response.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5608 | luactx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5609 | WILL_LJMP(lua_error(L)); |
| 5610 | } |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 5611 | sl->info.res.status = http_ctx->status; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5612 | |
| 5613 | /* Get the array associated to the field "response" in the object AppletHTTP. */ |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5614 | if (lua_getfield(L, 1, "response") != LUA_TTABLE) { |
| 5615 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5616 | luactx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5617 | WILL_LJMP(lua_error(L)); |
| 5618 | } |
| 5619 | |
| 5620 | /* Browse the list of headers. */ |
| 5621 | lua_pushnil(L); |
| 5622 | while(lua_next(L, -2) != 0) { |
| 5623 | /* We expect a string as -2. */ |
| 5624 | if (lua_type(L, -2) != LUA_TSTRING) { |
| 5625 | 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] | 5626 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5627 | lua_typename(L, lua_type(L, -2))); |
| 5628 | WILL_LJMP(lua_error(L)); |
| 5629 | } |
| 5630 | name = lua_tolstring(L, -2, &nlen); |
| 5631 | |
| 5632 | /* We expect an array as -1. */ |
| 5633 | if (lua_type(L, -1) != LUA_TTABLE) { |
| 5634 | 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] | 5635 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5636 | name, |
| 5637 | lua_typename(L, lua_type(L, -1))); |
| 5638 | WILL_LJMP(lua_error(L)); |
| 5639 | } |
| 5640 | |
| 5641 | /* Browse the table who is on the top of the stack. */ |
| 5642 | lua_pushnil(L); |
| 5643 | while(lua_next(L, -2) != 0) { |
| 5644 | int id; |
| 5645 | |
| 5646 | /* We expect a number as -2. */ |
| 5647 | if (lua_type(L, -2) != LUA_TNUMBER) { |
| 5648 | 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] | 5649 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5650 | name, |
| 5651 | lua_typename(L, lua_type(L, -2))); |
| 5652 | WILL_LJMP(lua_error(L)); |
| 5653 | } |
| 5654 | id = lua_tointeger(L, -2); |
| 5655 | |
| 5656 | /* We expect a string as -2. */ |
| 5657 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 5658 | 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] | 5659 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5660 | name, id, |
| 5661 | lua_typename(L, lua_type(L, -1))); |
| 5662 | WILL_LJMP(lua_error(L)); |
| 5663 | } |
| 5664 | value = lua_tolstring(L, -1, &vlen); |
| 5665 | |
| 5666 | /* Simple Protocol checks. */ |
Christopher Faulet | 545fbba | 2021-09-28 09:36:25 +0200 | [diff] [blame] | 5667 | if (isteqi(ist2(name, nlen), ist("transfer-encoding"))) { |
| 5668 | int ret; |
| 5669 | |
| 5670 | ret = h1_parse_xfer_enc_header(&h1m, ist2(value, vlen)); |
| 5671 | if (ret < 0) { |
| 5672 | hlua_pusherror(L, "Lua applet http '%s': Invalid '%s' header.\n", |
| 5673 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
| 5674 | name); |
| 5675 | WILL_LJMP(lua_error(L)); |
| 5676 | } |
| 5677 | else if (ret == 0) |
| 5678 | goto next; /* Skip it */ |
| 5679 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5680 | else if (isteqi(ist2(name, nlen), ist("content-length"))) { |
| 5681 | struct ist v = ist2(value, vlen); |
| 5682 | int ret; |
| 5683 | |
| 5684 | ret = h1_parse_cont_len_header(&h1m, &v); |
| 5685 | if (ret < 0) { |
| 5686 | hlua_pusherror(L, "Lua applet http '%s': Invalid '%s' header.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5687 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5688 | name); |
| 5689 | WILL_LJMP(lua_error(L)); |
| 5690 | } |
| 5691 | else if (ret == 0) |
| 5692 | goto next; /* Skip it */ |
| 5693 | } |
| 5694 | |
| 5695 | /* Add a new header */ |
| 5696 | if (!htx_add_header(htx, ist2(name, nlen), ist2(value, vlen))) { |
| 5697 | 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] | 5698 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5699 | name); |
| 5700 | WILL_LJMP(lua_error(L)); |
| 5701 | } |
| 5702 | next: |
| 5703 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 5704 | lua_pop(L, 1); |
| 5705 | } |
| 5706 | |
| 5707 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 5708 | lua_pop(L, 1); |
| 5709 | } |
| 5710 | |
| 5711 | if (h1m.flags & H1_MF_CHNK) |
| 5712 | h1m.flags &= ~H1_MF_CLEN; |
| 5713 | if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK)) |
| 5714 | h1m.flags |= H1_MF_XFER_LEN; |
| 5715 | |
| 5716 | /* Uset HTX start-line flags */ |
| 5717 | if (h1m.flags & H1_MF_XFER_ENC) |
| 5718 | flags |= HTX_SL_F_XFER_ENC; |
| 5719 | if (h1m.flags & H1_MF_XFER_LEN) { |
| 5720 | flags |= HTX_SL_F_XFER_LEN; |
| 5721 | if (h1m.flags & H1_MF_CHNK) |
| 5722 | flags |= HTX_SL_F_CHNK; |
| 5723 | else if (h1m.flags & H1_MF_CLEN) |
| 5724 | flags |= HTX_SL_F_CLEN; |
| 5725 | if (h1m.body_len == 0) |
| 5726 | flags |= HTX_SL_F_BODYLESS; |
| 5727 | } |
| 5728 | sl->flags |= flags; |
| 5729 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 5730 | /* 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] | 5731 | * and the status code implies the presence of a message body, we must |
| 5732 | * announce a transfer encoding chunked. This is required by haproxy |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 5733 | * for the keepalive compliance. If the applet announces a transfer-encoding |
| 5734 | * chunked itself, don't do anything. |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5735 | */ |
| 5736 | if ((flags & (HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN)) == HTX_SL_F_VER_11 && |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 5737 | http_ctx->status >= 200 && http_ctx->status != 204 && http_ctx->status != 304) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5738 | /* Add a new header */ |
| 5739 | sl->flags |= (HTX_SL_F_XFER_ENC|H1_MF_CHNK|H1_MF_XFER_LEN); |
| 5740 | if (!htx_add_header(htx, ist("transfer-encoding"), ist("chunked"))) { |
| 5741 | 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] | 5742 | luactx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5743 | WILL_LJMP(lua_error(L)); |
| 5744 | } |
| 5745 | } |
| 5746 | |
| 5747 | /* Finalize headers. */ |
| 5748 | if (!htx_add_endof(htx, HTX_BLK_EOH)) { |
| 5749 | hlua_pusherror(L, "Lua applet http '%s': Failed create the response.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5750 | luactx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5751 | WILL_LJMP(lua_error(L)); |
| 5752 | } |
| 5753 | |
| 5754 | if (htx_used_space(htx) > b_size(&res->buf) - global.tune.maxrewrite) { |
| 5755 | b_reset(&res->buf); |
| 5756 | hlua_pusherror(L, "Lua: 'start_response': response header block too big"); |
| 5757 | WILL_LJMP(lua_error(L)); |
| 5758 | } |
| 5759 | |
| 5760 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 5761 | channel_add_input(res, htx->data); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5762 | |
| 5763 | /* Headers sent, set the flag. */ |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 5764 | http_ctx->flags |= APPLET_HDR_SENT; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5765 | return 0; |
| 5766 | |
| 5767 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5768 | /* We will build the status line and the headers of the HTTP response. |
| 5769 | * We will try send at once if its not possible, we give back the hand |
| 5770 | * waiting for more room. |
| 5771 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5772 | __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] | 5773 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5774 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 5775 | struct stconn *sc = appctx_sc(luactx->appctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 5776 | struct channel *res = sc_ic(sc); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5777 | |
| 5778 | if (co_data(res)) { |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 5779 | sc_need_room(sc); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5780 | 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] | 5781 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5782 | return MAY_LJMP(hlua_applet_http_send_response(L)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5783 | } |
| 5784 | |
| 5785 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5786 | __LJMP static int hlua_applet_http_start_response(lua_State *L) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5787 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5788 | return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5789 | } |
| 5790 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5791 | /* |
| 5792 | * |
| 5793 | * |
| 5794 | * Class HTTP |
| 5795 | * |
| 5796 | * |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5797 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5798 | |
| 5799 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 5800 | * a class stream, otherwise it throws an error. |
| 5801 | */ |
| 5802 | __LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5803 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5804 | return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref)); |
| 5805 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5806 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5807 | /* This function creates and push in the stack a HTTP object |
| 5808 | * according with a current TXN. |
| 5809 | */ |
| 5810 | static int hlua_http_new(lua_State *L, struct hlua_txn *txn) |
| 5811 | { |
| 5812 | struct hlua_txn *htxn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5813 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5814 | /* Check stack size. */ |
| 5815 | if (!lua_checkstack(L, 3)) |
| 5816 | return 0; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5817 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5818 | /* Create the object: obj[0] = userdata. |
| 5819 | * Note that the base of the Converters object is the |
| 5820 | * same than the TXN object. |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5821 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5822 | lua_newtable(L); |
| 5823 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
| 5824 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5825 | |
| 5826 | htxn->s = txn->s; |
| 5827 | htxn->p = txn->p; |
Christopher Faulet | 256b69a | 2019-05-23 11:14:21 +0200 | [diff] [blame] | 5828 | htxn->dir = txn->dir; |
| 5829 | htxn->flags = txn->flags; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5830 | |
| 5831 | /* Pop a class stream metatable and affect it to the table. */ |
| 5832 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref); |
| 5833 | lua_setmetatable(L, -2); |
| 5834 | |
| 5835 | return 1; |
| 5836 | } |
| 5837 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 5838 | /* This function creates and returns an array containing the status-line |
| 5839 | * elements. This function does not fails. |
| 5840 | */ |
| 5841 | __LJMP static int hlua_http_get_stline(lua_State *L, struct htx_sl *sl) |
| 5842 | { |
| 5843 | /* Create the table. */ |
| 5844 | lua_newtable(L); |
| 5845 | |
| 5846 | if (sl->flags & HTX_SL_F_IS_RESP) { |
| 5847 | lua_pushstring(L, "version"); |
| 5848 | lua_pushlstring(L, HTX_SL_RES_VPTR(sl), HTX_SL_RES_VLEN(sl)); |
| 5849 | lua_settable(L, -3); |
| 5850 | lua_pushstring(L, "code"); |
| 5851 | lua_pushlstring(L, HTX_SL_RES_CPTR(sl), HTX_SL_RES_CLEN(sl)); |
| 5852 | lua_settable(L, -3); |
| 5853 | lua_pushstring(L, "reason"); |
| 5854 | lua_pushlstring(L, HTX_SL_RES_RPTR(sl), HTX_SL_RES_RLEN(sl)); |
| 5855 | lua_settable(L, -3); |
| 5856 | } |
| 5857 | else { |
| 5858 | lua_pushstring(L, "method"); |
| 5859 | lua_pushlstring(L, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); |
| 5860 | lua_settable(L, -3); |
| 5861 | lua_pushstring(L, "uri"); |
| 5862 | lua_pushlstring(L, HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl)); |
| 5863 | lua_settable(L, -3); |
| 5864 | lua_pushstring(L, "version"); |
| 5865 | lua_pushlstring(L, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); |
| 5866 | lua_settable(L, -3); |
| 5867 | } |
| 5868 | return 1; |
| 5869 | } |
| 5870 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5871 | /* This function creates ans returns an array of HTTP headers. |
| 5872 | * This function does not fails. It is used as wrapper with the |
| 5873 | * 2 following functions. |
| 5874 | */ |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 5875 | __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] | 5876 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5877 | struct htx *htx; |
| 5878 | int32_t pos; |
| 5879 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5880 | /* Create the table. */ |
| 5881 | lua_newtable(L); |
| 5882 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5883 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5884 | htx = htxbuf(&msg->chn->buf); |
| 5885 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 5886 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 5887 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 5888 | struct ist n, v; |
| 5889 | int len; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5890 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5891 | if (type == HTX_BLK_HDR) { |
| 5892 | n = htx_get_blk_name(htx,blk); |
| 5893 | v = htx_get_blk_value(htx, blk); |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5894 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5895 | else if (type == HTX_BLK_EOH) |
| 5896 | break; |
| 5897 | else |
| 5898 | continue; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5899 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5900 | /* Check for existing entry: |
| 5901 | * assume that the table is on the top of the stack, and |
| 5902 | * push the key in the stack, the function lua_gettable() |
| 5903 | * perform the lookup. |
| 5904 | */ |
| 5905 | lua_pushlstring(L, n.ptr, n.len); |
| 5906 | lua_gettable(L, -2); |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5907 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5908 | switch (lua_type(L, -1)) { |
| 5909 | case LUA_TNIL: |
| 5910 | /* Table not found, create it. */ |
| 5911 | lua_pop(L, 1); /* remove the nil value. */ |
| 5912 | lua_pushlstring(L, n.ptr, n.len); /* push the header name as key. */ |
| 5913 | lua_newtable(L); /* create and push empty table. */ |
| 5914 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 5915 | lua_rawseti(L, -2, 0); /* index header value (pop it). */ |
| 5916 | 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] | 5917 | break; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5918 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5919 | case LUA_TTABLE: |
| 5920 | /* Entry found: push the value in the table. */ |
| 5921 | len = lua_rawlen(L, -1); |
| 5922 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 5923 | lua_rawseti(L, -2, len+1); /* index header value (pop it). */ |
| 5924 | lua_pop(L, 1); /* remove the table (it is stored in the main table). */ |
| 5925 | break; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5926 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5927 | default: |
| 5928 | /* Other cases are errors. */ |
| 5929 | hlua_pusherror(L, "internal error during the parsing of headers."); |
| 5930 | WILL_LJMP(lua_error(L)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5931 | } |
| 5932 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5933 | return 1; |
| 5934 | } |
| 5935 | |
| 5936 | __LJMP static int hlua_http_req_get_headers(lua_State *L) |
| 5937 | { |
| 5938 | struct hlua_txn *htxn; |
| 5939 | |
| 5940 | MAY_LJMP(check_args(L, 1, "req_get_headers")); |
| 5941 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5942 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5943 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5944 | WILL_LJMP(lua_error(L)); |
| 5945 | |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 5946 | return hlua_http_get_headers(L, &htxn->s->txn->req); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5947 | } |
| 5948 | |
| 5949 | __LJMP static int hlua_http_res_get_headers(lua_State *L) |
| 5950 | { |
| 5951 | struct hlua_txn *htxn; |
| 5952 | |
| 5953 | MAY_LJMP(check_args(L, 1, "res_get_headers")); |
| 5954 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5955 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5956 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5957 | WILL_LJMP(lua_error(L)); |
| 5958 | |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 5959 | return hlua_http_get_headers(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5960 | } |
| 5961 | |
| 5962 | /* This function replace full header, or just a value in |
| 5963 | * the request or in the response. It is a wrapper fir the |
| 5964 | * 4 following functions. |
| 5965 | */ |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5966 | __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] | 5967 | { |
| 5968 | size_t name_len; |
| 5969 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5970 | const char *reg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 5971 | const char *value = MAY_LJMP(luaL_checkstring(L, 4)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5972 | struct htx *htx; |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 5973 | struct my_regex *re; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5974 | |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 5975 | if (!(re = regex_comp(reg, 1, 1, NULL))) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5976 | WILL_LJMP(luaL_argerror(L, 3, "invalid regex")); |
| 5977 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5978 | htx = htxbuf(&msg->chn->buf); |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5979 | 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] | 5980 | regex_free(re); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5981 | return 0; |
| 5982 | } |
| 5983 | |
| 5984 | __LJMP static int hlua_http_req_rep_hdr(lua_State *L) |
| 5985 | { |
| 5986 | struct hlua_txn *htxn; |
| 5987 | |
| 5988 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 5989 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5990 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5991 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5992 | WILL_LJMP(lua_error(L)); |
| 5993 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5994 | 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] | 5995 | } |
| 5996 | |
| 5997 | __LJMP static int hlua_http_res_rep_hdr(lua_State *L) |
| 5998 | { |
| 5999 | struct hlua_txn *htxn; |
| 6000 | |
| 6001 | MAY_LJMP(check_args(L, 4, "res_rep_hdr")); |
| 6002 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 6003 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 6004 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6005 | WILL_LJMP(lua_error(L)); |
| 6006 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 6007 | 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] | 6008 | } |
| 6009 | |
| 6010 | __LJMP static int hlua_http_req_rep_val(lua_State *L) |
| 6011 | { |
| 6012 | struct hlua_txn *htxn; |
| 6013 | |
| 6014 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 6015 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 6016 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 6017 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6018 | WILL_LJMP(lua_error(L)); |
| 6019 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 6020 | 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] | 6021 | } |
| 6022 | |
| 6023 | __LJMP static int hlua_http_res_rep_val(lua_State *L) |
| 6024 | { |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6025 | struct hlua_txn *htxn; |
| 6026 | |
| 6027 | MAY_LJMP(check_args(L, 4, "res_rep_val")); |
| 6028 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 6029 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 6030 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6031 | WILL_LJMP(lua_error(L)); |
| 6032 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 6033 | 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] | 6034 | } |
| 6035 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6036 | /* This function deletes all the occurrences of an header. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6037 | * It is a wrapper for the 2 following functions. |
| 6038 | */ |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 6039 | __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] | 6040 | { |
| 6041 | size_t len; |
| 6042 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 6043 | struct htx *htx = htxbuf(&msg->chn->buf); |
| 6044 | struct http_hdr_ctx ctx; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6045 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 6046 | ctx.blk = NULL; |
| 6047 | while (http_find_header(htx, ist2(name, len), &ctx, 1)) |
| 6048 | http_remove_header(htx, &ctx); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6049 | return 0; |
| 6050 | } |
| 6051 | |
| 6052 | __LJMP static int hlua_http_req_del_hdr(lua_State *L) |
| 6053 | { |
| 6054 | struct hlua_txn *htxn; |
| 6055 | |
| 6056 | MAY_LJMP(check_args(L, 2, "req_del_hdr")); |
| 6057 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 6058 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 6059 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6060 | WILL_LJMP(lua_error(L)); |
| 6061 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 6062 | return hlua_http_del_hdr(L, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6063 | } |
| 6064 | |
| 6065 | __LJMP static int hlua_http_res_del_hdr(lua_State *L) |
| 6066 | { |
| 6067 | struct hlua_txn *htxn; |
| 6068 | |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6069 | MAY_LJMP(check_args(L, 2, "res_del_hdr")); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6070 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 6071 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 6072 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6073 | WILL_LJMP(lua_error(L)); |
| 6074 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 6075 | return hlua_http_del_hdr(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6076 | } |
| 6077 | |
| 6078 | /* This function adds an header. It is a wrapper used by |
| 6079 | * the 2 following functions. |
| 6080 | */ |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 6081 | __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] | 6082 | { |
| 6083 | size_t name_len; |
| 6084 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 6085 | size_t value_len; |
| 6086 | const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 6087 | struct htx *htx = htxbuf(&msg->chn->buf); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6088 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 6089 | lua_pushboolean(L, http_add_header(htx, ist2(name, name_len), |
| 6090 | ist2(value, value_len))); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6091 | return 0; |
| 6092 | } |
| 6093 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6094 | __LJMP static int hlua_http_req_add_hdr(lua_State *L) |
| 6095 | { |
| 6096 | struct hlua_txn *htxn; |
| 6097 | |
| 6098 | MAY_LJMP(check_args(L, 3, "req_add_hdr")); |
| 6099 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 6100 | |
| 6101 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
| 6102 | WILL_LJMP(lua_error(L)); |
| 6103 | |
| 6104 | return hlua_http_add_hdr(L, &htxn->s->txn->req); |
| 6105 | } |
| 6106 | |
| 6107 | __LJMP static int hlua_http_res_add_hdr(lua_State *L) |
| 6108 | { |
| 6109 | struct hlua_txn *htxn; |
| 6110 | |
| 6111 | MAY_LJMP(check_args(L, 3, "res_add_hdr")); |
| 6112 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 6113 | |
| 6114 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
| 6115 | WILL_LJMP(lua_error(L)); |
| 6116 | |
| 6117 | return hlua_http_add_hdr(L, &htxn->s->txn->rsp); |
| 6118 | } |
| 6119 | |
| 6120 | static int hlua_http_req_set_hdr(lua_State *L) |
| 6121 | { |
| 6122 | struct hlua_txn *htxn; |
| 6123 | |
| 6124 | MAY_LJMP(check_args(L, 3, "req_set_hdr")); |
| 6125 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 6126 | |
| 6127 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
| 6128 | WILL_LJMP(lua_error(L)); |
| 6129 | |
| 6130 | hlua_http_del_hdr(L, &htxn->s->txn->req); |
| 6131 | return hlua_http_add_hdr(L, &htxn->s->txn->req); |
| 6132 | } |
| 6133 | |
| 6134 | static int hlua_http_res_set_hdr(lua_State *L) |
| 6135 | { |
| 6136 | struct hlua_txn *htxn; |
| 6137 | |
| 6138 | MAY_LJMP(check_args(L, 3, "res_set_hdr")); |
| 6139 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 6140 | |
| 6141 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
| 6142 | WILL_LJMP(lua_error(L)); |
| 6143 | |
| 6144 | hlua_http_del_hdr(L, &htxn->s->txn->rsp); |
| 6145 | return hlua_http_add_hdr(L, &htxn->s->txn->rsp); |
| 6146 | } |
| 6147 | |
| 6148 | /* This function set the method. */ |
| 6149 | static int hlua_http_req_set_meth(lua_State *L) |
| 6150 | { |
| 6151 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 6152 | size_t name_len; |
| 6153 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 6154 | |
| 6155 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
| 6156 | WILL_LJMP(lua_error(L)); |
| 6157 | |
| 6158 | lua_pushboolean(L, http_req_replace_stline(0, name, name_len, htxn->p, htxn->s) != -1); |
| 6159 | return 1; |
| 6160 | } |
| 6161 | |
| 6162 | /* This function set the method. */ |
| 6163 | static int hlua_http_req_set_path(lua_State *L) |
| 6164 | { |
| 6165 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 6166 | size_t name_len; |
| 6167 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 6168 | |
| 6169 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
| 6170 | WILL_LJMP(lua_error(L)); |
| 6171 | |
| 6172 | lua_pushboolean(L, http_req_replace_stline(1, name, name_len, htxn->p, htxn->s) != -1); |
| 6173 | return 1; |
| 6174 | } |
| 6175 | |
| 6176 | /* This function set the query-string. */ |
| 6177 | static int hlua_http_req_set_query(lua_State *L) |
| 6178 | { |
| 6179 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 6180 | size_t name_len; |
| 6181 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 6182 | |
| 6183 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
| 6184 | WILL_LJMP(lua_error(L)); |
| 6185 | |
| 6186 | /* Check length. */ |
| 6187 | if (name_len > trash.size - 1) { |
| 6188 | lua_pushboolean(L, 0); |
| 6189 | return 1; |
| 6190 | } |
| 6191 | |
| 6192 | /* Add the mark question as prefix. */ |
| 6193 | chunk_reset(&trash); |
| 6194 | trash.area[trash.data++] = '?'; |
| 6195 | memcpy(trash.area + trash.data, name, name_len); |
| 6196 | trash.data += name_len; |
| 6197 | |
| 6198 | lua_pushboolean(L, |
| 6199 | http_req_replace_stline(2, trash.area, trash.data, htxn->p, htxn->s) != -1); |
| 6200 | return 1; |
| 6201 | } |
| 6202 | |
| 6203 | /* This function set the uri. */ |
| 6204 | static int hlua_http_req_set_uri(lua_State *L) |
| 6205 | { |
| 6206 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 6207 | size_t name_len; |
| 6208 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 6209 | |
| 6210 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
| 6211 | WILL_LJMP(lua_error(L)); |
| 6212 | |
| 6213 | lua_pushboolean(L, http_req_replace_stline(3, name, name_len, htxn->p, htxn->s) != -1); |
| 6214 | return 1; |
| 6215 | } |
| 6216 | |
| 6217 | /* This function set the response code & optionally reason. */ |
| 6218 | static int hlua_http_res_set_status(lua_State *L) |
| 6219 | { |
| 6220 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 6221 | unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 6222 | const char *str = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
| 6223 | const struct ist reason = ist2(str, (str ? strlen(str) : 0)); |
| 6224 | |
| 6225 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
| 6226 | WILL_LJMP(lua_error(L)); |
| 6227 | |
| 6228 | http_res_set_status(code, reason, htxn->s); |
| 6229 | return 0; |
| 6230 | } |
| 6231 | |
| 6232 | /* |
| 6233 | * |
| 6234 | * |
| 6235 | * Class HTTPMessage |
| 6236 | * |
| 6237 | * |
| 6238 | */ |
| 6239 | |
| 6240 | /* Returns a struct http_msg if the stack entry "ud" is a class HTTPMessage, |
| 6241 | * otherwise it throws an error. |
| 6242 | */ |
| 6243 | __LJMP static struct http_msg *hlua_checkhttpmsg(lua_State *L, int ud) |
| 6244 | { |
| 6245 | return MAY_LJMP(hlua_checkudata(L, ud, class_http_msg_ref)); |
| 6246 | } |
| 6247 | |
| 6248 | /* Creates and pushes on the stack a HTTP object according with a current TXN. |
| 6249 | */ |
Christopher Faulet | 78c3547 | 2020-02-26 17:14:08 +0100 | [diff] [blame] | 6250 | static int hlua_http_msg_new(lua_State *L, struct http_msg *msg) |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6251 | { |
| 6252 | /* Check stack size. */ |
| 6253 | if (!lua_checkstack(L, 3)) |
| 6254 | return 0; |
| 6255 | |
| 6256 | lua_newtable(L); |
| 6257 | lua_pushlightuserdata(L, msg); |
| 6258 | lua_rawseti(L, -2, 0); |
| 6259 | |
| 6260 | /* Create the "channel" field that contains the request channel object. */ |
| 6261 | lua_pushstring(L, "channel"); |
| 6262 | if (!hlua_channel_new(L, msg->chn)) |
| 6263 | return 0; |
| 6264 | lua_rawset(L, -3); |
| 6265 | |
| 6266 | /* Pop a class stream metatable and affect it to the table. */ |
| 6267 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_msg_ref); |
| 6268 | lua_setmetatable(L, -2); |
| 6269 | |
| 6270 | return 1; |
| 6271 | } |
| 6272 | |
| 6273 | /* Helper function returning a filter attached to the HTTP message at the |
| 6274 | * position <ud> in the stack, filling the current offset and length of the |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 6275 | * filter. If no filter is attached, NULL is returned and <offset> and <len> are |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6276 | * filled with output and input length respectively. |
| 6277 | */ |
| 6278 | static struct filter *hlua_http_msg_filter(lua_State *L, int ud, struct http_msg *msg, size_t *offset, size_t *len) |
| 6279 | { |
| 6280 | struct channel *chn = msg->chn; |
| 6281 | struct htx *htx = htxbuf(&chn->buf); |
| 6282 | struct filter *filter = NULL; |
| 6283 | |
| 6284 | *offset = co_data(msg->chn); |
| 6285 | *len = htx->data - co_data(msg->chn); |
| 6286 | |
| 6287 | if (lua_getfield(L, ud, "__filter") == LUA_TLIGHTUSERDATA) { |
| 6288 | filter = lua_touserdata (L, -1); |
| 6289 | if (msg->msg_state >= HTTP_MSG_DATA) { |
| 6290 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 6291 | |
| 6292 | *offset = flt_ctx->cur_off[CHN_IDX(chn)]; |
| 6293 | *len = flt_ctx->cur_len[CHN_IDX(chn)]; |
| 6294 | } |
| 6295 | } |
| 6296 | |
| 6297 | lua_pop(L, 1); |
| 6298 | return filter; |
| 6299 | } |
| 6300 | |
| 6301 | /* Returns true if the channel attached to the HTTP message is the response |
| 6302 | * channel. |
| 6303 | */ |
| 6304 | __LJMP static int hlua_http_msg_is_resp(lua_State *L) |
| 6305 | { |
| 6306 | struct http_msg *msg; |
| 6307 | |
| 6308 | MAY_LJMP(check_args(L, 1, "is_resp")); |
| 6309 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6310 | |
| 6311 | lua_pushboolean(L, !!(msg->chn->flags & CF_ISRESP)); |
| 6312 | return 1; |
| 6313 | } |
| 6314 | |
| 6315 | /* Returns an array containing the elements status-line of the HTTP message. It relies |
| 6316 | * on hlua_http_get_stline(). |
| 6317 | */ |
| 6318 | __LJMP static int hlua_http_msg_get_stline(lua_State *L) |
| 6319 | { |
| 6320 | struct http_msg *msg; |
| 6321 | struct htx *htx; |
| 6322 | struct htx_sl *sl; |
| 6323 | |
| 6324 | MAY_LJMP(check_args(L, 1, "get_stline")); |
| 6325 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6326 | |
| 6327 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6328 | WILL_LJMP(lua_error(L)); |
| 6329 | |
| 6330 | htx = htxbuf(&msg->chn->buf); |
| 6331 | sl = http_get_stline(htx); |
| 6332 | if (!sl) |
| 6333 | return 0; |
| 6334 | return hlua_http_get_stline(L, sl); |
| 6335 | } |
| 6336 | |
| 6337 | /* Returns an array containing all headers of the HTTP message. it relies on |
| 6338 | * hlua_http_get_headers(). |
| 6339 | */ |
| 6340 | __LJMP static int hlua_http_msg_get_headers(lua_State *L) |
| 6341 | { |
| 6342 | struct http_msg *msg; |
| 6343 | |
| 6344 | MAY_LJMP(check_args(L, 1, "get_headers")); |
| 6345 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6346 | |
| 6347 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6348 | WILL_LJMP(lua_error(L)); |
| 6349 | |
| 6350 | return hlua_http_get_headers(L, msg); |
| 6351 | } |
| 6352 | |
| 6353 | /* Deletes all occurrences of an header in the HTTP message matching on its |
| 6354 | * name. It relies on hlua_http_del_hdr(). |
| 6355 | */ |
| 6356 | __LJMP static int hlua_http_msg_del_hdr(lua_State *L) |
| 6357 | { |
| 6358 | struct http_msg *msg; |
| 6359 | |
| 6360 | MAY_LJMP(check_args(L, 2, "del_header")); |
| 6361 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6362 | |
| 6363 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6364 | WILL_LJMP(lua_error(L)); |
| 6365 | |
| 6366 | return hlua_http_del_hdr(L, msg); |
| 6367 | } |
| 6368 | |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 6369 | /* Matches the full value line of all occurrences of an header in the HTTP |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6370 | * message given its name against a regex and replaces it if it matches. It |
| 6371 | * relies on hlua_http_rep_hdr(). |
| 6372 | */ |
| 6373 | __LJMP static int hlua_http_msg_rep_hdr(lua_State *L) |
| 6374 | { |
| 6375 | struct http_msg *msg; |
| 6376 | |
| 6377 | MAY_LJMP(check_args(L, 4, "rep_header")); |
| 6378 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6379 | |
| 6380 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6381 | WILL_LJMP(lua_error(L)); |
| 6382 | |
| 6383 | return hlua_http_rep_hdr(L, msg, 1); |
| 6384 | } |
| 6385 | |
Ilya Shipitsin | bd6b4be | 2021-10-15 16:18:21 +0500 | [diff] [blame] | 6386 | /* Matches all comma-separated values of all occurrences of an header in the HTTP |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6387 | * message given its name against a regex and replaces it if it matches. It |
| 6388 | * relies on hlua_http_rep_hdr(). |
| 6389 | */ |
| 6390 | __LJMP static int hlua_http_msg_rep_val(lua_State *L) |
| 6391 | { |
| 6392 | struct http_msg *msg; |
| 6393 | |
| 6394 | MAY_LJMP(check_args(L, 4, "rep_value")); |
| 6395 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6396 | |
| 6397 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6398 | WILL_LJMP(lua_error(L)); |
| 6399 | |
| 6400 | return hlua_http_rep_hdr(L, msg, 0); |
| 6401 | } |
| 6402 | |
| 6403 | /* Add an header in the HTTP message. It relies on hlua_http_add_hdr() */ |
| 6404 | __LJMP static int hlua_http_msg_add_hdr(lua_State *L) |
| 6405 | { |
| 6406 | struct http_msg *msg; |
| 6407 | |
| 6408 | MAY_LJMP(check_args(L, 3, "add_header")); |
| 6409 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6410 | |
| 6411 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6412 | WILL_LJMP(lua_error(L)); |
| 6413 | |
| 6414 | return hlua_http_add_hdr(L, msg); |
| 6415 | } |
| 6416 | |
| 6417 | /* Add an header in the HTTP message removing existing headers with the same |
| 6418 | * name. It relies on hlua_http_del_hdr() and hlua_http_add_hdr(). |
| 6419 | */ |
| 6420 | __LJMP static int hlua_http_msg_set_hdr(lua_State *L) |
| 6421 | { |
| 6422 | struct http_msg *msg; |
| 6423 | |
| 6424 | MAY_LJMP(check_args(L, 3, "set_header")); |
| 6425 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6426 | |
| 6427 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6428 | WILL_LJMP(lua_error(L)); |
| 6429 | |
| 6430 | hlua_http_del_hdr(L, msg); |
| 6431 | return hlua_http_add_hdr(L, msg); |
| 6432 | } |
| 6433 | |
| 6434 | /* Rewrites the request method. It relies on http_req_replace_stline(). */ |
| 6435 | __LJMP static int hlua_http_msg_set_meth(lua_State *L) |
| 6436 | { |
| 6437 | struct stream *s; |
| 6438 | struct http_msg *msg; |
| 6439 | const char *name; |
| 6440 | size_t name_len; |
| 6441 | |
| 6442 | MAY_LJMP(check_args(L, 2, "set_method")); |
| 6443 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6444 | name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 6445 | |
| 6446 | if ((msg->chn->flags & CF_ISRESP) || msg->msg_state > HTTP_MSG_BODY) |
| 6447 | WILL_LJMP(lua_error(L)); |
| 6448 | |
| 6449 | s = chn_strm(msg->chn); |
| 6450 | lua_pushboolean(L, http_req_replace_stline(0, name, name_len, s->be, s) != -1); |
| 6451 | return 1; |
| 6452 | } |
| 6453 | |
| 6454 | /* Rewrites the request path. It relies on http_req_replace_stline(). */ |
| 6455 | __LJMP static int hlua_http_msg_set_path(lua_State *L) |
| 6456 | { |
| 6457 | struct stream *s; |
| 6458 | struct http_msg *msg; |
| 6459 | const char *name; |
| 6460 | size_t name_len; |
| 6461 | |
| 6462 | MAY_LJMP(check_args(L, 2, "set_path")); |
| 6463 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6464 | name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 6465 | |
| 6466 | if ((msg->chn->flags & CF_ISRESP) || msg->msg_state > HTTP_MSG_BODY) |
| 6467 | WILL_LJMP(lua_error(L)); |
| 6468 | |
| 6469 | s = chn_strm(msg->chn); |
| 6470 | lua_pushboolean(L, http_req_replace_stline(1, name, name_len, s->be, s) != -1); |
| 6471 | return 1; |
| 6472 | } |
| 6473 | |
| 6474 | /* Rewrites the request query-string. It relies on http_req_replace_stline(). */ |
| 6475 | __LJMP static int hlua_http_msg_set_query(lua_State *L) |
| 6476 | { |
| 6477 | struct stream *s; |
| 6478 | struct http_msg *msg; |
| 6479 | const char *name; |
| 6480 | size_t name_len; |
| 6481 | |
| 6482 | MAY_LJMP(check_args(L, 2, "set_query")); |
| 6483 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6484 | name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 6485 | |
| 6486 | if ((msg->chn->flags & CF_ISRESP) || msg->msg_state > HTTP_MSG_BODY) |
| 6487 | WILL_LJMP(lua_error(L)); |
| 6488 | |
| 6489 | /* Check length. */ |
| 6490 | if (name_len > trash.size - 1) { |
| 6491 | lua_pushboolean(L, 0); |
| 6492 | return 1; |
| 6493 | } |
| 6494 | |
| 6495 | /* Add the mark question as prefix. */ |
| 6496 | chunk_reset(&trash); |
| 6497 | trash.area[trash.data++] = '?'; |
| 6498 | memcpy(trash.area + trash.data, name, name_len); |
| 6499 | trash.data += name_len; |
| 6500 | |
| 6501 | s = chn_strm(msg->chn); |
| 6502 | lua_pushboolean(L, http_req_replace_stline(2, trash.area, trash.data, s->be, s) != -1); |
| 6503 | return 1; |
| 6504 | } |
| 6505 | |
| 6506 | /* Rewrites the request URI. It relies on http_req_replace_stline(). */ |
| 6507 | __LJMP static int hlua_http_msg_set_uri(lua_State *L) |
| 6508 | { |
| 6509 | struct stream *s; |
| 6510 | struct http_msg *msg; |
| 6511 | const char *name; |
| 6512 | size_t name_len; |
| 6513 | |
| 6514 | MAY_LJMP(check_args(L, 2, "set_uri")); |
| 6515 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6516 | name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 6517 | |
| 6518 | if ((msg->chn->flags & CF_ISRESP) || msg->msg_state > HTTP_MSG_BODY) |
| 6519 | WILL_LJMP(lua_error(L)); |
| 6520 | |
| 6521 | s = chn_strm(msg->chn); |
| 6522 | lua_pushboolean(L, http_req_replace_stline(3, name, name_len, s->be, s) != -1); |
| 6523 | return 1; |
| 6524 | } |
| 6525 | |
| 6526 | /* Rewrites the response status code. It relies on http_res_set_status(). */ |
| 6527 | __LJMP static int hlua_http_msg_set_status(lua_State *L) |
| 6528 | { |
| 6529 | struct http_msg *msg; |
| 6530 | unsigned int code; |
| 6531 | const char *reason; |
| 6532 | size_t reason_len; |
| 6533 | |
| 6534 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6535 | code = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 6536 | reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, &reason_len)); |
| 6537 | |
| 6538 | if (!(msg->chn->flags & CF_ISRESP) || msg->msg_state > HTTP_MSG_BODY) |
| 6539 | WILL_LJMP(lua_error(L)); |
| 6540 | |
| 6541 | lua_pushboolean(L, http_res_set_status(code, ist2(reason, reason_len), chn_strm(msg->chn)) != -1); |
| 6542 | return 1; |
| 6543 | } |
| 6544 | |
| 6545 | /* Returns true if the HTTP message is full. */ |
| 6546 | __LJMP static int hlua_http_msg_is_full(lua_State *L) |
| 6547 | { |
| 6548 | struct http_msg *msg; |
| 6549 | |
| 6550 | MAY_LJMP(check_args(L, 1, "is_full")); |
| 6551 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6552 | lua_pushboolean(L, channel_full(msg->chn, 0)); |
| 6553 | return 1; |
| 6554 | } |
| 6555 | |
| 6556 | /* Returns true if the HTTP message may still receive data. */ |
| 6557 | __LJMP static int hlua_http_msg_may_recv(lua_State *L) |
| 6558 | { |
| 6559 | struct http_msg *msg; |
| 6560 | struct htx *htx; |
| 6561 | |
| 6562 | MAY_LJMP(check_args(L, 1, "may_recv")); |
| 6563 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6564 | htx = htxbuf(&msg->chn->buf); |
| 6565 | lua_pushboolean(L, (htx_expect_more(htx) && !channel_input_closed(msg->chn) && channel_may_recv(msg->chn))); |
| 6566 | return 1; |
| 6567 | } |
| 6568 | |
| 6569 | /* Returns true if the HTTP message EOM was received */ |
| 6570 | __LJMP static int hlua_http_msg_is_eom(lua_State *L) |
| 6571 | { |
| 6572 | struct http_msg *msg; |
| 6573 | struct htx *htx; |
| 6574 | |
| 6575 | MAY_LJMP(check_args(L, 1, "may_recv")); |
| 6576 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6577 | htx = htxbuf(&msg->chn->buf); |
| 6578 | lua_pushboolean(L, !htx_expect_more(htx)); |
| 6579 | return 1; |
| 6580 | } |
| 6581 | |
| 6582 | /* Returns the number of bytes available in the input side of the HTTP |
| 6583 | * message. This function never fails. |
| 6584 | */ |
| 6585 | __LJMP static int hlua_http_msg_get_in_len(lua_State *L) |
| 6586 | { |
| 6587 | struct http_msg *msg; |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 6588 | size_t output, input; |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6589 | |
| 6590 | MAY_LJMP(check_args(L, 1, "input")); |
| 6591 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 6592 | hlua_http_msg_filter(L, 1, msg, &output, &input); |
| 6593 | lua_pushinteger(L, input); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6594 | return 1; |
| 6595 | } |
| 6596 | |
| 6597 | /* Returns the number of bytes available in the output side of the HTTP |
| 6598 | * message. This function never fails. |
| 6599 | */ |
| 6600 | __LJMP static int hlua_http_msg_get_out_len(lua_State *L) |
| 6601 | { |
| 6602 | struct http_msg *msg; |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 6603 | size_t output, input; |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6604 | |
| 6605 | MAY_LJMP(check_args(L, 1, "output")); |
| 6606 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 6607 | hlua_http_msg_filter(L, 1, msg, &output, &input); |
| 6608 | lua_pushinteger(L, output); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6609 | return 1; |
| 6610 | } |
| 6611 | |
| 6612 | /* Copies at most <len> bytes of DATA blocks from the HTTP message <msg> |
| 6613 | * starting at the offset <offset> and put it in a string LUA variables. It |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 6614 | * returns the built string length. It stops on the first non-DATA HTX |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6615 | * block. This function is called during the payload filtering, so the headers |
| 6616 | * are already scheduled for output (from the filter point of view). |
| 6617 | */ |
| 6618 | static int _hlua_http_msg_dup(struct http_msg *msg, lua_State *L, size_t offset, size_t len) |
| 6619 | { |
| 6620 | struct htx *htx = htxbuf(&msg->chn->buf); |
| 6621 | struct htx_blk *blk; |
| 6622 | struct htx_ret htxret; |
| 6623 | luaL_Buffer b; |
| 6624 | int ret = 0; |
| 6625 | |
| 6626 | luaL_buffinit(L, &b); |
| 6627 | htxret = htx_find_offset(htx, offset); |
| 6628 | for (blk = htxret.blk, offset = htxret.ret; blk && len; blk = htx_get_next_blk(htx, blk)) { |
| 6629 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 6630 | struct ist v; |
| 6631 | |
| 6632 | switch (type) { |
| 6633 | case HTX_BLK_UNUSED: |
| 6634 | break; |
| 6635 | |
| 6636 | case HTX_BLK_DATA: |
| 6637 | v = htx_get_blk_value(htx, blk); |
Tim Duesterhus | b113b5c | 2021-09-15 13:58:44 +0200 | [diff] [blame] | 6638 | v = istadv(v, offset); |
Tim Duesterhus | 2471f5c | 2021-11-08 09:05:01 +0100 | [diff] [blame] | 6639 | v = isttrim(v, len); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6640 | |
| 6641 | luaL_addlstring(&b, v.ptr, v.len); |
| 6642 | ret += v.len; |
| 6643 | break; |
| 6644 | |
| 6645 | default: |
vishnu | 0af4bd7 | 2021-10-24 06:46:24 +0530 | [diff] [blame] | 6646 | if (!ret) |
| 6647 | goto no_data; |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6648 | goto end; |
| 6649 | } |
| 6650 | offset = 0; |
| 6651 | } |
| 6652 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6653 | end: |
vishnu | 0af4bd7 | 2021-10-24 06:46:24 +0530 | [diff] [blame] | 6654 | if (!ret && (htx->flags & HTX_FL_EOM)) |
| 6655 | goto no_data; |
| 6656 | luaL_pushresult(&b); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6657 | return ret; |
vishnu | 0af4bd7 | 2021-10-24 06:46:24 +0530 | [diff] [blame] | 6658 | |
| 6659 | no_data: |
| 6660 | /* Remove the empty string and push nil on the stack */ |
| 6661 | lua_pop(L, 1); |
| 6662 | lua_pushnil(L); |
| 6663 | return 0; |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6664 | } |
| 6665 | |
| 6666 | /* Copies the string <str> to the HTTP message <msg> at the offset |
| 6667 | * <offset>. This function returns -1 if data cannot be copied. Otherwise, it |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 6668 | * returns the amount of data written. This function is responsible to update |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6669 | * the filter context. |
| 6670 | */ |
| 6671 | static int _hlua_http_msg_insert(struct http_msg *msg, struct filter *filter, struct ist str, size_t offset) |
| 6672 | { |
| 6673 | struct htx *htx = htx_from_buf(&msg->chn->buf); |
| 6674 | struct htx_ret htxret; |
| 6675 | int /*max, */ret = 0; |
| 6676 | |
| 6677 | /* Nothing to do, just return */ |
| 6678 | if (unlikely(istlen(str) == 0)) |
| 6679 | goto end; |
| 6680 | |
| 6681 | if (istlen(str) > htx_free_data_space(htx)) { |
| 6682 | ret = -1; |
| 6683 | goto end; |
| 6684 | } |
| 6685 | |
| 6686 | htxret = htx_find_offset(htx, offset); |
| 6687 | if (!htxret.blk || htx_get_blk_type(htxret.blk) != HTX_BLK_DATA) { |
| 6688 | if (!htx_add_last_data(htx, str)) |
| 6689 | goto end; |
| 6690 | } |
| 6691 | else { |
| 6692 | struct ist v = htx_get_blk_value(htx, htxret.blk); |
| 6693 | v.ptr += htxret.ret; |
| 6694 | v.len = 0; |
| 6695 | if (!htx_replace_blk_value(htx, htxret.blk, v, str)) |
| 6696 | goto end; |
| 6697 | } |
| 6698 | ret = str.len; |
| 6699 | if (ret) { |
| 6700 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 6701 | flt_update_offsets(filter, msg->chn, ret); |
| 6702 | flt_ctx->cur_len[CHN_IDX(msg->chn)] += ret; |
| 6703 | } |
| 6704 | |
| 6705 | end: |
| 6706 | htx_to_buf(htx, &msg->chn->buf); |
| 6707 | return ret; |
| 6708 | } |
| 6709 | |
| 6710 | /* Helper function removing at most <len> bytes of DATA blocks at the absolute |
| 6711 | * position <offset>. It stops on the first non-DATA HTX block. This function is |
| 6712 | * called during the payload filtering, so the headers are already scheduled for |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 6713 | * output (from the filter point of view). This function is responsible to |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6714 | * update the filter context. |
| 6715 | */ |
| 6716 | static void _hlua_http_msg_delete(struct http_msg *msg, struct filter *filter, size_t offset, size_t len) |
| 6717 | { |
| 6718 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 6719 | struct htx *htx = htx_from_buf(&msg->chn->buf); |
| 6720 | struct htx_blk *blk; |
| 6721 | struct htx_ret htxret; |
| 6722 | size_t ret = 0; |
| 6723 | |
| 6724 | /* Be sure <len> is always the amount of DATA to remove */ |
| 6725 | if (htx->data == offset+len && htx_get_tail_type(htx) == HTX_BLK_DATA) { |
Aurelien DARRAGON | bcbcf98 | 2022-09-28 19:08:15 +0200 | [diff] [blame] | 6726 | /* When htx tail type == HTX_BLK_DATA, no need to take care |
| 6727 | * of special blocks like HTX_BLK_EOT. |
| 6728 | * We simply truncate after offset |
| 6729 | * (truncate targeted blk and discard the following ones) |
| 6730 | */ |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6731 | htx_truncate(htx, offset); |
| 6732 | ret = len; |
| 6733 | goto end; |
| 6734 | } |
| 6735 | |
| 6736 | htxret = htx_find_offset(htx, offset); |
| 6737 | blk = htxret.blk; |
| 6738 | if (htxret.ret) { |
Aurelien DARRAGON | bcbcf98 | 2022-09-28 19:08:15 +0200 | [diff] [blame] | 6739 | /* dealing with offset: we need to trim targeted blk */ |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6740 | struct ist v; |
| 6741 | |
| 6742 | if (htx_get_blk_type(blk) != HTX_BLK_DATA) |
| 6743 | goto end; |
Aurelien DARRAGON | bcbcf98 | 2022-09-28 19:08:15 +0200 | [diff] [blame] | 6744 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6745 | v = htx_get_blk_value(htx, blk); |
Tim Duesterhus | a029d78 | 2022-10-08 12:33:18 +0200 | [diff] [blame] | 6746 | v = istadv(v, htxret.ret); |
Aurelien DARRAGON | bcbcf98 | 2022-09-28 19:08:15 +0200 | [diff] [blame] | 6747 | |
Tim Duesterhus | 2471f5c | 2021-11-08 09:05:01 +0100 | [diff] [blame] | 6748 | v = isttrim(v, len); |
Aurelien DARRAGON | bcbcf98 | 2022-09-28 19:08:15 +0200 | [diff] [blame] | 6749 | /* trimming data in blk: discard everything after the offset |
| 6750 | * (replace 'v' with 'IST_NULL') |
| 6751 | */ |
Tim Duesterhus | b113b5c | 2021-09-15 13:58:44 +0200 | [diff] [blame] | 6752 | blk = htx_replace_blk_value(htx, blk, v, IST_NULL); |
Aurelien DARRAGON | bcbcf98 | 2022-09-28 19:08:15 +0200 | [diff] [blame] | 6753 | if (blk && v.len < len) { |
| 6754 | /* In this case, caller wants to keep removing data, |
| 6755 | * but we need to spare current blk |
| 6756 | * because it was already trimmed |
| 6757 | */ |
| 6758 | blk = htx_get_next_blk(htx, blk); |
| 6759 | } |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6760 | len -= v.len; |
| 6761 | ret += v.len; |
| 6762 | } |
| 6763 | |
| 6764 | |
| 6765 | while (blk && len) { |
Aurelien DARRAGON | bcbcf98 | 2022-09-28 19:08:15 +0200 | [diff] [blame] | 6766 | /* there is more data that needs to be discarded */ |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6767 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 6768 | uint32_t sz = htx_get_blksz(blk); |
| 6769 | |
| 6770 | switch (type) { |
| 6771 | case HTX_BLK_UNUSED: |
| 6772 | break; |
| 6773 | |
| 6774 | case HTX_BLK_DATA: |
| 6775 | if (len < sz) { |
Aurelien DARRAGON | bcbcf98 | 2022-09-28 19:08:15 +0200 | [diff] [blame] | 6776 | /* don't discard whole blk, only part of it |
| 6777 | * (from the beginning) |
| 6778 | */ |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6779 | htx_cut_data_blk(htx, blk, len); |
| 6780 | ret += len; |
| 6781 | goto end; |
| 6782 | } |
| 6783 | break; |
| 6784 | |
| 6785 | default: |
Aurelien DARRAGON | bcbcf98 | 2022-09-28 19:08:15 +0200 | [diff] [blame] | 6786 | /* HTX_BLK_EOT blk won't be removed */ |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6787 | goto end; |
| 6788 | } |
| 6789 | |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 6790 | /* Remove all the data block */ |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6791 | len -= sz; |
| 6792 | ret += sz; |
| 6793 | blk = htx_remove_blk(htx, blk); |
| 6794 | } |
| 6795 | |
| 6796 | end: |
| 6797 | flt_update_offsets(filter, msg->chn, -ret); |
| 6798 | flt_ctx->cur_len[CHN_IDX(msg->chn)] -= ret; |
| 6799 | /* WARNING: we don't call htx_to_buf() on purpose, because we don't want |
| 6800 | * to loose the EOM flag if the message is empty. |
| 6801 | */ |
| 6802 | } |
| 6803 | |
| 6804 | /* Copies input data found in an HTTP message. Unlike the channel function used |
| 6805 | * to duplicate raw data, this one can only be called inside a filter, from |
| 6806 | * http_payload callback. So it cannot yield. An exception is returned if it is |
| 6807 | * called from another callback. If nothing was copied, a nil value is pushed on |
| 6808 | * the stack. |
| 6809 | */ |
| 6810 | __LJMP static int hlua_http_msg_get_body(lua_State *L) |
| 6811 | { |
| 6812 | struct http_msg *msg; |
| 6813 | struct filter *filter; |
| 6814 | size_t output, input; |
| 6815 | int offset, len; |
| 6816 | |
| 6817 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 6818 | WILL_LJMP(luaL_error(L, "'data' expects at most 2 arguments")); |
| 6819 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6820 | |
| 6821 | if (msg->msg_state < HTTP_MSG_DATA) |
| 6822 | WILL_LJMP(lua_error(L)); |
| 6823 | |
| 6824 | filter = hlua_http_msg_filter(L, 1, msg, &output, &input); |
| 6825 | if (!filter || !hlua_filter_from_payload(filter)) |
| 6826 | WILL_LJMP(lua_error(L)); |
| 6827 | |
| 6828 | if (!ci_data(msg->chn) && channel_input_closed(msg->chn)) { |
| 6829 | lua_pushnil(L); |
| 6830 | return 1; |
| 6831 | } |
| 6832 | |
| 6833 | offset = output; |
| 6834 | if (lua_gettop(L) > 1) { |
| 6835 | offset = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 6836 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 6837 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6838 | offset += output; |
| 6839 | if (offset < output || offset > input + output) { |
| 6840 | lua_pushfstring(L, "offset out of range."); |
| 6841 | WILL_LJMP(lua_error(L)); |
| 6842 | } |
| 6843 | } |
| 6844 | len = output + input - offset; |
| 6845 | if (lua_gettop(L) == 3) { |
| 6846 | len = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 6847 | if (!len) |
| 6848 | goto dup; |
| 6849 | if (len == -1) |
| 6850 | len = global.tune.bufsize; |
| 6851 | if (len < 0) { |
| 6852 | lua_pushfstring(L, "length out of range."); |
| 6853 | WILL_LJMP(lua_error(L)); |
| 6854 | } |
| 6855 | } |
| 6856 | |
| 6857 | dup: |
| 6858 | _hlua_http_msg_dup(msg, L, offset, len); |
| 6859 | return 1; |
| 6860 | } |
| 6861 | |
| 6862 | /* Appends a string to the HTTP message, after all existing DATA blocks but |
| 6863 | * before the trailers, if any. It returns the amount of data written or -1 if |
| 6864 | * nothing was copied. Unlike the channel function used to append data, this one |
| 6865 | * can only be called inside a filter, from http_payload callback. So it cannot |
| 6866 | * yield. An exception is returned if it is called from another callback. |
| 6867 | */ |
| 6868 | __LJMP static int hlua_http_msg_append(lua_State *L) |
| 6869 | { |
| 6870 | struct http_msg *msg; |
| 6871 | struct filter *filter; |
| 6872 | const char *str; |
| 6873 | size_t offset, len, sz; |
| 6874 | int ret; |
| 6875 | |
| 6876 | MAY_LJMP(check_args(L, 2, "append")); |
| 6877 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6878 | |
| 6879 | if (msg->msg_state < HTTP_MSG_DATA) |
| 6880 | WILL_LJMP(lua_error(L)); |
| 6881 | |
| 6882 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 6883 | filter = hlua_http_msg_filter(L, 1, msg, &offset, &len); |
| 6884 | if (!filter || !hlua_filter_from_payload(filter)) |
| 6885 | WILL_LJMP(lua_error(L)); |
| 6886 | |
| 6887 | ret = _hlua_http_msg_insert(msg, filter, ist2(str, sz), offset+len); |
| 6888 | lua_pushinteger(L, ret); |
| 6889 | return 1; |
| 6890 | } |
| 6891 | |
| 6892 | /* Prepends a string to the HTTP message, before all existing DATA blocks. It |
| 6893 | * returns the amount of data written or -1 if nothing was copied. Unlike the |
| 6894 | * channel function used to prepend data, this one can only be called inside a |
| 6895 | * filter, from http_payload callback. So it cannot yield. An exception is |
| 6896 | * returned if it is called from another callback. |
| 6897 | */ |
| 6898 | __LJMP static int hlua_http_msg_prepend(lua_State *L) |
| 6899 | { |
| 6900 | struct http_msg *msg; |
| 6901 | struct filter *filter; |
| 6902 | const char *str; |
| 6903 | size_t offset, len, sz; |
| 6904 | int ret; |
| 6905 | |
| 6906 | MAY_LJMP(check_args(L, 2, "prepend")); |
| 6907 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6908 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6909 | if (msg->msg_state < HTTP_MSG_DATA) |
| 6910 | WILL_LJMP(lua_error(L)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6911 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6912 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 6913 | filter = hlua_http_msg_filter(L, 1, msg, &offset, &len); |
| 6914 | if (!filter || !hlua_filter_from_payload(filter)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6915 | WILL_LJMP(lua_error(L)); |
| 6916 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6917 | ret = _hlua_http_msg_insert(msg, filter, ist2(str, sz), offset); |
| 6918 | lua_pushinteger(L, ret); |
| 6919 | return 1; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6920 | } |
| 6921 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6922 | /* Inserts a string to the HTTP message at a given offset. By default the string |
| 6923 | * is appended at the end of DATA blocks. It returns the amount of data written |
| 6924 | * or -1 if nothing was copied. Unlike the channel function used to insert data, |
| 6925 | * this one can only be called inside a filter, from http_payload callback. So |
| 6926 | * it cannot yield. An exception is returned if it is called from another |
| 6927 | * callback. |
| 6928 | */ |
| 6929 | __LJMP static int hlua_http_msg_insert_data(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6930 | { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6931 | struct http_msg *msg; |
| 6932 | struct filter *filter; |
| 6933 | const char *str; |
| 6934 | size_t input, output, sz; |
| 6935 | int offset; |
| 6936 | int ret; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6937 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6938 | if (lua_gettop(L) < 2 || lua_gettop(L) > 3) |
| 6939 | WILL_LJMP(luaL_error(L, "'insert' expects at least 1 argument and at most 2 arguments")); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6940 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6941 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6942 | if (msg->msg_state < HTTP_MSG_DATA) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6943 | WILL_LJMP(lua_error(L)); |
| 6944 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6945 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
Aurelien DARRAGON | 7fdba0a | 2022-09-28 16:03:45 +0200 | [diff] [blame] | 6946 | filter = hlua_http_msg_filter(L, 1, msg, &output, &input); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6947 | if (!filter || !hlua_filter_from_payload(filter)) |
| 6948 | WILL_LJMP(lua_error(L)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6949 | |
Aurelien DARRAGON | 7fdba0a | 2022-09-28 16:03:45 +0200 | [diff] [blame] | 6950 | offset = output; |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6951 | if (lua_gettop(L) > 2) { |
| 6952 | offset = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 6953 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 6954 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6955 | offset += output; |
Aurelien DARRAGON | 7fdba0a | 2022-09-28 16:03:45 +0200 | [diff] [blame] | 6956 | if (offset > output + input) { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6957 | lua_pushfstring(L, "offset out of range."); |
| 6958 | WILL_LJMP(lua_error(L)); |
| 6959 | } |
| 6960 | } |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6961 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6962 | ret = _hlua_http_msg_insert(msg, filter, ist2(str, sz), offset); |
| 6963 | lua_pushinteger(L, ret); |
| 6964 | return 1; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6965 | } |
| 6966 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6967 | /* Removes a given amount of data from the HTTP message at a given offset. By |
| 6968 | * default all DATA blocks are removed. It returns the amount of data |
| 6969 | * removed. Unlike the channel function used to remove data, this one can only |
| 6970 | * be called inside a filter, from http_payload callback. So it cannot yield. An |
| 6971 | * exception is returned if it is called from another callback. |
| 6972 | */ |
| 6973 | __LJMP static int hlua_http_msg_del_data(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6974 | { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6975 | struct http_msg *msg; |
| 6976 | struct filter *filter; |
| 6977 | size_t input, output; |
| 6978 | int offset, len; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6979 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6980 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
Boyang Li | e0c5435 | 2022-05-10 17:47:23 +0000 | [diff] [blame] | 6981 | WILL_LJMP(luaL_error(L, "'remove' expects at most 2 arguments")); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6982 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6983 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6984 | if (msg->msg_state < HTTP_MSG_DATA) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6985 | WILL_LJMP(lua_error(L)); |
| 6986 | |
Aurelien DARRAGON | d7c71b0 | 2022-09-28 15:52:18 +0200 | [diff] [blame] | 6987 | filter = hlua_http_msg_filter(L, 1, msg, &output, &input); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6988 | if (!filter || !hlua_filter_from_payload(filter)) |
| 6989 | WILL_LJMP(lua_error(L)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6990 | |
Aurelien DARRAGON | d7c71b0 | 2022-09-28 15:52:18 +0200 | [diff] [blame] | 6991 | offset = output; |
Boyang Li | e0c5435 | 2022-05-10 17:47:23 +0000 | [diff] [blame] | 6992 | if (lua_gettop(L) > 1) { |
| 6993 | offset = MAY_LJMP(luaL_checkinteger(L, 2)); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6994 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 6995 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6996 | offset += output; |
Aurelien DARRAGON | d7c71b0 | 2022-09-28 15:52:18 +0200 | [diff] [blame] | 6997 | if (offset > output + input) { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6998 | lua_pushfstring(L, "offset out of range."); |
| 6999 | WILL_LJMP(lua_error(L)); |
| 7000 | } |
| 7001 | } |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 7002 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 7003 | len = output + input - offset; |
Boyang Li | e0c5435 | 2022-05-10 17:47:23 +0000 | [diff] [blame] | 7004 | if (lua_gettop(L) == 3) { |
| 7005 | len = MAY_LJMP(luaL_checkinteger(L, 3)); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 7006 | if (!len) |
| 7007 | goto end; |
| 7008 | if (len == -1) |
| 7009 | len = output + input - offset; |
| 7010 | if (len < 0 || offset + len > output + input) { |
| 7011 | lua_pushfstring(L, "length out of range."); |
| 7012 | WILL_LJMP(lua_error(L)); |
| 7013 | } |
| 7014 | } |
| 7015 | |
| 7016 | _hlua_http_msg_delete(msg, filter, offset, len); |
| 7017 | |
| 7018 | end: |
| 7019 | lua_pushinteger(L, len); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7020 | return 1; |
| 7021 | } |
| 7022 | |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 7023 | /* Replaces a given amount of data at the given offset by a string. By default, |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 7024 | * all remaining data are removed, accordingly to the filter context. It returns |
| 7025 | * the amount of data written or -1 if nothing was copied. Unlike the channel |
| 7026 | * function used to replace data, this one can only be called inside a filter, |
| 7027 | * from http_payload callback. So it cannot yield. An exception is returned if |
| 7028 | * it is called from another callback. |
| 7029 | */ |
| 7030 | __LJMP static int hlua_http_msg_set_data(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7031 | { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 7032 | struct http_msg *msg; |
| 7033 | struct filter *filter; |
| 7034 | struct htx *htx; |
| 7035 | const char *str; |
| 7036 | size_t input, output, sz; |
| 7037 | int offset, len; |
| 7038 | int ret; |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 7039 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 7040 | if (lua_gettop(L) < 2 || lua_gettop(L) > 4) |
| 7041 | WILL_LJMP(luaL_error(L, "'set' expects at least 1 argument and at most 3 arguments")); |
| 7042 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 7043 | |
| 7044 | if (msg->msg_state < HTTP_MSG_DATA) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 7045 | WILL_LJMP(lua_error(L)); |
| 7046 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 7047 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 7048 | filter = hlua_http_msg_filter(L, 1, msg, &output, &input); |
| 7049 | if (!filter || !hlua_filter_from_payload(filter)) |
| 7050 | WILL_LJMP(lua_error(L)); |
| 7051 | |
| 7052 | offset = output; |
| 7053 | if (lua_gettop(L) > 2) { |
| 7054 | offset = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 7055 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 7056 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 7057 | offset += output; |
| 7058 | if (offset < output || offset > input + output) { |
| 7059 | lua_pushfstring(L, "offset out of range."); |
| 7060 | WILL_LJMP(lua_error(L)); |
| 7061 | } |
| 7062 | } |
| 7063 | |
| 7064 | len = output + input - offset; |
| 7065 | if (lua_gettop(L) == 4) { |
| 7066 | len = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 7067 | if (!len) |
| 7068 | goto set; |
| 7069 | if (len == -1) |
| 7070 | len = output + input - offset; |
| 7071 | if (len < 0 || offset + len > output + input) { |
| 7072 | lua_pushfstring(L, "length out of range."); |
| 7073 | WILL_LJMP(lua_error(L)); |
| 7074 | } |
| 7075 | } |
| 7076 | |
| 7077 | set: |
| 7078 | /* Be sure we can copied the string once input data will be removed. */ |
| 7079 | htx = htx_from_buf(&msg->chn->buf); |
| 7080 | if (sz > htx_free_data_space(htx) + len) |
| 7081 | lua_pushinteger(L, -1); |
| 7082 | else { |
| 7083 | _hlua_http_msg_delete(msg, filter, offset, len); |
| 7084 | ret = _hlua_http_msg_insert(msg, filter, ist2(str, sz), offset); |
| 7085 | lua_pushinteger(L, ret); |
| 7086 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7087 | return 1; |
| 7088 | } |
| 7089 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 7090 | /* Prepends data into an HTTP message and forward it, from the filter point of |
| 7091 | * view. It returns the amount of data written or -1 if nothing was sent. Unlike |
| 7092 | * the channel function used to send data, this one can only be called inside a |
| 7093 | * filter, from http_payload callback. So it cannot yield. An exception is |
| 7094 | * returned if it is called from another callback. |
| 7095 | */ |
| 7096 | __LJMP static int hlua_http_msg_send(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7097 | { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 7098 | struct http_msg *msg; |
| 7099 | struct filter *filter; |
| 7100 | struct htx *htx; |
| 7101 | const char *str; |
| 7102 | size_t offset, len, sz; |
| 7103 | int ret; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7104 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 7105 | MAY_LJMP(check_args(L, 2, "send")); |
| 7106 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 7107 | |
| 7108 | if (msg->msg_state < HTTP_MSG_DATA) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 7109 | WILL_LJMP(lua_error(L)); |
| 7110 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 7111 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 7112 | filter = hlua_http_msg_filter(L, 1, msg, &offset, &len); |
| 7113 | if (!filter || !hlua_filter_from_payload(filter)) |
| 7114 | WILL_LJMP(lua_error(L)); |
| 7115 | |
| 7116 | /* Return an error if the channel's output is closed */ |
| 7117 | if (unlikely(channel_output_closed(msg->chn))) { |
| 7118 | lua_pushinteger(L, -1); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7119 | return 1; |
| 7120 | } |
| 7121 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 7122 | htx = htx_from_buf(&msg->chn->buf); |
| 7123 | if (sz > htx_free_data_space(htx)) { |
| 7124 | lua_pushinteger(L, -1); |
| 7125 | return 1; |
| 7126 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7127 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 7128 | ret = _hlua_http_msg_insert(msg, filter, ist2(str, sz), offset); |
| 7129 | if (ret > 0) { |
| 7130 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 7131 | |
| 7132 | FLT_OFF(filter, msg->chn) += ret; |
| 7133 | flt_ctx->cur_len[CHN_IDX(msg->chn)] -= ret; |
| 7134 | flt_ctx->cur_off[CHN_IDX(msg->chn)] += ret; |
| 7135 | } |
| 7136 | |
| 7137 | lua_pushinteger(L, ret); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7138 | return 1; |
| 7139 | } |
| 7140 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 7141 | /* Forwards a given amount of bytes. It return -1 if the channel's output is |
| 7142 | * closed. Otherwise, it returns the number of bytes forwarded. Unlike the |
| 7143 | * channel function used to forward data, this one can only be called inside a |
| 7144 | * filter, from http_payload callback. So it cannot yield. An exception is |
| 7145 | * returned if it is called from another callback. All other functions deal with |
| 7146 | * DATA block, this one not. |
| 7147 | */ |
| 7148 | __LJMP static int hlua_http_msg_forward(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7149 | { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 7150 | struct http_msg *msg; |
| 7151 | struct filter *filter; |
| 7152 | size_t offset, len; |
| 7153 | int fwd, ret = 0; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7154 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 7155 | MAY_LJMP(check_args(L, 2, "forward")); |
| 7156 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 7157 | |
| 7158 | if (msg->msg_state < HTTP_MSG_DATA) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 7159 | WILL_LJMP(lua_error(L)); |
| 7160 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 7161 | fwd = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 7162 | filter = hlua_http_msg_filter(L, 1, msg, &offset, &len); |
| 7163 | if (!filter || !hlua_filter_from_payload(filter)) |
| 7164 | WILL_LJMP(lua_error(L)); |
| 7165 | |
| 7166 | /* Nothing to do, just return */ |
| 7167 | if (!fwd) |
| 7168 | goto end; |
| 7169 | |
| 7170 | /* Return an error if the channel's output is closed */ |
| 7171 | if (unlikely(channel_output_closed(msg->chn))) { |
| 7172 | ret = -1; |
| 7173 | goto end; |
| 7174 | } |
| 7175 | |
| 7176 | ret = fwd; |
| 7177 | if (ret > len) |
| 7178 | ret = len; |
| 7179 | |
| 7180 | if (ret) { |
| 7181 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 7182 | |
| 7183 | FLT_OFF(filter, msg->chn) += ret; |
| 7184 | flt_ctx->cur_off[CHN_IDX(msg->chn)] += ret; |
| 7185 | flt_ctx->cur_len[CHN_IDX(msg->chn)] -= ret; |
| 7186 | } |
| 7187 | |
| 7188 | end: |
| 7189 | lua_pushinteger(L, ret); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7190 | return 1; |
| 7191 | } |
| 7192 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 7193 | /* Set EOM flag on the HTX message. |
| 7194 | * |
| 7195 | * NOTE: Not sure it is a good idea to manipulate this flag but for now I don't |
| 7196 | * really know how to do without this feature. |
| 7197 | */ |
| 7198 | __LJMP static int hlua_http_msg_set_eom(lua_State *L) |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 7199 | { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 7200 | struct http_msg *msg; |
| 7201 | struct htx *htx; |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 7202 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 7203 | MAY_LJMP(check_args(L, 1, "set_eom")); |
| 7204 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 7205 | htx = htxbuf(&msg->chn->buf); |
| 7206 | htx->flags |= HTX_FL_EOM; |
| 7207 | return 0; |
| 7208 | } |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 7209 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 7210 | /* Unset EOM flag on the HTX message. |
| 7211 | * |
| 7212 | * NOTE: Not sure it is a good idea to manipulate this flag but for now I don't |
| 7213 | * really know how to do without this feature. |
| 7214 | */ |
| 7215 | __LJMP static int hlua_http_msg_unset_eom(lua_State *L) |
| 7216 | { |
| 7217 | struct http_msg *msg; |
| 7218 | struct htx *htx; |
| 7219 | |
| 7220 | MAY_LJMP(check_args(L, 1, "set_eom")); |
| 7221 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 7222 | htx = htxbuf(&msg->chn->buf); |
| 7223 | htx->flags &= ~HTX_FL_EOM; |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 7224 | return 0; |
| 7225 | } |
| 7226 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7227 | /* |
| 7228 | * |
| 7229 | * |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7230 | * Class HTTPClient |
| 7231 | * |
| 7232 | * |
| 7233 | */ |
| 7234 | __LJMP static struct hlua_httpclient *hlua_checkhttpclient(lua_State *L, int ud) |
| 7235 | { |
| 7236 | return MAY_LJMP(hlua_checkudata(L, ud, class_httpclient_ref)); |
| 7237 | } |
| 7238 | |
William Lallemand | f77f1de | 2021-09-28 19:10:38 +0200 | [diff] [blame] | 7239 | |
| 7240 | /* stops the httpclient and ask it to kill itself */ |
| 7241 | __LJMP static int hlua_httpclient_gc(lua_State *L) |
| 7242 | { |
| 7243 | struct hlua_httpclient *hlua_hc; |
| 7244 | |
| 7245 | MAY_LJMP(check_args(L, 1, "__gc")); |
| 7246 | |
| 7247 | hlua_hc = MAY_LJMP(hlua_checkhttpclient(L, 1)); |
| 7248 | |
Aurelien DARRAGON | 3ffbf38 | 2023-02-09 17:02:57 +0100 | [diff] [blame] | 7249 | if (MT_LIST_DELETE(&hlua_hc->by_hlua)) { |
| 7250 | /* we won the race against hlua_httpclient_destroy_all() */ |
William Lallemand | bb58142 | 2022-10-20 10:57:28 +0200 | [diff] [blame] | 7251 | httpclient_stop_and_destroy(hlua_hc->hc); |
Aurelien DARRAGON | 3ffbf38 | 2023-02-09 17:02:57 +0100 | [diff] [blame] | 7252 | hlua_hc->hc = NULL; |
| 7253 | } |
William Lallemand | f77f1de | 2021-09-28 19:10:38 +0200 | [diff] [blame] | 7254 | |
| 7255 | return 0; |
| 7256 | } |
| 7257 | |
| 7258 | |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7259 | __LJMP static int hlua_httpclient_new(lua_State *L) |
| 7260 | { |
| 7261 | struct hlua_httpclient *hlua_hc; |
| 7262 | struct hlua *hlua; |
| 7263 | |
| 7264 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 7265 | hlua = hlua_gethlua(L); |
| 7266 | if (!hlua) |
| 7267 | return 0; |
| 7268 | |
| 7269 | /* Check stack size. */ |
| 7270 | if (!lua_checkstack(L, 3)) { |
| 7271 | hlua_pusherror(L, "httpclient: full stack"); |
| 7272 | goto err; |
| 7273 | } |
| 7274 | /* Create the object: obj[0] = userdata. */ |
| 7275 | lua_newtable(L); |
| 7276 | hlua_hc = MAY_LJMP(lua_newuserdata(L, sizeof(*hlua_hc))); |
| 7277 | lua_rawseti(L, -2, 0); |
| 7278 | memset(hlua_hc, 0, sizeof(*hlua_hc)); |
| 7279 | |
| 7280 | hlua_hc->hc = httpclient_new(hlua, 0, IST_NULL); |
| 7281 | if (!hlua_hc->hc) |
| 7282 | goto err; |
| 7283 | |
Aurelien DARRAGON | 3ffbf38 | 2023-02-09 17:02:57 +0100 | [diff] [blame] | 7284 | MT_LIST_APPEND(&hlua->hc_list, &hlua_hc->by_hlua); |
William Lallemand | bb58142 | 2022-10-20 10:57:28 +0200 | [diff] [blame] | 7285 | |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7286 | /* Pop a class stream metatable and affect it to the userdata. */ |
| 7287 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_httpclient_ref); |
| 7288 | lua_setmetatable(L, -2); |
| 7289 | |
| 7290 | return 1; |
| 7291 | |
| 7292 | err: |
| 7293 | WILL_LJMP(lua_error(L)); |
| 7294 | return 0; |
| 7295 | } |
| 7296 | |
| 7297 | |
| 7298 | /* |
| 7299 | * Callback of the httpclient, this callback wakes the lua task up, once the |
| 7300 | * httpclient receives some data |
| 7301 | * |
| 7302 | */ |
| 7303 | |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7304 | static void hlua_httpclient_cb(struct httpclient *hc) |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7305 | { |
| 7306 | struct hlua *hlua = hc->caller; |
| 7307 | |
| 7308 | if (!hlua || !hlua->task) |
| 7309 | return; |
| 7310 | |
| 7311 | task_wakeup(hlua->task, TASK_WOKEN_MSG); |
| 7312 | } |
| 7313 | |
| 7314 | /* |
William Lallemand | d7df73a | 2021-09-23 17:54:00 +0200 | [diff] [blame] | 7315 | * Fill the lua stack with headers from the httpclient response |
| 7316 | * This works the same way as the hlua_http_get_headers() function |
| 7317 | */ |
| 7318 | __LJMP static int hlua_httpclient_get_headers(lua_State *L, struct hlua_httpclient *hlua_hc) |
| 7319 | { |
| 7320 | struct http_hdr *hdr; |
| 7321 | |
| 7322 | lua_newtable(L); |
| 7323 | |
William Lallemand | ef574b2 | 2021-10-05 16:19:31 +0200 | [diff] [blame] | 7324 | for (hdr = hlua_hc->hc->res.hdrs; hdr && isttest(hdr->n); hdr++) { |
William Lallemand | d7df73a | 2021-09-23 17:54:00 +0200 | [diff] [blame] | 7325 | struct ist n, v; |
| 7326 | int len; |
| 7327 | |
| 7328 | n = hdr->n; |
| 7329 | v = hdr->v; |
| 7330 | |
| 7331 | /* Check for existing entry: |
| 7332 | * assume that the table is on the top of the stack, and |
| 7333 | * push the key in the stack, the function lua_gettable() |
| 7334 | * perform the lookup. |
| 7335 | */ |
| 7336 | |
| 7337 | lua_pushlstring(L, n.ptr, n.len); |
| 7338 | lua_gettable(L, -2); |
| 7339 | |
| 7340 | switch (lua_type(L, -1)) { |
| 7341 | case LUA_TNIL: |
| 7342 | /* Table not found, create it. */ |
| 7343 | lua_pop(L, 1); /* remove the nil value. */ |
| 7344 | lua_pushlstring(L, n.ptr, n.len); /* push the header name as key. */ |
| 7345 | lua_newtable(L); /* create and push empty table. */ |
| 7346 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 7347 | lua_rawseti(L, -2, 0); /* index header value (pop it). */ |
| 7348 | lua_rawset(L, -3); /* index new table with header name (pop the values). */ |
| 7349 | break; |
| 7350 | |
| 7351 | case LUA_TTABLE: |
| 7352 | /* Entry found: push the value in the table. */ |
| 7353 | len = lua_rawlen(L, -1); |
| 7354 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 7355 | lua_rawseti(L, -2, len+1); /* index header value (pop it). */ |
| 7356 | lua_pop(L, 1); /* remove the table (it is stored in the main table). */ |
| 7357 | break; |
| 7358 | |
| 7359 | default: |
| 7360 | /* Other cases are errors. */ |
| 7361 | hlua_pusherror(L, "internal error during the parsing of headers."); |
| 7362 | WILL_LJMP(lua_error(L)); |
| 7363 | } |
| 7364 | } |
| 7365 | return 1; |
| 7366 | } |
| 7367 | |
| 7368 | /* |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7369 | * Allocate and return an array of http_hdr ist extracted from the <headers> lua table |
| 7370 | * |
| 7371 | * Caller must free the result |
| 7372 | */ |
| 7373 | struct http_hdr *hlua_httpclient_table_to_hdrs(lua_State *L) |
| 7374 | { |
| 7375 | struct http_hdr hdrs[global.tune.max_http_hdr]; |
| 7376 | struct http_hdr *result = NULL; |
| 7377 | uint32_t hdr_num = 0; |
| 7378 | |
| 7379 | lua_pushnil(L); |
| 7380 | while (lua_next(L, -2) != 0) { |
| 7381 | struct ist name, value; |
| 7382 | const char *n, *v; |
| 7383 | size_t nlen, vlen; |
| 7384 | |
| 7385 | if (!lua_isstring(L, -2) || !lua_istable(L, -1)) { |
| 7386 | /* Skip element if the key is not a string or if the value is not a table */ |
| 7387 | goto next_hdr; |
| 7388 | } |
| 7389 | |
| 7390 | n = lua_tolstring(L, -2, &nlen); |
| 7391 | name = ist2(n, nlen); |
| 7392 | |
| 7393 | /* Loop on header's values */ |
| 7394 | lua_pushnil(L); |
| 7395 | while (lua_next(L, -2)) { |
| 7396 | if (!lua_isstring(L, -1)) { |
| 7397 | /* Skip the value if it is not a string */ |
| 7398 | goto next_value; |
| 7399 | } |
| 7400 | |
| 7401 | v = lua_tolstring(L, -1, &vlen); |
| 7402 | value = ist2(v, vlen); |
| 7403 | name = ist2(n, nlen); |
| 7404 | |
| 7405 | hdrs[hdr_num].n = istdup(name); |
| 7406 | hdrs[hdr_num].v = istdup(value); |
| 7407 | |
| 7408 | hdr_num++; |
| 7409 | |
| 7410 | next_value: |
| 7411 | lua_pop(L, 1); |
| 7412 | } |
| 7413 | |
| 7414 | next_hdr: |
| 7415 | lua_pop(L, 1); |
| 7416 | |
| 7417 | } |
| 7418 | |
| 7419 | if (hdr_num) { |
| 7420 | /* alloc and copy the headers in the httpclient struct */ |
Tim Duesterhus | 16cc16d | 2021-11-06 15:14:45 +0100 | [diff] [blame] | 7421 | result = calloc((hdr_num + 1), sizeof(*result)); |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7422 | if (!result) |
| 7423 | goto skip_headers; |
| 7424 | memcpy(result, hdrs, sizeof(struct http_hdr) * (hdr_num + 1)); |
| 7425 | |
| 7426 | result[hdr_num].n = IST_NULL; |
| 7427 | result[hdr_num].v = IST_NULL; |
| 7428 | } |
| 7429 | |
| 7430 | skip_headers: |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7431 | |
| 7432 | return result; |
| 7433 | } |
| 7434 | |
| 7435 | |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7436 | /* |
| 7437 | * For each yield, checks if there is some data in the httpclient and push them |
| 7438 | * in the lua buffer, once the httpclient finished its job, push the result on |
| 7439 | * the stack |
| 7440 | */ |
| 7441 | __LJMP static int hlua_httpclient_rcv_yield(lua_State *L, int status, lua_KContext ctx) |
| 7442 | { |
| 7443 | struct buffer *tr; |
| 7444 | int res; |
| 7445 | struct hlua *hlua = hlua_gethlua(L); |
| 7446 | struct hlua_httpclient *hlua_hc = hlua_checkhttpclient(L, 1); |
| 7447 | |
| 7448 | |
| 7449 | tr = get_trash_chunk(); |
| 7450 | |
| 7451 | res = httpclient_res_xfer(hlua_hc->hc, tr); |
| 7452 | luaL_addlstring(&hlua_hc->b, b_orig(tr), res); |
| 7453 | |
| 7454 | if (!httpclient_data(hlua_hc->hc) && httpclient_ended(hlua_hc->hc)) { |
| 7455 | |
| 7456 | luaL_pushresult(&hlua_hc->b); |
| 7457 | lua_settable(L, -3); |
| 7458 | |
| 7459 | lua_pushstring(L, "status"); |
| 7460 | lua_pushinteger(L, hlua_hc->hc->res.status); |
| 7461 | lua_settable(L, -3); |
| 7462 | |
| 7463 | |
| 7464 | lua_pushstring(L, "reason"); |
| 7465 | lua_pushlstring(L, hlua_hc->hc->res.reason.ptr, hlua_hc->hc->res.reason.len); |
| 7466 | lua_settable(L, -3); |
| 7467 | |
| 7468 | lua_pushstring(L, "headers"); |
| 7469 | hlua_httpclient_get_headers(L, hlua_hc); |
| 7470 | lua_settable(L, -3); |
| 7471 | |
| 7472 | return 1; |
| 7473 | } |
| 7474 | |
| 7475 | if (httpclient_data(hlua_hc->hc)) |
| 7476 | task_wakeup(hlua->task, TASK_WOKEN_MSG); |
| 7477 | |
| 7478 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_httpclient_rcv_yield, TICK_ETERNITY, 0)); |
| 7479 | |
| 7480 | return 0; |
| 7481 | } |
| 7482 | |
| 7483 | /* |
| 7484 | * Call this when trying to stream a body during a request |
| 7485 | */ |
| 7486 | __LJMP static int hlua_httpclient_snd_yield(lua_State *L, int status, lua_KContext ctx) |
| 7487 | { |
| 7488 | struct hlua *hlua; |
| 7489 | struct hlua_httpclient *hlua_hc = hlua_checkhttpclient(L, 1); |
| 7490 | const char *body_str = NULL; |
| 7491 | int ret; |
| 7492 | int end = 0; |
| 7493 | size_t buf_len; |
| 7494 | size_t to_send = 0; |
| 7495 | |
| 7496 | hlua = hlua_gethlua(L); |
| 7497 | |
| 7498 | if (!hlua || !hlua->task) |
| 7499 | WILL_LJMP(luaL_error(L, "The 'get' function is only allowed in " |
| 7500 | "'frontend', 'backend' or 'task'")); |
| 7501 | |
| 7502 | ret = lua_getfield(L, -1, "body"); |
| 7503 | if (ret != LUA_TSTRING) |
| 7504 | goto rcv; |
| 7505 | |
| 7506 | body_str = lua_tolstring(L, -1, &buf_len); |
| 7507 | lua_pop(L, 1); |
| 7508 | |
Christopher Faulet | fc59129 | 2022-01-12 14:46:03 +0100 | [diff] [blame] | 7509 | to_send = buf_len - hlua_hc->sent; |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7510 | |
| 7511 | if ((hlua_hc->sent + to_send) >= buf_len) |
| 7512 | end = 1; |
| 7513 | |
| 7514 | /* the end flag is always set since we are using the whole remaining size */ |
| 7515 | hlua_hc->sent += httpclient_req_xfer(hlua_hc->hc, ist2(body_str + hlua_hc->sent, to_send), end); |
| 7516 | |
| 7517 | if (buf_len > hlua_hc->sent) { |
| 7518 | /* still need to process the buffer */ |
| 7519 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_httpclient_snd_yield, TICK_ETERNITY, 0)); |
| 7520 | } else { |
| 7521 | goto rcv; |
| 7522 | /* we sent the whole request buffer we can recv */ |
| 7523 | } |
| 7524 | return 0; |
| 7525 | |
| 7526 | rcv: |
| 7527 | |
| 7528 | /* we return a "res" object */ |
| 7529 | lua_newtable(L); |
| 7530 | |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7531 | lua_pushstring(L, "body"); |
William Lallemand | d1187eb | 2021-11-02 10:40:06 +0100 | [diff] [blame] | 7532 | luaL_buffinit(L, &hlua_hc->b); |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7533 | |
William Lallemand | 933fe39 | 2021-11-04 09:45:58 +0100 | [diff] [blame] | 7534 | task_wakeup(hlua->task, TASK_WOKEN_MSG); |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7535 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_httpclient_rcv_yield, TICK_ETERNITY, 0)); |
| 7536 | |
| 7537 | return 1; |
| 7538 | } |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7539 | |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7540 | /* |
William Lallemand | dc2cc90 | 2021-10-26 11:43:26 +0200 | [diff] [blame] | 7541 | * Send an HTTP request and wait for a response |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7542 | */ |
| 7543 | |
William Lallemand | dc2cc90 | 2021-10-26 11:43:26 +0200 | [diff] [blame] | 7544 | __LJMP static int hlua_httpclient_send(lua_State *L, enum http_meth_t meth) |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7545 | { |
| 7546 | struct hlua_httpclient *hlua_hc; |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7547 | struct http_hdr *hdrs = NULL; |
| 7548 | struct http_hdr *hdrs_i = NULL; |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7549 | struct hlua *hlua; |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7550 | const char *url_str = NULL; |
William Lallemand | dec25c3 | 2021-10-25 19:48:37 +0200 | [diff] [blame] | 7551 | const char *body_str = NULL; |
Christopher Faulet | 380ae9c | 2022-10-14 14:57:04 +0200 | [diff] [blame] | 7552 | size_t buf_len = 0; |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7553 | int ret; |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7554 | |
| 7555 | hlua = hlua_gethlua(L); |
| 7556 | |
| 7557 | if (!hlua || !hlua->task) |
| 7558 | WILL_LJMP(luaL_error(L, "The 'get' function is only allowed in " |
| 7559 | "'frontend', 'backend' or 'task'")); |
| 7560 | |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7561 | if (lua_gettop(L) != 2 || lua_type(L, -1) != LUA_TTABLE) |
| 7562 | WILL_LJMP(luaL_error(L, "'get' needs a table as argument")); |
| 7563 | |
William Lallemand | 4f4f2b7 | 2022-02-17 20:00:23 +0100 | [diff] [blame] | 7564 | hlua_hc = hlua_checkhttpclient(L, 1); |
| 7565 | |
William Lallemand | 7177a95 | 2022-03-03 15:33:12 +0100 | [diff] [blame] | 7566 | lua_pushnil(L); /* first key */ |
| 7567 | while (lua_next(L, 2)) { |
| 7568 | if (strcmp(lua_tostring(L, -2), "dst") == 0) { |
| 7569 | if (httpclient_set_dst(hlua_hc->hc, lua_tostring(L, -1)) < 0) |
| 7570 | WILL_LJMP(luaL_error(L, "Can't use the 'dst' argument")); |
William Lallemand | 4f4f2b7 | 2022-02-17 20:00:23 +0100 | [diff] [blame] | 7571 | |
William Lallemand | 7177a95 | 2022-03-03 15:33:12 +0100 | [diff] [blame] | 7572 | } else if (strcmp(lua_tostring(L, -2), "url") == 0) { |
| 7573 | if (lua_type(L, -1) != LUA_TSTRING) |
| 7574 | WILL_LJMP(luaL_error(L, "invalid parameter in 'url', must be a string")); |
| 7575 | url_str = lua_tostring(L, -1); |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7576 | |
William Lallemand | 7177a95 | 2022-03-03 15:33:12 +0100 | [diff] [blame] | 7577 | } else if (strcmp(lua_tostring(L, -2), "timeout") == 0) { |
| 7578 | if (lua_type(L, -1) != LUA_TNUMBER) |
| 7579 | WILL_LJMP(luaL_error(L, "invalid parameter in 'timeout', must be a number")); |
| 7580 | httpclient_set_timeout(hlua_hc->hc, lua_tointeger(L, -1)); |
William Lallemand | b4a4ef6 | 2022-02-23 14:18:16 +0100 | [diff] [blame] | 7581 | |
William Lallemand | 7177a95 | 2022-03-03 15:33:12 +0100 | [diff] [blame] | 7582 | } else if (strcmp(lua_tostring(L, -2), "headers") == 0) { |
| 7583 | if (lua_type(L, -1) != LUA_TTABLE) |
| 7584 | WILL_LJMP(luaL_error(L, "invalid parameter in 'headers', must be a table")); |
| 7585 | hdrs = hlua_httpclient_table_to_hdrs(L); |
William Lallemand | 79416cb | 2021-09-24 14:51:44 +0200 | [diff] [blame] | 7586 | |
William Lallemand | 7177a95 | 2022-03-03 15:33:12 +0100 | [diff] [blame] | 7587 | } else if (strcmp(lua_tostring(L, -2), "body") == 0) { |
| 7588 | if (lua_type(L, -1) != LUA_TSTRING) |
| 7589 | WILL_LJMP(luaL_error(L, "invalid parameter in 'body', must be a string")); |
| 7590 | body_str = lua_tolstring(L, -1, &buf_len); |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7591 | |
William Lallemand | 7177a95 | 2022-03-03 15:33:12 +0100 | [diff] [blame] | 7592 | } else { |
| 7593 | WILL_LJMP(luaL_error(L, "'%s' invalid parameter name", lua_tostring(L, -2))); |
| 7594 | } |
| 7595 | /* removes 'value'; keeps 'key' for next iteration */ |
| 7596 | lua_pop(L, 1); |
| 7597 | } |
William Lallemand | dec25c3 | 2021-10-25 19:48:37 +0200 | [diff] [blame] | 7598 | |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7599 | if (!url_str) { |
| 7600 | WILL_LJMP(luaL_error(L, "'get' need a 'url' argument")); |
| 7601 | return 0; |
| 7602 | } |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7603 | |
William Lallemand | 10a3736 | 2022-03-02 16:18:26 +0100 | [diff] [blame] | 7604 | hlua_hc->sent = 0; |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7605 | |
Aurelien DARRAGON | 0356407 | 2023-02-09 15:26:25 +0100 | [diff] [blame] | 7606 | istfree(&hlua_hc->hc->req.url); |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7607 | hlua_hc->hc->req.url = istdup(ist(url_str)); |
William Lallemand | dc2cc90 | 2021-10-26 11:43:26 +0200 | [diff] [blame] | 7608 | hlua_hc->hc->req.meth = meth; |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7609 | |
| 7610 | /* update the httpclient callbacks */ |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7611 | hlua_hc->hc->ops.res_stline = hlua_httpclient_cb; |
| 7612 | hlua_hc->hc->ops.res_headers = hlua_httpclient_cb; |
| 7613 | hlua_hc->hc->ops.res_payload = hlua_httpclient_cb; |
William Lallemand | 8f170c7 | 2022-03-15 10:52:07 +0100 | [diff] [blame] | 7614 | hlua_hc->hc->ops.res_end = hlua_httpclient_cb; |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7615 | |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7616 | /* a body is available, it will use the request callback */ |
Christopher Faulet | 380ae9c | 2022-10-14 14:57:04 +0200 | [diff] [blame] | 7617 | if (body_str && buf_len) { |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7618 | hlua_hc->hc->ops.req_payload = hlua_httpclient_cb; |
| 7619 | } |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7620 | |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7621 | ret = httpclient_req_gen(hlua_hc->hc, hlua_hc->hc->req.url, meth, hdrs, IST_NULL); |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7622 | |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7623 | /* free the temporary headers array */ |
| 7624 | hdrs_i = hdrs; |
| 7625 | while (hdrs_i && isttest(hdrs_i->n)) { |
| 7626 | istfree(&hdrs_i->n); |
| 7627 | istfree(&hdrs_i->v); |
| 7628 | hdrs_i++; |
| 7629 | } |
| 7630 | ha_free(&hdrs); |
| 7631 | |
| 7632 | |
William Lallemand | 6137a9e | 2021-10-26 15:01:53 +0200 | [diff] [blame] | 7633 | if (ret != ERR_NONE) { |
| 7634 | WILL_LJMP(luaL_error(L, "Can't generate the HTTP request")); |
| 7635 | return 0; |
| 7636 | } |
| 7637 | |
William Lallemand | c2d3db4 | 2022-04-26 11:46:13 +0200 | [diff] [blame] | 7638 | if (!httpclient_start(hlua_hc->hc)) |
| 7639 | WILL_LJMP(luaL_error(L, "couldn't start the httpclient")); |
William Lallemand | 6137a9e | 2021-10-26 15:01:53 +0200 | [diff] [blame] | 7640 | |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7641 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_httpclient_snd_yield, TICK_ETERNITY, 0)); |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7642 | |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7643 | return 0; |
| 7644 | } |
| 7645 | |
| 7646 | /* |
William Lallemand | dc2cc90 | 2021-10-26 11:43:26 +0200 | [diff] [blame] | 7647 | * Sends an HTTP HEAD request and wait for a response |
| 7648 | * |
| 7649 | * httpclient:head(url, headers, payload) |
| 7650 | */ |
| 7651 | __LJMP static int hlua_httpclient_head(lua_State *L) |
| 7652 | { |
| 7653 | return hlua_httpclient_send(L, HTTP_METH_HEAD); |
| 7654 | } |
| 7655 | |
| 7656 | /* |
| 7657 | * Send an HTTP GET request and wait for a response |
| 7658 | * |
| 7659 | * httpclient:get(url, headers, payload) |
| 7660 | */ |
| 7661 | __LJMP static int hlua_httpclient_get(lua_State *L) |
| 7662 | { |
| 7663 | return hlua_httpclient_send(L, HTTP_METH_GET); |
| 7664 | |
| 7665 | } |
| 7666 | |
| 7667 | /* |
| 7668 | * Sends an HTTP PUT request and wait for a response |
| 7669 | * |
| 7670 | * httpclient:put(url, headers, payload) |
| 7671 | */ |
| 7672 | __LJMP static int hlua_httpclient_put(lua_State *L) |
| 7673 | { |
| 7674 | return hlua_httpclient_send(L, HTTP_METH_PUT); |
| 7675 | } |
| 7676 | |
| 7677 | /* |
| 7678 | * Send an HTTP POST request and wait for a response |
| 7679 | * |
| 7680 | * httpclient:post(url, headers, payload) |
| 7681 | */ |
| 7682 | __LJMP static int hlua_httpclient_post(lua_State *L) |
| 7683 | { |
| 7684 | return hlua_httpclient_send(L, HTTP_METH_POST); |
| 7685 | } |
| 7686 | |
| 7687 | |
| 7688 | /* |
| 7689 | * Sends an HTTP DELETE request and wait for a response |
| 7690 | * |
| 7691 | * httpclient:delete(url, headers, payload) |
| 7692 | */ |
| 7693 | __LJMP static int hlua_httpclient_delete(lua_State *L) |
| 7694 | { |
| 7695 | return hlua_httpclient_send(L, HTTP_METH_DELETE); |
| 7696 | } |
| 7697 | |
| 7698 | /* |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7699 | * |
| 7700 | * |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7701 | * Class TXN |
| 7702 | * |
| 7703 | * |
| 7704 | */ |
| 7705 | |
| 7706 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7707 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7708 | */ |
| 7709 | __LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud) |
| 7710 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7711 | return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref)); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7712 | } |
| 7713 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 7714 | __LJMP static int hlua_set_var(lua_State *L) |
| 7715 | { |
| 7716 | struct hlua_txn *htxn; |
| 7717 | const char *name; |
| 7718 | size_t len; |
| 7719 | struct sample smp; |
| 7720 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 7721 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 7722 | 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] | 7723 | |
| 7724 | /* It is useles to retrieve the stream, but this function |
| 7725 | * runs only in a stream context. |
| 7726 | */ |
| 7727 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7728 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 7729 | |
| 7730 | /* Converts the third argument in a sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 7731 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 7732 | hlua_lua2smp(L, 3, &smp); |
| 7733 | |
| 7734 | /* Store the sample in a variable. */ |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 7735 | 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] | 7736 | |
| 7737 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 7738 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 7739 | else |
| 7740 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 7741 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 7742 | return 1; |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 7743 | } |
| 7744 | |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 7745 | __LJMP static int hlua_unset_var(lua_State *L) |
| 7746 | { |
| 7747 | struct hlua_txn *htxn; |
| 7748 | const char *name; |
| 7749 | size_t len; |
| 7750 | struct sample smp; |
| 7751 | |
| 7752 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 7753 | |
| 7754 | /* It is useles to retrieve the stream, but this function |
| 7755 | * runs only in a stream context. |
| 7756 | */ |
| 7757 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7758 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 7759 | |
| 7760 | /* Unset the variable. */ |
| 7761 | 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] | 7762 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 7763 | return 1; |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 7764 | } |
| 7765 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 7766 | __LJMP static int hlua_get_var(lua_State *L) |
| 7767 | { |
| 7768 | struct hlua_txn *htxn; |
| 7769 | const char *name; |
| 7770 | size_t len; |
| 7771 | struct sample smp; |
| 7772 | |
| 7773 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 7774 | |
| 7775 | /* It is useles to retrieve the stream, but this function |
| 7776 | * runs only in a stream context. |
| 7777 | */ |
| 7778 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7779 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 7780 | |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 7781 | smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR); |
Willy Tarreau | e352b9d | 2021-09-03 11:52:38 +0200 | [diff] [blame] | 7782 | if (!vars_get_by_name(name, len, &smp, NULL)) { |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 7783 | lua_pushnil(L); |
| 7784 | return 1; |
| 7785 | } |
| 7786 | |
| 7787 | return hlua_smp2lua(L, &smp); |
| 7788 | } |
| 7789 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 7790 | __LJMP static int hlua_set_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7791 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7792 | struct hlua *hlua; |
| 7793 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7794 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 7795 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7796 | /* It is useles to retrieve the stream, but this function |
| 7797 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7798 | */ |
| 7799 | MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 7800 | |
| 7801 | /* Get hlua struct, or NULL if we execute from main lua state */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7802 | hlua = hlua_gethlua(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 7803 | if (!hlua) |
| 7804 | return 0; |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7805 | |
| 7806 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 7807 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7808 | |
| 7809 | /* Get and store new value. */ |
| 7810 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 7811 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 7812 | |
| 7813 | return 0; |
| 7814 | } |
| 7815 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 7816 | __LJMP static int hlua_get_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7817 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7818 | struct hlua *hlua; |
| 7819 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7820 | MAY_LJMP(check_args(L, 1, "get_priv")); |
| 7821 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7822 | /* It is useles to retrieve the stream, but this function |
| 7823 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7824 | */ |
| 7825 | MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 7826 | |
| 7827 | /* Get hlua struct, or NULL if we execute from main lua state */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7828 | hlua = hlua_gethlua(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 7829 | if (!hlua) { |
| 7830 | lua_pushnil(L); |
| 7831 | return 1; |
| 7832 | } |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7833 | |
| 7834 | /* Push configuration index in the stack. */ |
| 7835 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 7836 | |
| 7837 | return 1; |
| 7838 | } |
| 7839 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7840 | /* Create stack entry containing a class TXN. This function |
| 7841 | * return 0 if the stack does not contains free slots, |
| 7842 | * otherwise it returns 1. |
| 7843 | */ |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 7844 | 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] | 7845 | { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 7846 | struct hlua_txn *htxn; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7847 | |
| 7848 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 7849 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7850 | return 0; |
| 7851 | |
| 7852 | /* NOTE: The allocation never fails. The failure |
| 7853 | * throw an error, and the function never returns. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7854 | * if the throw is not available, the process is aborted. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7855 | */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 7856 | /* Create the object: obj[0] = userdata. */ |
| 7857 | lua_newtable(L); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 7858 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 7859 | lua_rawseti(L, -2, 0); |
| 7860 | |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 7861 | htxn->s = s; |
| 7862 | htxn->p = p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 7863 | htxn->dir = dir; |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 7864 | htxn->flags = flags; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7865 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 7866 | /* Create the "f" field that contains a list of fetches. */ |
| 7867 | lua_pushstring(L, "f"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 7868 | if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 7869 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7870 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 7871 | |
| 7872 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 7873 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 7874 | 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] | 7875 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7876 | lua_rawset(L, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 7877 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7878 | /* Create the "c" field that contains a list of converters. */ |
| 7879 | lua_pushstring(L, "c"); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 7880 | if (!hlua_converters_new(L, htxn, 0)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 7881 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7882 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 7883 | |
| 7884 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 7885 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 7886 | if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7887 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7888 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7889 | |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 7890 | /* Create the "req" field that contains the request channel object. */ |
| 7891 | lua_pushstring(L, "req"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 7892 | if (!hlua_channel_new(L, &s->req)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 7893 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7894 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 7895 | |
| 7896 | /* Create the "res" field that contains the response channel object. */ |
| 7897 | lua_pushstring(L, "res"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 7898 | if (!hlua_channel_new(L, &s->res)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 7899 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7900 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 7901 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7902 | /* Creates the HTTP object is the current proxy allows http. */ |
| 7903 | lua_pushstring(L, "http"); |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 7904 | if (IS_HTX_STRM(s)) { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 7905 | if (!hlua_http_new(L, htxn)) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7906 | return 0; |
| 7907 | } |
| 7908 | else |
| 7909 | lua_pushnil(L); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7910 | lua_rawset(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7911 | |
Christopher Faulet | 78c3547 | 2020-02-26 17:14:08 +0100 | [diff] [blame] | 7912 | if ((htxn->flags & HLUA_TXN_CTX_MASK) == HLUA_TXN_FLT_CTX) { |
| 7913 | /* HTTPMessage object are created when a lua TXN is created from |
| 7914 | * a filter context only |
| 7915 | */ |
| 7916 | |
| 7917 | /* Creates the HTTP-Request object is the current proxy allows http. */ |
| 7918 | lua_pushstring(L, "http_req"); |
| 7919 | if (p->mode == PR_MODE_HTTP) { |
| 7920 | if (!hlua_http_msg_new(L, &s->txn->req)) |
| 7921 | return 0; |
| 7922 | } |
| 7923 | else |
| 7924 | lua_pushnil(L); |
| 7925 | lua_rawset(L, -3); |
| 7926 | |
| 7927 | /* Creates the HTTP-Response object is the current proxy allows http. */ |
| 7928 | lua_pushstring(L, "http_res"); |
| 7929 | if (p->mode == PR_MODE_HTTP) { |
| 7930 | if (!hlua_http_msg_new(L, &s->txn->rsp)) |
| 7931 | return 0; |
| 7932 | } |
| 7933 | else |
| 7934 | lua_pushnil(L); |
| 7935 | lua_rawset(L, -3); |
| 7936 | } |
| 7937 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7938 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 7939 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref); |
| 7940 | lua_setmetatable(L, -2); |
| 7941 | |
| 7942 | return 1; |
| 7943 | } |
| 7944 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 7945 | __LJMP static int hlua_txn_deflog(lua_State *L) |
| 7946 | { |
| 7947 | const char *msg; |
| 7948 | struct hlua_txn *htxn; |
| 7949 | |
| 7950 | MAY_LJMP(check_args(L, 2, "deflog")); |
| 7951 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7952 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7953 | |
| 7954 | hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg); |
| 7955 | return 0; |
| 7956 | } |
| 7957 | |
| 7958 | __LJMP static int hlua_txn_log(lua_State *L) |
| 7959 | { |
| 7960 | int level; |
| 7961 | const char *msg; |
| 7962 | struct hlua_txn *htxn; |
| 7963 | |
| 7964 | MAY_LJMP(check_args(L, 3, "log")); |
| 7965 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7966 | level = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 7967 | msg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 7968 | |
| 7969 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 7970 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 7971 | |
| 7972 | hlua_sendlog(htxn->s->be, level, msg); |
| 7973 | return 0; |
| 7974 | } |
| 7975 | |
| 7976 | __LJMP static int hlua_txn_log_debug(lua_State *L) |
| 7977 | { |
| 7978 | const char *msg; |
| 7979 | struct hlua_txn *htxn; |
| 7980 | |
| 7981 | MAY_LJMP(check_args(L, 2, "Debug")); |
| 7982 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7983 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7984 | hlua_sendlog(htxn->s->be, LOG_DEBUG, msg); |
| 7985 | return 0; |
| 7986 | } |
| 7987 | |
| 7988 | __LJMP static int hlua_txn_log_info(lua_State *L) |
| 7989 | { |
| 7990 | const char *msg; |
| 7991 | struct hlua_txn *htxn; |
| 7992 | |
| 7993 | MAY_LJMP(check_args(L, 2, "Info")); |
| 7994 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7995 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7996 | hlua_sendlog(htxn->s->be, LOG_INFO, msg); |
| 7997 | return 0; |
| 7998 | } |
| 7999 | |
| 8000 | __LJMP static int hlua_txn_log_warning(lua_State *L) |
| 8001 | { |
| 8002 | const char *msg; |
| 8003 | struct hlua_txn *htxn; |
| 8004 | |
| 8005 | MAY_LJMP(check_args(L, 2, "Warning")); |
| 8006 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 8007 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 8008 | hlua_sendlog(htxn->s->be, LOG_WARNING, msg); |
| 8009 | return 0; |
| 8010 | } |
| 8011 | |
| 8012 | __LJMP static int hlua_txn_log_alert(lua_State *L) |
| 8013 | { |
| 8014 | const char *msg; |
| 8015 | struct hlua_txn *htxn; |
| 8016 | |
| 8017 | MAY_LJMP(check_args(L, 2, "Alert")); |
| 8018 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 8019 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 8020 | hlua_sendlog(htxn->s->be, LOG_ALERT, msg); |
| 8021 | return 0; |
| 8022 | } |
| 8023 | |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 8024 | __LJMP static int hlua_txn_set_loglevel(lua_State *L) |
| 8025 | { |
| 8026 | struct hlua_txn *htxn; |
| 8027 | int ll; |
| 8028 | |
| 8029 | MAY_LJMP(check_args(L, 2, "set_loglevel")); |
| 8030 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 8031 | ll = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 8032 | |
| 8033 | if (ll < 0 || ll > 7) |
| 8034 | WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7")); |
| 8035 | |
| 8036 | htxn->s->logs.level = ll; |
| 8037 | return 0; |
| 8038 | } |
| 8039 | |
| 8040 | __LJMP static int hlua_txn_set_tos(lua_State *L) |
| 8041 | { |
| 8042 | struct hlua_txn *htxn; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 8043 | int tos; |
| 8044 | |
| 8045 | MAY_LJMP(check_args(L, 2, "set_tos")); |
| 8046 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 8047 | tos = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 8048 | |
Willy Tarreau | 1a18b54 | 2018-12-11 16:37:42 +0100 | [diff] [blame] | 8049 | conn_set_tos(objt_conn(htxn->s->sess->origin), tos); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 8050 | return 0; |
| 8051 | } |
| 8052 | |
| 8053 | __LJMP static int hlua_txn_set_mark(lua_State *L) |
| 8054 | { |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 8055 | struct hlua_txn *htxn; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 8056 | int mark; |
| 8057 | |
| 8058 | MAY_LJMP(check_args(L, 2, "set_mark")); |
| 8059 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 8060 | mark = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 8061 | |
Lukas Tribus | 579e3e3 | 2019-08-11 18:03:45 +0200 | [diff] [blame] | 8062 | conn_set_mark(objt_conn(htxn->s->sess->origin), mark); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 8063 | return 0; |
| 8064 | } |
| 8065 | |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 8066 | __LJMP static int hlua_txn_set_priority_class(lua_State *L) |
| 8067 | { |
| 8068 | struct hlua_txn *htxn; |
| 8069 | |
| 8070 | MAY_LJMP(check_args(L, 2, "set_priority_class")); |
| 8071 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 8072 | htxn->s->priority_class = queue_limit_class(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 8073 | return 0; |
| 8074 | } |
| 8075 | |
| 8076 | __LJMP static int hlua_txn_set_priority_offset(lua_State *L) |
| 8077 | { |
| 8078 | struct hlua_txn *htxn; |
| 8079 | |
| 8080 | MAY_LJMP(check_args(L, 2, "set_priority_offset")); |
| 8081 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 8082 | htxn->s->priority_offset = queue_limit_offset(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 8083 | return 0; |
| 8084 | } |
| 8085 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8086 | /* 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] | 8087 | * 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] | 8088 | * message and terminate the transaction. It returns 1 on success and 0 on |
| 8089 | * error. The Reply must be on top of the stack. |
| 8090 | */ |
| 8091 | __LJMP static int hlua_txn_forward_reply(lua_State *L, struct stream *s) |
| 8092 | { |
| 8093 | struct htx *htx; |
| 8094 | struct htx_sl *sl; |
| 8095 | struct h1m h1m; |
| 8096 | const char *status, *reason, *body; |
| 8097 | size_t status_len, reason_len, body_len; |
| 8098 | int ret, code, flags; |
| 8099 | |
| 8100 | code = 200; |
| 8101 | status = "200"; |
| 8102 | status_len = 3; |
| 8103 | ret = lua_getfield(L, -1, "status"); |
| 8104 | if (ret == LUA_TNUMBER) { |
| 8105 | code = lua_tointeger(L, -1); |
| 8106 | status = lua_tolstring(L, -1, &status_len); |
| 8107 | } |
| 8108 | lua_pop(L, 1); |
| 8109 | |
| 8110 | reason = http_get_reason(code); |
| 8111 | reason_len = strlen(reason); |
| 8112 | ret = lua_getfield(L, -1, "reason"); |
| 8113 | if (ret == LUA_TSTRING) |
| 8114 | reason = lua_tolstring(L, -1, &reason_len); |
| 8115 | lua_pop(L, 1); |
| 8116 | |
| 8117 | body = NULL; |
| 8118 | body_len = 0; |
| 8119 | ret = lua_getfield(L, -1, "body"); |
| 8120 | if (ret == LUA_TSTRING) |
| 8121 | body = lua_tolstring(L, -1, &body_len); |
| 8122 | lua_pop(L, 1); |
| 8123 | |
| 8124 | /* Prepare the response before inserting the headers */ |
| 8125 | h1m_init_res(&h1m); |
| 8126 | htx = htx_from_buf(&s->res.buf); |
| 8127 | channel_htx_truncate(&s->res, htx); |
| 8128 | if (s->txn->req.flags & HTTP_MSGF_VER_11) { |
| 8129 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11); |
| 8130 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), |
| 8131 | ist2(status, status_len), ist2(reason, reason_len)); |
| 8132 | } |
| 8133 | else { |
| 8134 | flags = HTX_SL_F_IS_RESP; |
| 8135 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), |
| 8136 | ist2(status, status_len), ist2(reason, reason_len)); |
| 8137 | } |
| 8138 | if (!sl) |
| 8139 | goto fail; |
| 8140 | sl->info.res.status = code; |
| 8141 | |
| 8142 | /* Push in the stack the "headers" entry. */ |
| 8143 | ret = lua_getfield(L, -1, "headers"); |
| 8144 | if (ret != LUA_TTABLE) |
| 8145 | goto skip_headers; |
| 8146 | |
| 8147 | lua_pushnil(L); |
| 8148 | while (lua_next(L, -2) != 0) { |
| 8149 | struct ist name, value; |
| 8150 | const char *n, *v; |
| 8151 | size_t nlen, vlen; |
| 8152 | |
| 8153 | if (!lua_isstring(L, -2) || !lua_istable(L, -1)) { |
| 8154 | /* Skip element if the key is not a string or if the value is not a table */ |
| 8155 | goto next_hdr; |
| 8156 | } |
| 8157 | |
| 8158 | n = lua_tolstring(L, -2, &nlen); |
| 8159 | name = ist2(n, nlen); |
| 8160 | if (isteqi(name, ist("content-length"))) { |
| 8161 | /* Always skip content-length header. It will be added |
| 8162 | * later with the correct len |
| 8163 | */ |
| 8164 | goto next_hdr; |
| 8165 | } |
| 8166 | |
| 8167 | /* Loop on header's values */ |
| 8168 | lua_pushnil(L); |
| 8169 | while (lua_next(L, -2)) { |
| 8170 | if (!lua_isstring(L, -1)) { |
| 8171 | /* Skip the value if it is not a string */ |
| 8172 | goto next_value; |
| 8173 | } |
| 8174 | |
| 8175 | v = lua_tolstring(L, -1, &vlen); |
| 8176 | value = ist2(v, vlen); |
| 8177 | |
| 8178 | if (isteqi(name, ist("transfer-encoding"))) |
| 8179 | h1_parse_xfer_enc_header(&h1m, value); |
| 8180 | if (!htx_add_header(htx, ist2(n, nlen), ist2(v, vlen))) |
| 8181 | goto fail; |
| 8182 | |
| 8183 | next_value: |
| 8184 | lua_pop(L, 1); |
| 8185 | } |
| 8186 | |
| 8187 | next_hdr: |
| 8188 | lua_pop(L, 1); |
| 8189 | } |
| 8190 | skip_headers: |
| 8191 | lua_pop(L, 1); |
| 8192 | |
| 8193 | /* Update h1m flags: CLEN is set if CHNK is not present */ |
| 8194 | if (!(h1m.flags & H1_MF_CHNK)) { |
| 8195 | const char *clen = ultoa(body_len); |
| 8196 | |
| 8197 | h1m.flags |= H1_MF_CLEN; |
| 8198 | if (!htx_add_header(htx, ist("content-length"), ist(clen))) |
| 8199 | goto fail; |
| 8200 | } |
| 8201 | if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK)) |
| 8202 | h1m.flags |= H1_MF_XFER_LEN; |
| 8203 | |
| 8204 | /* Update HTX start-line flags */ |
| 8205 | if (h1m.flags & H1_MF_XFER_ENC) |
| 8206 | flags |= HTX_SL_F_XFER_ENC; |
| 8207 | if (h1m.flags & H1_MF_XFER_LEN) { |
| 8208 | flags |= HTX_SL_F_XFER_LEN; |
| 8209 | if (h1m.flags & H1_MF_CHNK) |
| 8210 | flags |= HTX_SL_F_CHNK; |
| 8211 | else if (h1m.flags & H1_MF_CLEN) |
| 8212 | flags |= HTX_SL_F_CLEN; |
| 8213 | if (h1m.body_len == 0) |
| 8214 | flags |= HTX_SL_F_BODYLESS; |
| 8215 | } |
| 8216 | sl->flags |= flags; |
| 8217 | |
| 8218 | |
| 8219 | if (!htx_add_endof(htx, HTX_BLK_EOH) || |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 8220 | (body_len && !htx_add_data_atonce(htx, ist2(body, body_len)))) |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8221 | goto fail; |
| 8222 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 8223 | htx->flags |= HTX_FL_EOM; |
| 8224 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8225 | /* Now, forward the response and terminate the transaction */ |
| 8226 | s->txn->status = code; |
| 8227 | htx_to_buf(htx, &s->res.buf); |
| 8228 | if (!http_forward_proxy_resp(s, 1)) |
| 8229 | goto fail; |
| 8230 | |
| 8231 | return 1; |
| 8232 | |
| 8233 | fail: |
| 8234 | channel_htx_truncate(&s->res, htx); |
| 8235 | return 0; |
| 8236 | } |
| 8237 | |
| 8238 | /* Terminate a transaction if called from a lua action. For TCP streams, |
| 8239 | * processing is just aborted. Nothing is returned to the client and all |
| 8240 | * arguments are ignored. For HTTP streams, if a reply is passed as argument, it |
| 8241 | * is forwarded to the client before terminating the transaction. On success, |
| 8242 | * the function exits with ACT_RET_DONE code. If an error occurred, it exits |
| 8243 | * with ACT_RET_ERR code. If this function is not called from a lua action, it |
| 8244 | * just exits without any processing. |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 8245 | */ |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 8246 | __LJMP static int hlua_txn_done(lua_State *L) |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 8247 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 8248 | struct hlua_txn *htxn; |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8249 | struct stream *s; |
| 8250 | int finst; |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 8251 | |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 8252 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 8253 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8254 | /* If the flags NOTERM is set, we cannot terminate the session, so we |
| 8255 | * just end the execution of the current lua code. */ |
| 8256 | if (htxn->flags & HLUA_TXN_NOTERM) |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 8257 | WILL_LJMP(hlua_done(L)); |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 8258 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8259 | s = htxn->s; |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 8260 | if (!IS_HTX_STRM(htxn->s)) { |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8261 | struct channel *req = &s->req; |
| 8262 | struct channel *res = &s->res; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 8263 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8264 | channel_auto_read(req); |
| 8265 | channel_abort(req); |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8266 | channel_erase(req); |
| 8267 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8268 | channel_auto_read(res); |
| 8269 | channel_auto_close(res); |
Christopher Faulet | 12762f0 | 2023-04-13 15:40:10 +0200 | [diff] [blame] | 8270 | sc_schedule_abort(s->scb); |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8271 | |
| 8272 | finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_D); |
| 8273 | goto done; |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 8274 | } |
Thierry FOURNIER | 10ec214 | 2015-08-24 17:23:45 +0200 | [diff] [blame] | 8275 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8276 | if (lua_gettop(L) == 1 || !lua_istable(L, 2)) { |
| 8277 | /* No reply or invalid reply */ |
| 8278 | s->txn->status = 0; |
| 8279 | http_reply_and_close(s, 0, NULL); |
| 8280 | } |
| 8281 | else { |
| 8282 | /* Remove extra args to have the reply on top of the stack */ |
| 8283 | if (lua_gettop(L) > 2) |
| 8284 | lua_pop(L, lua_gettop(L) - 2); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 8285 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8286 | if (!hlua_txn_forward_reply(L, s)) { |
| 8287 | if (!(s->flags & SF_ERR_MASK)) |
| 8288 | s->flags |= SF_ERR_PRXCOND; |
| 8289 | lua_pushinteger(L, ACT_RET_ERR); |
| 8290 | WILL_LJMP(hlua_done(L)); |
| 8291 | return 0; /* Never reached */ |
| 8292 | } |
Christopher Faulet | 4d0e263 | 2019-07-16 10:52:40 +0200 | [diff] [blame] | 8293 | } |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 8294 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8295 | finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_H); |
| 8296 | if (htxn->dir == SMP_OPT_DIR_REQ) { |
| 8297 | /* let's log the request time */ |
| 8298 | s->logs.tv_request = now; |
| 8299 | 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] | 8300 | _HA_ATOMIC_INC(&s->sess->fe->fe_counters.intercepted_req); |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8301 | } |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 8302 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8303 | done: |
| 8304 | if (!(s->flags & SF_ERR_MASK)) |
| 8305 | s->flags |= SF_ERR_LOCAL; |
| 8306 | if (!(s->flags & SF_FINST_MASK)) |
| 8307 | s->flags |= finst; |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 8308 | |
Christopher Faulet | e48d1dc | 2021-08-13 14:11:17 +0200 | [diff] [blame] | 8309 | if ((htxn->flags & HLUA_TXN_CTX_MASK) == HLUA_TXN_FLT_CTX) |
| 8310 | lua_pushinteger(L, -1); |
| 8311 | else |
| 8312 | lua_pushinteger(L, ACT_RET_ABRT); |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 8313 | WILL_LJMP(hlua_done(L)); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 8314 | return 0; |
| 8315 | } |
| 8316 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8317 | /* |
| 8318 | * |
| 8319 | * |
| 8320 | * Class REPLY |
| 8321 | * |
| 8322 | * |
| 8323 | */ |
| 8324 | |
| 8325 | /* Pushes the TXN reply onto the top of the stack. If the stask does not have a |
| 8326 | * free slots, the function fails and returns 0; |
| 8327 | */ |
| 8328 | static int hlua_txn_reply_new(lua_State *L) |
| 8329 | { |
| 8330 | struct hlua_txn *htxn; |
| 8331 | const char *reason, *body = NULL; |
| 8332 | int ret, status; |
| 8333 | |
| 8334 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 8335 | if (!IS_HTX_STRM(htxn->s)) { |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8336 | hlua_pusherror(L, "txn object is not an HTTP transaction."); |
| 8337 | WILL_LJMP(lua_error(L)); |
| 8338 | } |
| 8339 | |
| 8340 | /* Default value */ |
| 8341 | status = 200; |
| 8342 | reason = http_get_reason(status); |
| 8343 | |
| 8344 | if (lua_istable(L, 2)) { |
| 8345 | /* load status and reason from the table argument at index 2 */ |
| 8346 | ret = lua_getfield(L, 2, "status"); |
| 8347 | if (ret == LUA_TNIL) |
| 8348 | goto reason; |
| 8349 | else if (ret != LUA_TNUMBER) { |
| 8350 | /* invalid status: ignore the reason */ |
| 8351 | goto body; |
| 8352 | } |
| 8353 | status = lua_tointeger(L, -1); |
| 8354 | |
| 8355 | reason: |
| 8356 | lua_pop(L, 1); /* restore the stack: remove status */ |
| 8357 | ret = lua_getfield(L, 2, "reason"); |
| 8358 | if (ret == LUA_TSTRING) |
| 8359 | reason = lua_tostring(L, -1); |
| 8360 | |
| 8361 | body: |
| 8362 | lua_pop(L, 1); /* restore the stack: remove invalid status or reason */ |
| 8363 | ret = lua_getfield(L, 2, "body"); |
| 8364 | if (ret == LUA_TSTRING) |
| 8365 | body = lua_tostring(L, -1); |
| 8366 | lua_pop(L, 1); /* restore the stack: remove body */ |
| 8367 | } |
| 8368 | |
| 8369 | /* Create the Reply table */ |
| 8370 | lua_newtable(L); |
| 8371 | |
| 8372 | /* Add status element */ |
| 8373 | lua_pushstring(L, "status"); |
| 8374 | lua_pushinteger(L, status); |
| 8375 | lua_settable(L, -3); |
| 8376 | |
| 8377 | /* Add reason element */ |
| 8378 | reason = http_get_reason(status); |
| 8379 | lua_pushstring(L, "reason"); |
| 8380 | lua_pushstring(L, reason); |
| 8381 | lua_settable(L, -3); |
| 8382 | |
| 8383 | /* Add body element, nil if undefined */ |
| 8384 | lua_pushstring(L, "body"); |
| 8385 | if (body) |
| 8386 | lua_pushstring(L, body); |
| 8387 | else |
| 8388 | lua_pushnil(L); |
| 8389 | lua_settable(L, -3); |
| 8390 | |
| 8391 | /* Add headers element */ |
| 8392 | lua_pushstring(L, "headers"); |
| 8393 | lua_newtable(L); |
| 8394 | |
| 8395 | /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */ |
| 8396 | if (lua_istable(L, 2)) { |
| 8397 | /* load headers from the table argument at index 2. If it is a table, copy it. */ |
| 8398 | ret = lua_getfield(L, 2, "headers"); |
| 8399 | if (ret == LUA_TTABLE) { |
| 8400 | /* stack: [ ... <headers:table>, <table> ] */ |
| 8401 | lua_pushnil(L); |
| 8402 | while (lua_next(L, -2) != 0) { |
| 8403 | /* stack: [ ... <headers:table>, <table>, k, v] */ |
| 8404 | if (!lua_isstring(L, -1) && !lua_istable(L, -1)) { |
| 8405 | /* invalid value type, skip it */ |
| 8406 | lua_pop(L, 1); |
| 8407 | continue; |
| 8408 | } |
| 8409 | |
| 8410 | |
| 8411 | /* Duplicate the key and swap it with the value. */ |
| 8412 | lua_pushvalue(L, -2); |
| 8413 | lua_insert(L, -2); |
| 8414 | /* stack: [ ... <headers:table>, <table>, k, k, v ] */ |
| 8415 | |
| 8416 | lua_newtable(L); |
| 8417 | lua_insert(L, -2); |
| 8418 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, v ] */ |
| 8419 | |
| 8420 | if (lua_isstring(L, -1)) { |
| 8421 | /* push the value in the inner table */ |
| 8422 | lua_rawseti(L, -2, 1); |
| 8423 | } |
| 8424 | else { /* table */ |
| 8425 | lua_pushnil(L); |
| 8426 | while (lua_next(L, -2) != 0) { |
| 8427 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2, v2 ] */ |
| 8428 | if (!lua_isstring(L, -1)) { |
| 8429 | /* invalid value type, skip it*/ |
| 8430 | lua_pop(L, 1); |
| 8431 | continue; |
| 8432 | } |
| 8433 | /* push the value in the inner table */ |
| 8434 | lua_rawseti(L, -4, lua_rawlen(L, -4) + 1); |
| 8435 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2 ] */ |
| 8436 | } |
| 8437 | lua_pop(L, 1); |
| 8438 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table> ] */ |
| 8439 | } |
| 8440 | |
| 8441 | /* push (k,v) on the stack in the headers table: |
| 8442 | * stack: [ ... <headers:table>, <table>, k, k, v ] |
| 8443 | */ |
| 8444 | lua_settable(L, -5); |
| 8445 | /* stack: [ ... <headers:table>, <table>, k ] */ |
| 8446 | } |
| 8447 | } |
| 8448 | lua_pop(L, 1); |
| 8449 | } |
| 8450 | /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */ |
| 8451 | lua_settable(L, -3); |
| 8452 | /* stack: [ txn, <Arg:table>, <Reply:table> ] */ |
| 8453 | |
| 8454 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 8455 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_reply_ref); |
| 8456 | lua_setmetatable(L, -2); |
| 8457 | return 1; |
| 8458 | } |
| 8459 | |
| 8460 | /* Set the reply status code, and optionally the reason. If no reason is |
| 8461 | * provided, the default one corresponding to the status code is used. |
| 8462 | */ |
| 8463 | __LJMP static int hlua_txn_reply_set_status(lua_State *L) |
| 8464 | { |
| 8465 | int status = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 8466 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
| 8467 | |
| 8468 | /* First argument (self) must be a table */ |
Aurelien DARRAGON | d83d045 | 2022-10-05 11:46:45 +0200 | [diff] [blame] | 8469 | MAY_LJMP(luaL_checktype(L, 1, LUA_TTABLE)); |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8470 | |
| 8471 | if (status < 100 || status > 599) { |
| 8472 | lua_pushboolean(L, 0); |
| 8473 | return 1; |
| 8474 | } |
| 8475 | if (!reason) |
| 8476 | reason = http_get_reason(status); |
| 8477 | |
| 8478 | lua_pushinteger(L, status); |
| 8479 | lua_setfield(L, 1, "status"); |
| 8480 | |
| 8481 | lua_pushstring(L, reason); |
| 8482 | lua_setfield(L, 1, "reason"); |
| 8483 | |
| 8484 | lua_pushboolean(L, 1); |
| 8485 | return 1; |
| 8486 | } |
| 8487 | |
| 8488 | /* Add a header into the reply object. Each header name is associated to an |
| 8489 | * array of values in the "headers" table. If the header name is not found, a |
| 8490 | * new entry is created. |
| 8491 | */ |
| 8492 | __LJMP static int hlua_txn_reply_add_header(lua_State *L) |
| 8493 | { |
| 8494 | const char *name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 8495 | const char *value = MAY_LJMP(luaL_checkstring(L, 3)); |
| 8496 | int ret; |
| 8497 | |
| 8498 | /* First argument (self) must be a table */ |
Aurelien DARRAGON | d83d045 | 2022-10-05 11:46:45 +0200 | [diff] [blame] | 8499 | MAY_LJMP(luaL_checktype(L, 1, LUA_TTABLE)); |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8500 | |
| 8501 | /* Push in the stack the "headers" entry. */ |
| 8502 | ret = lua_getfield(L, 1, "headers"); |
| 8503 | if (ret != LUA_TTABLE) { |
| 8504 | hlua_pusherror(L, "Reply['headers'] is expected to a an array. %s found", lua_typename(L, ret)); |
| 8505 | WILL_LJMP(lua_error(L)); |
| 8506 | } |
| 8507 | |
| 8508 | /* check if the header is already registered. If not, register it. */ |
| 8509 | ret = lua_getfield(L, -1, name); |
| 8510 | if (ret == LUA_TNIL) { |
| 8511 | /* Entry not found. */ |
| 8512 | lua_pop(L, 1); /* remove the nil. The "headers" table is the top of the stack. */ |
| 8513 | |
| 8514 | /* Insert the new header name in the array in the top of the stack. |
| 8515 | * It left the new array in the top of the stack. |
| 8516 | */ |
| 8517 | lua_newtable(L); |
| 8518 | lua_pushstring(L, name); |
| 8519 | lua_pushvalue(L, -2); |
| 8520 | lua_settable(L, -4); |
| 8521 | } |
| 8522 | else if (ret != LUA_TTABLE) { |
| 8523 | hlua_pusherror(L, "Reply['headers']['%s'] is expected to be an array. %s found", name, lua_typename(L, ret)); |
| 8524 | WILL_LJMP(lua_error(L)); |
| 8525 | } |
| 8526 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 8527 | /* Now the top of thestack is an array of values. We push |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8528 | * the header value as new entry. |
| 8529 | */ |
| 8530 | lua_pushstring(L, value); |
| 8531 | ret = lua_rawlen(L, -2); |
| 8532 | lua_rawseti(L, -2, ret + 1); |
| 8533 | |
| 8534 | lua_pushboolean(L, 1); |
| 8535 | return 1; |
| 8536 | } |
| 8537 | |
| 8538 | /* Remove all occurrences of a given header name. */ |
| 8539 | __LJMP static int hlua_txn_reply_del_header(lua_State *L) |
| 8540 | { |
| 8541 | const char *name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 8542 | int ret; |
| 8543 | |
| 8544 | /* First argument (self) must be a table */ |
Aurelien DARRAGON | d83d045 | 2022-10-05 11:46:45 +0200 | [diff] [blame] | 8545 | MAY_LJMP(luaL_checktype(L, 1, LUA_TTABLE)); |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8546 | |
| 8547 | /* Push in the stack the "headers" entry. */ |
| 8548 | ret = lua_getfield(L, 1, "headers"); |
| 8549 | if (ret != LUA_TTABLE) { |
| 8550 | hlua_pusherror(L, "Reply['headers'] is expected to be an array. %s found", lua_typename(L, ret)); |
| 8551 | WILL_LJMP(lua_error(L)); |
| 8552 | } |
| 8553 | |
| 8554 | lua_pushstring(L, name); |
| 8555 | lua_pushnil(L); |
| 8556 | lua_settable(L, -3); |
| 8557 | |
| 8558 | lua_pushboolean(L, 1); |
| 8559 | return 1; |
| 8560 | } |
| 8561 | |
| 8562 | /* Set the reply's body. Overwrite any existing entry. */ |
| 8563 | __LJMP static int hlua_txn_reply_set_body(lua_State *L) |
| 8564 | { |
| 8565 | const char *payload = MAY_LJMP(luaL_checkstring(L, 2)); |
| 8566 | |
| 8567 | /* First argument (self) must be a table */ |
Aurelien DARRAGON | d83d045 | 2022-10-05 11:46:45 +0200 | [diff] [blame] | 8568 | MAY_LJMP(luaL_checktype(L, 1, LUA_TTABLE)); |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8569 | |
| 8570 | lua_pushstring(L, payload); |
| 8571 | lua_setfield(L, 1, "body"); |
| 8572 | |
| 8573 | lua_pushboolean(L, 1); |
| 8574 | return 1; |
| 8575 | } |
| 8576 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 8577 | __LJMP static int hlua_log(lua_State *L) |
| 8578 | { |
| 8579 | int level; |
| 8580 | const char *msg; |
| 8581 | |
| 8582 | MAY_LJMP(check_args(L, 2, "log")); |
| 8583 | level = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 8584 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 8585 | |
| 8586 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 8587 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 8588 | |
| 8589 | hlua_sendlog(NULL, level, msg); |
| 8590 | return 0; |
| 8591 | } |
| 8592 | |
| 8593 | __LJMP static int hlua_log_debug(lua_State *L) |
| 8594 | { |
| 8595 | const char *msg; |
| 8596 | |
| 8597 | MAY_LJMP(check_args(L, 1, "debug")); |
| 8598 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 8599 | hlua_sendlog(NULL, LOG_DEBUG, msg); |
| 8600 | return 0; |
| 8601 | } |
| 8602 | |
| 8603 | __LJMP static int hlua_log_info(lua_State *L) |
| 8604 | { |
| 8605 | const char *msg; |
| 8606 | |
| 8607 | MAY_LJMP(check_args(L, 1, "info")); |
| 8608 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 8609 | hlua_sendlog(NULL, LOG_INFO, msg); |
| 8610 | return 0; |
| 8611 | } |
| 8612 | |
| 8613 | __LJMP static int hlua_log_warning(lua_State *L) |
| 8614 | { |
| 8615 | const char *msg; |
| 8616 | |
| 8617 | MAY_LJMP(check_args(L, 1, "warning")); |
| 8618 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 8619 | hlua_sendlog(NULL, LOG_WARNING, msg); |
| 8620 | return 0; |
| 8621 | } |
| 8622 | |
| 8623 | __LJMP static int hlua_log_alert(lua_State *L) |
| 8624 | { |
| 8625 | const char *msg; |
| 8626 | |
| 8627 | MAY_LJMP(check_args(L, 1, "alert")); |
| 8628 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 8629 | hlua_sendlog(NULL, LOG_ALERT, msg); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 8630 | return 0; |
| 8631 | } |
| 8632 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 8633 | __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] | 8634 | { |
| 8635 | int wakeup_ms = lua_tointeger(L, -1); |
Willy Tarreau | 12c0270 | 2021-09-30 16:12:31 +0200 | [diff] [blame] | 8636 | if (!tick_is_expired(wakeup_ms, now_ms)) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 8637 | 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] | 8638 | return 0; |
| 8639 | } |
| 8640 | |
| 8641 | __LJMP static int hlua_sleep(lua_State *L) |
| 8642 | { |
| 8643 | unsigned int delay; |
Aurelien DARRAGON | 2a9764b | 2023-04-04 18:41:04 +0200 | [diff] [blame] | 8644 | int wakeup_ms; // tick value |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8645 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 8646 | MAY_LJMP(check_args(L, 1, "sleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8647 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 8648 | delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 8649 | wakeup_ms = tick_add(now_ms, delay); |
| 8650 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8651 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 8652 | 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] | 8653 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8654 | } |
| 8655 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 8656 | __LJMP static int hlua_msleep(lua_State *L) |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8657 | { |
| 8658 | unsigned int delay; |
Aurelien DARRAGON | 2a9764b | 2023-04-04 18:41:04 +0200 | [diff] [blame] | 8659 | int wakeup_ms; // tick value |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8660 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 8661 | MAY_LJMP(check_args(L, 1, "msleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8662 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 8663 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 8664 | wakeup_ms = tick_add(now_ms, delay); |
| 8665 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8666 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 8667 | 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] | 8668 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8669 | } |
| 8670 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 8671 | /* This functionis an LUA binding. it permits to give back |
| 8672 | * the hand at the HAProxy scheduler. It is used when the |
| 8673 | * LUA processing consumes a lot of time. |
| 8674 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 8675 | __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] | 8676 | { |
| 8677 | return 0; |
| 8678 | } |
| 8679 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 8680 | __LJMP static int hlua_yield(lua_State *L) |
| 8681 | { |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 8682 | 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] | 8683 | return 0; |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 8684 | } |
| 8685 | |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 8686 | /* This function change the nice of the currently executed |
| 8687 | * task. It is used set low or high priority at the current |
| 8688 | * task. |
| 8689 | */ |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 8690 | __LJMP static int hlua_set_nice(lua_State *L) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 8691 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8692 | struct hlua *hlua; |
| 8693 | int nice; |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 8694 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8695 | MAY_LJMP(check_args(L, 1, "set_nice")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8696 | nice = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 8697 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 8698 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 8699 | hlua = hlua_gethlua(L); |
| 8700 | |
| 8701 | /* If the task is not set, I'm in a start mode. */ |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 8702 | if (!hlua || !hlua->task) |
| 8703 | return 0; |
| 8704 | |
| 8705 | if (nice < -1024) |
| 8706 | nice = -1024; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8707 | else if (nice > 1024) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 8708 | nice = 1024; |
| 8709 | |
| 8710 | hlua->task->nice = nice; |
| 8711 | return 0; |
| 8712 | } |
| 8713 | |
Aurelien DARRAGON | 8cd620b | 2023-04-07 17:37:46 +0200 | [diff] [blame] | 8714 | /* safe lua coroutine.create() function: |
| 8715 | * |
| 8716 | * This is a simple wrapper for coroutine.create() that |
| 8717 | * ensures the current hlua state ctx is available from |
| 8718 | * the new subroutine state |
| 8719 | */ |
| 8720 | __LJMP static int hlua_coroutine_create(lua_State *L) |
| 8721 | { |
| 8722 | lua_State *new; /* new coroutine state */ |
| 8723 | struct hlua **hlua_store; |
| 8724 | struct hlua *hlua = hlua_gethlua(L); |
| 8725 | |
| 8726 | new = lua_newthread(L); |
| 8727 | if (!new) |
| 8728 | return 0; |
| 8729 | |
| 8730 | hlua_store = lua_getextraspace(new); |
| 8731 | /* Expose current hlua ctx on new lua thread |
| 8732 | * (hlua_gethlua() will properly return the last "known" |
| 8733 | * hlua ctx instead of NULL when it is called from such coroutines) |
| 8734 | */ |
| 8735 | *hlua_store = hlua; |
| 8736 | |
| 8737 | /* new lua thread is on the top of the stack, we |
| 8738 | * need to duplicate first stack argument (<f> from coroutine.create(<f>)) |
| 8739 | * on the top of the stack to be able to use xmove() to move it on the new |
| 8740 | * stack |
| 8741 | */ |
| 8742 | lua_pushvalue(L, 1); |
| 8743 | /* move <f> function to the new stack */ |
| 8744 | lua_xmove(L, new, 1); |
| 8745 | /* new lua thread is back at the top of the stack */ |
| 8746 | return 1; |
| 8747 | } |
| 8748 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 8749 | /* 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] | 8750 | * HAProxy task subsystem when the task is awaked. The LUA runtime can |
| 8751 | * return an E_AGAIN signal, the emmiter of this signal must set a |
| 8752 | * signal to wake the task. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8753 | * |
| 8754 | * Task wrapper are longjmp safe because the only one Lua code |
| 8755 | * executed is the safe hlua_ctx_resume(); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8756 | */ |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 8757 | 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] | 8758 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 8759 | struct hlua *hlua = context; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8760 | enum hlua_exec status; |
| 8761 | |
Willy Tarreau | 6ef52f4 | 2022-06-15 14:19:48 +0200 | [diff] [blame] | 8762 | if (task->tid < 0) |
| 8763 | task->tid = tid; |
| 8764 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8765 | /* If it is the first call to the task, we must initialize the |
| 8766 | * execution timeouts. |
| 8767 | */ |
| 8768 | if (!HLUA_IS_RUNNING(hlua)) |
Aurelien DARRAGON | da9503c | 2022-11-25 09:10:07 +0100 | [diff] [blame] | 8769 | hlua_timer_init(&hlua->timer, hlua_timeout_task); |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8770 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8771 | /* Execute the Lua code. */ |
| 8772 | status = hlua_ctx_resume(hlua, 1); |
| 8773 | |
| 8774 | switch (status) { |
| 8775 | /* finished or yield */ |
| 8776 | case HLUA_E_OK: |
| 8777 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 8778 | task_destroy(task); |
Tim Duesterhus | cd235c6 | 2018-04-24 13:56:01 +0200 | [diff] [blame] | 8779 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8780 | break; |
| 8781 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 8782 | case HLUA_E_AGAIN: /* co process or timeout wake me later. */ |
Thierry FOURNIER | cb14688 | 2017-12-10 17:10:57 +0100 | [diff] [blame] | 8783 | notification_gc(&hlua->com); |
PiBa-NL | fe971b3 | 2018-05-02 22:27:14 +0200 | [diff] [blame] | 8784 | task->expire = hlua->wake_time; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8785 | break; |
| 8786 | |
| 8787 | /* finished with error. */ |
Aurelien DARRAGON | 7954410 | 2022-11-24 14:27:15 +0100 | [diff] [blame] | 8788 | case HLUA_E_ETMOUT: |
| 8789 | SEND_ERR(NULL, "Lua task: execution timeout.\n"); |
| 8790 | goto err_task_abort; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8791 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8792 | SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1)); |
Aurelien DARRAGON | 7954410 | 2022-11-24 14:27:15 +0100 | [diff] [blame] | 8793 | goto err_task_abort; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8794 | case HLUA_E_ERR: |
| 8795 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8796 | SEND_ERR(NULL, "Lua task: unknown error.\n"); |
Aurelien DARRAGON | 7954410 | 2022-11-24 14:27:15 +0100 | [diff] [blame] | 8797 | err_task_abort: |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8798 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 8799 | task_destroy(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 8800 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8801 | break; |
| 8802 | } |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 8803 | return task; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8804 | } |
| 8805 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8806 | /* This function is an LUA binding that register LUA function to be |
| 8807 | * executed after the HAProxy configuration parsing and before the |
| 8808 | * HAProxy scheduler starts. This function expect only one LUA |
| 8809 | * argument that is a function. This function returns nothing, but |
| 8810 | * throws if an error is encountered. |
| 8811 | */ |
| 8812 | __LJMP static int hlua_register_init(lua_State *L) |
| 8813 | { |
| 8814 | struct hlua_init_function *init; |
| 8815 | int ref; |
| 8816 | |
| 8817 | MAY_LJMP(check_args(L, 1, "register_init")); |
| 8818 | |
Aurelien DARRAGON | 87f5297 | 2023-02-24 09:43:54 +0100 | [diff] [blame] | 8819 | if (hlua_gethlua(L)) { |
| 8820 | /* runtime processing */ |
| 8821 | WILL_LJMP(luaL_error(L, "register_init: not available outside of body context")); |
| 8822 | } |
| 8823 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8824 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 8825 | |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 8826 | init = calloc(1, sizeof(*init)); |
Aurelien DARRAGON | 55afbed | 2023-03-02 18:42:06 +0100 | [diff] [blame] | 8827 | if (!init) { |
| 8828 | hlua_unref(L, ref); |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8829 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Aurelien DARRAGON | 55afbed | 2023-03-02 18:42:06 +0100 | [diff] [blame] | 8830 | } |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8831 | |
| 8832 | init->function_ref = ref; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 8833 | LIST_APPEND(&hlua_init_functions[hlua_state_id], &init->l); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8834 | return 0; |
| 8835 | } |
| 8836 | |
Ilya Shipitsin | 6f86eaa | 2022-11-30 16:22:42 +0500 | [diff] [blame] | 8837 | /* This function is an LUA binding. It permits to register a task |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8838 | * executed in parallel of the main HAroxy activity. The task is |
| 8839 | * created and it is set in the HAProxy scheduler. It can be called |
| 8840 | * from the "init" section, "post init" or during the runtime. |
| 8841 | * |
| 8842 | * Lua prototype: |
| 8843 | * |
Aurelien DARRAGON | b803899 | 2023-03-09 16:48:30 +0100 | [diff] [blame] | 8844 | * <none> core.register_task(<function>[, <arg1>[, <arg2>[, ...[, <arg4>]]]]) |
| 8845 | * |
| 8846 | * <arg1..4> are optional arguments that will be provided to <function> |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8847 | */ |
| 8848 | static int hlua_register_task(lua_State *L) |
| 8849 | { |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 8850 | struct hlua *hlua = NULL; |
| 8851 | struct task *task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8852 | int ref; |
Aurelien DARRAGON | b803899 | 2023-03-09 16:48:30 +0100 | [diff] [blame] | 8853 | int nb_arg; |
| 8854 | int it; |
| 8855 | int arg_ref[4]; /* optional arguments */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 8856 | int state_id; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8857 | |
Aurelien DARRAGON | b803899 | 2023-03-09 16:48:30 +0100 | [diff] [blame] | 8858 | nb_arg = lua_gettop(L); |
| 8859 | if (nb_arg < 1) |
| 8860 | WILL_LJMP(luaL_error(L, "register_task: <func> argument is required")); |
| 8861 | else if (nb_arg > 5) |
| 8862 | WILL_LJMP(luaL_error(L, "register_task: no more that 4 optional arguments may be provided")); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8863 | |
Aurelien DARRAGON | b803899 | 2023-03-09 16:48:30 +0100 | [diff] [blame] | 8864 | /* first arg: function ref */ |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8865 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 8866 | |
Aurelien DARRAGON | b803899 | 2023-03-09 16:48:30 +0100 | [diff] [blame] | 8867 | /* extract optional args (if any) */ |
| 8868 | it = 0; |
| 8869 | while (--nb_arg) { |
| 8870 | lua_pushvalue(L, 2 + it); |
| 8871 | arg_ref[it] = hlua_ref(L); /* get arg reference */ |
| 8872 | it += 1; |
| 8873 | } |
| 8874 | nb_arg = it; |
| 8875 | |
Thierry Fournier | 75fc029 | 2020-11-28 13:18:56 +0100 | [diff] [blame] | 8876 | /* Get the reference state. If the reference is NULL, L is the master |
| 8877 | * state, otherwise hlua->T is. |
| 8878 | */ |
| 8879 | hlua = hlua_gethlua(L); |
| 8880 | if (hlua) |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 8881 | /* we are in runtime processing */ |
| 8882 | state_id = hlua->state_id; |
Thierry Fournier | 75fc029 | 2020-11-28 13:18:56 +0100 | [diff] [blame] | 8883 | else |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 8884 | /* we are in initialization mode */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8885 | state_id = hlua_state_id; |
Thierry Fournier | 75fc029 | 2020-11-28 13:18:56 +0100 | [diff] [blame] | 8886 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 8887 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8888 | if (!hlua) |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 8889 | goto alloc_error; |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 8890 | HLUA_INIT(hlua); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8891 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8892 | /* We are in the common lua state, execute the task anywhere, |
| 8893 | * otherwise, inherit the current thread identifier |
| 8894 | */ |
| 8895 | if (state_id == 0) |
Willy Tarreau | beeabf5 | 2021-10-01 18:23:30 +0200 | [diff] [blame] | 8896 | task = task_new_anywhere(); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8897 | else |
Willy Tarreau | beeabf5 | 2021-10-01 18:23:30 +0200 | [diff] [blame] | 8898 | task = task_new_here(); |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 8899 | if (!task) |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 8900 | goto alloc_error; |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 8901 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8902 | task->context = hlua; |
| 8903 | task->process = hlua_process_task; |
| 8904 | |
Aurelien DARRAGON | 6b0b9bd | 2023-03-21 14:02:16 +0100 | [diff] [blame] | 8905 | if (!hlua_ctx_init(hlua, state_id, task)) |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 8906 | goto alloc_error; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8907 | |
Aurelien DARRAGON | b803899 | 2023-03-09 16:48:30 +0100 | [diff] [blame] | 8908 | /* Ensure there is enough space on the stack for the function |
| 8909 | * plus optional arguments |
| 8910 | */ |
| 8911 | if (!lua_checkstack(hlua->T, (1 + nb_arg))) |
| 8912 | goto alloc_error; |
| 8913 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8914 | /* Restore the function in the stack. */ |
Aurelien DARRAGON | 4fdf8b5 | 2023-03-20 17:22:37 +0100 | [diff] [blame] | 8915 | hlua_pushref(hlua->T, ref); |
Aurelien DARRAGON | be58d66 | 2023-03-13 14:09:21 +0100 | [diff] [blame] | 8916 | /* function ref not needed anymore since it was pushed to the substack */ |
| 8917 | hlua_unref(L, ref); |
| 8918 | |
Aurelien DARRAGON | b803899 | 2023-03-09 16:48:30 +0100 | [diff] [blame] | 8919 | hlua->nargs = nb_arg; |
| 8920 | |
| 8921 | /* push optional arguments to the function */ |
| 8922 | for (it = 0; it < nb_arg; it++) { |
| 8923 | /* push arg to the stack */ |
| 8924 | hlua_pushref(hlua->T, arg_ref[it]); |
| 8925 | /* arg ref not needed anymore since it was pushed to the substack */ |
| 8926 | hlua_unref(L, arg_ref[it]); |
| 8927 | } |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8928 | |
| 8929 | /* Schedule task. */ |
Willy Tarreau | e3957f8 | 2021-09-30 16:17:37 +0200 | [diff] [blame] | 8930 | task_wakeup(task, TASK_WOKEN_INIT); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8931 | |
| 8932 | return 0; |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 8933 | |
| 8934 | alloc_error: |
| 8935 | task_destroy(task); |
Aurelien DARRAGON | 55afbed | 2023-03-02 18:42:06 +0100 | [diff] [blame] | 8936 | hlua_unref(L, ref); |
Aurelien DARRAGON | b803899 | 2023-03-09 16:48:30 +0100 | [diff] [blame] | 8937 | for (it = 0; it < nb_arg; it++) { |
| 8938 | hlua_unref(L, arg_ref[it]); |
| 8939 | } |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 8940 | hlua_ctx_destroy(hlua); |
| 8941 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 8942 | return 0; /* Never reached */ |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8943 | } |
| 8944 | |
Aurelien DARRAGON | c84899c | 2023-02-20 18:18:59 +0100 | [diff] [blame] | 8945 | /* called from unsafe location */ |
| 8946 | static void hlua_event_subscription_destroy(struct hlua_event_sub *hlua_sub) |
| 8947 | { |
| 8948 | /* hlua cleanup */ |
| 8949 | |
| 8950 | hlua_lock(hlua_sub->hlua); |
| 8951 | /* registry is shared between coroutines */ |
| 8952 | hlua_unref(hlua_sub->hlua->T, hlua_sub->fcn_ref); |
| 8953 | hlua_unlock(hlua_sub->hlua); |
| 8954 | |
| 8955 | hlua_ctx_destroy(hlua_sub->hlua); |
| 8956 | |
| 8957 | /* free */ |
| 8958 | pool_free(pool_head_hlua_event_sub, hlua_sub); |
| 8959 | } |
| 8960 | |
| 8961 | /* single event handler: hlua ctx is shared between multiple events handlers |
| 8962 | * issued from the same subscription. Thus, it is not destroyed when the event |
| 8963 | * is processed: it is destroyed when no more events are expected for the |
| 8964 | * subscription (ie: when the subscription ends). |
| 8965 | * |
| 8966 | * Moreover, events are processed sequentially within the subscription: |
| 8967 | * one event must be fully processed before another one may be processed. |
| 8968 | * This ensures proper consistency for lua event handling from an ordering |
| 8969 | * point of view. This is especially useful with server events for example |
| 8970 | * where ADD/DEL/UP/DOWN events ordering really matters to trigger specific |
| 8971 | * actions from lua (e.g.: sending emails or making API calls). |
| 8972 | * |
| 8973 | * Due to this design, each lua event handler is pleased to process the event |
| 8974 | * as fast as possible to prevent the event queue from growing up. |
| 8975 | * Strictly speaking, there is no runtime limit for the callback function |
| 8976 | * (timeout set to default task timeout), but if the event queue goes past |
| 8977 | * the limit of unconsumed events an error will be reported and the |
| 8978 | * susbscription will pause itself for as long as it takes for the handler to |
| 8979 | * catch up (events will be lost as a result). |
| 8980 | * If the event handler does not need the sequential ordering and wants to |
| 8981 | * process multiple events at a time, it may spawn a new side-task using |
| 8982 | * 'core.register_task' to delegate the event handling and make parallel event |
| 8983 | * processing within the same subscription set. |
| 8984 | */ |
| 8985 | static void hlua_event_handler(struct hlua *hlua) |
| 8986 | { |
| 8987 | enum hlua_exec status; |
| 8988 | |
| 8989 | /* If it is the first call to the task, we must initialize the |
| 8990 | * execution timeouts. |
| 8991 | */ |
Aurelien DARRAGON | da9503c | 2022-11-25 09:10:07 +0100 | [diff] [blame] | 8992 | if (!HLUA_IS_RUNNING(hlua)) |
| 8993 | hlua_timer_init(&hlua->timer, hlua_timeout_task); |
Aurelien DARRAGON | c84899c | 2023-02-20 18:18:59 +0100 | [diff] [blame] | 8994 | |
| 8995 | /* make sure to reset the task expiry before each hlua_ctx_resume() |
| 8996 | * since the task is re-used for multiple cb function calls |
| 8997 | * We couldn't risk to have t->expire pointing to a past date because |
| 8998 | * it was set during last function invocation but was never reset since |
| 8999 | * (ie: E_AGAIN) |
| 9000 | */ |
| 9001 | hlua->task->expire = TICK_ETERNITY; |
| 9002 | |
| 9003 | /* Execute the Lua code. */ |
| 9004 | status = hlua_ctx_resume(hlua, 1); |
| 9005 | |
| 9006 | switch (status) { |
| 9007 | /* finished or yield */ |
| 9008 | case HLUA_E_OK: |
| 9009 | break; |
| 9010 | |
| 9011 | case HLUA_E_AGAIN: /* co process or timeout wake me later. */ |
| 9012 | notification_gc(&hlua->com); |
| 9013 | hlua->task->expire = hlua->wake_time; |
| 9014 | break; |
| 9015 | |
| 9016 | /* finished with error. */ |
| 9017 | case HLUA_E_ETMOUT: |
| 9018 | SEND_ERR(NULL, "Lua event_hdl: execution timeout.\n"); |
| 9019 | break; |
| 9020 | |
| 9021 | case HLUA_E_ERRMSG: |
| 9022 | SEND_ERR(NULL, "Lua event_hdl: %s.\n", lua_tostring(hlua->T, -1)); |
| 9023 | break; |
| 9024 | |
| 9025 | case HLUA_E_ERR: |
| 9026 | default: |
| 9027 | SEND_ERR(NULL, "Lua event_hdl: unknown error.\n"); |
| 9028 | break; |
| 9029 | } |
| 9030 | } |
| 9031 | |
Aurelien DARRAGON | 2f6a07d | 2023-03-27 18:16:21 +0200 | [diff] [blame] | 9032 | /* This function pushes various arguments such as event type and event data to |
| 9033 | * the lua function that will be called to consume the event. |
| 9034 | */ |
| 9035 | __LJMP static void hlua_event_hdl_cb_push_args(struct hlua_event_sub *hlua_sub, |
| 9036 | struct event_hdl_async_event *e) |
Aurelien DARRAGON | c84899c | 2023-02-20 18:18:59 +0100 | [diff] [blame] | 9037 | { |
| 9038 | struct hlua *hlua = hlua_sub->hlua; |
Aurelien DARRAGON | 2f6a07d | 2023-03-27 18:16:21 +0200 | [diff] [blame] | 9039 | struct event_hdl_sub_type event = e->type; |
| 9040 | void *data = e->data; |
Aurelien DARRAGON | c84899c | 2023-02-20 18:18:59 +0100 | [diff] [blame] | 9041 | |
Aurelien DARRAGON | 2f6a07d | 2023-03-27 18:16:21 +0200 | [diff] [blame] | 9042 | /* push event type */ |
Aurelien DARRAGON | c84899c | 2023-02-20 18:18:59 +0100 | [diff] [blame] | 9043 | hlua->nargs = 1; |
| 9044 | lua_pushstring(hlua->T, event_hdl_sub_type_to_string(event)); |
Aurelien DARRAGON | 2f6a07d | 2023-03-27 18:16:21 +0200 | [diff] [blame] | 9045 | |
| 9046 | /* push event data (according to event type) */ |
Aurelien DARRAGON | c84899c | 2023-02-20 18:18:59 +0100 | [diff] [blame] | 9047 | if (event_hdl_sub_family_equal(EVENT_HDL_SUB_SERVER, event)) { |
| 9048 | struct event_hdl_cb_data_server *e_server = data; |
| 9049 | struct proxy *px; |
| 9050 | struct server *server; |
| 9051 | |
| 9052 | hlua->nargs += 1; |
| 9053 | lua_newtable(hlua->T); |
| 9054 | /* Add server name */ |
| 9055 | lua_pushstring(hlua->T, "name"); |
| 9056 | lua_pushstring(hlua->T, e_server->safe.name); |
| 9057 | lua_settable(hlua->T, -3); |
| 9058 | /* Add server puid */ |
| 9059 | lua_pushstring(hlua->T, "puid"); |
| 9060 | lua_pushinteger(hlua->T, e_server->safe.puid); |
| 9061 | lua_settable(hlua->T, -3); |
| 9062 | /* Add server rid */ |
| 9063 | lua_pushstring(hlua->T, "rid"); |
| 9064 | lua_pushinteger(hlua->T, e_server->safe.rid); |
| 9065 | lua_settable(hlua->T, -3); |
| 9066 | /* Add server proxy name */ |
| 9067 | lua_pushstring(hlua->T, "proxy_name"); |
| 9068 | lua_pushstring(hlua->T, e_server->safe.proxy_name); |
| 9069 | lua_settable(hlua->T, -3); |
Aurelien DARRAGON | 55f84c7 | 2023-03-22 17:49:04 +0100 | [diff] [blame] | 9070 | /* Add server proxy uuid */ |
| 9071 | lua_pushstring(hlua->T, "proxy_uuid"); |
| 9072 | lua_pushinteger(hlua->T, e_server->safe.proxy_uuid); |
| 9073 | lua_settable(hlua->T, -3); |
Aurelien DARRAGON | c84899c | 2023-02-20 18:18:59 +0100 | [diff] [blame] | 9074 | |
| 9075 | /* attempt to provide reference server object |
| 9076 | * (if it wasn't removed yet, SERVER_DEL will never succeed here) |
| 9077 | */ |
Aurelien DARRAGON | 3d9bf4e | 2023-03-22 17:46:12 +0100 | [diff] [blame] | 9078 | px = proxy_find_by_id(e_server->safe.proxy_uuid, PR_CAP_BE, 0); |
Aurelien DARRAGON | c84899c | 2023-02-20 18:18:59 +0100 | [diff] [blame] | 9079 | BUG_ON(!px); |
| 9080 | server = findserver_unique_id(px, e_server->safe.puid, e_server->safe.rid); |
| 9081 | if (server) { |
| 9082 | lua_pushstring(hlua->T, "reference"); |
| 9083 | hlua_fcn_new_server(hlua->T, server); |
| 9084 | lua_settable(hlua->T, -3); |
| 9085 | } |
| 9086 | } |
| 9087 | /* sub mgmt */ |
| 9088 | hlua->nargs += 1; |
| 9089 | hlua_fcn_new_event_sub(hlua->T, hlua_sub->sub); |
Aurelien DARRAGON | 096b383 | 2023-04-20 11:32:46 +0200 | [diff] [blame] | 9090 | |
| 9091 | /* when? */ |
| 9092 | hlua->nargs += 1; |
| 9093 | lua_pushinteger(hlua->T, e->when.tv_sec); |
Aurelien DARRAGON | c84899c | 2023-02-20 18:18:59 +0100 | [diff] [blame] | 9094 | } |
| 9095 | |
| 9096 | /* events runner: if there's an ongoing hlua event handling process, finish it |
| 9097 | * then, check if there are new events waiting to be processed |
| 9098 | * (events are processed sequentially) |
| 9099 | * |
| 9100 | * We have a safety measure to warn/guard if the event queue is growing up |
| 9101 | * too much due to many events being generated and lua handler is unable to |
| 9102 | * keep up the pace (e.g.: when the event queue grows past 100 unconsumed events). |
| 9103 | * TODO: make it tunable |
| 9104 | */ |
| 9105 | static struct task *hlua_event_runner(struct task *task, void *context, unsigned int state) |
| 9106 | { |
| 9107 | struct hlua_event_sub *hlua_sub = context; |
| 9108 | struct event_hdl_async_event *event; |
| 9109 | const char *error = NULL; |
| 9110 | |
| 9111 | if (!hlua_sub->paused && event_hdl_async_equeue_size(&hlua_sub->equeue) > 100) { |
| 9112 | const char *trace; |
| 9113 | |
| 9114 | /* We reached the limit of pending events in the queue: we should |
| 9115 | * warn the user, and temporarily pause the subscription to give a chance |
Ilya Shipitsin | ccf8012 | 2023-04-22 20:20:39 +0200 | [diff] [blame] | 9116 | * to the handler to catch up? (it also prevents resource shortage since |
Aurelien DARRAGON | c84899c | 2023-02-20 18:18:59 +0100 | [diff] [blame] | 9117 | * the queue could grow indefinitely otherwise) |
| 9118 | * TODO: find a way to inform the handler that it missed some events |
| 9119 | * (example: stats within the subscription in event_hdl api exposed via lua api?) |
| 9120 | * |
| 9121 | * Nonetheless, reaching this limit means that the handler is not fast enough |
| 9122 | * and/or that it subscribed to events that happen too frequently and did not |
| 9123 | * expect it. This could come from an inadequate design in the user's script. |
| 9124 | */ |
| 9125 | event_hdl_pause(hlua_sub->sub); |
| 9126 | hlua_sub->paused = 1; |
| 9127 | |
| 9128 | /* The following Lua calls can fail. */ |
| 9129 | if (!SET_SAFE_LJMP(hlua_sub->hlua)) |
| 9130 | trace = NULL; |
| 9131 | trace = hlua_traceback(hlua_sub->hlua->T, ", "); |
| 9132 | /* At this point the execution is safe. */ |
| 9133 | RESET_SAFE_LJMP(hlua_sub->hlua); |
| 9134 | |
| 9135 | ha_warning("Lua event_hdl: pausing the subscription because the function fails to keep up the pace (%u unconsumed events): %s\n", event_hdl_async_equeue_size(&hlua_sub->equeue), ((trace) ? trace : "")); |
| 9136 | } |
| 9137 | |
| 9138 | if (HLUA_IS_RUNNING(hlua_sub->hlua)) { |
| 9139 | /* ongoing hlua event handler, resume it */ |
| 9140 | hlua_event_handler(hlua_sub->hlua); |
| 9141 | } else if ((event = event_hdl_async_equeue_pop(&hlua_sub->equeue))) { /* check for new events */ |
| 9142 | if (event_hdl_sub_type_equal(event->type, EVENT_HDL_SUB_END)) { |
| 9143 | /* ending event: no more events to come */ |
| 9144 | event_hdl_async_free_event(event); |
| 9145 | task_destroy(task); |
| 9146 | hlua_event_subscription_destroy(hlua_sub); |
| 9147 | return NULL; |
| 9148 | } |
| 9149 | /* new event: start processing it */ |
| 9150 | |
| 9151 | /* The following Lua calls can fail. */ |
| 9152 | if (!SET_SAFE_LJMP(hlua_sub->hlua)) { |
| 9153 | if (lua_type(hlua_sub->hlua->T, -1) == LUA_TSTRING) |
| 9154 | error = lua_tostring(hlua_sub->hlua->T, -1); |
| 9155 | else |
| 9156 | error = "critical error"; |
| 9157 | ha_alert("Lua event_hdl: %s.\n", error); |
| 9158 | goto skip_event; |
| 9159 | } |
| 9160 | |
| 9161 | /* Check stack available size. */ |
| 9162 | if (!lua_checkstack(hlua_sub->hlua->T, 5)) { |
| 9163 | ha_alert("Lua event_hdl: full stack.\n"); |
| 9164 | RESET_SAFE_LJMP(hlua_sub->hlua); |
| 9165 | goto skip_event; |
| 9166 | } |
| 9167 | |
| 9168 | /* Restore the function in the stack. */ |
| 9169 | hlua_pushref(hlua_sub->hlua->T, hlua_sub->fcn_ref); |
| 9170 | |
| 9171 | /* push args */ |
| 9172 | hlua_sub->hlua->nargs = 0; |
Aurelien DARRAGON | 2f6a07d | 2023-03-27 18:16:21 +0200 | [diff] [blame] | 9173 | MAY_LJMP(hlua_event_hdl_cb_push_args(hlua_sub, event)); |
Aurelien DARRAGON | c84899c | 2023-02-20 18:18:59 +0100 | [diff] [blame] | 9174 | |
| 9175 | /* At this point the execution is safe. */ |
| 9176 | RESET_SAFE_LJMP(hlua_sub->hlua); |
| 9177 | |
| 9178 | /* At this point the event was successfully translated into hlua ctx, |
Ilya Shipitsin | ccf8012 | 2023-04-22 20:20:39 +0200 | [diff] [blame] | 9179 | * or hlua error occurred, so we can safely discard it |
Aurelien DARRAGON | c84899c | 2023-02-20 18:18:59 +0100 | [diff] [blame] | 9180 | */ |
| 9181 | event_hdl_async_free_event(event); |
| 9182 | event = NULL; |
| 9183 | |
| 9184 | hlua_event_handler(hlua_sub->hlua); |
| 9185 | skip_event: |
| 9186 | if (event) |
| 9187 | event_hdl_async_free_event(event); |
| 9188 | |
| 9189 | } |
| 9190 | |
| 9191 | if (!HLUA_IS_RUNNING(hlua_sub->hlua)) { |
| 9192 | /* we just finished the processing of one event.. |
| 9193 | * check for new events before becoming idle |
| 9194 | */ |
| 9195 | if (!event_hdl_async_equeue_isempty(&hlua_sub->equeue)) { |
| 9196 | /* more events to process, make sure the task |
| 9197 | * will be resumed ASAP to process pending events |
| 9198 | */ |
| 9199 | task_wakeup(task, TASK_WOKEN_OTHER); |
| 9200 | } |
| 9201 | else if (hlua_sub->paused) { |
Ilya Shipitsin | ccf8012 | 2023-04-22 20:20:39 +0200 | [diff] [blame] | 9202 | /* empty queue, the handler caught up: resume the subscription */ |
Aurelien DARRAGON | c84899c | 2023-02-20 18:18:59 +0100 | [diff] [blame] | 9203 | event_hdl_resume(hlua_sub->sub); |
| 9204 | hlua_sub->paused = 0; |
| 9205 | } |
| 9206 | } |
| 9207 | |
| 9208 | return task; |
| 9209 | } |
| 9210 | |
| 9211 | /* Must be called directly under lua protected/safe environment |
| 9212 | * (not from external callback) |
| 9213 | * <fcn_ref> should NOT be dropped after the function successfully returns: |
| 9214 | * it will be done automatically in hlua_event_subscription_destroy() when the |
| 9215 | * subscription ends. |
| 9216 | * |
| 9217 | * Returns the new subscription on success and NULL on failure (memory error) |
| 9218 | */ |
| 9219 | static struct event_hdl_sub *hlua_event_subscribe(event_hdl_sub_list *list, struct event_hdl_sub_type e_type, |
| 9220 | int state_id, int fcn_ref) |
| 9221 | { |
| 9222 | struct hlua_event_sub *hlua_sub; |
| 9223 | struct task *task = NULL; |
| 9224 | |
| 9225 | hlua_sub = pool_alloc(pool_head_hlua_event_sub); |
| 9226 | if (!hlua_sub) |
| 9227 | goto mem_error; |
| 9228 | hlua_sub->task = NULL; |
| 9229 | hlua_sub->hlua = NULL; |
| 9230 | hlua_sub->paused = 0; |
| 9231 | if ((task = task_new_here()) == NULL) { |
| 9232 | ha_alert("out of memory while allocating hlua event task"); |
| 9233 | goto mem_error; |
| 9234 | } |
| 9235 | task->process = hlua_event_runner; |
| 9236 | task->context = hlua_sub; |
| 9237 | event_hdl_async_equeue_init(&hlua_sub->equeue); |
| 9238 | hlua_sub->task = task; |
| 9239 | hlua_sub->fcn_ref = fcn_ref; |
| 9240 | hlua_sub->state_id = state_id; |
| 9241 | hlua_sub->hlua = pool_alloc(pool_head_hlua); |
| 9242 | if (!hlua_sub->hlua) |
| 9243 | goto mem_error; |
| 9244 | HLUA_INIT(hlua_sub->hlua); |
| 9245 | if (!hlua_ctx_init(hlua_sub->hlua, hlua_sub->state_id, task)) |
| 9246 | goto mem_error; |
| 9247 | |
| 9248 | hlua_sub->sub = event_hdl_subscribe_ptr(list, e_type, |
| 9249 | EVENT_HDL_ASYNC_TASK(&hlua_sub->equeue, |
| 9250 | task, |
| 9251 | hlua_sub, |
| 9252 | NULL)); |
| 9253 | if (!hlua_sub->sub) |
| 9254 | goto mem_error; |
| 9255 | |
| 9256 | return hlua_sub->sub; /* returns pointer to event_hdl_sub struct */ |
| 9257 | |
| 9258 | mem_error: |
| 9259 | if (hlua_sub) { |
Tim Duesterhus | fe83f58 | 2023-04-22 17:47:34 +0200 | [diff] [blame] | 9260 | task_destroy(hlua_sub->task); |
Aurelien DARRAGON | c84899c | 2023-02-20 18:18:59 +0100 | [diff] [blame] | 9261 | if (hlua_sub->hlua) |
| 9262 | hlua_ctx_destroy(hlua_sub->hlua); |
| 9263 | pool_free(pool_head_hlua_event_sub, hlua_sub); |
| 9264 | } |
| 9265 | |
| 9266 | return NULL; |
| 9267 | } |
| 9268 | |
| 9269 | /* looks for an array of strings referring to a composition of event_hdl subscription |
| 9270 | * types at <index> in <L> stack |
| 9271 | */ |
| 9272 | __LJMP static struct event_hdl_sub_type hlua_check_event_sub_types(lua_State *L, int index) |
| 9273 | { |
| 9274 | struct event_hdl_sub_type subscriptions; |
| 9275 | const char *msg; |
| 9276 | |
| 9277 | if (lua_type(L, index) != LUA_TTABLE) { |
| 9278 | msg = lua_pushfstring(L, "table of strings expected, got %s", luaL_typename(L, index)); |
| 9279 | luaL_argerror(L, index, msg); |
| 9280 | } |
| 9281 | |
| 9282 | subscriptions = EVENT_HDL_SUB_NONE; |
| 9283 | |
| 9284 | /* browse the argument as an array. */ |
| 9285 | lua_pushnil(L); |
| 9286 | while (lua_next(L, index) != 0) { |
| 9287 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 9288 | msg = lua_pushfstring(L, "table of strings expected, got %s", luaL_typename(L, index)); |
| 9289 | luaL_argerror(L, index, msg); |
| 9290 | } |
| 9291 | |
| 9292 | if (event_hdl_sub_type_equal(EVENT_HDL_SUB_NONE, event_hdl_string_to_sub_type(lua_tostring(L, -1)))) { |
| 9293 | msg = lua_pushfstring(L, "'%s' event type is unknown", lua_tostring(L, -1)); |
| 9294 | luaL_argerror(L, index, msg); |
| 9295 | } |
| 9296 | |
| 9297 | /* perform subscriptions |= current sub */ |
| 9298 | subscriptions = event_hdl_sub_type_add(subscriptions, event_hdl_string_to_sub_type(lua_tostring(L, -1))); |
| 9299 | |
| 9300 | /* pop the current value. */ |
| 9301 | lua_pop(L, 1); |
| 9302 | } |
| 9303 | |
| 9304 | return subscriptions; |
| 9305 | } |
| 9306 | |
| 9307 | /* Wrapper for hlua_fcn_new_event_sub(): catch errors raised by |
| 9308 | * the function to prevent LJMP |
| 9309 | * |
Ilya Shipitsin | ccf8012 | 2023-04-22 20:20:39 +0200 | [diff] [blame] | 9310 | * If no error occurred, the function returns 1, else it returns 0 and |
Aurelien DARRAGON | c84899c | 2023-02-20 18:18:59 +0100 | [diff] [blame] | 9311 | * the error message is pushed at the top of the stack |
| 9312 | */ |
| 9313 | __LJMP static int _hlua_new_event_sub_safe(lua_State *L) |
| 9314 | { |
| 9315 | struct event_hdl_sub *sub = lua_touserdata(L, 1); |
| 9316 | |
| 9317 | /* this function may raise errors */ |
| 9318 | return MAY_LJMP(hlua_fcn_new_event_sub(L, sub)); |
| 9319 | } |
| 9320 | static int hlua_new_event_sub_safe(lua_State *L, struct event_hdl_sub *sub) |
| 9321 | { |
| 9322 | if (!lua_checkstack(L, 2)) |
| 9323 | return 0; |
| 9324 | lua_pushcfunction(L, _hlua_new_event_sub_safe); |
| 9325 | lua_pushlightuserdata(L, sub); |
| 9326 | switch (lua_pcall(L, 1, 1, 0)) { |
| 9327 | case LUA_OK: |
| 9328 | return 1; |
| 9329 | default: |
Ilya Shipitsin | ccf8012 | 2023-04-22 20:20:39 +0200 | [diff] [blame] | 9330 | /* error was caught */ |
Aurelien DARRAGON | c84899c | 2023-02-20 18:18:59 +0100 | [diff] [blame] | 9331 | return 0; |
| 9332 | } |
| 9333 | } |
| 9334 | |
| 9335 | /* This function is a LUA helper used for registering lua event callbacks. |
| 9336 | * It expects an event subscription array and the function to be executed |
| 9337 | * when subscribed events occur (stack arguments). |
| 9338 | * It can be called from the "init" section, "post init" or during the runtime. |
| 9339 | * |
| 9340 | * <sub_list> is the subscription list where the subscription will be attempted |
| 9341 | * |
| 9342 | * Pushes the newly allocated subscription on the stack on success |
| 9343 | */ |
| 9344 | __LJMP int hlua_event_sub(lua_State *L, event_hdl_sub_list *sub_list) |
| 9345 | { |
| 9346 | struct hlua *hlua; |
| 9347 | struct event_hdl_sub *sub; |
| 9348 | struct event_hdl_sub_type subscriptions; |
| 9349 | int fcn_ref; |
| 9350 | int state_id; |
| 9351 | |
| 9352 | MAY_LJMP(check_args(L, 2, "event_sub")); |
| 9353 | |
| 9354 | /* Get the reference state */ |
| 9355 | hlua = hlua_gethlua(L); |
| 9356 | if (hlua) |
| 9357 | /* we are in runtime processing, any thread may subscribe to events: |
| 9358 | * subscription events will be handled by the thread who performed |
| 9359 | * the registration. |
| 9360 | */ |
| 9361 | state_id = hlua->state_id; |
| 9362 | else { |
| 9363 | /* we are in initialization mode, only thread 0 (actual calling thread) |
| 9364 | * may subscribe to events to prevent the same handler (from different lua |
| 9365 | * stacks) from being registered multiple times |
| 9366 | * |
| 9367 | * hlua_state_id == 0: monostack (lua-load) |
| 9368 | * hlua_state_id > 0: hlua_state_id=tid+1, multi-stack (lua-load-per-thread) |
| 9369 | * (thus if hlua_state_id > 1, it means we are not in primary thread ctx) |
| 9370 | */ |
| 9371 | if (hlua_state_id > 1) |
| 9372 | return 0; /* skip registration */ |
| 9373 | state_id = hlua_state_id; |
| 9374 | } |
| 9375 | |
| 9376 | /* First argument : event subscriptions. */ |
| 9377 | subscriptions = MAY_LJMP(hlua_check_event_sub_types(L, 1)); |
| 9378 | |
| 9379 | if (event_hdl_sub_type_equal(subscriptions, EVENT_HDL_SUB_NONE)) { |
| 9380 | WILL_LJMP(luaL_error(L, "event_sub: no valid event types were provided")); |
| 9381 | return 0; /* Never reached */ |
| 9382 | } |
| 9383 | |
| 9384 | /* Second argument : lua function. */ |
| 9385 | fcn_ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 9386 | |
| 9387 | /* try to subscribe */ |
| 9388 | sub = hlua_event_subscribe(sub_list, subscriptions, state_id, fcn_ref); |
| 9389 | if (!sub) { |
| 9390 | hlua_unref(L, fcn_ref); |
| 9391 | WILL_LJMP(luaL_error(L, "event_sub: lua out of memory error")); |
| 9392 | return 0; /* Never reached */ |
| 9393 | } |
| 9394 | |
| 9395 | /* push the subscription to the stack |
| 9396 | * |
| 9397 | * Here we use the safe function so that lua errors will be |
| 9398 | * handled explicitly to prevent 'sub' from being lost |
| 9399 | */ |
| 9400 | if (!hlua_new_event_sub_safe(L, sub)) { |
| 9401 | /* Some events could already be pending in the handler's queue. |
| 9402 | * However it is wiser to cancel the subscription since we are unable to |
| 9403 | * provide a valid reference to it. |
| 9404 | * Pending events will be delivered (unless lua keeps raising errors). |
| 9405 | */ |
| 9406 | event_hdl_unsubscribe(sub); /* cancel the subscription */ |
| 9407 | WILL_LJMP(luaL_error(L, "event_sub: cannot push the subscription (%s)", lua_tostring(L, -1))); |
| 9408 | return 0; /* Never reached */ |
| 9409 | } |
| 9410 | event_hdl_drop(sub); /* sub has been duplicated, discard old ref */ |
| 9411 | |
| 9412 | return 1; |
| 9413 | } |
| 9414 | |
| 9415 | /* This function is a LUA wrapper used for registering global lua event callbacks |
| 9416 | * The new subscription is pushed onto the stack on success |
| 9417 | * Returns the number of arguments pushed to the stack (1 for success) |
| 9418 | */ |
| 9419 | __LJMP static int hlua_event_global_sub(lua_State *L) |
| 9420 | { |
| 9421 | /* NULL <sub_list> = global subscription list */ |
| 9422 | return MAY_LJMP(hlua_event_sub(L, NULL)); |
| 9423 | } |
| 9424 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 9425 | /* Wrapper called by HAProxy to execute an LUA converter. This wrapper |
| 9426 | * doesn't allow "yield" functions because the HAProxy engine cannot |
| 9427 | * resume converters. |
| 9428 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 9429 | 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] | 9430 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 9431 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 9432 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 9433 | const char *error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 9434 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 9435 | if (!stream) |
| 9436 | return 0; |
| 9437 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 9438 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 9439 | * Lua context can be not initialized. This behavior |
| 9440 | * permits to save performances because a systematic |
| 9441 | * Lua initialization cause 5% performances loss. |
| 9442 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9443 | if (!stream->hlua) { |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 9444 | struct hlua *hlua; |
| 9445 | |
| 9446 | hlua = pool_alloc(pool_head_hlua); |
| 9447 | if (!hlua) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9448 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 9449 | return 0; |
| 9450 | } |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 9451 | HLUA_INIT(hlua); |
| 9452 | stream->hlua = hlua; |
Aurelien DARRAGON | 6b0b9bd | 2023-03-21 14:02:16 +0100 | [diff] [blame] | 9453 | if (!hlua_ctx_init(stream->hlua, fcn_ref_to_stack_id(fcn), stream->task)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9454 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 9455 | return 0; |
| 9456 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 9457 | } |
| 9458 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 9459 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9460 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 9461 | |
| 9462 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9463 | if (!SET_SAFE_LJMP(stream->hlua)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9464 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 9465 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 9466 | else |
| 9467 | error = "critical error"; |
| 9468 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 9469 | return 0; |
| 9470 | } |
| 9471 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 9472 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9473 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9474 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9475 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 9476 | return 0; |
| 9477 | } |
| 9478 | |
| 9479 | /* Restore the function in the stack. */ |
Aurelien DARRAGON | 4fdf8b5 | 2023-03-20 17:22:37 +0100 | [diff] [blame] | 9480 | hlua_pushref(stream->hlua->T, fcn->function_ref[stream->hlua->state_id]); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 9481 | |
| 9482 | /* convert input sample and pust-it in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9483 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9484 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9485 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 9486 | return 0; |
| 9487 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9488 | hlua_smp2lua(stream->hlua->T, smp); |
| 9489 | stream->hlua->nargs = 1; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 9490 | |
| 9491 | /* push keywords in the stack. */ |
| 9492 | if (arg_p) { |
| 9493 | for (; arg_p->type != ARGT_STOP; arg_p++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9494 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9495 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9496 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 9497 | return 0; |
| 9498 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9499 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 9500 | stream->hlua->nargs++; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 9501 | } |
| 9502 | } |
| 9503 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 9504 | /* We must initialize the execution timeouts. */ |
Aurelien DARRAGON | da9503c | 2022-11-25 09:10:07 +0100 | [diff] [blame] | 9505 | hlua_timer_init(&stream->hlua->timer, hlua_timeout_session); |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 9506 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 9507 | /* At this point the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9508 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 9509 | } |
| 9510 | |
| 9511 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9512 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 9513 | /* finished. */ |
| 9514 | case HLUA_E_OK: |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 9515 | /* If the stack is empty, the function fails. */ |
| 9516 | if (lua_gettop(stream->hlua->T) <= 0) |
| 9517 | return 0; |
| 9518 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 9519 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9520 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 9521 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 9522 | return 1; |
| 9523 | |
| 9524 | /* yield. */ |
| 9525 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9526 | 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] | 9527 | return 0; |
| 9528 | |
| 9529 | /* finished with error. */ |
| 9530 | case HLUA_E_ERRMSG: |
| 9531 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9532 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9533 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 9534 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 9535 | return 0; |
| 9536 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9537 | case HLUA_E_ETMOUT: |
| 9538 | SEND_ERR(stream->be, "Lua converter '%s': execution timeout.\n", fcn->name); |
| 9539 | return 0; |
| 9540 | |
| 9541 | case HLUA_E_NOMEM: |
| 9542 | SEND_ERR(stream->be, "Lua converter '%s': out of memory error.\n", fcn->name); |
| 9543 | return 0; |
| 9544 | |
| 9545 | case HLUA_E_YIELD: |
| 9546 | SEND_ERR(stream->be, "Lua converter '%s': yield functions like core.tcp() or core.sleep() are not allowed.\n", fcn->name); |
| 9547 | return 0; |
| 9548 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 9549 | case HLUA_E_ERR: |
| 9550 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9551 | SEND_ERR(stream->be, "Lua converter '%s' returns an unknown error.\n", fcn->name); |
Willy Tarreau | 14de395 | 2022-11-14 07:08:28 +0100 | [diff] [blame] | 9552 | __fallthrough; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 9553 | |
| 9554 | default: |
| 9555 | return 0; |
| 9556 | } |
| 9557 | } |
| 9558 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9559 | /* Wrapper called by HAProxy to execute a sample-fetch. this wrapper |
| 9560 | * doesn't allow "yield" functions because the HAProxy engine cannot |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 9561 | * resume sample-fetches. This function will be called by the sample |
| 9562 | * fetch engine to call lua-based fetch operations. |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9563 | */ |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9564 | static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp, |
| 9565 | const char *kw, void *private) |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9566 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 9567 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 9568 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 9569 | const char *error; |
Christopher Faulet | 8c9e6bb | 2021-08-06 16:29:41 +0200 | [diff] [blame] | 9570 | unsigned int hflags = HLUA_TXN_NOTERM | HLUA_TXN_SMP_CTX; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9571 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 9572 | if (!stream) |
| 9573 | return 0; |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 9574 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 9575 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 9576 | * Lua context can be not initialized. This behavior |
| 9577 | * permits to save performances because a systematic |
| 9578 | * Lua initialization cause 5% performances loss. |
| 9579 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9580 | if (!stream->hlua) { |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 9581 | struct hlua *hlua; |
| 9582 | |
| 9583 | hlua = pool_alloc(pool_head_hlua); |
| 9584 | if (!hlua) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9585 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 9586 | return 0; |
| 9587 | } |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 9588 | hlua->T = NULL; |
| 9589 | stream->hlua = hlua; |
Aurelien DARRAGON | 6b0b9bd | 2023-03-21 14:02:16 +0100 | [diff] [blame] | 9590 | if (!hlua_ctx_init(stream->hlua, fcn_ref_to_stack_id(fcn), stream->task)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9591 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 9592 | return 0; |
| 9593 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 9594 | } |
| 9595 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9596 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9597 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 9598 | |
| 9599 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9600 | if (!SET_SAFE_LJMP(stream->hlua)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9601 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 9602 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 9603 | else |
| 9604 | error = "critical error"; |
| 9605 | 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] | 9606 | return 0; |
| 9607 | } |
| 9608 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9609 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9610 | if (!lua_checkstack(stream->hlua->T, 2)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9611 | 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] | 9612 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9613 | return 0; |
| 9614 | } |
| 9615 | |
| 9616 | /* Restore the function in the stack. */ |
Aurelien DARRAGON | 4fdf8b5 | 2023-03-20 17:22:37 +0100 | [diff] [blame] | 9617 | hlua_pushref(stream->hlua->T, fcn->function_ref[stream->hlua->state_id]); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9618 | |
| 9619 | /* push arguments in the stack. */ |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 9620 | 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] | 9621 | 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] | 9622 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9623 | return 0; |
| 9624 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9625 | stream->hlua->nargs = 1; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9626 | |
| 9627 | /* push keywords in the stack. */ |
| 9628 | for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) { |
| 9629 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9630 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9631 | 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] | 9632 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9633 | return 0; |
| 9634 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9635 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 9636 | stream->hlua->nargs++; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9637 | } |
| 9638 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 9639 | /* We must initialize the execution timeouts. */ |
Aurelien DARRAGON | da9503c | 2022-11-25 09:10:07 +0100 | [diff] [blame] | 9640 | hlua_timer_init(&stream->hlua->timer, hlua_timeout_session); |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 9641 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 9642 | /* At this point the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9643 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9644 | } |
| 9645 | |
| 9646 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9647 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9648 | /* finished. */ |
| 9649 | case HLUA_E_OK: |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 9650 | /* If the stack is empty, the function fails. */ |
| 9651 | if (lua_gettop(stream->hlua->T) <= 0) |
| 9652 | return 0; |
| 9653 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9654 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9655 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 9656 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9657 | |
| 9658 | /* Set the end of execution flag. */ |
| 9659 | smp->flags &= ~SMP_F_MAY_CHANGE; |
| 9660 | return 1; |
| 9661 | |
| 9662 | /* yield. */ |
| 9663 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9664 | 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] | 9665 | return 0; |
| 9666 | |
| 9667 | /* finished with error. */ |
| 9668 | case HLUA_E_ERRMSG: |
| 9669 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9670 | SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9671 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 9672 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9673 | return 0; |
| 9674 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9675 | case HLUA_E_ETMOUT: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9676 | SEND_ERR(smp->px, "Lua sample-fetch '%s': execution timeout.\n", fcn->name); |
| 9677 | return 0; |
| 9678 | |
| 9679 | case HLUA_E_NOMEM: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9680 | SEND_ERR(smp->px, "Lua sample-fetch '%s': out of memory error.\n", fcn->name); |
| 9681 | return 0; |
| 9682 | |
| 9683 | case HLUA_E_YIELD: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9684 | SEND_ERR(smp->px, "Lua sample-fetch '%s': yield not allowed.\n", fcn->name); |
| 9685 | return 0; |
| 9686 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9687 | case HLUA_E_ERR: |
| 9688 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9689 | SEND_ERR(smp->px, "Lua sample-fetch '%s' returns an unknown error.\n", fcn->name); |
Willy Tarreau | 14de395 | 2022-11-14 07:08:28 +0100 | [diff] [blame] | 9690 | __fallthrough; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9691 | |
| 9692 | default: |
| 9693 | return 0; |
| 9694 | } |
| 9695 | } |
| 9696 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 9697 | /* This function is an LUA binding used for registering |
| 9698 | * "sample-conv" functions. It expects a converter name used |
| 9699 | * in the haproxy configuration file, and an LUA function. |
| 9700 | */ |
| 9701 | __LJMP static int hlua_register_converters(lua_State *L) |
| 9702 | { |
| 9703 | struct sample_conv_kw_list *sck; |
| 9704 | const char *name; |
| 9705 | int ref; |
| 9706 | int len; |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 9707 | struct hlua_function *fcn = NULL; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 9708 | struct sample_conv *sc; |
| 9709 | struct buffer *trash; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 9710 | |
| 9711 | MAY_LJMP(check_args(L, 2, "register_converters")); |
| 9712 | |
Aurelien DARRAGON | 87f5297 | 2023-02-24 09:43:54 +0100 | [diff] [blame] | 9713 | if (hlua_gethlua(L)) { |
| 9714 | /* runtime processing */ |
| 9715 | WILL_LJMP(luaL_error(L, "register_converters: not available outside of body context")); |
| 9716 | } |
| 9717 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 9718 | /* First argument : converter name. */ |
| 9719 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 9720 | |
| 9721 | /* Second argument : lua function. */ |
| 9722 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 9723 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 9724 | /* Check if the converter is already registered */ |
| 9725 | trash = get_trash_chunk(); |
| 9726 | chunk_printf(trash, "lua.%s", name); |
| 9727 | sc = find_sample_conv(trash->area, trash->data); |
| 9728 | if (sc != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 9729 | fcn = sc->private; |
| 9730 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 9731 | ha_warning("Trying to register converter 'lua.%s' more than once. " |
| 9732 | "This will become a hard error in version 2.5.\n", name); |
Aurelien DARRAGON | 55afbed | 2023-03-02 18:42:06 +0100 | [diff] [blame] | 9733 | hlua_unref(L, fcn->function_ref[hlua_state_id]); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 9734 | } |
| 9735 | fcn->function_ref[hlua_state_id] = ref; |
| 9736 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 9737 | } |
| 9738 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 9739 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 9740 | sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 9741 | if (!sck) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 9742 | goto alloc_error; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 9743 | fcn = new_hlua_function(); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 9744 | if (!fcn) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 9745 | goto alloc_error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 9746 | |
| 9747 | /* Fill fcn. */ |
| 9748 | fcn->name = strdup(name); |
| 9749 | if (!fcn->name) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 9750 | goto alloc_error; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9751 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 9752 | |
| 9753 | /* List head */ |
| 9754 | sck->list.n = sck->list.p = NULL; |
| 9755 | |
| 9756 | /* converter keyword. */ |
| 9757 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 9758 | sck->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 9759 | if (!sck->kw[0].kw) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 9760 | goto alloc_error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 9761 | |
| 9762 | snprintf((char *)sck->kw[0].kw, len, "lua.%s", name); |
| 9763 | sck->kw[0].process = hlua_sample_conv_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 9764 | 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] | 9765 | sck->kw[0].val_args = NULL; |
| 9766 | sck->kw[0].in_type = SMP_T_STR; |
| 9767 | sck->kw[0].out_type = SMP_T_STR; |
| 9768 | sck->kw[0].private = fcn; |
| 9769 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 9770 | /* Register this new converter */ |
| 9771 | sample_register_convs(sck); |
| 9772 | |
| 9773 | return 0; |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 9774 | |
| 9775 | alloc_error: |
| 9776 | release_hlua_function(fcn); |
Aurelien DARRAGON | 55afbed | 2023-03-02 18:42:06 +0100 | [diff] [blame] | 9777 | hlua_unref(L, ref); |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 9778 | ha_free(&sck); |
| 9779 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 9780 | return 0; /* Never reached */ |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 9781 | } |
| 9782 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 9783 | /* This function is an LUA binding used for registering |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9784 | * "sample-fetch" functions. It expects a converter name used |
| 9785 | * in the haproxy configuration file, and an LUA function. |
| 9786 | */ |
| 9787 | __LJMP static int hlua_register_fetches(lua_State *L) |
| 9788 | { |
| 9789 | const char *name; |
| 9790 | int ref; |
| 9791 | int len; |
| 9792 | struct sample_fetch_kw_list *sfk; |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 9793 | struct hlua_function *fcn = NULL; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 9794 | struct sample_fetch *sf; |
| 9795 | struct buffer *trash; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9796 | |
| 9797 | MAY_LJMP(check_args(L, 2, "register_fetches")); |
| 9798 | |
Aurelien DARRAGON | 87f5297 | 2023-02-24 09:43:54 +0100 | [diff] [blame] | 9799 | if (hlua_gethlua(L)) { |
| 9800 | /* runtime processing */ |
| 9801 | WILL_LJMP(luaL_error(L, "register_fetches: not available outside of body context")); |
| 9802 | } |
| 9803 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9804 | /* First argument : sample-fetch name. */ |
| 9805 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 9806 | |
| 9807 | /* Second argument : lua function. */ |
| 9808 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 9809 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 9810 | /* Check if the sample-fetch is already registered */ |
| 9811 | trash = get_trash_chunk(); |
| 9812 | chunk_printf(trash, "lua.%s", name); |
| 9813 | sf = find_sample_fetch(trash->area, trash->data); |
| 9814 | if (sf != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 9815 | fcn = sf->private; |
| 9816 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 9817 | ha_warning("Trying to register sample-fetch 'lua.%s' more than once. " |
| 9818 | "This will become a hard error in version 2.5.\n", name); |
Aurelien DARRAGON | 55afbed | 2023-03-02 18:42:06 +0100 | [diff] [blame] | 9819 | hlua_unref(L, fcn->function_ref[hlua_state_id]); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 9820 | } |
| 9821 | fcn->function_ref[hlua_state_id] = ref; |
| 9822 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 9823 | } |
| 9824 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9825 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 9826 | sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9827 | if (!sfk) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 9828 | goto alloc_error; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 9829 | fcn = new_hlua_function(); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9830 | if (!fcn) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 9831 | goto alloc_error; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9832 | |
| 9833 | /* Fill fcn. */ |
| 9834 | fcn->name = strdup(name); |
| 9835 | if (!fcn->name) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 9836 | goto alloc_error; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9837 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9838 | |
| 9839 | /* List head */ |
| 9840 | sfk->list.n = sfk->list.p = NULL; |
| 9841 | |
| 9842 | /* sample-fetch keyword. */ |
| 9843 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 9844 | sfk->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9845 | if (!sfk->kw[0].kw) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 9846 | goto alloc_error; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9847 | |
| 9848 | snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name); |
| 9849 | sfk->kw[0].process = hlua_sample_fetch_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 9850 | 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] | 9851 | sfk->kw[0].val_args = NULL; |
| 9852 | sfk->kw[0].out_type = SMP_T_STR; |
| 9853 | sfk->kw[0].use = SMP_USE_HTTP_ANY; |
| 9854 | sfk->kw[0].val = 0; |
| 9855 | sfk->kw[0].private = fcn; |
| 9856 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9857 | /* Register this new fetch. */ |
| 9858 | sample_register_fetches(sfk); |
| 9859 | |
| 9860 | return 0; |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 9861 | |
| 9862 | alloc_error: |
| 9863 | release_hlua_function(fcn); |
Aurelien DARRAGON | 55afbed | 2023-03-02 18:42:06 +0100 | [diff] [blame] | 9864 | hlua_unref(L, ref); |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 9865 | ha_free(&sfk); |
| 9866 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 9867 | return 0; /* Never reached */ |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9868 | } |
| 9869 | |
Christopher Faulet | 501465d | 2020-02-26 14:54:16 +0100 | [diff] [blame] | 9870 | /* This function is a lua binding to set the wake_time. |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 9871 | */ |
Christopher Faulet | 501465d | 2020-02-26 14:54:16 +0100 | [diff] [blame] | 9872 | __LJMP static int hlua_set_wake_time(lua_State *L) |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 9873 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 9874 | struct hlua *hlua; |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 9875 | unsigned int delay; |
Aurelien DARRAGON | 2a9764b | 2023-04-04 18:41:04 +0200 | [diff] [blame] | 9876 | int wakeup_ms; // tick value |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 9877 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 9878 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 9879 | hlua = hlua_gethlua(L); |
| 9880 | if (!hlua) { |
| 9881 | return 0; |
| 9882 | } |
| 9883 | |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 9884 | MAY_LJMP(check_args(L, 1, "wake_time")); |
| 9885 | |
| 9886 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 9887 | wakeup_ms = tick_add(now_ms, delay); |
| 9888 | hlua->wake_time = wakeup_ms; |
| 9889 | return 0; |
| 9890 | } |
| 9891 | |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9892 | /* This function is a wrapper to execute each LUA function declared as an action |
| 9893 | * wrapper during the initialisation period. This function may return any |
| 9894 | * ACT_RET_* value. On error ACT_RET_CONT is returned and the action is |
| 9895 | * ignored. If the lua action yields, ACT_RET_YIELD is returned. On success, the |
| 9896 | * return value is the first element on the stack. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9897 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9898 | 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] | 9899 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9900 | { |
| 9901 | char **arg; |
Christopher Faulet | 8c9e6bb | 2021-08-06 16:29:41 +0200 | [diff] [blame] | 9902 | unsigned int hflags = HLUA_TXN_ACT_CTX; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9903 | int dir, act_ret = ACT_RET_CONT; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 9904 | const char *error; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9905 | |
| 9906 | switch (rule->from) { |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 9907 | case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break; |
| 9908 | case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break; |
| 9909 | case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break; |
| 9910 | case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9911 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9912 | SEND_ERR(px, "Lua: internal error while execute action.\n"); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9913 | goto end; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9914 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9915 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 9916 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 9917 | * Lua context can be not initialized. This behavior |
| 9918 | * permits to save performances because a systematic |
| 9919 | * Lua initialization cause 5% performances loss. |
| 9920 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9921 | if (!s->hlua) { |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 9922 | struct hlua *hlua; |
| 9923 | |
| 9924 | hlua = pool_alloc(pool_head_hlua); |
| 9925 | if (!hlua) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9926 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9927 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9928 | goto end; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9929 | } |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 9930 | HLUA_INIT(hlua); |
| 9931 | s->hlua = hlua; |
Aurelien DARRAGON | 6b0b9bd | 2023-03-21 14:02:16 +0100 | [diff] [blame] | 9932 | if (!hlua_ctx_init(s->hlua, fcn_ref_to_stack_id(rule->arg.hlua_rule->fcn), s->task)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9933 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9934 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9935 | goto end; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9936 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 9937 | } |
| 9938 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9939 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9940 | if (!HLUA_IS_RUNNING(s->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 9941 | |
| 9942 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9943 | if (!SET_SAFE_LJMP(s->hlua)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9944 | if (lua_type(s->hlua->T, -1) == LUA_TSTRING) |
| 9945 | error = lua_tostring(s->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 9946 | else |
| 9947 | error = "critical error"; |
| 9948 | SEND_ERR(px, "Lua function '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9949 | rule->arg.hlua_rule->fcn->name, error); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9950 | goto end; |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 9951 | } |
| 9952 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9953 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9954 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9955 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9956 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9957 | RESET_SAFE_LJMP(s->hlua); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9958 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9959 | } |
| 9960 | |
| 9961 | /* Restore the function in the stack. */ |
Aurelien DARRAGON | 4fdf8b5 | 2023-03-20 17:22:37 +0100 | [diff] [blame] | 9962 | hlua_pushref(s->hlua->T, rule->arg.hlua_rule->fcn->function_ref[s->hlua->state_id]); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9963 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 9964 | /* Create and and push object stream in the stack. */ |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 9965 | if (!hlua_txn_new(s->hlua->T, s, px, dir, hflags)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9966 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9967 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9968 | RESET_SAFE_LJMP(s->hlua); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9969 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9970 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9971 | s->hlua->nargs = 1; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9972 | |
| 9973 | /* push keywords in the stack. */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9974 | for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9975 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9976 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9977 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9978 | RESET_SAFE_LJMP(s->hlua); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9979 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9980 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9981 | lua_pushstring(s->hlua->T, *arg); |
| 9982 | s->hlua->nargs++; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9983 | } |
| 9984 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 9985 | /* Now the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9986 | RESET_SAFE_LJMP(s->hlua); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 9987 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 9988 | /* We must initialize the execution timeouts. */ |
Aurelien DARRAGON | da9503c | 2022-11-25 09:10:07 +0100 | [diff] [blame] | 9989 | hlua_timer_init(&s->hlua->timer, hlua_timeout_session); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9990 | } |
| 9991 | |
| 9992 | /* Execute the function. */ |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 9993 | switch (hlua_ctx_resume(s->hlua, !(flags & ACT_OPT_FINAL))) { |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9994 | /* finished. */ |
| 9995 | case HLUA_E_OK: |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9996 | /* Catch the return value */ |
| 9997 | if (lua_gettop(s->hlua->T) > 0) |
| 9998 | act_ret = lua_tointeger(s->hlua->T, -1); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9999 | |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 10000 | /* Set timeout in the required channel. */ |
Christopher Faulet | 498c483 | 2020-07-28 11:59:58 +0200 | [diff] [blame] | 10001 | if (act_ret == ACT_RET_YIELD) { |
| 10002 | if (flags & ACT_OPT_FINAL) |
| 10003 | goto err_yield; |
| 10004 | |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 10005 | if (dir == SMP_OPT_DIR_REQ) |
| 10006 | s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp), |
| 10007 | s->hlua->wake_time); |
| 10008 | else |
| 10009 | s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp), |
| 10010 | s->hlua->wake_time); |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 10011 | } |
| 10012 | goto end; |
| 10013 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 10014 | /* yield. */ |
| 10015 | case HLUA_E_AGAIN: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 10016 | /* Set timeout in the required channel. */ |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 10017 | if (dir == SMP_OPT_DIR_REQ) |
| 10018 | s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp), |
| 10019 | s->hlua->wake_time); |
| 10020 | else |
| 10021 | s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp), |
| 10022 | s->hlua->wake_time); |
| 10023 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 10024 | /* Some actions can be wake up when a "write" event |
| 10025 | * is detected on a response channel. This is useful |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 10026 | * only for actions targeted on the requests. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 10027 | */ |
Christopher Faulet | 51fa358 | 2019-07-26 14:54:52 +0200 | [diff] [blame] | 10028 | if (HLUA_IS_WAKERESWR(s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 10029 | s->res.flags |= CF_WAKE_WRITE; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 10030 | if (HLUA_IS_WAKEREQWR(s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 10031 | s->req.flags |= CF_WAKE_WRITE; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 10032 | act_ret = ACT_RET_YIELD; |
| 10033 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 10034 | |
| 10035 | /* finished with error. */ |
| 10036 | case HLUA_E_ERRMSG: |
| 10037 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 10038 | SEND_ERR(px, "Lua function '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 10039 | rule->arg.hlua_rule->fcn->name, lua_tostring(s->hlua->T, -1)); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 10040 | lua_pop(s->hlua->T, 1); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 10041 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 10042 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 10043 | case HLUA_E_ETMOUT: |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 10044 | 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] | 10045 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 10046 | |
| 10047 | case HLUA_E_NOMEM: |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 10048 | 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] | 10049 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 10050 | |
| 10051 | case HLUA_E_YIELD: |
Christopher Faulet | 498c483 | 2020-07-28 11:59:58 +0200 | [diff] [blame] | 10052 | err_yield: |
| 10053 | act_ret = ACT_RET_CONT; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 10054 | SEND_ERR(px, "Lua function '%s': aborting Lua processing on expired timeout.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 10055 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 10056 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 10057 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 10058 | case HLUA_E_ERR: |
| 10059 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 10060 | SEND_ERR(px, "Lua function '%s' return an unknown error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 10061 | rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 10062 | |
| 10063 | default: |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 10064 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 10065 | } |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 10066 | |
| 10067 | end: |
Christopher Faulet | 2361fd9 | 2020-07-30 10:40:58 +0200 | [diff] [blame] | 10068 | if (act_ret != ACT_RET_YIELD && s->hlua) |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 10069 | s->hlua->wake_time = TICK_ETERNITY; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 10070 | return act_ret; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 10071 | } |
| 10072 | |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 10073 | 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] | 10074 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 10075 | struct appctx *ctx = context; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10076 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10077 | appctx_wakeup(ctx); |
Willy Tarreau | d958741 | 2017-08-23 16:07:33 +0200 | [diff] [blame] | 10078 | t->expire = TICK_ETERNITY; |
Willy Tarreau | d1aa41f | 2017-07-21 16:41:56 +0200 | [diff] [blame] | 10079 | return t; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10080 | } |
| 10081 | |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 10082 | static int hlua_applet_tcp_init(struct appctx *ctx) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10083 | { |
Willy Tarreau | e23f33b | 2022-05-06 14:07:13 +0200 | [diff] [blame] | 10084 | struct hlua_tcp_ctx *tcp_ctx = applet_reserve_svcctx(ctx, sizeof(*tcp_ctx)); |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 10085 | struct stconn *sc = appctx_sc(ctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 10086 | struct stream *strm = __sc_strm(sc); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 10087 | struct hlua *hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10088 | struct task *task; |
| 10089 | char **arg; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 10090 | const char *error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10091 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 10092 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 10093 | if (!hlua) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 10094 | SEND_ERR(strm->be, "Lua applet tcp '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 10095 | ctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 10096 | return -1; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 10097 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10098 | HLUA_INIT(hlua); |
Willy Tarreau | e23f33b | 2022-05-06 14:07:13 +0200 | [diff] [blame] | 10099 | tcp_ctx->hlua = hlua; |
| 10100 | tcp_ctx->flags = 0; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10101 | |
| 10102 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | beeabf5 | 2021-10-01 18:23:30 +0200 | [diff] [blame] | 10103 | task = task_new_here(); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10104 | if (!task) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 10105 | SEND_ERR(strm->be, "Lua applet tcp '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 10106 | ctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 10107 | return -1; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10108 | } |
| 10109 | task->nice = 0; |
| 10110 | task->context = ctx; |
| 10111 | task->process = hlua_applet_wakeup; |
Willy Tarreau | e23f33b | 2022-05-06 14:07:13 +0200 | [diff] [blame] | 10112 | tcp_ctx->task = task; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10113 | |
| 10114 | /* In the execution wrappers linked with a stream, the |
| 10115 | * Lua context can be not initialized. This behavior |
| 10116 | * permits to save performances because a systematic |
| 10117 | * Lua initialization cause 5% performances loss. |
| 10118 | */ |
Aurelien DARRAGON | 6b0b9bd | 2023-03-21 14:02:16 +0100 | [diff] [blame] | 10119 | if (!hlua_ctx_init(hlua, fcn_ref_to_stack_id(ctx->rule->arg.hlua_rule->fcn), task)) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 10120 | SEND_ERR(strm->be, "Lua applet tcp '%s': can't initialize Lua context.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 10121 | ctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 10122 | return -1; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10123 | } |
| 10124 | |
| 10125 | /* Set timeout according with the applet configuration. */ |
Aurelien DARRAGON | da9503c | 2022-11-25 09:10:07 +0100 | [diff] [blame] | 10126 | hlua_timer_init(&hlua->timer, ctx->applet->timeout); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10127 | |
| 10128 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 10129 | if (!SET_SAFE_LJMP(hlua)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 10130 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 10131 | error = lua_tostring(hlua->T, -1); |
| 10132 | else |
| 10133 | error = "critical error"; |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 10134 | SEND_ERR(strm->be, "Lua applet tcp '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 10135 | ctx->rule->arg.hlua_rule->fcn->name, error); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 10136 | return -1; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10137 | } |
| 10138 | |
| 10139 | /* Check stack available size. */ |
| 10140 | if (!lua_checkstack(hlua->T, 1)) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 10141 | SEND_ERR(strm->be, "Lua applet tcp '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 10142 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 10143 | RESET_SAFE_LJMP(hlua); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 10144 | return -1; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10145 | } |
| 10146 | |
| 10147 | /* Restore the function in the stack. */ |
Aurelien DARRAGON | 4fdf8b5 | 2023-03-20 17:22:37 +0100 | [diff] [blame] | 10148 | hlua_pushref(hlua->T, ctx->rule->arg.hlua_rule->fcn->function_ref[hlua->state_id]); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10149 | |
| 10150 | /* Create and and push object stream in the stack. */ |
| 10151 | if (!hlua_applet_tcp_new(hlua->T, ctx)) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 10152 | SEND_ERR(strm->be, "Lua applet tcp '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 10153 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 10154 | RESET_SAFE_LJMP(hlua); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 10155 | return -1; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10156 | } |
| 10157 | hlua->nargs = 1; |
| 10158 | |
| 10159 | /* push keywords in the stack. */ |
| 10160 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 10161 | if (!lua_checkstack(hlua->T, 1)) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 10162 | SEND_ERR(strm->be, "Lua applet tcp '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 10163 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 10164 | RESET_SAFE_LJMP(hlua); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 10165 | return -1; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10166 | } |
| 10167 | lua_pushstring(hlua->T, *arg); |
| 10168 | hlua->nargs++; |
| 10169 | } |
| 10170 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 10171 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10172 | |
| 10173 | /* Wakeup the applet ASAP. */ |
Willy Tarreau | 90e8b45 | 2022-05-25 18:21:43 +0200 | [diff] [blame] | 10174 | applet_need_more_data(ctx); |
Willy Tarreau | 4164eb9 | 2022-05-25 15:42:03 +0200 | [diff] [blame] | 10175 | applet_have_more_data(ctx); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10176 | |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 10177 | return 0; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10178 | } |
| 10179 | |
Willy Tarreau | 60409db | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 10180 | void hlua_applet_tcp_fct(struct appctx *ctx) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10181 | { |
Willy Tarreau | e23f33b | 2022-05-06 14:07:13 +0200 | [diff] [blame] | 10182 | struct hlua_tcp_ctx *tcp_ctx = ctx->svcctx; |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 10183 | struct stconn *sc = appctx_sc(ctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 10184 | struct stream *strm = __sc_strm(sc); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10185 | struct act_rule *rule = ctx->rule; |
| 10186 | struct proxy *px = strm->be; |
Willy Tarreau | e23f33b | 2022-05-06 14:07:13 +0200 | [diff] [blame] | 10187 | struct hlua *hlua = tcp_ctx->hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10188 | |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 10189 | if (unlikely(se_fl_test(ctx->sedesc, (SE_FL_EOS|SE_FL_ERROR|SE_FL_SHR|SE_FL_SHW)))) |
| 10190 | goto out; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10191 | |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 10192 | /* The applet execution is already done. */ |
| 10193 | if (tcp_ctx->flags & APPLET_DONE) |
| 10194 | goto out; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10195 | |
| 10196 | /* Execute the function. */ |
| 10197 | switch (hlua_ctx_resume(hlua, 1)) { |
| 10198 | /* finished. */ |
| 10199 | case HLUA_E_OK: |
Willy Tarreau | e23f33b | 2022-05-06 14:07:13 +0200 | [diff] [blame] | 10200 | tcp_ctx->flags |= APPLET_DONE; |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 10201 | se_fl_set(ctx->sedesc, SE_FL_EOI|SE_FL_EOS); |
| 10202 | break; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10203 | |
| 10204 | /* yield. */ |
| 10205 | case HLUA_E_AGAIN: |
Thierry Fournier | 0164f20 | 2016-02-20 17:47:43 +0100 | [diff] [blame] | 10206 | if (hlua->wake_time != TICK_ETERNITY) |
Willy Tarreau | e23f33b | 2022-05-06 14:07:13 +0200 | [diff] [blame] | 10207 | task_schedule(tcp_ctx->task, hlua->wake_time); |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 10208 | break; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10209 | |
| 10210 | /* finished with error. */ |
| 10211 | case HLUA_E_ERRMSG: |
| 10212 | /* Display log. */ |
| 10213 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 10214 | rule->arg.hlua_rule->fcn->name, lua_tostring(hlua->T, -1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10215 | lua_pop(hlua->T, 1); |
| 10216 | goto error; |
| 10217 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 10218 | case HLUA_E_ETMOUT: |
| 10219 | SEND_ERR(px, "Lua applet tcp '%s': execution timeout.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 10220 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 10221 | goto error; |
| 10222 | |
| 10223 | case HLUA_E_NOMEM: |
| 10224 | SEND_ERR(px, "Lua applet tcp '%s': out of memory error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 10225 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 10226 | goto error; |
| 10227 | |
| 10228 | case HLUA_E_YIELD: /* unexpected */ |
| 10229 | SEND_ERR(px, "Lua applet tcp '%s': yield not allowed.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 10230 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 10231 | goto error; |
| 10232 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10233 | case HLUA_E_ERR: |
| 10234 | /* Display log. */ |
| 10235 | SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 10236 | rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10237 | goto error; |
| 10238 | |
| 10239 | default: |
| 10240 | goto error; |
| 10241 | } |
| 10242 | |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 10243 | out: |
| 10244 | /* eat the whole request */ |
| 10245 | co_skip(sc_oc(sc), co_data(sc_oc(sc))); |
| 10246 | return; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10247 | |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 10248 | error: |
| 10249 | se_fl_set(ctx->sedesc, SE_FL_ERROR); |
Willy Tarreau | e23f33b | 2022-05-06 14:07:13 +0200 | [diff] [blame] | 10250 | tcp_ctx->flags |= APPLET_DONE; |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 10251 | goto out; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10252 | } |
| 10253 | |
| 10254 | static void hlua_applet_tcp_release(struct appctx *ctx) |
| 10255 | { |
Willy Tarreau | e23f33b | 2022-05-06 14:07:13 +0200 | [diff] [blame] | 10256 | struct hlua_tcp_ctx *tcp_ctx = ctx->svcctx; |
| 10257 | |
| 10258 | task_destroy(tcp_ctx->task); |
| 10259 | tcp_ctx->task = NULL; |
| 10260 | hlua_ctx_destroy(tcp_ctx->hlua); |
| 10261 | tcp_ctx->hlua = NULL; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10262 | } |
| 10263 | |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 10264 | /* The function returns 0 if the initialisation is complete or -1 if |
| 10265 | * an errors occurs. It also reserves the appctx for an hlua_http_ctx. |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 10266 | */ |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 10267 | static int hlua_applet_http_init(struct appctx *ctx) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 10268 | { |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 10269 | struct hlua_http_ctx *http_ctx = applet_reserve_svcctx(ctx, sizeof(*http_ctx)); |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 10270 | struct stconn *sc = appctx_sc(ctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 10271 | struct stream *strm = __sc_strm(sc); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 10272 | struct http_txn *txn; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 10273 | struct hlua *hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 10274 | char **arg; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 10275 | struct task *task; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 10276 | const char *error; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 10277 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 10278 | txn = strm->txn; |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 10279 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 10280 | if (!hlua) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 10281 | SEND_ERR(strm->be, "Lua applet http '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 10282 | ctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 10283 | return -1; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 10284 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 10285 | HLUA_INIT(hlua); |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 10286 | http_ctx->hlua = hlua; |
| 10287 | http_ctx->left_bytes = -1; |
| 10288 | http_ctx->flags = 0; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 10289 | |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 10290 | if (txn->req.flags & HTTP_MSGF_VER_11) |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 10291 | http_ctx->flags |= APPLET_HTTP11; |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 10292 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 10293 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | beeabf5 | 2021-10-01 18:23:30 +0200 | [diff] [blame] | 10294 | task = task_new_here(); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 10295 | if (!task) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 10296 | SEND_ERR(strm->be, "Lua applet http '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 10297 | ctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 10298 | return -1; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 10299 | } |
| 10300 | task->nice = 0; |
| 10301 | task->context = ctx; |
| 10302 | task->process = hlua_applet_wakeup; |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 10303 | http_ctx->task = task; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 10304 | |
| 10305 | /* In the execution wrappers linked with a stream, the |
| 10306 | * Lua context can be not initialized. This behavior |
| 10307 | * permits to save performances because a systematic |
| 10308 | * Lua initialization cause 5% performances loss. |
| 10309 | */ |
Aurelien DARRAGON | 6b0b9bd | 2023-03-21 14:02:16 +0100 | [diff] [blame] | 10310 | if (!hlua_ctx_init(hlua, fcn_ref_to_stack_id(ctx->rule->arg.hlua_rule->fcn), task)) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 10311 | SEND_ERR(strm->be, "Lua applet http '%s': can't initialize Lua context.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 10312 | ctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 10313 | return -1; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 10314 | } |
| 10315 | |
| 10316 | /* Set timeout according with the applet configuration. */ |
Aurelien DARRAGON | da9503c | 2022-11-25 09:10:07 +0100 | [diff] [blame] | 10317 | hlua_timer_init(&hlua->timer, ctx->applet->timeout); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 10318 | |
| 10319 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 10320 | if (!SET_SAFE_LJMP(hlua)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 10321 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 10322 | error = lua_tostring(hlua->T, -1); |
| 10323 | else |
| 10324 | error = "critical error"; |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 10325 | SEND_ERR(strm->be, "Lua applet http '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 10326 | ctx->rule->arg.hlua_rule->fcn->name, error); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 10327 | return -1; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 10328 | } |
| 10329 | |
| 10330 | /* Check stack available size. */ |
| 10331 | if (!lua_checkstack(hlua->T, 1)) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 10332 | SEND_ERR(strm->be, "Lua applet http '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 10333 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 10334 | RESET_SAFE_LJMP(hlua); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 10335 | return -1; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 10336 | } |
| 10337 | |
| 10338 | /* Restore the function in the stack. */ |
Aurelien DARRAGON | 4fdf8b5 | 2023-03-20 17:22:37 +0100 | [diff] [blame] | 10339 | hlua_pushref(hlua->T, ctx->rule->arg.hlua_rule->fcn->function_ref[hlua->state_id]); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 10340 | |
| 10341 | /* Create and and push object stream in the stack. */ |
| 10342 | if (!hlua_applet_http_new(hlua->T, ctx)) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 10343 | SEND_ERR(strm->be, "Lua applet http '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 10344 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 10345 | RESET_SAFE_LJMP(hlua); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 10346 | return -1; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 10347 | } |
| 10348 | hlua->nargs = 1; |
| 10349 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 10350 | /* push keywords in the stack. */ |
| 10351 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 10352 | if (!lua_checkstack(hlua->T, 1)) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 10353 | SEND_ERR(strm->be, "Lua applet http '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 10354 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 10355 | RESET_SAFE_LJMP(hlua); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 10356 | return -1; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 10357 | } |
| 10358 | lua_pushstring(hlua->T, *arg); |
| 10359 | hlua->nargs++; |
| 10360 | } |
| 10361 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 10362 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 10363 | |
| 10364 | /* Wakeup the applet when data is ready for read. */ |
Willy Tarreau | 90e8b45 | 2022-05-25 18:21:43 +0200 | [diff] [blame] | 10365 | applet_need_more_data(ctx); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 10366 | |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 10367 | return 0; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 10368 | } |
| 10369 | |
Willy Tarreau | 60409db | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 10370 | void hlua_applet_http_fct(struct appctx *ctx) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 10371 | { |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 10372 | struct hlua_http_ctx *http_ctx = ctx->svcctx; |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 10373 | struct stconn *sc = appctx_sc(ctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 10374 | struct stream *strm = __sc_strm(sc); |
| 10375 | struct channel *req = sc_oc(sc); |
| 10376 | struct channel *res = sc_ic(sc); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 10377 | struct act_rule *rule = ctx->rule; |
| 10378 | struct proxy *px = strm->be; |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 10379 | struct hlua *hlua = http_ctx->hlua; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 10380 | struct htx *req_htx, *res_htx; |
| 10381 | |
| 10382 | res_htx = htx_from_buf(&res->buf); |
| 10383 | |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 10384 | if (unlikely(se_fl_test(ctx->sedesc, (SE_FL_EOS|SE_FL_ERROR|SE_FL_SHR|SE_FL_SHW)))) |
| 10385 | goto out; |
| 10386 | |
| 10387 | /* The applet execution is already done. */ |
| 10388 | if (http_ctx->flags & APPLET_DONE) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 10389 | goto out; |
| 10390 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 10391 | /* Check if the input buffer is available. */ |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 10392 | if (!b_size(&res->buf)) { |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 10393 | sc_need_room(sc); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 10394 | goto out; |
| 10395 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 10396 | |
| 10397 | /* Set the currently running flag. */ |
| 10398 | if (!HLUA_IS_RUNNING(hlua) && |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 10399 | !(http_ctx->flags & APPLET_DONE)) { |
Christopher Faulet | bd878d2 | 2021-04-28 10:50:21 +0200 | [diff] [blame] | 10400 | if (!co_data(req)) { |
Willy Tarreau | 90e8b45 | 2022-05-25 18:21:43 +0200 | [diff] [blame] | 10401 | applet_need_more_data(ctx); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 10402 | goto out; |
| 10403 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 10404 | } |
| 10405 | |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 10406 | /* Execute the function. */ |
| 10407 | switch (hlua_ctx_resume(hlua, 1)) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 10408 | /* finished. */ |
| 10409 | case HLUA_E_OK: |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 10410 | http_ctx->flags |= APPLET_DONE; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 10411 | break; |
| 10412 | |
| 10413 | /* yield. */ |
| 10414 | case HLUA_E_AGAIN: |
| 10415 | if (hlua->wake_time != TICK_ETERNITY) |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 10416 | task_schedule(http_ctx->task, hlua->wake_time); |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 10417 | goto out; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 10418 | |
| 10419 | /* finished with error. */ |
| 10420 | case HLUA_E_ERRMSG: |
| 10421 | /* Display log. */ |
| 10422 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 10423 | rule->arg.hlua_rule->fcn->name, lua_tostring(hlua->T, -1)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 10424 | lua_pop(hlua->T, 1); |
| 10425 | goto error; |
| 10426 | |
| 10427 | case HLUA_E_ETMOUT: |
| 10428 | SEND_ERR(px, "Lua applet http '%s': execution timeout.\n", |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 10429 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 10430 | goto error; |
| 10431 | |
| 10432 | case HLUA_E_NOMEM: |
| 10433 | SEND_ERR(px, "Lua applet http '%s': out of memory error.\n", |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 10434 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 10435 | goto error; |
| 10436 | |
| 10437 | case HLUA_E_YIELD: /* unexpected */ |
| 10438 | SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n", |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 10439 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 10440 | goto error; |
| 10441 | |
| 10442 | case HLUA_E_ERR: |
| 10443 | /* Display log. */ |
| 10444 | SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n", |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 10445 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 10446 | goto error; |
| 10447 | |
| 10448 | default: |
| 10449 | goto error; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 10450 | } |
| 10451 | |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 10452 | if (http_ctx->flags & APPLET_DONE) { |
| 10453 | if (http_ctx->flags & APPLET_RSP_SENT) |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 10454 | goto out; |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 10455 | |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 10456 | if (!(http_ctx->flags & APPLET_HDR_SENT)) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 10457 | goto error; |
| 10458 | |
Christopher Faulet | f89af9c | 2022-04-07 10:07:18 +0200 | [diff] [blame] | 10459 | /* no more data are expected. If the response buffer is empty |
| 10460 | * for a chunked message, be sure to add something (EOT block in |
| 10461 | * this case) to have something to send. It is important to be |
| 10462 | * sure the EOM flags will be handled by the endpoint. |
| 10463 | */ |
| 10464 | if (htx_is_empty(res_htx) && (strm->txn->rsp.flags & (HTTP_MSGF_XFER_LEN|HTTP_MSGF_CNT_LEN)) == HTTP_MSGF_XFER_LEN) { |
| 10465 | if (!htx_add_endof(res_htx, HTX_BLK_EOT)) { |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 10466 | sc_need_room(sc); |
Christopher Faulet | f89af9c | 2022-04-07 10:07:18 +0200 | [diff] [blame] | 10467 | goto out; |
| 10468 | } |
| 10469 | channel_add_input(res, 1); |
| 10470 | } |
| 10471 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 10472 | res_htx->flags |= HTX_FL_EOM; |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 10473 | se_fl_set(ctx->sedesc, SE_FL_EOI|SE_FL_EOS); |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 10474 | strm->txn->status = http_ctx->status; |
| 10475 | http_ctx->flags |= APPLET_RSP_SENT; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 10476 | } |
| 10477 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 10478 | out: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 10479 | htx_to_buf(res_htx, &res->buf); |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 10480 | /* eat the whole request */ |
| 10481 | if (co_data(req)) { |
| 10482 | req_htx = htx_from_buf(&req->buf); |
| 10483 | co_htx_skip(req, req_htx, co_data(req)); |
| 10484 | htx_to_buf(req_htx, &req->buf); |
| 10485 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 10486 | return; |
| 10487 | |
| 10488 | error: |
| 10489 | |
| 10490 | /* If we are in HTTP mode, and we are not send any |
| 10491 | * data, return a 500 server error in best effort: |
| 10492 | * if there is no room available in the buffer, |
| 10493 | * just close the connection. |
| 10494 | */ |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 10495 | if (!(http_ctx->flags & APPLET_HDR_SENT)) { |
Christopher Faulet | f734638 | 2019-07-17 22:02:08 +0200 | [diff] [blame] | 10496 | struct buffer *err = &http_err_chunks[HTTP_ERR_500]; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 10497 | |
| 10498 | channel_erase(res); |
| 10499 | res->buf.data = b_data(err); |
| 10500 | memcpy(res->buf.area, b_head(err), b_data(err)); |
| 10501 | res_htx = htx_from_buf(&res->buf); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 10502 | channel_add_input(res, res_htx->data); |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 10503 | se_fl_set(ctx->sedesc, SE_FL_EOI|SE_FL_EOS); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 10504 | } |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 10505 | else |
| 10506 | se_fl_set(ctx->sedesc, SE_FL_ERROR); |
| 10507 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 10508 | if (!(strm->flags & SF_ERR_MASK)) |
| 10509 | strm->flags |= SF_ERR_RESOURCE; |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 10510 | http_ctx->flags |= APPLET_DONE; |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 10511 | goto out; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 10512 | } |
| 10513 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 10514 | static void hlua_applet_http_release(struct appctx *ctx) |
| 10515 | { |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 10516 | struct hlua_http_ctx *http_ctx = ctx->svcctx; |
| 10517 | |
| 10518 | task_destroy(http_ctx->task); |
| 10519 | http_ctx->task = NULL; |
| 10520 | hlua_ctx_destroy(http_ctx->hlua); |
| 10521 | http_ctx->hlua = NULL; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 10522 | } |
| 10523 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 10524 | /* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 10525 | * success case, else return ACT_RET_PRS_ERR. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 10526 | * |
| 10527 | * This function can fail with an abort() due to an Lua critical error. |
| 10528 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 10529 | * tolerated. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 10530 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 10531 | static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px, |
| 10532 | struct act_rule *rule, char **err) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 10533 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 10534 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 10535 | int i; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 10536 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 10537 | /* Memory for the rule. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 10538 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 10539 | if (!rule->arg.hlua_rule) { |
| 10540 | memprintf(err, "out of memory error"); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 10541 | goto error; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 10542 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 10543 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 10544 | /* Memory for arguments. */ |
Tim Duesterhus | e52b6e5 | 2020-09-12 20:26:43 +0200 | [diff] [blame] | 10545 | rule->arg.hlua_rule->args = calloc(fcn->nargs + 1, |
| 10546 | sizeof(*rule->arg.hlua_rule->args)); |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 10547 | if (!rule->arg.hlua_rule->args) { |
| 10548 | memprintf(err, "out of memory error"); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 10549 | goto error; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 10550 | } |
| 10551 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 10552 | /* Reference the Lua function and store the reference. */ |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 10553 | rule->arg.hlua_rule->fcn = fcn; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 10554 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 10555 | /* Expect some arguments */ |
| 10556 | for (i = 0; i < fcn->nargs; i++) { |
Thierry FOURNIER | 1725c2e | 2019-01-06 19:38:49 +0100 | [diff] [blame] | 10557 | if (*args[*cur_arg] == '\0') { |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 10558 | memprintf(err, "expect %d arguments", fcn->nargs); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 10559 | goto error; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 10560 | } |
Thierry FOURNIER | 1725c2e | 2019-01-06 19:38:49 +0100 | [diff] [blame] | 10561 | rule->arg.hlua_rule->args[i] = strdup(args[*cur_arg]); |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 10562 | if (!rule->arg.hlua_rule->args[i]) { |
| 10563 | memprintf(err, "out of memory error"); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 10564 | goto error; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 10565 | } |
| 10566 | (*cur_arg)++; |
| 10567 | } |
| 10568 | rule->arg.hlua_rule->args[i] = NULL; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 10569 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 10570 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 10571 | rule->action_ptr = hlua_action; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 10572 | return ACT_RET_PRS_OK; |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 10573 | |
| 10574 | error: |
| 10575 | if (rule->arg.hlua_rule) { |
| 10576 | if (rule->arg.hlua_rule->args) { |
| 10577 | for (i = 0; i < fcn->nargs; i++) |
| 10578 | ha_free(&rule->arg.hlua_rule->args[i]); |
| 10579 | ha_free(&rule->arg.hlua_rule->args); |
| 10580 | } |
| 10581 | ha_free(&rule->arg.hlua_rule); |
| 10582 | } |
| 10583 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 10584 | } |
| 10585 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 10586 | static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px, |
| 10587 | struct act_rule *rule, char **err) |
| 10588 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 10589 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 10590 | |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 10591 | /* HTTP applets are forbidden in tcp-request rules. |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 10592 | * HTTP applet request requires everything initialized by |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 10593 | * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER). |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 10594 | * The applet will be immediately initialized, but its before |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 10595 | * the call of this analyzer. |
| 10596 | */ |
| 10597 | if (rule->from != ACT_F_HTTP_REQ) { |
| 10598 | memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets"); |
| 10599 | return ACT_RET_PRS_ERR; |
| 10600 | } |
| 10601 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 10602 | /* Memory for the rule. */ |
| 10603 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 10604 | if (!rule->arg.hlua_rule) { |
| 10605 | memprintf(err, "out of memory error"); |
| 10606 | return ACT_RET_PRS_ERR; |
| 10607 | } |
| 10608 | |
| 10609 | /* Reference the Lua function and store the reference. */ |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 10610 | rule->arg.hlua_rule->fcn = fcn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 10611 | |
| 10612 | /* TODO: later accept arguments. */ |
| 10613 | rule->arg.hlua_rule->args = NULL; |
| 10614 | |
| 10615 | /* Add applet pointer in the rule. */ |
| 10616 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 10617 | rule->applet.name = fcn->name; |
| 10618 | rule->applet.init = hlua_applet_http_init; |
| 10619 | rule->applet.fct = hlua_applet_http_fct; |
| 10620 | rule->applet.release = hlua_applet_http_release; |
| 10621 | rule->applet.timeout = hlua_timeout_applet; |
| 10622 | |
| 10623 | return ACT_RET_PRS_OK; |
| 10624 | } |
| 10625 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 10626 | /* This function is an LUA binding used for registering |
| 10627 | * "sample-conv" functions. It expects a converter name used |
| 10628 | * in the haproxy configuration file, and an LUA function. |
| 10629 | */ |
| 10630 | __LJMP static int hlua_register_action(lua_State *L) |
| 10631 | { |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 10632 | struct action_kw_list *akl = NULL; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 10633 | const char *name; |
| 10634 | int ref; |
| 10635 | int len; |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 10636 | struct hlua_function *fcn = NULL; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 10637 | int nargs; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 10638 | struct buffer *trash; |
| 10639 | struct action_kw *akw; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 10640 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 10641 | /* Initialise the number of expected arguments at 0. */ |
| 10642 | nargs = 0; |
| 10643 | |
| 10644 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 10645 | 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] | 10646 | |
Aurelien DARRAGON | 87f5297 | 2023-02-24 09:43:54 +0100 | [diff] [blame] | 10647 | if (hlua_gethlua(L)) { |
| 10648 | /* runtime processing */ |
| 10649 | WILL_LJMP(luaL_error(L, "register_action: not available outside of body context")); |
| 10650 | } |
| 10651 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 10652 | /* First argument : converter name. */ |
| 10653 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 10654 | |
| 10655 | /* Second argument : environment. */ |
| 10656 | if (lua_type(L, 2) != LUA_TTABLE) |
| 10657 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 10658 | |
| 10659 | /* Third argument : lua function. */ |
| 10660 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 10661 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 10662 | /* 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] | 10663 | if (lua_gettop(L) >= 4) |
| 10664 | nargs = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 10665 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 10666 | /* browse the second argument as an array. */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 10667 | lua_pushnil(L); |
| 10668 | while (lua_next(L, 2) != 0) { |
Aurelien DARRAGON | 55afbed | 2023-03-02 18:42:06 +0100 | [diff] [blame] | 10669 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 10670 | hlua_unref(L, ref); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 10671 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
Aurelien DARRAGON | 55afbed | 2023-03-02 18:42:06 +0100 | [diff] [blame] | 10672 | } |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 10673 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 10674 | /* Check if action exists */ |
| 10675 | trash = get_trash_chunk(); |
| 10676 | chunk_printf(trash, "lua.%s", name); |
| 10677 | if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) { |
| 10678 | akw = tcp_req_cont_action(trash->area); |
| 10679 | } else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) { |
| 10680 | akw = tcp_res_cont_action(trash->area); |
| 10681 | } else if (strcmp(lua_tostring(L, -1), "http-req") == 0) { |
| 10682 | akw = action_http_req_custom(trash->area); |
| 10683 | } else if (strcmp(lua_tostring(L, -1), "http-res") == 0) { |
| 10684 | akw = action_http_res_custom(trash->area); |
| 10685 | } else { |
| 10686 | akw = NULL; |
| 10687 | } |
| 10688 | if (akw != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 10689 | fcn = akw->private; |
| 10690 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 10691 | ha_warning("Trying to register action 'lua.%s' more than once. " |
| 10692 | "This will become a hard error in version 2.5.\n", name); |
Aurelien DARRAGON | 55afbed | 2023-03-02 18:42:06 +0100 | [diff] [blame] | 10693 | hlua_unref(L, fcn->function_ref[hlua_state_id]); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 10694 | } |
| 10695 | fcn->function_ref[hlua_state_id] = ref; |
| 10696 | |
| 10697 | /* pop the environment string. */ |
| 10698 | lua_pop(L, 1); |
| 10699 | continue; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 10700 | } |
| 10701 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 10702 | /* Check required environment. Only accepted "http" or "tcp". */ |
| 10703 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 10704 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 10705 | if (!akl) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 10706 | goto alloc_error;; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 10707 | fcn = new_hlua_function(); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 10708 | if (!fcn) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 10709 | goto alloc_error; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 10710 | |
| 10711 | /* Fill fcn. */ |
| 10712 | fcn->name = strdup(name); |
| 10713 | if (!fcn->name) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 10714 | goto alloc_error; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 10715 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 10716 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 10717 | /* Set the expected number of arguments. */ |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 10718 | fcn->nargs = nargs; |
| 10719 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 10720 | /* List head */ |
| 10721 | akl->list.n = akl->list.p = NULL; |
| 10722 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 10723 | /* action keyword. */ |
| 10724 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 10725 | akl->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 10726 | if (!akl->kw[0].kw) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 10727 | goto alloc_error; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 10728 | |
| 10729 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 10730 | |
Amaury Denoyelle | e4a617c | 2021-05-06 15:33:09 +0200 | [diff] [blame] | 10731 | akl->kw[0].flags = 0; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 10732 | akl->kw[0].private = fcn; |
| 10733 | akl->kw[0].parse = action_register_lua; |
| 10734 | |
| 10735 | /* select the action registering point. */ |
| 10736 | if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) |
| 10737 | tcp_req_cont_keywords_register(akl); |
| 10738 | else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) |
| 10739 | tcp_res_cont_keywords_register(akl); |
| 10740 | else if (strcmp(lua_tostring(L, -1), "http-req") == 0) |
| 10741 | http_req_keywords_register(akl); |
| 10742 | else if (strcmp(lua_tostring(L, -1), "http-res") == 0) |
| 10743 | http_res_keywords_register(akl); |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 10744 | else { |
| 10745 | release_hlua_function(fcn); |
Aurelien DARRAGON | 55afbed | 2023-03-02 18:42:06 +0100 | [diff] [blame] | 10746 | hlua_unref(L, ref); |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 10747 | if (akl) |
| 10748 | ha_free((char **)&(akl->kw[0].kw)); |
| 10749 | ha_free(&akl); |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 10750 | WILL_LJMP(luaL_error(L, "Lua action environment '%s' is unknown. " |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 10751 | "'tcp-req', 'tcp-res', 'http-req' or 'http-res' " |
| 10752 | "are expected.", lua_tostring(L, -1))); |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 10753 | } |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 10754 | |
| 10755 | /* pop the environment string. */ |
| 10756 | lua_pop(L, 1); |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 10757 | |
| 10758 | /* reset for next loop */ |
| 10759 | akl = NULL; |
| 10760 | fcn = NULL; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 10761 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10762 | return ACT_RET_PRS_OK; |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 10763 | |
| 10764 | alloc_error: |
| 10765 | release_hlua_function(fcn); |
Aurelien DARRAGON | 55afbed | 2023-03-02 18:42:06 +0100 | [diff] [blame] | 10766 | hlua_unref(L, ref); |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 10767 | ha_free(&akl); |
| 10768 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 10769 | return 0; /* Never reached */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10770 | } |
| 10771 | |
| 10772 | static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px, |
| 10773 | struct act_rule *rule, char **err) |
| 10774 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 10775 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10776 | |
Christopher Faulet | 280f85b | 2019-07-15 15:02:04 +0200 | [diff] [blame] | 10777 | if (px->mode == PR_MODE_HTTP) { |
| 10778 | memprintf(err, "Lua TCP services cannot be used on HTTP proxies"); |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 10779 | return ACT_RET_PRS_ERR; |
| 10780 | } |
| 10781 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10782 | /* Memory for the rule. */ |
| 10783 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 10784 | if (!rule->arg.hlua_rule) { |
| 10785 | memprintf(err, "out of memory error"); |
| 10786 | return ACT_RET_PRS_ERR; |
| 10787 | } |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 10788 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10789 | /* Reference the Lua function and store the reference. */ |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 10790 | rule->arg.hlua_rule->fcn = fcn; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10791 | |
| 10792 | /* TODO: later accept arguments. */ |
| 10793 | rule->arg.hlua_rule->args = NULL; |
| 10794 | |
| 10795 | /* Add applet pointer in the rule. */ |
| 10796 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 10797 | rule->applet.name = fcn->name; |
| 10798 | rule->applet.init = hlua_applet_tcp_init; |
| 10799 | rule->applet.fct = hlua_applet_tcp_fct; |
| 10800 | rule->applet.release = hlua_applet_tcp_release; |
| 10801 | rule->applet.timeout = hlua_timeout_applet; |
| 10802 | |
| 10803 | return 0; |
| 10804 | } |
| 10805 | |
| 10806 | /* This function is an LUA binding used for registering |
| 10807 | * "sample-conv" functions. It expects a converter name used |
| 10808 | * in the haproxy configuration file, and an LUA function. |
| 10809 | */ |
| 10810 | __LJMP static int hlua_register_service(lua_State *L) |
| 10811 | { |
| 10812 | struct action_kw_list *akl; |
| 10813 | const char *name; |
| 10814 | const char *env; |
| 10815 | int ref; |
| 10816 | int len; |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 10817 | struct hlua_function *fcn = NULL; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 10818 | struct buffer *trash; |
| 10819 | struct action_kw *akw; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10820 | |
| 10821 | MAY_LJMP(check_args(L, 3, "register_service")); |
| 10822 | |
Aurelien DARRAGON | 87f5297 | 2023-02-24 09:43:54 +0100 | [diff] [blame] | 10823 | if (hlua_gethlua(L)) { |
| 10824 | /* runtime processing */ |
| 10825 | WILL_LJMP(luaL_error(L, "register_service: not available outside of body context")); |
| 10826 | } |
| 10827 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10828 | /* First argument : converter name. */ |
| 10829 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 10830 | |
| 10831 | /* Second argument : environment. */ |
| 10832 | env = MAY_LJMP(luaL_checkstring(L, 2)); |
| 10833 | |
| 10834 | /* Third argument : lua function. */ |
| 10835 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 10836 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 10837 | /* Check for service already registered */ |
| 10838 | trash = get_trash_chunk(); |
| 10839 | chunk_printf(trash, "lua.%s", name); |
| 10840 | akw = service_find(trash->area); |
| 10841 | if (akw != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 10842 | fcn = akw->private; |
| 10843 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 10844 | ha_warning("Trying to register service 'lua.%s' more than once. " |
| 10845 | "This will become a hard error in version 2.5.\n", name); |
Aurelien DARRAGON | 55afbed | 2023-03-02 18:42:06 +0100 | [diff] [blame] | 10846 | hlua_unref(L, fcn->function_ref[hlua_state_id]); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 10847 | } |
| 10848 | fcn->function_ref[hlua_state_id] = ref; |
| 10849 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 10850 | } |
| 10851 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10852 | /* Allocate and fill the sample fetch keyword struct. */ |
| 10853 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
| 10854 | if (!akl) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 10855 | goto alloc_error; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 10856 | fcn = new_hlua_function(); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10857 | if (!fcn) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 10858 | goto alloc_error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10859 | |
| 10860 | /* Fill fcn. */ |
| 10861 | len = strlen("<lua.>") + strlen(name) + 1; |
| 10862 | fcn->name = calloc(1, len); |
| 10863 | if (!fcn->name) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 10864 | goto alloc_error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10865 | snprintf((char *)fcn->name, len, "<lua.%s>", name); |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 10866 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10867 | |
| 10868 | /* List head */ |
| 10869 | akl->list.n = akl->list.p = NULL; |
| 10870 | |
| 10871 | /* converter keyword. */ |
| 10872 | len = strlen("lua.") + strlen(name) + 1; |
| 10873 | akl->kw[0].kw = calloc(1, len); |
| 10874 | if (!akl->kw[0].kw) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 10875 | goto alloc_error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10876 | |
| 10877 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 10878 | |
Thierry FOURNIER / OZON.IO | 02564fd | 2016-11-12 11:07:05 +0100 | [diff] [blame] | 10879 | /* Check required environment. Only accepted "http" or "tcp". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10880 | if (strcmp(env, "tcp") == 0) |
| 10881 | akl->kw[0].parse = action_register_service_tcp; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 10882 | else if (strcmp(env, "http") == 0) |
| 10883 | akl->kw[0].parse = action_register_service_http; |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 10884 | else { |
| 10885 | release_hlua_function(fcn); |
Aurelien DARRAGON | 55afbed | 2023-03-02 18:42:06 +0100 | [diff] [blame] | 10886 | hlua_unref(L, ref); |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 10887 | if (akl) |
| 10888 | ha_free((char **)&(akl->kw[0].kw)); |
| 10889 | ha_free(&akl); |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 10890 | WILL_LJMP(luaL_error(L, "Lua service environment '%s' is unknown. " |
Eric Salama | fe7456f | 2017-12-21 14:30:07 +0100 | [diff] [blame] | 10891 | "'tcp' or 'http' are expected.", env)); |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 10892 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10893 | |
Amaury Denoyelle | e4a617c | 2021-05-06 15:33:09 +0200 | [diff] [blame] | 10894 | akl->kw[0].flags = 0; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10895 | akl->kw[0].private = fcn; |
| 10896 | |
| 10897 | /* End of array. */ |
| 10898 | memset(&akl->kw[1], 0, sizeof(*akl->kw)); |
| 10899 | |
| 10900 | /* Register this new converter */ |
| 10901 | service_keywords_register(akl); |
| 10902 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 10903 | return 0; |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 10904 | |
| 10905 | alloc_error: |
| 10906 | release_hlua_function(fcn); |
Aurelien DARRAGON | 55afbed | 2023-03-02 18:42:06 +0100 | [diff] [blame] | 10907 | hlua_unref(L, ref); |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 10908 | ha_free(&akl); |
| 10909 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 10910 | return 0; /* Never reached */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 10911 | } |
| 10912 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10913 | /* This function initialises Lua cli handler. It copies the |
| 10914 | * arguments in the Lua stack and create channel IO objects. |
| 10915 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 10916 | 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] | 10917 | { |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 10918 | struct hlua_cli_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx)); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10919 | struct hlua *hlua; |
| 10920 | struct hlua_function *fcn; |
| 10921 | int i; |
| 10922 | const char *error; |
| 10923 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10924 | fcn = private; |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 10925 | ctx->fcn = private; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 10926 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 10927 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 10928 | if (!hlua) { |
| 10929 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 10930 | return 1; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 10931 | } |
| 10932 | HLUA_INIT(hlua); |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 10933 | ctx->hlua = hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10934 | |
| 10935 | /* Create task used by signal to wakeup applets. |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 10936 | * 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] | 10937 | * applet_http. It is absolutely compatible. |
| 10938 | */ |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 10939 | ctx->task = task_new_here(); |
| 10940 | if (!ctx->task) { |
Thierry FOURNIER | ffbf569 | 2016-12-16 11:14:06 +0100 | [diff] [blame] | 10941 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 10942 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10943 | } |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 10944 | ctx->task->nice = 0; |
| 10945 | ctx->task->context = appctx; |
| 10946 | ctx->task->process = hlua_applet_wakeup; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10947 | |
| 10948 | /* Initialises the Lua context */ |
Aurelien DARRAGON | 6b0b9bd | 2023-03-21 14:02:16 +0100 | [diff] [blame] | 10949 | if (!hlua_ctx_init(hlua, fcn_ref_to_stack_id(fcn), ctx->task)) { |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10950 | 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] | 10951 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10952 | } |
| 10953 | |
| 10954 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 10955 | if (!SET_SAFE_LJMP(hlua)) { |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10956 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 10957 | error = lua_tostring(hlua->T, -1); |
| 10958 | else |
| 10959 | error = "critical error"; |
| 10960 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error); |
| 10961 | goto error; |
| 10962 | } |
| 10963 | |
| 10964 | /* Check stack available size. */ |
| 10965 | if (!lua_checkstack(hlua->T, 2)) { |
| 10966 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 10967 | goto error; |
| 10968 | } |
| 10969 | |
| 10970 | /* Restore the function in the stack. */ |
Aurelien DARRAGON | 4fdf8b5 | 2023-03-20 17:22:37 +0100 | [diff] [blame] | 10971 | hlua_pushref(hlua->T, fcn->function_ref[hlua->state_id]); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10972 | |
| 10973 | /* Once the arguments parsed, the CLI is like an AppletTCP, |
| 10974 | * so push AppletTCP in the stack. |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10975 | */ |
| 10976 | if (!hlua_applet_tcp_new(hlua->T, appctx)) { |
| 10977 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 10978 | goto error; |
| 10979 | } |
| 10980 | hlua->nargs = 1; |
| 10981 | |
| 10982 | /* push keywords in the stack. */ |
| 10983 | for (i = 0; *args[i]; i++) { |
| 10984 | /* Check stack available size. */ |
| 10985 | if (!lua_checkstack(hlua->T, 1)) { |
| 10986 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 10987 | goto error; |
| 10988 | } |
| 10989 | lua_pushstring(hlua->T, args[i]); |
| 10990 | hlua->nargs++; |
| 10991 | } |
| 10992 | |
| 10993 | /* We must initialize the execution timeouts. */ |
Aurelien DARRAGON | da9503c | 2022-11-25 09:10:07 +0100 | [diff] [blame] | 10994 | hlua_timer_init(&hlua->timer, hlua_timeout_session); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10995 | |
| 10996 | /* At this point the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 10997 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10998 | |
| 10999 | /* It's ok */ |
| 11000 | return 0; |
| 11001 | |
| 11002 | /* It's not ok. */ |
| 11003 | error: |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 11004 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 11005 | hlua_ctx_destroy(hlua); |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 11006 | ctx->hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 11007 | return 1; |
| 11008 | } |
| 11009 | |
| 11010 | static int hlua_cli_io_handler_fct(struct appctx *appctx) |
| 11011 | { |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 11012 | struct hlua_cli_ctx *ctx = appctx->svcctx; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 11013 | struct hlua *hlua; |
Willy Tarreau | 475e463 | 2022-05-27 10:26:46 +0200 | [diff] [blame] | 11014 | struct stconn *sc; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 11015 | struct hlua_function *fcn; |
| 11016 | |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 11017 | hlua = ctx->hlua; |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 11018 | sc = appctx_sc(appctx); |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 11019 | fcn = ctx->fcn; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 11020 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 11021 | /* Execute the function. */ |
| 11022 | switch (hlua_ctx_resume(hlua, 1)) { |
| 11023 | |
| 11024 | /* finished. */ |
| 11025 | case HLUA_E_OK: |
| 11026 | return 1; |
| 11027 | |
| 11028 | /* yield. */ |
| 11029 | case HLUA_E_AGAIN: |
| 11030 | /* We want write. */ |
| 11031 | if (HLUA_IS_WAKERESWR(hlua)) |
Willy Tarreau | 475e463 | 2022-05-27 10:26:46 +0200 | [diff] [blame] | 11032 | sc_need_room(sc); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 11033 | /* Set the timeout. */ |
| 11034 | if (hlua->wake_time != TICK_ETERNITY) |
| 11035 | task_schedule(hlua->task, hlua->wake_time); |
| 11036 | return 0; |
| 11037 | |
| 11038 | /* finished with error. */ |
| 11039 | case HLUA_E_ERRMSG: |
| 11040 | /* Display log. */ |
| 11041 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", |
| 11042 | fcn->name, lua_tostring(hlua->T, -1)); |
| 11043 | lua_pop(hlua->T, 1); |
| 11044 | return 1; |
| 11045 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 11046 | case HLUA_E_ETMOUT: |
| 11047 | SEND_ERR(NULL, "Lua converter '%s': execution timeout.\n", |
| 11048 | fcn->name); |
| 11049 | return 1; |
| 11050 | |
| 11051 | case HLUA_E_NOMEM: |
| 11052 | SEND_ERR(NULL, "Lua converter '%s': out of memory error.\n", |
| 11053 | fcn->name); |
| 11054 | return 1; |
| 11055 | |
| 11056 | case HLUA_E_YIELD: /* unexpected */ |
| 11057 | SEND_ERR(NULL, "Lua converter '%s': yield not allowed.\n", |
| 11058 | fcn->name); |
| 11059 | return 1; |
| 11060 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 11061 | case HLUA_E_ERR: |
| 11062 | /* Display log. */ |
| 11063 | SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n", |
| 11064 | fcn->name); |
| 11065 | return 1; |
| 11066 | |
| 11067 | default: |
| 11068 | return 1; |
| 11069 | } |
| 11070 | |
| 11071 | return 1; |
| 11072 | } |
| 11073 | |
| 11074 | static void hlua_cli_io_release_fct(struct appctx *appctx) |
| 11075 | { |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 11076 | struct hlua_cli_ctx *ctx = appctx->svcctx; |
| 11077 | |
| 11078 | hlua_ctx_destroy(ctx->hlua); |
| 11079 | ctx->hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 11080 | } |
| 11081 | |
| 11082 | /* This function is an LUA binding used for registering |
| 11083 | * new keywords in the cli. It expects a list of keywords |
| 11084 | * which are the "path". It is limited to 5 keywords. A |
| 11085 | * description of the command, a function to be executed |
| 11086 | * for the parsing and a function for io handlers. |
| 11087 | */ |
| 11088 | __LJMP static int hlua_register_cli(lua_State *L) |
| 11089 | { |
| 11090 | struct cli_kw_list *cli_kws; |
| 11091 | const char *message; |
| 11092 | int ref_io; |
| 11093 | int len; |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 11094 | struct hlua_function *fcn = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 11095 | int index; |
| 11096 | int i; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 11097 | struct buffer *trash; |
| 11098 | const char *kw[5]; |
| 11099 | struct cli_kw *cli_kw; |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 11100 | const char *errmsg; |
Willy Tarreau | 22450af | 2023-04-07 15:27:55 +0200 | [diff] [blame] | 11101 | char *end; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 11102 | |
| 11103 | MAY_LJMP(check_args(L, 3, "register_cli")); |
| 11104 | |
Aurelien DARRAGON | 87f5297 | 2023-02-24 09:43:54 +0100 | [diff] [blame] | 11105 | if (hlua_gethlua(L)) { |
| 11106 | /* runtime processing */ |
| 11107 | WILL_LJMP(luaL_error(L, "register_cli: not available outside of body context")); |
| 11108 | } |
| 11109 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 11110 | /* First argument : an array of maximum 5 keywords. */ |
| 11111 | if (!lua_istable(L, 1)) |
| 11112 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table")); |
| 11113 | |
| 11114 | /* Second argument : string with contextual message. */ |
| 11115 | message = MAY_LJMP(luaL_checkstring(L, 2)); |
| 11116 | |
| 11117 | /* Third and fourth argument : lua function. */ |
| 11118 | ref_io = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 11119 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 11120 | /* Check for CLI service already registered */ |
| 11121 | trash = get_trash_chunk(); |
| 11122 | index = 0; |
| 11123 | lua_pushnil(L); |
| 11124 | memset(kw, 0, sizeof(kw)); |
| 11125 | while (lua_next(L, 1) != 0) { |
Aurelien DARRAGON | 55afbed | 2023-03-02 18:42:06 +0100 | [diff] [blame] | 11126 | if (index >= CLI_PREFIX_KW_NB) { |
| 11127 | hlua_unref(L, ref_io); |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 11128 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries")); |
Aurelien DARRAGON | 55afbed | 2023-03-02 18:42:06 +0100 | [diff] [blame] | 11129 | } |
| 11130 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 11131 | hlua_unref(L, ref_io); |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 11132 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings")); |
Aurelien DARRAGON | 55afbed | 2023-03-02 18:42:06 +0100 | [diff] [blame] | 11133 | } |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 11134 | kw[index] = lua_tostring(L, -1); |
| 11135 | if (index == 0) |
| 11136 | chunk_printf(trash, "%s", kw[index]); |
| 11137 | else |
| 11138 | chunk_appendf(trash, " %s", kw[index]); |
| 11139 | index++; |
| 11140 | lua_pop(L, 1); |
| 11141 | } |
| 11142 | cli_kw = cli_find_kw_exact((char **)kw); |
| 11143 | if (cli_kw != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11144 | fcn = cli_kw->private; |
| 11145 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 11146 | ha_warning("Trying to register CLI keyword 'lua.%s' more than once. " |
| 11147 | "This will become a hard error in version 2.5.\n", trash->area); |
Aurelien DARRAGON | 55afbed | 2023-03-02 18:42:06 +0100 | [diff] [blame] | 11148 | hlua_unref(L, fcn->function_ref[hlua_state_id]); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11149 | } |
| 11150 | fcn->function_ref[hlua_state_id] = ref_io; |
| 11151 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 11152 | } |
| 11153 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 11154 | /* Allocate and fill the sample fetch keyword struct. */ |
| 11155 | cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 11156 | if (!cli_kws) { |
| 11157 | errmsg = "Lua out of memory error."; |
| 11158 | goto error; |
| 11159 | } |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 11160 | fcn = new_hlua_function(); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 11161 | if (!fcn) { |
| 11162 | errmsg = "Lua out of memory error."; |
| 11163 | goto error; |
| 11164 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 11165 | |
| 11166 | /* Fill path. */ |
| 11167 | index = 0; |
| 11168 | lua_pushnil(L); |
| 11169 | while(lua_next(L, 1) != 0) { |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 11170 | if (index >= 5) { |
| 11171 | errmsg = "1st argument must be a table with a maximum of 5 entries"; |
| 11172 | goto error; |
| 11173 | } |
| 11174 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 11175 | errmsg = "1st argument must be a table filled with strings"; |
| 11176 | goto error; |
| 11177 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 11178 | cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1)); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 11179 | if (!cli_kws->kw[0].str_kw[index]) { |
| 11180 | errmsg = "Lua out of memory error."; |
| 11181 | goto error; |
| 11182 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 11183 | index++; |
| 11184 | lua_pop(L, 1); |
| 11185 | } |
| 11186 | |
| 11187 | /* Copy help message. */ |
| 11188 | cli_kws->kw[0].usage = strdup(message); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 11189 | if (!cli_kws->kw[0].usage) { |
| 11190 | errmsg = "Lua out of memory error."; |
| 11191 | goto error; |
| 11192 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 11193 | |
| 11194 | /* Fill fcn io handler. */ |
| 11195 | len = strlen("<lua.cli>") + 1; |
| 11196 | for (i = 0; i < index; i++) |
| 11197 | len += strlen(cli_kws->kw[0].str_kw[i]) + 1; |
| 11198 | fcn->name = calloc(1, len); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 11199 | if (!fcn->name) { |
| 11200 | errmsg = "Lua out of memory error."; |
| 11201 | goto error; |
| 11202 | } |
Willy Tarreau | 22450af | 2023-04-07 15:27:55 +0200 | [diff] [blame] | 11203 | |
| 11204 | end = fcn->name; |
| 11205 | len = 8; |
| 11206 | memcpy(end, "<lua.cli", len); |
| 11207 | end += len; |
| 11208 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 11209 | for (i = 0; i < index; i++) { |
Willy Tarreau | 22450af | 2023-04-07 15:27:55 +0200 | [diff] [blame] | 11210 | *(end++) = '.'; |
| 11211 | len = strlen(cli_kws->kw[0].str_kw[i]); |
| 11212 | memcpy(end, cli_kws->kw[0].str_kw[i], len); |
| 11213 | end += len; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 11214 | } |
Willy Tarreau | 22450af | 2023-04-07 15:27:55 +0200 | [diff] [blame] | 11215 | *(end++) = '>'; |
| 11216 | *(end++) = 0; |
| 11217 | |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 11218 | fcn->function_ref[hlua_state_id] = ref_io; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 11219 | |
| 11220 | /* Fill last entries. */ |
| 11221 | cli_kws->kw[0].private = fcn; |
| 11222 | cli_kws->kw[0].parse = hlua_cli_parse_fct; |
| 11223 | cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct; |
| 11224 | cli_kws->kw[0].io_release = hlua_cli_io_release_fct; |
| 11225 | |
| 11226 | /* Register this new converter */ |
| 11227 | cli_register_kw(cli_kws); |
| 11228 | |
| 11229 | return 0; |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 11230 | |
| 11231 | error: |
| 11232 | release_hlua_function(fcn); |
Aurelien DARRAGON | 55afbed | 2023-03-02 18:42:06 +0100 | [diff] [blame] | 11233 | hlua_unref(L, ref_io); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 11234 | if (cli_kws) { |
| 11235 | for (i = 0; i < index; i++) |
| 11236 | ha_free((char **)&(cli_kws->kw[0].str_kw[i])); |
| 11237 | ha_free((char **)&(cli_kws->kw[0].usage)); |
| 11238 | } |
| 11239 | ha_free(&cli_kws); |
| 11240 | WILL_LJMP(luaL_error(L, errmsg)); |
| 11241 | return 0; /* Never reached */ |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 11242 | } |
| 11243 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11244 | static int hlua_filter_init_per_thread(struct proxy *px, struct flt_conf *fconf) |
| 11245 | { |
| 11246 | struct hlua_flt_config *conf = fconf->conf; |
| 11247 | lua_State *L; |
| 11248 | int error, pos, state_id, flt_ref; |
| 11249 | |
| 11250 | state_id = reg_flt_to_stack_id(conf->reg); |
| 11251 | L = hlua_states[state_id]; |
| 11252 | pos = lua_gettop(L); |
| 11253 | |
| 11254 | /* The filter parsing function */ |
Aurelien DARRAGON | 4fdf8b5 | 2023-03-20 17:22:37 +0100 | [diff] [blame] | 11255 | hlua_pushref(L, conf->reg->fun_ref[state_id]); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11256 | |
| 11257 | /* Push the filter class on the stack and resolve all callbacks */ |
Aurelien DARRAGON | 4fdf8b5 | 2023-03-20 17:22:37 +0100 | [diff] [blame] | 11258 | hlua_pushref(L, conf->reg->flt_ref[state_id]); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11259 | |
| 11260 | /* Duplicate the filter class so each filter will have its own copy */ |
| 11261 | lua_newtable(L); |
| 11262 | lua_pushnil(L); |
| 11263 | |
| 11264 | while (lua_next(L, pos+2)) { |
| 11265 | lua_pushvalue(L, -2); |
| 11266 | lua_insert(L, -2); |
| 11267 | lua_settable(L, -4); |
| 11268 | } |
Aurelien DARRAGON | 73d1a98 | 2023-03-20 17:42:08 +0100 | [diff] [blame] | 11269 | flt_ref = hlua_ref(L); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11270 | |
| 11271 | /* Remove the original lua filter class from the stack */ |
| 11272 | lua_pop(L, 1); |
| 11273 | |
| 11274 | /* Push the copy on the stack */ |
Aurelien DARRAGON | 4fdf8b5 | 2023-03-20 17:22:37 +0100 | [diff] [blame] | 11275 | hlua_pushref(L, flt_ref); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11276 | |
| 11277 | /* extra args are pushed in a table */ |
| 11278 | lua_newtable(L); |
| 11279 | for (pos = 0; conf->args[pos]; pos++) { |
| 11280 | /* Check stack available size. */ |
| 11281 | if (!lua_checkstack(L, 1)) { |
| 11282 | ha_alert("Lua filter '%s' : Lua error : full stack.", conf->reg->name); |
| 11283 | goto error; |
| 11284 | } |
| 11285 | lua_pushstring(L, conf->args[pos]); |
| 11286 | lua_rawseti(L, -2, lua_rawlen(L, -2) + 1); |
| 11287 | } |
| 11288 | |
| 11289 | error = lua_pcall(L, 2, LUA_MULTRET, 0); |
| 11290 | switch (error) { |
| 11291 | case LUA_OK: |
| 11292 | /* replace the filter ref */ |
| 11293 | conf->ref[state_id] = flt_ref; |
| 11294 | break; |
| 11295 | case LUA_ERRRUN: |
| 11296 | ha_alert("Lua filter '%s' : runtime error : %s", conf->reg->name, lua_tostring(L, -1)); |
| 11297 | goto error; |
| 11298 | case LUA_ERRMEM: |
| 11299 | ha_alert("Lua filter '%s' : out of memory error", conf->reg->name); |
| 11300 | goto error; |
| 11301 | case LUA_ERRERR: |
| 11302 | ha_alert("Lua filter '%s' : message handler error : %s", conf->reg->name, lua_tostring(L, -1)); |
| 11303 | goto error; |
| 11304 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM <= 503 |
| 11305 | case LUA_ERRGCMM: |
| 11306 | ha_alert("Lua filter '%s' : garbage collector error : %s", conf->reg->name, lua_tostring(L, -1)); |
| 11307 | goto error; |
| 11308 | #endif |
| 11309 | default: |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 11310 | ha_alert("Lua filter '%s' : unknown error : %s", conf->reg->name, lua_tostring(L, -1)); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11311 | goto error; |
| 11312 | } |
| 11313 | |
| 11314 | lua_settop(L, 0); |
| 11315 | return 0; |
| 11316 | |
| 11317 | error: |
| 11318 | lua_settop(L, 0); |
| 11319 | return -1; |
| 11320 | } |
| 11321 | |
| 11322 | static void hlua_filter_deinit_per_thread(struct proxy *px, struct flt_conf *fconf) |
| 11323 | { |
| 11324 | struct hlua_flt_config *conf = fconf->conf; |
| 11325 | lua_State *L; |
| 11326 | int state_id; |
| 11327 | |
| 11328 | if (!conf) |
| 11329 | return; |
| 11330 | |
| 11331 | state_id = reg_flt_to_stack_id(conf->reg); |
| 11332 | L = hlua_states[state_id]; |
Aurelien DARRAGON | fde199d | 2023-03-20 15:09:33 +0100 | [diff] [blame] | 11333 | hlua_unref(L, conf->ref[state_id]); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11334 | } |
| 11335 | |
| 11336 | static int hlua_filter_init(struct proxy *px, struct flt_conf *fconf) |
| 11337 | { |
| 11338 | struct hlua_flt_config *conf = fconf->conf; |
| 11339 | int state_id = reg_flt_to_stack_id(conf->reg); |
| 11340 | |
| 11341 | /* Rely on per-thread init for global scripts */ |
| 11342 | if (!state_id) |
| 11343 | return hlua_filter_init_per_thread(px, fconf); |
| 11344 | return 0; |
| 11345 | } |
| 11346 | |
| 11347 | static void hlua_filter_deinit(struct proxy *px, struct flt_conf *fconf) |
| 11348 | { |
| 11349 | |
| 11350 | if (fconf->conf) { |
| 11351 | struct hlua_flt_config *conf = fconf->conf; |
| 11352 | int state_id = reg_flt_to_stack_id(conf->reg); |
| 11353 | int pos; |
| 11354 | |
| 11355 | /* Rely on per-thread deinit for global scripts */ |
| 11356 | if (!state_id) |
| 11357 | hlua_filter_deinit_per_thread(px, fconf); |
| 11358 | |
| 11359 | for (pos = 0; conf->args[pos]; pos++) |
| 11360 | free(conf->args[pos]); |
| 11361 | free(conf->args); |
| 11362 | } |
| 11363 | ha_free(&fconf->conf); |
| 11364 | ha_free((char **)&fconf->id); |
| 11365 | ha_free(&fconf->ops); |
| 11366 | } |
| 11367 | |
| 11368 | static int hlua_filter_new(struct stream *s, struct filter *filter) |
| 11369 | { |
| 11370 | struct hlua_flt_config *conf = FLT_CONF(filter); |
| 11371 | struct hlua_flt_ctx *flt_ctx = NULL; |
| 11372 | int ret = 1; |
| 11373 | |
| 11374 | /* In the execution wrappers linked with a stream, the |
| 11375 | * Lua context can be not initialized. This behavior |
| 11376 | * permits to save performances because a systematic |
| 11377 | * Lua initialization cause 5% performances loss. |
| 11378 | */ |
| 11379 | if (!s->hlua) { |
| 11380 | struct hlua *hlua; |
| 11381 | |
| 11382 | hlua = pool_alloc(pool_head_hlua); |
| 11383 | if (!hlua) { |
| 11384 | SEND_ERR(s->be, "Lua filter '%s': can't initialize Lua context.\n", |
| 11385 | conf->reg->name); |
| 11386 | ret = 0; |
| 11387 | goto end; |
| 11388 | } |
| 11389 | HLUA_INIT(hlua); |
| 11390 | s->hlua = hlua; |
Aurelien DARRAGON | 6b0b9bd | 2023-03-21 14:02:16 +0100 | [diff] [blame] | 11391 | if (!hlua_ctx_init(s->hlua, reg_flt_to_stack_id(conf->reg), s->task)) { |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11392 | SEND_ERR(s->be, "Lua filter '%s': can't initialize Lua context.\n", |
| 11393 | conf->reg->name); |
| 11394 | ret = 0; |
| 11395 | goto end; |
| 11396 | } |
| 11397 | } |
| 11398 | |
| 11399 | flt_ctx = pool_zalloc(pool_head_hlua_flt_ctx); |
| 11400 | if (!flt_ctx) { |
| 11401 | SEND_ERR(s->be, "Lua filter '%s': can't initialize filter Lua context.\n", |
| 11402 | conf->reg->name); |
| 11403 | ret = 0; |
| 11404 | goto end; |
| 11405 | } |
| 11406 | flt_ctx->hlua[0] = pool_alloc(pool_head_hlua); |
| 11407 | flt_ctx->hlua[1] = pool_alloc(pool_head_hlua); |
| 11408 | if (!flt_ctx->hlua[0] || !flt_ctx->hlua[1]) { |
| 11409 | SEND_ERR(s->be, "Lua filter '%s': can't initialize filter Lua context.\n", |
| 11410 | conf->reg->name); |
| 11411 | ret = 0; |
| 11412 | goto end; |
| 11413 | } |
| 11414 | HLUA_INIT(flt_ctx->hlua[0]); |
| 11415 | HLUA_INIT(flt_ctx->hlua[1]); |
Aurelien DARRAGON | 6b0b9bd | 2023-03-21 14:02:16 +0100 | [diff] [blame] | 11416 | if (!hlua_ctx_init(flt_ctx->hlua[0], reg_flt_to_stack_id(conf->reg), s->task) || |
| 11417 | !hlua_ctx_init(flt_ctx->hlua[1], reg_flt_to_stack_id(conf->reg), s->task)) { |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11418 | SEND_ERR(s->be, "Lua filter '%s': can't initialize filter Lua context.\n", |
| 11419 | conf->reg->name); |
| 11420 | ret = 0; |
| 11421 | goto end; |
| 11422 | } |
| 11423 | |
| 11424 | if (!HLUA_IS_RUNNING(s->hlua)) { |
| 11425 | /* The following Lua calls can fail. */ |
| 11426 | if (!SET_SAFE_LJMP(s->hlua)) { |
| 11427 | const char *error; |
| 11428 | |
| 11429 | if (lua_type(s->hlua->T, -1) == LUA_TSTRING) |
| 11430 | error = lua_tostring(s->hlua->T, -1); |
| 11431 | else |
| 11432 | error = "critical error"; |
| 11433 | SEND_ERR(s->be, "Lua filter '%s': %s.\n", conf->reg->name, error); |
| 11434 | ret = 0; |
| 11435 | goto end; |
| 11436 | } |
| 11437 | |
| 11438 | /* Check stack size. */ |
| 11439 | if (!lua_checkstack(s->hlua->T, 1)) { |
| 11440 | SEND_ERR(s->be, "Lua filter '%s': full stack.\n", conf->reg->name); |
Tim Duesterhus | 2281738 | 2021-09-11 23:17:25 +0200 | [diff] [blame] | 11441 | RESET_SAFE_LJMP(s->hlua); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11442 | ret = 0; |
| 11443 | goto end; |
| 11444 | } |
| 11445 | |
Aurelien DARRAGON | 4fdf8b5 | 2023-03-20 17:22:37 +0100 | [diff] [blame] | 11446 | hlua_pushref(s->hlua->T, conf->ref[s->hlua->state_id]); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11447 | if (lua_getfield(s->hlua->T, -1, "new") != LUA_TFUNCTION) { |
| 11448 | SEND_ERR(s->be, "Lua filter '%s': 'new' field is not a function.\n", |
| 11449 | conf->reg->name); |
| 11450 | RESET_SAFE_LJMP(s->hlua); |
| 11451 | ret = 0; |
| 11452 | goto end; |
| 11453 | } |
| 11454 | lua_insert(s->hlua->T, -2); |
| 11455 | |
| 11456 | /* Push the copy on the stack */ |
| 11457 | s->hlua->nargs = 1; |
| 11458 | |
| 11459 | /* We must initialize the execution timeouts. */ |
Aurelien DARRAGON | da9503c | 2022-11-25 09:10:07 +0100 | [diff] [blame] | 11460 | hlua_timer_init(&s->hlua->timer, hlua_timeout_session); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11461 | |
| 11462 | /* At this point the execution is safe. */ |
| 11463 | RESET_SAFE_LJMP(s->hlua); |
| 11464 | } |
| 11465 | |
| 11466 | switch (hlua_ctx_resume(s->hlua, 0)) { |
| 11467 | case HLUA_E_OK: |
| 11468 | /* Nothing returned or not a table, ignore the filter for current stream */ |
| 11469 | if (!lua_gettop(s->hlua->T) || !lua_istable(s->hlua->T, 1)) { |
| 11470 | ret = 0; |
| 11471 | goto end; |
| 11472 | } |
| 11473 | |
| 11474 | /* Attached the filter pointer to the ctx */ |
| 11475 | lua_pushstring(s->hlua->T, "__filter"); |
| 11476 | lua_pushlightuserdata(s->hlua->T, filter); |
| 11477 | lua_settable(s->hlua->T, -3); |
| 11478 | |
| 11479 | /* Save a ref on the filter ctx */ |
| 11480 | lua_pushvalue(s->hlua->T, 1); |
Aurelien DARRAGON | 73d1a98 | 2023-03-20 17:42:08 +0100 | [diff] [blame] | 11481 | flt_ctx->ref = hlua_ref(s->hlua->T); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11482 | filter->ctx = flt_ctx; |
| 11483 | break; |
| 11484 | case HLUA_E_ERRMSG: |
| 11485 | SEND_ERR(s->be, "Lua filter '%s' : %s.\n", conf->reg->name, lua_tostring(s->hlua->T, -1)); |
| 11486 | ret = -1; |
| 11487 | goto end; |
| 11488 | case HLUA_E_ETMOUT: |
| 11489 | SEND_ERR(s->be, "Lua filter '%s' : 'new' execution timeout.\n", conf->reg->name); |
| 11490 | ret = 0; |
| 11491 | goto end; |
| 11492 | case HLUA_E_NOMEM: |
| 11493 | SEND_ERR(s->be, "Lua filter '%s' : out of memory error.\n", conf->reg->name); |
| 11494 | ret = 0; |
| 11495 | goto end; |
| 11496 | case HLUA_E_AGAIN: |
| 11497 | case HLUA_E_YIELD: |
| 11498 | SEND_ERR(s->be, "Lua filter '%s': yield functions like core.tcp() or core.sleep()" |
| 11499 | " are not allowed from 'new' function.\n", conf->reg->name); |
| 11500 | ret = 0; |
| 11501 | goto end; |
| 11502 | case HLUA_E_ERR: |
| 11503 | SEND_ERR(s->be, "Lua filter '%s': 'new' returns an unknown error.\n", conf->reg->name); |
| 11504 | ret = 0; |
| 11505 | goto end; |
| 11506 | default: |
| 11507 | ret = 0; |
| 11508 | goto end; |
| 11509 | } |
| 11510 | |
| 11511 | end: |
| 11512 | if (s->hlua) |
| 11513 | lua_settop(s->hlua->T, 0); |
| 11514 | if (ret <= 0) { |
| 11515 | if (flt_ctx) { |
| 11516 | hlua_ctx_destroy(flt_ctx->hlua[0]); |
| 11517 | hlua_ctx_destroy(flt_ctx->hlua[1]); |
| 11518 | pool_free(pool_head_hlua_flt_ctx, flt_ctx); |
| 11519 | } |
| 11520 | } |
| 11521 | return ret; |
| 11522 | } |
| 11523 | |
| 11524 | static void hlua_filter_delete(struct stream *s, struct filter *filter) |
| 11525 | { |
| 11526 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 11527 | |
Aurelien DARRAGON | fde199d | 2023-03-20 15:09:33 +0100 | [diff] [blame] | 11528 | hlua_unref(s->hlua->T, flt_ctx->ref); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11529 | hlua_ctx_destroy(flt_ctx->hlua[0]); |
| 11530 | hlua_ctx_destroy(flt_ctx->hlua[1]); |
| 11531 | pool_free(pool_head_hlua_flt_ctx, flt_ctx); |
| 11532 | filter->ctx = NULL; |
| 11533 | } |
| 11534 | |
Christopher Faulet | 9f55a50 | 2020-02-25 15:21:02 +0100 | [diff] [blame] | 11535 | static int hlua_filter_from_payload(struct filter *filter) |
| 11536 | { |
| 11537 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 11538 | |
| 11539 | return (flt_ctx && !!(flt_ctx->flags & HLUA_FLT_CTX_FL_PAYLOAD)); |
| 11540 | } |
| 11541 | |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 11542 | static int hlua_filter_callback(struct stream *s, struct filter *filter, const char *fun, |
| 11543 | int dir, unsigned int flags) |
| 11544 | { |
| 11545 | struct hlua *flt_hlua; |
| 11546 | struct hlua_flt_config *conf = FLT_CONF(filter); |
| 11547 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 11548 | unsigned int hflags = HLUA_TXN_FLT_CTX; |
| 11549 | int ret = 1; |
| 11550 | |
| 11551 | flt_hlua = flt_ctx->hlua[(dir == SMP_OPT_DIR_REQ ? 0 : 1)]; |
| 11552 | if (!flt_hlua) |
| 11553 | goto end; |
| 11554 | |
| 11555 | if (!HLUA_IS_RUNNING(flt_hlua)) { |
| 11556 | int extra_idx = lua_gettop(flt_hlua->T); |
| 11557 | |
| 11558 | /* The following Lua calls can fail. */ |
| 11559 | if (!SET_SAFE_LJMP(flt_hlua)) { |
| 11560 | const char *error; |
| 11561 | |
| 11562 | if (lua_type(flt_hlua->T, -1) == LUA_TSTRING) |
| 11563 | error = lua_tostring(flt_hlua->T, -1); |
| 11564 | else |
| 11565 | error = "critical error"; |
| 11566 | SEND_ERR(s->be, "Lua filter '%s': %s.\n", conf->reg->name, error); |
| 11567 | goto end; |
| 11568 | } |
| 11569 | |
| 11570 | /* Check stack size. */ |
| 11571 | if (!lua_checkstack(flt_hlua->T, 3)) { |
| 11572 | SEND_ERR(s->be, "Lua filter '%s': full stack.\n", conf->reg->name); |
| 11573 | RESET_SAFE_LJMP(flt_hlua); |
| 11574 | goto end; |
| 11575 | } |
| 11576 | |
Aurelien DARRAGON | 4fdf8b5 | 2023-03-20 17:22:37 +0100 | [diff] [blame] | 11577 | hlua_pushref(flt_hlua->T, flt_ctx->ref); |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 11578 | if (lua_getfield(flt_hlua->T, -1, fun) != LUA_TFUNCTION) { |
| 11579 | RESET_SAFE_LJMP(flt_hlua); |
| 11580 | goto end; |
| 11581 | } |
| 11582 | lua_insert(flt_hlua->T, -2); |
| 11583 | |
| 11584 | if (!hlua_txn_new(flt_hlua->T, s, s->be, dir, hflags)) { |
| 11585 | SEND_ERR(s->be, "Lua filter '%s': full stack.\n", conf->reg->name); |
| 11586 | RESET_SAFE_LJMP(flt_hlua); |
| 11587 | goto end; |
| 11588 | } |
| 11589 | flt_hlua->nargs = 2; |
| 11590 | |
| 11591 | if (flags & HLUA_FLT_CB_ARG_CHN) { |
| 11592 | if (dir == SMP_OPT_DIR_REQ) |
| 11593 | lua_getfield(flt_hlua->T, -1, "req"); |
| 11594 | else |
| 11595 | lua_getfield(flt_hlua->T, -1, "res"); |
| 11596 | if (lua_type(flt_hlua->T, -1) == LUA_TTABLE) { |
| 11597 | lua_pushstring(flt_hlua->T, "__filter"); |
| 11598 | lua_pushlightuserdata(flt_hlua->T, filter); |
| 11599 | lua_settable(flt_hlua->T, -3); |
| 11600 | } |
| 11601 | flt_hlua->nargs++; |
| 11602 | } |
| 11603 | else if (flags & HLUA_FLT_CB_ARG_HTTP_MSG) { |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 11604 | if (dir == SMP_OPT_DIR_REQ) |
| 11605 | lua_getfield(flt_hlua->T, -1, "http_req"); |
| 11606 | else |
| 11607 | lua_getfield(flt_hlua->T, -1, "http_res"); |
| 11608 | if (lua_type(flt_hlua->T, -1) == LUA_TTABLE) { |
| 11609 | lua_pushstring(flt_hlua->T, "__filter"); |
| 11610 | lua_pushlightuserdata(flt_hlua->T, filter); |
| 11611 | lua_settable(flt_hlua->T, -3); |
| 11612 | } |
| 11613 | flt_hlua->nargs++; |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 11614 | } |
| 11615 | |
| 11616 | /* Check stack size. */ |
| 11617 | if (!lua_checkstack(flt_hlua->T, 1)) { |
| 11618 | SEND_ERR(s->be, "Lua filter '%s': full stack.\n", conf->reg->name); |
| 11619 | RESET_SAFE_LJMP(flt_hlua); |
| 11620 | goto end; |
| 11621 | } |
| 11622 | |
| 11623 | while (extra_idx--) { |
| 11624 | lua_pushvalue(flt_hlua->T, 1); |
| 11625 | lua_remove(flt_hlua->T, 1); |
| 11626 | flt_hlua->nargs++; |
| 11627 | } |
| 11628 | |
| 11629 | /* We must initialize the execution timeouts. */ |
Aurelien DARRAGON | da9503c | 2022-11-25 09:10:07 +0100 | [diff] [blame] | 11630 | hlua_timer_init(&flt_hlua->timer, hlua_timeout_session); |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 11631 | |
| 11632 | /* At this point the execution is safe. */ |
| 11633 | RESET_SAFE_LJMP(flt_hlua); |
| 11634 | } |
| 11635 | |
| 11636 | switch (hlua_ctx_resume(flt_hlua, !(flags & HLUA_FLT_CB_FINAL))) { |
| 11637 | case HLUA_E_OK: |
| 11638 | /* Catch the return value if it required */ |
| 11639 | if ((flags & HLUA_FLT_CB_RETVAL) && lua_gettop(flt_hlua->T) > 0) { |
| 11640 | ret = lua_tointeger(flt_hlua->T, -1); |
| 11641 | lua_settop(flt_hlua->T, 0); /* Empty the stack. */ |
| 11642 | } |
| 11643 | |
| 11644 | /* Set timeout in the required channel. */ |
| 11645 | if (flt_hlua->wake_time != TICK_ETERNITY) { |
| 11646 | if (dir == SMP_OPT_DIR_REQ) |
| 11647 | s->req.analyse_exp = flt_hlua->wake_time; |
| 11648 | else |
| 11649 | s->res.analyse_exp = flt_hlua->wake_time; |
| 11650 | } |
| 11651 | break; |
| 11652 | case HLUA_E_AGAIN: |
| 11653 | /* Set timeout in the required channel. */ |
| 11654 | if (flt_hlua->wake_time != TICK_ETERNITY) { |
| 11655 | if (dir == SMP_OPT_DIR_REQ) |
| 11656 | s->req.analyse_exp = flt_hlua->wake_time; |
| 11657 | else |
| 11658 | s->res.analyse_exp = flt_hlua->wake_time; |
| 11659 | } |
| 11660 | /* Some actions can be wake up when a "write" event |
| 11661 | * is detected on a response channel. This is useful |
| 11662 | * only for actions targeted on the requests. |
| 11663 | */ |
| 11664 | if (HLUA_IS_WAKERESWR(flt_hlua)) |
| 11665 | s->res.flags |= CF_WAKE_WRITE; |
| 11666 | if (HLUA_IS_WAKEREQWR(flt_hlua)) |
| 11667 | s->req.flags |= CF_WAKE_WRITE; |
| 11668 | ret = 0; |
| 11669 | goto end; |
| 11670 | case HLUA_E_ERRMSG: |
| 11671 | SEND_ERR(s->be, "Lua filter '%s' : %s.\n", conf->reg->name, lua_tostring(flt_hlua->T, -1)); |
| 11672 | ret = -1; |
| 11673 | goto end; |
| 11674 | case HLUA_E_ETMOUT: |
| 11675 | SEND_ERR(s->be, "Lua filter '%s' : '%s' callback execution timeout.\n", conf->reg->name, fun); |
| 11676 | goto end; |
| 11677 | case HLUA_E_NOMEM: |
| 11678 | SEND_ERR(s->be, "Lua filter '%s' : out of memory error.\n", conf->reg->name); |
| 11679 | goto end; |
| 11680 | case HLUA_E_YIELD: |
| 11681 | SEND_ERR(s->be, "Lua filter '%s': yield functions like core.tcp() or core.sleep()" |
| 11682 | " are not allowed from '%s' callback.\n", conf->reg->name, fun); |
| 11683 | goto end; |
| 11684 | case HLUA_E_ERR: |
| 11685 | SEND_ERR(s->be, "Lua filter '%s': '%s' returns an unknown error.\n", conf->reg->name, fun); |
| 11686 | goto end; |
| 11687 | default: |
| 11688 | goto end; |
| 11689 | } |
| 11690 | |
| 11691 | |
| 11692 | end: |
| 11693 | return ret; |
| 11694 | } |
| 11695 | |
| 11696 | static int hlua_filter_start_analyze(struct stream *s, struct filter *filter, struct channel *chn) |
| 11697 | { |
| 11698 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 11699 | |
| 11700 | flt_ctx->flags = 0; |
| 11701 | return hlua_filter_callback(s, filter, "start_analyze", |
| 11702 | (!(chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES), |
| 11703 | (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_RETVAL | HLUA_FLT_CB_ARG_CHN)); |
| 11704 | } |
| 11705 | |
| 11706 | static int hlua_filter_end_analyze(struct stream *s, struct filter *filter, struct channel *chn) |
| 11707 | { |
| 11708 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 11709 | |
| 11710 | flt_ctx->flags &= ~HLUA_FLT_CTX_FL_PAYLOAD; |
| 11711 | return hlua_filter_callback(s, filter, "end_analyze", |
| 11712 | (!(chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES), |
| 11713 | (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_RETVAL | HLUA_FLT_CB_ARG_CHN)); |
| 11714 | } |
| 11715 | |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 11716 | static int hlua_filter_http_headers(struct stream *s, struct filter *filter, struct http_msg *msg) |
| 11717 | { |
| 11718 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 11719 | |
| 11720 | flt_ctx->flags &= ~HLUA_FLT_CTX_FL_PAYLOAD; |
| 11721 | return hlua_filter_callback(s, filter, "http_headers", |
| 11722 | (!(msg->chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES), |
| 11723 | (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_RETVAL | HLUA_FLT_CB_ARG_HTTP_MSG)); |
| 11724 | } |
| 11725 | |
| 11726 | static int hlua_filter_http_payload(struct stream *s, struct filter *filter, struct http_msg *msg, |
| 11727 | unsigned int offset, unsigned int len) |
| 11728 | { |
| 11729 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 11730 | struct hlua *flt_hlua; |
| 11731 | int dir = (!(msg->chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES); |
| 11732 | int idx = (dir == SMP_OPT_DIR_REQ ? 0 : 1); |
| 11733 | int ret; |
| 11734 | |
| 11735 | flt_hlua = flt_ctx->hlua[idx]; |
| 11736 | flt_ctx->cur_off[idx] = offset; |
| 11737 | flt_ctx->cur_len[idx] = len; |
| 11738 | flt_ctx->flags |= HLUA_FLT_CTX_FL_PAYLOAD; |
| 11739 | ret = hlua_filter_callback(s, filter, "http_payload", dir, (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_ARG_HTTP_MSG)); |
| 11740 | if (ret != -1) { |
| 11741 | ret = flt_ctx->cur_len[idx]; |
| 11742 | if (lua_gettop(flt_hlua->T) > 0) { |
| 11743 | ret = lua_tointeger(flt_hlua->T, -1); |
| 11744 | if (ret > flt_ctx->cur_len[idx]) |
| 11745 | ret = flt_ctx->cur_len[idx]; |
| 11746 | lua_settop(flt_hlua->T, 0); /* Empty the stack. */ |
| 11747 | } |
| 11748 | } |
| 11749 | return ret; |
| 11750 | } |
| 11751 | |
| 11752 | static int hlua_filter_http_end(struct stream *s, struct filter *filter, struct http_msg *msg) |
| 11753 | { |
| 11754 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 11755 | |
| 11756 | flt_ctx->flags &= ~HLUA_FLT_CTX_FL_PAYLOAD; |
| 11757 | return hlua_filter_callback(s, filter, "http_end", |
| 11758 | (!(msg->chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES), |
| 11759 | (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_RETVAL | HLUA_FLT_CB_ARG_HTTP_MSG)); |
| 11760 | } |
| 11761 | |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 11762 | static int hlua_filter_tcp_payload(struct stream *s, struct filter *filter, struct channel *chn, |
| 11763 | unsigned int offset, unsigned int len) |
| 11764 | { |
| 11765 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 11766 | struct hlua *flt_hlua; |
| 11767 | int dir = (!(chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES); |
| 11768 | int idx = (dir == SMP_OPT_DIR_REQ ? 0 : 1); |
| 11769 | int ret; |
| 11770 | |
| 11771 | flt_hlua = flt_ctx->hlua[idx]; |
| 11772 | flt_ctx->cur_off[idx] = offset; |
| 11773 | flt_ctx->cur_len[idx] = len; |
| 11774 | flt_ctx->flags |= HLUA_FLT_CTX_FL_PAYLOAD; |
| 11775 | ret = hlua_filter_callback(s, filter, "tcp_payload", dir, (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_ARG_CHN)); |
| 11776 | if (ret != -1) { |
| 11777 | ret = flt_ctx->cur_len[idx]; |
| 11778 | if (lua_gettop(flt_hlua->T) > 0) { |
| 11779 | ret = lua_tointeger(flt_hlua->T, -1); |
| 11780 | if (ret > flt_ctx->cur_len[idx]) |
| 11781 | ret = flt_ctx->cur_len[idx]; |
| 11782 | lua_settop(flt_hlua->T, 0); /* Empty the stack. */ |
| 11783 | } |
| 11784 | } |
| 11785 | return ret; |
| 11786 | } |
| 11787 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11788 | static int hlua_filter_parse_fct(char **args, int *cur_arg, struct proxy *px, |
| 11789 | struct flt_conf *fconf, char **err, void *private) |
| 11790 | { |
| 11791 | struct hlua_reg_filter *reg_flt = private; |
| 11792 | lua_State *L; |
| 11793 | struct hlua_flt_config *conf = NULL; |
| 11794 | const char *flt_id = NULL; |
| 11795 | int state_id, pos, flt_flags = 0; |
| 11796 | struct flt_ops *hlua_flt_ops = NULL; |
| 11797 | |
| 11798 | state_id = reg_flt_to_stack_id(reg_flt); |
| 11799 | L = hlua_states[state_id]; |
| 11800 | |
| 11801 | /* Initialize the filter ops with default callbacks */ |
| 11802 | hlua_flt_ops = calloc(1, sizeof(*hlua_flt_ops)); |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 11803 | if (!hlua_flt_ops) |
| 11804 | goto error; |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11805 | hlua_flt_ops->init = hlua_filter_init; |
| 11806 | hlua_flt_ops->deinit = hlua_filter_deinit; |
| 11807 | if (state_id) { |
| 11808 | /* Set per-thread callback if script is loaded per-thread */ |
| 11809 | hlua_flt_ops->init_per_thread = hlua_filter_init_per_thread; |
| 11810 | hlua_flt_ops->deinit_per_thread = hlua_filter_deinit_per_thread; |
| 11811 | } |
| 11812 | hlua_flt_ops->attach = hlua_filter_new; |
| 11813 | hlua_flt_ops->detach = hlua_filter_delete; |
| 11814 | |
| 11815 | /* Push the filter class on the stack and resolve all callbacks */ |
Aurelien DARRAGON | 4fdf8b5 | 2023-03-20 17:22:37 +0100 | [diff] [blame] | 11816 | hlua_pushref(L, reg_flt->flt_ref[state_id]); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11817 | |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 11818 | if (lua_getfield(L, -1, "start_analyze") == LUA_TFUNCTION) |
| 11819 | hlua_flt_ops->channel_start_analyze = hlua_filter_start_analyze; |
| 11820 | lua_pop(L, 1); |
| 11821 | if (lua_getfield(L, -1, "end_analyze") == LUA_TFUNCTION) |
| 11822 | hlua_flt_ops->channel_end_analyze = hlua_filter_end_analyze; |
| 11823 | lua_pop(L, 1); |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 11824 | if (lua_getfield(L, -1, "http_headers") == LUA_TFUNCTION) |
| 11825 | hlua_flt_ops->http_headers = hlua_filter_http_headers; |
| 11826 | lua_pop(L, 1); |
| 11827 | if (lua_getfield(L, -1, "http_payload") == LUA_TFUNCTION) |
| 11828 | hlua_flt_ops->http_payload = hlua_filter_http_payload; |
| 11829 | lua_pop(L, 1); |
| 11830 | if (lua_getfield(L, -1, "http_end") == LUA_TFUNCTION) |
| 11831 | hlua_flt_ops->http_end = hlua_filter_http_end; |
| 11832 | lua_pop(L, 1); |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 11833 | if (lua_getfield(L, -1, "tcp_payload") == LUA_TFUNCTION) |
| 11834 | hlua_flt_ops->tcp_payload = hlua_filter_tcp_payload; |
| 11835 | lua_pop(L, 1); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11836 | |
| 11837 | /* Get id and flags of the filter class */ |
| 11838 | if (lua_getfield(L, -1, "id") == LUA_TSTRING) |
| 11839 | flt_id = lua_tostring(L, -1); |
| 11840 | lua_pop(L, 1); |
| 11841 | if (lua_getfield(L, -1, "flags") == LUA_TNUMBER) |
| 11842 | flt_flags = lua_tointeger(L, -1); |
| 11843 | lua_pop(L, 1); |
| 11844 | |
| 11845 | /* Create the filter config */ |
| 11846 | conf = calloc(1, sizeof(*conf)); |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 11847 | if (!conf) |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11848 | goto error; |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11849 | conf->reg = reg_flt; |
| 11850 | |
| 11851 | /* duplicate args */ |
| 11852 | for (pos = 0; *args[*cur_arg + 1 + pos]; pos++); |
| 11853 | conf->args = calloc(pos + 1, sizeof(*conf->args)); |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 11854 | if (!conf->args) |
| 11855 | goto error; |
| 11856 | for (pos = 0; *args[*cur_arg + 1 + pos]; pos++) { |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11857 | conf->args[pos] = strdup(args[*cur_arg + 1 + pos]); |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 11858 | if (!conf->args[pos]) |
| 11859 | goto error; |
| 11860 | } |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11861 | conf->args[pos] = NULL; |
| 11862 | *cur_arg += pos + 1; |
| 11863 | |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 11864 | if (flt_id) { |
| 11865 | fconf->id = strdup(flt_id); |
| 11866 | if (!fconf->id) |
| 11867 | goto error; |
| 11868 | } |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11869 | fconf->flags = flt_flags; |
| 11870 | fconf->conf = conf; |
| 11871 | fconf->ops = hlua_flt_ops; |
| 11872 | |
| 11873 | lua_settop(L, 0); |
| 11874 | return 0; |
| 11875 | |
| 11876 | error: |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 11877 | memprintf(err, "Lua filter '%s' : Lua out of memory error", reg_flt->name); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11878 | free(hlua_flt_ops); |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 11879 | if (conf && conf->args) { |
| 11880 | for (pos = 0; conf->args[pos]; pos++) |
| 11881 | free(conf->args[pos]); |
| 11882 | free(conf->args); |
| 11883 | } |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11884 | free(conf); |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 11885 | free((char *)fconf->id); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11886 | lua_settop(L, 0); |
| 11887 | return -1; |
| 11888 | } |
| 11889 | |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 11890 | __LJMP static int hlua_register_data_filter(lua_State *L) |
| 11891 | { |
| 11892 | struct filter *filter; |
| 11893 | struct channel *chn; |
| 11894 | |
| 11895 | MAY_LJMP(check_args(L, 2, "register_data_filter")); |
| 11896 | MAY_LJMP(luaL_checktype(L, 1, LUA_TTABLE)); |
| 11897 | chn = MAY_LJMP(hlua_checkchannel(L, 2)); |
| 11898 | |
| 11899 | lua_getfield(L, 1, "__filter"); |
| 11900 | MAY_LJMP(luaL_checktype(L, -1, LUA_TLIGHTUSERDATA)); |
| 11901 | filter = lua_touserdata (L, -1); |
| 11902 | lua_pop(L, 1); |
| 11903 | |
| 11904 | register_data_filter(chn_strm(chn), chn, filter); |
| 11905 | return 1; |
| 11906 | } |
| 11907 | |
| 11908 | __LJMP static int hlua_unregister_data_filter(lua_State *L) |
| 11909 | { |
| 11910 | struct filter *filter; |
| 11911 | struct channel *chn; |
| 11912 | |
| 11913 | MAY_LJMP(check_args(L, 2, "unregister_data_filter")); |
| 11914 | MAY_LJMP(luaL_checktype(L, 1, LUA_TTABLE)); |
| 11915 | chn = MAY_LJMP(hlua_checkchannel(L, 2)); |
| 11916 | |
| 11917 | lua_getfield(L, 1, "__filter"); |
| 11918 | MAY_LJMP(luaL_checktype(L, -1, LUA_TLIGHTUSERDATA)); |
| 11919 | filter = lua_touserdata (L, -1); |
| 11920 | lua_pop(L, 1); |
| 11921 | |
| 11922 | unregister_data_filter(chn_strm(chn), chn, filter); |
| 11923 | return 1; |
| 11924 | } |
| 11925 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11926 | /* This function is an LUA binding used for registering a filter. It expects a |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 11927 | * filter name used in the haproxy configuration file and a LUA function to |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11928 | * parse configuration arguments. |
| 11929 | */ |
| 11930 | __LJMP static int hlua_register_filter(lua_State *L) |
| 11931 | { |
| 11932 | struct buffer *trash; |
| 11933 | struct flt_kw_list *fkl; |
| 11934 | struct flt_kw *fkw; |
| 11935 | const char *name; |
| 11936 | struct hlua_reg_filter *reg_flt= NULL; |
| 11937 | int flt_ref, fun_ref; |
| 11938 | int len; |
| 11939 | |
| 11940 | MAY_LJMP(check_args(L, 3, "register_filter")); |
| 11941 | |
Aurelien DARRAGON | 87f5297 | 2023-02-24 09:43:54 +0100 | [diff] [blame] | 11942 | if (hlua_gethlua(L)) { |
| 11943 | /* runtime processing */ |
| 11944 | WILL_LJMP(luaL_error(L, "register_filter: not available outside of body context")); |
| 11945 | } |
| 11946 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11947 | /* First argument : filter name. */ |
| 11948 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 11949 | |
| 11950 | /* Second argument : The filter class */ |
| 11951 | flt_ref = MAY_LJMP(hlua_checktable(L, 2)); |
| 11952 | |
| 11953 | /* Third argument : lua function. */ |
| 11954 | fun_ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 11955 | |
| 11956 | trash = get_trash_chunk(); |
| 11957 | chunk_printf(trash, "lua.%s", name); |
| 11958 | fkw = flt_find_kw(trash->area); |
| 11959 | if (fkw != NULL) { |
| 11960 | reg_flt = fkw->private; |
| 11961 | if (reg_flt->flt_ref[hlua_state_id] != -1 || reg_flt->fun_ref[hlua_state_id] != -1) { |
| 11962 | ha_warning("Trying to register filter 'lua.%s' more than once. " |
| 11963 | "This will become a hard error in version 2.5.\n", name); |
Aurelien DARRAGON | 55afbed | 2023-03-02 18:42:06 +0100 | [diff] [blame] | 11964 | if (reg_flt->flt_ref[hlua_state_id] != -1) |
| 11965 | hlua_unref(L, reg_flt->flt_ref[hlua_state_id]); |
| 11966 | if (reg_flt->fun_ref[hlua_state_id] != -1) |
| 11967 | hlua_unref(L, reg_flt->fun_ref[hlua_state_id]); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11968 | } |
| 11969 | reg_flt->flt_ref[hlua_state_id] = flt_ref; |
| 11970 | reg_flt->fun_ref[hlua_state_id] = fun_ref; |
| 11971 | return 0; |
| 11972 | } |
| 11973 | |
| 11974 | fkl = calloc(1, sizeof(*fkl) + sizeof(struct flt_kw) * 2); |
| 11975 | if (!fkl) |
| 11976 | goto alloc_error; |
| 11977 | fkl->scope = "HLUA"; |
| 11978 | |
| 11979 | reg_flt = new_hlua_reg_filter(name); |
| 11980 | if (!reg_flt) |
| 11981 | goto alloc_error; |
| 11982 | |
| 11983 | reg_flt->flt_ref[hlua_state_id] = flt_ref; |
| 11984 | reg_flt->fun_ref[hlua_state_id] = fun_ref; |
| 11985 | |
| 11986 | /* The filter keyword */ |
| 11987 | len = strlen("lua.") + strlen(name) + 1; |
| 11988 | fkl->kw[0].kw = calloc(1, len); |
| 11989 | if (!fkl->kw[0].kw) |
| 11990 | goto alloc_error; |
| 11991 | |
| 11992 | snprintf((char *)fkl->kw[0].kw, len, "lua.%s", name); |
| 11993 | |
| 11994 | fkl->kw[0].parse = hlua_filter_parse_fct; |
| 11995 | fkl->kw[0].private = reg_flt; |
| 11996 | memset(&fkl->kw[1], 0, sizeof(*fkl->kw)); |
| 11997 | |
| 11998 | /* Register this new filter */ |
| 11999 | flt_register_keywords(fkl); |
| 12000 | |
| 12001 | return 0; |
| 12002 | |
| 12003 | alloc_error: |
| 12004 | release_hlua_reg_filter(reg_flt); |
Aurelien DARRAGON | 55afbed | 2023-03-02 18:42:06 +0100 | [diff] [blame] | 12005 | hlua_unref(L, flt_ref); |
| 12006 | hlua_unref(L, fun_ref); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 12007 | ha_free(&fkl); |
| 12008 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 12009 | return 0; /* Never reached */ |
| 12010 | } |
| 12011 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 12012 | 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] | 12013 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 12014 | char **err, unsigned int *timeout) |
| 12015 | { |
| 12016 | const char *error; |
| 12017 | |
| 12018 | error = parse_time_err(args[1], timeout, TIME_UNIT_MS); |
Willy Tarreau | 9faebe3 | 2019-06-07 19:00:37 +0200 | [diff] [blame] | 12019 | if (error == PARSE_TIME_OVER) { |
| 12020 | memprintf(err, "timer overflow in argument <%s> to <%s> (maximum value is 2147483647 ms or ~24.8 days)", |
| 12021 | args[1], args[0]); |
| 12022 | return -1; |
| 12023 | } |
| 12024 | else if (error == PARSE_TIME_UNDER) { |
| 12025 | memprintf(err, "timer underflow in argument <%s> to <%s> (minimum non-null value is 1 ms)", |
| 12026 | args[1], args[0]); |
| 12027 | return -1; |
| 12028 | } |
| 12029 | else if (error) { |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 12030 | memprintf(err, "%s: invalid timeout", args[0]); |
| 12031 | return -1; |
| 12032 | } |
| 12033 | return 0; |
| 12034 | } |
| 12035 | |
Aurelien DARRAGON | 58e36e5 | 2023-04-06 22:51:56 +0200 | [diff] [blame] | 12036 | static int hlua_burst_timeout(char **args, int section_type, struct proxy *curpx, |
| 12037 | const struct proxy *defpx, const char *file, int line, |
| 12038 | char **err) |
| 12039 | { |
| 12040 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 12041 | file, line, err, &hlua_timeout_burst); |
| 12042 | } |
| 12043 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 12044 | 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] | 12045 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 12046 | char **err) |
| 12047 | { |
| 12048 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 12049 | file, line, err, &hlua_timeout_session); |
| 12050 | } |
| 12051 | |
| 12052 | 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] | 12053 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 12054 | char **err) |
| 12055 | { |
| 12056 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 12057 | file, line, err, &hlua_timeout_task); |
| 12058 | } |
| 12059 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 12060 | 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] | 12061 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 12062 | char **err) |
| 12063 | { |
| 12064 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 12065 | file, line, err, &hlua_timeout_applet); |
| 12066 | } |
| 12067 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 12068 | 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] | 12069 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 12070 | char **err) |
| 12071 | { |
| 12072 | char *error; |
| 12073 | |
| 12074 | hlua_nb_instruction = strtoll(args[1], &error, 10); |
| 12075 | if (*error != '\0') { |
| 12076 | memprintf(err, "%s: invalid number", args[0]); |
| 12077 | return -1; |
| 12078 | } |
| 12079 | return 0; |
| 12080 | } |
| 12081 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 12082 | 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] | 12083 | const struct proxy *defpx, const char *file, int line, |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 12084 | char **err) |
| 12085 | { |
| 12086 | char *error; |
| 12087 | |
| 12088 | if (*(args[1]) == 0) { |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 12089 | memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).", args[0]); |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 12090 | return -1; |
| 12091 | } |
| 12092 | hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L; |
| 12093 | if (*error != '\0') { |
| 12094 | memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error); |
| 12095 | return -1; |
| 12096 | } |
| 12097 | return 0; |
| 12098 | } |
| 12099 | |
| 12100 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 12101 | /* This function is called by the main configuration key "lua-load". It loads and |
| 12102 | * execute an lua file during the parsing of the HAProxy configuration file. It is |
| 12103 | * the main lua entry point. |
| 12104 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 12105 | * This function runs with the HAProxy keywords API. It returns -1 if an error |
| 12106 | * occurs, otherwise it returns 0. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 12107 | * |
| 12108 | * In some error case, LUA set an error message in top of the stack. This function |
| 12109 | * 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] | 12110 | * |
| 12111 | * This function can fail with an abort() due to an Lua critical error. |
| 12112 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 12113 | * tolerated. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 12114 | */ |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 12115 | static int hlua_load_state(char **args, lua_State *L, char **err) |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 12116 | { |
| 12117 | int error; |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 12118 | int nargs; |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 12119 | |
| 12120 | /* Just load and compile the file. */ |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 12121 | error = luaL_loadfile(L, args[0]); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 12122 | if (error) { |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 12123 | memprintf(err, "error in Lua file '%s': %s", args[0], lua_tostring(L, -1)); |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 12124 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 12125 | return -1; |
| 12126 | } |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 12127 | |
| 12128 | /* Push args in the Lua stack, except the first one which is the filename */ |
| 12129 | for (nargs = 1; *(args[nargs]) != 0; nargs++) { |
Aurelien DARRAGON | 4d7aefe | 2022-09-23 10:22:14 +0200 | [diff] [blame] | 12130 | /* Check stack size. */ |
| 12131 | if (!lua_checkstack(L, 1)) { |
| 12132 | memprintf(err, "Lua runtime error while loading arguments: stack is full."); |
| 12133 | return -1; |
| 12134 | } |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 12135 | lua_pushstring(L, args[nargs]); |
| 12136 | } |
| 12137 | nargs--; |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 12138 | |
| 12139 | /* If no syntax error where detected, execute the code. */ |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 12140 | error = lua_pcall(L, nargs, LUA_MULTRET, 0); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 12141 | switch (error) { |
| 12142 | case LUA_OK: |
| 12143 | break; |
| 12144 | case LUA_ERRRUN: |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 12145 | memprintf(err, "Lua runtime error: %s", lua_tostring(L, -1)); |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 12146 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 12147 | return -1; |
| 12148 | case LUA_ERRMEM: |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 12149 | memprintf(err, "Lua out of memory error"); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 12150 | return -1; |
| 12151 | case LUA_ERRERR: |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 12152 | memprintf(err, "Lua message handler error: %s", lua_tostring(L, -1)); |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 12153 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 12154 | return -1; |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 12155 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM <= 503 |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 12156 | case LUA_ERRGCMM: |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 12157 | memprintf(err, "Lua garbage collector error: %s", lua_tostring(L, -1)); |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 12158 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 12159 | return -1; |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 12160 | #endif |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 12161 | default: |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 12162 | memprintf(err, "Lua unknown error: %s", lua_tostring(L, -1)); |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 12163 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 12164 | return -1; |
| 12165 | } |
| 12166 | |
| 12167 | return 0; |
| 12168 | } |
| 12169 | |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 12170 | static int hlua_load(char **args, int section_type, struct proxy *curpx, |
Willy Tarreau | 0182516 | 2021-03-09 09:53:46 +0100 | [diff] [blame] | 12171 | const struct proxy *defpx, const char *file, int line, |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 12172 | char **err) |
| 12173 | { |
| 12174 | if (*(args[1]) == 0) { |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 12175 | memprintf(err, "'%s' expects a file name as parameter.", args[0]); |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 12176 | return -1; |
| 12177 | } |
| 12178 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 12179 | /* loading for global state */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 12180 | hlua_state_id = 0; |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 12181 | ha_set_thread(NULL); |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 12182 | return hlua_load_state(&args[1], hlua_states[0], err); |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 12183 | } |
| 12184 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 12185 | 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] | 12186 | const struct proxy *defpx, const char *file, int line, |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 12187 | char **err) |
| 12188 | { |
| 12189 | int len; |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 12190 | int i; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 12191 | |
| 12192 | if (*(args[1]) == 0) { |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 12193 | memprintf(err, "'%s' expects a file as parameter.", args[0]); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 12194 | return -1; |
| 12195 | } |
| 12196 | |
| 12197 | if (per_thread_load == NULL) { |
| 12198 | /* allocate the first entry large enough to store the final NULL */ |
| 12199 | per_thread_load = calloc(1, sizeof(*per_thread_load)); |
| 12200 | if (per_thread_load == NULL) { |
| 12201 | memprintf(err, "out of memory error"); |
| 12202 | return -1; |
| 12203 | } |
| 12204 | } |
| 12205 | |
| 12206 | /* count used entries */ |
| 12207 | for (len = 0; per_thread_load[len] != NULL; len++) |
| 12208 | ; |
| 12209 | |
| 12210 | per_thread_load = realloc(per_thread_load, (len + 2) * sizeof(*per_thread_load)); |
| 12211 | if (per_thread_load == NULL) { |
| 12212 | memprintf(err, "out of memory error"); |
| 12213 | return -1; |
| 12214 | } |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 12215 | per_thread_load[len + 1] = NULL; |
| 12216 | |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 12217 | /* count args excepting the first, allocate array and copy args */ |
| 12218 | for (i = 0; *(args[i + 1]) != 0; i++); |
Aurelien DARRAGON | b12d169 | 2022-09-23 08:48:34 +0200 | [diff] [blame] | 12219 | per_thread_load[len] = calloc(i + 1, sizeof(*per_thread_load[len])); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 12220 | if (per_thread_load[len] == NULL) { |
| 12221 | memprintf(err, "out of memory error"); |
| 12222 | return -1; |
| 12223 | } |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 12224 | for (i = 1; *(args[i]) != 0; i++) { |
| 12225 | per_thread_load[len][i - 1] = strdup(args[i]); |
| 12226 | if (per_thread_load[len][i - 1] == NULL) { |
| 12227 | memprintf(err, "out of memory error"); |
| 12228 | return -1; |
| 12229 | } |
| 12230 | } |
| 12231 | per_thread_load[len][i - 1] = strdup(""); |
| 12232 | if (per_thread_load[len][i - 1] == NULL) { |
| 12233 | memprintf(err, "out of memory error"); |
| 12234 | return -1; |
| 12235 | } |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 12236 | |
| 12237 | /* loading for thread 1 only */ |
| 12238 | hlua_state_id = 1; |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 12239 | ha_set_thread(NULL); |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 12240 | return hlua_load_state(per_thread_load[len], hlua_states[1], err); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 12241 | } |
| 12242 | |
Tim Duesterhus | c9fc9f2 | 2020-01-12 13:55:39 +0100 | [diff] [blame] | 12243 | /* Prepend the given <path> followed by a semicolon to the `package.<type>` variable |
| 12244 | * in the given <ctx>. |
| 12245 | */ |
Thierry Fournier | 3fb9e51 | 2020-11-28 10:13:12 +0100 | [diff] [blame] | 12246 | static int hlua_prepend_path(lua_State *L, char *type, char *path) |
Tim Duesterhus | c9fc9f2 | 2020-01-12 13:55:39 +0100 | [diff] [blame] | 12247 | { |
Thierry Fournier | 3fb9e51 | 2020-11-28 10:13:12 +0100 | [diff] [blame] | 12248 | lua_getglobal(L, "package"); /* push package variable */ |
| 12249 | lua_pushstring(L, path); /* push given path */ |
| 12250 | lua_pushstring(L, ";"); /* push semicolon */ |
| 12251 | lua_getfield(L, -3, type); /* push old path */ |
| 12252 | lua_concat(L, 3); /* concatenate to new path */ |
| 12253 | lua_setfield(L, -2, type); /* store new path */ |
| 12254 | lua_pop(L, 1); /* pop package variable */ |
Tim Duesterhus | c9fc9f2 | 2020-01-12 13:55:39 +0100 | [diff] [blame] | 12255 | |
| 12256 | return 0; |
| 12257 | } |
| 12258 | |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 12259 | 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] | 12260 | const struct proxy *defpx, const char *file, int line, |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 12261 | char **err) |
| 12262 | { |
| 12263 | char *path; |
| 12264 | char *type = "path"; |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 12265 | struct prepend_path *p = NULL; |
Bertrand Jacquin | 7fbc770 | 2021-11-24 21:16:06 +0000 | [diff] [blame] | 12266 | size_t i; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 12267 | |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 12268 | if (too_many_args(2, args, err, NULL)) { |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 12269 | goto err; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 12270 | } |
| 12271 | |
| 12272 | if (!(*args[1])) { |
| 12273 | memprintf(err, "'%s' expects to receive a <path> as argument", args[0]); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 12274 | goto err; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 12275 | } |
| 12276 | path = args[1]; |
| 12277 | |
| 12278 | if (*args[2]) { |
| 12279 | if (strcmp(args[2], "path") != 0 && strcmp(args[2], "cpath") != 0) { |
| 12280 | 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] | 12281 | goto err; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 12282 | } |
| 12283 | type = args[2]; |
| 12284 | } |
| 12285 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 12286 | p = calloc(1, sizeof(*p)); |
| 12287 | if (p == NULL) { |
Tim Duesterhus | f89d43a | 2021-01-03 20:04:37 +0100 | [diff] [blame] | 12288 | memprintf(err, "memory allocation failed"); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 12289 | goto err; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 12290 | } |
| 12291 | p->path = strdup(path); |
| 12292 | if (p->path == NULL) { |
Tim Duesterhus | f89d43a | 2021-01-03 20:04:37 +0100 | [diff] [blame] | 12293 | memprintf(err, "memory allocation failed"); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 12294 | goto err2; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 12295 | } |
| 12296 | p->type = strdup(type); |
| 12297 | if (p->type == NULL) { |
Tim Duesterhus | f89d43a | 2021-01-03 20:04:37 +0100 | [diff] [blame] | 12298 | memprintf(err, "memory allocation failed"); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 12299 | goto err2; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 12300 | } |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 12301 | LIST_APPEND(&prepend_path_list, &p->l); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 12302 | |
Tim Duesterhus | 9e5e586 | 2021-10-11 18:51:08 +0200 | [diff] [blame] | 12303 | /* Handle the global state and the per-thread state for the first |
| 12304 | * thread. The remaining threads will be initialized based on |
| 12305 | * prepend_path_list. |
| 12306 | */ |
Bertrand Jacquin | 7fbc770 | 2021-11-24 21:16:06 +0000 | [diff] [blame] | 12307 | for (i = 0; i < 2; i++) { |
Tim Duesterhus | 9e5e586 | 2021-10-11 18:51:08 +0200 | [diff] [blame] | 12308 | lua_State *L = hlua_states[i]; |
| 12309 | const char *error; |
| 12310 | |
| 12311 | if (setjmp(safe_ljmp_env) != 0) { |
| 12312 | lua_atpanic(L, hlua_panic_safe); |
| 12313 | if (lua_type(L, -1) == LUA_TSTRING) |
| 12314 | error = lua_tostring(L, -1); |
| 12315 | else |
| 12316 | error = "critical error"; |
| 12317 | fprintf(stderr, "lua-prepend-path: %s.\n", error); |
| 12318 | exit(1); |
| 12319 | } else { |
| 12320 | lua_atpanic(L, hlua_panic_ljmp); |
| 12321 | } |
| 12322 | |
| 12323 | hlua_prepend_path(L, type, path); |
| 12324 | |
| 12325 | lua_atpanic(L, hlua_panic_safe); |
| 12326 | } |
| 12327 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 12328 | return 0; |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 12329 | |
| 12330 | err2: |
| 12331 | free(p->type); |
| 12332 | free(p->path); |
| 12333 | err: |
| 12334 | free(p); |
| 12335 | return -1; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 12336 | } |
| 12337 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 12338 | /* configuration keywords declaration */ |
| 12339 | static struct cfg_kw_list cfg_kws = {{ },{ |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 12340 | { CFG_GLOBAL, "lua-prepend-path", hlua_config_prepend_path }, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 12341 | { CFG_GLOBAL, "lua-load", hlua_load }, |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 12342 | { CFG_GLOBAL, "lua-load-per-thread", hlua_load_per_thread }, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 12343 | { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout }, |
| 12344 | { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout }, |
Thierry FOURNIER | 56da101 | 2015-10-01 08:42:31 +0200 | [diff] [blame] | 12345 | { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout }, |
Aurelien DARRAGON | 58e36e5 | 2023-04-06 22:51:56 +0200 | [diff] [blame] | 12346 | { CFG_GLOBAL, "tune.lua.burst-timeout", hlua_burst_timeout }, |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 12347 | { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield }, |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 12348 | { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem }, |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 12349 | { 0, NULL, NULL }, |
| 12350 | }}; |
| 12351 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 12352 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |
| 12353 | |
William Lallemand | 5213918 | 2022-03-30 15:05:42 +0200 | [diff] [blame] | 12354 | #ifdef USE_OPENSSL |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 12355 | |
William Lallemand | 30fcca1 | 2022-03-30 12:03:12 +0200 | [diff] [blame] | 12356 | /* |
| 12357 | * This function replace a ckch_store by another one, and rebuild the ckch_inst and all its dependencies. |
| 12358 | * It does the sam as "cli_io_handler_commit_cert" but for lua, the major |
| 12359 | * difference is that the yield in lua and for the CLI is not handled the same |
| 12360 | * way. |
| 12361 | */ |
| 12362 | __LJMP static int hlua_ckch_commit_yield(lua_State *L, int status, lua_KContext ctx) |
| 12363 | { |
| 12364 | struct ckch_inst **lua_ckchi = lua_touserdata(L, -1); |
| 12365 | struct ckch_store **lua_ckchs = lua_touserdata(L, -2); |
| 12366 | struct ckch_inst *ckchi = *lua_ckchi; |
| 12367 | struct ckch_store *old_ckchs = lua_ckchs[0]; |
| 12368 | struct ckch_store *new_ckchs = lua_ckchs[1]; |
| 12369 | struct hlua *hlua; |
| 12370 | char *err = NULL; |
| 12371 | int y = 1; |
| 12372 | |
| 12373 | hlua = hlua_gethlua(L); |
| 12374 | |
| 12375 | /* get the first ckchi to copy */ |
| 12376 | if (ckchi == NULL) |
| 12377 | ckchi = LIST_ELEM(old_ckchs->ckch_inst.n, typeof(ckchi), by_ckchs); |
| 12378 | |
| 12379 | /* walk through the old ckch_inst and creates new ckch_inst using the updated ckchs */ |
| 12380 | list_for_each_entry_from(ckchi, &old_ckchs->ckch_inst, by_ckchs) { |
| 12381 | struct ckch_inst *new_inst; |
| 12382 | |
| 12383 | /* it takes a lot of CPU to creates SSL_CTXs, so we yield every 10 CKCH instances */ |
| 12384 | if (y % 10 == 0) { |
| 12385 | |
| 12386 | *lua_ckchi = ckchi; |
| 12387 | |
| 12388 | task_wakeup(hlua->task, TASK_WOKEN_MSG); |
| 12389 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_ckch_commit_yield, TICK_ETERNITY, 0)); |
| 12390 | } |
| 12391 | |
| 12392 | if (ckch_inst_rebuild(new_ckchs, ckchi, &new_inst, &err)) |
| 12393 | goto error; |
| 12394 | |
| 12395 | /* link the new ckch_inst to the duplicate */ |
| 12396 | LIST_APPEND(&new_ckchs->ckch_inst, &new_inst->by_ckchs); |
| 12397 | y++; |
| 12398 | } |
| 12399 | |
| 12400 | /* The generation is finished, we can insert everything */ |
| 12401 | ckch_store_replace(old_ckchs, new_ckchs); |
| 12402 | |
| 12403 | lua_pop(L, 2); /* pop the lua_ckchs and ckchi */ |
| 12404 | |
| 12405 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 12406 | |
| 12407 | return 0; |
| 12408 | |
| 12409 | error: |
| 12410 | ckch_store_free(new_ckchs); |
| 12411 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 12412 | WILL_LJMP(luaL_error(L, "%s", err)); |
| 12413 | free(err); |
| 12414 | |
| 12415 | return 0; |
| 12416 | } |
| 12417 | |
| 12418 | /* |
| 12419 | * Replace a ckch_store <filename> in the ckchs_tree with a ckch_store created |
| 12420 | * from the table in parameter. |
| 12421 | * |
| 12422 | * This is equivalent to "set ssl cert" + "commit ssl cert" over the CLI, which |
| 12423 | * means it does not need to have a transaction since everything is done in the |
| 12424 | * same function. |
| 12425 | * |
| 12426 | * CertCache.set{filename="", crt="", key="", sctl="", ocsp="", issuer=""} |
| 12427 | * |
| 12428 | */ |
| 12429 | __LJMP static int hlua_ckch_set(lua_State *L) |
| 12430 | { |
| 12431 | struct hlua *hlua; |
| 12432 | struct ckch_inst **lua_ckchi; |
| 12433 | struct ckch_store **lua_ckchs; |
| 12434 | struct ckch_store *old_ckchs = NULL; |
| 12435 | struct ckch_store *new_ckchs = NULL; |
| 12436 | int errcode = 0; |
| 12437 | char *err = NULL; |
| 12438 | struct cert_exts *cert_ext = NULL; |
| 12439 | char *filename; |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 12440 | struct ckch_data *data; |
William Lallemand | 30fcca1 | 2022-03-30 12:03:12 +0200 | [diff] [blame] | 12441 | int ret; |
| 12442 | |
| 12443 | if (lua_type(L, -1) != LUA_TTABLE) |
| 12444 | WILL_LJMP(luaL_error(L, "'CertCache.set' needs a table as argument")); |
| 12445 | |
| 12446 | hlua = hlua_gethlua(L); |
| 12447 | |
| 12448 | /* FIXME: this should not return an error but should come back later */ |
| 12449 | if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock)) |
| 12450 | WILL_LJMP(luaL_error(L, "CertCache already under lock")); |
| 12451 | |
| 12452 | ret = lua_getfield(L, -1, "filename"); |
| 12453 | if (ret != LUA_TSTRING) { |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 12454 | memprintf(&err, "%sNo filename specified!", err ? err : ""); |
William Lallemand | 30fcca1 | 2022-03-30 12:03:12 +0200 | [diff] [blame] | 12455 | errcode |= ERR_ALERT | ERR_FATAL; |
| 12456 | goto end; |
| 12457 | } |
| 12458 | filename = (char *)lua_tostring(L, -1); |
| 12459 | |
| 12460 | |
| 12461 | /* look for the filename in the tree */ |
| 12462 | old_ckchs = ckchs_lookup(filename); |
| 12463 | if (!old_ckchs) { |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 12464 | memprintf(&err, "%sCan't replace a certificate which is not referenced by the configuration!", err ? err : ""); |
William Lallemand | 30fcca1 | 2022-03-30 12:03:12 +0200 | [diff] [blame] | 12465 | errcode |= ERR_ALERT | ERR_FATAL; |
| 12466 | goto end; |
| 12467 | } |
| 12468 | /* TODO: handle extra_files_noext */ |
| 12469 | |
| 12470 | new_ckchs = ckchs_dup(old_ckchs); |
| 12471 | if (!new_ckchs) { |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 12472 | memprintf(&err, "%sCannot allocate memory!", err ? err : ""); |
William Lallemand | 30fcca1 | 2022-03-30 12:03:12 +0200 | [diff] [blame] | 12473 | errcode |= ERR_ALERT | ERR_FATAL; |
| 12474 | goto end; |
| 12475 | } |
| 12476 | |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 12477 | data = new_ckchs->data; |
William Lallemand | 30fcca1 | 2022-03-30 12:03:12 +0200 | [diff] [blame] | 12478 | |
| 12479 | /* loop on the field in the table, which have the same name as the |
| 12480 | * possible extensions of files */ |
| 12481 | lua_pushnil(L); |
| 12482 | while (lua_next(L, 1)) { |
| 12483 | int i; |
| 12484 | const char *field = lua_tostring(L, -2); |
| 12485 | char *payload = (char *)lua_tostring(L, -1); |
| 12486 | |
| 12487 | if (!field || strcmp(field, "filename") == 0) { |
| 12488 | lua_pop(L, 1); |
| 12489 | continue; |
| 12490 | } |
| 12491 | |
| 12492 | for (i = 0; field && cert_exts[i].ext != NULL; i++) { |
| 12493 | if (strcmp(field, cert_exts[i].ext) == 0) { |
| 12494 | cert_ext = &cert_exts[i]; |
| 12495 | break; |
| 12496 | } |
| 12497 | } |
| 12498 | |
| 12499 | /* this is the default type, the field is not supported */ |
| 12500 | if (cert_ext == NULL) { |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 12501 | memprintf(&err, "%sUnsupported field '%s'", err ? err : "", field); |
William Lallemand | 30fcca1 | 2022-03-30 12:03:12 +0200 | [diff] [blame] | 12502 | errcode |= ERR_ALERT | ERR_FATAL; |
| 12503 | goto end; |
| 12504 | } |
| 12505 | |
| 12506 | /* appply the change on the duplicate */ |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 12507 | if (cert_ext->load(filename, payload, data, &err) != 0) { |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 12508 | memprintf(&err, "%sCan't load the payload for '%s'", err ? err : "", cert_ext->ext); |
William Lallemand | 30fcca1 | 2022-03-30 12:03:12 +0200 | [diff] [blame] | 12509 | errcode |= ERR_ALERT | ERR_FATAL; |
| 12510 | goto end; |
| 12511 | } |
| 12512 | lua_pop(L, 1); |
| 12513 | } |
| 12514 | |
| 12515 | /* store the pointers on the lua stack */ |
| 12516 | lua_ckchs = lua_newuserdata(L, sizeof(struct ckch_store *) * 2); |
| 12517 | lua_ckchs[0] = old_ckchs; |
| 12518 | lua_ckchs[1] = new_ckchs; |
| 12519 | lua_ckchi = lua_newuserdata(L, sizeof(struct ckch_inst *)); |
| 12520 | *lua_ckchi = NULL; |
| 12521 | |
| 12522 | task_wakeup(hlua->task, TASK_WOKEN_MSG); |
| 12523 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_ckch_commit_yield, TICK_ETERNITY, 0)); |
| 12524 | |
| 12525 | end: |
| 12526 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 12527 | |
| 12528 | if (errcode & ERR_CODE) { |
| 12529 | ckch_store_free(new_ckchs); |
| 12530 | WILL_LJMP(luaL_error(L, "%s", err)); |
| 12531 | } |
| 12532 | free(err); |
| 12533 | |
| 12534 | return 0; |
| 12535 | } |
| 12536 | |
William Lallemand | 5213918 | 2022-03-30 15:05:42 +0200 | [diff] [blame] | 12537 | #else |
| 12538 | |
| 12539 | __LJMP static int hlua_ckch_set(lua_State *L) |
| 12540 | { |
| 12541 | WILL_LJMP(luaL_error(L, "'CertCache.set' needs an HAProxy built with OpenSSL")); |
| 12542 | |
| 12543 | return 0; |
| 12544 | } |
| 12545 | #endif /* ! USE_OPENSSL */ |
| 12546 | |
| 12547 | |
| 12548 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 12549 | /* This function can fail with an abort() due to an Lua critical error. |
| 12550 | * We are in the initialisation process of HAProxy, this abort() is |
| 12551 | * tolerated. |
| 12552 | */ |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 12553 | int hlua_post_init_state(lua_State *L) |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 12554 | { |
| 12555 | struct hlua_init_function *init; |
| 12556 | const char *msg; |
| 12557 | enum hlua_exec ret; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 12558 | const char *error; |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 12559 | const char *kind; |
| 12560 | const char *trace; |
| 12561 | int return_status = 1; |
| 12562 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
| 12563 | int nres; |
| 12564 | #endif |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 12565 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 12566 | /* disable memory limit checks if limit is not set */ |
| 12567 | if (!hlua_global_allocator.limit) |
| 12568 | hlua_global_allocator.limit = ~hlua_global_allocator.limit; |
| 12569 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 12570 | /* Call post initialisation function in safe environment. */ |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 12571 | if (setjmp(safe_ljmp_env) != 0) { |
| 12572 | lua_atpanic(L, hlua_panic_safe); |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 12573 | if (lua_type(L, -1) == LUA_TSTRING) |
| 12574 | error = lua_tostring(L, -1); |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 12575 | else |
| 12576 | error = "critical error"; |
| 12577 | fprintf(stderr, "Lua post-init: %s.\n", error); |
| 12578 | exit(1); |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 12579 | } else { |
| 12580 | lua_atpanic(L, hlua_panic_ljmp); |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 12581 | } |
Frédéric Lécaille | 54f2bcf | 2018-08-29 13:46:24 +0200 | [diff] [blame] | 12582 | |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 12583 | list_for_each_entry(init, &hlua_init_functions[hlua_state_id], l) { |
Aurelien DARRAGON | 4fdf8b5 | 2023-03-20 17:22:37 +0100 | [diff] [blame] | 12584 | hlua_pushref(L, init->function_ref); |
Aurelien DARRAGON | 16d047b | 2023-03-20 16:29:55 +0100 | [diff] [blame] | 12585 | /* function ref should be released right away since it was pushed |
| 12586 | * on the stack and will not be used anymore |
| 12587 | */ |
| 12588 | hlua_unref(L, init->function_ref); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 12589 | |
| 12590 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 12591 | ret = lua_resume(L, L, 0, &nres); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 12592 | #else |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 12593 | ret = lua_resume(L, L, 0); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 12594 | #endif |
| 12595 | kind = NULL; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 12596 | switch (ret) { |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 12597 | |
| 12598 | case LUA_OK: |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 12599 | lua_pop(L, -1); |
Thierry Fournier | 13d08b7 | 2020-11-28 11:02:58 +0100 | [diff] [blame] | 12600 | break; |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 12601 | |
| 12602 | case LUA_ERRERR: |
| 12603 | kind = "message handler error"; |
Willy Tarreau | 14de395 | 2022-11-14 07:08:28 +0100 | [diff] [blame] | 12604 | __fallthrough; |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 12605 | case LUA_ERRRUN: |
| 12606 | if (!kind) |
| 12607 | kind = "runtime error"; |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 12608 | msg = lua_tostring(L, -1); |
| 12609 | lua_settop(L, 0); /* Empty the stack. */ |
| 12610 | lua_pop(L, 1); |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 12611 | trace = hlua_traceback(L, ", "); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 12612 | if (msg) |
| 12613 | ha_alert("Lua init: %s: '%s' from %s\n", kind, msg, trace); |
| 12614 | else |
| 12615 | ha_alert("Lua init: unknown %s from %s\n", kind, trace); |
| 12616 | return_status = 0; |
| 12617 | break; |
| 12618 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 12619 | default: |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 12620 | /* Unknown error */ |
| 12621 | kind = "Unknown error"; |
Willy Tarreau | 14de395 | 2022-11-14 07:08:28 +0100 | [diff] [blame] | 12622 | __fallthrough; |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 12623 | case LUA_YIELD: |
| 12624 | /* yield is not configured at this step, this state doesn't happen */ |
| 12625 | if (!kind) |
| 12626 | kind = "yield not allowed"; |
Willy Tarreau | 14de395 | 2022-11-14 07:08:28 +0100 | [diff] [blame] | 12627 | __fallthrough; |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 12628 | case LUA_ERRMEM: |
| 12629 | if (!kind) |
| 12630 | kind = "out of memory error"; |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 12631 | lua_settop(L, 0); |
| 12632 | lua_pop(L, 1); |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 12633 | trace = hlua_traceback(L, ", "); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 12634 | ha_alert("Lua init: %s: %s\n", kind, trace); |
| 12635 | return_status = 0; |
| 12636 | break; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 12637 | } |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 12638 | if (!return_status) |
| 12639 | break; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 12640 | } |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 12641 | |
| 12642 | lua_atpanic(L, hlua_panic_safe); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 12643 | return return_status; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 12644 | } |
| 12645 | |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 12646 | int hlua_post_init() |
| 12647 | { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 12648 | int ret; |
| 12649 | int i; |
| 12650 | int errors; |
| 12651 | char *err = NULL; |
| 12652 | struct hlua_function *fcn; |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 12653 | struct hlua_reg_filter *reg_flt; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 12654 | |
Willy Tarreau | b131049 | 2021-08-30 09:35:18 +0200 | [diff] [blame] | 12655 | #if defined(USE_OPENSSL) |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 12656 | /* Initialize SSL server. */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 12657 | if (socket_ssl->xprt->prepare_srv) { |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 12658 | int saved_used_backed = global.ssl_used_backend; |
| 12659 | // don't affect maxconn automatic computation |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 12660 | socket_ssl->xprt->prepare_srv(socket_ssl); |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 12661 | global.ssl_used_backend = saved_used_backed; |
| 12662 | } |
| 12663 | #endif |
| 12664 | |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 12665 | /* Perform post init of common thread */ |
| 12666 | hlua_state_id = 0; |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 12667 | ha_set_thread(&ha_thread_info[0]); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 12668 | ret = hlua_post_init_state(hlua_states[hlua_state_id]); |
| 12669 | if (ret == 0) |
| 12670 | return 0; |
| 12671 | |
| 12672 | /* init remaining lua states and load files */ |
| 12673 | for (hlua_state_id = 2; hlua_state_id < global.nbthread + 1; hlua_state_id++) { |
| 12674 | |
| 12675 | /* set thread context */ |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 12676 | ha_set_thread(&ha_thread_info[hlua_state_id - 1]); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 12677 | |
| 12678 | /* Init lua state */ |
| 12679 | hlua_states[hlua_state_id] = hlua_init_state(hlua_state_id); |
| 12680 | |
| 12681 | /* Load lua files */ |
| 12682 | for (i = 0; per_thread_load && per_thread_load[i]; i++) { |
| 12683 | ret = hlua_load_state(per_thread_load[i], hlua_states[hlua_state_id], &err); |
| 12684 | if (ret != 0) { |
| 12685 | ha_alert("Lua init: %s\n", err); |
| 12686 | return 0; |
| 12687 | } |
| 12688 | } |
| 12689 | } |
| 12690 | |
| 12691 | /* Reset thread context */ |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 12692 | ha_set_thread(NULL); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 12693 | |
| 12694 | /* Execute post init for all states */ |
| 12695 | for (hlua_state_id = 1; hlua_state_id < global.nbthread + 1; hlua_state_id++) { |
| 12696 | |
| 12697 | /* set thread context */ |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 12698 | ha_set_thread(&ha_thread_info[hlua_state_id - 1]); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 12699 | |
| 12700 | /* run post init */ |
| 12701 | ret = hlua_post_init_state(hlua_states[hlua_state_id]); |
| 12702 | if (ret == 0) |
| 12703 | return 0; |
| 12704 | } |
| 12705 | |
| 12706 | /* Reset thread context */ |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 12707 | ha_set_thread(NULL); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 12708 | |
| 12709 | /* control functions registering. Each function must have: |
| 12710 | * - only the function_ref[0] set positive and all other to -1 |
| 12711 | * - only the function_ref[0] set to -1 and all other positive |
| 12712 | * This ensure a same reference is not used both in shared |
| 12713 | * lua state and thread dedicated lua state. Note: is the case |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 12714 | * reach, the shared state is priority, but the bug will be |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 12715 | * complicated to found for the end user. |
| 12716 | */ |
| 12717 | errors = 0; |
| 12718 | list_for_each_entry(fcn, &referenced_functions, l) { |
| 12719 | ret = 0; |
| 12720 | for (i = 1; i < global.nbthread + 1; i++) { |
| 12721 | if (fcn->function_ref[i] == -1) |
| 12722 | ret--; |
| 12723 | else |
| 12724 | ret++; |
| 12725 | } |
| 12726 | if (abs(ret) != global.nbthread) { |
| 12727 | ha_alert("Lua function '%s' is not referenced in all thread. " |
| 12728 | "Expect function in all thread or in none thread.\n", fcn->name); |
| 12729 | errors++; |
| 12730 | continue; |
| 12731 | } |
| 12732 | |
| 12733 | if ((fcn->function_ref[0] == -1) == (ret < 0)) { |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 12734 | ha_alert("Lua function '%s' is referenced both ins shared Lua context (through lua-load) " |
| 12735 | "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] | 12736 | "exclusive.\n", fcn->name); |
| 12737 | errors++; |
| 12738 | } |
| 12739 | } |
| 12740 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 12741 | /* Do the same with registered filters */ |
| 12742 | list_for_each_entry(reg_flt, &referenced_filters, l) { |
| 12743 | ret = 0; |
| 12744 | for (i = 1; i < global.nbthread + 1; i++) { |
| 12745 | if (reg_flt->flt_ref[i] == -1) |
| 12746 | ret--; |
| 12747 | else |
| 12748 | ret++; |
| 12749 | } |
| 12750 | if (abs(ret) != global.nbthread) { |
| 12751 | ha_alert("Lua filter '%s' is not referenced in all thread. " |
| 12752 | "Expect function in all thread or in none thread.\n", reg_flt->name); |
| 12753 | errors++; |
| 12754 | continue; |
| 12755 | } |
| 12756 | |
| 12757 | if ((reg_flt->flt_ref[0] == -1) == (ret < 0)) { |
| 12758 | ha_alert("Lua filter '%s' is referenced both ins shared Lua context (through lua-load) " |
| 12759 | "and per-thread Lua context (through lua-load-per-thread). these two context " |
| 12760 | "exclusive.\n", fcn->name); |
| 12761 | errors++; |
| 12762 | } |
| 12763 | } |
| 12764 | |
| 12765 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 12766 | if (errors > 0) |
| 12767 | return 0; |
| 12768 | |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 12769 | /* 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] | 12770 | * -1 in order to have probably a segfault if someone use it |
| 12771 | */ |
| 12772 | hlua_state_id = -1; |
| 12773 | |
| 12774 | return 1; |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 12775 | } |
| 12776 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 12777 | /* The memory allocator used by the Lua stack. <ud> is a pointer to the |
| 12778 | * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize> |
| 12779 | * 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] | 12780 | * allocation. <nsize> is the requested new size. A new allocation is |
| 12781 | * indicated by <ptr> being NULL. A free is indicated by <nsize> being |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 12782 | * zero. This one verifies that the limits are respected but is optimized |
| 12783 | * for the fast case where limits are not used, hence stats are not updated. |
Willy Tarreau | a5efdff | 2021-10-22 16:00:02 +0200 | [diff] [blame] | 12784 | * |
| 12785 | * Warning: while this API ressembles glibc's realloc() a lot, glibc surpasses |
| 12786 | * POSIX by making realloc(ptr,0) an effective free(), but others do not do |
| 12787 | * that and will simply allocate zero as if it were the result of malloc(0), |
| 12788 | * so mapping this onto realloc() will lead to memory leaks on non-glibc |
| 12789 | * systems. |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 12790 | */ |
| 12791 | static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize) |
| 12792 | { |
| 12793 | struct hlua_mem_allocator *zone = ud; |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 12794 | size_t limit, old, new; |
| 12795 | |
| 12796 | /* a limit of ~0 means unlimited and boot complete, so there's no need |
| 12797 | * for accounting anymore. |
| 12798 | */ |
Willy Tarreau | a5efdff | 2021-10-22 16:00:02 +0200 | [diff] [blame] | 12799 | if (likely(~zone->limit == 0)) { |
| 12800 | if (!nsize) |
| 12801 | ha_free(&ptr); |
| 12802 | else |
| 12803 | ptr = realloc(ptr, nsize); |
| 12804 | return ptr; |
| 12805 | } |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 12806 | |
Willy Tarreau | d36c7fa | 2020-12-02 12:26:29 +0100 | [diff] [blame] | 12807 | if (!ptr) |
| 12808 | osize = 0; |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 12809 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 12810 | /* enforce strict limits across all threads */ |
| 12811 | limit = zone->limit; |
| 12812 | old = _HA_ATOMIC_LOAD(&zone->allocated); |
| 12813 | do { |
| 12814 | new = old + nsize - osize; |
| 12815 | if (unlikely(nsize && limit && new > limit)) |
| 12816 | return NULL; |
| 12817 | } while (!_HA_ATOMIC_CAS(&zone->allocated, &old, new)); |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 12818 | |
Willy Tarreau | a5efdff | 2021-10-22 16:00:02 +0200 | [diff] [blame] | 12819 | if (!nsize) |
| 12820 | ha_free(&ptr); |
| 12821 | else |
| 12822 | ptr = realloc(ptr, nsize); |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 12823 | |
| 12824 | if (unlikely(!ptr && nsize)) // failed |
| 12825 | _HA_ATOMIC_SUB(&zone->allocated, nsize - osize); |
| 12826 | |
| 12827 | __ha_barrier_atomic_store(); |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 12828 | return ptr; |
| 12829 | } |
| 12830 | |
Thierry Fournier | ecb83c2 | 2020-11-28 15:49:44 +0100 | [diff] [blame] | 12831 | /* 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] | 12832 | * We are in the initialisation process of HAProxy, this abort() is |
| 12833 | * tolerated. |
| 12834 | */ |
Thierry Fournier | ecb83c2 | 2020-11-28 15:49:44 +0100 | [diff] [blame] | 12835 | lua_State *hlua_init_state(int thread_num) |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 12836 | { |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 12837 | int i; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 12838 | int idx; |
| 12839 | struct sample_fetch *sf; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 12840 | struct sample_conv *sc; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 12841 | char *p; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 12842 | const char *error_msg; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 12843 | void **context; |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12844 | lua_State *L; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 12845 | struct prepend_path *pp; |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 12846 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 12847 | /* Init main lua stack. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12848 | L = lua_newstate(hlua_alloc, &hlua_global_allocator); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 12849 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 12850 | /* Initialise Lua context to NULL */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12851 | context = lua_getextraspace(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 12852 | *context = NULL; |
| 12853 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 12854 | /* From this point, until the end of the initialisation function, |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 12855 | * the Lua function can fail with an abort. We are in the initialisation |
| 12856 | * process of HAProxy, this abort() is tolerated. |
| 12857 | */ |
| 12858 | |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 12859 | /* Call post initialisation function in safe environment. */ |
| 12860 | if (setjmp(safe_ljmp_env) != 0) { |
| 12861 | lua_atpanic(L, hlua_panic_safe); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12862 | if (lua_type(L, -1) == LUA_TSTRING) |
| 12863 | error_msg = lua_tostring(L, -1); |
Thierry Fournier | 2f05cc6 | 2020-11-28 16:08:02 +0100 | [diff] [blame] | 12864 | else |
| 12865 | error_msg = "critical error"; |
| 12866 | fprintf(stderr, "Lua init: %s.\n", error_msg); |
| 12867 | exit(1); |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 12868 | } else { |
| 12869 | lua_atpanic(L, hlua_panic_ljmp); |
Thierry Fournier | 2f05cc6 | 2020-11-28 16:08:02 +0100 | [diff] [blame] | 12870 | } |
| 12871 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 12872 | /* Initialise lua. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12873 | luaL_openlibs(L); |
Tim Duesterhus | 541fe1e | 2020-01-12 13:55:41 +0100 | [diff] [blame] | 12874 | #define HLUA_PREPEND_PATH_TOSTRING1(x) #x |
| 12875 | #define HLUA_PREPEND_PATH_TOSTRING(x) HLUA_PREPEND_PATH_TOSTRING1(x) |
| 12876 | #ifdef HLUA_PREPEND_PATH |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12877 | hlua_prepend_path(L, "path", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_PATH)); |
Tim Duesterhus | 541fe1e | 2020-01-12 13:55:41 +0100 | [diff] [blame] | 12878 | #endif |
| 12879 | #ifdef HLUA_PREPEND_CPATH |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12880 | hlua_prepend_path(L, "cpath", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_CPATH)); |
Tim Duesterhus | 541fe1e | 2020-01-12 13:55:41 +0100 | [diff] [blame] | 12881 | #endif |
| 12882 | #undef HLUA_PREPEND_PATH_TOSTRING |
| 12883 | #undef HLUA_PREPEND_PATH_TOSTRING1 |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 12884 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 12885 | /* Apply configured prepend path */ |
| 12886 | list_for_each_entry(pp, &prepend_path_list, l) |
| 12887 | hlua_prepend_path(L, pp->type, pp->path); |
| 12888 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 12889 | /* |
Aurelien DARRAGON | 8cd620b | 2023-04-07 17:37:46 +0200 | [diff] [blame] | 12890 | * Override some lua functions. |
| 12891 | * |
| 12892 | */ |
| 12893 | |
| 12894 | /* push our "safe" coroutine.create() function */ |
| 12895 | lua_getglobal(L, "coroutine"); |
| 12896 | lua_pushcclosure(L, hlua_coroutine_create, 0); |
| 12897 | lua_setfield(L, -2, "create"); |
| 12898 | |
| 12899 | /* |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 12900 | * |
| 12901 | * Create "core" object. |
| 12902 | * |
| 12903 | */ |
| 12904 | |
Thierry FOURNIER | a2d8c65 | 2015-03-11 17:29:39 +0100 | [diff] [blame] | 12905 | /* This table entry is the object "core" base. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12906 | lua_newtable(L); |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 12907 | |
Thierry Fournier | ecb83c2 | 2020-11-28 15:49:44 +0100 | [diff] [blame] | 12908 | /* set the thread id */ |
| 12909 | hlua_class_const_int(L, "thread", thread_num); |
| 12910 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 12911 | /* Push the loglevel constants. */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 12912 | for (i = 0; i < NB_LOG_LEVELS; i++) |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12913 | hlua_class_const_int(L, log_levels[i], i); |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 12914 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 12915 | /* Register special functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12916 | hlua_class_function(L, "register_init", hlua_register_init); |
| 12917 | hlua_class_function(L, "register_task", hlua_register_task); |
| 12918 | hlua_class_function(L, "register_fetches", hlua_register_fetches); |
| 12919 | hlua_class_function(L, "register_converters", hlua_register_converters); |
| 12920 | hlua_class_function(L, "register_action", hlua_register_action); |
| 12921 | hlua_class_function(L, "register_service", hlua_register_service); |
| 12922 | hlua_class_function(L, "register_cli", hlua_register_cli); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 12923 | hlua_class_function(L, "register_filter", hlua_register_filter); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12924 | hlua_class_function(L, "yield", hlua_yield); |
| 12925 | hlua_class_function(L, "set_nice", hlua_set_nice); |
| 12926 | hlua_class_function(L, "sleep", hlua_sleep); |
| 12927 | hlua_class_function(L, "msleep", hlua_msleep); |
| 12928 | hlua_class_function(L, "add_acl", hlua_add_acl); |
| 12929 | hlua_class_function(L, "del_acl", hlua_del_acl); |
| 12930 | hlua_class_function(L, "set_map", hlua_set_map); |
| 12931 | hlua_class_function(L, "del_map", hlua_del_map); |
| 12932 | hlua_class_function(L, "tcp", hlua_socket_new); |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 12933 | hlua_class_function(L, "httpclient", hlua_httpclient_new); |
Aurelien DARRAGON | c84899c | 2023-02-20 18:18:59 +0100 | [diff] [blame] | 12934 | hlua_class_function(L, "event_sub", hlua_event_global_sub); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12935 | hlua_class_function(L, "log", hlua_log); |
| 12936 | hlua_class_function(L, "Debug", hlua_log_debug); |
| 12937 | hlua_class_function(L, "Info", hlua_log_info); |
| 12938 | hlua_class_function(L, "Warning", hlua_log_warning); |
| 12939 | hlua_class_function(L, "Alert", hlua_log_alert); |
| 12940 | hlua_class_function(L, "done", hlua_done); |
| 12941 | hlua_fcn_reg_core_fcn(L); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 12942 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12943 | lua_setglobal(L, "core"); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 12944 | |
| 12945 | /* |
| 12946 | * |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 12947 | * Create "act" object. |
| 12948 | * |
| 12949 | */ |
| 12950 | |
| 12951 | /* This table entry is the object "act" base. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12952 | lua_newtable(L); |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 12953 | |
| 12954 | /* push action return constants */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12955 | hlua_class_const_int(L, "CONTINUE", ACT_RET_CONT); |
| 12956 | hlua_class_const_int(L, "STOP", ACT_RET_STOP); |
| 12957 | hlua_class_const_int(L, "YIELD", ACT_RET_YIELD); |
| 12958 | hlua_class_const_int(L, "ERROR", ACT_RET_ERR); |
| 12959 | hlua_class_const_int(L, "DONE", ACT_RET_DONE); |
| 12960 | hlua_class_const_int(L, "DENY", ACT_RET_DENY); |
| 12961 | hlua_class_const_int(L, "ABORT", ACT_RET_ABRT); |
| 12962 | hlua_class_const_int(L, "INVALID", ACT_RET_INV); |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 12963 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12964 | hlua_class_function(L, "wake_time", hlua_set_wake_time); |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 12965 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12966 | lua_setglobal(L, "act"); |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 12967 | |
| 12968 | /* |
| 12969 | * |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 12970 | * Create "Filter" object. |
| 12971 | * |
| 12972 | */ |
| 12973 | |
| 12974 | /* This table entry is the object "filter" base. */ |
| 12975 | lua_newtable(L); |
| 12976 | |
| 12977 | /* push flags and constants */ |
| 12978 | hlua_class_const_int(L, "CONTINUE", 1); |
| 12979 | hlua_class_const_int(L, "WAIT", 0); |
| 12980 | hlua_class_const_int(L, "ERROR", -1); |
| 12981 | |
| 12982 | hlua_class_const_int(L, "FLT_CFG_FL_HTX", FLT_CFG_FL_HTX); |
| 12983 | |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 12984 | hlua_class_function(L, "wake_time", hlua_set_wake_time); |
| 12985 | hlua_class_function(L, "register_data_filter", hlua_register_data_filter); |
| 12986 | hlua_class_function(L, "unregister_data_filter", hlua_unregister_data_filter); |
| 12987 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 12988 | lua_setglobal(L, "filter"); |
| 12989 | |
| 12990 | /* |
| 12991 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12992 | * Register class Map |
| 12993 | * |
| 12994 | */ |
| 12995 | |
| 12996 | /* This table entry is the object "Map" base. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12997 | lua_newtable(L); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12998 | |
| 12999 | /* register pattern types. */ |
| 13000 | for (i=0; i<PAT_MATCH_NUM; i++) |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13001 | hlua_class_const_int(L, pat_match_names[i], i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 13002 | for (i=0; i<PAT_MATCH_NUM; i++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 13003 | snprintf(trash.area, trash.size, "_%s", pat_match_names[i]); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13004 | hlua_class_const_int(L, trash.area, i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 13005 | } |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 13006 | |
| 13007 | /* register constructor. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13008 | hlua_class_function(L, "new", hlua_map_new); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 13009 | |
| 13010 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13011 | lua_newtable(L); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 13012 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 13013 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13014 | lua_pushstring(L, "__index"); |
| 13015 | lua_newtable(L); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 13016 | |
| 13017 | /* Register . */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13018 | hlua_class_function(L, "lookup", hlua_map_lookup); |
| 13019 | hlua_class_function(L, "slookup", hlua_map_slookup); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 13020 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13021 | lua_rawset(L, -3); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 13022 | |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 13023 | /* Register previous table in the registry with reference and named entry. |
| 13024 | * The function hlua_register_metatable() pops the stack, so we |
| 13025 | * previously create a copy of the table. |
| 13026 | */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13027 | lua_pushvalue(L, -1); /* Copy the -1 entry and push it on the stack. */ |
| 13028 | class_map_ref = hlua_register_metatable(L, CLASS_MAP); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 13029 | |
| 13030 | /* Assign the metatable to the mai Map object. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13031 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 13032 | |
| 13033 | /* Set a name to the table. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13034 | lua_setglobal(L, "Map"); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 13035 | |
| 13036 | /* |
| 13037 | * |
William Lallemand | 30fcca1 | 2022-03-30 12:03:12 +0200 | [diff] [blame] | 13038 | * Register "CertCache" class |
| 13039 | * |
| 13040 | */ |
| 13041 | |
| 13042 | /* Create and fill the metatable. */ |
| 13043 | lua_newtable(L); |
| 13044 | /* Register */ |
| 13045 | hlua_class_function(L, "set", hlua_ckch_set); |
| 13046 | lua_setglobal(L, CLASS_CERTCACHE); /* Create global object called Regex */ |
| 13047 | |
| 13048 | /* |
| 13049 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 13050 | * Register class Channel |
| 13051 | * |
| 13052 | */ |
| 13053 | |
| 13054 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13055 | lua_newtable(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 13056 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 13057 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13058 | lua_pushstring(L, "__index"); |
| 13059 | lua_newtable(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 13060 | |
| 13061 | /* Register . */ |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 13062 | hlua_class_function(L, "data", hlua_channel_get_data); |
| 13063 | hlua_class_function(L, "line", hlua_channel_get_line); |
| 13064 | hlua_class_function(L, "set", hlua_channel_set_data); |
| 13065 | hlua_class_function(L, "remove", hlua_channel_del_data); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13066 | hlua_class_function(L, "append", hlua_channel_append); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 13067 | hlua_class_function(L, "prepend", hlua_channel_prepend); |
| 13068 | hlua_class_function(L, "insert", hlua_channel_insert_data); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13069 | hlua_class_function(L, "send", hlua_channel_send); |
| 13070 | hlua_class_function(L, "forward", hlua_channel_forward); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 13071 | hlua_class_function(L, "input", hlua_channel_get_in_len); |
| 13072 | hlua_class_function(L, "output", hlua_channel_get_out_len); |
| 13073 | hlua_class_function(L, "may_recv", hlua_channel_may_recv); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13074 | hlua_class_function(L, "is_full", hlua_channel_is_full); |
| 13075 | hlua_class_function(L, "is_resp", hlua_channel_is_resp); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 13076 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 13077 | /* Deprecated API */ |
| 13078 | hlua_class_function(L, "get", hlua_channel_get); |
| 13079 | hlua_class_function(L, "dup", hlua_channel_dup); |
| 13080 | hlua_class_function(L, "getline", hlua_channel_getline); |
| 13081 | hlua_class_function(L, "get_in_len", hlua_channel_get_in_len); |
| 13082 | hlua_class_function(L, "get_out_len", hlua_channel_get_out_len); |
| 13083 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13084 | lua_rawset(L, -3); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 13085 | |
| 13086 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13087 | class_channel_ref = hlua_register_metatable(L, CLASS_CHANNEL); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 13088 | |
| 13089 | /* |
| 13090 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 13091 | * Register class Fetches |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 13092 | * |
| 13093 | */ |
| 13094 | |
| 13095 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13096 | lua_newtable(L); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 13097 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 13098 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13099 | lua_pushstring(L, "__index"); |
| 13100 | lua_newtable(L); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 13101 | |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 13102 | /* Browse existing fetches and create the associated |
| 13103 | * object method. |
| 13104 | */ |
| 13105 | sf = NULL; |
| 13106 | while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) { |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 13107 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 13108 | * by an underscore. |
| 13109 | */ |
Willy Tarreau | 5ef9656 | 2021-08-26 16:48:53 +0200 | [diff] [blame] | 13110 | strlcpy2(trash.area, sf->kw, trash.size); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 13111 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 13112 | if (*p == '.' || *p == '-' || *p == '+') |
| 13113 | *p = '_'; |
| 13114 | |
| 13115 | /* Register the function. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13116 | lua_pushstring(L, trash.area); |
| 13117 | lua_pushlightuserdata(L, sf); |
| 13118 | lua_pushcclosure(L, hlua_run_sample_fetch, 1); |
| 13119 | lua_rawset(L, -3); |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 13120 | } |
| 13121 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13122 | lua_rawset(L, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 13123 | |
| 13124 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13125 | class_fetches_ref = hlua_register_metatable(L, CLASS_FETCHES); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 13126 | |
| 13127 | /* |
| 13128 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 13129 | * Register class Converters |
| 13130 | * |
| 13131 | */ |
| 13132 | |
| 13133 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13134 | lua_newtable(L); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 13135 | |
| 13136 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13137 | lua_pushstring(L, "__index"); |
| 13138 | lua_newtable(L); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 13139 | |
| 13140 | /* Browse existing converters and create the associated |
| 13141 | * object method. |
| 13142 | */ |
| 13143 | sc = NULL; |
| 13144 | while ((sc = sample_conv_getnext(sc, &idx)) != NULL) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 13145 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 13146 | * by an underscore. |
| 13147 | */ |
Willy Tarreau | 5ef9656 | 2021-08-26 16:48:53 +0200 | [diff] [blame] | 13148 | strlcpy2(trash.area, sc->kw, trash.size); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 13149 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 13150 | if (*p == '.' || *p == '-' || *p == '+') |
| 13151 | *p = '_'; |
| 13152 | |
| 13153 | /* Register the function. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13154 | lua_pushstring(L, trash.area); |
| 13155 | lua_pushlightuserdata(L, sc); |
| 13156 | lua_pushcclosure(L, hlua_run_sample_conv, 1); |
| 13157 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 13158 | } |
| 13159 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13160 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 13161 | |
| 13162 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13163 | class_converters_ref = hlua_register_metatable(L, CLASS_CONVERTERS); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 13164 | |
| 13165 | /* |
| 13166 | * |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 13167 | * Register class HTTP |
| 13168 | * |
| 13169 | */ |
| 13170 | |
| 13171 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13172 | lua_newtable(L); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 13173 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 13174 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13175 | lua_pushstring(L, "__index"); |
| 13176 | lua_newtable(L); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 13177 | |
| 13178 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13179 | hlua_class_function(L, "req_get_headers",hlua_http_req_get_headers); |
| 13180 | hlua_class_function(L, "req_del_header", hlua_http_req_del_hdr); |
| 13181 | hlua_class_function(L, "req_rep_header", hlua_http_req_rep_hdr); |
| 13182 | hlua_class_function(L, "req_rep_value", hlua_http_req_rep_val); |
| 13183 | hlua_class_function(L, "req_add_header", hlua_http_req_add_hdr); |
| 13184 | hlua_class_function(L, "req_set_header", hlua_http_req_set_hdr); |
| 13185 | hlua_class_function(L, "req_set_method", hlua_http_req_set_meth); |
| 13186 | hlua_class_function(L, "req_set_path", hlua_http_req_set_path); |
| 13187 | hlua_class_function(L, "req_set_query", hlua_http_req_set_query); |
| 13188 | hlua_class_function(L, "req_set_uri", hlua_http_req_set_uri); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 13189 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13190 | hlua_class_function(L, "res_get_headers",hlua_http_res_get_headers); |
| 13191 | hlua_class_function(L, "res_del_header", hlua_http_res_del_hdr); |
| 13192 | hlua_class_function(L, "res_rep_header", hlua_http_res_rep_hdr); |
| 13193 | hlua_class_function(L, "res_rep_value", hlua_http_res_rep_val); |
| 13194 | hlua_class_function(L, "res_add_header", hlua_http_res_add_hdr); |
| 13195 | hlua_class_function(L, "res_set_header", hlua_http_res_set_hdr); |
| 13196 | hlua_class_function(L, "res_set_status", hlua_http_res_set_status); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 13197 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13198 | lua_rawset(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 13199 | |
| 13200 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13201 | class_http_ref = hlua_register_metatable(L, CLASS_HTTP); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 13202 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 13203 | /* |
| 13204 | * |
| 13205 | * Register class HTTPMessage |
| 13206 | * |
| 13207 | */ |
| 13208 | |
| 13209 | /* Create and fill the metatable. */ |
| 13210 | lua_newtable(L); |
| 13211 | |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 13212 | /* Create and fill the __index entry. */ |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 13213 | lua_pushstring(L, "__index"); |
| 13214 | lua_newtable(L); |
| 13215 | |
| 13216 | /* Register Lua functions. */ |
| 13217 | hlua_class_function(L, "is_resp", hlua_http_msg_is_resp); |
| 13218 | hlua_class_function(L, "get_stline", hlua_http_msg_get_stline); |
| 13219 | hlua_class_function(L, "get_headers", hlua_http_msg_get_headers); |
| 13220 | hlua_class_function(L, "del_header", hlua_http_msg_del_hdr); |
| 13221 | hlua_class_function(L, "rep_header", hlua_http_msg_rep_hdr); |
| 13222 | hlua_class_function(L, "rep_value", hlua_http_msg_rep_val); |
| 13223 | hlua_class_function(L, "add_header", hlua_http_msg_add_hdr); |
| 13224 | hlua_class_function(L, "set_header", hlua_http_msg_set_hdr); |
| 13225 | hlua_class_function(L, "set_method", hlua_http_msg_set_meth); |
| 13226 | hlua_class_function(L, "set_path", hlua_http_msg_set_path); |
| 13227 | hlua_class_function(L, "set_query", hlua_http_msg_set_query); |
| 13228 | hlua_class_function(L, "set_uri", hlua_http_msg_set_uri); |
| 13229 | hlua_class_function(L, "set_status", hlua_http_msg_set_status); |
| 13230 | hlua_class_function(L, "is_full", hlua_http_msg_is_full); |
| 13231 | hlua_class_function(L, "may_recv", hlua_http_msg_may_recv); |
| 13232 | hlua_class_function(L, "eom", hlua_http_msg_is_eom); |
| 13233 | hlua_class_function(L, "input", hlua_http_msg_get_in_len); |
| 13234 | hlua_class_function(L, "output", hlua_http_msg_get_out_len); |
| 13235 | |
| 13236 | hlua_class_function(L, "body", hlua_http_msg_get_body); |
| 13237 | hlua_class_function(L, "set", hlua_http_msg_set_data); |
| 13238 | hlua_class_function(L, "remove", hlua_http_msg_del_data); |
| 13239 | hlua_class_function(L, "append", hlua_http_msg_append); |
| 13240 | hlua_class_function(L, "prepend", hlua_http_msg_prepend); |
| 13241 | hlua_class_function(L, "insert", hlua_http_msg_insert_data); |
| 13242 | hlua_class_function(L, "set_eom", hlua_http_msg_set_eom); |
| 13243 | hlua_class_function(L, "unset_eom", hlua_http_msg_unset_eom); |
| 13244 | |
| 13245 | hlua_class_function(L, "send", hlua_http_msg_send); |
| 13246 | hlua_class_function(L, "forward", hlua_http_msg_forward); |
| 13247 | |
| 13248 | lua_rawset(L, -3); |
| 13249 | |
| 13250 | /* Register previous table in the registry with reference and named entry. */ |
| 13251 | class_http_msg_ref = hlua_register_metatable(L, CLASS_HTTP_MSG); |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 13252 | |
| 13253 | /* |
| 13254 | * |
| 13255 | * Register class HTTPClient |
| 13256 | * |
| 13257 | */ |
| 13258 | |
| 13259 | /* Create and fill the metatable. */ |
| 13260 | lua_newtable(L); |
| 13261 | lua_pushstring(L, "__index"); |
| 13262 | lua_newtable(L); |
| 13263 | hlua_class_function(L, "get", hlua_httpclient_get); |
William Lallemand | dc2cc90 | 2021-10-26 11:43:26 +0200 | [diff] [blame] | 13264 | hlua_class_function(L, "head", hlua_httpclient_head); |
| 13265 | hlua_class_function(L, "put", hlua_httpclient_put); |
| 13266 | hlua_class_function(L, "post", hlua_httpclient_post); |
| 13267 | hlua_class_function(L, "delete", hlua_httpclient_delete); |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 13268 | lua_settable(L, -3); /* Sets the __index entry. */ |
William Lallemand | f77f1de | 2021-09-28 19:10:38 +0200 | [diff] [blame] | 13269 | /* Register the garbage collector entry. */ |
| 13270 | lua_pushstring(L, "__gc"); |
| 13271 | lua_pushcclosure(L, hlua_httpclient_gc, 0); |
| 13272 | lua_settable(L, -3); /* Push the last 2 entries in the table at index -3 */ |
| 13273 | |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 13274 | |
| 13275 | |
| 13276 | class_httpclient_ref = hlua_register_metatable(L, CLASS_HTTPCLIENT); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 13277 | /* |
| 13278 | * |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 13279 | * Register class AppletTCP |
| 13280 | * |
| 13281 | */ |
| 13282 | |
| 13283 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13284 | lua_newtable(L); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 13285 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 13286 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13287 | lua_pushstring(L, "__index"); |
| 13288 | lua_newtable(L); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 13289 | |
| 13290 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13291 | hlua_class_function(L, "getline", hlua_applet_tcp_getline); |
| 13292 | hlua_class_function(L, "receive", hlua_applet_tcp_recv); |
| 13293 | hlua_class_function(L, "send", hlua_applet_tcp_send); |
| 13294 | hlua_class_function(L, "set_priv", hlua_applet_tcp_set_priv); |
| 13295 | hlua_class_function(L, "get_priv", hlua_applet_tcp_get_priv); |
| 13296 | hlua_class_function(L, "set_var", hlua_applet_tcp_set_var); |
| 13297 | hlua_class_function(L, "unset_var", hlua_applet_tcp_unset_var); |
| 13298 | hlua_class_function(L, "get_var", hlua_applet_tcp_get_var); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 13299 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13300 | lua_settable(L, -3); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 13301 | |
| 13302 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13303 | class_applet_tcp_ref = hlua_register_metatable(L, CLASS_APPLET_TCP); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 13304 | |
| 13305 | /* |
| 13306 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 13307 | * Register class AppletHTTP |
| 13308 | * |
| 13309 | */ |
| 13310 | |
| 13311 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13312 | lua_newtable(L); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 13313 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 13314 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13315 | lua_pushstring(L, "__index"); |
| 13316 | lua_newtable(L); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 13317 | |
| 13318 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13319 | hlua_class_function(L, "set_priv", hlua_applet_http_set_priv); |
| 13320 | hlua_class_function(L, "get_priv", hlua_applet_http_get_priv); |
| 13321 | hlua_class_function(L, "set_var", hlua_applet_http_set_var); |
| 13322 | hlua_class_function(L, "unset_var", hlua_applet_http_unset_var); |
| 13323 | hlua_class_function(L, "get_var", hlua_applet_http_get_var); |
| 13324 | hlua_class_function(L, "getline", hlua_applet_http_getline); |
| 13325 | hlua_class_function(L, "receive", hlua_applet_http_recv); |
| 13326 | hlua_class_function(L, "send", hlua_applet_http_send); |
| 13327 | hlua_class_function(L, "add_header", hlua_applet_http_addheader); |
| 13328 | hlua_class_function(L, "set_status", hlua_applet_http_status); |
| 13329 | hlua_class_function(L, "start_response", hlua_applet_http_start_response); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 13330 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13331 | lua_settable(L, -3); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 13332 | |
| 13333 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13334 | class_applet_http_ref = hlua_register_metatable(L, CLASS_APPLET_HTTP); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 13335 | |
| 13336 | /* |
| 13337 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 13338 | * Register class TXN |
| 13339 | * |
| 13340 | */ |
| 13341 | |
| 13342 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13343 | lua_newtable(L); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 13344 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 13345 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13346 | lua_pushstring(L, "__index"); |
| 13347 | lua_newtable(L); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 13348 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 13349 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13350 | hlua_class_function(L, "set_priv", hlua_set_priv); |
| 13351 | hlua_class_function(L, "get_priv", hlua_get_priv); |
| 13352 | hlua_class_function(L, "set_var", hlua_set_var); |
| 13353 | hlua_class_function(L, "unset_var", hlua_unset_var); |
| 13354 | hlua_class_function(L, "get_var", hlua_get_var); |
| 13355 | hlua_class_function(L, "done", hlua_txn_done); |
| 13356 | hlua_class_function(L, "reply", hlua_txn_reply_new); |
| 13357 | hlua_class_function(L, "set_loglevel", hlua_txn_set_loglevel); |
| 13358 | hlua_class_function(L, "set_tos", hlua_txn_set_tos); |
| 13359 | hlua_class_function(L, "set_mark", hlua_txn_set_mark); |
| 13360 | hlua_class_function(L, "set_priority_class", hlua_txn_set_priority_class); |
| 13361 | hlua_class_function(L, "set_priority_offset", hlua_txn_set_priority_offset); |
| 13362 | hlua_class_function(L, "deflog", hlua_txn_deflog); |
| 13363 | hlua_class_function(L, "log", hlua_txn_log); |
| 13364 | hlua_class_function(L, "Debug", hlua_txn_log_debug); |
| 13365 | hlua_class_function(L, "Info", hlua_txn_log_info); |
| 13366 | hlua_class_function(L, "Warning", hlua_txn_log_warning); |
| 13367 | hlua_class_function(L, "Alert", hlua_txn_log_alert); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 13368 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13369 | lua_rawset(L, -3); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 13370 | |
| 13371 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13372 | class_txn_ref = hlua_register_metatable(L, CLASS_TXN); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 13373 | |
| 13374 | /* |
| 13375 | * |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 13376 | * Register class reply |
| 13377 | * |
| 13378 | */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13379 | lua_newtable(L); |
| 13380 | lua_pushstring(L, "__index"); |
| 13381 | lua_newtable(L); |
| 13382 | hlua_class_function(L, "set_status", hlua_txn_reply_set_status); |
| 13383 | hlua_class_function(L, "add_header", hlua_txn_reply_add_header); |
| 13384 | hlua_class_function(L, "del_header", hlua_txn_reply_del_header); |
| 13385 | hlua_class_function(L, "set_body", hlua_txn_reply_set_body); |
| 13386 | lua_settable(L, -3); /* Sets the __index entry. */ |
| 13387 | class_txn_reply_ref = luaL_ref(L, LUA_REGISTRYINDEX); |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 13388 | |
| 13389 | |
| 13390 | /* |
| 13391 | * |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 13392 | * Register class Socket |
| 13393 | * |
| 13394 | */ |
| 13395 | |
| 13396 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13397 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 13398 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 13399 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13400 | lua_pushstring(L, "__index"); |
| 13401 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 13402 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 13403 | #ifdef USE_OPENSSL |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13404 | hlua_class_function(L, "connect_ssl", hlua_socket_connect_ssl); |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 13405 | #endif |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13406 | hlua_class_function(L, "connect", hlua_socket_connect); |
| 13407 | hlua_class_function(L, "send", hlua_socket_send); |
| 13408 | hlua_class_function(L, "receive", hlua_socket_receive); |
| 13409 | hlua_class_function(L, "close", hlua_socket_close); |
| 13410 | hlua_class_function(L, "getpeername", hlua_socket_getpeername); |
| 13411 | hlua_class_function(L, "getsockname", hlua_socket_getsockname); |
| 13412 | hlua_class_function(L, "setoption", hlua_socket_setoption); |
| 13413 | hlua_class_function(L, "settimeout", hlua_socket_settimeout); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 13414 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13415 | 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] | 13416 | |
| 13417 | /* Register the garbage collector entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13418 | lua_pushstring(L, "__gc"); |
| 13419 | lua_pushcclosure(L, hlua_socket_gc, 0); |
| 13420 | 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] | 13421 | |
| 13422 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13423 | class_socket_ref = hlua_register_metatable(L, CLASS_SOCKET); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 13424 | |
Thierry Fournier | aafc777 | 2020-12-04 11:47:47 +0100 | [diff] [blame] | 13425 | lua_atpanic(L, hlua_panic_safe); |
| 13426 | |
| 13427 | return L; |
| 13428 | } |
| 13429 | |
| 13430 | void hlua_init(void) { |
| 13431 | int i; |
Amaury Denoyelle | 239fdbf | 2021-03-24 10:22:03 +0100 | [diff] [blame] | 13432 | char *errmsg; |
Thierry Fournier | aafc777 | 2020-12-04 11:47:47 +0100 | [diff] [blame] | 13433 | #ifdef USE_OPENSSL |
| 13434 | struct srv_kw *kw; |
| 13435 | int tmp_error; |
| 13436 | char *error; |
| 13437 | char *args[] = { /* SSL client configuration. */ |
| 13438 | "ssl", |
| 13439 | "verify", |
| 13440 | "none", |
| 13441 | NULL |
| 13442 | }; |
| 13443 | #endif |
| 13444 | |
| 13445 | /* Init post init function list head */ |
| 13446 | for (i = 0; i < MAX_THREADS + 1; i++) |
| 13447 | LIST_INIT(&hlua_init_functions[i]); |
| 13448 | |
| 13449 | /* Init state for common/shared lua parts */ |
| 13450 | hlua_state_id = 0; |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 13451 | ha_set_thread(NULL); |
Thierry Fournier | aafc777 | 2020-12-04 11:47:47 +0100 | [diff] [blame] | 13452 | hlua_states[0] = hlua_init_state(0); |
| 13453 | |
| 13454 | /* Init state 1 for thread 0. We have at least one thread. */ |
| 13455 | hlua_state_id = 1; |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 13456 | ha_set_thread(NULL); |
Thierry Fournier | aafc777 | 2020-12-04 11:47:47 +0100 | [diff] [blame] | 13457 | hlua_states[1] = hlua_init_state(1); |
| 13458 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 13459 | /* Proxy and server configuration initialisation. */ |
William Lallemand | 6bb77b9 | 2021-07-28 15:48:16 +0200 | [diff] [blame] | 13460 | socket_proxy = alloc_new_proxy("LUA-SOCKET", PR_CAP_FE|PR_CAP_BE|PR_CAP_INT, &errmsg); |
Amaury Denoyelle | 239fdbf | 2021-03-24 10:22:03 +0100 | [diff] [blame] | 13461 | if (!socket_proxy) { |
| 13462 | fprintf(stderr, "Lua init: %s\n", errmsg); |
| 13463 | exit(1); |
| 13464 | } |
| 13465 | proxy_preset_defaults(socket_proxy); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 13466 | |
| 13467 | /* Init TCP server: unchanged parameters */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 13468 | socket_tcp = new_server(socket_proxy); |
| 13469 | if (!socket_tcp) { |
| 13470 | fprintf(stderr, "Lua init: failed to allocate tcp server socket\n"); |
| 13471 | exit(1); |
| 13472 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 13473 | |
| 13474 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 13475 | /* Init TCP server: unchanged parameters */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 13476 | socket_ssl = new_server(socket_proxy); |
| 13477 | if (!socket_ssl) { |
| 13478 | fprintf(stderr, "Lua init: failed to allocate ssl server socket\n"); |
| 13479 | exit(1); |
| 13480 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 13481 | |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 13482 | socket_ssl->use_ssl = 1; |
| 13483 | socket_ssl->xprt = xprt_get(XPRT_SSL); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 13484 | |
Bertrand Jacquin | 80839ff | 2021-01-21 19:14:46 +0000 | [diff] [blame] | 13485 | for (i = 0; args[i] != NULL; i++) { |
| 13486 | 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] | 13487 | /* |
| 13488 | * |
| 13489 | * If the keyword is not known, we can search in the registered |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 13490 | * server keywords. This is useful to configure special SSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 13491 | * features like client certificates and ssl_verify. |
| 13492 | * |
| 13493 | */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 13494 | tmp_error = kw->parse(args, &i, socket_proxy, socket_ssl, &error); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 13495 | if (tmp_error != 0) { |
| 13496 | fprintf(stderr, "INTERNAL ERROR: %s\n", error); |
| 13497 | abort(); /* This must be never arrives because the command line |
| 13498 | not editable by the user. */ |
| 13499 | } |
Bertrand Jacquin | 80839ff | 2021-01-21 19:14:46 +0000 | [diff] [blame] | 13500 | i += kw->skip; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 13501 | } |
| 13502 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 13503 | #endif |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 13504 | |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 13505 | } |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 13506 | |
Tim Duesterhus | d0c0ca2 | 2020-07-04 11:53:26 +0200 | [diff] [blame] | 13507 | static void hlua_deinit() |
| 13508 | { |
Willy Tarreau | 186f376 | 2020-12-04 11:48:12 +0100 | [diff] [blame] | 13509 | int thr; |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 13510 | struct hlua_reg_filter *reg_flt, *reg_flt_bck; |
| 13511 | |
| 13512 | list_for_each_entry_safe(reg_flt, reg_flt_bck, &referenced_filters, l) |
| 13513 | release_hlua_reg_filter(reg_flt); |
Willy Tarreau | 186f376 | 2020-12-04 11:48:12 +0100 | [diff] [blame] | 13514 | |
| 13515 | for (thr = 0; thr < MAX_THREADS+1; thr++) { |
| 13516 | if (hlua_states[thr]) |
| 13517 | lua_close(hlua_states[thr]); |
| 13518 | } |
Willy Tarreau | 430bf4a | 2021-03-04 09:45:32 +0100 | [diff] [blame] | 13519 | |
Amaury Denoyelle | bc2ebfa | 2021-08-25 15:34:53 +0200 | [diff] [blame] | 13520 | srv_drop(socket_tcp); |
Willy Tarreau | 430bf4a | 2021-03-04 09:45:32 +0100 | [diff] [blame] | 13521 | |
Willy Tarreau | 0f143af | 2021-03-05 10:41:48 +0100 | [diff] [blame] | 13522 | #ifdef USE_OPENSSL |
Amaury Denoyelle | bc2ebfa | 2021-08-25 15:34:53 +0200 | [diff] [blame] | 13523 | srv_drop(socket_ssl); |
Willy Tarreau | 0f143af | 2021-03-05 10:41:48 +0100 | [diff] [blame] | 13524 | #endif |
Amaury Denoyelle | 239fdbf | 2021-03-24 10:22:03 +0100 | [diff] [blame] | 13525 | |
| 13526 | free_proxy(socket_proxy); |
Tim Duesterhus | d0c0ca2 | 2020-07-04 11:53:26 +0200 | [diff] [blame] | 13527 | } |
| 13528 | |
| 13529 | REGISTER_POST_DEINIT(hlua_deinit); |
| 13530 | |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 13531 | static void hlua_register_build_options(void) |
| 13532 | { |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 13533 | char *ptr = NULL; |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 13534 | |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 13535 | memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE); |
| 13536 | hap_register_build_opts(ptr, 1); |
| 13537 | } |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 13538 | |
| 13539 | INITCALL0(STG_REGISTER, hlua_register_build_options); |