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 |
| 1372 | */ |
| 1373 | __LJMP void hlua_yieldk(lua_State *L, int nresults, int ctx, |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1374 | lua_KFunction k, int timeout, unsigned int flags) |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1375 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 1376 | struct hlua *hlua; |
| 1377 | |
| 1378 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 1379 | hlua = hlua_gethlua(L); |
| 1380 | if (!hlua) { |
| 1381 | return; |
| 1382 | } |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1383 | |
| 1384 | /* Set the wake timeout. If timeout is required, we set |
| 1385 | * the expiration time. |
| 1386 | */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1387 | hlua->wake_time = timeout; |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1388 | |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 1389 | hlua->flags |= flags; |
| 1390 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1391 | /* Process the yield. */ |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 1392 | MAY_LJMP(lua_yieldk(L, nresults, ctx, k)); |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1393 | } |
| 1394 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1395 | /* This function initialises the Lua environment stored in the stream. |
| 1396 | * 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] | 1397 | * 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] | 1398 | * |
| 1399 | * This function is particular. it initialises a new Lua thread. If the |
| 1400 | * initialisation fails (example: out of memory error), the lua function |
| 1401 | * throws an error (longjmp). |
| 1402 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1403 | * This function manipulates two Lua stacks: the main and the thread. Only |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 1404 | * the main stack can fail. The thread is not manipulated. This function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1405 | * MUST NOT manipulate the created thread stack state, because it is not |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1406 | * protected against errors thrown by the thread stack. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1407 | */ |
Aurelien DARRAGON | 6b0b9bd | 2023-03-21 14:02:16 +0100 | [diff] [blame] | 1408 | 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] | 1409 | { |
| 1410 | lua->Mref = LUA_REFNIL; |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1411 | lua->flags = 0; |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 1412 | lua->gc_count = 0; |
Christopher Faulet | bc275a9 | 2020-02-26 14:55:16 +0100 | [diff] [blame] | 1413 | lua->wake_time = TICK_ETERNITY; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1414 | lua->state_id = state_id; |
Aurelien DARRAGON | da9503c | 2022-11-25 09:10:07 +0100 | [diff] [blame] | 1415 | hlua_timer_init(&lua->timer, 0); /* default value, no timeout */ |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 1416 | LIST_INIT(&lua->com); |
Aurelien DARRAGON | 3ffbf38 | 2023-02-09 17:02:57 +0100 | [diff] [blame] | 1417 | MT_LIST_INIT(&lua->hc_list); |
Aurelien DARRAGON | 6b0b9bd | 2023-03-21 14:02:16 +0100 | [diff] [blame] | 1418 | if (!SET_SAFE_LJMP_PARENT(lua)) { |
| 1419 | lua->Tref = LUA_REFNIL; |
| 1420 | return 0; |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1421 | } |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1422 | lua->T = lua_newthread(hlua_states[state_id]); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1423 | if (!lua->T) { |
| 1424 | lua->Tref = LUA_REFNIL; |
Aurelien DARRAGON | 6b0b9bd | 2023-03-21 14:02:16 +0100 | [diff] [blame] | 1425 | RESET_SAFE_LJMP_PARENT(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1426 | return 0; |
| 1427 | } |
| 1428 | hlua_sethlua(lua); |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1429 | lua->Tref = luaL_ref(hlua_states[state_id], LUA_REGISTRYINDEX); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1430 | lua->task = task; |
Aurelien DARRAGON | 6b0b9bd | 2023-03-21 14:02:16 +0100 | [diff] [blame] | 1431 | RESET_SAFE_LJMP_PARENT(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1432 | return 1; |
| 1433 | } |
| 1434 | |
Aurelien DARRAGON | 3ffbf38 | 2023-02-09 17:02:57 +0100 | [diff] [blame] | 1435 | /* kill all associated httpclient to this hlua task |
| 1436 | * We must take extra precautions as we're manipulating lua-exposed |
| 1437 | * objects without the main lua lock. |
| 1438 | */ |
William Lallemand | bb58142 | 2022-10-20 10:57:28 +0200 | [diff] [blame] | 1439 | static void hlua_httpclient_destroy_all(struct hlua *hlua) |
| 1440 | { |
Aurelien DARRAGON | 3ffbf38 | 2023-02-09 17:02:57 +0100 | [diff] [blame] | 1441 | struct hlua_httpclient *hlua_hc; |
William Lallemand | bb58142 | 2022-10-20 10:57:28 +0200 | [diff] [blame] | 1442 | |
Aurelien DARRAGON | 3ffbf38 | 2023-02-09 17:02:57 +0100 | [diff] [blame] | 1443 | /* use thread-safe accessors for hc_list since GC cycle initiated by |
| 1444 | * another thread sharing the same main lua stack (lua coroutine) |
| 1445 | * could execute hlua_httpclient_gc() on the hlua->hc_list items |
| 1446 | * in parallel: Lua GC applies on the main stack, it is not limited to |
| 1447 | * a single coroutine stack, see Github issue #2037 for reference. |
| 1448 | * Remember, coroutines created using lua_newthread() are not meant to |
| 1449 | * be thread safe in Lua. (From lua co-author: |
| 1450 | * http://lua-users.org/lists/lua-l/2011-07/msg00072.html) |
| 1451 | * |
| 1452 | * This security measure is superfluous when 'lua-load-per-thread' is used |
| 1453 | * since in this case coroutines exclusively run on the same thread |
| 1454 | * (main stack is not shared between OS threads). |
| 1455 | */ |
| 1456 | while ((hlua_hc = MT_LIST_POP(&hlua->hc_list, typeof(hlua_hc), by_hlua))) { |
| 1457 | httpclient_stop_and_destroy(hlua_hc->hc); |
William Lallemand | bb58142 | 2022-10-20 10:57:28 +0200 | [diff] [blame] | 1458 | hlua_hc->hc = NULL; |
William Lallemand | bb58142 | 2022-10-20 10:57:28 +0200 | [diff] [blame] | 1459 | } |
| 1460 | } |
| 1461 | |
| 1462 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1463 | /* Used to destroy the Lua coroutine when the attached stream or task |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1464 | * is destroyed. The destroy also the memory context. The struct "lua" |
Aurelien DARRAGON | 60ab0f7 | 2023-03-01 16:45:50 +0100 | [diff] [blame] | 1465 | * will be freed. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1466 | */ |
| 1467 | void hlua_ctx_destroy(struct hlua *lua) |
| 1468 | { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 1469 | if (!lua) |
Thierry FOURNIER | a718b29 | 2015-03-04 16:48:34 +0100 | [diff] [blame] | 1470 | return; |
| 1471 | |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 1472 | if (!lua->T) |
| 1473 | goto end; |
| 1474 | |
William Lallemand | bb58142 | 2022-10-20 10:57:28 +0200 | [diff] [blame] | 1475 | /* clean all running httpclient */ |
| 1476 | hlua_httpclient_destroy_all(lua); |
| 1477 | |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 1478 | /* Purge all the pending signals. */ |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1479 | notification_purge(&lua->com); |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 1480 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1481 | if (!SET_SAFE_LJMP(lua)) |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 1482 | return; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1483 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1484 | RESET_SAFE_LJMP(lua); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 1485 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1486 | if (!SET_SAFE_LJMP_PARENT(lua)) |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 1487 | return; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1488 | luaL_unref(hlua_states[lua->state_id], LUA_REGISTRYINDEX, lua->Tref); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1489 | RESET_SAFE_LJMP_PARENT(lua); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 1490 | /* Forces a garbage collecting process. If the Lua program is finished |
| 1491 | * without error, we run the GC on the thread pointer. Its freed all |
| 1492 | * the unused memory. |
| 1493 | * If the thread is finnish with an error or is currently yielded, |
| 1494 | * it seems that the GC applied on the thread doesn't clean anything, |
| 1495 | * so e run the GC on the main thread. |
| 1496 | * NOTE: maybe this action locks all the Lua threads untiml the en of |
| 1497 | * the garbage collection. |
| 1498 | */ |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 1499 | if (lua->gc_count) { |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1500 | if (!SET_SAFE_LJMP_PARENT(lua)) |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 1501 | return; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1502 | lua_gc(hlua_states[lua->state_id], LUA_GCCOLLECT, 0); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1503 | RESET_SAFE_LJMP_PARENT(lua); |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 1504 | } |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 1505 | |
Thierry FOURNIER | a7b536b | 2015-09-21 22:50:24 +0200 | [diff] [blame] | 1506 | lua->T = NULL; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 1507 | |
| 1508 | end: |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 1509 | pool_free(pool_head_hlua, lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1510 | } |
| 1511 | |
| 1512 | /* This function is used to restore the Lua context when a coroutine |
| 1513 | * fails. This function copy the common memory between old coroutine |
| 1514 | * and the new coroutine. The old coroutine is destroyed, and its |
| 1515 | * replaced by the new coroutine. |
| 1516 | * If the flag "keep_msg" is set, the last entry of the old is assumed |
| 1517 | * as string error message and it is copied in the new stack. |
| 1518 | */ |
| 1519 | static int hlua_ctx_renew(struct hlua *lua, int keep_msg) |
| 1520 | { |
| 1521 | lua_State *T; |
| 1522 | int new_ref; |
| 1523 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1524 | /* New Lua coroutine. */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1525 | T = lua_newthread(hlua_states[lua->state_id]); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1526 | if (!T) |
| 1527 | return 0; |
| 1528 | |
| 1529 | /* Copy last error message. */ |
| 1530 | if (keep_msg) |
| 1531 | lua_xmove(lua->T, T, 1); |
| 1532 | |
| 1533 | /* Copy data between the coroutines. */ |
| 1534 | lua_rawgeti(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 1535 | lua_xmove(lua->T, T, 1); |
Ilya Shipitsin | 46a030c | 2020-07-05 16:36:08 +0500 | [diff] [blame] | 1536 | new_ref = luaL_ref(T, LUA_REGISTRYINDEX); /* Value popped. */ |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1537 | |
| 1538 | /* Destroy old data. */ |
| 1539 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 1540 | |
| 1541 | /* The thread is garbage collected by Lua. */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1542 | luaL_unref(hlua_states[lua->state_id], LUA_REGISTRYINDEX, lua->Tref); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1543 | |
| 1544 | /* Fill the struct with the new coroutine values. */ |
| 1545 | lua->Mref = new_ref; |
| 1546 | lua->T = T; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1547 | lua->Tref = luaL_ref(hlua_states[lua->state_id], LUA_REGISTRYINDEX); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1548 | |
| 1549 | /* Set context. */ |
| 1550 | hlua_sethlua(lua); |
| 1551 | |
| 1552 | return 1; |
| 1553 | } |
| 1554 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1555 | void hlua_hook(lua_State *L, lua_Debug *ar) |
| 1556 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 1557 | struct hlua *hlua; |
| 1558 | |
| 1559 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 1560 | hlua = hlua_gethlua(L); |
| 1561 | if (!hlua) |
| 1562 | return; |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1563 | |
| 1564 | /* Lua cannot yield when its returning from a function, |
| 1565 | * so, we can fix the interrupt hook to 1 instruction, |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1566 | * expecting that the function is finished. |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1567 | */ |
| 1568 | if (lua_gethookmask(L) & LUA_MASKRET) { |
| 1569 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, 1); |
| 1570 | return; |
| 1571 | } |
| 1572 | |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1573 | /* If we interrupt the Lua processing in yieldable state, we yield. |
| 1574 | * If the state is not yieldable, trying yield causes an error. |
| 1575 | */ |
Aurelien DARRAGON | 0ebd41f | 2022-11-24 09:51:40 +0100 | [diff] [blame] | 1576 | if (lua_isyieldable(L)) { |
| 1577 | /* note: for converters/fetches.. where yielding is not allowed |
| 1578 | * hlua_ctx_resume() will simply perform a goto resume_execution |
| 1579 | * instead of rescheduling hlua->task. |
| 1580 | * also: hlua_ctx_resume() will take care of checking execution |
| 1581 | * timeout and re-applying the hook as needed. |
| 1582 | */ |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 1583 | MAY_LJMP(hlua_yieldk(L, 0, 0, NULL, TICK_ETERNITY, HLUA_CTRLYIELD)); |
Aurelien DARRAGON | 0ebd41f | 2022-11-24 09:51:40 +0100 | [diff] [blame] | 1584 | /* lua docs says that the hook should return immediately after lua_yieldk |
| 1585 | * |
| 1586 | * From: https://www.lua.org/manual/5.3/manual.html#lua_yieldk |
| 1587 | * |
| 1588 | * Moreover, it seems that we don't want to continue after the yield |
| 1589 | * because the end of the function is about handling unyieldable function, |
| 1590 | * which is not the case here. |
| 1591 | * |
| 1592 | * ->if we don't return lua_sethook gets incorrectly set with MASKRET later |
| 1593 | * in the function. |
| 1594 | */ |
| 1595 | return; |
| 1596 | } |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1597 | |
Aurelien DARRAGON | da9503c | 2022-11-25 09:10:07 +0100 | [diff] [blame] | 1598 | /* If we cannot yield, check the timeout. */ |
| 1599 | if (!hlua_timer_check(&hlua->timer)) { |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1600 | lua_pushfstring(L, "execution timeout"); |
| 1601 | WILL_LJMP(lua_error(L)); |
| 1602 | } |
| 1603 | |
| 1604 | /* Try to interrupt the process at the end of the current |
| 1605 | * unyieldable function. |
| 1606 | */ |
| 1607 | 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] | 1608 | } |
| 1609 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1610 | /* This function start or resumes the Lua stack execution. If the flag |
| 1611 | * "yield_allowed" if no set and the LUA stack execution returns a yield |
| 1612 | * The function return an error. |
| 1613 | * |
| 1614 | * The function can returns 4 values: |
| 1615 | * - HLUA_E_OK : The execution is terminated without any errors. |
| 1616 | * - HLUA_E_AGAIN : The execution must continue at the next associated |
| 1617 | * task wakeup. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1618 | * - 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] | 1619 | * the top of the stack. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1620 | * - HLUA_E_ERR : An error has occurred without error message. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1621 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1622 | * 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] | 1623 | * LUA code. |
| 1624 | */ |
| 1625 | static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed) |
| 1626 | { |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1627 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
| 1628 | int nres; |
| 1629 | #endif |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1630 | int ret; |
| 1631 | const char *msg; |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1632 | const char *trace; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1633 | |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1634 | /* Lock the whole Lua execution. This lock must be before the |
| 1635 | * label "resume_execution". |
| 1636 | */ |
Aurelien DARRAGON | e36f803 | 2023-03-21 13:22:33 +0100 | [diff] [blame] | 1637 | hlua_lock(lua); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1638 | |
Aurelien DARRAGON | da9503c | 2022-11-25 09:10:07 +0100 | [diff] [blame] | 1639 | /* reset the timer as we might be re-entering the function to |
| 1640 | * resume the coroutine after a successful yield |
| 1641 | * (cumulative time will be updated) |
| 1642 | */ |
| 1643 | hlua_timer_reset(&lua->timer); |
| 1644 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1645 | resume_execution: |
| 1646 | |
| 1647 | /* This hook interrupts the Lua processing each 'hlua_nb_instruction' |
| 1648 | * instructions. it is used for preventing infinite loops. |
| 1649 | */ |
| 1650 | lua_sethook(lua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 1651 | |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1652 | /* Remove all flags except the running flags. */ |
Thierry FOURNIER | 2f3867f | 2015-09-28 01:02:01 +0200 | [diff] [blame] | 1653 | HLUA_SET_RUN(lua); |
| 1654 | HLUA_CLR_CTRLYIELD(lua); |
| 1655 | HLUA_CLR_WAKERESWR(lua); |
| 1656 | HLUA_CLR_WAKEREQWR(lua); |
Christopher Faulet | 1f43a34 | 2021-08-04 17:58:21 +0200 | [diff] [blame] | 1657 | HLUA_CLR_NOYIELD(lua); |
| 1658 | if (!yield_allowed) |
| 1659 | HLUA_SET_NOYIELD(lua); |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1660 | |
Aurelien DARRAGON | da9503c | 2022-11-25 09:10:07 +0100 | [diff] [blame] | 1661 | /* reset wake_time. */ |
Christopher Faulet | bc275a9 | 2020-02-26 14:55:16 +0100 | [diff] [blame] | 1662 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1663 | |
Aurelien DARRAGON | da9503c | 2022-11-25 09:10:07 +0100 | [diff] [blame] | 1664 | /* start the timer as we're about to start lua processing */ |
| 1665 | hlua_timer_start(&lua->timer); |
| 1666 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1667 | /* Call the function. */ |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1668 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1669 | 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] | 1670 | #else |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1671 | ret = lua_resume(lua->T, hlua_states[lua->state_id], lua->nargs); |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1672 | #endif |
Aurelien DARRAGON | da9503c | 2022-11-25 09:10:07 +0100 | [diff] [blame] | 1673 | |
| 1674 | /* out of lua processing, stop the timer */ |
| 1675 | hlua_timer_stop(&lua->timer); |
| 1676 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1677 | switch (ret) { |
| 1678 | |
| 1679 | case LUA_OK: |
| 1680 | ret = HLUA_E_OK; |
| 1681 | break; |
| 1682 | |
| 1683 | case LUA_YIELD: |
Aurelien DARRAGON | da9503c | 2022-11-25 09:10:07 +0100 | [diff] [blame] | 1684 | /* 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] | 1685 | * break the Lua execution. |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1686 | */ |
Aurelien DARRAGON | da9503c | 2022-11-25 09:10:07 +0100 | [diff] [blame] | 1687 | if (!hlua_timer_check(&lua->timer)) { |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1688 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1689 | ret = HLUA_E_ETMOUT; |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1690 | break; |
| 1691 | } |
| 1692 | /* Process the forced yield. if the general yield is not allowed or |
| 1693 | * if no task were associated this the current Lua execution |
| 1694 | * coroutine, we resume the execution. Else we want to return in the |
| 1695 | * scheduler and we want to be waked up again, to continue the |
| 1696 | * current Lua execution. So we schedule our own task. |
| 1697 | */ |
| 1698 | if (HLUA_IS_CTRLYIELDING(lua)) { |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1699 | if (!yield_allowed || !lua->task) |
| 1700 | goto resume_execution; |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1701 | task_wakeup(lua->task, TASK_WOKEN_MSG); |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1702 | } |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1703 | if (!yield_allowed) { |
| 1704 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1705 | ret = HLUA_E_YIELD; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1706 | break; |
| 1707 | } |
| 1708 | ret = HLUA_E_AGAIN; |
| 1709 | break; |
| 1710 | |
| 1711 | case LUA_ERRRUN: |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1712 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1713 | /* Special exit case. The traditional exit is returned as an error |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1714 | * because the errors ares the only one mean to return immediately |
| 1715 | * from and lua execution. |
| 1716 | */ |
| 1717 | if (lua->flags & HLUA_EXIT) { |
| 1718 | ret = HLUA_E_OK; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 1719 | hlua_ctx_renew(lua, 1); |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1720 | break; |
| 1721 | } |
| 1722 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1723 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1724 | if (!lua_checkstack(lua->T, 1)) { |
| 1725 | ret = HLUA_E_ERR; |
| 1726 | break; |
| 1727 | } |
| 1728 | msg = lua_tostring(lua->T, -1); |
| 1729 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1730 | lua_pop(lua->T, 1); |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 1731 | trace = hlua_traceback(lua->T, ", "); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1732 | if (msg) |
Thierry Fournier | 46278ff | 2020-11-29 11:48:12 +0100 | [diff] [blame] | 1733 | 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] | 1734 | else |
Thierry Fournier | 46278ff | 2020-11-29 11:48:12 +0100 | [diff] [blame] | 1735 | 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] | 1736 | ret = HLUA_E_ERRMSG; |
| 1737 | break; |
| 1738 | |
| 1739 | case LUA_ERRMEM: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1740 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1741 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1742 | ret = HLUA_E_NOMEM; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1743 | break; |
| 1744 | |
| 1745 | case LUA_ERRERR: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1746 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1747 | if (!lua_checkstack(lua->T, 1)) { |
| 1748 | ret = HLUA_E_ERR; |
| 1749 | break; |
| 1750 | } |
| 1751 | msg = lua_tostring(lua->T, -1); |
| 1752 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1753 | lua_pop(lua->T, 1); |
| 1754 | if (msg) |
Thierry Fournier | 46278ff | 2020-11-29 11:48:12 +0100 | [diff] [blame] | 1755 | 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] | 1756 | else |
Thierry Fournier | 46278ff | 2020-11-29 11:48:12 +0100 | [diff] [blame] | 1757 | 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] | 1758 | ret = HLUA_E_ERRMSG; |
| 1759 | break; |
| 1760 | |
| 1761 | default: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1762 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1763 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1764 | ret = HLUA_E_ERR; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1765 | break; |
| 1766 | } |
| 1767 | |
| 1768 | switch (ret) { |
| 1769 | case HLUA_E_AGAIN: |
| 1770 | break; |
| 1771 | |
| 1772 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1773 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1774 | hlua_ctx_renew(lua, 1); |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1775 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1776 | break; |
| 1777 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1778 | case HLUA_E_ETMOUT: |
| 1779 | case HLUA_E_NOMEM: |
| 1780 | case HLUA_E_YIELD: |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1781 | case HLUA_E_ERR: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1782 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1783 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1784 | hlua_ctx_renew(lua, 0); |
| 1785 | break; |
| 1786 | |
| 1787 | case HLUA_E_OK: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1788 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1789 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1790 | break; |
| 1791 | } |
| 1792 | |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1793 | /* This is the main exit point, remove the Lua lock. */ |
Aurelien DARRAGON | e36f803 | 2023-03-21 13:22:33 +0100 | [diff] [blame] | 1794 | hlua_unlock(lua); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1795 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1796 | return ret; |
| 1797 | } |
| 1798 | |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1799 | /* This function exit the current code. */ |
| 1800 | __LJMP static int hlua_done(lua_State *L) |
| 1801 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 1802 | struct hlua *hlua; |
| 1803 | |
| 1804 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 1805 | hlua = hlua_gethlua(L); |
| 1806 | if (!hlua) |
| 1807 | return 0; |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1808 | |
| 1809 | hlua->flags |= HLUA_EXIT; |
| 1810 | WILL_LJMP(lua_error(L)); |
| 1811 | |
| 1812 | return 0; |
| 1813 | } |
| 1814 | |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1815 | /* This function is an LUA binding. It provides a function |
| 1816 | * for deleting ACL from a referenced ACL file. |
| 1817 | */ |
| 1818 | __LJMP static int hlua_del_acl(lua_State *L) |
| 1819 | { |
| 1820 | const char *name; |
| 1821 | const char *key; |
| 1822 | struct pat_ref *ref; |
| 1823 | |
| 1824 | MAY_LJMP(check_args(L, 2, "del_acl")); |
| 1825 | |
| 1826 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1827 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1828 | |
| 1829 | ref = pat_ref_lookup(name); |
| 1830 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1831 | WILL_LJMP(luaL_error(L, "'del_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1832 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1833 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1834 | pat_ref_delete(ref, key); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1835 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1836 | return 0; |
| 1837 | } |
| 1838 | |
| 1839 | /* This function is an LUA binding. It provides a function |
| 1840 | * for deleting map entry from a referenced map file. |
| 1841 | */ |
| 1842 | static int hlua_del_map(lua_State *L) |
| 1843 | { |
| 1844 | const char *name; |
| 1845 | const char *key; |
| 1846 | struct pat_ref *ref; |
| 1847 | |
| 1848 | MAY_LJMP(check_args(L, 2, "del_map")); |
| 1849 | |
| 1850 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1851 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1852 | |
| 1853 | ref = pat_ref_lookup(name); |
| 1854 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1855 | WILL_LJMP(luaL_error(L, "'del_map': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1856 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1857 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1858 | pat_ref_delete(ref, key); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1859 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1860 | return 0; |
| 1861 | } |
| 1862 | |
| 1863 | /* This function is an LUA binding. It provides a function |
| 1864 | * for adding ACL pattern from a referenced ACL file. |
| 1865 | */ |
| 1866 | static int hlua_add_acl(lua_State *L) |
| 1867 | { |
| 1868 | const char *name; |
| 1869 | const char *key; |
| 1870 | struct pat_ref *ref; |
| 1871 | |
| 1872 | MAY_LJMP(check_args(L, 2, "add_acl")); |
| 1873 | |
| 1874 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1875 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1876 | |
| 1877 | ref = pat_ref_lookup(name); |
| 1878 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1879 | WILL_LJMP(luaL_error(L, "'add_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1880 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1881 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1882 | if (pat_ref_find_elt(ref, key) == NULL) |
| 1883 | pat_ref_add(ref, key, NULL, NULL); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1884 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1885 | return 0; |
| 1886 | } |
| 1887 | |
| 1888 | /* This function is an LUA binding. It provides a function |
| 1889 | * for setting map pattern and sample from a referenced map |
| 1890 | * file. |
| 1891 | */ |
| 1892 | static int hlua_set_map(lua_State *L) |
| 1893 | { |
| 1894 | const char *name; |
| 1895 | const char *key; |
| 1896 | const char *value; |
| 1897 | struct pat_ref *ref; |
| 1898 | |
| 1899 | MAY_LJMP(check_args(L, 3, "set_map")); |
| 1900 | |
| 1901 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1902 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1903 | value = MAY_LJMP(luaL_checkstring(L, 3)); |
| 1904 | |
| 1905 | ref = pat_ref_lookup(name); |
| 1906 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1907 | WILL_LJMP(luaL_error(L, "'set_map': unknown map file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1908 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1909 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1910 | if (pat_ref_find_elt(ref, key) != NULL) |
| 1911 | pat_ref_set(ref, key, value, NULL); |
| 1912 | else |
| 1913 | pat_ref_add(ref, key, value, NULL); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1914 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1915 | return 0; |
| 1916 | } |
| 1917 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1918 | /* A class is a lot of memory that contain data. This data can be a table, |
| 1919 | * 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] | 1920 | * metatable have an original version registered in the global context with |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1921 | * the name of the object (_G[<name>] = <metable> ). |
| 1922 | * |
| 1923 | * A metable is a table that modify the standard behavior of a standard |
| 1924 | * access to the associated data. The entries of this new metatable are |
| 1925 | * defined as is: |
| 1926 | * |
| 1927 | * http://lua-users.org/wiki/MetatableEvents |
| 1928 | * |
| 1929 | * __index |
| 1930 | * |
| 1931 | * we access an absent field in a table, the result is nil. This is |
| 1932 | * true, but it is not the whole truth. Actually, such access triggers |
| 1933 | * the interpreter to look for an __index metamethod: If there is no |
| 1934 | * such method, as usually happens, then the access results in nil; |
| 1935 | * otherwise, the metamethod will provide the result. |
| 1936 | * |
| 1937 | * Control 'prototype' inheritance. When accessing "myTable[key]" and |
| 1938 | * the key does not appear in the table, but the metatable has an __index |
| 1939 | * property: |
| 1940 | * |
| 1941 | * - if the value is a function, the function is called, passing in the |
| 1942 | * table and the key; the return value of that function is returned as |
| 1943 | * the result. |
| 1944 | * |
| 1945 | * - if the value is another table, the value of the key in that table is |
| 1946 | * asked for and returned (and if it doesn't exist in that table, but that |
| 1947 | * table's metatable has an __index property, then it continues on up) |
| 1948 | * |
| 1949 | * - Use "rawget(myTable,key)" to skip this metamethod. |
| 1950 | * |
| 1951 | * http://www.lua.org/pil/13.4.1.html |
| 1952 | * |
| 1953 | * __newindex |
| 1954 | * |
| 1955 | * Like __index, but control property assignment. |
| 1956 | * |
| 1957 | * __mode - Control weak references. A string value with one or both |
| 1958 | * of the characters 'k' and 'v' which specifies that the the |
| 1959 | * keys and/or values in the table are weak references. |
| 1960 | * |
| 1961 | * __call - Treat a table like a function. When a table is followed by |
| 1962 | * parenthesis such as "myTable( 'foo' )" and the metatable has |
| 1963 | * a __call key pointing to a function, that function is invoked |
| 1964 | * (passing any specified arguments) and the return value is |
| 1965 | * returned. |
| 1966 | * |
| 1967 | * __metatable - Hide the metatable. When "getmetatable( myTable )" is |
| 1968 | * called, if the metatable for myTable has a __metatable |
| 1969 | * key, the value of that key is returned instead of the |
| 1970 | * actual metatable. |
| 1971 | * |
| 1972 | * __tostring - Control string representation. When the builtin |
| 1973 | * "tostring( myTable )" function is called, if the metatable |
| 1974 | * for myTable has a __tostring property set to a function, |
| 1975 | * that function is invoked (passing myTable to it) and the |
| 1976 | * return value is used as the string representation. |
| 1977 | * |
| 1978 | * __len - Control table length. When the table length is requested using |
| 1979 | * the length operator ( '#' ), if the metatable for myTable has |
| 1980 | * a __len key pointing to a function, that function is invoked |
| 1981 | * (passing myTable to it) and the return value used as the value |
| 1982 | * of "#myTable". |
| 1983 | * |
| 1984 | * __gc - Userdata finalizer code. When userdata is set to be garbage |
| 1985 | * collected, if the metatable has a __gc field pointing to a |
| 1986 | * function, that function is first invoked, passing the userdata |
| 1987 | * to it. The __gc metamethod is not called for tables. |
| 1988 | * (See http://lua-users.org/lists/lua-l/2006-11/msg00508.html) |
| 1989 | * |
| 1990 | * Special metamethods for redefining standard operators: |
| 1991 | * http://www.lua.org/pil/13.1.html |
| 1992 | * |
| 1993 | * __add "+" |
| 1994 | * __sub "-" |
| 1995 | * __mul "*" |
| 1996 | * __div "/" |
| 1997 | * __unm "!" |
| 1998 | * __pow "^" |
| 1999 | * __concat ".." |
| 2000 | * |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 2001 | * Special methods for redefining standard relations |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 2002 | * http://www.lua.org/pil/13.2.html |
| 2003 | * |
| 2004 | * __eq "==" |
| 2005 | * __lt "<" |
| 2006 | * __le "<=" |
| 2007 | */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2008 | |
| 2009 | /* |
| 2010 | * |
| 2011 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2012 | * Class Map |
| 2013 | * |
| 2014 | * |
| 2015 | */ |
| 2016 | |
| 2017 | /* Returns a struct hlua_map if the stack entry "ud" is |
| 2018 | * a class session, otherwise it throws an error. |
| 2019 | */ |
| 2020 | __LJMP static struct map_descriptor *hlua_checkmap(lua_State *L, int ud) |
| 2021 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 2022 | return MAY_LJMP(hlua_checkudata(L, ud, class_map_ref)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2023 | } |
| 2024 | |
| 2025 | /* This function is the map constructor. It don't need |
| 2026 | * the class Map object. It creates and return a new Map |
| 2027 | * object. It must be called only during "body" or "init" |
| 2028 | * context because it process some filesystem accesses. |
| 2029 | */ |
| 2030 | __LJMP static int hlua_map_new(struct lua_State *L) |
| 2031 | { |
| 2032 | const char *fn; |
| 2033 | int match = PAT_MATCH_STR; |
| 2034 | struct sample_conv conv; |
| 2035 | const char *file = ""; |
| 2036 | int line = 0; |
| 2037 | lua_Debug ar; |
| 2038 | char *err = NULL; |
| 2039 | struct arg args[2]; |
| 2040 | |
| 2041 | if (lua_gettop(L) < 1 || lua_gettop(L) > 2) |
| 2042 | WILL_LJMP(luaL_error(L, "'new' needs at least 1 argument.")); |
| 2043 | |
| 2044 | fn = MAY_LJMP(luaL_checkstring(L, 1)); |
| 2045 | |
| 2046 | if (lua_gettop(L) >= 2) { |
| 2047 | match = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 2048 | if (match < 0 || match >= PAT_MATCH_NUM) |
| 2049 | WILL_LJMP(luaL_error(L, "'new' needs a valid match method.")); |
| 2050 | } |
| 2051 | |
| 2052 | /* Get Lua filename and line number. */ |
| 2053 | if (lua_getstack(L, 1, &ar)) { /* check function at level */ |
| 2054 | lua_getinfo(L, "Sl", &ar); /* get info about it */ |
| 2055 | if (ar.currentline > 0) { /* is there info? */ |
| 2056 | file = ar.short_src; |
| 2057 | line = ar.currentline; |
| 2058 | } |
| 2059 | } |
| 2060 | |
| 2061 | /* fill fake sample_conv struct. */ |
| 2062 | conv.kw = ""; /* unused. */ |
| 2063 | conv.process = NULL; /* unused. */ |
| 2064 | conv.arg_mask = 0; /* unused. */ |
| 2065 | conv.val_args = NULL; /* unused. */ |
| 2066 | conv.out_type = SMP_T_STR; |
| 2067 | conv.private = (void *)(long)match; |
| 2068 | switch (match) { |
| 2069 | case PAT_MATCH_STR: conv.in_type = SMP_T_STR; break; |
| 2070 | case PAT_MATCH_BEG: conv.in_type = SMP_T_STR; break; |
| 2071 | case PAT_MATCH_SUB: conv.in_type = SMP_T_STR; break; |
| 2072 | case PAT_MATCH_DIR: conv.in_type = SMP_T_STR; break; |
| 2073 | case PAT_MATCH_DOM: conv.in_type = SMP_T_STR; break; |
| 2074 | case PAT_MATCH_END: conv.in_type = SMP_T_STR; break; |
| 2075 | case PAT_MATCH_REG: conv.in_type = SMP_T_STR; break; |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 2076 | case PAT_MATCH_INT: conv.in_type = SMP_T_SINT; break; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2077 | case PAT_MATCH_IP: conv.in_type = SMP_T_ADDR; break; |
| 2078 | default: |
| 2079 | WILL_LJMP(luaL_error(L, "'new' doesn't support this match mode.")); |
| 2080 | } |
| 2081 | |
| 2082 | /* fill fake args. */ |
| 2083 | args[0].type = ARGT_STR; |
Christopher Faulet | 73292e9 | 2020-08-06 08:40:09 +0200 | [diff] [blame] | 2084 | args[0].data.str.area = strdup(fn); |
| 2085 | args[0].data.str.data = strlen(fn); |
| 2086 | args[0].data.str.size = args[0].data.str.data+1; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2087 | args[1].type = ARGT_STOP; |
| 2088 | |
| 2089 | /* load the map. */ |
| 2090 | if (!sample_load_map(args, &conv, file, line, &err)) { |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 2091 | /* error case: we can't use luaL_error because we must |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2092 | * free the err variable. |
| 2093 | */ |
| 2094 | luaL_where(L, 1); |
| 2095 | lua_pushfstring(L, "'new': %s.", err); |
| 2096 | lua_concat(L, 2); |
| 2097 | free(err); |
Christopher Faulet | 6ad7df4 | 2020-08-07 11:45:18 +0200 | [diff] [blame] | 2098 | chunk_destroy(&args[0].data.str); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2099 | WILL_LJMP(lua_error(L)); |
| 2100 | } |
| 2101 | |
| 2102 | /* create the lua object. */ |
| 2103 | lua_newtable(L); |
| 2104 | lua_pushlightuserdata(L, args[0].data.map); |
| 2105 | lua_rawseti(L, -2, 0); |
| 2106 | |
| 2107 | /* Pop a class Map metatable and affect it to the userdata. */ |
| 2108 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_map_ref); |
| 2109 | lua_setmetatable(L, -2); |
| 2110 | |
| 2111 | |
| 2112 | return 1; |
| 2113 | } |
| 2114 | |
| 2115 | __LJMP static inline int _hlua_map_lookup(struct lua_State *L, int str) |
| 2116 | { |
| 2117 | struct map_descriptor *desc; |
| 2118 | struct pattern *pat; |
| 2119 | struct sample smp; |
| 2120 | |
| 2121 | MAY_LJMP(check_args(L, 2, "lookup")); |
| 2122 | desc = MAY_LJMP(hlua_checkmap(L, 1)); |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 2123 | if (desc->pat.expect_type == SMP_T_SINT) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 2124 | smp.data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 2125 | smp.data.u.sint = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2126 | } |
| 2127 | else { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 2128 | smp.data.type = SMP_T_STR; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2129 | smp.flags = SMP_F_CONST; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2130 | 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] | 2131 | smp.data.u.str.size = smp.data.u.str.data + 1; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2132 | } |
| 2133 | |
| 2134 | pat = pattern_exec_match(&desc->pat, &smp, 1); |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 2135 | if (!pat || !pat->data) { |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2136 | if (str) |
| 2137 | lua_pushstring(L, ""); |
| 2138 | else |
| 2139 | lua_pushnil(L); |
| 2140 | return 1; |
| 2141 | } |
| 2142 | |
| 2143 | /* 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] | 2144 | 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] | 2145 | return 1; |
| 2146 | } |
| 2147 | |
| 2148 | __LJMP static int hlua_map_lookup(struct lua_State *L) |
| 2149 | { |
| 2150 | return _hlua_map_lookup(L, 0); |
| 2151 | } |
| 2152 | |
| 2153 | __LJMP static int hlua_map_slookup(struct lua_State *L) |
| 2154 | { |
| 2155 | return _hlua_map_lookup(L, 1); |
| 2156 | } |
| 2157 | |
| 2158 | /* |
| 2159 | * |
| 2160 | * |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2161 | * Class Socket |
| 2162 | * |
| 2163 | * |
| 2164 | */ |
| 2165 | |
| 2166 | __LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud) |
| 2167 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 2168 | return MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2169 | } |
| 2170 | |
| 2171 | /* 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] | 2172 | * connection. It is used for notify space available to send or data |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2173 | * received. |
| 2174 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 2175 | static void hlua_socket_handler(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2176 | { |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2177 | struct hlua_csk_ctx *ctx = appctx->svcctx; |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 2178 | struct stconn *sc = appctx_sc(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2179 | |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 2180 | if (unlikely(se_fl_test(appctx->sedesc, (SE_FL_EOS|SE_FL_ERROR|SE_FL_SHR|SE_FL_SHW)))) { |
| 2181 | notification_wake(&ctx->wake_on_read); |
| 2182 | notification_wake(&ctx->wake_on_write); |
| 2183 | return; |
| 2184 | } |
| 2185 | |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2186 | if (ctx->die) { |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 2187 | se_fl_set(appctx->sedesc, SE_FL_EOI|SE_FL_EOS); |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2188 | notification_wake(&ctx->wake_on_read); |
| 2189 | notification_wake(&ctx->wake_on_write); |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 2190 | return; |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 2191 | } |
| 2192 | |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 2193 | /* If we can't write, wakeup the pending write signals. */ |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 2194 | if (channel_output_closed(sc_ic(sc))) |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2195 | notification_wake(&ctx->wake_on_write); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 2196 | |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 2197 | /* If we can't read, wakeup the pending read signals. */ |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 2198 | if (channel_input_closed(sc_oc(sc))) |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2199 | notification_wake(&ctx->wake_on_read); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 2200 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2201 | /* if the connection is not established, inform the stream that we want |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 2202 | * to be notified whenever the connection completes. |
| 2203 | */ |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 2204 | if (sc_opposite(sc)->state < SC_ST_EST) { |
Willy Tarreau | 90e8b45 | 2022-05-25 18:21:43 +0200 | [diff] [blame] | 2205 | applet_need_more_data(appctx); |
Willy Tarreau | b23edc8 | 2022-05-24 16:49:03 +0200 | [diff] [blame] | 2206 | se_need_remote_conn(appctx->sedesc); |
Willy Tarreau | 4164eb9 | 2022-05-25 15:42:03 +0200 | [diff] [blame] | 2207 | applet_have_more_data(appctx); |
Willy Tarreau | d4da196 | 2015-04-20 01:31:23 +0200 | [diff] [blame] | 2208 | return; |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 2209 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2210 | |
| 2211 | /* This function is called after the connect. */ |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2212 | ctx->connected = 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2213 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2214 | /* 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] | 2215 | if (channel_may_recv(sc_ic(sc))) |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2216 | notification_wake(&ctx->wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2217 | |
| 2218 | /* Wake the tasks which wants to read if the buffer contains data. */ |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 2219 | if (!channel_is_empty(sc_oc(sc))) |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2220 | notification_wake(&ctx->wake_on_read); |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 2221 | |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 2222 | /* If write notifications are registered, we considers we want |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 2223 | * to write, so we clear the blocking flag. |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 2224 | */ |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2225 | if (notification_registered(&ctx->wake_on_write)) |
Willy Tarreau | 4164eb9 | 2022-05-25 15:42:03 +0200 | [diff] [blame] | 2226 | applet_have_more_data(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2227 | } |
| 2228 | |
Christopher Faulet | 5f35a3e | 2022-05-12 15:31:05 +0200 | [diff] [blame] | 2229 | static int hlua_socket_init(struct appctx *appctx) |
| 2230 | { |
| 2231 | struct hlua_csk_ctx *ctx = appctx->svcctx; |
| 2232 | struct stream *s; |
| 2233 | |
| 2234 | if (appctx_finalize_startup(appctx, socket_proxy, &BUF_NULL) == -1) |
| 2235 | goto error; |
| 2236 | |
| 2237 | s = appctx_strm(appctx); |
| 2238 | |
Willy Tarreau | 4596fe2 | 2022-05-17 19:07:51 +0200 | [diff] [blame] | 2239 | /* Configure "right" stream connector. This stconn is used to connect |
Christopher Faulet | 5f35a3e | 2022-05-12 15:31:05 +0200 | [diff] [blame] | 2240 | * and retrieve data from the server. The connection is initialized |
| 2241 | * with the "struct server". |
| 2242 | */ |
Willy Tarreau | 74568cf | 2022-05-27 09:03:30 +0200 | [diff] [blame] | 2243 | sc_set_state(s->scb, SC_ST_ASS); |
Christopher Faulet | 5f35a3e | 2022-05-12 15:31:05 +0200 | [diff] [blame] | 2244 | |
| 2245 | /* Force destination server. */ |
| 2246 | s->flags |= SF_DIRECT | SF_ASSIGNED | SF_BE_ASSIGNED; |
| 2247 | s->target = &socket_tcp->obj_type; |
| 2248 | |
| 2249 | ctx->appctx = appctx; |
| 2250 | return 0; |
| 2251 | |
| 2252 | error: |
| 2253 | return -1; |
| 2254 | } |
| 2255 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2256 | /* This function is called when the "struct stream" is destroyed. |
| 2257 | * Remove the link from the object to this stream. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2258 | * Wake all the pending signals. |
| 2259 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 2260 | static void hlua_socket_release(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2261 | { |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2262 | struct hlua_csk_ctx *ctx = appctx->svcctx; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2263 | struct xref *peer; |
| 2264 | |
Christopher Faulet | 5f35a3e | 2022-05-12 15:31:05 +0200 | [diff] [blame] | 2265 | /* Remove my link in the original objects. */ |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2266 | peer = xref_get_peer_and_lock(&ctx->xref); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2267 | if (peer) |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2268 | xref_disconnect(&ctx->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2269 | |
| 2270 | /* Wake all the task waiting for me. */ |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2271 | notification_wake(&ctx->wake_on_read); |
| 2272 | notification_wake(&ctx->wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2273 | } |
| 2274 | |
| 2275 | /* If the garbage collectio of the object is launch, nobody |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2276 | * uses this object. If the stream does not exists, just quit. |
| 2277 | * Send the shutdown signal to the stream. In some cases, |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2278 | * pending signal can rest in the read and write lists. destroy |
| 2279 | * it. |
| 2280 | */ |
| 2281 | __LJMP static int hlua_socket_gc(lua_State *L) |
| 2282 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2283 | struct hlua_socket *socket; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2284 | struct hlua_csk_ctx *ctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2285 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2286 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2287 | MAY_LJMP(check_args(L, 1, "__gc")); |
| 2288 | |
| 2289 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2290 | peer = xref_get_peer_and_lock(&socket->xref); |
| 2291 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2292 | return 0; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2293 | |
| 2294 | ctx = container_of(peer, struct hlua_csk_ctx, xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2295 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2296 | /* Set the flag which destroy the session. */ |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2297 | ctx->die = 1; |
| 2298 | appctx_wakeup(ctx->appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2299 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2300 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2301 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2302 | return 0; |
| 2303 | } |
| 2304 | |
| 2305 | /* The close function send shutdown signal and break the |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2306 | * links between the stream and the object. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2307 | */ |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 2308 | __LJMP static int hlua_socket_close_helper(lua_State *L) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2309 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2310 | struct hlua_socket *socket; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2311 | struct hlua_csk_ctx *ctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2312 | struct xref *peer; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2313 | struct hlua *hlua; |
| 2314 | |
| 2315 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 2316 | hlua = hlua_gethlua(L); |
| 2317 | if (!hlua) |
| 2318 | return 0; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2319 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2320 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2321 | |
| 2322 | /* Check if we run on the same thread than the xreator thread. |
| 2323 | * We cannot access to the socket if the thread is different. |
| 2324 | */ |
| 2325 | if (socket->tid != tid) |
| 2326 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2327 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2328 | peer = xref_get_peer_and_lock(&socket->xref); |
| 2329 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2330 | return 0; |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 2331 | |
| 2332 | hlua->gc_count--; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2333 | ctx = container_of(peer, struct hlua_csk_ctx, xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2334 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2335 | /* Set the flag which destroy the session. */ |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2336 | ctx->die = 1; |
| 2337 | appctx_wakeup(ctx->appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2338 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2339 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2340 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2341 | return 0; |
| 2342 | } |
| 2343 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 2344 | /* The close function calls close_helper. |
| 2345 | */ |
| 2346 | __LJMP static int hlua_socket_close(lua_State *L) |
| 2347 | { |
| 2348 | MAY_LJMP(check_args(L, 1, "close")); |
| 2349 | return hlua_socket_close_helper(L); |
| 2350 | } |
| 2351 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2352 | /* This Lua function assumes that the stack contain three parameters. |
| 2353 | * 1 - USERDATA containing a struct socket |
| 2354 | * 2 - INTEGER with values of the macro defined below |
| 2355 | * If the integer is -1, we must read at most one line. |
| 2356 | * If the integer is -2, we ust read all the data until the |
| 2357 | * end of the stream. |
| 2358 | * If the integer is positive value, we must read a number of |
| 2359 | * bytes corresponding to this value. |
| 2360 | */ |
| 2361 | #define HLSR_READ_LINE (-1) |
| 2362 | #define HLSR_READ_ALL (-2) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2363 | __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] | 2364 | { |
| 2365 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2366 | int wanted = lua_tointeger(L, 2); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2367 | struct hlua *hlua; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2368 | struct hlua_csk_ctx *csk_ctx; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2369 | struct appctx *appctx; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2370 | size_t len; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2371 | int nblk; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 2372 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2373 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 2374 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2375 | size_t len2; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 2376 | int skip_at_end = 0; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 2377 | struct channel *oc; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2378 | struct stream *s; |
| 2379 | struct xref *peer; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 2380 | int missing_bytes; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2381 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2382 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 2383 | hlua = hlua_gethlua(L); |
| 2384 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2385 | /* Check if this lua stack is schedulable. */ |
| 2386 | if (!hlua || !hlua->task) |
| 2387 | WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in " |
| 2388 | "'frontend', 'backend' or 'task'")); |
| 2389 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2390 | /* Check if we run on the same thread than the xreator thread. |
| 2391 | * We cannot access to the socket if the thread is different. |
| 2392 | */ |
| 2393 | if (socket->tid != tid) |
| 2394 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2395 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2396 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2397 | peer = xref_get_peer_and_lock(&socket->xref); |
| 2398 | if (!peer) |
| 2399 | goto no_peer; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2400 | |
| 2401 | csk_ctx = container_of(peer, struct hlua_csk_ctx, xref); |
| 2402 | appctx = csk_ctx->appctx; |
Willy Tarreau | 0698c80 | 2022-05-11 14:09:57 +0200 | [diff] [blame] | 2403 | s = appctx_strm(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2404 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2405 | oc = &s->res; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2406 | if (wanted == HLSR_READ_LINE) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2407 | /* Read line. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2408 | nblk = co_getline_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2409 | if (nblk < 0) /* Connection close. */ |
| 2410 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2411 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2412 | goto connection_empty; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 2413 | |
| 2414 | /* remove final \r\n. */ |
| 2415 | if (nblk == 1) { |
| 2416 | if (blk1[len1-1] == '\n') { |
| 2417 | len1--; |
| 2418 | skip_at_end++; |
| 2419 | if (blk1[len1-1] == '\r') { |
| 2420 | len1--; |
| 2421 | skip_at_end++; |
| 2422 | } |
| 2423 | } |
| 2424 | } |
| 2425 | else { |
| 2426 | if (blk2[len2-1] == '\n') { |
| 2427 | len2--; |
| 2428 | skip_at_end++; |
| 2429 | if (blk2[len2-1] == '\r') { |
| 2430 | len2--; |
| 2431 | skip_at_end++; |
| 2432 | } |
| 2433 | } |
| 2434 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2435 | } |
| 2436 | |
| 2437 | else if (wanted == HLSR_READ_ALL) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2438 | /* Read all the available data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2439 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2440 | if (nblk < 0) /* Connection close. */ |
| 2441 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2442 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2443 | goto connection_empty; |
| 2444 | } |
| 2445 | |
| 2446 | else { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2447 | /* Read a block of data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2448 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2449 | if (nblk < 0) /* Connection close. */ |
| 2450 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2451 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2452 | goto connection_empty; |
| 2453 | |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 2454 | missing_bytes = wanted - socket->b.n; |
| 2455 | if (len1 > missing_bytes) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2456 | nblk = 1; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 2457 | len1 = missing_bytes; |
| 2458 | } if (nblk == 2 && len1 + len2 > missing_bytes) |
| 2459 | len2 = missing_bytes - len1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2460 | } |
| 2461 | |
| 2462 | len = len1; |
| 2463 | |
| 2464 | luaL_addlstring(&socket->b, blk1, len1); |
| 2465 | if (nblk == 2) { |
| 2466 | len += len2; |
| 2467 | luaL_addlstring(&socket->b, blk2, len2); |
| 2468 | } |
| 2469 | |
| 2470 | /* Consume data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2471 | co_skip(oc, len + skip_at_end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2472 | |
| 2473 | /* Don't wait anything. */ |
Thierry FOURNIER | 7e4ee47 | 2018-05-25 15:03:50 +0200 | [diff] [blame] | 2474 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2475 | |
| 2476 | /* If the pattern reclaim to read all the data |
| 2477 | * in the connection, got out. |
| 2478 | */ |
| 2479 | if (wanted == HLSR_READ_ALL) |
| 2480 | goto connection_empty; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 2481 | else if (wanted >= 0 && socket->b.n < wanted) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2482 | goto connection_empty; |
| 2483 | |
| 2484 | /* Return result. */ |
| 2485 | luaL_pushresult(&socket->b); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2486 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2487 | return 1; |
| 2488 | |
| 2489 | connection_closed: |
| 2490 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2491 | xref_unlock(&socket->xref, peer); |
| 2492 | |
| 2493 | no_peer: |
| 2494 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2495 | /* If the buffer containds data. */ |
| 2496 | if (socket->b.n > 0) { |
| 2497 | luaL_pushresult(&socket->b); |
| 2498 | return 1; |
| 2499 | } |
| 2500 | lua_pushnil(L); |
| 2501 | lua_pushstring(L, "connection closed."); |
| 2502 | return 2; |
| 2503 | |
| 2504 | connection_empty: |
| 2505 | |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2506 | if (!notification_new(&hlua->com, &csk_ctx->wake_on_read, hlua->task)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2507 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2508 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2509 | } |
| 2510 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2511 | 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] | 2512 | return 0; |
| 2513 | } |
| 2514 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2515 | /* This Lua function gets two parameters. The first one can be string |
| 2516 | * or a number. If the string is "*l", the user requires one line. If |
| 2517 | * 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] | 2518 | * If the value is a number, the user require a number of bytes equal |
| 2519 | * to the value. The default value is "*l" (a line). |
| 2520 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2521 | * This parameter with a variable type is converted in integer. This |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2522 | * integer takes this values: |
| 2523 | * -1 : read a line |
| 2524 | * -2 : read all the stream |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2525 | * >0 : amount of bytes. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2526 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2527 | * The second parameter is optional. It contains a string that must be |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2528 | * concatenated with the read data. |
| 2529 | */ |
| 2530 | __LJMP static int hlua_socket_receive(struct lua_State *L) |
| 2531 | { |
| 2532 | int wanted = HLSR_READ_LINE; |
| 2533 | const char *pattern; |
Christopher Faulet | c31b200 | 2021-05-03 10:11:13 +0200 | [diff] [blame] | 2534 | int lastarg, type; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2535 | char *error; |
| 2536 | size_t len; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2537 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2538 | |
| 2539 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 2540 | WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments.")); |
| 2541 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2542 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2543 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2544 | /* Check if we run on the same thread than the xreator thread. |
| 2545 | * We cannot access to the socket if the thread is different. |
| 2546 | */ |
| 2547 | if (socket->tid != tid) |
| 2548 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2549 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2550 | /* check for pattern. */ |
| 2551 | if (lua_gettop(L) >= 2) { |
| 2552 | type = lua_type(L, 2); |
| 2553 | if (type == LUA_TSTRING) { |
| 2554 | pattern = lua_tostring(L, 2); |
| 2555 | if (strcmp(pattern, "*a") == 0) |
| 2556 | wanted = HLSR_READ_ALL; |
| 2557 | else if (strcmp(pattern, "*l") == 0) |
| 2558 | wanted = HLSR_READ_LINE; |
| 2559 | else { |
| 2560 | wanted = strtoll(pattern, &error, 10); |
| 2561 | if (*error != '\0') |
| 2562 | WILL_LJMP(luaL_error(L, "Unsupported pattern.")); |
| 2563 | } |
| 2564 | } |
| 2565 | else if (type == LUA_TNUMBER) { |
| 2566 | wanted = lua_tointeger(L, 2); |
| 2567 | if (wanted < 0) |
| 2568 | WILL_LJMP(luaL_error(L, "Unsupported size.")); |
| 2569 | } |
| 2570 | } |
| 2571 | |
| 2572 | /* Set pattern. */ |
| 2573 | lua_pushinteger(L, wanted); |
Tim Duesterhus | c6e377e | 2018-01-04 19:32:13 +0100 | [diff] [blame] | 2574 | |
| 2575 | /* Check if we would replace the top by itself. */ |
| 2576 | if (lua_gettop(L) != 2) |
| 2577 | lua_replace(L, 2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2578 | |
Christopher Faulet | c31b200 | 2021-05-03 10:11:13 +0200 | [diff] [blame] | 2579 | /* Save index of the top of the stack because since buffers are used, it |
| 2580 | * may change |
| 2581 | */ |
| 2582 | lastarg = lua_gettop(L); |
| 2583 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2584 | /* init buffer, and fill it with prefix. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2585 | luaL_buffinit(L, &socket->b); |
| 2586 | |
| 2587 | /* Check prefix. */ |
Christopher Faulet | c31b200 | 2021-05-03 10:11:13 +0200 | [diff] [blame] | 2588 | if (lastarg >= 3) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2589 | if (lua_type(L, 3) != LUA_TSTRING) |
| 2590 | WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix")); |
| 2591 | pattern = lua_tolstring(L, 3, &len); |
| 2592 | luaL_addlstring(&socket->b, pattern, len); |
| 2593 | } |
| 2594 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2595 | return __LJMP(hlua_socket_receive_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2596 | } |
| 2597 | |
| 2598 | /* Write the Lua input string in the output buffer. |
Mark Lakes | 22154b4 | 2018-01-29 14:38:40 -0800 | [diff] [blame] | 2599 | * This function returns a yield if no space is available. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2600 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2601 | 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] | 2602 | { |
| 2603 | struct hlua_socket *socket; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2604 | struct hlua *hlua; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2605 | struct hlua_csk_ctx *csk_ctx; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2606 | struct appctx *appctx; |
| 2607 | size_t buf_len; |
| 2608 | const char *buf; |
| 2609 | int len; |
| 2610 | int send_len; |
| 2611 | int sent; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2612 | struct xref *peer; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2613 | struct stream *s; |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 2614 | struct stconn *sc; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2615 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2616 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 2617 | hlua = hlua_gethlua(L); |
| 2618 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2619 | /* Check if this lua stack is schedulable. */ |
| 2620 | if (!hlua || !hlua->task) |
| 2621 | WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in " |
| 2622 | "'frontend', 'backend' or 'task'")); |
| 2623 | |
| 2624 | /* Get object */ |
| 2625 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2626 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2627 | sent = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2628 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2629 | /* Check if we run on the same thread than the xreator thread. |
| 2630 | * We cannot access to the socket if the thread is different. |
| 2631 | */ |
| 2632 | if (socket->tid != tid) |
| 2633 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2634 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2635 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2636 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2637 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2638 | lua_pushinteger(L, -1); |
| 2639 | return 1; |
| 2640 | } |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2641 | |
| 2642 | csk_ctx = container_of(peer, struct hlua_csk_ctx, xref); |
| 2643 | appctx = csk_ctx->appctx; |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 2644 | sc = appctx_sc(appctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 2645 | s = __sc_strm(sc); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2646 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2647 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2648 | if (channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2649 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2650 | lua_pushinteger(L, -1); |
| 2651 | return 1; |
| 2652 | } |
| 2653 | |
| 2654 | /* Update the input buffer data. */ |
| 2655 | buf += sent; |
| 2656 | send_len = buf_len - sent; |
| 2657 | |
| 2658 | /* All the data are sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2659 | if (sent >= buf_len) { |
| 2660 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2661 | return 1; /* Implicitly return the length sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2662 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2663 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2664 | /* Check if the buffer is available because HAProxy doesn't allocate |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 2665 | * the request buffer if its not required. |
| 2666 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2667 | if (s->req.buf.size == 0) { |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 2668 | if (!sc_alloc_ibuf(sc, &appctx->buffer_wait)) |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2669 | goto hlua_socket_write_yield_return; |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 2670 | } |
| 2671 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2672 | /* Check for available space. */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2673 | len = b_room(&s->req.buf); |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2674 | if (len <= 0) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2675 | goto hlua_socket_write_yield_return; |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2676 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2677 | |
| 2678 | /* send data */ |
| 2679 | if (len < send_len) |
| 2680 | send_len = len; |
Thierry FOURNIER | 66b8919 | 2018-05-27 01:14:47 +0200 | [diff] [blame] | 2681 | len = ci_putblk(&s->req, buf, send_len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2682 | |
| 2683 | /* "Not enough space" (-1), "Buffer too little to contain |
| 2684 | * the data" (-2) are not expected because the available length |
| 2685 | * is tested. |
| 2686 | * Other unknown error are also not expected. |
| 2687 | */ |
| 2688 | if (len <= 0) { |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2689 | if (len == -1) |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2690 | s->req.flags |= CF_WAKE_WRITE; |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2691 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 2692 | MAY_LJMP(hlua_socket_close_helper(L)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2693 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2694 | lua_pushinteger(L, -1); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2695 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2696 | return 1; |
| 2697 | } |
| 2698 | |
| 2699 | /* update buffers. */ |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 2700 | appctx_wakeup(appctx); |
Willy Tarreau | de70fa1 | 2015-09-26 11:25:05 +0200 | [diff] [blame] | 2701 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2702 | /* Update length sent. */ |
| 2703 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2704 | lua_pushinteger(L, sent + len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2705 | |
| 2706 | /* All the data buffer is sent ? */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2707 | if (sent + len >= buf_len) { |
| 2708 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2709 | return 1; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2710 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2711 | |
| 2712 | hlua_socket_write_yield_return: |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2713 | if (!notification_new(&hlua->com, &csk_ctx->wake_on_write, hlua->task)) { |
Thierry FOURNIER | ba42fcd | 2018-05-27 00:59:48 +0200 | [diff] [blame] | 2714 | xref_unlock(&socket->xref, peer); |
| 2715 | WILL_LJMP(luaL_error(L, "out of memory")); |
| 2716 | } |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2717 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2718 | 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] | 2719 | return 0; |
| 2720 | } |
| 2721 | |
| 2722 | /* This function initiate the send of data. It just check the input |
| 2723 | * parameters and push an integer in the Lua stack that contain the |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2724 | * 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] | 2725 | * "hlua_socket_write_yield" that can yield. |
| 2726 | * |
| 2727 | * The Lua function gets between 3 and 4 parameters. The first one is |
| 2728 | * the associated object. The second is a string buffer. The third is |
| 2729 | * a facultative integer that represents where is the buffer position |
| 2730 | * of the start of the data that can send. The first byte is the |
| 2731 | * position "1". The default value is "1". The fourth argument is a |
| 2732 | * facultative integer that represents where is the buffer position |
| 2733 | * of the end of the data that can send. The default is the last byte. |
| 2734 | */ |
| 2735 | static int hlua_socket_send(struct lua_State *L) |
| 2736 | { |
| 2737 | int i; |
| 2738 | int j; |
| 2739 | const char *buf; |
| 2740 | size_t buf_len; |
| 2741 | |
| 2742 | /* Check number of arguments. */ |
| 2743 | if (lua_gettop(L) < 2 || lua_gettop(L) > 4) |
| 2744 | WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments")); |
| 2745 | |
| 2746 | /* Get the string. */ |
| 2747 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
| 2748 | |
| 2749 | /* Get and check j. */ |
| 2750 | if (lua_gettop(L) == 4) { |
| 2751 | j = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 2752 | if (j < 0) |
| 2753 | j = buf_len + j + 1; |
| 2754 | if (j > buf_len) |
| 2755 | j = buf_len + 1; |
| 2756 | lua_pop(L, 1); |
| 2757 | } |
| 2758 | else |
| 2759 | j = buf_len; |
| 2760 | |
| 2761 | /* Get and check i. */ |
| 2762 | if (lua_gettop(L) == 3) { |
| 2763 | i = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2764 | if (i < 0) |
| 2765 | i = buf_len + i + 1; |
| 2766 | if (i > buf_len) |
| 2767 | i = buf_len + 1; |
| 2768 | lua_pop(L, 1); |
| 2769 | } else |
| 2770 | i = 1; |
| 2771 | |
| 2772 | /* Check bth i and j. */ |
| 2773 | if (i > j) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2774 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2775 | return 1; |
| 2776 | } |
| 2777 | if (i == 0 && j == 0) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2778 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2779 | return 1; |
| 2780 | } |
| 2781 | if (i == 0) |
| 2782 | i = 1; |
| 2783 | if (j == 0) |
| 2784 | j = 1; |
| 2785 | |
| 2786 | /* Pop the string. */ |
| 2787 | lua_pop(L, 1); |
| 2788 | |
| 2789 | /* Update the buffer length. */ |
| 2790 | buf += i - 1; |
| 2791 | buf_len = j - i + 1; |
| 2792 | lua_pushlstring(L, buf, buf_len); |
| 2793 | |
| 2794 | /* This unsigned is used to remember the amount of sent data. */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2795 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2796 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2797 | return MAY_LJMP(hlua_socket_write_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2798 | } |
| 2799 | |
Willy Tarreau | 22b0a68 | 2015-06-17 19:43:49 +0200 | [diff] [blame] | 2800 | #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] | 2801 | __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] | 2802 | { |
| 2803 | static char buffer[SOCKET_INFO_MAX_LEN]; |
| 2804 | int ret; |
| 2805 | int len; |
| 2806 | char *p; |
| 2807 | |
| 2808 | ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1); |
| 2809 | if (ret <= 0) { |
| 2810 | lua_pushnil(L); |
| 2811 | return 1; |
| 2812 | } |
| 2813 | |
| 2814 | if (ret == AF_UNIX) { |
| 2815 | lua_pushstring(L, buffer+1); |
| 2816 | return 1; |
| 2817 | } |
| 2818 | else if (ret == AF_INET6) { |
| 2819 | buffer[0] = '['; |
| 2820 | len = strlen(buffer); |
| 2821 | buffer[len] = ']'; |
| 2822 | len++; |
| 2823 | buffer[len] = ':'; |
| 2824 | len++; |
| 2825 | p = buffer; |
| 2826 | } |
| 2827 | else if (ret == AF_INET) { |
| 2828 | p = buffer + 1; |
| 2829 | len = strlen(p); |
| 2830 | p[len] = ':'; |
| 2831 | len++; |
| 2832 | } |
| 2833 | else { |
| 2834 | lua_pushnil(L); |
| 2835 | return 1; |
| 2836 | } |
| 2837 | |
| 2838 | if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) { |
| 2839 | lua_pushnil(L); |
| 2840 | return 1; |
| 2841 | } |
| 2842 | |
| 2843 | lua_pushstring(L, p); |
| 2844 | return 1; |
| 2845 | } |
| 2846 | |
| 2847 | /* Returns information about the peer of the connection. */ |
| 2848 | __LJMP static int hlua_socket_getpeername(struct lua_State *L) |
| 2849 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2850 | struct hlua_socket *socket; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2851 | struct xref *peer; |
| 2852 | struct appctx *appctx; |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 2853 | struct stconn *sc; |
Christopher Faulet | 16f16af | 2021-10-27 09:34:56 +0200 | [diff] [blame] | 2854 | const struct sockaddr_storage *dst; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2855 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2856 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2857 | MAY_LJMP(check_args(L, 1, "getpeername")); |
| 2858 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2859 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2860 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2861 | /* Check if we run on the same thread than the xreator thread. |
| 2862 | * We cannot access to the socket if the thread is different. |
| 2863 | */ |
| 2864 | if (socket->tid != tid) |
| 2865 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2866 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2867 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2868 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2869 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2870 | lua_pushnil(L); |
| 2871 | return 1; |
| 2872 | } |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2873 | |
| 2874 | appctx = container_of(peer, struct hlua_csk_ctx, xref)->appctx; |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 2875 | sc = appctx_sc(appctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 2876 | dst = sc_dst(sc_opposite(sc)); |
Christopher Faulet | 16f16af | 2021-10-27 09:34:56 +0200 | [diff] [blame] | 2877 | if (!dst) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2878 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2879 | lua_pushnil(L); |
| 2880 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2881 | } |
| 2882 | |
Christopher Faulet | 16f16af | 2021-10-27 09:34:56 +0200 | [diff] [blame] | 2883 | ret = MAY_LJMP(hlua_socket_info(L, dst)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2884 | xref_unlock(&socket->xref, peer); |
| 2885 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2886 | } |
| 2887 | |
| 2888 | /* Returns information about my connection side. */ |
| 2889 | static int hlua_socket_getsockname(struct lua_State *L) |
| 2890 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2891 | struct hlua_socket *socket; |
| 2892 | struct connection *conn; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2893 | struct appctx *appctx; |
| 2894 | struct xref *peer; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2895 | struct stream *s; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2896 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2897 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2898 | MAY_LJMP(check_args(L, 1, "getsockname")); |
| 2899 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2900 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2901 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2902 | /* Check if we run on the same thread than the xreator thread. |
| 2903 | * We cannot access to the socket if the thread is different. |
| 2904 | */ |
| 2905 | if (socket->tid != tid) |
| 2906 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2907 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2908 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2909 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2910 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2911 | lua_pushnil(L); |
| 2912 | return 1; |
| 2913 | } |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2914 | |
| 2915 | appctx = container_of(peer, struct hlua_csk_ctx, xref)->appctx; |
Willy Tarreau | 0698c80 | 2022-05-11 14:09:57 +0200 | [diff] [blame] | 2916 | s = appctx_strm(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2917 | |
Willy Tarreau | fd9417b | 2022-05-18 16:23:22 +0200 | [diff] [blame] | 2918 | conn = sc_conn(s->scb); |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2919 | if (!conn || !conn_get_src(conn)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2920 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2921 | lua_pushnil(L); |
| 2922 | return 1; |
| 2923 | } |
| 2924 | |
Willy Tarreau | 9da9a6f | 2019-07-17 14:49:44 +0200 | [diff] [blame] | 2925 | ret = hlua_socket_info(L, conn->src); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2926 | xref_unlock(&socket->xref, peer); |
| 2927 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2928 | } |
| 2929 | |
| 2930 | /* This struct define the applet. */ |
Willy Tarreau | 3057645 | 2015-04-13 13:50:30 +0200 | [diff] [blame] | 2931 | static struct applet update_applet = { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2932 | .obj_type = OBJ_TYPE_APPLET, |
| 2933 | .name = "<LUA_TCP>", |
| 2934 | .fct = hlua_socket_handler, |
Christopher Faulet | 5f35a3e | 2022-05-12 15:31:05 +0200 | [diff] [blame] | 2935 | .init = hlua_socket_init, |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2936 | .release = hlua_socket_release, |
| 2937 | }; |
| 2938 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2939 | __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] | 2940 | { |
| 2941 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2942 | struct hlua *hlua; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2943 | struct xref *peer; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2944 | struct hlua_csk_ctx *csk_ctx; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2945 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2946 | struct stream *s; |
| 2947 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2948 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 2949 | hlua = hlua_gethlua(L); |
| 2950 | if (!hlua) |
| 2951 | return 0; |
| 2952 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2953 | /* Check if we run on the same thread than the xreator thread. |
| 2954 | * We cannot access to the socket if the thread is different. |
| 2955 | */ |
| 2956 | if (socket->tid != tid) |
| 2957 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2958 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2959 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2960 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2961 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2962 | lua_pushnil(L); |
| 2963 | lua_pushstring(L, "Can't connect"); |
| 2964 | return 2; |
| 2965 | } |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2966 | |
| 2967 | csk_ctx = container_of(peer, struct hlua_csk_ctx, xref); |
| 2968 | appctx = csk_ctx->appctx; |
Willy Tarreau | 0698c80 | 2022-05-11 14:09:57 +0200 | [diff] [blame] | 2969 | s = appctx_strm(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2970 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2971 | /* Check if we run on the same thread than the xreator thread. |
| 2972 | * We cannot access to the socket if the thread is different. |
| 2973 | */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2974 | if (socket->tid != tid) { |
| 2975 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2976 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2977 | } |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2978 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2979 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2980 | if (!hlua || channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2981 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2982 | lua_pushnil(L); |
| 2983 | lua_pushstring(L, "Can't connect"); |
| 2984 | return 2; |
| 2985 | } |
| 2986 | |
Willy Tarreau | 8e7c6e6 | 2022-05-18 17:58:02 +0200 | [diff] [blame] | 2987 | appctx = __sc_appctx(s->scf); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2988 | |
| 2989 | /* Check for connection established. */ |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2990 | if (csk_ctx->connected) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2991 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2992 | lua_pushinteger(L, 1); |
| 2993 | return 1; |
| 2994 | } |
| 2995 | |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2996 | if (!notification_new(&hlua->com, &csk_ctx->wake_on_write, hlua->task)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2997 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2998 | WILL_LJMP(luaL_error(L, "out of memory error")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2999 | } |
| 3000 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3001 | 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] | 3002 | return 0; |
| 3003 | } |
| 3004 | |
| 3005 | /* This function fail or initite the connection. */ |
| 3006 | __LJMP static int hlua_socket_connect(struct lua_State *L) |
| 3007 | { |
| 3008 | struct hlua_socket *socket; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 3009 | int port = -1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3010 | const char *ip; |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 3011 | struct hlua *hlua; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 3012 | struct hlua_csk_ctx *csk_ctx; |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 3013 | struct appctx *appctx; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 3014 | int low, high; |
| 3015 | struct sockaddr_storage *addr; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 3016 | struct xref *peer; |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 3017 | struct stconn *sc; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 3018 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3019 | |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 3020 | if (lua_gettop(L) < 2) |
| 3021 | WILL_LJMP(luaL_error(L, "connect: need at least 2 arguments")); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3022 | |
| 3023 | /* Get args. */ |
| 3024 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 3025 | |
| 3026 | /* Check if we run on the same thread than the xreator thread. |
| 3027 | * We cannot access to the socket if the thread is different. |
| 3028 | */ |
| 3029 | if (socket->tid != tid) |
| 3030 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 3031 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3032 | ip = MAY_LJMP(luaL_checkstring(L, 2)); |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 3033 | if (lua_gettop(L) >= 3) { |
| 3034 | luaL_Buffer b; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 3035 | port = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3036 | |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 3037 | /* Force the ip to end with a colon, to support IPv6 addresses |
| 3038 | * that are not enclosed within square brackets. |
| 3039 | */ |
| 3040 | if (port > 0) { |
| 3041 | luaL_buffinit(L, &b); |
| 3042 | luaL_addstring(&b, ip); |
| 3043 | luaL_addchar(&b, ':'); |
| 3044 | luaL_pushresult(&b); |
| 3045 | ip = lua_tolstring(L, lua_gettop(L), NULL); |
| 3046 | } |
| 3047 | } |
| 3048 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 3049 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 3050 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 3051 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 3052 | lua_pushnil(L); |
| 3053 | return 1; |
| 3054 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3055 | |
| 3056 | /* Parse ip address. */ |
Willy Tarreau | 5fc9328 | 2020-09-16 18:25:03 +0200 | [diff] [blame] | 3057 | 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] | 3058 | if (!addr) { |
| 3059 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 3060 | WILL_LJMP(luaL_error(L, "connect: cannot parse destination address '%s'", ip)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 3061 | } |
Willy Tarreau | 9da9a6f | 2019-07-17 14:49:44 +0200 | [diff] [blame] | 3062 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3063 | /* Set port. */ |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 3064 | if (low == 0) { |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 3065 | if (addr->ss_family == AF_INET) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 3066 | if (port == -1) { |
| 3067 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 3068 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 3069 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 3070 | ((struct sockaddr_in *)addr)->sin_port = htons(port); |
| 3071 | } else if (addr->ss_family == AF_INET6) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 3072 | if (port == -1) { |
| 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: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 3075 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 3076 | ((struct sockaddr_in6 *)addr)->sin6_port = htons(port); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 3077 | } |
| 3078 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 3079 | |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 3080 | csk_ctx = container_of(peer, struct hlua_csk_ctx, xref); |
| 3081 | appctx = csk_ctx->appctx; |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 3082 | sc = appctx_sc(appctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 3083 | s = __sc_strm(sc); |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 3084 | |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 3085 | if (!sockaddr_alloc(&sc_opposite(sc)->dst, addr, sizeof(*addr))) { |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 3086 | xref_unlock(&socket->xref, peer); |
| 3087 | WILL_LJMP(luaL_error(L, "connect: internal error")); |
| 3088 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3089 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 3090 | /* Get hlua struct, or NULL if we execute from main lua state */ |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 3091 | hlua = hlua_gethlua(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 3092 | if (!hlua) |
| 3093 | return 0; |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 3094 | |
| 3095 | /* inform the stream that we want to be notified whenever the |
| 3096 | * connection completes. |
| 3097 | */ |
Willy Tarreau | 90e8b45 | 2022-05-25 18:21:43 +0200 | [diff] [blame] | 3098 | applet_need_more_data(appctx); |
Willy Tarreau | 4164eb9 | 2022-05-25 15:42:03 +0200 | [diff] [blame] | 3099 | applet_have_more_data(appctx); |
Thierry FOURNIER | 8c8fbbe | 2015-09-26 17:02:35 +0200 | [diff] [blame] | 3100 | appctx_wakeup(appctx); |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 3101 | |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 3102 | hlua->gc_count++; |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 3103 | |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 3104 | if (!notification_new(&hlua->com, &csk_ctx->wake_on_write, hlua->task)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 3105 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 3106 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 3107 | } |
| 3108 | xref_unlock(&socket->xref, peer); |
PiBa-NL | 706d5ee | 2018-05-05 23:51:42 +0200 | [diff] [blame] | 3109 | |
| 3110 | task_wakeup(s->task, TASK_WOKEN_INIT); |
| 3111 | /* Return yield waiting for connection. */ |
| 3112 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3113 | 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] | 3114 | |
| 3115 | return 0; |
| 3116 | } |
| 3117 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 3118 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3119 | __LJMP static int hlua_socket_connect_ssl(struct lua_State *L) |
| 3120 | { |
| 3121 | struct hlua_socket *socket; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 3122 | struct xref *peer; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 3123 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3124 | |
| 3125 | MAY_LJMP(check_args(L, 3, "connect_ssl")); |
| 3126 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 3127 | |
| 3128 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 3129 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 3130 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 3131 | lua_pushnil(L); |
| 3132 | return 1; |
| 3133 | } |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 3134 | |
Willy Tarreau | 0698c80 | 2022-05-11 14:09:57 +0200 | [diff] [blame] | 3135 | s = appctx_strm(container_of(peer, struct hlua_csk_ctx, xref)->appctx); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 3136 | |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 3137 | s->target = &socket_ssl->obj_type; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 3138 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3139 | return MAY_LJMP(hlua_socket_connect(L)); |
| 3140 | } |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 3141 | #endif |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3142 | |
| 3143 | __LJMP static int hlua_socket_setoption(struct lua_State *L) |
| 3144 | { |
| 3145 | return 0; |
| 3146 | } |
| 3147 | |
| 3148 | __LJMP static int hlua_socket_settimeout(struct lua_State *L) |
| 3149 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3150 | struct hlua_socket *socket; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3151 | int tmout; |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 3152 | double dtmout; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 3153 | struct xref *peer; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 3154 | struct stream *s; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3155 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3156 | MAY_LJMP(check_args(L, 2, "settimeout")); |
| 3157 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3158 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 3159 | |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 3160 | /* convert the timeout to millis */ |
| 3161 | dtmout = MAY_LJMP(luaL_checknumber(L, 2)) * 1000; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3162 | |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 3163 | /* Check for negative values */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 3164 | if (dtmout < 0) |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 3165 | WILL_LJMP(luaL_error(L, "settimeout: cannot set negatives values")); |
| 3166 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 3167 | if (dtmout > INT_MAX) /* overflow check */ |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 3168 | 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] | 3169 | |
| 3170 | tmout = MS_TO_TICKS((int)dtmout); |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 3171 | if (tmout == 0) |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3172 | tmout++; /* very small timeouts are adjusted to a minimum of 1ms */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 3173 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 3174 | /* Check if we run on the same thread than the xreator thread. |
| 3175 | * We cannot access to the socket if the thread is different. |
| 3176 | */ |
| 3177 | if (socket->tid != tid) |
| 3178 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 3179 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 3180 | /* check for connection break. If some data were read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 3181 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 3182 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 3183 | hlua_pusherror(L, "socket: not yet initialised, you can't set timeouts."); |
| 3184 | WILL_LJMP(lua_error(L)); |
| 3185 | return 0; |
| 3186 | } |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 3187 | |
Willy Tarreau | 0698c80 | 2022-05-11 14:09:57 +0200 | [diff] [blame] | 3188 | s = appctx_strm(container_of(peer, struct hlua_csk_ctx, xref)->appctx); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 3189 | |
Cyril Bonté | 7bb6345 | 2018-08-17 23:51:02 +0200 | [diff] [blame] | 3190 | s->sess->fe->timeout.connect = tmout; |
Christopher Faulet | 5aaacfb | 2023-02-15 08:13:33 +0100 | [diff] [blame] | 3191 | s->scf->ioto = tmout; |
| 3192 | s->scb->ioto = tmout; |
Cyril Bonté | 7bb6345 | 2018-08-17 23:51:02 +0200 | [diff] [blame] | 3193 | |
| 3194 | s->task->expire = tick_add_ifset(now_ms, tmout); |
| 3195 | task_queue(s->task); |
| 3196 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 3197 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3198 | |
Thierry Fournier | e9636f1 | 2018-03-08 09:54:32 +0100 | [diff] [blame] | 3199 | lua_pushinteger(L, 1); |
Tim Duesterhus | 119a5f1 | 2018-01-06 19:16:25 +0100 | [diff] [blame] | 3200 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3201 | } |
| 3202 | |
| 3203 | __LJMP static int hlua_socket_new(lua_State *L) |
| 3204 | { |
| 3205 | struct hlua_socket *socket; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 3206 | struct hlua_csk_ctx *ctx; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3207 | struct appctx *appctx; |
| 3208 | |
| 3209 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 3210 | if (!lua_checkstack(L, 3)) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3211 | hlua_pusherror(L, "socket: full stack"); |
| 3212 | goto out_fail_conf; |
| 3213 | } |
| 3214 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 3215 | /* Create the object: obj[0] = userdata. */ |
| 3216 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3217 | socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket))); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 3218 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3219 | memset(socket, 0, sizeof(*socket)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 3220 | socket->tid = tid; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3221 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3222 | /* Check if the various memory pools are initialized. */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 3223 | if (!pool_head_stream || !pool_head_buffer) { |
Thierry FOURNIER | 4a6170c | 2015-03-09 17:07:10 +0100 | [diff] [blame] | 3224 | hlua_pusherror(L, "socket: uninitialized pools."); |
| 3225 | goto out_fail_conf; |
| 3226 | } |
| 3227 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3228 | /* Pop a class stream metatable and affect it to the userdata. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3229 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref); |
| 3230 | lua_setmetatable(L, -2); |
| 3231 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 3232 | /* Create the applet context */ |
Christopher Faulet | 6095d57 | 2022-05-16 17:09:48 +0200 | [diff] [blame] | 3233 | appctx = appctx_new_here(&update_applet, NULL); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 3234 | if (!appctx) { |
| 3235 | hlua_pusherror(L, "socket: out of memory"); |
Christopher Faulet | a9e8b39 | 2022-03-23 11:01:09 +0100 | [diff] [blame] | 3236 | goto out_fail_conf; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 3237 | } |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 3238 | ctx = applet_reserve_svcctx(appctx, sizeof(*ctx)); |
| 3239 | ctx->connected = 0; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 3240 | ctx->die = 0; |
| 3241 | LIST_INIT(&ctx->wake_on_write); |
| 3242 | LIST_INIT(&ctx->wake_on_read); |
Willy Tarreau | b2bf833 | 2015-04-04 15:58:58 +0200 | [diff] [blame] | 3243 | |
Christopher Faulet | 5f35a3e | 2022-05-12 15:31:05 +0200 | [diff] [blame] | 3244 | if (appctx_init(appctx) == -1) { |
| 3245 | hlua_pusherror(L, "socket: fail to init applet."); |
Christopher Faulet | 13a35e5 | 2021-12-20 15:34:16 +0100 | [diff] [blame] | 3246 | goto out_fail_appctx; |
| 3247 | } |
| 3248 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 3249 | /* Initialise cross reference between stream and Lua socket object. */ |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 3250 | xref_create(&socket->xref, &ctx->xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3251 | return 1; |
| 3252 | |
Christopher Faulet | 13a35e5 | 2021-12-20 15:34:16 +0100 | [diff] [blame] | 3253 | out_fail_appctx: |
Christopher Faulet | 5f35a3e | 2022-05-12 15:31:05 +0200 | [diff] [blame] | 3254 | appctx_free_on_early_error(appctx); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 3255 | out_fail_conf: |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3256 | WILL_LJMP(lua_error(L)); |
| 3257 | return 0; |
| 3258 | } |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 3259 | |
| 3260 | /* |
| 3261 | * |
| 3262 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3263 | * Class Channel |
| 3264 | * |
| 3265 | * |
| 3266 | */ |
| 3267 | |
| 3268 | /* Returns the struct hlua_channel join to the class channel in the |
| 3269 | * stack entry "ud" or throws an argument error. |
| 3270 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3271 | __LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3272 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3273 | return MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3274 | } |
| 3275 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3276 | /* Pushes the channel onto the top of the stack. If the stask does not have a |
| 3277 | * free slots, the function fails and returns 0; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3278 | */ |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 3279 | static int hlua_channel_new(lua_State *L, struct channel *channel) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3280 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3281 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 3282 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3283 | return 0; |
| 3284 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 3285 | lua_newtable(L); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3286 | lua_pushlightuserdata(L, channel); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 3287 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3288 | |
| 3289 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 3290 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref); |
| 3291 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3292 | return 1; |
| 3293 | } |
| 3294 | |
Christopher Faulet | 9f55a50 | 2020-02-25 15:21:02 +0100 | [diff] [blame] | 3295 | /* Helper function returning a filter attached to a channel at the position <ud> |
| 3296 | * 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] | 3297 | * filter is attached, NULL is returned and <offset> and <len> are not |
Christopher Faulet | 9f55a50 | 2020-02-25 15:21:02 +0100 | [diff] [blame] | 3298 | * initialized. |
| 3299 | */ |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3300 | 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] | 3301 | { |
| 3302 | struct filter *filter = NULL; |
| 3303 | |
| 3304 | if (lua_getfield(L, ud, "__filter") == LUA_TLIGHTUSERDATA) { |
| 3305 | struct hlua_flt_ctx *flt_ctx; |
| 3306 | |
| 3307 | filter = lua_touserdata (L, -1); |
| 3308 | flt_ctx = filter->ctx; |
| 3309 | if (hlua_filter_from_payload(filter)) { |
| 3310 | *offset = flt_ctx->cur_off[CHN_IDX(chn)]; |
| 3311 | *len = flt_ctx->cur_len[CHN_IDX(chn)]; |
| 3312 | } |
| 3313 | } |
| 3314 | |
| 3315 | lua_pop(L, 1); |
| 3316 | return filter; |
| 3317 | } |
| 3318 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3319 | /* Copies <len> bytes of data present in the channel's buffer, starting at the |
| 3320 | * 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] | 3321 | * responsibility to ensure <len> and <offset> are valid. It always return the |
| 3322 | * length of the built string. <len> may be 0, in this case, an empty string is |
| 3323 | * created and 0 is returned. |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3324 | */ |
| 3325 | 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] | 3326 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3327 | size_t block1, block2; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3328 | luaL_Buffer b; |
| 3329 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3330 | block1 = len; |
| 3331 | if (block1 > b_contig_data(&chn->buf, b_peek_ofs(&chn->buf, offset))) |
| 3332 | block1 = b_contig_data(&chn->buf, b_peek_ofs(&chn->buf, offset)); |
| 3333 | block2 = len - block1; |
| 3334 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3335 | luaL_buffinit(L, &b); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3336 | luaL_addlstring(&b, b_peek(&chn->buf, offset), block1); |
| 3337 | if (block2) |
| 3338 | luaL_addlstring(&b, b_orig(&chn->buf), block2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3339 | luaL_pushresult(&b); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3340 | return len; |
| 3341 | } |
| 3342 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3343 | /* Inserts the string <str> to the channel's buffer at the offset <offset>. This |
| 3344 | * function returns -1 if data cannot be copied. Otherwise, it returns the |
| 3345 | * number of bytes copied. |
| 3346 | */ |
| 3347 | static int _hlua_channel_insert(struct channel *chn, lua_State *L, struct ist str, size_t offset) |
| 3348 | { |
| 3349 | int ret = 0; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3350 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3351 | /* Nothing to do, just return */ |
| 3352 | if (unlikely(istlen(str) == 0)) |
| 3353 | goto end; |
| 3354 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3355 | if (istlen(str) > c_room(chn)) { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3356 | ret = -1; |
| 3357 | goto end; |
| 3358 | } |
| 3359 | ret = b_insert_blk(&chn->buf, offset, istptr(str), istlen(str)); |
| 3360 | |
| 3361 | end: |
| 3362 | return ret; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3363 | } |
| 3364 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3365 | /* Removes <len> bytes of data at the absolute position <offset>. |
| 3366 | */ |
| 3367 | static void _hlua_channel_delete(struct channel *chn, size_t offset, size_t len) |
| 3368 | { |
| 3369 | size_t end = offset + len; |
| 3370 | |
| 3371 | if (b_peek(&chn->buf, end) != b_tail(&chn->buf)) |
| 3372 | b_move(&chn->buf, b_peek_ofs(&chn->buf, end), |
| 3373 | b_data(&chn->buf) - end, -len); |
| 3374 | b_sub(&chn->buf, len); |
| 3375 | } |
| 3376 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3377 | /* Copies input data in the channel's buffer. It is possible to set a specific |
| 3378 | * offset (0 by default) and a length (all remaining input data starting for the |
| 3379 | * offset by default). If there is not enough input data and more data can be |
| 3380 | * received, this function yields. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3381 | * |
| 3382 | * From an action, All input data are considered. For a filter, the offset and |
| 3383 | * 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] | 3384 | */ |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3385 | __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] | 3386 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3387 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3388 | struct filter *filter; |
| 3389 | size_t input, output; |
| 3390 | int offset, len; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3391 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3392 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3393 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3394 | output = co_data(chn); |
| 3395 | input = ci_data(chn); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3396 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3397 | filter = hlua_channel_filter(L, 1, chn, &output, &input); |
| 3398 | if (filter && !hlua_filter_from_payload(filter)) |
| 3399 | WILL_LJMP(lua_error(L)); |
| 3400 | |
| 3401 | offset = output; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3402 | if (lua_gettop(L) > 1) { |
| 3403 | offset = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3404 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 3405 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3406 | offset += output; |
| 3407 | if (offset < output || offset > input + output) { |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3408 | lua_pushfstring(L, "offset out of range."); |
| 3409 | WILL_LJMP(lua_error(L)); |
| 3410 | } |
| 3411 | } |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3412 | len = output + input - offset; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3413 | if (lua_gettop(L) == 3) { |
| 3414 | len = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3415 | if (!len) |
| 3416 | goto dup; |
| 3417 | if (len == -1) |
| 3418 | len = global.tune.bufsize; |
| 3419 | if (len < 0) { |
| 3420 | lua_pushfstring(L, "length out of range."); |
| 3421 | WILL_LJMP(lua_error(L)); |
| 3422 | } |
| 3423 | } |
| 3424 | |
Christopher Faulet | 0ae2e63 | 2023-01-10 15:29:54 +0100 | [diff] [blame] | 3425 | /* Wait for more data if possible if no length was specified and there |
| 3426 | * is no data or not enough data was received. |
| 3427 | */ |
| 3428 | if (!len || offset + len > output + input) { |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3429 | if (!HLUA_CANT_YIELD(hlua_gethlua(L)) && !channel_input_closed(chn) && channel_may_recv(chn)) { |
| 3430 | /* Yield waiting for more data, as requested */ |
| 3431 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_get_data_yield, TICK_ETERNITY, 0)); |
| 3432 | } |
Christopher Faulet | 0ae2e63 | 2023-01-10 15:29:54 +0100 | [diff] [blame] | 3433 | |
| 3434 | /* Return 'nil' if there is no data and the channel can't receive more data */ |
| 3435 | if (!len) { |
| 3436 | lua_pushnil(L); |
| 3437 | return -1; |
| 3438 | } |
| 3439 | |
| 3440 | /* Otherwise, return all data */ |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3441 | len = output + input - offset; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3442 | } |
| 3443 | |
| 3444 | dup: |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3445 | _hlua_channel_dup(chn, L, offset, len); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3446 | return 1; |
| 3447 | } |
| 3448 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3449 | /* Copies the first line (including the trailing LF) of input data in the |
| 3450 | * channel's buffer. It is possible to set a specific offset (0 by default) and |
| 3451 | * a length (all remaining input data starting for the offset by default). If |
| 3452 | * there is not enough input data and more data can be received, the function |
| 3453 | * yields. If a length is explicitly specified, no more data are |
| 3454 | * copied. Otherwise, if no LF is found and more data can be received, this |
| 3455 | * function yields. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3456 | * |
| 3457 | * From an action, All input data are considered. For a filter, the offset and |
| 3458 | * 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] | 3459 | */ |
| 3460 | __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] | 3461 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3462 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3463 | struct filter *filter; |
| 3464 | size_t l, input, output; |
| 3465 | int offset, len; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3466 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3467 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3468 | output = co_data(chn); |
| 3469 | input = ci_data(chn); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3470 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3471 | filter = hlua_channel_filter(L, 1, chn, &output, &input); |
| 3472 | if (filter && !hlua_filter_from_payload(filter)) |
| 3473 | WILL_LJMP(lua_error(L)); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3474 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3475 | offset = output; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3476 | if (lua_gettop(L) > 1) { |
| 3477 | offset = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3478 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 3479 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3480 | offset += output; |
| 3481 | if (offset < output || offset > input + output) { |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3482 | lua_pushfstring(L, "offset out of range."); |
| 3483 | WILL_LJMP(lua_error(L)); |
| 3484 | } |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3485 | } |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3486 | |
| 3487 | len = output + input - offset; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3488 | if (lua_gettop(L) == 3) { |
| 3489 | len = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3490 | if (!len) |
| 3491 | goto dup; |
| 3492 | if (len == -1) |
| 3493 | len = global.tune.bufsize; |
| 3494 | if (len < 0) { |
| 3495 | lua_pushfstring(L, "length out of range."); |
| 3496 | WILL_LJMP(lua_error(L)); |
| 3497 | } |
| 3498 | } |
| 3499 | |
| 3500 | for (l = 0; l < len; l++) { |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3501 | if (l + offset >= output + input) |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3502 | break; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3503 | if (*(b_peek(&chn->buf, offset + l)) == '\n') { |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3504 | len = l+1; |
| 3505 | goto dup; |
| 3506 | } |
| 3507 | } |
| 3508 | |
Christopher Faulet | 0ae2e63 | 2023-01-10 15:29:54 +0100 | [diff] [blame] | 3509 | /* Wait for more data if possible if no line is found and no length was |
| 3510 | * specified or not enough data was received. |
| 3511 | */ |
| 3512 | if (lua_gettop(L) != 3 || offset + len > output + input) { |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3513 | if (!HLUA_CANT_YIELD(hlua_gethlua(L)) && !channel_input_closed(chn) && channel_may_recv(chn)) { |
| 3514 | /* Yield waiting for more data */ |
| 3515 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_get_line_yield, TICK_ETERNITY, 0)); |
| 3516 | } |
Christopher Faulet | 0ae2e63 | 2023-01-10 15:29:54 +0100 | [diff] [blame] | 3517 | |
| 3518 | /* Return 'nil' if there is no data and the channel can't receive more data */ |
| 3519 | if (!len) { |
| 3520 | lua_pushnil(L); |
| 3521 | return -1; |
| 3522 | } |
| 3523 | |
| 3524 | /* Otherwise, return all data */ |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3525 | len = output + input - offset; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3526 | } |
| 3527 | |
| 3528 | dup: |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3529 | _hlua_channel_dup(chn, L, offset, len); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3530 | return 1; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3531 | } |
| 3532 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3533 | /* [ DEPRECATED ] |
| 3534 | * |
| 3535 | * Duplicate all input data foud in the channel's buffer. The data are not |
| 3536 | * removed from the buffer. This function relies on _hlua_channel_dup(). |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3537 | * |
| 3538 | * From an action, All input data are considered. For a filter, the offset and |
| 3539 | * 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] | 3540 | */ |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3541 | __LJMP static int hlua_channel_dup(lua_State *L) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3542 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3543 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3544 | struct filter *filter; |
| 3545 | size_t offset, len; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3546 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3547 | MAY_LJMP(check_args(L, 1, "dup")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3548 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3549 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3550 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3551 | WILL_LJMP(lua_error(L)); |
| 3552 | } |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3553 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3554 | offset = co_data(chn); |
| 3555 | len = ci_data(chn); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3556 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3557 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 3558 | if (filter && !hlua_filter_from_payload(filter)) |
| 3559 | WILL_LJMP(lua_error(L)); |
| 3560 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3561 | if (!ci_data(chn) && channel_input_closed(chn)) { |
| 3562 | lua_pushnil(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3563 | return 1; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3564 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3565 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3566 | _hlua_channel_dup(chn, L, offset, len); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3567 | return 1; |
| 3568 | } |
| 3569 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3570 | /* [ DEPRECATED ] |
| 3571 | * |
| 3572 | * Get all input data foud in the channel's buffer. The data are removed from |
| 3573 | * the buffer after the copy. This function relies on _hlua_channel_dup() and |
| 3574 | * _hlua_channel_delete(). |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3575 | * |
| 3576 | * From an action, All input data are considered. For a filter, the offset and |
| 3577 | * 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] | 3578 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3579 | __LJMP static int hlua_channel_get(lua_State *L) |
| 3580 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3581 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3582 | struct filter *filter; |
| 3583 | size_t offset, len; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3584 | int ret; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3585 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3586 | MAY_LJMP(check_args(L, 1, "get")); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3587 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3588 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3589 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3590 | WILL_LJMP(lua_error(L)); |
| 3591 | } |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3592 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3593 | offset = co_data(chn); |
| 3594 | len = ci_data(chn); |
| 3595 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3596 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 3597 | if (filter && !hlua_filter_from_payload(filter)) |
| 3598 | WILL_LJMP(lua_error(L)); |
| 3599 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3600 | if (!ci_data(chn) && channel_input_closed(chn)) { |
| 3601 | lua_pushnil(L); |
| 3602 | return 1; |
| 3603 | } |
| 3604 | |
| 3605 | ret = _hlua_channel_dup(chn, L, offset, len); |
| 3606 | _hlua_channel_delete(chn, offset, ret); |
| 3607 | return 1; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3608 | } |
| 3609 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3610 | /* This functions consumes and returns one line. If the channel is closed, |
| 3611 | * 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] | 3612 | * 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] | 3613 | * value. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3614 | * |
| 3615 | * From an action, All input data are considered. For a filter, the offset and |
| 3616 | * 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] | 3617 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3618 | __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] | 3619 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3620 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3621 | struct filter *filter; |
| 3622 | size_t l, offset, len; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3623 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3624 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3625 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3626 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3627 | offset = co_data(chn); |
| 3628 | len = ci_data(chn); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3629 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3630 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 3631 | if (filter && !hlua_filter_from_payload(filter)) |
| 3632 | WILL_LJMP(lua_error(L)); |
| 3633 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3634 | if (!ci_data(chn) && channel_input_closed(chn)) { |
| 3635 | lua_pushnil(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3636 | return 1; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3637 | } |
| 3638 | |
| 3639 | for (l = 0; l < len; l++) { |
| 3640 | if (*(b_peek(&chn->buf, offset+l)) == '\n') { |
| 3641 | len = l+1; |
| 3642 | goto dup; |
| 3643 | } |
| 3644 | } |
| 3645 | |
| 3646 | if (!HLUA_CANT_YIELD(hlua_gethlua(L)) && !channel_input_closed(chn) && channel_may_recv(chn)) { |
| 3647 | /* Yield waiting for more data */ |
| 3648 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_getline_yield, TICK_ETERNITY, 0)); |
| 3649 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3650 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3651 | dup: |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3652 | ret = _hlua_channel_dup(chn, L, offset, len); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3653 | _hlua_channel_delete(chn, offset, ret); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3654 | return 1; |
| 3655 | } |
| 3656 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3657 | /* [ DEPRECATED ] |
| 3658 | * |
| 3659 | * Check arguments for the function "hlua_channel_getline_yield". |
| 3660 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3661 | __LJMP static int hlua_channel_getline(lua_State *L) |
| 3662 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3663 | struct channel *chn; |
| 3664 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3665 | MAY_LJMP(check_args(L, 1, "getline")); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3666 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3667 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3668 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3669 | WILL_LJMP(lua_error(L)); |
| 3670 | } |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3671 | return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0)); |
| 3672 | } |
| 3673 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3674 | /* Retrieves a given amount of input data at the given offset. By default all |
| 3675 | * available input data are returned. The offset may be negactive to start from |
| 3676 | * the end of input data. The length may be -1 to set it to the maximum buffer |
| 3677 | * size. |
| 3678 | */ |
| 3679 | __LJMP static int hlua_channel_get_data(lua_State *L) |
| 3680 | { |
| 3681 | struct channel *chn; |
| 3682 | |
| 3683 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 3684 | WILL_LJMP(luaL_error(L, "'data' expects at most 2 arguments")); |
| 3685 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3686 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3687 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3688 | WILL_LJMP(lua_error(L)); |
| 3689 | } |
| 3690 | return MAY_LJMP(hlua_channel_get_data_yield(L, 0, 0)); |
| 3691 | } |
| 3692 | |
| 3693 | /* Retrieves a given amount of input data at the given offset. By default all |
| 3694 | * available input data are returned. The offset may be negactive to start from |
| 3695 | * the end of input data. The length may be -1 to set it to the maximum buffer |
| 3696 | * size. |
| 3697 | */ |
| 3698 | __LJMP static int hlua_channel_get_line(lua_State *L) |
| 3699 | { |
| 3700 | struct channel *chn; |
| 3701 | |
| 3702 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 3703 | WILL_LJMP(luaL_error(L, "'line' expects at most 2 arguments")); |
| 3704 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3705 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3706 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3707 | WILL_LJMP(lua_error(L)); |
| 3708 | } |
| 3709 | return MAY_LJMP(hlua_channel_get_line_yield(L, 0, 0)); |
| 3710 | } |
| 3711 | |
| 3712 | /* Appends a string into the input side of channel. It returns the length of the |
| 3713 | * written string, or -1 if the channel is closed or if the buffer size is too |
| 3714 | * little for the data. 0 may be returned if nothing is copied. This function |
| 3715 | * does not yield. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3716 | * |
| 3717 | * For a filter, the context is updated on success. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3718 | */ |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3719 | __LJMP static int hlua_channel_append(lua_State *L) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3720 | { |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3721 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3722 | struct filter *filter; |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3723 | const char *str; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3724 | size_t sz, offset, len; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3725 | int ret; |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3726 | |
| 3727 | MAY_LJMP(check_args(L, 2, "append")); |
| 3728 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3729 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 3730 | if (IS_HTX_STRM(chn_strm(chn))) { |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3731 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3732 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3733 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3734 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3735 | offset = co_data(chn); |
| 3736 | len = ci_data(chn); |
| 3737 | |
| 3738 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 3739 | if (filter && !hlua_filter_from_payload(filter)) |
| 3740 | WILL_LJMP(lua_error(L)); |
| 3741 | |
| 3742 | ret = _hlua_channel_insert(chn, L, ist2(str, sz), offset); |
| 3743 | if (ret > 0 && filter) { |
| 3744 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 3745 | |
| 3746 | flt_update_offsets(filter, chn, ret); |
| 3747 | flt_ctx->cur_len[CHN_IDX(chn)] += ret; |
| 3748 | } |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3749 | lua_pushinteger(L, ret); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3750 | return 1; |
| 3751 | } |
| 3752 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3753 | /* Prepends a string into the input side of channel. It returns the length of the |
| 3754 | * written string, or -1 if the channel is closed or if the buffer size is too |
| 3755 | * little for the data. 0 may be returned if nothing is copied. This function |
| 3756 | * does not yield. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3757 | * |
| 3758 | * For a filter, the context is updated on success. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3759 | */ |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3760 | __LJMP static int hlua_channel_prepend(lua_State *L) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3761 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3762 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3763 | struct filter *filter; |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3764 | const char *str; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3765 | size_t sz, offset, len; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3766 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3767 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3768 | MAY_LJMP(check_args(L, 2, "prepend")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3769 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3770 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 3771 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3772 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3773 | WILL_LJMP(lua_error(L)); |
| 3774 | } |
| 3775 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3776 | offset = co_data(chn); |
| 3777 | len = ci_data(chn); |
| 3778 | |
| 3779 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 3780 | if (filter && !hlua_filter_from_payload(filter)) |
| 3781 | WILL_LJMP(lua_error(L)); |
| 3782 | |
| 3783 | ret = _hlua_channel_insert(chn, L, ist2(str, sz), offset); |
| 3784 | if (ret > 0 && filter) { |
| 3785 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 3786 | |
| 3787 | flt_update_offsets(filter, chn, ret); |
| 3788 | flt_ctx->cur_len[CHN_IDX(chn)] += ret; |
| 3789 | } |
| 3790 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3791 | lua_pushinteger(L, ret); |
| 3792 | return 1; |
| 3793 | } |
| 3794 | |
| 3795 | /* 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] | 3796 | * 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] | 3797 | * returns the length of the written string, or -1 if the channel is closed or |
| 3798 | * if the buffer size is too little for the data. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3799 | * |
| 3800 | * For a filter, the context is updated on success. |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3801 | */ |
| 3802 | __LJMP static int hlua_channel_insert_data(lua_State *L) |
| 3803 | { |
| 3804 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3805 | struct filter *filter; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3806 | const char *str; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3807 | size_t sz, input, output; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3808 | int ret, offset; |
| 3809 | |
| 3810 | if (lua_gettop(L) < 2 || lua_gettop(L) > 3) |
| 3811 | WILL_LJMP(luaL_error(L, "'insert' expects at least 1 argument and at most 2 arguments")); |
| 3812 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3813 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3814 | |
| 3815 | output = co_data(chn); |
| 3816 | input = ci_data(chn); |
| 3817 | |
| 3818 | filter = hlua_channel_filter(L, 1, chn, &output, &input); |
| 3819 | if (filter && !hlua_filter_from_payload(filter)) |
| 3820 | WILL_LJMP(lua_error(L)); |
| 3821 | |
Aurelien DARRAGON | afb7daf | 2022-10-04 12:16:05 +0200 | [diff] [blame] | 3822 | offset = output; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3823 | if (lua_gettop(L) > 2) { |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3824 | offset = MAY_LJMP(luaL_checkinteger(L, 3)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3825 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 3826 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3827 | offset += output; |
Aurelien DARRAGON | afb7daf | 2022-10-04 12:16:05 +0200 | [diff] [blame] | 3828 | if (offset > output + input) { |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3829 | lua_pushfstring(L, "offset out of range."); |
| 3830 | WILL_LJMP(lua_error(L)); |
| 3831 | } |
| 3832 | } |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3833 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3834 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3835 | WILL_LJMP(lua_error(L)); |
| 3836 | } |
| 3837 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3838 | ret = _hlua_channel_insert(chn, L, ist2(str, sz), offset); |
| 3839 | if (ret > 0 && filter) { |
| 3840 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3841 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3842 | flt_update_offsets(filter, chn, ret); |
| 3843 | flt_ctx->cur_len[CHN_IDX(chn)] += ret; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3844 | } |
| 3845 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3846 | lua_pushinteger(L, ret); |
| 3847 | return 1; |
| 3848 | } |
| 3849 | /* Replaces a given amount of input data at the given offset by a string |
| 3850 | * content. By default all remaining data are removed (offset = 0 and len = |
| 3851 | * -1). It returns the length of the written string, or -1 if the channel is |
| 3852 | * closed or if the buffer size is too little for the data. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3853 | * |
| 3854 | * For a filter, the context is updated on success. |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3855 | */ |
| 3856 | __LJMP static int hlua_channel_set_data(lua_State *L) |
| 3857 | { |
| 3858 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3859 | struct filter *filter; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3860 | const char *str; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3861 | size_t sz, input, output; |
| 3862 | int ret, offset, len; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3863 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3864 | if (lua_gettop(L) < 2 || lua_gettop(L) > 4) |
| 3865 | WILL_LJMP(luaL_error(L, "'set' expects at least 1 argument and at most 3 arguments")); |
| 3866 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3867 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3868 | |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 3869 | if (IS_HTX_STRM(chn_strm(chn))) { |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3870 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3871 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3872 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3873 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3874 | output = co_data(chn); |
| 3875 | input = ci_data(chn); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3876 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3877 | filter = hlua_channel_filter(L, 1, chn, &output, &input); |
| 3878 | if (filter && !hlua_filter_from_payload(filter)) |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3879 | WILL_LJMP(lua_error(L)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3880 | |
| 3881 | offset = output; |
| 3882 | if (lua_gettop(L) > 2) { |
| 3883 | offset = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3884 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 3885 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3886 | offset += output; |
| 3887 | if (offset < output || offset > input + output) { |
| 3888 | lua_pushfstring(L, "offset out of range."); |
| 3889 | WILL_LJMP(lua_error(L)); |
| 3890 | } |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3891 | } |
| 3892 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3893 | len = output + input - offset; |
| 3894 | if (lua_gettop(L) == 4) { |
| 3895 | len = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 3896 | if (!len) |
| 3897 | goto set; |
| 3898 | if (len == -1) |
| 3899 | len = output + input - offset; |
| 3900 | if (len < 0 || offset + len > output + input) { |
| 3901 | lua_pushfstring(L, "length out of range."); |
| 3902 | WILL_LJMP(lua_error(L)); |
| 3903 | } |
| 3904 | } |
| 3905 | |
| 3906 | set: |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3907 | /* 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] | 3908 | if (sz > c_room(chn) + len) |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3909 | lua_pushinteger(L, -1); |
| 3910 | else { |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3911 | _hlua_channel_delete(chn, offset, len); |
| 3912 | ret = _hlua_channel_insert(chn, L, ist2(str, sz), offset); |
| 3913 | if (filter) { |
| 3914 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 3915 | |
| 3916 | len -= (ret > 0 ? ret : 0); |
| 3917 | flt_update_offsets(filter, chn, -len); |
| 3918 | flt_ctx->cur_len[CHN_IDX(chn)] -= len; |
| 3919 | } |
| 3920 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3921 | lua_pushinteger(L, ret); |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3922 | } |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3923 | return 1; |
| 3924 | } |
| 3925 | |
| 3926 | /* 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] | 3927 | * 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] | 3928 | * the removed data. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3929 | * |
| 3930 | * For a filter, the context is updated on success. |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3931 | */ |
| 3932 | __LJMP static int hlua_channel_del_data(lua_State *L) |
| 3933 | { |
| 3934 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3935 | struct filter *filter; |
| 3936 | size_t input, output; |
| 3937 | int offset, len; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3938 | |
| 3939 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 3940 | WILL_LJMP(luaL_error(L, "'remove' expects at most 2 arguments")); |
| 3941 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3942 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3943 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3944 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3945 | WILL_LJMP(lua_error(L)); |
| 3946 | } |
| 3947 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3948 | output = co_data(chn); |
| 3949 | input = ci_data(chn); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3950 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3951 | filter = hlua_channel_filter(L, 1, chn, &output, &input); |
| 3952 | if (filter && !hlua_filter_from_payload(filter)) |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3953 | WILL_LJMP(lua_error(L)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3954 | |
| 3955 | offset = output; |
Boyang Li | e0c5435 | 2022-05-10 17:47:23 +0000 | [diff] [blame] | 3956 | if (lua_gettop(L) > 1) { |
| 3957 | offset = MAY_LJMP(luaL_checkinteger(L, 2)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3958 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 3959 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3960 | offset += output; |
| 3961 | if (offset < output || offset > input + output) { |
| 3962 | lua_pushfstring(L, "offset out of range."); |
| 3963 | WILL_LJMP(lua_error(L)); |
| 3964 | } |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3965 | } |
| 3966 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3967 | len = output + input - offset; |
Boyang Li | e0c5435 | 2022-05-10 17:47:23 +0000 | [diff] [blame] | 3968 | if (lua_gettop(L) == 3) { |
| 3969 | len = MAY_LJMP(luaL_checkinteger(L, 3)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3970 | if (!len) |
| 3971 | goto end; |
| 3972 | if (len == -1) |
| 3973 | len = output + input - offset; |
| 3974 | if (len < 0 || offset + len > output + input) { |
| 3975 | lua_pushfstring(L, "length out of range."); |
| 3976 | WILL_LJMP(lua_error(L)); |
| 3977 | } |
| 3978 | } |
| 3979 | |
| 3980 | _hlua_channel_delete(chn, offset, len); |
| 3981 | if (filter) { |
| 3982 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 3983 | |
| 3984 | flt_update_offsets(filter, chn, -len); |
| 3985 | flt_ctx->cur_len[CHN_IDX(chn)] -= len; |
| 3986 | } |
| 3987 | |
| 3988 | end: |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3989 | lua_pushinteger(L, len); |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3990 | return 1; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3991 | } |
| 3992 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3993 | /* Append data in the output side of the buffer. This data is immediately |
| 3994 | * sent. The function returns the amount of data written. If the buffer |
| 3995 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3996 | * if the channel is closed. |
| 3997 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3998 | __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] | 3999 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 4000 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4001 | struct filter *filter; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 4002 | const char *str; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4003 | size_t offset, len, sz; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 4004 | int l, ret; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 4005 | struct hlua *hlua; |
| 4006 | |
| 4007 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 4008 | hlua = hlua_gethlua(L); |
| 4009 | if (!hlua) { |
| 4010 | lua_pushnil(L); |
| 4011 | return 1; |
| 4012 | } |
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 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 4015 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 4016 | l = MAY_LJMP(luaL_checkinteger(L, 3)); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 4017 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4018 | offset = co_data(chn); |
| 4019 | len = ci_data(chn); |
| 4020 | |
| 4021 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 4022 | if (filter && !hlua_filter_from_payload(filter)) |
| 4023 | WILL_LJMP(lua_error(L)); |
| 4024 | |
| 4025 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 4026 | if (unlikely(channel_output_closed(chn))) { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4027 | lua_pushinteger(L, -1); |
| 4028 | return 1; |
| 4029 | } |
| 4030 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 4031 | len = c_room(chn); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4032 | if (len > sz -l) { |
| 4033 | if (filter) { |
| 4034 | lua_pushinteger(L, -1); |
| 4035 | return 1; |
| 4036 | } |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 4037 | len = sz - l; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4038 | } |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 4039 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4040 | ret = _hlua_channel_insert(chn, L, ist2(str, len), offset); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 4041 | if (ret == -1) { |
| 4042 | lua_pop(L, 1); |
| 4043 | lua_pushinteger(L, -1); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 4044 | return 1; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 4045 | } |
| 4046 | if (ret) { |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4047 | if (filter) { |
| 4048 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 4049 | |
| 4050 | |
| 4051 | flt_update_offsets(filter, chn, ret); |
| 4052 | FLT_OFF(filter, chn) += ret; |
| 4053 | flt_ctx->cur_off[CHN_IDX(chn)] += ret; |
| 4054 | } |
| 4055 | else |
| 4056 | c_adv(chn, ret); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4057 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 4058 | l += ret; |
| 4059 | lua_pop(L, 1); |
| 4060 | lua_pushinteger(L, l); |
| 4061 | } |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4062 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 4063 | if (l < sz) { |
| 4064 | /* Yield only if the channel's output is not empty. |
| 4065 | * Otherwise it means we cannot add more data. */ |
| 4066 | if (co_data(chn) == 0 || HLUA_CANT_YIELD(hlua_gethlua(L))) |
Christopher Faulet | 2e60aa4 | 2021-08-05 11:58:37 +0200 | [diff] [blame] | 4067 | return 1; |
| 4068 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 4069 | /* If we are waiting for space in the response buffer, we |
| 4070 | * must set the flag WAKERESWR. This flag required the task |
| 4071 | * wake up if any activity is detected on the response buffer. |
| 4072 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 4073 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 4074 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 4075 | else |
| 4076 | HLUA_SET_WAKEREQWR(hlua); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4077 | 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] | 4078 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4079 | |
| 4080 | return 1; |
| 4081 | } |
| 4082 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4083 | /* Just a wrapper of "_hlua_channel_send". This wrapper permits |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4084 | * yield the LUA process, and resume it without checking the |
| 4085 | * input arguments. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4086 | * |
| 4087 | * This function cannot be called from a filter. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4088 | */ |
| 4089 | __LJMP static int hlua_channel_send(lua_State *L) |
| 4090 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 4091 | struct channel *chn; |
| 4092 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4093 | MAY_LJMP(check_args(L, 2, "send")); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 4094 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 4095 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 4096 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 4097 | WILL_LJMP(lua_error(L)); |
| 4098 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4099 | lua_pushinteger(L, 0); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 4100 | return MAY_LJMP(hlua_channel_send_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4101 | } |
| 4102 | |
| 4103 | /* This function forward and amount of butes. The data pass from |
| 4104 | * the input side of the buffer to the output side, and can be |
| 4105 | * forwarded. This function never fails. |
| 4106 | * |
| 4107 | * The Lua function takes an amount of bytes to be forwarded in |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4108 | * input. It returns the number of bytes forwarded. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4109 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 4110 | __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] | 4111 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 4112 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4113 | struct filter *filter; |
| 4114 | size_t offset, len, fwd; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 4115 | int l, max; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 4116 | struct hlua *hlua; |
| 4117 | |
| 4118 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 4119 | hlua = hlua_gethlua(L); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 4120 | if (!hlua) { |
| 4121 | lua_pushnil(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 4122 | return 1; |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 4123 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 4124 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 4125 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4126 | fwd = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4127 | l = MAY_LJMP(luaL_checkinteger(L, -1)); |
| 4128 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4129 | offset = co_data(chn); |
| 4130 | len = ci_data(chn); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 4131 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4132 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 4133 | if (filter && !hlua_filter_from_payload(filter)) |
| 4134 | WILL_LJMP(lua_error(L)); |
| 4135 | |
| 4136 | max = fwd - l; |
| 4137 | if (max > len) |
| 4138 | max = len; |
| 4139 | |
| 4140 | if (filter) { |
| 4141 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 4142 | |
| 4143 | FLT_OFF(filter, chn) += max; |
| 4144 | flt_ctx->cur_off[CHN_IDX(chn)] += max; |
| 4145 | flt_ctx->cur_len[CHN_IDX(chn)] -= max; |
| 4146 | } |
| 4147 | else |
| 4148 | channel_forward(chn, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4149 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 4150 | l += max; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4151 | lua_pop(L, 1); |
| 4152 | lua_pushinteger(L, l); |
| 4153 | |
| 4154 | /* Check if it miss bytes to forward. */ |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4155 | if (l < fwd) { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4156 | /* The the input channel or the output channel are closed, we |
| 4157 | * must return the amount of data forwarded. |
| 4158 | */ |
Christopher Faulet | 2e60aa4 | 2021-08-05 11:58:37 +0200 | [diff] [blame] | 4159 | 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] | 4160 | return 1; |
| 4161 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 4162 | /* If we are waiting for space data in the response buffer, we |
| 4163 | * must set the flag WAKERESWR. This flag required the task |
| 4164 | * wake up if any activity is detected on the response buffer. |
| 4165 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 4166 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 4167 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 4168 | else |
| 4169 | HLUA_SET_WAKEREQWR(hlua); |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 4170 | |
Ilya Shipitsin | 4a689da | 2022-10-29 09:34:32 +0500 | [diff] [blame] | 4171 | /* Otherwise, we can yield waiting for new data in the input side. */ |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4172 | 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] | 4173 | } |
| 4174 | |
| 4175 | return 1; |
| 4176 | } |
| 4177 | |
| 4178 | /* Just check the input and prepare the stack for the previous |
| 4179 | * function "hlua_channel_forward_yield" |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4180 | * |
| 4181 | * This function cannot be called from a filter. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4182 | */ |
| 4183 | __LJMP static int hlua_channel_forward(lua_State *L) |
| 4184 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 4185 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4186 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 4187 | MAY_LJMP(check_args(L, 2, "forward")); |
| 4188 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 4189 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 4190 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 4191 | WILL_LJMP(lua_error(L)); |
| 4192 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4193 | lua_pushinteger(L, 0); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 4194 | return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4195 | } |
| 4196 | |
| 4197 | /* Just returns the number of bytes available in the input |
| 4198 | * side of the buffer. This function never fails. |
| 4199 | */ |
| 4200 | __LJMP static int hlua_channel_get_in_len(lua_State *L) |
| 4201 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 4202 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4203 | struct filter *filter; |
| 4204 | size_t output, input; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 4205 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 4206 | MAY_LJMP(check_args(L, 1, "input")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 4207 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4208 | |
| 4209 | output = co_data(chn); |
| 4210 | input = ci_data(chn); |
| 4211 | filter = hlua_channel_filter(L, 1, chn, &output, &input); |
| 4212 | if (filter || !IS_HTX_STRM(chn_strm(chn))) |
| 4213 | lua_pushinteger(L, input); |
| 4214 | else { |
Christopher Faulet | a3ceac1 | 2018-12-14 13:39:09 +0100 | [diff] [blame] | 4215 | struct htx *htx = htxbuf(&chn->buf); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4216 | |
Christopher Faulet | a3ceac1 | 2018-12-14 13:39:09 +0100 | [diff] [blame] | 4217 | lua_pushinteger(L, htx->data - co_data(chn)); |
| 4218 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4219 | return 1; |
| 4220 | } |
| 4221 | |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 4222 | /* Returns true if the channel is full. */ |
| 4223 | __LJMP static int hlua_channel_is_full(lua_State *L) |
| 4224 | { |
| 4225 | struct channel *chn; |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 4226 | |
| 4227 | MAY_LJMP(check_args(L, 1, "is_full")); |
| 4228 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 0ec740e | 2020-02-26 11:59:19 +0100 | [diff] [blame] | 4229 | /* ignore the reserve, we are not on a producer side (ie in an |
| 4230 | * applet). |
| 4231 | */ |
| 4232 | lua_pushboolean(L, channel_full(chn, 0)); |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 4233 | return 1; |
| 4234 | } |
| 4235 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 4236 | /* Returns true if the channel may still receive data. */ |
| 4237 | __LJMP static int hlua_channel_may_recv(lua_State *L) |
| 4238 | { |
| 4239 | struct channel *chn; |
| 4240 | |
| 4241 | MAY_LJMP(check_args(L, 1, "may_recv")); |
| 4242 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 4243 | lua_pushboolean(L, (!channel_input_closed(chn) && channel_may_recv(chn))); |
| 4244 | return 1; |
| 4245 | } |
| 4246 | |
Christopher Faulet | 2ac9ba2 | 2020-02-25 10:15:50 +0100 | [diff] [blame] | 4247 | /* Returns true if the channel is the response channel. */ |
| 4248 | __LJMP static int hlua_channel_is_resp(lua_State *L) |
| 4249 | { |
| 4250 | struct channel *chn; |
| 4251 | |
| 4252 | MAY_LJMP(check_args(L, 1, "is_resp")); |
| 4253 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 4254 | |
| 4255 | lua_pushboolean(L, !!(chn->flags & CF_ISRESP)); |
| 4256 | return 1; |
| 4257 | } |
| 4258 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4259 | /* Just returns the number of bytes available in the output |
| 4260 | * side of the buffer. This function never fails. |
| 4261 | */ |
| 4262 | __LJMP static int hlua_channel_get_out_len(lua_State *L) |
| 4263 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 4264 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4265 | size_t output, input; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 4266 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 4267 | MAY_LJMP(check_args(L, 1, "output")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 4268 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4269 | |
| 4270 | output = co_data(chn); |
| 4271 | input = ci_data(chn); |
| 4272 | hlua_channel_filter(L, 1, chn, &output, &input); |
| 4273 | |
| 4274 | lua_pushinteger(L, output); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4275 | return 1; |
| 4276 | } |
| 4277 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4278 | /* |
| 4279 | * |
| 4280 | * |
| 4281 | * Class Fetches |
| 4282 | * |
| 4283 | * |
| 4284 | */ |
| 4285 | |
| 4286 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4287 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4288 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4289 | __LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud) |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4290 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4291 | return MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4292 | } |
| 4293 | |
| 4294 | /* This function creates and push in the stack a fetch object according |
| 4295 | * with a current TXN. |
| 4296 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4297 | 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] | 4298 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 4299 | struct hlua_smp *hsmp; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4300 | |
| 4301 | /* Check stack size. */ |
| 4302 | if (!lua_checkstack(L, 3)) |
| 4303 | return 0; |
| 4304 | |
| 4305 | /* Create the object: obj[0] = userdata. |
| 4306 | * Note that the base of the Fetches object is the |
| 4307 | * transaction object. |
| 4308 | */ |
| 4309 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 4310 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4311 | lua_rawseti(L, -2, 0); |
| 4312 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 4313 | hsmp->s = txn->s; |
| 4314 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 4315 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4316 | hsmp->flags = flags; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4317 | |
| 4318 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 4319 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref); |
| 4320 | lua_setmetatable(L, -2); |
| 4321 | |
| 4322 | return 1; |
| 4323 | } |
| 4324 | |
| 4325 | /* This function is an LUA binding. It is called with each sample-fetch. |
| 4326 | * It uses closure argument to store the associated sample-fetch. It |
| 4327 | * returns only one argument or throws an error. An error is thrown |
| 4328 | * only if an error is encountered during the argument parsing. If |
| 4329 | * the "sample-fetch" function fails, nil is returned. |
| 4330 | */ |
| 4331 | __LJMP static int hlua_run_sample_fetch(lua_State *L) |
| 4332 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 4333 | struct hlua_smp *hsmp; |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 4334 | struct sample_fetch *f; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 4335 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4336 | int i; |
| 4337 | struct sample smp; |
| 4338 | |
| 4339 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4340 | f = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4341 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4342 | /* Get traditional arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 4343 | hsmp = MAY_LJMP(hlua_checkfetches(L, 1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4344 | |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 4345 | /* Check execution authorization. */ |
| 4346 | if (f->use & SMP_USE_HTTP_ANY && |
| 4347 | !(hsmp->flags & HLUA_F_MAY_USE_HTTP)) { |
| 4348 | lua_pushfstring(L, "the sample-fetch '%s' needs an HTTP parser which " |
| 4349 | "is not available in Lua services", f->kw); |
| 4350 | WILL_LJMP(lua_error(L)); |
| 4351 | } |
| 4352 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4353 | /* Get extra arguments. */ |
| 4354 | for (i = 0; i < lua_gettop(L) - 1; i++) { |
| 4355 | if (i >= ARGM_NBARGS) |
| 4356 | break; |
| 4357 | hlua_lua2arg(L, i + 2, &args[i]); |
| 4358 | } |
| 4359 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4360 | args[i].data.str.area = NULL; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4361 | |
| 4362 | /* Check arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 4363 | 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] | 4364 | |
| 4365 | /* Run the special args checker. */ |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 4366 | if (f->val_args && !f->val_args(args, NULL)) { |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4367 | lua_pushfstring(L, "error in arguments"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4368 | goto error; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4369 | } |
| 4370 | |
| 4371 | /* Initialise the sample. */ |
| 4372 | memset(&smp, 0, sizeof(smp)); |
| 4373 | |
| 4374 | /* Run the sample fetch process. */ |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 4375 | 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] | 4376 | if (!f->process(args, &smp, f->kw, f->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4377 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4378 | lua_pushstring(L, ""); |
| 4379 | else |
| 4380 | lua_pushnil(L); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4381 | goto end; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4382 | } |
| 4383 | |
| 4384 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4385 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4386 | hlua_smp2lua_str(L, &smp); |
| 4387 | else |
| 4388 | hlua_smp2lua(L, &smp); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4389 | |
| 4390 | end: |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 4391 | free_args(args); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4392 | return 1; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4393 | |
| 4394 | error: |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 4395 | free_args(args); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4396 | WILL_LJMP(lua_error(L)); |
| 4397 | return 0; /* Never reached */ |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4398 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4399 | |
| 4400 | /* |
| 4401 | * |
| 4402 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4403 | * Class Converters |
| 4404 | * |
| 4405 | * |
| 4406 | */ |
| 4407 | |
| 4408 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4409 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4410 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4411 | __LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4412 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4413 | return MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4414 | } |
| 4415 | |
| 4416 | /* This function creates and push in the stack a Converters object |
| 4417 | * according with a current TXN. |
| 4418 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4419 | 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] | 4420 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 4421 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4422 | |
| 4423 | /* Check stack size. */ |
| 4424 | if (!lua_checkstack(L, 3)) |
| 4425 | return 0; |
| 4426 | |
| 4427 | /* Create the object: obj[0] = userdata. |
| 4428 | * Note that the base of the Converters object is the |
| 4429 | * same than the TXN object. |
| 4430 | */ |
| 4431 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 4432 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4433 | lua_rawseti(L, -2, 0); |
| 4434 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 4435 | hsmp->s = txn->s; |
| 4436 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 4437 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4438 | hsmp->flags = flags; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4439 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4440 | /* Pop a class stream metatable and affect it to the table. */ |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4441 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref); |
| 4442 | lua_setmetatable(L, -2); |
| 4443 | |
| 4444 | return 1; |
| 4445 | } |
| 4446 | |
| 4447 | /* This function is an LUA binding. It is called with each converter. |
| 4448 | * It uses closure argument to store the associated converter. It |
| 4449 | * returns only one argument or throws an error. An error is thrown |
| 4450 | * only if an error is encountered during the argument parsing. If |
| 4451 | * the converter function function fails, nil is returned. |
| 4452 | */ |
| 4453 | __LJMP static int hlua_run_sample_conv(lua_State *L) |
| 4454 | { |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 4455 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4456 | struct sample_conv *conv; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 4457 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4458 | int i; |
| 4459 | struct sample smp; |
| 4460 | |
| 4461 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4462 | conv = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4463 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4464 | /* Get traditional arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 4465 | hsmp = MAY_LJMP(hlua_checkconverters(L, 1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4466 | |
| 4467 | /* Get extra arguments. */ |
| 4468 | for (i = 0; i < lua_gettop(L) - 2; i++) { |
| 4469 | if (i >= ARGM_NBARGS) |
| 4470 | break; |
| 4471 | hlua_lua2arg(L, i + 3, &args[i]); |
| 4472 | } |
| 4473 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4474 | args[i].data.str.area = NULL; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4475 | |
| 4476 | /* Check arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 4477 | 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] | 4478 | |
| 4479 | /* Run the special args checker. */ |
| 4480 | if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) { |
| 4481 | hlua_pusherror(L, "error in arguments"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4482 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4483 | } |
| 4484 | |
| 4485 | /* Initialise the sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 4486 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4487 | if (!hlua_lua2smp(L, 2, &smp)) { |
| 4488 | hlua_pusherror(L, "error in the input argument"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4489 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4490 | } |
| 4491 | |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 4492 | smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR); |
| 4493 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4494 | /* Apply expected cast. */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 4495 | if (!sample_casts[smp.data.type][conv->in_type]) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4496 | hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'", |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 4497 | smp_to_type[smp.data.type], smp_to_type[conv->in_type]); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4498 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4499 | } |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 4500 | if (sample_casts[smp.data.type][conv->in_type] != c_none && |
| 4501 | !sample_casts[smp.data.type][conv->in_type](&smp)) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4502 | hlua_pusherror(L, "error during the input argument casting"); |
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 | |
| 4506 | /* Run the sample conversion process. */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 4507 | if (!conv->process(args, &smp, conv->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4508 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4509 | lua_pushstring(L, ""); |
| 4510 | else |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 4511 | lua_pushnil(L); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4512 | goto end; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4513 | } |
| 4514 | |
| 4515 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4516 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4517 | hlua_smp2lua_str(L, &smp); |
| 4518 | else |
| 4519 | hlua_smp2lua(L, &smp); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4520 | end: |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 4521 | free_args(args); |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 4522 | return 1; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4523 | |
| 4524 | error: |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 4525 | free_args(args); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4526 | WILL_LJMP(lua_error(L)); |
| 4527 | return 0; /* Never reached */ |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4528 | } |
| 4529 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4530 | /* |
| 4531 | * |
| 4532 | * |
| 4533 | * Class AppletTCP |
| 4534 | * |
| 4535 | * |
| 4536 | */ |
| 4537 | |
| 4538 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 4539 | * a class stream, otherwise it throws an error. |
| 4540 | */ |
| 4541 | __LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud) |
| 4542 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4543 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4544 | } |
| 4545 | |
| 4546 | /* This function creates and push in the stack an Applet object |
| 4547 | * according with a current TXN. |
| 4548 | */ |
| 4549 | static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx) |
| 4550 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4551 | struct hlua_appctx *luactx; |
Willy Tarreau | 0698c80 | 2022-05-11 14:09:57 +0200 | [diff] [blame] | 4552 | struct stream *s = appctx_strm(ctx); |
Christopher Faulet | 2da02ae | 2022-02-24 13:45:27 +0100 | [diff] [blame] | 4553 | struct proxy *p; |
| 4554 | |
| 4555 | ALREADY_CHECKED(s); |
| 4556 | p = s->be; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4557 | |
| 4558 | /* Check stack size. */ |
| 4559 | if (!lua_checkstack(L, 3)) |
| 4560 | return 0; |
| 4561 | |
| 4562 | /* Create the object: obj[0] = userdata. |
| 4563 | * Note that the base of the Converters object is the |
| 4564 | * same than the TXN object. |
| 4565 | */ |
| 4566 | lua_newtable(L); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4567 | luactx = lua_newuserdata(L, sizeof(*luactx)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4568 | lua_rawseti(L, -2, 0); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4569 | luactx->appctx = ctx; |
| 4570 | luactx->htxn.s = s; |
| 4571 | luactx->htxn.p = p; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4572 | |
| 4573 | /* Create the "f" field that contains a list of fetches. */ |
| 4574 | lua_pushstring(L, "f"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4575 | if (!hlua_fetches_new(L, &luactx->htxn, 0)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4576 | return 0; |
| 4577 | lua_settable(L, -3); |
| 4578 | |
| 4579 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 4580 | lua_pushstring(L, "sf"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4581 | if (!hlua_fetches_new(L, &luactx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4582 | return 0; |
| 4583 | lua_settable(L, -3); |
| 4584 | |
| 4585 | /* Create the "c" field that contains a list of converters. */ |
| 4586 | lua_pushstring(L, "c"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4587 | if (!hlua_converters_new(L, &luactx->htxn, 0)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4588 | return 0; |
| 4589 | lua_settable(L, -3); |
| 4590 | |
| 4591 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 4592 | lua_pushstring(L, "sc"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4593 | if (!hlua_converters_new(L, &luactx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4594 | return 0; |
| 4595 | lua_settable(L, -3); |
| 4596 | |
| 4597 | /* Pop a class stream metatable and affect it to the table. */ |
| 4598 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref); |
| 4599 | lua_setmetatable(L, -2); |
| 4600 | |
| 4601 | return 1; |
| 4602 | } |
| 4603 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4604 | __LJMP static int hlua_applet_tcp_set_var(lua_State *L) |
| 4605 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4606 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4607 | struct stream *s; |
| 4608 | const char *name; |
| 4609 | size_t len; |
| 4610 | struct sample smp; |
| 4611 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 4612 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 4613 | 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] | 4614 | |
| 4615 | /* It is useles to retrieve the stream, but this function |
| 4616 | * runs only in a stream context. |
| 4617 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4618 | luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4619 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4620 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4621 | |
| 4622 | /* Converts the third argument in a sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 4623 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4624 | hlua_lua2smp(L, 3, &smp); |
| 4625 | |
| 4626 | /* Store the sample in a variable. */ |
| 4627 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 4628 | |
| 4629 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 4630 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 4631 | else |
| 4632 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 4633 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 4634 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4635 | } |
| 4636 | |
| 4637 | __LJMP static int hlua_applet_tcp_unset_var(lua_State *L) |
| 4638 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4639 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4640 | struct stream *s; |
| 4641 | const char *name; |
| 4642 | size_t len; |
| 4643 | struct sample smp; |
| 4644 | |
| 4645 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 4646 | |
| 4647 | /* It is useles to retrieve the stream, but this function |
| 4648 | * runs only in a stream context. |
| 4649 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4650 | luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4651 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4652 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4653 | |
| 4654 | /* Unset the variable. */ |
| 4655 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 4656 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 4657 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4658 | } |
| 4659 | |
| 4660 | __LJMP static int hlua_applet_tcp_get_var(lua_State *L) |
| 4661 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4662 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4663 | struct stream *s; |
| 4664 | const char *name; |
| 4665 | size_t len; |
| 4666 | struct sample smp; |
| 4667 | |
| 4668 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 4669 | |
| 4670 | /* It is useles to retrieve the stream, but this function |
| 4671 | * runs only in a stream context. |
| 4672 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4673 | luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4674 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4675 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4676 | |
| 4677 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Willy Tarreau | e352b9d | 2021-09-03 11:52:38 +0200 | [diff] [blame] | 4678 | if (!vars_get_by_name(name, len, &smp, NULL)) { |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4679 | lua_pushnil(L); |
| 4680 | return 1; |
| 4681 | } |
| 4682 | |
| 4683 | return hlua_smp2lua(L, &smp); |
| 4684 | } |
| 4685 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4686 | __LJMP static int hlua_applet_tcp_set_priv(lua_State *L) |
| 4687 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4688 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 4689 | struct stream *s = luactx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4690 | struct hlua *hlua; |
| 4691 | |
| 4692 | /* 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] | 4693 | if (!s->hlua) |
| 4694 | return 0; |
| 4695 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4696 | |
| 4697 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 4698 | |
| 4699 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 4700 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4701 | |
| 4702 | /* Get and store new value. */ |
| 4703 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 4704 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 4705 | |
| 4706 | return 0; |
| 4707 | } |
| 4708 | |
| 4709 | __LJMP static int hlua_applet_tcp_get_priv(lua_State *L) |
| 4710 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4711 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 4712 | struct stream *s = luactx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4713 | struct hlua *hlua; |
| 4714 | |
| 4715 | /* 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] | 4716 | if (!s->hlua) { |
| 4717 | lua_pushnil(L); |
| 4718 | return 1; |
| 4719 | } |
| 4720 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4721 | |
| 4722 | /* Push configuration index in the stack. */ |
| 4723 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 4724 | |
| 4725 | return 1; |
| 4726 | } |
| 4727 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4728 | /* If expected data not yet available, it returns a yield. This function |
| 4729 | * consumes the data in the buffer. It returns a string containing the |
| 4730 | * data. This string can be empty. |
| 4731 | */ |
| 4732 | __LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx) |
| 4733 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4734 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 4735 | struct stconn *sc = appctx_sc(luactx->appctx); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4736 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4737 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4738 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4739 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4740 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4741 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4742 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 4743 | ret = co_getline_nc(sc_oc(sc), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4744 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4745 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4746 | if (ret == 0) { |
Willy Tarreau | 90e8b45 | 2022-05-25 18:21:43 +0200 | [diff] [blame] | 4747 | applet_need_more_data(luactx->appctx); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4748 | 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] | 4749 | } |
| 4750 | |
| 4751 | /* End of data: commit the total strings and return. */ |
| 4752 | if (ret < 0) { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4753 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4754 | return 1; |
| 4755 | } |
| 4756 | |
| 4757 | /* Ensure that the block 2 length is usable. */ |
| 4758 | if (ret == 1) |
| 4759 | len2 = 0; |
| 4760 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 4761 | /* don't check the max length read and don't check. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4762 | luaL_addlstring(&luactx->b, blk1, len1); |
| 4763 | luaL_addlstring(&luactx->b, blk2, len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4764 | |
| 4765 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 4766 | co_skip(sc_oc(sc), len1 + len2); |
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 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4771 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4772 | __LJMP static int hlua_applet_tcp_getline(lua_State *L) |
| 4773 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4774 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4775 | |
| 4776 | /* Initialise the string catenation. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4777 | luaL_buffinit(L, &luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4778 | |
| 4779 | return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0)); |
| 4780 | } |
| 4781 | |
| 4782 | /* If expected data not yet available, it returns a yield. This function |
| 4783 | * consumes the data in the buffer. It returns a string containing the |
| 4784 | * data. This string can be empty. |
| 4785 | */ |
| 4786 | __LJMP static int hlua_applet_tcp_recv_yield(lua_State *L, int status, lua_KContext ctx) |
| 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)); |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 4789 | struct stconn *sc = appctx_sc(luactx->appctx); |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4790 | size_t len = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4791 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4792 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4793 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4794 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4795 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4796 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4797 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 4798 | ret = co_getblk_nc(sc_oc(sc), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4799 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4800 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4801 | if (ret == 0) { |
Willy Tarreau | 90e8b45 | 2022-05-25 18:21:43 +0200 | [diff] [blame] | 4802 | applet_need_more_data(luactx->appctx); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4803 | 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] | 4804 | } |
| 4805 | |
| 4806 | /* End of data: commit the total strings and return. */ |
| 4807 | if (ret < 0) { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4808 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4809 | return 1; |
| 4810 | } |
| 4811 | |
| 4812 | /* Ensure that the block 2 length is usable. */ |
| 4813 | if (ret == 1) |
| 4814 | len2 = 0; |
| 4815 | |
| 4816 | if (len == -1) { |
| 4817 | |
| 4818 | /* If len == -1, catenate all the data avalaile and |
| 4819 | * yield because we want to get all the data until |
| 4820 | * the end of data stream. |
| 4821 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4822 | luaL_addlstring(&luactx->b, blk1, len1); |
| 4823 | luaL_addlstring(&luactx->b, blk2, len2); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 4824 | co_skip(sc_oc(sc), len1 + len2); |
Willy Tarreau | 90e8b45 | 2022-05-25 18:21:43 +0200 | [diff] [blame] | 4825 | applet_need_more_data(luactx->appctx); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4826 | 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] | 4827 | |
| 4828 | } else { |
| 4829 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4830 | /* Copy the first block caping to the length required. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4831 | if (len1 > len) |
| 4832 | len1 = len; |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4833 | luaL_addlstring(&luactx->b, blk1, len1); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4834 | len -= len1; |
| 4835 | |
| 4836 | /* Copy the second block. */ |
| 4837 | if (len2 > len) |
| 4838 | len2 = len; |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4839 | luaL_addlstring(&luactx->b, blk2, len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4840 | len -= len2; |
| 4841 | |
| 4842 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 4843 | co_skip(sc_oc(sc), len1 + len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4844 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4845 | /* If there is no other data available, yield waiting for new data. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4846 | if (len > 0) { |
| 4847 | lua_pushinteger(L, len); |
| 4848 | lua_replace(L, 2); |
Willy Tarreau | 90e8b45 | 2022-05-25 18:21:43 +0200 | [diff] [blame] | 4849 | applet_need_more_data(luactx->appctx); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4850 | 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] | 4851 | } |
| 4852 | |
| 4853 | /* return the result. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4854 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4855 | return 1; |
| 4856 | } |
| 4857 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4858 | /* we never execute this */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4859 | hlua_pusherror(L, "Lua: internal error"); |
| 4860 | WILL_LJMP(lua_error(L)); |
| 4861 | return 0; |
| 4862 | } |
| 4863 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4864 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4865 | __LJMP static int hlua_applet_tcp_recv(lua_State *L) |
| 4866 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4867 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4868 | int len = -1; |
| 4869 | |
| 4870 | if (lua_gettop(L) > 2) |
| 4871 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 4872 | if (lua_gettop(L) >= 2) { |
| 4873 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4874 | lua_pop(L, 1); |
| 4875 | } |
| 4876 | |
| 4877 | /* Confirm or set the required length */ |
| 4878 | lua_pushinteger(L, len); |
| 4879 | |
| 4880 | /* Initialise the string catenation. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4881 | luaL_buffinit(L, &luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4882 | |
| 4883 | return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0)); |
| 4884 | } |
| 4885 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4886 | /* Append data in the output side of the buffer. This data is immediately |
| 4887 | * sent. The function returns the amount of data written. If the buffer |
| 4888 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4889 | * if the channel is closed. |
| 4890 | */ |
| 4891 | __LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx) |
| 4892 | { |
| 4893 | size_t len; |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4894 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4895 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4896 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 4897 | struct stconn *sc = appctx_sc(luactx->appctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 4898 | struct channel *chn = sc_ic(sc); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4899 | int max; |
| 4900 | |
| 4901 | /* Get the max amount of data which can write as input in the channel. */ |
| 4902 | max = channel_recv_max(chn); |
| 4903 | if (max > (len - l)) |
| 4904 | max = len - l; |
| 4905 | |
| 4906 | /* Copy data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4907 | ci_putblk(chn, str + l, max); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4908 | |
| 4909 | /* update counters. */ |
| 4910 | l += max; |
| 4911 | lua_pop(L, 1); |
| 4912 | lua_pushinteger(L, l); |
| 4913 | |
| 4914 | /* If some data is not send, declares the situation to the |
| 4915 | * applet, and returns a yield. |
| 4916 | */ |
| 4917 | if (l < len) { |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 4918 | sc_need_room(sc); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4919 | 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] | 4920 | } |
| 4921 | |
| 4922 | return 1; |
| 4923 | } |
| 4924 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4925 | /* Just a wrapper of "hlua_applet_tcp_send_yield". This wrapper permits |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4926 | * yield the LUA process, and resume it without checking the |
| 4927 | * input arguments. |
| 4928 | */ |
| 4929 | __LJMP static int hlua_applet_tcp_send(lua_State *L) |
| 4930 | { |
| 4931 | MAY_LJMP(check_args(L, 2, "send")); |
| 4932 | lua_pushinteger(L, 0); |
| 4933 | |
| 4934 | return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0)); |
| 4935 | } |
| 4936 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4937 | /* |
| 4938 | * |
| 4939 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4940 | * Class AppletHTTP |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4941 | * |
| 4942 | * |
| 4943 | */ |
| 4944 | |
| 4945 | /* Returns a struct hlua_txn if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4946 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4947 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4948 | __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] | 4949 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4950 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_http_ref)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4951 | } |
| 4952 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4953 | /* This function creates and push in the stack an Applet object |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4954 | * according with a current TXN. |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 4955 | * It relies on the caller to have already reserved the room in ctx->svcctx |
| 4956 | * for the local storage of hlua_http_ctx. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4957 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4958 | static int hlua_applet_http_new(lua_State *L, struct appctx *ctx) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4959 | { |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 4960 | struct hlua_http_ctx *http_ctx = ctx->svcctx; |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4961 | struct hlua_appctx *luactx; |
Thierry FOURNIER | 841475e | 2015-12-11 17:10:09 +0100 | [diff] [blame] | 4962 | struct hlua_txn htxn; |
Willy Tarreau | 0698c80 | 2022-05-11 14:09:57 +0200 | [diff] [blame] | 4963 | struct stream *s = appctx_strm(ctx); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4964 | struct proxy *px = s->be; |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4965 | struct htx *htx; |
| 4966 | struct htx_blk *blk; |
| 4967 | struct htx_sl *sl; |
| 4968 | struct ist path; |
| 4969 | unsigned long long len = 0; |
| 4970 | int32_t pos; |
Amaury Denoyelle | c453f95 | 2021-07-06 11:40:12 +0200 | [diff] [blame] | 4971 | struct http_uri_parser parser; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4972 | |
| 4973 | /* Check stack size. */ |
| 4974 | if (!lua_checkstack(L, 3)) |
| 4975 | return 0; |
| 4976 | |
| 4977 | /* Create the object: obj[0] = userdata. |
| 4978 | * Note that the base of the Converters object is the |
| 4979 | * same than the TXN object. |
| 4980 | */ |
| 4981 | lua_newtable(L); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4982 | luactx = lua_newuserdata(L, sizeof(*luactx)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4983 | lua_rawseti(L, -2, 0); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4984 | luactx->appctx = ctx; |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 4985 | http_ctx->status = 200; /* Default status code returned. */ |
| 4986 | http_ctx->reason = NULL; /* Use default reason based on status */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4987 | luactx->htxn.s = s; |
| 4988 | luactx->htxn.p = px; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4989 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4990 | /* Create the "f" field that contains a list of fetches. */ |
| 4991 | lua_pushstring(L, "f"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4992 | if (!hlua_fetches_new(L, &luactx->htxn, 0)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4993 | return 0; |
| 4994 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4995 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4996 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 4997 | lua_pushstring(L, "sf"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4998 | if (!hlua_fetches_new(L, &luactx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4999 | return 0; |
| 5000 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5001 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5002 | /* Create the "c" field that contains a list of converters. */ |
| 5003 | lua_pushstring(L, "c"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5004 | if (!hlua_converters_new(L, &luactx->htxn, 0)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5005 | return 0; |
| 5006 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5007 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5008 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 5009 | lua_pushstring(L, "sc"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5010 | if (!hlua_converters_new(L, &luactx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5011 | return 0; |
| 5012 | lua_settable(L, -3); |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5013 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5014 | htx = htxbuf(&s->req.buf); |
| 5015 | blk = htx_get_first_blk(htx); |
Christopher Faulet | ea00973 | 2019-11-18 15:50:25 +0100 | [diff] [blame] | 5016 | BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5017 | sl = htx_get_blk_ptr(htx, blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5018 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5019 | /* Stores the request method. */ |
| 5020 | lua_pushstring(L, "method"); |
| 5021 | lua_pushlstring(L, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); |
| 5022 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5023 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5024 | /* Stores the http version. */ |
| 5025 | lua_pushstring(L, "version"); |
| 5026 | lua_pushlstring(L, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); |
| 5027 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5028 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5029 | /* creates an array of headers. hlua_http_get_headers() crates and push |
| 5030 | * the array on the top of the stack. |
| 5031 | */ |
| 5032 | lua_pushstring(L, "headers"); |
| 5033 | htxn.s = s; |
| 5034 | htxn.p = px; |
| 5035 | htxn.dir = SMP_OPT_DIR_REQ; |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 5036 | if (!hlua_http_get_headers(L, &htxn.s->txn->req)) |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5037 | return 0; |
| 5038 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5039 | |
Amaury Denoyelle | c453f95 | 2021-07-06 11:40:12 +0200 | [diff] [blame] | 5040 | parser = http_uri_parser_init(htx_sl_req_uri(sl)); |
| 5041 | path = http_parse_path(&parser); |
Tim Duesterhus | ed52637 | 2020-03-05 17:56:33 +0100 | [diff] [blame] | 5042 | if (isttest(path)) { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5043 | char *p, *q, *end; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5044 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5045 | p = path.ptr; |
Tim Duesterhus | 4c8f75f | 2021-11-06 15:14:44 +0100 | [diff] [blame] | 5046 | end = istend(path); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5047 | q = p; |
| 5048 | while (q < end && *q != '?') |
| 5049 | q++; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5050 | |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 5051 | /* Stores the request path. */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5052 | lua_pushstring(L, "path"); |
| 5053 | lua_pushlstring(L, p, q - p); |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 5054 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 5055 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5056 | /* Stores the query string. */ |
| 5057 | lua_pushstring(L, "qs"); |
| 5058 | if (*q == '?') |
| 5059 | q++; |
| 5060 | lua_pushlstring(L, q, end - q); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5061 | lua_settable(L, -3); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5062 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5063 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5064 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 5065 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 5066 | enum htx_blk_type type = htx_get_blk_type(blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5067 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 5068 | if (type == HTX_BLK_TLR || type == HTX_BLK_EOT) |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5069 | break; |
| 5070 | if (type == HTX_BLK_DATA) |
| 5071 | len += htx_get_blksz(blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5072 | } |
Christopher Faulet | 2e47e3a | 2023-01-13 11:40:24 +0100 | [diff] [blame] | 5073 | if (htx->extra != HTX_UNKOWN_PAYLOAD_LENGTH) |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5074 | len += htx->extra; |
| 5075 | |
| 5076 | /* Stores the request path. */ |
| 5077 | lua_pushstring(L, "length"); |
| 5078 | lua_pushinteger(L, len); |
| 5079 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 5080 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5081 | /* Create an empty array of HTTP request headers. */ |
| 5082 | lua_pushstring(L, "response"); |
| 5083 | lua_newtable(L); |
| 5084 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 5085 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5086 | /* Pop a class stream metatable and affect it to the table. */ |
| 5087 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref); |
| 5088 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5089 | |
| 5090 | return 1; |
| 5091 | } |
| 5092 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 5093 | __LJMP static int hlua_applet_http_set_var(lua_State *L) |
| 5094 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5095 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 5096 | struct stream *s; |
| 5097 | const char *name; |
| 5098 | size_t len; |
| 5099 | struct sample smp; |
| 5100 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 5101 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 5102 | 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] | 5103 | |
| 5104 | /* It is useles to retrieve the stream, but this function |
| 5105 | * runs only in a stream context. |
| 5106 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5107 | luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 5108 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5109 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 5110 | |
| 5111 | /* Converts the third argument in a sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 5112 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 5113 | hlua_lua2smp(L, 3, &smp); |
| 5114 | |
| 5115 | /* Store the sample in a variable. */ |
| 5116 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 5117 | |
| 5118 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 5119 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 5120 | else |
| 5121 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 5122 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 5123 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 5124 | } |
| 5125 | |
| 5126 | __LJMP static int hlua_applet_http_unset_var(lua_State *L) |
| 5127 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5128 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 5129 | struct stream *s; |
| 5130 | const char *name; |
| 5131 | size_t len; |
| 5132 | struct sample smp; |
| 5133 | |
| 5134 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 5135 | |
| 5136 | /* It is useles to retrieve the stream, but this function |
| 5137 | * runs only in a stream context. |
| 5138 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5139 | luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 5140 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5141 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 5142 | |
| 5143 | /* Unset the variable. */ |
| 5144 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 5145 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 5146 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 5147 | } |
| 5148 | |
| 5149 | __LJMP static int hlua_applet_http_get_var(lua_State *L) |
| 5150 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5151 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 5152 | struct stream *s; |
| 5153 | const char *name; |
| 5154 | size_t len; |
| 5155 | struct sample smp; |
| 5156 | |
| 5157 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 5158 | |
| 5159 | /* It is useles to retrieve the stream, but this function |
| 5160 | * runs only in a stream context. |
| 5161 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5162 | luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 5163 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5164 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 5165 | |
| 5166 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Willy Tarreau | e352b9d | 2021-09-03 11:52:38 +0200 | [diff] [blame] | 5167 | if (!vars_get_by_name(name, len, &smp, NULL)) { |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 5168 | lua_pushnil(L); |
| 5169 | return 1; |
| 5170 | } |
| 5171 | |
| 5172 | return hlua_smp2lua(L, &smp); |
| 5173 | } |
| 5174 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 5175 | __LJMP static int hlua_applet_http_set_priv(lua_State *L) |
| 5176 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5177 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 5178 | struct stream *s = luactx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 5179 | struct hlua *hlua; |
| 5180 | |
| 5181 | /* 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] | 5182 | if (!s->hlua) |
| 5183 | return 0; |
| 5184 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 5185 | |
| 5186 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 5187 | |
| 5188 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 5189 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 5190 | |
| 5191 | /* Get and store new value. */ |
| 5192 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 5193 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 5194 | |
| 5195 | return 0; |
| 5196 | } |
| 5197 | |
| 5198 | __LJMP static int hlua_applet_http_get_priv(lua_State *L) |
| 5199 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5200 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 5201 | struct stream *s = luactx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 5202 | struct hlua *hlua; |
| 5203 | |
| 5204 | /* 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] | 5205 | if (!s->hlua) { |
| 5206 | lua_pushnil(L); |
| 5207 | return 1; |
| 5208 | } |
| 5209 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 5210 | |
| 5211 | /* Push configuration index in the stack. */ |
| 5212 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 5213 | |
| 5214 | return 1; |
| 5215 | } |
| 5216 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5217 | /* If expected data not yet available, it returns a yield. This function |
| 5218 | * consumes the data in the buffer. It returns a string containing the |
| 5219 | * data. This string can be empty. |
| 5220 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5221 | __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] | 5222 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5223 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 5224 | struct stconn *sc = appctx_sc(luactx->appctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 5225 | struct channel *req = sc_oc(sc); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5226 | struct htx *htx; |
| 5227 | struct htx_blk *blk; |
| 5228 | size_t count; |
| 5229 | int stop = 0; |
| 5230 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5231 | htx = htx_from_buf(&req->buf); |
| 5232 | count = co_data(req); |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 5233 | blk = htx_get_first_blk(htx); |
Christopher Faulet | cc26b13 | 2018-12-18 21:20:57 +0100 | [diff] [blame] | 5234 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5235 | while (count && !stop && blk) { |
| 5236 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 5237 | uint32_t sz = htx_get_blksz(blk); |
| 5238 | struct ist v; |
| 5239 | uint32_t vlen; |
| 5240 | char *nl; |
| 5241 | |
| 5242 | vlen = sz; |
| 5243 | if (vlen > count) { |
| 5244 | if (type != HTX_BLK_DATA) |
| 5245 | break; |
| 5246 | vlen = count; |
| 5247 | } |
| 5248 | |
| 5249 | switch (type) { |
| 5250 | case HTX_BLK_UNUSED: |
| 5251 | break; |
| 5252 | |
| 5253 | case HTX_BLK_DATA: |
| 5254 | v = htx_get_blk_value(htx, blk); |
| 5255 | v.len = vlen; |
| 5256 | nl = istchr(v, '\n'); |
| 5257 | if (nl != NULL) { |
| 5258 | stop = 1; |
| 5259 | vlen = nl - v.ptr + 1; |
| 5260 | } |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5261 | luaL_addlstring(&luactx->b, v.ptr, vlen); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5262 | break; |
| 5263 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5264 | case HTX_BLK_TLR: |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 5265 | case HTX_BLK_EOT: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5266 | stop = 1; |
| 5267 | break; |
| 5268 | |
| 5269 | default: |
| 5270 | break; |
| 5271 | } |
| 5272 | |
Willy Tarreau | 8424004 | 2022-02-28 16:51:23 +0100 | [diff] [blame] | 5273 | c_rew(req, vlen); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5274 | count -= vlen; |
| 5275 | if (sz == vlen) |
| 5276 | blk = htx_remove_blk(htx, blk); |
| 5277 | else { |
| 5278 | htx_cut_data_blk(htx, blk, vlen); |
| 5279 | break; |
| 5280 | } |
| 5281 | } |
| 5282 | |
Christopher Faulet | eccb31c | 2021-04-02 14:24:56 +0200 | [diff] [blame] | 5283 | /* The message was fully consumed and no more data are expected |
| 5284 | * (EOM flag set). |
| 5285 | */ |
Christopher Faulet | 904763f | 2023-03-22 14:53:11 +0100 | [diff] [blame] | 5286 | if (htx_is_empty(htx) && (sc->flags & SC_FL_EOI)) |
Christopher Faulet | eccb31c | 2021-04-02 14:24:56 +0200 | [diff] [blame] | 5287 | stop = 1; |
| 5288 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 5289 | htx_to_buf(htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5290 | if (!stop) { |
Willy Tarreau | 90e8b45 | 2022-05-25 18:21:43 +0200 | [diff] [blame] | 5291 | applet_need_more_data(luactx->appctx); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5292 | 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] | 5293 | } |
| 5294 | |
| 5295 | /* return the result. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5296 | luaL_pushresult(&luactx->b); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5297 | return 1; |
| 5298 | } |
| 5299 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5300 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5301 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5302 | __LJMP static int hlua_applet_http_getline(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5303 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5304 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5305 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5306 | /* Initialise the string catenation. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5307 | luaL_buffinit(L, &luactx->b); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5308 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5309 | return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5310 | } |
| 5311 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5312 | /* If expected data not yet available, it returns a yield. This function |
| 5313 | * consumes the data in the buffer. It returns a string containing the |
| 5314 | * data. This string can be empty. |
| 5315 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5316 | __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] | 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)); |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 5319 | struct stconn *sc = appctx_sc(luactx->appctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 5320 | struct channel *req = sc_oc(sc); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5321 | struct htx *htx; |
| 5322 | struct htx_blk *blk; |
| 5323 | size_t count; |
| 5324 | int len; |
| 5325 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5326 | htx = htx_from_buf(&req->buf); |
| 5327 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5328 | count = co_data(req); |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 5329 | blk = htx_get_head_blk(htx); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5330 | while (count && len && blk) { |
| 5331 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 5332 | uint32_t sz = htx_get_blksz(blk); |
| 5333 | struct ist v; |
| 5334 | uint32_t vlen; |
| 5335 | |
| 5336 | vlen = sz; |
| 5337 | if (len > 0 && vlen > len) |
| 5338 | vlen = len; |
| 5339 | if (vlen > count) { |
| 5340 | if (type != HTX_BLK_DATA) |
| 5341 | break; |
| 5342 | vlen = count; |
| 5343 | } |
| 5344 | |
| 5345 | switch (type) { |
| 5346 | case HTX_BLK_UNUSED: |
| 5347 | break; |
| 5348 | |
| 5349 | case HTX_BLK_DATA: |
| 5350 | v = htx_get_blk_value(htx, blk); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5351 | luaL_addlstring(&luactx->b, v.ptr, vlen); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5352 | break; |
| 5353 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5354 | case HTX_BLK_TLR: |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 5355 | case HTX_BLK_EOT: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5356 | len = 0; |
| 5357 | break; |
| 5358 | |
| 5359 | default: |
| 5360 | break; |
| 5361 | } |
| 5362 | |
Willy Tarreau | 8424004 | 2022-02-28 16:51:23 +0100 | [diff] [blame] | 5363 | c_rew(req, vlen); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5364 | count -= vlen; |
| 5365 | if (len > 0) |
| 5366 | len -= vlen; |
| 5367 | if (sz == vlen) |
| 5368 | blk = htx_remove_blk(htx, blk); |
| 5369 | else { |
| 5370 | htx_cut_data_blk(htx, blk, vlen); |
| 5371 | break; |
| 5372 | } |
| 5373 | } |
| 5374 | |
Christopher Faulet | eccb31c | 2021-04-02 14:24:56 +0200 | [diff] [blame] | 5375 | /* The message was fully consumed and no more data are expected |
| 5376 | * (EOM flag set). |
| 5377 | */ |
Christopher Faulet | 904763f | 2023-03-22 14:53:11 +0100 | [diff] [blame] | 5378 | if (htx_is_empty(htx) && (sc->flags & SC_FL_EOI)) |
Christopher Faulet | eccb31c | 2021-04-02 14:24:56 +0200 | [diff] [blame] | 5379 | len = 0; |
| 5380 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 5381 | htx_to_buf(htx, &req->buf); |
| 5382 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5383 | /* If we are no other data available, yield waiting for new data. */ |
| 5384 | if (len) { |
| 5385 | if (len > 0) { |
| 5386 | lua_pushinteger(L, len); |
| 5387 | lua_replace(L, 2); |
| 5388 | } |
Willy Tarreau | 90e8b45 | 2022-05-25 18:21:43 +0200 | [diff] [blame] | 5389 | applet_need_more_data(luactx->appctx); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 5390 | 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] | 5391 | } |
| 5392 | |
| 5393 | /* return the result. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5394 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5395 | return 1; |
| 5396 | } |
| 5397 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5398 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5399 | __LJMP static int hlua_applet_http_recv(lua_State *L) |
| 5400 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5401 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5402 | int len = -1; |
| 5403 | |
| 5404 | /* Check arguments. */ |
| 5405 | if (lua_gettop(L) > 2) |
| 5406 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 5407 | if (lua_gettop(L) >= 2) { |
| 5408 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5409 | lua_pop(L, 1); |
| 5410 | } |
| 5411 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5412 | lua_pushinteger(L, len); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5413 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5414 | /* Initialise the string catenation. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5415 | luaL_buffinit(L, &luactx->b); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5416 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5417 | return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5418 | } |
| 5419 | |
| 5420 | /* Append data in the output side of the buffer. This data is immediately |
| 5421 | * sent. The function returns the amount of data written. If the buffer |
| 5422 | * cannot contain the data, the function yields. The function returns -1 |
| 5423 | * if the channel is closed. |
| 5424 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5425 | __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] | 5426 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5427 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 5428 | struct stconn *sc = appctx_sc(luactx->appctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 5429 | struct channel *res = sc_ic(sc); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5430 | struct htx *htx = htx_from_buf(&res->buf); |
| 5431 | const char *data; |
| 5432 | size_t len; |
| 5433 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 5434 | int max; |
| 5435 | |
Christopher Faulet | 9060fc0 | 2019-07-03 11:39:30 +0200 | [diff] [blame] | 5436 | max = htx_get_max_blksz(htx, channel_htx_recv_max(res, htx)); |
Christopher Faulet | 4b0e9b2 | 2019-01-09 12:16:58 +0100 | [diff] [blame] | 5437 | if (!max) |
| 5438 | goto snd_yield; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5439 | |
| 5440 | data = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5441 | |
| 5442 | /* Get the max amount of data which can write as input in the channel. */ |
| 5443 | if (max > (len - l)) |
| 5444 | max = len - l; |
| 5445 | |
| 5446 | /* Copy data. */ |
Willy Tarreau | 0a7ef02 | 2019-05-28 10:30:11 +0200 | [diff] [blame] | 5447 | max = htx_add_data(htx, ist2(data + l, max)); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 5448 | channel_add_input(res, max); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5449 | |
| 5450 | /* update counters. */ |
| 5451 | l += max; |
| 5452 | lua_pop(L, 1); |
| 5453 | lua_pushinteger(L, l); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5454 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5455 | /* If some data is not send, declares the situation to the |
| 5456 | * applet, and returns a yield. |
| 5457 | */ |
| 5458 | if (l < len) { |
Christopher Faulet | 4b0e9b2 | 2019-01-09 12:16:58 +0100 | [diff] [blame] | 5459 | snd_yield: |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 5460 | htx_to_buf(htx, &res->buf); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 5461 | sc_need_room(sc); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 5462 | 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] | 5463 | } |
| 5464 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5465 | htx_to_buf(htx, &res->buf); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5466 | return 1; |
| 5467 | } |
| 5468 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 5469 | /* Just a wrapper of "hlua_applet_send_yield". This wrapper permits |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5470 | * yield the LUA process, and resume it without checking the |
| 5471 | * input arguments. |
| 5472 | */ |
| 5473 | __LJMP static int hlua_applet_http_send(lua_State *L) |
| 5474 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5475 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 5476 | struct hlua_http_ctx *http_ctx = luactx->appctx->svcctx; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5477 | |
| 5478 | /* We want to send some data. Headers must be sent. */ |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 5479 | if (!(http_ctx->flags & APPLET_HDR_SENT)) { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5480 | hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data."); |
| 5481 | WILL_LJMP(lua_error(L)); |
| 5482 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5483 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 5484 | /* This integer is used for followinf the amount of data sent. */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5485 | lua_pushinteger(L, 0); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5486 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5487 | return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5488 | } |
| 5489 | |
| 5490 | __LJMP static int hlua_applet_http_addheader(lua_State *L) |
| 5491 | { |
| 5492 | const char *name; |
| 5493 | int ret; |
| 5494 | |
| 5495 | MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 5496 | name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5497 | MAY_LJMP(luaL_checkstring(L, 3)); |
| 5498 | |
| 5499 | /* Push in the stack the "response" entry. */ |
| 5500 | ret = lua_getfield(L, 1, "response"); |
| 5501 | if (ret != LUA_TTABLE) { |
| 5502 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] " |
| 5503 | "is expected as an array. %s found", lua_typename(L, ret)); |
| 5504 | WILL_LJMP(lua_error(L)); |
| 5505 | } |
| 5506 | |
| 5507 | /* check if the header is already registered if it is not |
| 5508 | * the case, register it. |
| 5509 | */ |
| 5510 | ret = lua_getfield(L, -1, name); |
| 5511 | if (ret == LUA_TNIL) { |
| 5512 | |
| 5513 | /* Entry not found. */ |
| 5514 | lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */ |
| 5515 | |
| 5516 | /* Insert the new header name in the array in the top of the stack. |
| 5517 | * It left the new array in the top of the stack. |
| 5518 | */ |
| 5519 | lua_newtable(L); |
| 5520 | lua_pushvalue(L, 2); |
| 5521 | lua_pushvalue(L, -2); |
| 5522 | lua_settable(L, -4); |
| 5523 | |
| 5524 | } else if (ret != LUA_TTABLE) { |
| 5525 | |
| 5526 | /* corruption error. */ |
| 5527 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] " |
| 5528 | "is expected as an array. %s found", name, lua_typename(L, ret)); |
| 5529 | WILL_LJMP(lua_error(L)); |
| 5530 | } |
| 5531 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 5532 | /* Now the top of thestack is an array of values. We push |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5533 | * the header value as new entry. |
| 5534 | */ |
| 5535 | lua_pushvalue(L, 3); |
| 5536 | ret = lua_rawlen(L, -2); |
| 5537 | lua_rawseti(L, -2, ret + 1); |
| 5538 | lua_pushboolean(L, 1); |
| 5539 | return 1; |
| 5540 | } |
| 5541 | |
| 5542 | __LJMP static int hlua_applet_http_status(lua_State *L) |
| 5543 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5544 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5545 | int status = MAY_LJMP(luaL_checkinteger(L, 2)); |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5546 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 5547 | struct hlua_http_ctx *http_ctx = luactx->appctx->svcctx; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5548 | |
| 5549 | if (status < 100 || status > 599) { |
| 5550 | lua_pushboolean(L, 0); |
| 5551 | return 1; |
| 5552 | } |
| 5553 | |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 5554 | http_ctx->status = status; |
| 5555 | http_ctx->reason = reason; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5556 | lua_pushboolean(L, 1); |
| 5557 | return 1; |
| 5558 | } |
| 5559 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5560 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5561 | __LJMP static int hlua_applet_http_send_response(lua_State *L) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5562 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5563 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 5564 | struct hlua_http_ctx *http_ctx = luactx->appctx->svcctx; |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 5565 | struct stconn *sc = appctx_sc(luactx->appctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 5566 | struct channel *res = sc_ic(sc); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5567 | struct htx *htx; |
| 5568 | struct htx_sl *sl; |
| 5569 | struct h1m h1m; |
| 5570 | const char *status, *reason; |
| 5571 | const char *name, *value; |
| 5572 | size_t nlen, vlen; |
| 5573 | unsigned int flags; |
| 5574 | |
| 5575 | /* Send the message at once. */ |
| 5576 | htx = htx_from_buf(&res->buf); |
| 5577 | h1m_init_res(&h1m); |
| 5578 | |
| 5579 | /* Use the same http version than the request. */ |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 5580 | status = ultoa_r(http_ctx->status, trash.area, trash.size); |
| 5581 | reason = http_ctx->reason; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5582 | if (reason == NULL) |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 5583 | reason = http_get_reason(http_ctx->status); |
| 5584 | if (http_ctx->flags & APPLET_HTTP11) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5585 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11); |
| 5586 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist(status), ist(reason)); |
| 5587 | } |
| 5588 | else { |
| 5589 | flags = HTX_SL_F_IS_RESP; |
| 5590 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), ist(status), ist(reason)); |
| 5591 | } |
| 5592 | if (!sl) { |
| 5593 | hlua_pusherror(L, "Lua applet http '%s': Failed to create response.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5594 | luactx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5595 | WILL_LJMP(lua_error(L)); |
| 5596 | } |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 5597 | sl->info.res.status = http_ctx->status; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5598 | |
| 5599 | /* Get the array associated to the field "response" in the object AppletHTTP. */ |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5600 | if (lua_getfield(L, 1, "response") != LUA_TTABLE) { |
| 5601 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5602 | luactx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5603 | WILL_LJMP(lua_error(L)); |
| 5604 | } |
| 5605 | |
| 5606 | /* Browse the list of headers. */ |
| 5607 | lua_pushnil(L); |
| 5608 | while(lua_next(L, -2) != 0) { |
| 5609 | /* We expect a string as -2. */ |
| 5610 | if (lua_type(L, -2) != LUA_TSTRING) { |
| 5611 | 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] | 5612 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5613 | lua_typename(L, lua_type(L, -2))); |
| 5614 | WILL_LJMP(lua_error(L)); |
| 5615 | } |
| 5616 | name = lua_tolstring(L, -2, &nlen); |
| 5617 | |
| 5618 | /* We expect an array as -1. */ |
| 5619 | if (lua_type(L, -1) != LUA_TTABLE) { |
| 5620 | 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] | 5621 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5622 | name, |
| 5623 | lua_typename(L, lua_type(L, -1))); |
| 5624 | WILL_LJMP(lua_error(L)); |
| 5625 | } |
| 5626 | |
| 5627 | /* Browse the table who is on the top of the stack. */ |
| 5628 | lua_pushnil(L); |
| 5629 | while(lua_next(L, -2) != 0) { |
| 5630 | int id; |
| 5631 | |
| 5632 | /* We expect a number as -2. */ |
| 5633 | if (lua_type(L, -2) != LUA_TNUMBER) { |
| 5634 | 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] | 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, -2))); |
| 5638 | WILL_LJMP(lua_error(L)); |
| 5639 | } |
| 5640 | id = lua_tointeger(L, -2); |
| 5641 | |
| 5642 | /* We expect a string as -2. */ |
| 5643 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 5644 | 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] | 5645 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5646 | name, id, |
| 5647 | lua_typename(L, lua_type(L, -1))); |
| 5648 | WILL_LJMP(lua_error(L)); |
| 5649 | } |
| 5650 | value = lua_tolstring(L, -1, &vlen); |
| 5651 | |
| 5652 | /* Simple Protocol checks. */ |
Christopher Faulet | 545fbba | 2021-09-28 09:36:25 +0200 | [diff] [blame] | 5653 | if (isteqi(ist2(name, nlen), ist("transfer-encoding"))) { |
| 5654 | int ret; |
| 5655 | |
| 5656 | ret = h1_parse_xfer_enc_header(&h1m, ist2(value, vlen)); |
| 5657 | if (ret < 0) { |
| 5658 | hlua_pusherror(L, "Lua applet http '%s': Invalid '%s' header.\n", |
| 5659 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
| 5660 | name); |
| 5661 | WILL_LJMP(lua_error(L)); |
| 5662 | } |
| 5663 | else if (ret == 0) |
| 5664 | goto next; /* Skip it */ |
| 5665 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5666 | else if (isteqi(ist2(name, nlen), ist("content-length"))) { |
| 5667 | struct ist v = ist2(value, vlen); |
| 5668 | int ret; |
| 5669 | |
| 5670 | ret = h1_parse_cont_len_header(&h1m, &v); |
| 5671 | if (ret < 0) { |
| 5672 | hlua_pusherror(L, "Lua applet http '%s': Invalid '%s' header.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5673 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5674 | name); |
| 5675 | WILL_LJMP(lua_error(L)); |
| 5676 | } |
| 5677 | else if (ret == 0) |
| 5678 | goto next; /* Skip it */ |
| 5679 | } |
| 5680 | |
| 5681 | /* Add a new header */ |
| 5682 | if (!htx_add_header(htx, ist2(name, nlen), ist2(value, vlen))) { |
| 5683 | 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] | 5684 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5685 | name); |
| 5686 | WILL_LJMP(lua_error(L)); |
| 5687 | } |
| 5688 | next: |
| 5689 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 5690 | lua_pop(L, 1); |
| 5691 | } |
| 5692 | |
| 5693 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 5694 | lua_pop(L, 1); |
| 5695 | } |
| 5696 | |
| 5697 | if (h1m.flags & H1_MF_CHNK) |
| 5698 | h1m.flags &= ~H1_MF_CLEN; |
| 5699 | if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK)) |
| 5700 | h1m.flags |= H1_MF_XFER_LEN; |
| 5701 | |
| 5702 | /* Uset HTX start-line flags */ |
| 5703 | if (h1m.flags & H1_MF_XFER_ENC) |
| 5704 | flags |= HTX_SL_F_XFER_ENC; |
| 5705 | if (h1m.flags & H1_MF_XFER_LEN) { |
| 5706 | flags |= HTX_SL_F_XFER_LEN; |
| 5707 | if (h1m.flags & H1_MF_CHNK) |
| 5708 | flags |= HTX_SL_F_CHNK; |
| 5709 | else if (h1m.flags & H1_MF_CLEN) |
| 5710 | flags |= HTX_SL_F_CLEN; |
| 5711 | if (h1m.body_len == 0) |
| 5712 | flags |= HTX_SL_F_BODYLESS; |
| 5713 | } |
| 5714 | sl->flags |= flags; |
| 5715 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 5716 | /* 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] | 5717 | * and the status code implies the presence of a message body, we must |
| 5718 | * announce a transfer encoding chunked. This is required by haproxy |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 5719 | * for the keepalive compliance. If the applet announces a transfer-encoding |
| 5720 | * chunked itself, don't do anything. |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5721 | */ |
| 5722 | 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] | 5723 | http_ctx->status >= 200 && http_ctx->status != 204 && http_ctx->status != 304) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5724 | /* Add a new header */ |
| 5725 | sl->flags |= (HTX_SL_F_XFER_ENC|H1_MF_CHNK|H1_MF_XFER_LEN); |
| 5726 | if (!htx_add_header(htx, ist("transfer-encoding"), ist("chunked"))) { |
| 5727 | 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] | 5728 | luactx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5729 | WILL_LJMP(lua_error(L)); |
| 5730 | } |
| 5731 | } |
| 5732 | |
| 5733 | /* Finalize headers. */ |
| 5734 | if (!htx_add_endof(htx, HTX_BLK_EOH)) { |
| 5735 | hlua_pusherror(L, "Lua applet http '%s': Failed create the response.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5736 | luactx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5737 | WILL_LJMP(lua_error(L)); |
| 5738 | } |
| 5739 | |
| 5740 | if (htx_used_space(htx) > b_size(&res->buf) - global.tune.maxrewrite) { |
| 5741 | b_reset(&res->buf); |
| 5742 | hlua_pusherror(L, "Lua: 'start_response': response header block too big"); |
| 5743 | WILL_LJMP(lua_error(L)); |
| 5744 | } |
| 5745 | |
| 5746 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 5747 | channel_add_input(res, htx->data); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5748 | |
| 5749 | /* Headers sent, set the flag. */ |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 5750 | http_ctx->flags |= APPLET_HDR_SENT; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5751 | return 0; |
| 5752 | |
| 5753 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5754 | /* We will build the status line and the headers of the HTTP response. |
| 5755 | * We will try send at once if its not possible, we give back the hand |
| 5756 | * waiting for more room. |
| 5757 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5758 | __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] | 5759 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5760 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 5761 | struct stconn *sc = appctx_sc(luactx->appctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 5762 | struct channel *res = sc_ic(sc); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5763 | |
| 5764 | if (co_data(res)) { |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 5765 | sc_need_room(sc); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5766 | 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] | 5767 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5768 | return MAY_LJMP(hlua_applet_http_send_response(L)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5769 | } |
| 5770 | |
| 5771 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5772 | __LJMP static int hlua_applet_http_start_response(lua_State *L) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5773 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5774 | return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5775 | } |
| 5776 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5777 | /* |
| 5778 | * |
| 5779 | * |
| 5780 | * Class HTTP |
| 5781 | * |
| 5782 | * |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5783 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5784 | |
| 5785 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 5786 | * a class stream, otherwise it throws an error. |
| 5787 | */ |
| 5788 | __LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5789 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5790 | return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref)); |
| 5791 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5792 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5793 | /* This function creates and push in the stack a HTTP object |
| 5794 | * according with a current TXN. |
| 5795 | */ |
| 5796 | static int hlua_http_new(lua_State *L, struct hlua_txn *txn) |
| 5797 | { |
| 5798 | struct hlua_txn *htxn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5799 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5800 | /* Check stack size. */ |
| 5801 | if (!lua_checkstack(L, 3)) |
| 5802 | return 0; |
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 | /* Create the object: obj[0] = userdata. |
| 5805 | * Note that the base of the Converters object is the |
| 5806 | * same than the TXN object. |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5807 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5808 | lua_newtable(L); |
| 5809 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
| 5810 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5811 | |
| 5812 | htxn->s = txn->s; |
| 5813 | htxn->p = txn->p; |
Christopher Faulet | 256b69a | 2019-05-23 11:14:21 +0200 | [diff] [blame] | 5814 | htxn->dir = txn->dir; |
| 5815 | htxn->flags = txn->flags; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5816 | |
| 5817 | /* Pop a class stream metatable and affect it to the table. */ |
| 5818 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref); |
| 5819 | lua_setmetatable(L, -2); |
| 5820 | |
| 5821 | return 1; |
| 5822 | } |
| 5823 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 5824 | /* This function creates and returns an array containing the status-line |
| 5825 | * elements. This function does not fails. |
| 5826 | */ |
| 5827 | __LJMP static int hlua_http_get_stline(lua_State *L, struct htx_sl *sl) |
| 5828 | { |
| 5829 | /* Create the table. */ |
| 5830 | lua_newtable(L); |
| 5831 | |
| 5832 | if (sl->flags & HTX_SL_F_IS_RESP) { |
| 5833 | lua_pushstring(L, "version"); |
| 5834 | lua_pushlstring(L, HTX_SL_RES_VPTR(sl), HTX_SL_RES_VLEN(sl)); |
| 5835 | lua_settable(L, -3); |
| 5836 | lua_pushstring(L, "code"); |
| 5837 | lua_pushlstring(L, HTX_SL_RES_CPTR(sl), HTX_SL_RES_CLEN(sl)); |
| 5838 | lua_settable(L, -3); |
| 5839 | lua_pushstring(L, "reason"); |
| 5840 | lua_pushlstring(L, HTX_SL_RES_RPTR(sl), HTX_SL_RES_RLEN(sl)); |
| 5841 | lua_settable(L, -3); |
| 5842 | } |
| 5843 | else { |
| 5844 | lua_pushstring(L, "method"); |
| 5845 | lua_pushlstring(L, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); |
| 5846 | lua_settable(L, -3); |
| 5847 | lua_pushstring(L, "uri"); |
| 5848 | lua_pushlstring(L, HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl)); |
| 5849 | lua_settable(L, -3); |
| 5850 | lua_pushstring(L, "version"); |
| 5851 | lua_pushlstring(L, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); |
| 5852 | lua_settable(L, -3); |
| 5853 | } |
| 5854 | return 1; |
| 5855 | } |
| 5856 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5857 | /* This function creates ans returns an array of HTTP headers. |
| 5858 | * This function does not fails. It is used as wrapper with the |
| 5859 | * 2 following functions. |
| 5860 | */ |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 5861 | __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] | 5862 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5863 | struct htx *htx; |
| 5864 | int32_t pos; |
| 5865 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5866 | /* Create the table. */ |
| 5867 | lua_newtable(L); |
| 5868 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5869 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5870 | htx = htxbuf(&msg->chn->buf); |
| 5871 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 5872 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 5873 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 5874 | struct ist n, v; |
| 5875 | int len; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5876 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5877 | if (type == HTX_BLK_HDR) { |
| 5878 | n = htx_get_blk_name(htx,blk); |
| 5879 | v = htx_get_blk_value(htx, blk); |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5880 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5881 | else if (type == HTX_BLK_EOH) |
| 5882 | break; |
| 5883 | else |
| 5884 | continue; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5885 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5886 | /* Check for existing entry: |
| 5887 | * assume that the table is on the top of the stack, and |
| 5888 | * push the key in the stack, the function lua_gettable() |
| 5889 | * perform the lookup. |
| 5890 | */ |
| 5891 | lua_pushlstring(L, n.ptr, n.len); |
| 5892 | lua_gettable(L, -2); |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5893 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5894 | switch (lua_type(L, -1)) { |
| 5895 | case LUA_TNIL: |
| 5896 | /* Table not found, create it. */ |
| 5897 | lua_pop(L, 1); /* remove the nil value. */ |
| 5898 | lua_pushlstring(L, n.ptr, n.len); /* push the header name as key. */ |
| 5899 | lua_newtable(L); /* create and push empty table. */ |
| 5900 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 5901 | lua_rawseti(L, -2, 0); /* index header value (pop it). */ |
| 5902 | 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] | 5903 | break; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5904 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5905 | case LUA_TTABLE: |
| 5906 | /* Entry found: push the value in the table. */ |
| 5907 | len = lua_rawlen(L, -1); |
| 5908 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 5909 | lua_rawseti(L, -2, len+1); /* index header value (pop it). */ |
| 5910 | lua_pop(L, 1); /* remove the table (it is stored in the main table). */ |
| 5911 | break; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5912 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5913 | default: |
| 5914 | /* Other cases are errors. */ |
| 5915 | hlua_pusherror(L, "internal error during the parsing of headers."); |
| 5916 | WILL_LJMP(lua_error(L)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5917 | } |
| 5918 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5919 | return 1; |
| 5920 | } |
| 5921 | |
| 5922 | __LJMP static int hlua_http_req_get_headers(lua_State *L) |
| 5923 | { |
| 5924 | struct hlua_txn *htxn; |
| 5925 | |
| 5926 | MAY_LJMP(check_args(L, 1, "req_get_headers")); |
| 5927 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5928 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5929 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5930 | WILL_LJMP(lua_error(L)); |
| 5931 | |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 5932 | return hlua_http_get_headers(L, &htxn->s->txn->req); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5933 | } |
| 5934 | |
| 5935 | __LJMP static int hlua_http_res_get_headers(lua_State *L) |
| 5936 | { |
| 5937 | struct hlua_txn *htxn; |
| 5938 | |
| 5939 | MAY_LJMP(check_args(L, 1, "res_get_headers")); |
| 5940 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5941 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5942 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5943 | WILL_LJMP(lua_error(L)); |
| 5944 | |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 5945 | return hlua_http_get_headers(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5946 | } |
| 5947 | |
| 5948 | /* This function replace full header, or just a value in |
| 5949 | * the request or in the response. It is a wrapper fir the |
| 5950 | * 4 following functions. |
| 5951 | */ |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5952 | __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] | 5953 | { |
| 5954 | size_t name_len; |
| 5955 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5956 | const char *reg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 5957 | const char *value = MAY_LJMP(luaL_checkstring(L, 4)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5958 | struct htx *htx; |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 5959 | struct my_regex *re; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5960 | |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 5961 | if (!(re = regex_comp(reg, 1, 1, NULL))) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5962 | WILL_LJMP(luaL_argerror(L, 3, "invalid regex")); |
| 5963 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5964 | htx = htxbuf(&msg->chn->buf); |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5965 | 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] | 5966 | regex_free(re); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5967 | return 0; |
| 5968 | } |
| 5969 | |
| 5970 | __LJMP static int hlua_http_req_rep_hdr(lua_State *L) |
| 5971 | { |
| 5972 | struct hlua_txn *htxn; |
| 5973 | |
| 5974 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 5975 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5976 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5977 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5978 | WILL_LJMP(lua_error(L)); |
| 5979 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5980 | 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] | 5981 | } |
| 5982 | |
| 5983 | __LJMP static int hlua_http_res_rep_hdr(lua_State *L) |
| 5984 | { |
| 5985 | struct hlua_txn *htxn; |
| 5986 | |
| 5987 | MAY_LJMP(check_args(L, 4, "res_rep_hdr")); |
| 5988 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5989 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5990 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5991 | WILL_LJMP(lua_error(L)); |
| 5992 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5993 | 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] | 5994 | } |
| 5995 | |
| 5996 | __LJMP static int hlua_http_req_rep_val(lua_State *L) |
| 5997 | { |
| 5998 | struct hlua_txn *htxn; |
| 5999 | |
| 6000 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 6001 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 6002 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 6003 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6004 | WILL_LJMP(lua_error(L)); |
| 6005 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 6006 | 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] | 6007 | } |
| 6008 | |
| 6009 | __LJMP static int hlua_http_res_rep_val(lua_State *L) |
| 6010 | { |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6011 | struct hlua_txn *htxn; |
| 6012 | |
| 6013 | MAY_LJMP(check_args(L, 4, "res_rep_val")); |
| 6014 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 6015 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 6016 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6017 | WILL_LJMP(lua_error(L)); |
| 6018 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 6019 | 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] | 6020 | } |
| 6021 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6022 | /* This function deletes all the occurrences of an header. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6023 | * It is a wrapper for the 2 following functions. |
| 6024 | */ |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 6025 | __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] | 6026 | { |
| 6027 | size_t len; |
| 6028 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 6029 | struct htx *htx = htxbuf(&msg->chn->buf); |
| 6030 | struct http_hdr_ctx ctx; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6031 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 6032 | ctx.blk = NULL; |
| 6033 | while (http_find_header(htx, ist2(name, len), &ctx, 1)) |
| 6034 | http_remove_header(htx, &ctx); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6035 | return 0; |
| 6036 | } |
| 6037 | |
| 6038 | __LJMP static int hlua_http_req_del_hdr(lua_State *L) |
| 6039 | { |
| 6040 | struct hlua_txn *htxn; |
| 6041 | |
| 6042 | MAY_LJMP(check_args(L, 2, "req_del_hdr")); |
| 6043 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 6044 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 6045 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6046 | WILL_LJMP(lua_error(L)); |
| 6047 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 6048 | return hlua_http_del_hdr(L, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6049 | } |
| 6050 | |
| 6051 | __LJMP static int hlua_http_res_del_hdr(lua_State *L) |
| 6052 | { |
| 6053 | struct hlua_txn *htxn; |
| 6054 | |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6055 | MAY_LJMP(check_args(L, 2, "res_del_hdr")); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6056 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 6057 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 6058 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6059 | WILL_LJMP(lua_error(L)); |
| 6060 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 6061 | return hlua_http_del_hdr(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6062 | } |
| 6063 | |
| 6064 | /* This function adds an header. It is a wrapper used by |
| 6065 | * the 2 following functions. |
| 6066 | */ |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 6067 | __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] | 6068 | { |
| 6069 | size_t name_len; |
| 6070 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 6071 | size_t value_len; |
| 6072 | const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 6073 | struct htx *htx = htxbuf(&msg->chn->buf); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6074 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 6075 | lua_pushboolean(L, http_add_header(htx, ist2(name, name_len), |
| 6076 | ist2(value, value_len))); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6077 | return 0; |
| 6078 | } |
| 6079 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6080 | __LJMP static int hlua_http_req_add_hdr(lua_State *L) |
| 6081 | { |
| 6082 | struct hlua_txn *htxn; |
| 6083 | |
| 6084 | MAY_LJMP(check_args(L, 3, "req_add_hdr")); |
| 6085 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 6086 | |
| 6087 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
| 6088 | WILL_LJMP(lua_error(L)); |
| 6089 | |
| 6090 | return hlua_http_add_hdr(L, &htxn->s->txn->req); |
| 6091 | } |
| 6092 | |
| 6093 | __LJMP static int hlua_http_res_add_hdr(lua_State *L) |
| 6094 | { |
| 6095 | struct hlua_txn *htxn; |
| 6096 | |
| 6097 | MAY_LJMP(check_args(L, 3, "res_add_hdr")); |
| 6098 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 6099 | |
| 6100 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
| 6101 | WILL_LJMP(lua_error(L)); |
| 6102 | |
| 6103 | return hlua_http_add_hdr(L, &htxn->s->txn->rsp); |
| 6104 | } |
| 6105 | |
| 6106 | static int hlua_http_req_set_hdr(lua_State *L) |
| 6107 | { |
| 6108 | struct hlua_txn *htxn; |
| 6109 | |
| 6110 | MAY_LJMP(check_args(L, 3, "req_set_hdr")); |
| 6111 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 6112 | |
| 6113 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
| 6114 | WILL_LJMP(lua_error(L)); |
| 6115 | |
| 6116 | hlua_http_del_hdr(L, &htxn->s->txn->req); |
| 6117 | return hlua_http_add_hdr(L, &htxn->s->txn->req); |
| 6118 | } |
| 6119 | |
| 6120 | static int hlua_http_res_set_hdr(lua_State *L) |
| 6121 | { |
| 6122 | struct hlua_txn *htxn; |
| 6123 | |
| 6124 | MAY_LJMP(check_args(L, 3, "res_set_hdr")); |
| 6125 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 6126 | |
| 6127 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
| 6128 | WILL_LJMP(lua_error(L)); |
| 6129 | |
| 6130 | hlua_http_del_hdr(L, &htxn->s->txn->rsp); |
| 6131 | return hlua_http_add_hdr(L, &htxn->s->txn->rsp); |
| 6132 | } |
| 6133 | |
| 6134 | /* This function set the method. */ |
| 6135 | static int hlua_http_req_set_meth(lua_State *L) |
| 6136 | { |
| 6137 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 6138 | size_t name_len; |
| 6139 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 6140 | |
| 6141 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
| 6142 | WILL_LJMP(lua_error(L)); |
| 6143 | |
| 6144 | lua_pushboolean(L, http_req_replace_stline(0, name, name_len, htxn->p, htxn->s) != -1); |
| 6145 | return 1; |
| 6146 | } |
| 6147 | |
| 6148 | /* This function set the method. */ |
| 6149 | static int hlua_http_req_set_path(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(1, name, name_len, htxn->p, htxn->s) != -1); |
| 6159 | return 1; |
| 6160 | } |
| 6161 | |
| 6162 | /* This function set the query-string. */ |
| 6163 | static int hlua_http_req_set_query(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 | /* Check length. */ |
| 6173 | if (name_len > trash.size - 1) { |
| 6174 | lua_pushboolean(L, 0); |
| 6175 | return 1; |
| 6176 | } |
| 6177 | |
| 6178 | /* Add the mark question as prefix. */ |
| 6179 | chunk_reset(&trash); |
| 6180 | trash.area[trash.data++] = '?'; |
| 6181 | memcpy(trash.area + trash.data, name, name_len); |
| 6182 | trash.data += name_len; |
| 6183 | |
| 6184 | lua_pushboolean(L, |
| 6185 | http_req_replace_stline(2, trash.area, trash.data, htxn->p, htxn->s) != -1); |
| 6186 | return 1; |
| 6187 | } |
| 6188 | |
| 6189 | /* This function set the uri. */ |
| 6190 | static int hlua_http_req_set_uri(lua_State *L) |
| 6191 | { |
| 6192 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 6193 | size_t name_len; |
| 6194 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 6195 | |
| 6196 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
| 6197 | WILL_LJMP(lua_error(L)); |
| 6198 | |
| 6199 | lua_pushboolean(L, http_req_replace_stline(3, name, name_len, htxn->p, htxn->s) != -1); |
| 6200 | return 1; |
| 6201 | } |
| 6202 | |
| 6203 | /* This function set the response code & optionally reason. */ |
| 6204 | static int hlua_http_res_set_status(lua_State *L) |
| 6205 | { |
| 6206 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 6207 | unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 6208 | const char *str = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
| 6209 | const struct ist reason = ist2(str, (str ? strlen(str) : 0)); |
| 6210 | |
| 6211 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
| 6212 | WILL_LJMP(lua_error(L)); |
| 6213 | |
| 6214 | http_res_set_status(code, reason, htxn->s); |
| 6215 | return 0; |
| 6216 | } |
| 6217 | |
| 6218 | /* |
| 6219 | * |
| 6220 | * |
| 6221 | * Class HTTPMessage |
| 6222 | * |
| 6223 | * |
| 6224 | */ |
| 6225 | |
| 6226 | /* Returns a struct http_msg if the stack entry "ud" is a class HTTPMessage, |
| 6227 | * otherwise it throws an error. |
| 6228 | */ |
| 6229 | __LJMP static struct http_msg *hlua_checkhttpmsg(lua_State *L, int ud) |
| 6230 | { |
| 6231 | return MAY_LJMP(hlua_checkudata(L, ud, class_http_msg_ref)); |
| 6232 | } |
| 6233 | |
| 6234 | /* Creates and pushes on the stack a HTTP object according with a current TXN. |
| 6235 | */ |
Christopher Faulet | 78c3547 | 2020-02-26 17:14:08 +0100 | [diff] [blame] | 6236 | 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] | 6237 | { |
| 6238 | /* Check stack size. */ |
| 6239 | if (!lua_checkstack(L, 3)) |
| 6240 | return 0; |
| 6241 | |
| 6242 | lua_newtable(L); |
| 6243 | lua_pushlightuserdata(L, msg); |
| 6244 | lua_rawseti(L, -2, 0); |
| 6245 | |
| 6246 | /* Create the "channel" field that contains the request channel object. */ |
| 6247 | lua_pushstring(L, "channel"); |
| 6248 | if (!hlua_channel_new(L, msg->chn)) |
| 6249 | return 0; |
| 6250 | lua_rawset(L, -3); |
| 6251 | |
| 6252 | /* Pop a class stream metatable and affect it to the table. */ |
| 6253 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_msg_ref); |
| 6254 | lua_setmetatable(L, -2); |
| 6255 | |
| 6256 | return 1; |
| 6257 | } |
| 6258 | |
| 6259 | /* Helper function returning a filter attached to the HTTP message at the |
| 6260 | * 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] | 6261 | * 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] | 6262 | * filled with output and input length respectively. |
| 6263 | */ |
| 6264 | static struct filter *hlua_http_msg_filter(lua_State *L, int ud, struct http_msg *msg, size_t *offset, size_t *len) |
| 6265 | { |
| 6266 | struct channel *chn = msg->chn; |
| 6267 | struct htx *htx = htxbuf(&chn->buf); |
| 6268 | struct filter *filter = NULL; |
| 6269 | |
| 6270 | *offset = co_data(msg->chn); |
| 6271 | *len = htx->data - co_data(msg->chn); |
| 6272 | |
| 6273 | if (lua_getfield(L, ud, "__filter") == LUA_TLIGHTUSERDATA) { |
| 6274 | filter = lua_touserdata (L, -1); |
| 6275 | if (msg->msg_state >= HTTP_MSG_DATA) { |
| 6276 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 6277 | |
| 6278 | *offset = flt_ctx->cur_off[CHN_IDX(chn)]; |
| 6279 | *len = flt_ctx->cur_len[CHN_IDX(chn)]; |
| 6280 | } |
| 6281 | } |
| 6282 | |
| 6283 | lua_pop(L, 1); |
| 6284 | return filter; |
| 6285 | } |
| 6286 | |
| 6287 | /* Returns true if the channel attached to the HTTP message is the response |
| 6288 | * channel. |
| 6289 | */ |
| 6290 | __LJMP static int hlua_http_msg_is_resp(lua_State *L) |
| 6291 | { |
| 6292 | struct http_msg *msg; |
| 6293 | |
| 6294 | MAY_LJMP(check_args(L, 1, "is_resp")); |
| 6295 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6296 | |
| 6297 | lua_pushboolean(L, !!(msg->chn->flags & CF_ISRESP)); |
| 6298 | return 1; |
| 6299 | } |
| 6300 | |
| 6301 | /* Returns an array containing the elements status-line of the HTTP message. It relies |
| 6302 | * on hlua_http_get_stline(). |
| 6303 | */ |
| 6304 | __LJMP static int hlua_http_msg_get_stline(lua_State *L) |
| 6305 | { |
| 6306 | struct http_msg *msg; |
| 6307 | struct htx *htx; |
| 6308 | struct htx_sl *sl; |
| 6309 | |
| 6310 | MAY_LJMP(check_args(L, 1, "get_stline")); |
| 6311 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6312 | |
| 6313 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6314 | WILL_LJMP(lua_error(L)); |
| 6315 | |
| 6316 | htx = htxbuf(&msg->chn->buf); |
| 6317 | sl = http_get_stline(htx); |
| 6318 | if (!sl) |
| 6319 | return 0; |
| 6320 | return hlua_http_get_stline(L, sl); |
| 6321 | } |
| 6322 | |
| 6323 | /* Returns an array containing all headers of the HTTP message. it relies on |
| 6324 | * hlua_http_get_headers(). |
| 6325 | */ |
| 6326 | __LJMP static int hlua_http_msg_get_headers(lua_State *L) |
| 6327 | { |
| 6328 | struct http_msg *msg; |
| 6329 | |
| 6330 | MAY_LJMP(check_args(L, 1, "get_headers")); |
| 6331 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6332 | |
| 6333 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6334 | WILL_LJMP(lua_error(L)); |
| 6335 | |
| 6336 | return hlua_http_get_headers(L, msg); |
| 6337 | } |
| 6338 | |
| 6339 | /* Deletes all occurrences of an header in the HTTP message matching on its |
| 6340 | * name. It relies on hlua_http_del_hdr(). |
| 6341 | */ |
| 6342 | __LJMP static int hlua_http_msg_del_hdr(lua_State *L) |
| 6343 | { |
| 6344 | struct http_msg *msg; |
| 6345 | |
| 6346 | MAY_LJMP(check_args(L, 2, "del_header")); |
| 6347 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6348 | |
| 6349 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6350 | WILL_LJMP(lua_error(L)); |
| 6351 | |
| 6352 | return hlua_http_del_hdr(L, msg); |
| 6353 | } |
| 6354 | |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 6355 | /* 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] | 6356 | * message given its name against a regex and replaces it if it matches. It |
| 6357 | * relies on hlua_http_rep_hdr(). |
| 6358 | */ |
| 6359 | __LJMP static int hlua_http_msg_rep_hdr(lua_State *L) |
| 6360 | { |
| 6361 | struct http_msg *msg; |
| 6362 | |
| 6363 | MAY_LJMP(check_args(L, 4, "rep_header")); |
| 6364 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6365 | |
| 6366 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6367 | WILL_LJMP(lua_error(L)); |
| 6368 | |
| 6369 | return hlua_http_rep_hdr(L, msg, 1); |
| 6370 | } |
| 6371 | |
Ilya Shipitsin | bd6b4be | 2021-10-15 16:18:21 +0500 | [diff] [blame] | 6372 | /* 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] | 6373 | * message given its name against a regex and replaces it if it matches. It |
| 6374 | * relies on hlua_http_rep_hdr(). |
| 6375 | */ |
| 6376 | __LJMP static int hlua_http_msg_rep_val(lua_State *L) |
| 6377 | { |
| 6378 | struct http_msg *msg; |
| 6379 | |
| 6380 | MAY_LJMP(check_args(L, 4, "rep_value")); |
| 6381 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6382 | |
| 6383 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6384 | WILL_LJMP(lua_error(L)); |
| 6385 | |
| 6386 | return hlua_http_rep_hdr(L, msg, 0); |
| 6387 | } |
| 6388 | |
| 6389 | /* Add an header in the HTTP message. It relies on hlua_http_add_hdr() */ |
| 6390 | __LJMP static int hlua_http_msg_add_hdr(lua_State *L) |
| 6391 | { |
| 6392 | struct http_msg *msg; |
| 6393 | |
| 6394 | MAY_LJMP(check_args(L, 3, "add_header")); |
| 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_add_hdr(L, msg); |
| 6401 | } |
| 6402 | |
| 6403 | /* Add an header in the HTTP message removing existing headers with the same |
| 6404 | * name. It relies on hlua_http_del_hdr() and hlua_http_add_hdr(). |
| 6405 | */ |
| 6406 | __LJMP static int hlua_http_msg_set_hdr(lua_State *L) |
| 6407 | { |
| 6408 | struct http_msg *msg; |
| 6409 | |
| 6410 | MAY_LJMP(check_args(L, 3, "set_header")); |
| 6411 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6412 | |
| 6413 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6414 | WILL_LJMP(lua_error(L)); |
| 6415 | |
| 6416 | hlua_http_del_hdr(L, msg); |
| 6417 | return hlua_http_add_hdr(L, msg); |
| 6418 | } |
| 6419 | |
| 6420 | /* Rewrites the request method. It relies on http_req_replace_stline(). */ |
| 6421 | __LJMP static int hlua_http_msg_set_meth(lua_State *L) |
| 6422 | { |
| 6423 | struct stream *s; |
| 6424 | struct http_msg *msg; |
| 6425 | const char *name; |
| 6426 | size_t name_len; |
| 6427 | |
| 6428 | MAY_LJMP(check_args(L, 2, "set_method")); |
| 6429 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6430 | name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 6431 | |
| 6432 | if ((msg->chn->flags & CF_ISRESP) || msg->msg_state > HTTP_MSG_BODY) |
| 6433 | WILL_LJMP(lua_error(L)); |
| 6434 | |
| 6435 | s = chn_strm(msg->chn); |
| 6436 | lua_pushboolean(L, http_req_replace_stline(0, name, name_len, s->be, s) != -1); |
| 6437 | return 1; |
| 6438 | } |
| 6439 | |
| 6440 | /* Rewrites the request path. It relies on http_req_replace_stline(). */ |
| 6441 | __LJMP static int hlua_http_msg_set_path(lua_State *L) |
| 6442 | { |
| 6443 | struct stream *s; |
| 6444 | struct http_msg *msg; |
| 6445 | const char *name; |
| 6446 | size_t name_len; |
| 6447 | |
| 6448 | MAY_LJMP(check_args(L, 2, "set_path")); |
| 6449 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6450 | name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 6451 | |
| 6452 | if ((msg->chn->flags & CF_ISRESP) || msg->msg_state > HTTP_MSG_BODY) |
| 6453 | WILL_LJMP(lua_error(L)); |
| 6454 | |
| 6455 | s = chn_strm(msg->chn); |
| 6456 | lua_pushboolean(L, http_req_replace_stline(1, name, name_len, s->be, s) != -1); |
| 6457 | return 1; |
| 6458 | } |
| 6459 | |
| 6460 | /* Rewrites the request query-string. It relies on http_req_replace_stline(). */ |
| 6461 | __LJMP static int hlua_http_msg_set_query(lua_State *L) |
| 6462 | { |
| 6463 | struct stream *s; |
| 6464 | struct http_msg *msg; |
| 6465 | const char *name; |
| 6466 | size_t name_len; |
| 6467 | |
| 6468 | MAY_LJMP(check_args(L, 2, "set_query")); |
| 6469 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6470 | name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 6471 | |
| 6472 | if ((msg->chn->flags & CF_ISRESP) || msg->msg_state > HTTP_MSG_BODY) |
| 6473 | WILL_LJMP(lua_error(L)); |
| 6474 | |
| 6475 | /* Check length. */ |
| 6476 | if (name_len > trash.size - 1) { |
| 6477 | lua_pushboolean(L, 0); |
| 6478 | return 1; |
| 6479 | } |
| 6480 | |
| 6481 | /* Add the mark question as prefix. */ |
| 6482 | chunk_reset(&trash); |
| 6483 | trash.area[trash.data++] = '?'; |
| 6484 | memcpy(trash.area + trash.data, name, name_len); |
| 6485 | trash.data += name_len; |
| 6486 | |
| 6487 | s = chn_strm(msg->chn); |
| 6488 | lua_pushboolean(L, http_req_replace_stline(2, trash.area, trash.data, s->be, s) != -1); |
| 6489 | return 1; |
| 6490 | } |
| 6491 | |
| 6492 | /* Rewrites the request URI. It relies on http_req_replace_stline(). */ |
| 6493 | __LJMP static int hlua_http_msg_set_uri(lua_State *L) |
| 6494 | { |
| 6495 | struct stream *s; |
| 6496 | struct http_msg *msg; |
| 6497 | const char *name; |
| 6498 | size_t name_len; |
| 6499 | |
| 6500 | MAY_LJMP(check_args(L, 2, "set_uri")); |
| 6501 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6502 | name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 6503 | |
| 6504 | if ((msg->chn->flags & CF_ISRESP) || msg->msg_state > HTTP_MSG_BODY) |
| 6505 | WILL_LJMP(lua_error(L)); |
| 6506 | |
| 6507 | s = chn_strm(msg->chn); |
| 6508 | lua_pushboolean(L, http_req_replace_stline(3, name, name_len, s->be, s) != -1); |
| 6509 | return 1; |
| 6510 | } |
| 6511 | |
| 6512 | /* Rewrites the response status code. It relies on http_res_set_status(). */ |
| 6513 | __LJMP static int hlua_http_msg_set_status(lua_State *L) |
| 6514 | { |
| 6515 | struct http_msg *msg; |
| 6516 | unsigned int code; |
| 6517 | const char *reason; |
| 6518 | size_t reason_len; |
| 6519 | |
| 6520 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6521 | code = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 6522 | reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, &reason_len)); |
| 6523 | |
| 6524 | if (!(msg->chn->flags & CF_ISRESP) || msg->msg_state > HTTP_MSG_BODY) |
| 6525 | WILL_LJMP(lua_error(L)); |
| 6526 | |
| 6527 | lua_pushboolean(L, http_res_set_status(code, ist2(reason, reason_len), chn_strm(msg->chn)) != -1); |
| 6528 | return 1; |
| 6529 | } |
| 6530 | |
| 6531 | /* Returns true if the HTTP message is full. */ |
| 6532 | __LJMP static int hlua_http_msg_is_full(lua_State *L) |
| 6533 | { |
| 6534 | struct http_msg *msg; |
| 6535 | |
| 6536 | MAY_LJMP(check_args(L, 1, "is_full")); |
| 6537 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6538 | lua_pushboolean(L, channel_full(msg->chn, 0)); |
| 6539 | return 1; |
| 6540 | } |
| 6541 | |
| 6542 | /* Returns true if the HTTP message may still receive data. */ |
| 6543 | __LJMP static int hlua_http_msg_may_recv(lua_State *L) |
| 6544 | { |
| 6545 | struct http_msg *msg; |
| 6546 | struct htx *htx; |
| 6547 | |
| 6548 | MAY_LJMP(check_args(L, 1, "may_recv")); |
| 6549 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6550 | htx = htxbuf(&msg->chn->buf); |
| 6551 | lua_pushboolean(L, (htx_expect_more(htx) && !channel_input_closed(msg->chn) && channel_may_recv(msg->chn))); |
| 6552 | return 1; |
| 6553 | } |
| 6554 | |
| 6555 | /* Returns true if the HTTP message EOM was received */ |
| 6556 | __LJMP static int hlua_http_msg_is_eom(lua_State *L) |
| 6557 | { |
| 6558 | struct http_msg *msg; |
| 6559 | struct htx *htx; |
| 6560 | |
| 6561 | MAY_LJMP(check_args(L, 1, "may_recv")); |
| 6562 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6563 | htx = htxbuf(&msg->chn->buf); |
| 6564 | lua_pushboolean(L, !htx_expect_more(htx)); |
| 6565 | return 1; |
| 6566 | } |
| 6567 | |
| 6568 | /* Returns the number of bytes available in the input side of the HTTP |
| 6569 | * message. This function never fails. |
| 6570 | */ |
| 6571 | __LJMP static int hlua_http_msg_get_in_len(lua_State *L) |
| 6572 | { |
| 6573 | struct http_msg *msg; |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 6574 | size_t output, input; |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6575 | |
| 6576 | MAY_LJMP(check_args(L, 1, "input")); |
| 6577 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 6578 | hlua_http_msg_filter(L, 1, msg, &output, &input); |
| 6579 | lua_pushinteger(L, input); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6580 | return 1; |
| 6581 | } |
| 6582 | |
| 6583 | /* Returns the number of bytes available in the output side of the HTTP |
| 6584 | * message. This function never fails. |
| 6585 | */ |
| 6586 | __LJMP static int hlua_http_msg_get_out_len(lua_State *L) |
| 6587 | { |
| 6588 | struct http_msg *msg; |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 6589 | size_t output, input; |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6590 | |
| 6591 | MAY_LJMP(check_args(L, 1, "output")); |
| 6592 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 6593 | hlua_http_msg_filter(L, 1, msg, &output, &input); |
| 6594 | lua_pushinteger(L, output); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6595 | return 1; |
| 6596 | } |
| 6597 | |
| 6598 | /* Copies at most <len> bytes of DATA blocks from the HTTP message <msg> |
| 6599 | * 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] | 6600 | * 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] | 6601 | * block. This function is called during the payload filtering, so the headers |
| 6602 | * are already scheduled for output (from the filter point of view). |
| 6603 | */ |
| 6604 | static int _hlua_http_msg_dup(struct http_msg *msg, lua_State *L, size_t offset, size_t len) |
| 6605 | { |
| 6606 | struct htx *htx = htxbuf(&msg->chn->buf); |
| 6607 | struct htx_blk *blk; |
| 6608 | struct htx_ret htxret; |
| 6609 | luaL_Buffer b; |
| 6610 | int ret = 0; |
| 6611 | |
| 6612 | luaL_buffinit(L, &b); |
| 6613 | htxret = htx_find_offset(htx, offset); |
| 6614 | for (blk = htxret.blk, offset = htxret.ret; blk && len; blk = htx_get_next_blk(htx, blk)) { |
| 6615 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 6616 | struct ist v; |
| 6617 | |
| 6618 | switch (type) { |
| 6619 | case HTX_BLK_UNUSED: |
| 6620 | break; |
| 6621 | |
| 6622 | case HTX_BLK_DATA: |
| 6623 | v = htx_get_blk_value(htx, blk); |
Tim Duesterhus | b113b5c | 2021-09-15 13:58:44 +0200 | [diff] [blame] | 6624 | v = istadv(v, offset); |
Tim Duesterhus | 2471f5c | 2021-11-08 09:05:01 +0100 | [diff] [blame] | 6625 | v = isttrim(v, len); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6626 | |
| 6627 | luaL_addlstring(&b, v.ptr, v.len); |
| 6628 | ret += v.len; |
| 6629 | break; |
| 6630 | |
| 6631 | default: |
vishnu | 0af4bd7 | 2021-10-24 06:46:24 +0530 | [diff] [blame] | 6632 | if (!ret) |
| 6633 | goto no_data; |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6634 | goto end; |
| 6635 | } |
| 6636 | offset = 0; |
| 6637 | } |
| 6638 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6639 | end: |
vishnu | 0af4bd7 | 2021-10-24 06:46:24 +0530 | [diff] [blame] | 6640 | if (!ret && (htx->flags & HTX_FL_EOM)) |
| 6641 | goto no_data; |
| 6642 | luaL_pushresult(&b); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6643 | return ret; |
vishnu | 0af4bd7 | 2021-10-24 06:46:24 +0530 | [diff] [blame] | 6644 | |
| 6645 | no_data: |
| 6646 | /* Remove the empty string and push nil on the stack */ |
| 6647 | lua_pop(L, 1); |
| 6648 | lua_pushnil(L); |
| 6649 | return 0; |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6650 | } |
| 6651 | |
| 6652 | /* Copies the string <str> to the HTTP message <msg> at the offset |
| 6653 | * <offset>. This function returns -1 if data cannot be copied. Otherwise, it |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 6654 | * returns the amount of data written. This function is responsible to update |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6655 | * the filter context. |
| 6656 | */ |
| 6657 | static int _hlua_http_msg_insert(struct http_msg *msg, struct filter *filter, struct ist str, size_t offset) |
| 6658 | { |
| 6659 | struct htx *htx = htx_from_buf(&msg->chn->buf); |
| 6660 | struct htx_ret htxret; |
| 6661 | int /*max, */ret = 0; |
| 6662 | |
| 6663 | /* Nothing to do, just return */ |
| 6664 | if (unlikely(istlen(str) == 0)) |
| 6665 | goto end; |
| 6666 | |
| 6667 | if (istlen(str) > htx_free_data_space(htx)) { |
| 6668 | ret = -1; |
| 6669 | goto end; |
| 6670 | } |
| 6671 | |
| 6672 | htxret = htx_find_offset(htx, offset); |
| 6673 | if (!htxret.blk || htx_get_blk_type(htxret.blk) != HTX_BLK_DATA) { |
| 6674 | if (!htx_add_last_data(htx, str)) |
| 6675 | goto end; |
| 6676 | } |
| 6677 | else { |
| 6678 | struct ist v = htx_get_blk_value(htx, htxret.blk); |
| 6679 | v.ptr += htxret.ret; |
| 6680 | v.len = 0; |
| 6681 | if (!htx_replace_blk_value(htx, htxret.blk, v, str)) |
| 6682 | goto end; |
| 6683 | } |
| 6684 | ret = str.len; |
| 6685 | if (ret) { |
| 6686 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 6687 | flt_update_offsets(filter, msg->chn, ret); |
| 6688 | flt_ctx->cur_len[CHN_IDX(msg->chn)] += ret; |
| 6689 | } |
| 6690 | |
| 6691 | end: |
| 6692 | htx_to_buf(htx, &msg->chn->buf); |
| 6693 | return ret; |
| 6694 | } |
| 6695 | |
| 6696 | /* Helper function removing at most <len> bytes of DATA blocks at the absolute |
| 6697 | * position <offset>. It stops on the first non-DATA HTX block. This function is |
| 6698 | * called during the payload filtering, so the headers are already scheduled for |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 6699 | * output (from the filter point of view). This function is responsible to |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6700 | * update the filter context. |
| 6701 | */ |
| 6702 | static void _hlua_http_msg_delete(struct http_msg *msg, struct filter *filter, size_t offset, size_t len) |
| 6703 | { |
| 6704 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 6705 | struct htx *htx = htx_from_buf(&msg->chn->buf); |
| 6706 | struct htx_blk *blk; |
| 6707 | struct htx_ret htxret; |
| 6708 | size_t ret = 0; |
| 6709 | |
| 6710 | /* Be sure <len> is always the amount of DATA to remove */ |
| 6711 | 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] | 6712 | /* When htx tail type == HTX_BLK_DATA, no need to take care |
| 6713 | * of special blocks like HTX_BLK_EOT. |
| 6714 | * We simply truncate after offset |
| 6715 | * (truncate targeted blk and discard the following ones) |
| 6716 | */ |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6717 | htx_truncate(htx, offset); |
| 6718 | ret = len; |
| 6719 | goto end; |
| 6720 | } |
| 6721 | |
| 6722 | htxret = htx_find_offset(htx, offset); |
| 6723 | blk = htxret.blk; |
| 6724 | if (htxret.ret) { |
Aurelien DARRAGON | bcbcf98 | 2022-09-28 19:08:15 +0200 | [diff] [blame] | 6725 | /* dealing with offset: we need to trim targeted blk */ |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6726 | struct ist v; |
| 6727 | |
| 6728 | if (htx_get_blk_type(blk) != HTX_BLK_DATA) |
| 6729 | goto end; |
Aurelien DARRAGON | bcbcf98 | 2022-09-28 19:08:15 +0200 | [diff] [blame] | 6730 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6731 | v = htx_get_blk_value(htx, blk); |
Tim Duesterhus | a029d78 | 2022-10-08 12:33:18 +0200 | [diff] [blame] | 6732 | v = istadv(v, htxret.ret); |
Aurelien DARRAGON | bcbcf98 | 2022-09-28 19:08:15 +0200 | [diff] [blame] | 6733 | |
Tim Duesterhus | 2471f5c | 2021-11-08 09:05:01 +0100 | [diff] [blame] | 6734 | v = isttrim(v, len); |
Aurelien DARRAGON | bcbcf98 | 2022-09-28 19:08:15 +0200 | [diff] [blame] | 6735 | /* trimming data in blk: discard everything after the offset |
| 6736 | * (replace 'v' with 'IST_NULL') |
| 6737 | */ |
Tim Duesterhus | b113b5c | 2021-09-15 13:58:44 +0200 | [diff] [blame] | 6738 | blk = htx_replace_blk_value(htx, blk, v, IST_NULL); |
Aurelien DARRAGON | bcbcf98 | 2022-09-28 19:08:15 +0200 | [diff] [blame] | 6739 | if (blk && v.len < len) { |
| 6740 | /* In this case, caller wants to keep removing data, |
| 6741 | * but we need to spare current blk |
| 6742 | * because it was already trimmed |
| 6743 | */ |
| 6744 | blk = htx_get_next_blk(htx, blk); |
| 6745 | } |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6746 | len -= v.len; |
| 6747 | ret += v.len; |
| 6748 | } |
| 6749 | |
| 6750 | |
| 6751 | while (blk && len) { |
Aurelien DARRAGON | bcbcf98 | 2022-09-28 19:08:15 +0200 | [diff] [blame] | 6752 | /* there is more data that needs to be discarded */ |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6753 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 6754 | uint32_t sz = htx_get_blksz(blk); |
| 6755 | |
| 6756 | switch (type) { |
| 6757 | case HTX_BLK_UNUSED: |
| 6758 | break; |
| 6759 | |
| 6760 | case HTX_BLK_DATA: |
| 6761 | if (len < sz) { |
Aurelien DARRAGON | bcbcf98 | 2022-09-28 19:08:15 +0200 | [diff] [blame] | 6762 | /* don't discard whole blk, only part of it |
| 6763 | * (from the beginning) |
| 6764 | */ |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6765 | htx_cut_data_blk(htx, blk, len); |
| 6766 | ret += len; |
| 6767 | goto end; |
| 6768 | } |
| 6769 | break; |
| 6770 | |
| 6771 | default: |
Aurelien DARRAGON | bcbcf98 | 2022-09-28 19:08:15 +0200 | [diff] [blame] | 6772 | /* HTX_BLK_EOT blk won't be removed */ |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6773 | goto end; |
| 6774 | } |
| 6775 | |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 6776 | /* Remove all the data block */ |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6777 | len -= sz; |
| 6778 | ret += sz; |
| 6779 | blk = htx_remove_blk(htx, blk); |
| 6780 | } |
| 6781 | |
| 6782 | end: |
| 6783 | flt_update_offsets(filter, msg->chn, -ret); |
| 6784 | flt_ctx->cur_len[CHN_IDX(msg->chn)] -= ret; |
| 6785 | /* WARNING: we don't call htx_to_buf() on purpose, because we don't want |
| 6786 | * to loose the EOM flag if the message is empty. |
| 6787 | */ |
| 6788 | } |
| 6789 | |
| 6790 | /* Copies input data found in an HTTP message. Unlike the channel function used |
| 6791 | * to duplicate raw data, this one can only be called inside a filter, from |
| 6792 | * http_payload callback. So it cannot yield. An exception is returned if it is |
| 6793 | * called from another callback. If nothing was copied, a nil value is pushed on |
| 6794 | * the stack. |
| 6795 | */ |
| 6796 | __LJMP static int hlua_http_msg_get_body(lua_State *L) |
| 6797 | { |
| 6798 | struct http_msg *msg; |
| 6799 | struct filter *filter; |
| 6800 | size_t output, input; |
| 6801 | int offset, len; |
| 6802 | |
| 6803 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 6804 | WILL_LJMP(luaL_error(L, "'data' expects at most 2 arguments")); |
| 6805 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6806 | |
| 6807 | if (msg->msg_state < HTTP_MSG_DATA) |
| 6808 | WILL_LJMP(lua_error(L)); |
| 6809 | |
| 6810 | filter = hlua_http_msg_filter(L, 1, msg, &output, &input); |
| 6811 | if (!filter || !hlua_filter_from_payload(filter)) |
| 6812 | WILL_LJMP(lua_error(L)); |
| 6813 | |
| 6814 | if (!ci_data(msg->chn) && channel_input_closed(msg->chn)) { |
| 6815 | lua_pushnil(L); |
| 6816 | return 1; |
| 6817 | } |
| 6818 | |
| 6819 | offset = output; |
| 6820 | if (lua_gettop(L) > 1) { |
| 6821 | offset = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 6822 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 6823 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6824 | offset += output; |
| 6825 | if (offset < output || offset > input + output) { |
| 6826 | lua_pushfstring(L, "offset out of range."); |
| 6827 | WILL_LJMP(lua_error(L)); |
| 6828 | } |
| 6829 | } |
| 6830 | len = output + input - offset; |
| 6831 | if (lua_gettop(L) == 3) { |
| 6832 | len = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 6833 | if (!len) |
| 6834 | goto dup; |
| 6835 | if (len == -1) |
| 6836 | len = global.tune.bufsize; |
| 6837 | if (len < 0) { |
| 6838 | lua_pushfstring(L, "length out of range."); |
| 6839 | WILL_LJMP(lua_error(L)); |
| 6840 | } |
| 6841 | } |
| 6842 | |
| 6843 | dup: |
| 6844 | _hlua_http_msg_dup(msg, L, offset, len); |
| 6845 | return 1; |
| 6846 | } |
| 6847 | |
| 6848 | /* Appends a string to the HTTP message, after all existing DATA blocks but |
| 6849 | * before the trailers, if any. It returns the amount of data written or -1 if |
| 6850 | * nothing was copied. Unlike the channel function used to append data, this one |
| 6851 | * can only be called inside a filter, from http_payload callback. So it cannot |
| 6852 | * yield. An exception is returned if it is called from another callback. |
| 6853 | */ |
| 6854 | __LJMP static int hlua_http_msg_append(lua_State *L) |
| 6855 | { |
| 6856 | struct http_msg *msg; |
| 6857 | struct filter *filter; |
| 6858 | const char *str; |
| 6859 | size_t offset, len, sz; |
| 6860 | int ret; |
| 6861 | |
| 6862 | MAY_LJMP(check_args(L, 2, "append")); |
| 6863 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6864 | |
| 6865 | if (msg->msg_state < HTTP_MSG_DATA) |
| 6866 | WILL_LJMP(lua_error(L)); |
| 6867 | |
| 6868 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 6869 | filter = hlua_http_msg_filter(L, 1, msg, &offset, &len); |
| 6870 | if (!filter || !hlua_filter_from_payload(filter)) |
| 6871 | WILL_LJMP(lua_error(L)); |
| 6872 | |
| 6873 | ret = _hlua_http_msg_insert(msg, filter, ist2(str, sz), offset+len); |
| 6874 | lua_pushinteger(L, ret); |
| 6875 | return 1; |
| 6876 | } |
| 6877 | |
| 6878 | /* Prepends a string to the HTTP message, before all existing DATA blocks. It |
| 6879 | * returns the amount of data written or -1 if nothing was copied. Unlike the |
| 6880 | * channel function used to prepend data, this one can only be called inside a |
| 6881 | * filter, from http_payload callback. So it cannot yield. An exception is |
| 6882 | * returned if it is called from another callback. |
| 6883 | */ |
| 6884 | __LJMP static int hlua_http_msg_prepend(lua_State *L) |
| 6885 | { |
| 6886 | struct http_msg *msg; |
| 6887 | struct filter *filter; |
| 6888 | const char *str; |
| 6889 | size_t offset, len, sz; |
| 6890 | int ret; |
| 6891 | |
| 6892 | MAY_LJMP(check_args(L, 2, "prepend")); |
| 6893 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6894 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6895 | if (msg->msg_state < HTTP_MSG_DATA) |
| 6896 | WILL_LJMP(lua_error(L)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6897 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6898 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 6899 | filter = hlua_http_msg_filter(L, 1, msg, &offset, &len); |
| 6900 | if (!filter || !hlua_filter_from_payload(filter)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6901 | WILL_LJMP(lua_error(L)); |
| 6902 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6903 | ret = _hlua_http_msg_insert(msg, filter, ist2(str, sz), offset); |
| 6904 | lua_pushinteger(L, ret); |
| 6905 | return 1; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6906 | } |
| 6907 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6908 | /* Inserts a string to the HTTP message at a given offset. By default the string |
| 6909 | * is appended at the end of DATA blocks. It returns the amount of data written |
| 6910 | * or -1 if nothing was copied. Unlike the channel function used to insert data, |
| 6911 | * this one can only be called inside a filter, from http_payload callback. So |
| 6912 | * it cannot yield. An exception is returned if it is called from another |
| 6913 | * callback. |
| 6914 | */ |
| 6915 | __LJMP static int hlua_http_msg_insert_data(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6916 | { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6917 | struct http_msg *msg; |
| 6918 | struct filter *filter; |
| 6919 | const char *str; |
| 6920 | size_t input, output, sz; |
| 6921 | int offset; |
| 6922 | int ret; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6923 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6924 | if (lua_gettop(L) < 2 || lua_gettop(L) > 3) |
| 6925 | 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] | 6926 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6927 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6928 | if (msg->msg_state < HTTP_MSG_DATA) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6929 | WILL_LJMP(lua_error(L)); |
| 6930 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6931 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
Aurelien DARRAGON | 7fdba0a | 2022-09-28 16:03:45 +0200 | [diff] [blame] | 6932 | filter = hlua_http_msg_filter(L, 1, msg, &output, &input); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6933 | if (!filter || !hlua_filter_from_payload(filter)) |
| 6934 | WILL_LJMP(lua_error(L)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6935 | |
Aurelien DARRAGON | 7fdba0a | 2022-09-28 16:03:45 +0200 | [diff] [blame] | 6936 | offset = output; |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6937 | if (lua_gettop(L) > 2) { |
| 6938 | offset = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 6939 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 6940 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6941 | offset += output; |
Aurelien DARRAGON | 7fdba0a | 2022-09-28 16:03:45 +0200 | [diff] [blame] | 6942 | if (offset > output + input) { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6943 | lua_pushfstring(L, "offset out of range."); |
| 6944 | WILL_LJMP(lua_error(L)); |
| 6945 | } |
| 6946 | } |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6947 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6948 | ret = _hlua_http_msg_insert(msg, filter, ist2(str, sz), offset); |
| 6949 | lua_pushinteger(L, ret); |
| 6950 | return 1; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6951 | } |
| 6952 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6953 | /* Removes a given amount of data from the HTTP message at a given offset. By |
| 6954 | * default all DATA blocks are removed. It returns the amount of data |
| 6955 | * removed. Unlike the channel function used to remove data, this one can only |
| 6956 | * be called inside a filter, from http_payload callback. So it cannot yield. An |
| 6957 | * exception is returned if it is called from another callback. |
| 6958 | */ |
| 6959 | __LJMP static int hlua_http_msg_del_data(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6960 | { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6961 | struct http_msg *msg; |
| 6962 | struct filter *filter; |
| 6963 | size_t input, output; |
| 6964 | int offset, len; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6965 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6966 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
Boyang Li | e0c5435 | 2022-05-10 17:47:23 +0000 | [diff] [blame] | 6967 | WILL_LJMP(luaL_error(L, "'remove' expects at most 2 arguments")); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6968 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6969 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6970 | if (msg->msg_state < HTTP_MSG_DATA) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6971 | WILL_LJMP(lua_error(L)); |
| 6972 | |
Aurelien DARRAGON | d7c71b0 | 2022-09-28 15:52:18 +0200 | [diff] [blame] | 6973 | filter = hlua_http_msg_filter(L, 1, msg, &output, &input); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6974 | if (!filter || !hlua_filter_from_payload(filter)) |
| 6975 | WILL_LJMP(lua_error(L)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6976 | |
Aurelien DARRAGON | d7c71b0 | 2022-09-28 15:52:18 +0200 | [diff] [blame] | 6977 | offset = output; |
Boyang Li | e0c5435 | 2022-05-10 17:47:23 +0000 | [diff] [blame] | 6978 | if (lua_gettop(L) > 1) { |
| 6979 | offset = MAY_LJMP(luaL_checkinteger(L, 2)); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6980 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 6981 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6982 | offset += output; |
Aurelien DARRAGON | d7c71b0 | 2022-09-28 15:52:18 +0200 | [diff] [blame] | 6983 | if (offset > output + input) { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6984 | lua_pushfstring(L, "offset out of range."); |
| 6985 | WILL_LJMP(lua_error(L)); |
| 6986 | } |
| 6987 | } |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6988 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6989 | len = output + input - offset; |
Boyang Li | e0c5435 | 2022-05-10 17:47:23 +0000 | [diff] [blame] | 6990 | if (lua_gettop(L) == 3) { |
| 6991 | len = MAY_LJMP(luaL_checkinteger(L, 3)); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6992 | if (!len) |
| 6993 | goto end; |
| 6994 | if (len == -1) |
| 6995 | len = output + input - offset; |
| 6996 | if (len < 0 || offset + len > output + input) { |
| 6997 | lua_pushfstring(L, "length out of range."); |
| 6998 | WILL_LJMP(lua_error(L)); |
| 6999 | } |
| 7000 | } |
| 7001 | |
| 7002 | _hlua_http_msg_delete(msg, filter, offset, len); |
| 7003 | |
| 7004 | end: |
| 7005 | lua_pushinteger(L, len); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7006 | return 1; |
| 7007 | } |
| 7008 | |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 7009 | /* 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] | 7010 | * all remaining data are removed, accordingly to the filter context. It returns |
| 7011 | * the amount of data written or -1 if nothing was copied. Unlike the channel |
| 7012 | * function used to replace data, this one can only be called inside a filter, |
| 7013 | * from http_payload callback. So it cannot yield. An exception is returned if |
| 7014 | * it is called from another callback. |
| 7015 | */ |
| 7016 | __LJMP static int hlua_http_msg_set_data(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7017 | { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 7018 | struct http_msg *msg; |
| 7019 | struct filter *filter; |
| 7020 | struct htx *htx; |
| 7021 | const char *str; |
| 7022 | size_t input, output, sz; |
| 7023 | int offset, len; |
| 7024 | int ret; |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 7025 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 7026 | if (lua_gettop(L) < 2 || lua_gettop(L) > 4) |
| 7027 | WILL_LJMP(luaL_error(L, "'set' expects at least 1 argument and at most 3 arguments")); |
| 7028 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 7029 | |
| 7030 | if (msg->msg_state < HTTP_MSG_DATA) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 7031 | WILL_LJMP(lua_error(L)); |
| 7032 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 7033 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 7034 | filter = hlua_http_msg_filter(L, 1, msg, &output, &input); |
| 7035 | if (!filter || !hlua_filter_from_payload(filter)) |
| 7036 | WILL_LJMP(lua_error(L)); |
| 7037 | |
| 7038 | offset = output; |
| 7039 | if (lua_gettop(L) > 2) { |
| 7040 | offset = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 7041 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 7042 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 7043 | offset += output; |
| 7044 | if (offset < output || offset > input + output) { |
| 7045 | lua_pushfstring(L, "offset out of range."); |
| 7046 | WILL_LJMP(lua_error(L)); |
| 7047 | } |
| 7048 | } |
| 7049 | |
| 7050 | len = output + input - offset; |
| 7051 | if (lua_gettop(L) == 4) { |
| 7052 | len = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 7053 | if (!len) |
| 7054 | goto set; |
| 7055 | if (len == -1) |
| 7056 | len = output + input - offset; |
| 7057 | if (len < 0 || offset + len > output + input) { |
| 7058 | lua_pushfstring(L, "length out of range."); |
| 7059 | WILL_LJMP(lua_error(L)); |
| 7060 | } |
| 7061 | } |
| 7062 | |
| 7063 | set: |
| 7064 | /* Be sure we can copied the string once input data will be removed. */ |
| 7065 | htx = htx_from_buf(&msg->chn->buf); |
| 7066 | if (sz > htx_free_data_space(htx) + len) |
| 7067 | lua_pushinteger(L, -1); |
| 7068 | else { |
| 7069 | _hlua_http_msg_delete(msg, filter, offset, len); |
| 7070 | ret = _hlua_http_msg_insert(msg, filter, ist2(str, sz), offset); |
| 7071 | lua_pushinteger(L, ret); |
| 7072 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7073 | return 1; |
| 7074 | } |
| 7075 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 7076 | /* Prepends data into an HTTP message and forward it, from the filter point of |
| 7077 | * view. It returns the amount of data written or -1 if nothing was sent. Unlike |
| 7078 | * the channel function used to send data, this one can only be called inside a |
| 7079 | * filter, from http_payload callback. So it cannot yield. An exception is |
| 7080 | * returned if it is called from another callback. |
| 7081 | */ |
| 7082 | __LJMP static int hlua_http_msg_send(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7083 | { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 7084 | struct http_msg *msg; |
| 7085 | struct filter *filter; |
| 7086 | struct htx *htx; |
| 7087 | const char *str; |
| 7088 | size_t offset, len, sz; |
| 7089 | int ret; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7090 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 7091 | MAY_LJMP(check_args(L, 2, "send")); |
| 7092 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 7093 | |
| 7094 | if (msg->msg_state < HTTP_MSG_DATA) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 7095 | WILL_LJMP(lua_error(L)); |
| 7096 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 7097 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 7098 | filter = hlua_http_msg_filter(L, 1, msg, &offset, &len); |
| 7099 | if (!filter || !hlua_filter_from_payload(filter)) |
| 7100 | WILL_LJMP(lua_error(L)); |
| 7101 | |
| 7102 | /* Return an error if the channel's output is closed */ |
| 7103 | if (unlikely(channel_output_closed(msg->chn))) { |
| 7104 | lua_pushinteger(L, -1); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7105 | return 1; |
| 7106 | } |
| 7107 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 7108 | htx = htx_from_buf(&msg->chn->buf); |
| 7109 | if (sz > htx_free_data_space(htx)) { |
| 7110 | lua_pushinteger(L, -1); |
| 7111 | return 1; |
| 7112 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7113 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 7114 | ret = _hlua_http_msg_insert(msg, filter, ist2(str, sz), offset); |
| 7115 | if (ret > 0) { |
| 7116 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 7117 | |
| 7118 | FLT_OFF(filter, msg->chn) += ret; |
| 7119 | flt_ctx->cur_len[CHN_IDX(msg->chn)] -= ret; |
| 7120 | flt_ctx->cur_off[CHN_IDX(msg->chn)] += ret; |
| 7121 | } |
| 7122 | |
| 7123 | lua_pushinteger(L, ret); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7124 | return 1; |
| 7125 | } |
| 7126 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 7127 | /* Forwards a given amount of bytes. It return -1 if the channel's output is |
| 7128 | * closed. Otherwise, it returns the number of bytes forwarded. Unlike the |
| 7129 | * channel function used to forward data, this one can only be called inside a |
| 7130 | * filter, from http_payload callback. So it cannot yield. An exception is |
| 7131 | * returned if it is called from another callback. All other functions deal with |
| 7132 | * DATA block, this one not. |
| 7133 | */ |
| 7134 | __LJMP static int hlua_http_msg_forward(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7135 | { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 7136 | struct http_msg *msg; |
| 7137 | struct filter *filter; |
| 7138 | size_t offset, len; |
| 7139 | int fwd, ret = 0; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7140 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 7141 | MAY_LJMP(check_args(L, 2, "forward")); |
| 7142 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 7143 | |
| 7144 | if (msg->msg_state < HTTP_MSG_DATA) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 7145 | WILL_LJMP(lua_error(L)); |
| 7146 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 7147 | fwd = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 7148 | filter = hlua_http_msg_filter(L, 1, msg, &offset, &len); |
| 7149 | if (!filter || !hlua_filter_from_payload(filter)) |
| 7150 | WILL_LJMP(lua_error(L)); |
| 7151 | |
| 7152 | /* Nothing to do, just return */ |
| 7153 | if (!fwd) |
| 7154 | goto end; |
| 7155 | |
| 7156 | /* Return an error if the channel's output is closed */ |
| 7157 | if (unlikely(channel_output_closed(msg->chn))) { |
| 7158 | ret = -1; |
| 7159 | goto end; |
| 7160 | } |
| 7161 | |
| 7162 | ret = fwd; |
| 7163 | if (ret > len) |
| 7164 | ret = len; |
| 7165 | |
| 7166 | if (ret) { |
| 7167 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 7168 | |
| 7169 | FLT_OFF(filter, msg->chn) += ret; |
| 7170 | flt_ctx->cur_off[CHN_IDX(msg->chn)] += ret; |
| 7171 | flt_ctx->cur_len[CHN_IDX(msg->chn)] -= ret; |
| 7172 | } |
| 7173 | |
| 7174 | end: |
| 7175 | lua_pushinteger(L, ret); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7176 | return 1; |
| 7177 | } |
| 7178 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 7179 | /* Set EOM flag on the HTX message. |
| 7180 | * |
| 7181 | * NOTE: Not sure it is a good idea to manipulate this flag but for now I don't |
| 7182 | * really know how to do without this feature. |
| 7183 | */ |
| 7184 | __LJMP static int hlua_http_msg_set_eom(lua_State *L) |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 7185 | { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 7186 | struct http_msg *msg; |
| 7187 | struct htx *htx; |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 7188 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 7189 | MAY_LJMP(check_args(L, 1, "set_eom")); |
| 7190 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 7191 | htx = htxbuf(&msg->chn->buf); |
| 7192 | htx->flags |= HTX_FL_EOM; |
| 7193 | return 0; |
| 7194 | } |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 7195 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 7196 | /* Unset EOM flag on the HTX message. |
| 7197 | * |
| 7198 | * NOTE: Not sure it is a good idea to manipulate this flag but for now I don't |
| 7199 | * really know how to do without this feature. |
| 7200 | */ |
| 7201 | __LJMP static int hlua_http_msg_unset_eom(lua_State *L) |
| 7202 | { |
| 7203 | struct http_msg *msg; |
| 7204 | struct htx *htx; |
| 7205 | |
| 7206 | MAY_LJMP(check_args(L, 1, "set_eom")); |
| 7207 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 7208 | htx = htxbuf(&msg->chn->buf); |
| 7209 | htx->flags &= ~HTX_FL_EOM; |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 7210 | return 0; |
| 7211 | } |
| 7212 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7213 | /* |
| 7214 | * |
| 7215 | * |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7216 | * Class HTTPClient |
| 7217 | * |
| 7218 | * |
| 7219 | */ |
| 7220 | __LJMP static struct hlua_httpclient *hlua_checkhttpclient(lua_State *L, int ud) |
| 7221 | { |
| 7222 | return MAY_LJMP(hlua_checkudata(L, ud, class_httpclient_ref)); |
| 7223 | } |
| 7224 | |
William Lallemand | f77f1de | 2021-09-28 19:10:38 +0200 | [diff] [blame] | 7225 | |
| 7226 | /* stops the httpclient and ask it to kill itself */ |
| 7227 | __LJMP static int hlua_httpclient_gc(lua_State *L) |
| 7228 | { |
| 7229 | struct hlua_httpclient *hlua_hc; |
| 7230 | |
| 7231 | MAY_LJMP(check_args(L, 1, "__gc")); |
| 7232 | |
| 7233 | hlua_hc = MAY_LJMP(hlua_checkhttpclient(L, 1)); |
| 7234 | |
Aurelien DARRAGON | 3ffbf38 | 2023-02-09 17:02:57 +0100 | [diff] [blame] | 7235 | if (MT_LIST_DELETE(&hlua_hc->by_hlua)) { |
| 7236 | /* we won the race against hlua_httpclient_destroy_all() */ |
William Lallemand | bb58142 | 2022-10-20 10:57:28 +0200 | [diff] [blame] | 7237 | httpclient_stop_and_destroy(hlua_hc->hc); |
Aurelien DARRAGON | 3ffbf38 | 2023-02-09 17:02:57 +0100 | [diff] [blame] | 7238 | hlua_hc->hc = NULL; |
| 7239 | } |
William Lallemand | f77f1de | 2021-09-28 19:10:38 +0200 | [diff] [blame] | 7240 | |
| 7241 | return 0; |
| 7242 | } |
| 7243 | |
| 7244 | |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7245 | __LJMP static int hlua_httpclient_new(lua_State *L) |
| 7246 | { |
| 7247 | struct hlua_httpclient *hlua_hc; |
| 7248 | struct hlua *hlua; |
| 7249 | |
| 7250 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 7251 | hlua = hlua_gethlua(L); |
| 7252 | if (!hlua) |
| 7253 | return 0; |
| 7254 | |
| 7255 | /* Check stack size. */ |
| 7256 | if (!lua_checkstack(L, 3)) { |
| 7257 | hlua_pusherror(L, "httpclient: full stack"); |
| 7258 | goto err; |
| 7259 | } |
| 7260 | /* Create the object: obj[0] = userdata. */ |
| 7261 | lua_newtable(L); |
| 7262 | hlua_hc = MAY_LJMP(lua_newuserdata(L, sizeof(*hlua_hc))); |
| 7263 | lua_rawseti(L, -2, 0); |
| 7264 | memset(hlua_hc, 0, sizeof(*hlua_hc)); |
| 7265 | |
| 7266 | hlua_hc->hc = httpclient_new(hlua, 0, IST_NULL); |
| 7267 | if (!hlua_hc->hc) |
| 7268 | goto err; |
| 7269 | |
Aurelien DARRAGON | 3ffbf38 | 2023-02-09 17:02:57 +0100 | [diff] [blame] | 7270 | MT_LIST_APPEND(&hlua->hc_list, &hlua_hc->by_hlua); |
William Lallemand | bb58142 | 2022-10-20 10:57:28 +0200 | [diff] [blame] | 7271 | |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7272 | /* Pop a class stream metatable and affect it to the userdata. */ |
| 7273 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_httpclient_ref); |
| 7274 | lua_setmetatable(L, -2); |
| 7275 | |
| 7276 | return 1; |
| 7277 | |
| 7278 | err: |
| 7279 | WILL_LJMP(lua_error(L)); |
| 7280 | return 0; |
| 7281 | } |
| 7282 | |
| 7283 | |
| 7284 | /* |
| 7285 | * Callback of the httpclient, this callback wakes the lua task up, once the |
| 7286 | * httpclient receives some data |
| 7287 | * |
| 7288 | */ |
| 7289 | |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7290 | static void hlua_httpclient_cb(struct httpclient *hc) |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7291 | { |
| 7292 | struct hlua *hlua = hc->caller; |
| 7293 | |
| 7294 | if (!hlua || !hlua->task) |
| 7295 | return; |
| 7296 | |
| 7297 | task_wakeup(hlua->task, TASK_WOKEN_MSG); |
| 7298 | } |
| 7299 | |
| 7300 | /* |
William Lallemand | d7df73a | 2021-09-23 17:54:00 +0200 | [diff] [blame] | 7301 | * Fill the lua stack with headers from the httpclient response |
| 7302 | * This works the same way as the hlua_http_get_headers() function |
| 7303 | */ |
| 7304 | __LJMP static int hlua_httpclient_get_headers(lua_State *L, struct hlua_httpclient *hlua_hc) |
| 7305 | { |
| 7306 | struct http_hdr *hdr; |
| 7307 | |
| 7308 | lua_newtable(L); |
| 7309 | |
William Lallemand | ef574b2 | 2021-10-05 16:19:31 +0200 | [diff] [blame] | 7310 | for (hdr = hlua_hc->hc->res.hdrs; hdr && isttest(hdr->n); hdr++) { |
William Lallemand | d7df73a | 2021-09-23 17:54:00 +0200 | [diff] [blame] | 7311 | struct ist n, v; |
| 7312 | int len; |
| 7313 | |
| 7314 | n = hdr->n; |
| 7315 | v = hdr->v; |
| 7316 | |
| 7317 | /* Check for existing entry: |
| 7318 | * assume that the table is on the top of the stack, and |
| 7319 | * push the key in the stack, the function lua_gettable() |
| 7320 | * perform the lookup. |
| 7321 | */ |
| 7322 | |
| 7323 | lua_pushlstring(L, n.ptr, n.len); |
| 7324 | lua_gettable(L, -2); |
| 7325 | |
| 7326 | switch (lua_type(L, -1)) { |
| 7327 | case LUA_TNIL: |
| 7328 | /* Table not found, create it. */ |
| 7329 | lua_pop(L, 1); /* remove the nil value. */ |
| 7330 | lua_pushlstring(L, n.ptr, n.len); /* push the header name as key. */ |
| 7331 | lua_newtable(L); /* create and push empty table. */ |
| 7332 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 7333 | lua_rawseti(L, -2, 0); /* index header value (pop it). */ |
| 7334 | lua_rawset(L, -3); /* index new table with header name (pop the values). */ |
| 7335 | break; |
| 7336 | |
| 7337 | case LUA_TTABLE: |
| 7338 | /* Entry found: push the value in the table. */ |
| 7339 | len = lua_rawlen(L, -1); |
| 7340 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 7341 | lua_rawseti(L, -2, len+1); /* index header value (pop it). */ |
| 7342 | lua_pop(L, 1); /* remove the table (it is stored in the main table). */ |
| 7343 | break; |
| 7344 | |
| 7345 | default: |
| 7346 | /* Other cases are errors. */ |
| 7347 | hlua_pusherror(L, "internal error during the parsing of headers."); |
| 7348 | WILL_LJMP(lua_error(L)); |
| 7349 | } |
| 7350 | } |
| 7351 | return 1; |
| 7352 | } |
| 7353 | |
| 7354 | /* |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7355 | * Allocate and return an array of http_hdr ist extracted from the <headers> lua table |
| 7356 | * |
| 7357 | * Caller must free the result |
| 7358 | */ |
| 7359 | struct http_hdr *hlua_httpclient_table_to_hdrs(lua_State *L) |
| 7360 | { |
| 7361 | struct http_hdr hdrs[global.tune.max_http_hdr]; |
| 7362 | struct http_hdr *result = NULL; |
| 7363 | uint32_t hdr_num = 0; |
| 7364 | |
| 7365 | lua_pushnil(L); |
| 7366 | while (lua_next(L, -2) != 0) { |
| 7367 | struct ist name, value; |
| 7368 | const char *n, *v; |
| 7369 | size_t nlen, vlen; |
| 7370 | |
| 7371 | if (!lua_isstring(L, -2) || !lua_istable(L, -1)) { |
| 7372 | /* Skip element if the key is not a string or if the value is not a table */ |
| 7373 | goto next_hdr; |
| 7374 | } |
| 7375 | |
| 7376 | n = lua_tolstring(L, -2, &nlen); |
| 7377 | name = ist2(n, nlen); |
| 7378 | |
| 7379 | /* Loop on header's values */ |
| 7380 | lua_pushnil(L); |
| 7381 | while (lua_next(L, -2)) { |
| 7382 | if (!lua_isstring(L, -1)) { |
| 7383 | /* Skip the value if it is not a string */ |
| 7384 | goto next_value; |
| 7385 | } |
| 7386 | |
| 7387 | v = lua_tolstring(L, -1, &vlen); |
| 7388 | value = ist2(v, vlen); |
| 7389 | name = ist2(n, nlen); |
| 7390 | |
| 7391 | hdrs[hdr_num].n = istdup(name); |
| 7392 | hdrs[hdr_num].v = istdup(value); |
| 7393 | |
| 7394 | hdr_num++; |
| 7395 | |
| 7396 | next_value: |
| 7397 | lua_pop(L, 1); |
| 7398 | } |
| 7399 | |
| 7400 | next_hdr: |
| 7401 | lua_pop(L, 1); |
| 7402 | |
| 7403 | } |
| 7404 | |
| 7405 | if (hdr_num) { |
| 7406 | /* alloc and copy the headers in the httpclient struct */ |
Tim Duesterhus | 16cc16d | 2021-11-06 15:14:45 +0100 | [diff] [blame] | 7407 | result = calloc((hdr_num + 1), sizeof(*result)); |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7408 | if (!result) |
| 7409 | goto skip_headers; |
| 7410 | memcpy(result, hdrs, sizeof(struct http_hdr) * (hdr_num + 1)); |
| 7411 | |
| 7412 | result[hdr_num].n = IST_NULL; |
| 7413 | result[hdr_num].v = IST_NULL; |
| 7414 | } |
| 7415 | |
| 7416 | skip_headers: |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7417 | |
| 7418 | return result; |
| 7419 | } |
| 7420 | |
| 7421 | |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7422 | /* |
| 7423 | * For each yield, checks if there is some data in the httpclient and push them |
| 7424 | * in the lua buffer, once the httpclient finished its job, push the result on |
| 7425 | * the stack |
| 7426 | */ |
| 7427 | __LJMP static int hlua_httpclient_rcv_yield(lua_State *L, int status, lua_KContext ctx) |
| 7428 | { |
| 7429 | struct buffer *tr; |
| 7430 | int res; |
| 7431 | struct hlua *hlua = hlua_gethlua(L); |
| 7432 | struct hlua_httpclient *hlua_hc = hlua_checkhttpclient(L, 1); |
| 7433 | |
| 7434 | |
| 7435 | tr = get_trash_chunk(); |
| 7436 | |
| 7437 | res = httpclient_res_xfer(hlua_hc->hc, tr); |
| 7438 | luaL_addlstring(&hlua_hc->b, b_orig(tr), res); |
| 7439 | |
| 7440 | if (!httpclient_data(hlua_hc->hc) && httpclient_ended(hlua_hc->hc)) { |
| 7441 | |
| 7442 | luaL_pushresult(&hlua_hc->b); |
| 7443 | lua_settable(L, -3); |
| 7444 | |
| 7445 | lua_pushstring(L, "status"); |
| 7446 | lua_pushinteger(L, hlua_hc->hc->res.status); |
| 7447 | lua_settable(L, -3); |
| 7448 | |
| 7449 | |
| 7450 | lua_pushstring(L, "reason"); |
| 7451 | lua_pushlstring(L, hlua_hc->hc->res.reason.ptr, hlua_hc->hc->res.reason.len); |
| 7452 | lua_settable(L, -3); |
| 7453 | |
| 7454 | lua_pushstring(L, "headers"); |
| 7455 | hlua_httpclient_get_headers(L, hlua_hc); |
| 7456 | lua_settable(L, -3); |
| 7457 | |
| 7458 | return 1; |
| 7459 | } |
| 7460 | |
| 7461 | if (httpclient_data(hlua_hc->hc)) |
| 7462 | task_wakeup(hlua->task, TASK_WOKEN_MSG); |
| 7463 | |
| 7464 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_httpclient_rcv_yield, TICK_ETERNITY, 0)); |
| 7465 | |
| 7466 | return 0; |
| 7467 | } |
| 7468 | |
| 7469 | /* |
| 7470 | * Call this when trying to stream a body during a request |
| 7471 | */ |
| 7472 | __LJMP static int hlua_httpclient_snd_yield(lua_State *L, int status, lua_KContext ctx) |
| 7473 | { |
| 7474 | struct hlua *hlua; |
| 7475 | struct hlua_httpclient *hlua_hc = hlua_checkhttpclient(L, 1); |
| 7476 | const char *body_str = NULL; |
| 7477 | int ret; |
| 7478 | int end = 0; |
| 7479 | size_t buf_len; |
| 7480 | size_t to_send = 0; |
| 7481 | |
| 7482 | hlua = hlua_gethlua(L); |
| 7483 | |
| 7484 | if (!hlua || !hlua->task) |
| 7485 | WILL_LJMP(luaL_error(L, "The 'get' function is only allowed in " |
| 7486 | "'frontend', 'backend' or 'task'")); |
| 7487 | |
| 7488 | ret = lua_getfield(L, -1, "body"); |
| 7489 | if (ret != LUA_TSTRING) |
| 7490 | goto rcv; |
| 7491 | |
| 7492 | body_str = lua_tolstring(L, -1, &buf_len); |
| 7493 | lua_pop(L, 1); |
| 7494 | |
Christopher Faulet | fc59129 | 2022-01-12 14:46:03 +0100 | [diff] [blame] | 7495 | to_send = buf_len - hlua_hc->sent; |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7496 | |
| 7497 | if ((hlua_hc->sent + to_send) >= buf_len) |
| 7498 | end = 1; |
| 7499 | |
| 7500 | /* the end flag is always set since we are using the whole remaining size */ |
| 7501 | hlua_hc->sent += httpclient_req_xfer(hlua_hc->hc, ist2(body_str + hlua_hc->sent, to_send), end); |
| 7502 | |
| 7503 | if (buf_len > hlua_hc->sent) { |
| 7504 | /* still need to process the buffer */ |
| 7505 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_httpclient_snd_yield, TICK_ETERNITY, 0)); |
| 7506 | } else { |
| 7507 | goto rcv; |
| 7508 | /* we sent the whole request buffer we can recv */ |
| 7509 | } |
| 7510 | return 0; |
| 7511 | |
| 7512 | rcv: |
| 7513 | |
| 7514 | /* we return a "res" object */ |
| 7515 | lua_newtable(L); |
| 7516 | |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7517 | lua_pushstring(L, "body"); |
William Lallemand | d1187eb | 2021-11-02 10:40:06 +0100 | [diff] [blame] | 7518 | luaL_buffinit(L, &hlua_hc->b); |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7519 | |
William Lallemand | 933fe39 | 2021-11-04 09:45:58 +0100 | [diff] [blame] | 7520 | task_wakeup(hlua->task, TASK_WOKEN_MSG); |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7521 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_httpclient_rcv_yield, TICK_ETERNITY, 0)); |
| 7522 | |
| 7523 | return 1; |
| 7524 | } |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7525 | |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7526 | /* |
William Lallemand | dc2cc90 | 2021-10-26 11:43:26 +0200 | [diff] [blame] | 7527 | * Send an HTTP request and wait for a response |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7528 | */ |
| 7529 | |
William Lallemand | dc2cc90 | 2021-10-26 11:43:26 +0200 | [diff] [blame] | 7530 | __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] | 7531 | { |
| 7532 | struct hlua_httpclient *hlua_hc; |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7533 | struct http_hdr *hdrs = NULL; |
| 7534 | struct http_hdr *hdrs_i = NULL; |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7535 | struct hlua *hlua; |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7536 | const char *url_str = NULL; |
William Lallemand | dec25c3 | 2021-10-25 19:48:37 +0200 | [diff] [blame] | 7537 | const char *body_str = NULL; |
Christopher Faulet | 380ae9c | 2022-10-14 14:57:04 +0200 | [diff] [blame] | 7538 | size_t buf_len = 0; |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7539 | int ret; |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7540 | |
| 7541 | hlua = hlua_gethlua(L); |
| 7542 | |
| 7543 | if (!hlua || !hlua->task) |
| 7544 | WILL_LJMP(luaL_error(L, "The 'get' function is only allowed in " |
| 7545 | "'frontend', 'backend' or 'task'")); |
| 7546 | |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7547 | if (lua_gettop(L) != 2 || lua_type(L, -1) != LUA_TTABLE) |
| 7548 | WILL_LJMP(luaL_error(L, "'get' needs a table as argument")); |
| 7549 | |
William Lallemand | 4f4f2b7 | 2022-02-17 20:00:23 +0100 | [diff] [blame] | 7550 | hlua_hc = hlua_checkhttpclient(L, 1); |
| 7551 | |
William Lallemand | 7177a95 | 2022-03-03 15:33:12 +0100 | [diff] [blame] | 7552 | lua_pushnil(L); /* first key */ |
| 7553 | while (lua_next(L, 2)) { |
| 7554 | if (strcmp(lua_tostring(L, -2), "dst") == 0) { |
| 7555 | if (httpclient_set_dst(hlua_hc->hc, lua_tostring(L, -1)) < 0) |
| 7556 | WILL_LJMP(luaL_error(L, "Can't use the 'dst' argument")); |
William Lallemand | 4f4f2b7 | 2022-02-17 20:00:23 +0100 | [diff] [blame] | 7557 | |
William Lallemand | 7177a95 | 2022-03-03 15:33:12 +0100 | [diff] [blame] | 7558 | } else if (strcmp(lua_tostring(L, -2), "url") == 0) { |
| 7559 | if (lua_type(L, -1) != LUA_TSTRING) |
| 7560 | WILL_LJMP(luaL_error(L, "invalid parameter in 'url', must be a string")); |
| 7561 | url_str = lua_tostring(L, -1); |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7562 | |
William Lallemand | 7177a95 | 2022-03-03 15:33:12 +0100 | [diff] [blame] | 7563 | } else if (strcmp(lua_tostring(L, -2), "timeout") == 0) { |
| 7564 | if (lua_type(L, -1) != LUA_TNUMBER) |
| 7565 | WILL_LJMP(luaL_error(L, "invalid parameter in 'timeout', must be a number")); |
| 7566 | httpclient_set_timeout(hlua_hc->hc, lua_tointeger(L, -1)); |
William Lallemand | b4a4ef6 | 2022-02-23 14:18:16 +0100 | [diff] [blame] | 7567 | |
William Lallemand | 7177a95 | 2022-03-03 15:33:12 +0100 | [diff] [blame] | 7568 | } else if (strcmp(lua_tostring(L, -2), "headers") == 0) { |
| 7569 | if (lua_type(L, -1) != LUA_TTABLE) |
| 7570 | WILL_LJMP(luaL_error(L, "invalid parameter in 'headers', must be a table")); |
| 7571 | hdrs = hlua_httpclient_table_to_hdrs(L); |
William Lallemand | 79416cb | 2021-09-24 14:51:44 +0200 | [diff] [blame] | 7572 | |
William Lallemand | 7177a95 | 2022-03-03 15:33:12 +0100 | [diff] [blame] | 7573 | } else if (strcmp(lua_tostring(L, -2), "body") == 0) { |
| 7574 | if (lua_type(L, -1) != LUA_TSTRING) |
| 7575 | WILL_LJMP(luaL_error(L, "invalid parameter in 'body', must be a string")); |
| 7576 | body_str = lua_tolstring(L, -1, &buf_len); |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7577 | |
William Lallemand | 7177a95 | 2022-03-03 15:33:12 +0100 | [diff] [blame] | 7578 | } else { |
| 7579 | WILL_LJMP(luaL_error(L, "'%s' invalid parameter name", lua_tostring(L, -2))); |
| 7580 | } |
| 7581 | /* removes 'value'; keeps 'key' for next iteration */ |
| 7582 | lua_pop(L, 1); |
| 7583 | } |
William Lallemand | dec25c3 | 2021-10-25 19:48:37 +0200 | [diff] [blame] | 7584 | |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7585 | if (!url_str) { |
| 7586 | WILL_LJMP(luaL_error(L, "'get' need a 'url' argument")); |
| 7587 | return 0; |
| 7588 | } |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7589 | |
William Lallemand | 10a3736 | 2022-03-02 16:18:26 +0100 | [diff] [blame] | 7590 | hlua_hc->sent = 0; |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7591 | |
Aurelien DARRAGON | 0356407 | 2023-02-09 15:26:25 +0100 | [diff] [blame] | 7592 | istfree(&hlua_hc->hc->req.url); |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7593 | hlua_hc->hc->req.url = istdup(ist(url_str)); |
William Lallemand | dc2cc90 | 2021-10-26 11:43:26 +0200 | [diff] [blame] | 7594 | hlua_hc->hc->req.meth = meth; |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7595 | |
| 7596 | /* update the httpclient callbacks */ |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7597 | hlua_hc->hc->ops.res_stline = hlua_httpclient_cb; |
| 7598 | hlua_hc->hc->ops.res_headers = hlua_httpclient_cb; |
| 7599 | hlua_hc->hc->ops.res_payload = hlua_httpclient_cb; |
William Lallemand | 8f170c7 | 2022-03-15 10:52:07 +0100 | [diff] [blame] | 7600 | hlua_hc->hc->ops.res_end = hlua_httpclient_cb; |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7601 | |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7602 | /* a body is available, it will use the request callback */ |
Christopher Faulet | 380ae9c | 2022-10-14 14:57:04 +0200 | [diff] [blame] | 7603 | if (body_str && buf_len) { |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7604 | hlua_hc->hc->ops.req_payload = hlua_httpclient_cb; |
| 7605 | } |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7606 | |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7607 | 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] | 7608 | |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7609 | /* free the temporary headers array */ |
| 7610 | hdrs_i = hdrs; |
| 7611 | while (hdrs_i && isttest(hdrs_i->n)) { |
| 7612 | istfree(&hdrs_i->n); |
| 7613 | istfree(&hdrs_i->v); |
| 7614 | hdrs_i++; |
| 7615 | } |
| 7616 | ha_free(&hdrs); |
| 7617 | |
| 7618 | |
William Lallemand | 6137a9e | 2021-10-26 15:01:53 +0200 | [diff] [blame] | 7619 | if (ret != ERR_NONE) { |
| 7620 | WILL_LJMP(luaL_error(L, "Can't generate the HTTP request")); |
| 7621 | return 0; |
| 7622 | } |
| 7623 | |
William Lallemand | c2d3db4 | 2022-04-26 11:46:13 +0200 | [diff] [blame] | 7624 | if (!httpclient_start(hlua_hc->hc)) |
| 7625 | WILL_LJMP(luaL_error(L, "couldn't start the httpclient")); |
William Lallemand | 6137a9e | 2021-10-26 15:01:53 +0200 | [diff] [blame] | 7626 | |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7627 | 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] | 7628 | |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7629 | return 0; |
| 7630 | } |
| 7631 | |
| 7632 | /* |
William Lallemand | dc2cc90 | 2021-10-26 11:43:26 +0200 | [diff] [blame] | 7633 | * Sends an HTTP HEAD request and wait for a response |
| 7634 | * |
| 7635 | * httpclient:head(url, headers, payload) |
| 7636 | */ |
| 7637 | __LJMP static int hlua_httpclient_head(lua_State *L) |
| 7638 | { |
| 7639 | return hlua_httpclient_send(L, HTTP_METH_HEAD); |
| 7640 | } |
| 7641 | |
| 7642 | /* |
| 7643 | * Send an HTTP GET request and wait for a response |
| 7644 | * |
| 7645 | * httpclient:get(url, headers, payload) |
| 7646 | */ |
| 7647 | __LJMP static int hlua_httpclient_get(lua_State *L) |
| 7648 | { |
| 7649 | return hlua_httpclient_send(L, HTTP_METH_GET); |
| 7650 | |
| 7651 | } |
| 7652 | |
| 7653 | /* |
| 7654 | * Sends an HTTP PUT request and wait for a response |
| 7655 | * |
| 7656 | * httpclient:put(url, headers, payload) |
| 7657 | */ |
| 7658 | __LJMP static int hlua_httpclient_put(lua_State *L) |
| 7659 | { |
| 7660 | return hlua_httpclient_send(L, HTTP_METH_PUT); |
| 7661 | } |
| 7662 | |
| 7663 | /* |
| 7664 | * Send an HTTP POST request and wait for a response |
| 7665 | * |
| 7666 | * httpclient:post(url, headers, payload) |
| 7667 | */ |
| 7668 | __LJMP static int hlua_httpclient_post(lua_State *L) |
| 7669 | { |
| 7670 | return hlua_httpclient_send(L, HTTP_METH_POST); |
| 7671 | } |
| 7672 | |
| 7673 | |
| 7674 | /* |
| 7675 | * Sends an HTTP DELETE request and wait for a response |
| 7676 | * |
| 7677 | * httpclient:delete(url, headers, payload) |
| 7678 | */ |
| 7679 | __LJMP static int hlua_httpclient_delete(lua_State *L) |
| 7680 | { |
| 7681 | return hlua_httpclient_send(L, HTTP_METH_DELETE); |
| 7682 | } |
| 7683 | |
| 7684 | /* |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7685 | * |
| 7686 | * |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7687 | * Class TXN |
| 7688 | * |
| 7689 | * |
| 7690 | */ |
| 7691 | |
| 7692 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7693 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7694 | */ |
| 7695 | __LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud) |
| 7696 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7697 | return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref)); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7698 | } |
| 7699 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 7700 | __LJMP static int hlua_set_var(lua_State *L) |
| 7701 | { |
| 7702 | struct hlua_txn *htxn; |
| 7703 | const char *name; |
| 7704 | size_t len; |
| 7705 | struct sample smp; |
| 7706 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 7707 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 7708 | 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] | 7709 | |
| 7710 | /* It is useles to retrieve the stream, but this function |
| 7711 | * runs only in a stream context. |
| 7712 | */ |
| 7713 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7714 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 7715 | |
| 7716 | /* Converts the third argument in a sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 7717 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 7718 | hlua_lua2smp(L, 3, &smp); |
| 7719 | |
| 7720 | /* Store the sample in a variable. */ |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 7721 | 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] | 7722 | |
| 7723 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 7724 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 7725 | else |
| 7726 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 7727 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 7728 | return 1; |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 7729 | } |
| 7730 | |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 7731 | __LJMP static int hlua_unset_var(lua_State *L) |
| 7732 | { |
| 7733 | struct hlua_txn *htxn; |
| 7734 | const char *name; |
| 7735 | size_t len; |
| 7736 | struct sample smp; |
| 7737 | |
| 7738 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 7739 | |
| 7740 | /* It is useles to retrieve the stream, but this function |
| 7741 | * runs only in a stream context. |
| 7742 | */ |
| 7743 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7744 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 7745 | |
| 7746 | /* Unset the variable. */ |
| 7747 | 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] | 7748 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 7749 | return 1; |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 7750 | } |
| 7751 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 7752 | __LJMP static int hlua_get_var(lua_State *L) |
| 7753 | { |
| 7754 | struct hlua_txn *htxn; |
| 7755 | const char *name; |
| 7756 | size_t len; |
| 7757 | struct sample smp; |
| 7758 | |
| 7759 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 7760 | |
| 7761 | /* It is useles to retrieve the stream, but this function |
| 7762 | * runs only in a stream context. |
| 7763 | */ |
| 7764 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7765 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 7766 | |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 7767 | 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] | 7768 | if (!vars_get_by_name(name, len, &smp, NULL)) { |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 7769 | lua_pushnil(L); |
| 7770 | return 1; |
| 7771 | } |
| 7772 | |
| 7773 | return hlua_smp2lua(L, &smp); |
| 7774 | } |
| 7775 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 7776 | __LJMP static int hlua_set_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7777 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7778 | struct hlua *hlua; |
| 7779 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7780 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 7781 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7782 | /* It is useles to retrieve the stream, but this function |
| 7783 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7784 | */ |
| 7785 | MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 7786 | |
| 7787 | /* Get hlua struct, or NULL if we execute from main lua state */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7788 | hlua = hlua_gethlua(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 7789 | if (!hlua) |
| 7790 | return 0; |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7791 | |
| 7792 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 7793 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7794 | |
| 7795 | /* Get and store new value. */ |
| 7796 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 7797 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 7798 | |
| 7799 | return 0; |
| 7800 | } |
| 7801 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 7802 | __LJMP static int hlua_get_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7803 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7804 | struct hlua *hlua; |
| 7805 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7806 | MAY_LJMP(check_args(L, 1, "get_priv")); |
| 7807 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7808 | /* It is useles to retrieve the stream, but this function |
| 7809 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7810 | */ |
| 7811 | MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 7812 | |
| 7813 | /* Get hlua struct, or NULL if we execute from main lua state */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7814 | hlua = hlua_gethlua(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 7815 | if (!hlua) { |
| 7816 | lua_pushnil(L); |
| 7817 | return 1; |
| 7818 | } |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7819 | |
| 7820 | /* Push configuration index in the stack. */ |
| 7821 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 7822 | |
| 7823 | return 1; |
| 7824 | } |
| 7825 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7826 | /* Create stack entry containing a class TXN. This function |
| 7827 | * return 0 if the stack does not contains free slots, |
| 7828 | * otherwise it returns 1. |
| 7829 | */ |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 7830 | 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] | 7831 | { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 7832 | struct hlua_txn *htxn; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7833 | |
| 7834 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 7835 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7836 | return 0; |
| 7837 | |
| 7838 | /* NOTE: The allocation never fails. The failure |
| 7839 | * throw an error, and the function never returns. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7840 | * if the throw is not available, the process is aborted. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7841 | */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 7842 | /* Create the object: obj[0] = userdata. */ |
| 7843 | lua_newtable(L); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 7844 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 7845 | lua_rawseti(L, -2, 0); |
| 7846 | |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 7847 | htxn->s = s; |
| 7848 | htxn->p = p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 7849 | htxn->dir = dir; |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 7850 | htxn->flags = flags; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7851 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 7852 | /* Create the "f" field that contains a list of fetches. */ |
| 7853 | lua_pushstring(L, "f"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 7854 | if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 7855 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7856 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 7857 | |
| 7858 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 7859 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 7860 | 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] | 7861 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7862 | lua_rawset(L, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 7863 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7864 | /* Create the "c" field that contains a list of converters. */ |
| 7865 | lua_pushstring(L, "c"); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 7866 | if (!hlua_converters_new(L, htxn, 0)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 7867 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7868 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 7869 | |
| 7870 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 7871 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 7872 | if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7873 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7874 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7875 | |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 7876 | /* Create the "req" field that contains the request channel object. */ |
| 7877 | lua_pushstring(L, "req"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 7878 | if (!hlua_channel_new(L, &s->req)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 7879 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7880 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 7881 | |
| 7882 | /* Create the "res" field that contains the response channel object. */ |
| 7883 | lua_pushstring(L, "res"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 7884 | if (!hlua_channel_new(L, &s->res)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 7885 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7886 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 7887 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7888 | /* Creates the HTTP object is the current proxy allows http. */ |
| 7889 | lua_pushstring(L, "http"); |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 7890 | if (IS_HTX_STRM(s)) { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 7891 | if (!hlua_http_new(L, htxn)) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7892 | return 0; |
| 7893 | } |
| 7894 | else |
| 7895 | lua_pushnil(L); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7896 | lua_rawset(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7897 | |
Christopher Faulet | 78c3547 | 2020-02-26 17:14:08 +0100 | [diff] [blame] | 7898 | if ((htxn->flags & HLUA_TXN_CTX_MASK) == HLUA_TXN_FLT_CTX) { |
| 7899 | /* HTTPMessage object are created when a lua TXN is created from |
| 7900 | * a filter context only |
| 7901 | */ |
| 7902 | |
| 7903 | /* Creates the HTTP-Request object is the current proxy allows http. */ |
| 7904 | lua_pushstring(L, "http_req"); |
| 7905 | if (p->mode == PR_MODE_HTTP) { |
| 7906 | if (!hlua_http_msg_new(L, &s->txn->req)) |
| 7907 | return 0; |
| 7908 | } |
| 7909 | else |
| 7910 | lua_pushnil(L); |
| 7911 | lua_rawset(L, -3); |
| 7912 | |
| 7913 | /* Creates the HTTP-Response object is the current proxy allows http. */ |
| 7914 | lua_pushstring(L, "http_res"); |
| 7915 | if (p->mode == PR_MODE_HTTP) { |
| 7916 | if (!hlua_http_msg_new(L, &s->txn->rsp)) |
| 7917 | return 0; |
| 7918 | } |
| 7919 | else |
| 7920 | lua_pushnil(L); |
| 7921 | lua_rawset(L, -3); |
| 7922 | } |
| 7923 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7924 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 7925 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref); |
| 7926 | lua_setmetatable(L, -2); |
| 7927 | |
| 7928 | return 1; |
| 7929 | } |
| 7930 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 7931 | __LJMP static int hlua_txn_deflog(lua_State *L) |
| 7932 | { |
| 7933 | const char *msg; |
| 7934 | struct hlua_txn *htxn; |
| 7935 | |
| 7936 | MAY_LJMP(check_args(L, 2, "deflog")); |
| 7937 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7938 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7939 | |
| 7940 | hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg); |
| 7941 | return 0; |
| 7942 | } |
| 7943 | |
| 7944 | __LJMP static int hlua_txn_log(lua_State *L) |
| 7945 | { |
| 7946 | int level; |
| 7947 | const char *msg; |
| 7948 | struct hlua_txn *htxn; |
| 7949 | |
| 7950 | MAY_LJMP(check_args(L, 3, "log")); |
| 7951 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7952 | level = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 7953 | msg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 7954 | |
| 7955 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 7956 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 7957 | |
| 7958 | hlua_sendlog(htxn->s->be, level, msg); |
| 7959 | return 0; |
| 7960 | } |
| 7961 | |
| 7962 | __LJMP static int hlua_txn_log_debug(lua_State *L) |
| 7963 | { |
| 7964 | const char *msg; |
| 7965 | struct hlua_txn *htxn; |
| 7966 | |
| 7967 | MAY_LJMP(check_args(L, 2, "Debug")); |
| 7968 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7969 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7970 | hlua_sendlog(htxn->s->be, LOG_DEBUG, msg); |
| 7971 | return 0; |
| 7972 | } |
| 7973 | |
| 7974 | __LJMP static int hlua_txn_log_info(lua_State *L) |
| 7975 | { |
| 7976 | const char *msg; |
| 7977 | struct hlua_txn *htxn; |
| 7978 | |
| 7979 | MAY_LJMP(check_args(L, 2, "Info")); |
| 7980 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7981 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7982 | hlua_sendlog(htxn->s->be, LOG_INFO, msg); |
| 7983 | return 0; |
| 7984 | } |
| 7985 | |
| 7986 | __LJMP static int hlua_txn_log_warning(lua_State *L) |
| 7987 | { |
| 7988 | const char *msg; |
| 7989 | struct hlua_txn *htxn; |
| 7990 | |
| 7991 | MAY_LJMP(check_args(L, 2, "Warning")); |
| 7992 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7993 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7994 | hlua_sendlog(htxn->s->be, LOG_WARNING, msg); |
| 7995 | return 0; |
| 7996 | } |
| 7997 | |
| 7998 | __LJMP static int hlua_txn_log_alert(lua_State *L) |
| 7999 | { |
| 8000 | const char *msg; |
| 8001 | struct hlua_txn *htxn; |
| 8002 | |
| 8003 | MAY_LJMP(check_args(L, 2, "Alert")); |
| 8004 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 8005 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 8006 | hlua_sendlog(htxn->s->be, LOG_ALERT, msg); |
| 8007 | return 0; |
| 8008 | } |
| 8009 | |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 8010 | __LJMP static int hlua_txn_set_loglevel(lua_State *L) |
| 8011 | { |
| 8012 | struct hlua_txn *htxn; |
| 8013 | int ll; |
| 8014 | |
| 8015 | MAY_LJMP(check_args(L, 2, "set_loglevel")); |
| 8016 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 8017 | ll = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 8018 | |
| 8019 | if (ll < 0 || ll > 7) |
| 8020 | WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7")); |
| 8021 | |
| 8022 | htxn->s->logs.level = ll; |
| 8023 | return 0; |
| 8024 | } |
| 8025 | |
| 8026 | __LJMP static int hlua_txn_set_tos(lua_State *L) |
| 8027 | { |
| 8028 | struct hlua_txn *htxn; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 8029 | int tos; |
| 8030 | |
| 8031 | MAY_LJMP(check_args(L, 2, "set_tos")); |
| 8032 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 8033 | tos = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 8034 | |
Willy Tarreau | 1a18b54 | 2018-12-11 16:37:42 +0100 | [diff] [blame] | 8035 | conn_set_tos(objt_conn(htxn->s->sess->origin), tos); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 8036 | return 0; |
| 8037 | } |
| 8038 | |
| 8039 | __LJMP static int hlua_txn_set_mark(lua_State *L) |
| 8040 | { |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 8041 | struct hlua_txn *htxn; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 8042 | int mark; |
| 8043 | |
| 8044 | MAY_LJMP(check_args(L, 2, "set_mark")); |
| 8045 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 8046 | mark = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 8047 | |
Lukas Tribus | 579e3e3 | 2019-08-11 18:03:45 +0200 | [diff] [blame] | 8048 | conn_set_mark(objt_conn(htxn->s->sess->origin), mark); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 8049 | return 0; |
| 8050 | } |
| 8051 | |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 8052 | __LJMP static int hlua_txn_set_priority_class(lua_State *L) |
| 8053 | { |
| 8054 | struct hlua_txn *htxn; |
| 8055 | |
| 8056 | MAY_LJMP(check_args(L, 2, "set_priority_class")); |
| 8057 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 8058 | htxn->s->priority_class = queue_limit_class(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 8059 | return 0; |
| 8060 | } |
| 8061 | |
| 8062 | __LJMP static int hlua_txn_set_priority_offset(lua_State *L) |
| 8063 | { |
| 8064 | struct hlua_txn *htxn; |
| 8065 | |
| 8066 | MAY_LJMP(check_args(L, 2, "set_priority_offset")); |
| 8067 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 8068 | htxn->s->priority_offset = queue_limit_offset(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 8069 | return 0; |
| 8070 | } |
| 8071 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8072 | /* 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] | 8073 | * 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] | 8074 | * message and terminate the transaction. It returns 1 on success and 0 on |
| 8075 | * error. The Reply must be on top of the stack. |
| 8076 | */ |
| 8077 | __LJMP static int hlua_txn_forward_reply(lua_State *L, struct stream *s) |
| 8078 | { |
| 8079 | struct htx *htx; |
| 8080 | struct htx_sl *sl; |
| 8081 | struct h1m h1m; |
| 8082 | const char *status, *reason, *body; |
| 8083 | size_t status_len, reason_len, body_len; |
| 8084 | int ret, code, flags; |
| 8085 | |
| 8086 | code = 200; |
| 8087 | status = "200"; |
| 8088 | status_len = 3; |
| 8089 | ret = lua_getfield(L, -1, "status"); |
| 8090 | if (ret == LUA_TNUMBER) { |
| 8091 | code = lua_tointeger(L, -1); |
| 8092 | status = lua_tolstring(L, -1, &status_len); |
| 8093 | } |
| 8094 | lua_pop(L, 1); |
| 8095 | |
| 8096 | reason = http_get_reason(code); |
| 8097 | reason_len = strlen(reason); |
| 8098 | ret = lua_getfield(L, -1, "reason"); |
| 8099 | if (ret == LUA_TSTRING) |
| 8100 | reason = lua_tolstring(L, -1, &reason_len); |
| 8101 | lua_pop(L, 1); |
| 8102 | |
| 8103 | body = NULL; |
| 8104 | body_len = 0; |
| 8105 | ret = lua_getfield(L, -1, "body"); |
| 8106 | if (ret == LUA_TSTRING) |
| 8107 | body = lua_tolstring(L, -1, &body_len); |
| 8108 | lua_pop(L, 1); |
| 8109 | |
| 8110 | /* Prepare the response before inserting the headers */ |
| 8111 | h1m_init_res(&h1m); |
| 8112 | htx = htx_from_buf(&s->res.buf); |
| 8113 | channel_htx_truncate(&s->res, htx); |
| 8114 | if (s->txn->req.flags & HTTP_MSGF_VER_11) { |
| 8115 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11); |
| 8116 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), |
| 8117 | ist2(status, status_len), ist2(reason, reason_len)); |
| 8118 | } |
| 8119 | else { |
| 8120 | flags = HTX_SL_F_IS_RESP; |
| 8121 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), |
| 8122 | ist2(status, status_len), ist2(reason, reason_len)); |
| 8123 | } |
| 8124 | if (!sl) |
| 8125 | goto fail; |
| 8126 | sl->info.res.status = code; |
| 8127 | |
| 8128 | /* Push in the stack the "headers" entry. */ |
| 8129 | ret = lua_getfield(L, -1, "headers"); |
| 8130 | if (ret != LUA_TTABLE) |
| 8131 | goto skip_headers; |
| 8132 | |
| 8133 | lua_pushnil(L); |
| 8134 | while (lua_next(L, -2) != 0) { |
| 8135 | struct ist name, value; |
| 8136 | const char *n, *v; |
| 8137 | size_t nlen, vlen; |
| 8138 | |
| 8139 | if (!lua_isstring(L, -2) || !lua_istable(L, -1)) { |
| 8140 | /* Skip element if the key is not a string or if the value is not a table */ |
| 8141 | goto next_hdr; |
| 8142 | } |
| 8143 | |
| 8144 | n = lua_tolstring(L, -2, &nlen); |
| 8145 | name = ist2(n, nlen); |
| 8146 | if (isteqi(name, ist("content-length"))) { |
| 8147 | /* Always skip content-length header. It will be added |
| 8148 | * later with the correct len |
| 8149 | */ |
| 8150 | goto next_hdr; |
| 8151 | } |
| 8152 | |
| 8153 | /* Loop on header's values */ |
| 8154 | lua_pushnil(L); |
| 8155 | while (lua_next(L, -2)) { |
| 8156 | if (!lua_isstring(L, -1)) { |
| 8157 | /* Skip the value if it is not a string */ |
| 8158 | goto next_value; |
| 8159 | } |
| 8160 | |
| 8161 | v = lua_tolstring(L, -1, &vlen); |
| 8162 | value = ist2(v, vlen); |
| 8163 | |
| 8164 | if (isteqi(name, ist("transfer-encoding"))) |
| 8165 | h1_parse_xfer_enc_header(&h1m, value); |
| 8166 | if (!htx_add_header(htx, ist2(n, nlen), ist2(v, vlen))) |
| 8167 | goto fail; |
| 8168 | |
| 8169 | next_value: |
| 8170 | lua_pop(L, 1); |
| 8171 | } |
| 8172 | |
| 8173 | next_hdr: |
| 8174 | lua_pop(L, 1); |
| 8175 | } |
| 8176 | skip_headers: |
| 8177 | lua_pop(L, 1); |
| 8178 | |
| 8179 | /* Update h1m flags: CLEN is set if CHNK is not present */ |
| 8180 | if (!(h1m.flags & H1_MF_CHNK)) { |
| 8181 | const char *clen = ultoa(body_len); |
| 8182 | |
| 8183 | h1m.flags |= H1_MF_CLEN; |
| 8184 | if (!htx_add_header(htx, ist("content-length"), ist(clen))) |
| 8185 | goto fail; |
| 8186 | } |
| 8187 | if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK)) |
| 8188 | h1m.flags |= H1_MF_XFER_LEN; |
| 8189 | |
| 8190 | /* Update HTX start-line flags */ |
| 8191 | if (h1m.flags & H1_MF_XFER_ENC) |
| 8192 | flags |= HTX_SL_F_XFER_ENC; |
| 8193 | if (h1m.flags & H1_MF_XFER_LEN) { |
| 8194 | flags |= HTX_SL_F_XFER_LEN; |
| 8195 | if (h1m.flags & H1_MF_CHNK) |
| 8196 | flags |= HTX_SL_F_CHNK; |
| 8197 | else if (h1m.flags & H1_MF_CLEN) |
| 8198 | flags |= HTX_SL_F_CLEN; |
| 8199 | if (h1m.body_len == 0) |
| 8200 | flags |= HTX_SL_F_BODYLESS; |
| 8201 | } |
| 8202 | sl->flags |= flags; |
| 8203 | |
| 8204 | |
| 8205 | if (!htx_add_endof(htx, HTX_BLK_EOH) || |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 8206 | (body_len && !htx_add_data_atonce(htx, ist2(body, body_len)))) |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8207 | goto fail; |
| 8208 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 8209 | htx->flags |= HTX_FL_EOM; |
| 8210 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8211 | /* Now, forward the response and terminate the transaction */ |
| 8212 | s->txn->status = code; |
| 8213 | htx_to_buf(htx, &s->res.buf); |
| 8214 | if (!http_forward_proxy_resp(s, 1)) |
| 8215 | goto fail; |
| 8216 | |
| 8217 | return 1; |
| 8218 | |
| 8219 | fail: |
| 8220 | channel_htx_truncate(&s->res, htx); |
| 8221 | return 0; |
| 8222 | } |
| 8223 | |
| 8224 | /* Terminate a transaction if called from a lua action. For TCP streams, |
| 8225 | * processing is just aborted. Nothing is returned to the client and all |
| 8226 | * arguments are ignored. For HTTP streams, if a reply is passed as argument, it |
| 8227 | * is forwarded to the client before terminating the transaction. On success, |
| 8228 | * the function exits with ACT_RET_DONE code. If an error occurred, it exits |
| 8229 | * with ACT_RET_ERR code. If this function is not called from a lua action, it |
| 8230 | * just exits without any processing. |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 8231 | */ |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 8232 | __LJMP static int hlua_txn_done(lua_State *L) |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 8233 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 8234 | struct hlua_txn *htxn; |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8235 | struct stream *s; |
| 8236 | int finst; |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 8237 | |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 8238 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 8239 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8240 | /* If the flags NOTERM is set, we cannot terminate the session, so we |
| 8241 | * just end the execution of the current lua code. */ |
| 8242 | if (htxn->flags & HLUA_TXN_NOTERM) |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 8243 | WILL_LJMP(hlua_done(L)); |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 8244 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8245 | s = htxn->s; |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 8246 | if (!IS_HTX_STRM(htxn->s)) { |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8247 | struct channel *req = &s->req; |
| 8248 | struct channel *res = &s->res; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 8249 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8250 | channel_auto_read(req); |
| 8251 | channel_abort(req); |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8252 | channel_erase(req); |
| 8253 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8254 | channel_auto_read(res); |
| 8255 | channel_auto_close(res); |
Christopher Faulet | 12762f0 | 2023-04-13 15:40:10 +0200 | [diff] [blame] | 8256 | sc_schedule_abort(s->scb); |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8257 | |
| 8258 | finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_D); |
| 8259 | goto done; |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 8260 | } |
Thierry FOURNIER | 10ec214 | 2015-08-24 17:23:45 +0200 | [diff] [blame] | 8261 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8262 | if (lua_gettop(L) == 1 || !lua_istable(L, 2)) { |
| 8263 | /* No reply or invalid reply */ |
| 8264 | s->txn->status = 0; |
| 8265 | http_reply_and_close(s, 0, NULL); |
| 8266 | } |
| 8267 | else { |
| 8268 | /* Remove extra args to have the reply on top of the stack */ |
| 8269 | if (lua_gettop(L) > 2) |
| 8270 | lua_pop(L, lua_gettop(L) - 2); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 8271 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8272 | if (!hlua_txn_forward_reply(L, s)) { |
| 8273 | if (!(s->flags & SF_ERR_MASK)) |
| 8274 | s->flags |= SF_ERR_PRXCOND; |
| 8275 | lua_pushinteger(L, ACT_RET_ERR); |
| 8276 | WILL_LJMP(hlua_done(L)); |
| 8277 | return 0; /* Never reached */ |
| 8278 | } |
Christopher Faulet | 4d0e263 | 2019-07-16 10:52:40 +0200 | [diff] [blame] | 8279 | } |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 8280 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8281 | finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_H); |
| 8282 | if (htxn->dir == SMP_OPT_DIR_REQ) { |
| 8283 | /* let's log the request time */ |
| 8284 | s->logs.tv_request = now; |
| 8285 | 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] | 8286 | _HA_ATOMIC_INC(&s->sess->fe->fe_counters.intercepted_req); |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8287 | } |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 8288 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8289 | done: |
| 8290 | if (!(s->flags & SF_ERR_MASK)) |
| 8291 | s->flags |= SF_ERR_LOCAL; |
| 8292 | if (!(s->flags & SF_FINST_MASK)) |
| 8293 | s->flags |= finst; |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 8294 | |
Christopher Faulet | e48d1dc | 2021-08-13 14:11:17 +0200 | [diff] [blame] | 8295 | if ((htxn->flags & HLUA_TXN_CTX_MASK) == HLUA_TXN_FLT_CTX) |
| 8296 | lua_pushinteger(L, -1); |
| 8297 | else |
| 8298 | lua_pushinteger(L, ACT_RET_ABRT); |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 8299 | WILL_LJMP(hlua_done(L)); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 8300 | return 0; |
| 8301 | } |
| 8302 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8303 | /* |
| 8304 | * |
| 8305 | * |
| 8306 | * Class REPLY |
| 8307 | * |
| 8308 | * |
| 8309 | */ |
| 8310 | |
| 8311 | /* Pushes the TXN reply onto the top of the stack. If the stask does not have a |
| 8312 | * free slots, the function fails and returns 0; |
| 8313 | */ |
| 8314 | static int hlua_txn_reply_new(lua_State *L) |
| 8315 | { |
| 8316 | struct hlua_txn *htxn; |
| 8317 | const char *reason, *body = NULL; |
| 8318 | int ret, status; |
| 8319 | |
| 8320 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 8321 | if (!IS_HTX_STRM(htxn->s)) { |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8322 | hlua_pusherror(L, "txn object is not an HTTP transaction."); |
| 8323 | WILL_LJMP(lua_error(L)); |
| 8324 | } |
| 8325 | |
| 8326 | /* Default value */ |
| 8327 | status = 200; |
| 8328 | reason = http_get_reason(status); |
| 8329 | |
| 8330 | if (lua_istable(L, 2)) { |
| 8331 | /* load status and reason from the table argument at index 2 */ |
| 8332 | ret = lua_getfield(L, 2, "status"); |
| 8333 | if (ret == LUA_TNIL) |
| 8334 | goto reason; |
| 8335 | else if (ret != LUA_TNUMBER) { |
| 8336 | /* invalid status: ignore the reason */ |
| 8337 | goto body; |
| 8338 | } |
| 8339 | status = lua_tointeger(L, -1); |
| 8340 | |
| 8341 | reason: |
| 8342 | lua_pop(L, 1); /* restore the stack: remove status */ |
| 8343 | ret = lua_getfield(L, 2, "reason"); |
| 8344 | if (ret == LUA_TSTRING) |
| 8345 | reason = lua_tostring(L, -1); |
| 8346 | |
| 8347 | body: |
| 8348 | lua_pop(L, 1); /* restore the stack: remove invalid status or reason */ |
| 8349 | ret = lua_getfield(L, 2, "body"); |
| 8350 | if (ret == LUA_TSTRING) |
| 8351 | body = lua_tostring(L, -1); |
| 8352 | lua_pop(L, 1); /* restore the stack: remove body */ |
| 8353 | } |
| 8354 | |
| 8355 | /* Create the Reply table */ |
| 8356 | lua_newtable(L); |
| 8357 | |
| 8358 | /* Add status element */ |
| 8359 | lua_pushstring(L, "status"); |
| 8360 | lua_pushinteger(L, status); |
| 8361 | lua_settable(L, -3); |
| 8362 | |
| 8363 | /* Add reason element */ |
| 8364 | reason = http_get_reason(status); |
| 8365 | lua_pushstring(L, "reason"); |
| 8366 | lua_pushstring(L, reason); |
| 8367 | lua_settable(L, -3); |
| 8368 | |
| 8369 | /* Add body element, nil if undefined */ |
| 8370 | lua_pushstring(L, "body"); |
| 8371 | if (body) |
| 8372 | lua_pushstring(L, body); |
| 8373 | else |
| 8374 | lua_pushnil(L); |
| 8375 | lua_settable(L, -3); |
| 8376 | |
| 8377 | /* Add headers element */ |
| 8378 | lua_pushstring(L, "headers"); |
| 8379 | lua_newtable(L); |
| 8380 | |
| 8381 | /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */ |
| 8382 | if (lua_istable(L, 2)) { |
| 8383 | /* load headers from the table argument at index 2. If it is a table, copy it. */ |
| 8384 | ret = lua_getfield(L, 2, "headers"); |
| 8385 | if (ret == LUA_TTABLE) { |
| 8386 | /* stack: [ ... <headers:table>, <table> ] */ |
| 8387 | lua_pushnil(L); |
| 8388 | while (lua_next(L, -2) != 0) { |
| 8389 | /* stack: [ ... <headers:table>, <table>, k, v] */ |
| 8390 | if (!lua_isstring(L, -1) && !lua_istable(L, -1)) { |
| 8391 | /* invalid value type, skip it */ |
| 8392 | lua_pop(L, 1); |
| 8393 | continue; |
| 8394 | } |
| 8395 | |
| 8396 | |
| 8397 | /* Duplicate the key and swap it with the value. */ |
| 8398 | lua_pushvalue(L, -2); |
| 8399 | lua_insert(L, -2); |
| 8400 | /* stack: [ ... <headers:table>, <table>, k, k, v ] */ |
| 8401 | |
| 8402 | lua_newtable(L); |
| 8403 | lua_insert(L, -2); |
| 8404 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, v ] */ |
| 8405 | |
| 8406 | if (lua_isstring(L, -1)) { |
| 8407 | /* push the value in the inner table */ |
| 8408 | lua_rawseti(L, -2, 1); |
| 8409 | } |
| 8410 | else { /* table */ |
| 8411 | lua_pushnil(L); |
| 8412 | while (lua_next(L, -2) != 0) { |
| 8413 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2, v2 ] */ |
| 8414 | if (!lua_isstring(L, -1)) { |
| 8415 | /* invalid value type, skip it*/ |
| 8416 | lua_pop(L, 1); |
| 8417 | continue; |
| 8418 | } |
| 8419 | /* push the value in the inner table */ |
| 8420 | lua_rawseti(L, -4, lua_rawlen(L, -4) + 1); |
| 8421 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2 ] */ |
| 8422 | } |
| 8423 | lua_pop(L, 1); |
| 8424 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table> ] */ |
| 8425 | } |
| 8426 | |
| 8427 | /* push (k,v) on the stack in the headers table: |
| 8428 | * stack: [ ... <headers:table>, <table>, k, k, v ] |
| 8429 | */ |
| 8430 | lua_settable(L, -5); |
| 8431 | /* stack: [ ... <headers:table>, <table>, k ] */ |
| 8432 | } |
| 8433 | } |
| 8434 | lua_pop(L, 1); |
| 8435 | } |
| 8436 | /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */ |
| 8437 | lua_settable(L, -3); |
| 8438 | /* stack: [ txn, <Arg:table>, <Reply:table> ] */ |
| 8439 | |
| 8440 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 8441 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_reply_ref); |
| 8442 | lua_setmetatable(L, -2); |
| 8443 | return 1; |
| 8444 | } |
| 8445 | |
| 8446 | /* Set the reply status code, and optionally the reason. If no reason is |
| 8447 | * provided, the default one corresponding to the status code is used. |
| 8448 | */ |
| 8449 | __LJMP static int hlua_txn_reply_set_status(lua_State *L) |
| 8450 | { |
| 8451 | int status = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 8452 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
| 8453 | |
| 8454 | /* First argument (self) must be a table */ |
Aurelien DARRAGON | d83d045 | 2022-10-05 11:46:45 +0200 | [diff] [blame] | 8455 | MAY_LJMP(luaL_checktype(L, 1, LUA_TTABLE)); |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8456 | |
| 8457 | if (status < 100 || status > 599) { |
| 8458 | lua_pushboolean(L, 0); |
| 8459 | return 1; |
| 8460 | } |
| 8461 | if (!reason) |
| 8462 | reason = http_get_reason(status); |
| 8463 | |
| 8464 | lua_pushinteger(L, status); |
| 8465 | lua_setfield(L, 1, "status"); |
| 8466 | |
| 8467 | lua_pushstring(L, reason); |
| 8468 | lua_setfield(L, 1, "reason"); |
| 8469 | |
| 8470 | lua_pushboolean(L, 1); |
| 8471 | return 1; |
| 8472 | } |
| 8473 | |
| 8474 | /* Add a header into the reply object. Each header name is associated to an |
| 8475 | * array of values in the "headers" table. If the header name is not found, a |
| 8476 | * new entry is created. |
| 8477 | */ |
| 8478 | __LJMP static int hlua_txn_reply_add_header(lua_State *L) |
| 8479 | { |
| 8480 | const char *name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 8481 | const char *value = MAY_LJMP(luaL_checkstring(L, 3)); |
| 8482 | int ret; |
| 8483 | |
| 8484 | /* First argument (self) must be a table */ |
Aurelien DARRAGON | d83d045 | 2022-10-05 11:46:45 +0200 | [diff] [blame] | 8485 | MAY_LJMP(luaL_checktype(L, 1, LUA_TTABLE)); |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8486 | |
| 8487 | /* Push in the stack the "headers" entry. */ |
| 8488 | ret = lua_getfield(L, 1, "headers"); |
| 8489 | if (ret != LUA_TTABLE) { |
| 8490 | hlua_pusherror(L, "Reply['headers'] is expected to a an array. %s found", lua_typename(L, ret)); |
| 8491 | WILL_LJMP(lua_error(L)); |
| 8492 | } |
| 8493 | |
| 8494 | /* check if the header is already registered. If not, register it. */ |
| 8495 | ret = lua_getfield(L, -1, name); |
| 8496 | if (ret == LUA_TNIL) { |
| 8497 | /* Entry not found. */ |
| 8498 | lua_pop(L, 1); /* remove the nil. The "headers" table is the top of the stack. */ |
| 8499 | |
| 8500 | /* Insert the new header name in the array in the top of the stack. |
| 8501 | * It left the new array in the top of the stack. |
| 8502 | */ |
| 8503 | lua_newtable(L); |
| 8504 | lua_pushstring(L, name); |
| 8505 | lua_pushvalue(L, -2); |
| 8506 | lua_settable(L, -4); |
| 8507 | } |
| 8508 | else if (ret != LUA_TTABLE) { |
| 8509 | hlua_pusherror(L, "Reply['headers']['%s'] is expected to be an array. %s found", name, lua_typename(L, ret)); |
| 8510 | WILL_LJMP(lua_error(L)); |
| 8511 | } |
| 8512 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 8513 | /* Now the top of thestack is an array of values. We push |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8514 | * the header value as new entry. |
| 8515 | */ |
| 8516 | lua_pushstring(L, value); |
| 8517 | ret = lua_rawlen(L, -2); |
| 8518 | lua_rawseti(L, -2, ret + 1); |
| 8519 | |
| 8520 | lua_pushboolean(L, 1); |
| 8521 | return 1; |
| 8522 | } |
| 8523 | |
| 8524 | /* Remove all occurrences of a given header name. */ |
| 8525 | __LJMP static int hlua_txn_reply_del_header(lua_State *L) |
| 8526 | { |
| 8527 | const char *name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 8528 | int ret; |
| 8529 | |
| 8530 | /* First argument (self) must be a table */ |
Aurelien DARRAGON | d83d045 | 2022-10-05 11:46:45 +0200 | [diff] [blame] | 8531 | MAY_LJMP(luaL_checktype(L, 1, LUA_TTABLE)); |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8532 | |
| 8533 | /* Push in the stack the "headers" entry. */ |
| 8534 | ret = lua_getfield(L, 1, "headers"); |
| 8535 | if (ret != LUA_TTABLE) { |
| 8536 | hlua_pusherror(L, "Reply['headers'] is expected to be an array. %s found", lua_typename(L, ret)); |
| 8537 | WILL_LJMP(lua_error(L)); |
| 8538 | } |
| 8539 | |
| 8540 | lua_pushstring(L, name); |
| 8541 | lua_pushnil(L); |
| 8542 | lua_settable(L, -3); |
| 8543 | |
| 8544 | lua_pushboolean(L, 1); |
| 8545 | return 1; |
| 8546 | } |
| 8547 | |
| 8548 | /* Set the reply's body. Overwrite any existing entry. */ |
| 8549 | __LJMP static int hlua_txn_reply_set_body(lua_State *L) |
| 8550 | { |
| 8551 | const char *payload = MAY_LJMP(luaL_checkstring(L, 2)); |
| 8552 | |
| 8553 | /* First argument (self) must be a table */ |
Aurelien DARRAGON | d83d045 | 2022-10-05 11:46:45 +0200 | [diff] [blame] | 8554 | MAY_LJMP(luaL_checktype(L, 1, LUA_TTABLE)); |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8555 | |
| 8556 | lua_pushstring(L, payload); |
| 8557 | lua_setfield(L, 1, "body"); |
| 8558 | |
| 8559 | lua_pushboolean(L, 1); |
| 8560 | return 1; |
| 8561 | } |
| 8562 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 8563 | __LJMP static int hlua_log(lua_State *L) |
| 8564 | { |
| 8565 | int level; |
| 8566 | const char *msg; |
| 8567 | |
| 8568 | MAY_LJMP(check_args(L, 2, "log")); |
| 8569 | level = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 8570 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 8571 | |
| 8572 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 8573 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 8574 | |
| 8575 | hlua_sendlog(NULL, level, msg); |
| 8576 | return 0; |
| 8577 | } |
| 8578 | |
| 8579 | __LJMP static int hlua_log_debug(lua_State *L) |
| 8580 | { |
| 8581 | const char *msg; |
| 8582 | |
| 8583 | MAY_LJMP(check_args(L, 1, "debug")); |
| 8584 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 8585 | hlua_sendlog(NULL, LOG_DEBUG, msg); |
| 8586 | return 0; |
| 8587 | } |
| 8588 | |
| 8589 | __LJMP static int hlua_log_info(lua_State *L) |
| 8590 | { |
| 8591 | const char *msg; |
| 8592 | |
| 8593 | MAY_LJMP(check_args(L, 1, "info")); |
| 8594 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 8595 | hlua_sendlog(NULL, LOG_INFO, msg); |
| 8596 | return 0; |
| 8597 | } |
| 8598 | |
| 8599 | __LJMP static int hlua_log_warning(lua_State *L) |
| 8600 | { |
| 8601 | const char *msg; |
| 8602 | |
| 8603 | MAY_LJMP(check_args(L, 1, "warning")); |
| 8604 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 8605 | hlua_sendlog(NULL, LOG_WARNING, msg); |
| 8606 | return 0; |
| 8607 | } |
| 8608 | |
| 8609 | __LJMP static int hlua_log_alert(lua_State *L) |
| 8610 | { |
| 8611 | const char *msg; |
| 8612 | |
| 8613 | MAY_LJMP(check_args(L, 1, "alert")); |
| 8614 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 8615 | hlua_sendlog(NULL, LOG_ALERT, msg); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 8616 | return 0; |
| 8617 | } |
| 8618 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 8619 | __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] | 8620 | { |
| 8621 | int wakeup_ms = lua_tointeger(L, -1); |
Willy Tarreau | 12c0270 | 2021-09-30 16:12:31 +0200 | [diff] [blame] | 8622 | if (!tick_is_expired(wakeup_ms, now_ms)) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 8623 | 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] | 8624 | return 0; |
| 8625 | } |
| 8626 | |
| 8627 | __LJMP static int hlua_sleep(lua_State *L) |
| 8628 | { |
| 8629 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 8630 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8631 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 8632 | MAY_LJMP(check_args(L, 1, "sleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8633 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 8634 | delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 8635 | wakeup_ms = tick_add(now_ms, delay); |
| 8636 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8637 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 8638 | 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] | 8639 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8640 | } |
| 8641 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 8642 | __LJMP static int hlua_msleep(lua_State *L) |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8643 | { |
| 8644 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 8645 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8646 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 8647 | MAY_LJMP(check_args(L, 1, "msleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8648 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 8649 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 8650 | wakeup_ms = tick_add(now_ms, delay); |
| 8651 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8652 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 8653 | 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] | 8654 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8655 | } |
| 8656 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 8657 | /* This functionis an LUA binding. it permits to give back |
| 8658 | * the hand at the HAProxy scheduler. It is used when the |
| 8659 | * LUA processing consumes a lot of time. |
| 8660 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 8661 | __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] | 8662 | { |
| 8663 | return 0; |
| 8664 | } |
| 8665 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 8666 | __LJMP static int hlua_yield(lua_State *L) |
| 8667 | { |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 8668 | 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] | 8669 | return 0; |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 8670 | } |
| 8671 | |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 8672 | /* This function change the nice of the currently executed |
| 8673 | * task. It is used set low or high priority at the current |
| 8674 | * task. |
| 8675 | */ |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 8676 | __LJMP static int hlua_set_nice(lua_State *L) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 8677 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8678 | struct hlua *hlua; |
| 8679 | int nice; |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 8680 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8681 | MAY_LJMP(check_args(L, 1, "set_nice")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8682 | nice = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 8683 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 8684 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 8685 | hlua = hlua_gethlua(L); |
| 8686 | |
| 8687 | /* If the task is not set, I'm in a start mode. */ |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 8688 | if (!hlua || !hlua->task) |
| 8689 | return 0; |
| 8690 | |
| 8691 | if (nice < -1024) |
| 8692 | nice = -1024; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8693 | else if (nice > 1024) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 8694 | nice = 1024; |
| 8695 | |
| 8696 | hlua->task->nice = nice; |
| 8697 | return 0; |
| 8698 | } |
| 8699 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 8700 | /* 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] | 8701 | * HAProxy task subsystem when the task is awaked. The LUA runtime can |
| 8702 | * return an E_AGAIN signal, the emmiter of this signal must set a |
| 8703 | * signal to wake the task. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8704 | * |
| 8705 | * Task wrapper are longjmp safe because the only one Lua code |
| 8706 | * executed is the safe hlua_ctx_resume(); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8707 | */ |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 8708 | 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] | 8709 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 8710 | struct hlua *hlua = context; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8711 | enum hlua_exec status; |
| 8712 | |
Willy Tarreau | 6ef52f4 | 2022-06-15 14:19:48 +0200 | [diff] [blame] | 8713 | if (task->tid < 0) |
| 8714 | task->tid = tid; |
| 8715 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8716 | /* If it is the first call to the task, we must initialize the |
| 8717 | * execution timeouts. |
| 8718 | */ |
| 8719 | if (!HLUA_IS_RUNNING(hlua)) |
Aurelien DARRAGON | da9503c | 2022-11-25 09:10:07 +0100 | [diff] [blame] | 8720 | hlua_timer_init(&hlua->timer, hlua_timeout_task); |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8721 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8722 | /* Execute the Lua code. */ |
| 8723 | status = hlua_ctx_resume(hlua, 1); |
| 8724 | |
| 8725 | switch (status) { |
| 8726 | /* finished or yield */ |
| 8727 | case HLUA_E_OK: |
| 8728 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 8729 | task_destroy(task); |
Tim Duesterhus | cd235c6 | 2018-04-24 13:56:01 +0200 | [diff] [blame] | 8730 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8731 | break; |
| 8732 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 8733 | case HLUA_E_AGAIN: /* co process or timeout wake me later. */ |
Thierry FOURNIER | cb14688 | 2017-12-10 17:10:57 +0100 | [diff] [blame] | 8734 | notification_gc(&hlua->com); |
PiBa-NL | fe971b3 | 2018-05-02 22:27:14 +0200 | [diff] [blame] | 8735 | task->expire = hlua->wake_time; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8736 | break; |
| 8737 | |
| 8738 | /* finished with error. */ |
Aurelien DARRAGON | 7954410 | 2022-11-24 14:27:15 +0100 | [diff] [blame] | 8739 | case HLUA_E_ETMOUT: |
| 8740 | SEND_ERR(NULL, "Lua task: execution timeout.\n"); |
| 8741 | goto err_task_abort; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8742 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8743 | SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1)); |
Aurelien DARRAGON | 7954410 | 2022-11-24 14:27:15 +0100 | [diff] [blame] | 8744 | goto err_task_abort; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8745 | case HLUA_E_ERR: |
| 8746 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8747 | SEND_ERR(NULL, "Lua task: unknown error.\n"); |
Aurelien DARRAGON | 7954410 | 2022-11-24 14:27:15 +0100 | [diff] [blame] | 8748 | err_task_abort: |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8749 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 8750 | task_destroy(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 8751 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8752 | break; |
| 8753 | } |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 8754 | return task; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8755 | } |
| 8756 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8757 | /* This function is an LUA binding that register LUA function to be |
| 8758 | * executed after the HAProxy configuration parsing and before the |
| 8759 | * HAProxy scheduler starts. This function expect only one LUA |
| 8760 | * argument that is a function. This function returns nothing, but |
| 8761 | * throws if an error is encountered. |
| 8762 | */ |
| 8763 | __LJMP static int hlua_register_init(lua_State *L) |
| 8764 | { |
| 8765 | struct hlua_init_function *init; |
| 8766 | int ref; |
| 8767 | |
| 8768 | MAY_LJMP(check_args(L, 1, "register_init")); |
| 8769 | |
Aurelien DARRAGON | 87f5297 | 2023-02-24 09:43:54 +0100 | [diff] [blame] | 8770 | if (hlua_gethlua(L)) { |
| 8771 | /* runtime processing */ |
| 8772 | WILL_LJMP(luaL_error(L, "register_init: not available outside of body context")); |
| 8773 | } |
| 8774 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8775 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 8776 | |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 8777 | init = calloc(1, sizeof(*init)); |
Aurelien DARRAGON | 55afbed | 2023-03-02 18:42:06 +0100 | [diff] [blame] | 8778 | if (!init) { |
| 8779 | hlua_unref(L, ref); |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8780 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Aurelien DARRAGON | 55afbed | 2023-03-02 18:42:06 +0100 | [diff] [blame] | 8781 | } |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8782 | |
| 8783 | init->function_ref = ref; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 8784 | LIST_APPEND(&hlua_init_functions[hlua_state_id], &init->l); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8785 | return 0; |
| 8786 | } |
| 8787 | |
Ilya Shipitsin | 6f86eaa | 2022-11-30 16:22:42 +0500 | [diff] [blame] | 8788 | /* This function is an LUA binding. It permits to register a task |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8789 | * executed in parallel of the main HAroxy activity. The task is |
| 8790 | * created and it is set in the HAProxy scheduler. It can be called |
| 8791 | * from the "init" section, "post init" or during the runtime. |
| 8792 | * |
| 8793 | * Lua prototype: |
| 8794 | * |
Aurelien DARRAGON | b803899 | 2023-03-09 16:48:30 +0100 | [diff] [blame] | 8795 | * <none> core.register_task(<function>[, <arg1>[, <arg2>[, ...[, <arg4>]]]]) |
| 8796 | * |
| 8797 | * <arg1..4> are optional arguments that will be provided to <function> |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8798 | */ |
| 8799 | static int hlua_register_task(lua_State *L) |
| 8800 | { |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 8801 | struct hlua *hlua = NULL; |
| 8802 | struct task *task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8803 | int ref; |
Aurelien DARRAGON | b803899 | 2023-03-09 16:48:30 +0100 | [diff] [blame] | 8804 | int nb_arg; |
| 8805 | int it; |
| 8806 | int arg_ref[4]; /* optional arguments */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 8807 | int state_id; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8808 | |
Aurelien DARRAGON | b803899 | 2023-03-09 16:48:30 +0100 | [diff] [blame] | 8809 | nb_arg = lua_gettop(L); |
| 8810 | if (nb_arg < 1) |
| 8811 | WILL_LJMP(luaL_error(L, "register_task: <func> argument is required")); |
| 8812 | else if (nb_arg > 5) |
| 8813 | 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] | 8814 | |
Aurelien DARRAGON | b803899 | 2023-03-09 16:48:30 +0100 | [diff] [blame] | 8815 | /* first arg: function ref */ |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8816 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 8817 | |
Aurelien DARRAGON | b803899 | 2023-03-09 16:48:30 +0100 | [diff] [blame] | 8818 | /* extract optional args (if any) */ |
| 8819 | it = 0; |
| 8820 | while (--nb_arg) { |
| 8821 | lua_pushvalue(L, 2 + it); |
| 8822 | arg_ref[it] = hlua_ref(L); /* get arg reference */ |
| 8823 | it += 1; |
| 8824 | } |
| 8825 | nb_arg = it; |
| 8826 | |
Thierry Fournier | 75fc029 | 2020-11-28 13:18:56 +0100 | [diff] [blame] | 8827 | /* Get the reference state. If the reference is NULL, L is the master |
| 8828 | * state, otherwise hlua->T is. |
| 8829 | */ |
| 8830 | hlua = hlua_gethlua(L); |
| 8831 | if (hlua) |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 8832 | /* we are in runtime processing */ |
| 8833 | state_id = hlua->state_id; |
Thierry Fournier | 75fc029 | 2020-11-28 13:18:56 +0100 | [diff] [blame] | 8834 | else |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 8835 | /* we are in initialization mode */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8836 | state_id = hlua_state_id; |
Thierry Fournier | 75fc029 | 2020-11-28 13:18:56 +0100 | [diff] [blame] | 8837 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 8838 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8839 | if (!hlua) |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 8840 | goto alloc_error; |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 8841 | HLUA_INIT(hlua); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8842 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8843 | /* We are in the common lua state, execute the task anywhere, |
| 8844 | * otherwise, inherit the current thread identifier |
| 8845 | */ |
| 8846 | if (state_id == 0) |
Willy Tarreau | beeabf5 | 2021-10-01 18:23:30 +0200 | [diff] [blame] | 8847 | task = task_new_anywhere(); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8848 | else |
Willy Tarreau | beeabf5 | 2021-10-01 18:23:30 +0200 | [diff] [blame] | 8849 | task = task_new_here(); |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 8850 | if (!task) |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 8851 | goto alloc_error; |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 8852 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8853 | task->context = hlua; |
| 8854 | task->process = hlua_process_task; |
| 8855 | |
Aurelien DARRAGON | 6b0b9bd | 2023-03-21 14:02:16 +0100 | [diff] [blame] | 8856 | if (!hlua_ctx_init(hlua, state_id, task)) |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 8857 | goto alloc_error; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8858 | |
Aurelien DARRAGON | b803899 | 2023-03-09 16:48:30 +0100 | [diff] [blame] | 8859 | /* Ensure there is enough space on the stack for the function |
| 8860 | * plus optional arguments |
| 8861 | */ |
| 8862 | if (!lua_checkstack(hlua->T, (1 + nb_arg))) |
| 8863 | goto alloc_error; |
| 8864 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8865 | /* Restore the function in the stack. */ |
Aurelien DARRAGON | 4fdf8b5 | 2023-03-20 17:22:37 +0100 | [diff] [blame] | 8866 | hlua_pushref(hlua->T, ref); |
Aurelien DARRAGON | be58d66 | 2023-03-13 14:09:21 +0100 | [diff] [blame] | 8867 | /* function ref not needed anymore since it was pushed to the substack */ |
| 8868 | hlua_unref(L, ref); |
| 8869 | |
Aurelien DARRAGON | b803899 | 2023-03-09 16:48:30 +0100 | [diff] [blame] | 8870 | hlua->nargs = nb_arg; |
| 8871 | |
| 8872 | /* push optional arguments to the function */ |
| 8873 | for (it = 0; it < nb_arg; it++) { |
| 8874 | /* push arg to the stack */ |
| 8875 | hlua_pushref(hlua->T, arg_ref[it]); |
| 8876 | /* arg ref not needed anymore since it was pushed to the substack */ |
| 8877 | hlua_unref(L, arg_ref[it]); |
| 8878 | } |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8879 | |
| 8880 | /* Schedule task. */ |
Willy Tarreau | e3957f8 | 2021-09-30 16:17:37 +0200 | [diff] [blame] | 8881 | task_wakeup(task, TASK_WOKEN_INIT); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8882 | |
| 8883 | return 0; |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 8884 | |
| 8885 | alloc_error: |
| 8886 | task_destroy(task); |
Aurelien DARRAGON | 55afbed | 2023-03-02 18:42:06 +0100 | [diff] [blame] | 8887 | hlua_unref(L, ref); |
Aurelien DARRAGON | b803899 | 2023-03-09 16:48:30 +0100 | [diff] [blame] | 8888 | for (it = 0; it < nb_arg; it++) { |
| 8889 | hlua_unref(L, arg_ref[it]); |
| 8890 | } |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 8891 | hlua_ctx_destroy(hlua); |
| 8892 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 8893 | return 0; /* Never reached */ |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8894 | } |
| 8895 | |
Aurelien DARRAGON | c84899c | 2023-02-20 18:18:59 +0100 | [diff] [blame] | 8896 | /* called from unsafe location */ |
| 8897 | static void hlua_event_subscription_destroy(struct hlua_event_sub *hlua_sub) |
| 8898 | { |
| 8899 | /* hlua cleanup */ |
| 8900 | |
| 8901 | hlua_lock(hlua_sub->hlua); |
| 8902 | /* registry is shared between coroutines */ |
| 8903 | hlua_unref(hlua_sub->hlua->T, hlua_sub->fcn_ref); |
| 8904 | hlua_unlock(hlua_sub->hlua); |
| 8905 | |
| 8906 | hlua_ctx_destroy(hlua_sub->hlua); |
| 8907 | |
| 8908 | /* free */ |
| 8909 | pool_free(pool_head_hlua_event_sub, hlua_sub); |
| 8910 | } |
| 8911 | |
| 8912 | /* single event handler: hlua ctx is shared between multiple events handlers |
| 8913 | * issued from the same subscription. Thus, it is not destroyed when the event |
| 8914 | * is processed: it is destroyed when no more events are expected for the |
| 8915 | * subscription (ie: when the subscription ends). |
| 8916 | * |
| 8917 | * Moreover, events are processed sequentially within the subscription: |
| 8918 | * one event must be fully processed before another one may be processed. |
| 8919 | * This ensures proper consistency for lua event handling from an ordering |
| 8920 | * point of view. This is especially useful with server events for example |
| 8921 | * where ADD/DEL/UP/DOWN events ordering really matters to trigger specific |
| 8922 | * actions from lua (e.g.: sending emails or making API calls). |
| 8923 | * |
| 8924 | * Due to this design, each lua event handler is pleased to process the event |
| 8925 | * as fast as possible to prevent the event queue from growing up. |
| 8926 | * Strictly speaking, there is no runtime limit for the callback function |
| 8927 | * (timeout set to default task timeout), but if the event queue goes past |
| 8928 | * the limit of unconsumed events an error will be reported and the |
| 8929 | * susbscription will pause itself for as long as it takes for the handler to |
| 8930 | * catch up (events will be lost as a result). |
| 8931 | * If the event handler does not need the sequential ordering and wants to |
| 8932 | * process multiple events at a time, it may spawn a new side-task using |
| 8933 | * 'core.register_task' to delegate the event handling and make parallel event |
| 8934 | * processing within the same subscription set. |
| 8935 | */ |
| 8936 | static void hlua_event_handler(struct hlua *hlua) |
| 8937 | { |
| 8938 | enum hlua_exec status; |
| 8939 | |
| 8940 | /* If it is the first call to the task, we must initialize the |
| 8941 | * execution timeouts. |
| 8942 | */ |
Aurelien DARRAGON | da9503c | 2022-11-25 09:10:07 +0100 | [diff] [blame] | 8943 | if (!HLUA_IS_RUNNING(hlua)) |
| 8944 | hlua_timer_init(&hlua->timer, hlua_timeout_task); |
Aurelien DARRAGON | c84899c | 2023-02-20 18:18:59 +0100 | [diff] [blame] | 8945 | |
| 8946 | /* make sure to reset the task expiry before each hlua_ctx_resume() |
| 8947 | * since the task is re-used for multiple cb function calls |
| 8948 | * We couldn't risk to have t->expire pointing to a past date because |
| 8949 | * it was set during last function invocation but was never reset since |
| 8950 | * (ie: E_AGAIN) |
| 8951 | */ |
| 8952 | hlua->task->expire = TICK_ETERNITY; |
| 8953 | |
| 8954 | /* Execute the Lua code. */ |
| 8955 | status = hlua_ctx_resume(hlua, 1); |
| 8956 | |
| 8957 | switch (status) { |
| 8958 | /* finished or yield */ |
| 8959 | case HLUA_E_OK: |
| 8960 | break; |
| 8961 | |
| 8962 | case HLUA_E_AGAIN: /* co process or timeout wake me later. */ |
| 8963 | notification_gc(&hlua->com); |
| 8964 | hlua->task->expire = hlua->wake_time; |
| 8965 | break; |
| 8966 | |
| 8967 | /* finished with error. */ |
| 8968 | case HLUA_E_ETMOUT: |
| 8969 | SEND_ERR(NULL, "Lua event_hdl: execution timeout.\n"); |
| 8970 | break; |
| 8971 | |
| 8972 | case HLUA_E_ERRMSG: |
| 8973 | SEND_ERR(NULL, "Lua event_hdl: %s.\n", lua_tostring(hlua->T, -1)); |
| 8974 | break; |
| 8975 | |
| 8976 | case HLUA_E_ERR: |
| 8977 | default: |
| 8978 | SEND_ERR(NULL, "Lua event_hdl: unknown error.\n"); |
| 8979 | break; |
| 8980 | } |
| 8981 | } |
| 8982 | |
| 8983 | __LJMP static int hlua_event_hdl_cb_data_push_args(struct hlua_event_sub *hlua_sub, |
| 8984 | struct event_hdl_sub_type event, void *data) |
| 8985 | { |
| 8986 | struct hlua *hlua = hlua_sub->hlua; |
| 8987 | |
| 8988 | hlua->nargs = 1; |
| 8989 | lua_pushstring(hlua->T, event_hdl_sub_type_to_string(event)); |
| 8990 | if (event_hdl_sub_family_equal(EVENT_HDL_SUB_SERVER, event)) { |
| 8991 | struct event_hdl_cb_data_server *e_server = data; |
| 8992 | struct proxy *px; |
| 8993 | struct server *server; |
| 8994 | |
| 8995 | hlua->nargs += 1; |
| 8996 | lua_newtable(hlua->T); |
| 8997 | /* Add server name */ |
| 8998 | lua_pushstring(hlua->T, "name"); |
| 8999 | lua_pushstring(hlua->T, e_server->safe.name); |
| 9000 | lua_settable(hlua->T, -3); |
| 9001 | /* Add server puid */ |
| 9002 | lua_pushstring(hlua->T, "puid"); |
| 9003 | lua_pushinteger(hlua->T, e_server->safe.puid); |
| 9004 | lua_settable(hlua->T, -3); |
| 9005 | /* Add server rid */ |
| 9006 | lua_pushstring(hlua->T, "rid"); |
| 9007 | lua_pushinteger(hlua->T, e_server->safe.rid); |
| 9008 | lua_settable(hlua->T, -3); |
| 9009 | /* Add server proxy name */ |
| 9010 | lua_pushstring(hlua->T, "proxy_name"); |
| 9011 | lua_pushstring(hlua->T, e_server->safe.proxy_name); |
| 9012 | lua_settable(hlua->T, -3); |
| 9013 | |
| 9014 | /* attempt to provide reference server object |
| 9015 | * (if it wasn't removed yet, SERVER_DEL will never succeed here) |
| 9016 | */ |
| 9017 | px = proxy_find_by_name(e_server->safe.proxy_name, PR_CAP_BE, 0); |
| 9018 | BUG_ON(!px); |
| 9019 | server = findserver_unique_id(px, e_server->safe.puid, e_server->safe.rid); |
| 9020 | if (server) { |
| 9021 | lua_pushstring(hlua->T, "reference"); |
| 9022 | hlua_fcn_new_server(hlua->T, server); |
| 9023 | lua_settable(hlua->T, -3); |
| 9024 | } |
| 9025 | } |
| 9026 | /* sub mgmt */ |
| 9027 | hlua->nargs += 1; |
| 9028 | hlua_fcn_new_event_sub(hlua->T, hlua_sub->sub); |
| 9029 | |
| 9030 | return 1; |
| 9031 | } |
| 9032 | |
| 9033 | /* events runner: if there's an ongoing hlua event handling process, finish it |
| 9034 | * then, check if there are new events waiting to be processed |
| 9035 | * (events are processed sequentially) |
| 9036 | * |
| 9037 | * We have a safety measure to warn/guard if the event queue is growing up |
| 9038 | * too much due to many events being generated and lua handler is unable to |
| 9039 | * keep up the pace (e.g.: when the event queue grows past 100 unconsumed events). |
| 9040 | * TODO: make it tunable |
| 9041 | */ |
| 9042 | static struct task *hlua_event_runner(struct task *task, void *context, unsigned int state) |
| 9043 | { |
| 9044 | struct hlua_event_sub *hlua_sub = context; |
| 9045 | struct event_hdl_async_event *event; |
| 9046 | const char *error = NULL; |
| 9047 | |
| 9048 | if (!hlua_sub->paused && event_hdl_async_equeue_size(&hlua_sub->equeue) > 100) { |
| 9049 | const char *trace; |
| 9050 | |
| 9051 | /* We reached the limit of pending events in the queue: we should |
| 9052 | * warn the user, and temporarily pause the subscription to give a chance |
| 9053 | * to the handler to catch up? (it also prevents ressource shortage since |
| 9054 | * the queue could grow indefinitely otherwise) |
| 9055 | * TODO: find a way to inform the handler that it missed some events |
| 9056 | * (example: stats within the subscription in event_hdl api exposed via lua api?) |
| 9057 | * |
| 9058 | * Nonetheless, reaching this limit means that the handler is not fast enough |
| 9059 | * and/or that it subscribed to events that happen too frequently and did not |
| 9060 | * expect it. This could come from an inadequate design in the user's script. |
| 9061 | */ |
| 9062 | event_hdl_pause(hlua_sub->sub); |
| 9063 | hlua_sub->paused = 1; |
| 9064 | |
| 9065 | /* The following Lua calls can fail. */ |
| 9066 | if (!SET_SAFE_LJMP(hlua_sub->hlua)) |
| 9067 | trace = NULL; |
| 9068 | trace = hlua_traceback(hlua_sub->hlua->T, ", "); |
| 9069 | /* At this point the execution is safe. */ |
| 9070 | RESET_SAFE_LJMP(hlua_sub->hlua); |
| 9071 | |
| 9072 | 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 : "")); |
| 9073 | } |
| 9074 | |
| 9075 | if (HLUA_IS_RUNNING(hlua_sub->hlua)) { |
| 9076 | /* ongoing hlua event handler, resume it */ |
| 9077 | hlua_event_handler(hlua_sub->hlua); |
| 9078 | } else if ((event = event_hdl_async_equeue_pop(&hlua_sub->equeue))) { /* check for new events */ |
| 9079 | if (event_hdl_sub_type_equal(event->type, EVENT_HDL_SUB_END)) { |
| 9080 | /* ending event: no more events to come */ |
| 9081 | event_hdl_async_free_event(event); |
| 9082 | task_destroy(task); |
| 9083 | hlua_event_subscription_destroy(hlua_sub); |
| 9084 | return NULL; |
| 9085 | } |
| 9086 | /* new event: start processing it */ |
| 9087 | |
| 9088 | /* The following Lua calls can fail. */ |
| 9089 | if (!SET_SAFE_LJMP(hlua_sub->hlua)) { |
| 9090 | if (lua_type(hlua_sub->hlua->T, -1) == LUA_TSTRING) |
| 9091 | error = lua_tostring(hlua_sub->hlua->T, -1); |
| 9092 | else |
| 9093 | error = "critical error"; |
| 9094 | ha_alert("Lua event_hdl: %s.\n", error); |
| 9095 | goto skip_event; |
| 9096 | } |
| 9097 | |
| 9098 | /* Check stack available size. */ |
| 9099 | if (!lua_checkstack(hlua_sub->hlua->T, 5)) { |
| 9100 | ha_alert("Lua event_hdl: full stack.\n"); |
| 9101 | RESET_SAFE_LJMP(hlua_sub->hlua); |
| 9102 | goto skip_event; |
| 9103 | } |
| 9104 | |
| 9105 | /* Restore the function in the stack. */ |
| 9106 | hlua_pushref(hlua_sub->hlua->T, hlua_sub->fcn_ref); |
| 9107 | |
| 9108 | /* push args */ |
| 9109 | hlua_sub->hlua->nargs = 0; |
| 9110 | if (!hlua_event_hdl_cb_data_push_args(hlua_sub, event->type, event->data)) { |
| 9111 | RESET_SAFE_LJMP(hlua_sub->hlua); |
| 9112 | goto skip_event; |
| 9113 | } |
| 9114 | |
| 9115 | /* At this point the execution is safe. */ |
| 9116 | RESET_SAFE_LJMP(hlua_sub->hlua); |
| 9117 | |
| 9118 | /* At this point the event was successfully translated into hlua ctx, |
| 9119 | * or hlua error occured, so we can safely discard it |
| 9120 | */ |
| 9121 | event_hdl_async_free_event(event); |
| 9122 | event = NULL; |
| 9123 | |
| 9124 | hlua_event_handler(hlua_sub->hlua); |
| 9125 | skip_event: |
| 9126 | if (event) |
| 9127 | event_hdl_async_free_event(event); |
| 9128 | |
| 9129 | } |
| 9130 | |
| 9131 | if (!HLUA_IS_RUNNING(hlua_sub->hlua)) { |
| 9132 | /* we just finished the processing of one event.. |
| 9133 | * check for new events before becoming idle |
| 9134 | */ |
| 9135 | if (!event_hdl_async_equeue_isempty(&hlua_sub->equeue)) { |
| 9136 | /* more events to process, make sure the task |
| 9137 | * will be resumed ASAP to process pending events |
| 9138 | */ |
| 9139 | task_wakeup(task, TASK_WOKEN_OTHER); |
| 9140 | } |
| 9141 | else if (hlua_sub->paused) { |
| 9142 | /* empty queue, the handler catched up: resume the subscription */ |
| 9143 | event_hdl_resume(hlua_sub->sub); |
| 9144 | hlua_sub->paused = 0; |
| 9145 | } |
| 9146 | } |
| 9147 | |
| 9148 | return task; |
| 9149 | } |
| 9150 | |
| 9151 | /* Must be called directly under lua protected/safe environment |
| 9152 | * (not from external callback) |
| 9153 | * <fcn_ref> should NOT be dropped after the function successfully returns: |
| 9154 | * it will be done automatically in hlua_event_subscription_destroy() when the |
| 9155 | * subscription ends. |
| 9156 | * |
| 9157 | * Returns the new subscription on success and NULL on failure (memory error) |
| 9158 | */ |
| 9159 | static struct event_hdl_sub *hlua_event_subscribe(event_hdl_sub_list *list, struct event_hdl_sub_type e_type, |
| 9160 | int state_id, int fcn_ref) |
| 9161 | { |
| 9162 | struct hlua_event_sub *hlua_sub; |
| 9163 | struct task *task = NULL; |
| 9164 | |
| 9165 | hlua_sub = pool_alloc(pool_head_hlua_event_sub); |
| 9166 | if (!hlua_sub) |
| 9167 | goto mem_error; |
| 9168 | hlua_sub->task = NULL; |
| 9169 | hlua_sub->hlua = NULL; |
| 9170 | hlua_sub->paused = 0; |
| 9171 | if ((task = task_new_here()) == NULL) { |
| 9172 | ha_alert("out of memory while allocating hlua event task"); |
| 9173 | goto mem_error; |
| 9174 | } |
| 9175 | task->process = hlua_event_runner; |
| 9176 | task->context = hlua_sub; |
| 9177 | event_hdl_async_equeue_init(&hlua_sub->equeue); |
| 9178 | hlua_sub->task = task; |
| 9179 | hlua_sub->fcn_ref = fcn_ref; |
| 9180 | hlua_sub->state_id = state_id; |
| 9181 | hlua_sub->hlua = pool_alloc(pool_head_hlua); |
| 9182 | if (!hlua_sub->hlua) |
| 9183 | goto mem_error; |
| 9184 | HLUA_INIT(hlua_sub->hlua); |
| 9185 | if (!hlua_ctx_init(hlua_sub->hlua, hlua_sub->state_id, task)) |
| 9186 | goto mem_error; |
| 9187 | |
| 9188 | hlua_sub->sub = event_hdl_subscribe_ptr(list, e_type, |
| 9189 | EVENT_HDL_ASYNC_TASK(&hlua_sub->equeue, |
| 9190 | task, |
| 9191 | hlua_sub, |
| 9192 | NULL)); |
| 9193 | if (!hlua_sub->sub) |
| 9194 | goto mem_error; |
| 9195 | |
| 9196 | return hlua_sub->sub; /* returns pointer to event_hdl_sub struct */ |
| 9197 | |
| 9198 | mem_error: |
| 9199 | if (hlua_sub) { |
| 9200 | if (hlua_sub->task) |
| 9201 | task_destroy(hlua_sub->task); |
| 9202 | if (hlua_sub->hlua) |
| 9203 | hlua_ctx_destroy(hlua_sub->hlua); |
| 9204 | pool_free(pool_head_hlua_event_sub, hlua_sub); |
| 9205 | } |
| 9206 | |
| 9207 | return NULL; |
| 9208 | } |
| 9209 | |
| 9210 | /* looks for an array of strings referring to a composition of event_hdl subscription |
| 9211 | * types at <index> in <L> stack |
| 9212 | */ |
| 9213 | __LJMP static struct event_hdl_sub_type hlua_check_event_sub_types(lua_State *L, int index) |
| 9214 | { |
| 9215 | struct event_hdl_sub_type subscriptions; |
| 9216 | const char *msg; |
| 9217 | |
| 9218 | if (lua_type(L, index) != LUA_TTABLE) { |
| 9219 | msg = lua_pushfstring(L, "table of strings expected, got %s", luaL_typename(L, index)); |
| 9220 | luaL_argerror(L, index, msg); |
| 9221 | } |
| 9222 | |
| 9223 | subscriptions = EVENT_HDL_SUB_NONE; |
| 9224 | |
| 9225 | /* browse the argument as an array. */ |
| 9226 | lua_pushnil(L); |
| 9227 | while (lua_next(L, index) != 0) { |
| 9228 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 9229 | msg = lua_pushfstring(L, "table of strings expected, got %s", luaL_typename(L, index)); |
| 9230 | luaL_argerror(L, index, msg); |
| 9231 | } |
| 9232 | |
| 9233 | if (event_hdl_sub_type_equal(EVENT_HDL_SUB_NONE, event_hdl_string_to_sub_type(lua_tostring(L, -1)))) { |
| 9234 | msg = lua_pushfstring(L, "'%s' event type is unknown", lua_tostring(L, -1)); |
| 9235 | luaL_argerror(L, index, msg); |
| 9236 | } |
| 9237 | |
| 9238 | /* perform subscriptions |= current sub */ |
| 9239 | subscriptions = event_hdl_sub_type_add(subscriptions, event_hdl_string_to_sub_type(lua_tostring(L, -1))); |
| 9240 | |
| 9241 | /* pop the current value. */ |
| 9242 | lua_pop(L, 1); |
| 9243 | } |
| 9244 | |
| 9245 | return subscriptions; |
| 9246 | } |
| 9247 | |
| 9248 | /* Wrapper for hlua_fcn_new_event_sub(): catch errors raised by |
| 9249 | * the function to prevent LJMP |
| 9250 | * |
| 9251 | * If no error occured, the function returns 1, else it returns 0 and |
| 9252 | * the error message is pushed at the top of the stack |
| 9253 | */ |
| 9254 | __LJMP static int _hlua_new_event_sub_safe(lua_State *L) |
| 9255 | { |
| 9256 | struct event_hdl_sub *sub = lua_touserdata(L, 1); |
| 9257 | |
| 9258 | /* this function may raise errors */ |
| 9259 | return MAY_LJMP(hlua_fcn_new_event_sub(L, sub)); |
| 9260 | } |
| 9261 | static int hlua_new_event_sub_safe(lua_State *L, struct event_hdl_sub *sub) |
| 9262 | { |
| 9263 | if (!lua_checkstack(L, 2)) |
| 9264 | return 0; |
| 9265 | lua_pushcfunction(L, _hlua_new_event_sub_safe); |
| 9266 | lua_pushlightuserdata(L, sub); |
| 9267 | switch (lua_pcall(L, 1, 1, 0)) { |
| 9268 | case LUA_OK: |
| 9269 | return 1; |
| 9270 | default: |
| 9271 | /* error was catched */ |
| 9272 | return 0; |
| 9273 | } |
| 9274 | } |
| 9275 | |
| 9276 | /* This function is a LUA helper used for registering lua event callbacks. |
| 9277 | * It expects an event subscription array and the function to be executed |
| 9278 | * when subscribed events occur (stack arguments). |
| 9279 | * It can be called from the "init" section, "post init" or during the runtime. |
| 9280 | * |
| 9281 | * <sub_list> is the subscription list where the subscription will be attempted |
| 9282 | * |
| 9283 | * Pushes the newly allocated subscription on the stack on success |
| 9284 | */ |
| 9285 | __LJMP int hlua_event_sub(lua_State *L, event_hdl_sub_list *sub_list) |
| 9286 | { |
| 9287 | struct hlua *hlua; |
| 9288 | struct event_hdl_sub *sub; |
| 9289 | struct event_hdl_sub_type subscriptions; |
| 9290 | int fcn_ref; |
| 9291 | int state_id; |
| 9292 | |
| 9293 | MAY_LJMP(check_args(L, 2, "event_sub")); |
| 9294 | |
| 9295 | /* Get the reference state */ |
| 9296 | hlua = hlua_gethlua(L); |
| 9297 | if (hlua) |
| 9298 | /* we are in runtime processing, any thread may subscribe to events: |
| 9299 | * subscription events will be handled by the thread who performed |
| 9300 | * the registration. |
| 9301 | */ |
| 9302 | state_id = hlua->state_id; |
| 9303 | else { |
| 9304 | /* we are in initialization mode, only thread 0 (actual calling thread) |
| 9305 | * may subscribe to events to prevent the same handler (from different lua |
| 9306 | * stacks) from being registered multiple times |
| 9307 | * |
| 9308 | * hlua_state_id == 0: monostack (lua-load) |
| 9309 | * hlua_state_id > 0: hlua_state_id=tid+1, multi-stack (lua-load-per-thread) |
| 9310 | * (thus if hlua_state_id > 1, it means we are not in primary thread ctx) |
| 9311 | */ |
| 9312 | if (hlua_state_id > 1) |
| 9313 | return 0; /* skip registration */ |
| 9314 | state_id = hlua_state_id; |
| 9315 | } |
| 9316 | |
| 9317 | /* First argument : event subscriptions. */ |
| 9318 | subscriptions = MAY_LJMP(hlua_check_event_sub_types(L, 1)); |
| 9319 | |
| 9320 | if (event_hdl_sub_type_equal(subscriptions, EVENT_HDL_SUB_NONE)) { |
| 9321 | WILL_LJMP(luaL_error(L, "event_sub: no valid event types were provided")); |
| 9322 | return 0; /* Never reached */ |
| 9323 | } |
| 9324 | |
| 9325 | /* Second argument : lua function. */ |
| 9326 | fcn_ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 9327 | |
| 9328 | /* try to subscribe */ |
| 9329 | sub = hlua_event_subscribe(sub_list, subscriptions, state_id, fcn_ref); |
| 9330 | if (!sub) { |
| 9331 | hlua_unref(L, fcn_ref); |
| 9332 | WILL_LJMP(luaL_error(L, "event_sub: lua out of memory error")); |
| 9333 | return 0; /* Never reached */ |
| 9334 | } |
| 9335 | |
| 9336 | /* push the subscription to the stack |
| 9337 | * |
| 9338 | * Here we use the safe function so that lua errors will be |
| 9339 | * handled explicitly to prevent 'sub' from being lost |
| 9340 | */ |
| 9341 | if (!hlua_new_event_sub_safe(L, sub)) { |
| 9342 | /* Some events could already be pending in the handler's queue. |
| 9343 | * However it is wiser to cancel the subscription since we are unable to |
| 9344 | * provide a valid reference to it. |
| 9345 | * Pending events will be delivered (unless lua keeps raising errors). |
| 9346 | */ |
| 9347 | event_hdl_unsubscribe(sub); /* cancel the subscription */ |
| 9348 | WILL_LJMP(luaL_error(L, "event_sub: cannot push the subscription (%s)", lua_tostring(L, -1))); |
| 9349 | return 0; /* Never reached */ |
| 9350 | } |
| 9351 | event_hdl_drop(sub); /* sub has been duplicated, discard old ref */ |
| 9352 | |
| 9353 | return 1; |
| 9354 | } |
| 9355 | |
| 9356 | /* This function is a LUA wrapper used for registering global lua event callbacks |
| 9357 | * The new subscription is pushed onto the stack on success |
| 9358 | * Returns the number of arguments pushed to the stack (1 for success) |
| 9359 | */ |
| 9360 | __LJMP static int hlua_event_global_sub(lua_State *L) |
| 9361 | { |
| 9362 | /* NULL <sub_list> = global subscription list */ |
| 9363 | return MAY_LJMP(hlua_event_sub(L, NULL)); |
| 9364 | } |
| 9365 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 9366 | /* Wrapper called by HAProxy to execute an LUA converter. This wrapper |
| 9367 | * doesn't allow "yield" functions because the HAProxy engine cannot |
| 9368 | * resume converters. |
| 9369 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 9370 | 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] | 9371 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 9372 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 9373 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 9374 | const char *error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 9375 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 9376 | if (!stream) |
| 9377 | return 0; |
| 9378 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 9379 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 9380 | * Lua context can be not initialized. This behavior |
| 9381 | * permits to save performances because a systematic |
| 9382 | * Lua initialization cause 5% performances loss. |
| 9383 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9384 | if (!stream->hlua) { |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 9385 | struct hlua *hlua; |
| 9386 | |
| 9387 | hlua = pool_alloc(pool_head_hlua); |
| 9388 | if (!hlua) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9389 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 9390 | return 0; |
| 9391 | } |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 9392 | HLUA_INIT(hlua); |
| 9393 | stream->hlua = hlua; |
Aurelien DARRAGON | 6b0b9bd | 2023-03-21 14:02:16 +0100 | [diff] [blame] | 9394 | 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] | 9395 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 9396 | return 0; |
| 9397 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 9398 | } |
| 9399 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 9400 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9401 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 9402 | |
| 9403 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9404 | if (!SET_SAFE_LJMP(stream->hlua)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9405 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 9406 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 9407 | else |
| 9408 | error = "critical error"; |
| 9409 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 9410 | return 0; |
| 9411 | } |
| 9412 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 9413 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9414 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9415 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9416 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 9417 | return 0; |
| 9418 | } |
| 9419 | |
| 9420 | /* Restore the function in the stack. */ |
Aurelien DARRAGON | 4fdf8b5 | 2023-03-20 17:22:37 +0100 | [diff] [blame] | 9421 | hlua_pushref(stream->hlua->T, fcn->function_ref[stream->hlua->state_id]); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 9422 | |
| 9423 | /* convert input sample and pust-it in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9424 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9425 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9426 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 9427 | return 0; |
| 9428 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9429 | hlua_smp2lua(stream->hlua->T, smp); |
| 9430 | stream->hlua->nargs = 1; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 9431 | |
| 9432 | /* push keywords in the stack. */ |
| 9433 | if (arg_p) { |
| 9434 | for (; arg_p->type != ARGT_STOP; arg_p++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9435 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9436 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9437 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 9438 | return 0; |
| 9439 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9440 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 9441 | stream->hlua->nargs++; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 9442 | } |
| 9443 | } |
| 9444 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 9445 | /* We must initialize the execution timeouts. */ |
Aurelien DARRAGON | da9503c | 2022-11-25 09:10:07 +0100 | [diff] [blame] | 9446 | hlua_timer_init(&stream->hlua->timer, hlua_timeout_session); |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 9447 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 9448 | /* At this point the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9449 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 9450 | } |
| 9451 | |
| 9452 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9453 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 9454 | /* finished. */ |
| 9455 | case HLUA_E_OK: |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 9456 | /* If the stack is empty, the function fails. */ |
| 9457 | if (lua_gettop(stream->hlua->T) <= 0) |
| 9458 | return 0; |
| 9459 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 9460 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9461 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 9462 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 9463 | return 1; |
| 9464 | |
| 9465 | /* yield. */ |
| 9466 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9467 | 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] | 9468 | return 0; |
| 9469 | |
| 9470 | /* finished with error. */ |
| 9471 | case HLUA_E_ERRMSG: |
| 9472 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9473 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9474 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 9475 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 9476 | return 0; |
| 9477 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9478 | case HLUA_E_ETMOUT: |
| 9479 | SEND_ERR(stream->be, "Lua converter '%s': execution timeout.\n", fcn->name); |
| 9480 | return 0; |
| 9481 | |
| 9482 | case HLUA_E_NOMEM: |
| 9483 | SEND_ERR(stream->be, "Lua converter '%s': out of memory error.\n", fcn->name); |
| 9484 | return 0; |
| 9485 | |
| 9486 | case HLUA_E_YIELD: |
| 9487 | SEND_ERR(stream->be, "Lua converter '%s': yield functions like core.tcp() or core.sleep() are not allowed.\n", fcn->name); |
| 9488 | return 0; |
| 9489 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 9490 | case HLUA_E_ERR: |
| 9491 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9492 | 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] | 9493 | __fallthrough; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 9494 | |
| 9495 | default: |
| 9496 | return 0; |
| 9497 | } |
| 9498 | } |
| 9499 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9500 | /* Wrapper called by HAProxy to execute a sample-fetch. this wrapper |
| 9501 | * doesn't allow "yield" functions because the HAProxy engine cannot |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 9502 | * resume sample-fetches. This function will be called by the sample |
| 9503 | * fetch engine to call lua-based fetch operations. |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9504 | */ |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9505 | static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp, |
| 9506 | const char *kw, void *private) |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9507 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 9508 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 9509 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 9510 | const char *error; |
Christopher Faulet | 8c9e6bb | 2021-08-06 16:29:41 +0200 | [diff] [blame] | 9511 | unsigned int hflags = HLUA_TXN_NOTERM | HLUA_TXN_SMP_CTX; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9512 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 9513 | if (!stream) |
| 9514 | return 0; |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 9515 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 9516 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 9517 | * Lua context can be not initialized. This behavior |
| 9518 | * permits to save performances because a systematic |
| 9519 | * Lua initialization cause 5% performances loss. |
| 9520 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9521 | if (!stream->hlua) { |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 9522 | struct hlua *hlua; |
| 9523 | |
| 9524 | hlua = pool_alloc(pool_head_hlua); |
| 9525 | if (!hlua) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9526 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 9527 | return 0; |
| 9528 | } |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 9529 | hlua->T = NULL; |
| 9530 | stream->hlua = hlua; |
Aurelien DARRAGON | 6b0b9bd | 2023-03-21 14:02:16 +0100 | [diff] [blame] | 9531 | 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] | 9532 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 9533 | return 0; |
| 9534 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 9535 | } |
| 9536 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9537 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9538 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 9539 | |
| 9540 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9541 | if (!SET_SAFE_LJMP(stream->hlua)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9542 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 9543 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 9544 | else |
| 9545 | error = "critical error"; |
| 9546 | 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] | 9547 | return 0; |
| 9548 | } |
| 9549 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9550 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9551 | if (!lua_checkstack(stream->hlua->T, 2)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9552 | 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] | 9553 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9554 | return 0; |
| 9555 | } |
| 9556 | |
| 9557 | /* Restore the function in the stack. */ |
Aurelien DARRAGON | 4fdf8b5 | 2023-03-20 17:22:37 +0100 | [diff] [blame] | 9558 | hlua_pushref(stream->hlua->T, fcn->function_ref[stream->hlua->state_id]); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9559 | |
| 9560 | /* push arguments in the stack. */ |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 9561 | 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] | 9562 | 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] | 9563 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9564 | return 0; |
| 9565 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9566 | stream->hlua->nargs = 1; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9567 | |
| 9568 | /* push keywords in the stack. */ |
| 9569 | for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) { |
| 9570 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9571 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9572 | 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] | 9573 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9574 | return 0; |
| 9575 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9576 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 9577 | stream->hlua->nargs++; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9578 | } |
| 9579 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 9580 | /* We must initialize the execution timeouts. */ |
Aurelien DARRAGON | da9503c | 2022-11-25 09:10:07 +0100 | [diff] [blame] | 9581 | hlua_timer_init(&stream->hlua->timer, hlua_timeout_session); |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 9582 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 9583 | /* At this point the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9584 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9585 | } |
| 9586 | |
| 9587 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9588 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9589 | /* finished. */ |
| 9590 | case HLUA_E_OK: |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 9591 | /* If the stack is empty, the function fails. */ |
| 9592 | if (lua_gettop(stream->hlua->T) <= 0) |
| 9593 | return 0; |
| 9594 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9595 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9596 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 9597 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9598 | |
| 9599 | /* Set the end of execution flag. */ |
| 9600 | smp->flags &= ~SMP_F_MAY_CHANGE; |
| 9601 | return 1; |
| 9602 | |
| 9603 | /* yield. */ |
| 9604 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9605 | 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] | 9606 | return 0; |
| 9607 | |
| 9608 | /* finished with error. */ |
| 9609 | case HLUA_E_ERRMSG: |
| 9610 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9611 | SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9612 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 9613 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9614 | return 0; |
| 9615 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9616 | case HLUA_E_ETMOUT: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9617 | SEND_ERR(smp->px, "Lua sample-fetch '%s': execution timeout.\n", fcn->name); |
| 9618 | return 0; |
| 9619 | |
| 9620 | case HLUA_E_NOMEM: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9621 | SEND_ERR(smp->px, "Lua sample-fetch '%s': out of memory error.\n", fcn->name); |
| 9622 | return 0; |
| 9623 | |
| 9624 | case HLUA_E_YIELD: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9625 | SEND_ERR(smp->px, "Lua sample-fetch '%s': yield not allowed.\n", fcn->name); |
| 9626 | return 0; |
| 9627 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9628 | case HLUA_E_ERR: |
| 9629 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9630 | 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] | 9631 | __fallthrough; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9632 | |
| 9633 | default: |
| 9634 | return 0; |
| 9635 | } |
| 9636 | } |
| 9637 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 9638 | /* This function is an LUA binding used for registering |
| 9639 | * "sample-conv" functions. It expects a converter name used |
| 9640 | * in the haproxy configuration file, and an LUA function. |
| 9641 | */ |
| 9642 | __LJMP static int hlua_register_converters(lua_State *L) |
| 9643 | { |
| 9644 | struct sample_conv_kw_list *sck; |
| 9645 | const char *name; |
| 9646 | int ref; |
| 9647 | int len; |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 9648 | struct hlua_function *fcn = NULL; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 9649 | struct sample_conv *sc; |
| 9650 | struct buffer *trash; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 9651 | |
| 9652 | MAY_LJMP(check_args(L, 2, "register_converters")); |
| 9653 | |
Aurelien DARRAGON | 87f5297 | 2023-02-24 09:43:54 +0100 | [diff] [blame] | 9654 | if (hlua_gethlua(L)) { |
| 9655 | /* runtime processing */ |
| 9656 | WILL_LJMP(luaL_error(L, "register_converters: not available outside of body context")); |
| 9657 | } |
| 9658 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 9659 | /* First argument : converter name. */ |
| 9660 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 9661 | |
| 9662 | /* Second argument : lua function. */ |
| 9663 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 9664 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 9665 | /* Check if the converter is already registered */ |
| 9666 | trash = get_trash_chunk(); |
| 9667 | chunk_printf(trash, "lua.%s", name); |
| 9668 | sc = find_sample_conv(trash->area, trash->data); |
| 9669 | if (sc != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 9670 | fcn = sc->private; |
| 9671 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 9672 | ha_warning("Trying to register converter 'lua.%s' more than once. " |
| 9673 | "This will become a hard error in version 2.5.\n", name); |
Aurelien DARRAGON | 55afbed | 2023-03-02 18:42:06 +0100 | [diff] [blame] | 9674 | hlua_unref(L, fcn->function_ref[hlua_state_id]); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 9675 | } |
| 9676 | fcn->function_ref[hlua_state_id] = ref; |
| 9677 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 9678 | } |
| 9679 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 9680 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 9681 | sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 9682 | if (!sck) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 9683 | goto alloc_error; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 9684 | fcn = new_hlua_function(); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 9685 | if (!fcn) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 9686 | goto alloc_error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 9687 | |
| 9688 | /* Fill fcn. */ |
| 9689 | fcn->name = strdup(name); |
| 9690 | if (!fcn->name) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 9691 | goto alloc_error; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9692 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 9693 | |
| 9694 | /* List head */ |
| 9695 | sck->list.n = sck->list.p = NULL; |
| 9696 | |
| 9697 | /* converter keyword. */ |
| 9698 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 9699 | sck->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 9700 | if (!sck->kw[0].kw) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 9701 | goto alloc_error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 9702 | |
| 9703 | snprintf((char *)sck->kw[0].kw, len, "lua.%s", name); |
| 9704 | sck->kw[0].process = hlua_sample_conv_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 9705 | 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] | 9706 | sck->kw[0].val_args = NULL; |
| 9707 | sck->kw[0].in_type = SMP_T_STR; |
| 9708 | sck->kw[0].out_type = SMP_T_STR; |
| 9709 | sck->kw[0].private = fcn; |
| 9710 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 9711 | /* Register this new converter */ |
| 9712 | sample_register_convs(sck); |
| 9713 | |
| 9714 | return 0; |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 9715 | |
| 9716 | alloc_error: |
| 9717 | release_hlua_function(fcn); |
Aurelien DARRAGON | 55afbed | 2023-03-02 18:42:06 +0100 | [diff] [blame] | 9718 | hlua_unref(L, ref); |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 9719 | ha_free(&sck); |
| 9720 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 9721 | return 0; /* Never reached */ |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 9722 | } |
| 9723 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 9724 | /* This function is an LUA binding used for registering |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9725 | * "sample-fetch" functions. It expects a converter name used |
| 9726 | * in the haproxy configuration file, and an LUA function. |
| 9727 | */ |
| 9728 | __LJMP static int hlua_register_fetches(lua_State *L) |
| 9729 | { |
| 9730 | const char *name; |
| 9731 | int ref; |
| 9732 | int len; |
| 9733 | struct sample_fetch_kw_list *sfk; |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 9734 | struct hlua_function *fcn = NULL; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 9735 | struct sample_fetch *sf; |
| 9736 | struct buffer *trash; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9737 | |
| 9738 | MAY_LJMP(check_args(L, 2, "register_fetches")); |
| 9739 | |
Aurelien DARRAGON | 87f5297 | 2023-02-24 09:43:54 +0100 | [diff] [blame] | 9740 | if (hlua_gethlua(L)) { |
| 9741 | /* runtime processing */ |
| 9742 | WILL_LJMP(luaL_error(L, "register_fetches: not available outside of body context")); |
| 9743 | } |
| 9744 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9745 | /* First argument : sample-fetch name. */ |
| 9746 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 9747 | |
| 9748 | /* Second argument : lua function. */ |
| 9749 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 9750 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 9751 | /* Check if the sample-fetch is already registered */ |
| 9752 | trash = get_trash_chunk(); |
| 9753 | chunk_printf(trash, "lua.%s", name); |
| 9754 | sf = find_sample_fetch(trash->area, trash->data); |
| 9755 | if (sf != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 9756 | fcn = sf->private; |
| 9757 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 9758 | ha_warning("Trying to register sample-fetch 'lua.%s' more than once. " |
| 9759 | "This will become a hard error in version 2.5.\n", name); |
Aurelien DARRAGON | 55afbed | 2023-03-02 18:42:06 +0100 | [diff] [blame] | 9760 | hlua_unref(L, fcn->function_ref[hlua_state_id]); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 9761 | } |
| 9762 | fcn->function_ref[hlua_state_id] = ref; |
| 9763 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 9764 | } |
| 9765 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9766 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 9767 | sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9768 | if (!sfk) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 9769 | goto alloc_error; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 9770 | fcn = new_hlua_function(); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9771 | if (!fcn) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 9772 | goto alloc_error; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9773 | |
| 9774 | /* Fill fcn. */ |
| 9775 | fcn->name = strdup(name); |
| 9776 | if (!fcn->name) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 9777 | goto alloc_error; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9778 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9779 | |
| 9780 | /* List head */ |
| 9781 | sfk->list.n = sfk->list.p = NULL; |
| 9782 | |
| 9783 | /* sample-fetch keyword. */ |
| 9784 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 9785 | sfk->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9786 | if (!sfk->kw[0].kw) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 9787 | goto alloc_error; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9788 | |
| 9789 | snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name); |
| 9790 | sfk->kw[0].process = hlua_sample_fetch_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 9791 | 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] | 9792 | sfk->kw[0].val_args = NULL; |
| 9793 | sfk->kw[0].out_type = SMP_T_STR; |
| 9794 | sfk->kw[0].use = SMP_USE_HTTP_ANY; |
| 9795 | sfk->kw[0].val = 0; |
| 9796 | sfk->kw[0].private = fcn; |
| 9797 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9798 | /* Register this new fetch. */ |
| 9799 | sample_register_fetches(sfk); |
| 9800 | |
| 9801 | return 0; |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 9802 | |
| 9803 | alloc_error: |
| 9804 | release_hlua_function(fcn); |
Aurelien DARRAGON | 55afbed | 2023-03-02 18:42:06 +0100 | [diff] [blame] | 9805 | hlua_unref(L, ref); |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 9806 | ha_free(&sfk); |
| 9807 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 9808 | return 0; /* Never reached */ |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9809 | } |
| 9810 | |
Christopher Faulet | 501465d | 2020-02-26 14:54:16 +0100 | [diff] [blame] | 9811 | /* This function is a lua binding to set the wake_time. |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 9812 | */ |
Christopher Faulet | 501465d | 2020-02-26 14:54:16 +0100 | [diff] [blame] | 9813 | __LJMP static int hlua_set_wake_time(lua_State *L) |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 9814 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 9815 | struct hlua *hlua; |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 9816 | unsigned int delay; |
| 9817 | unsigned int wakeup_ms; |
| 9818 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 9819 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 9820 | hlua = hlua_gethlua(L); |
| 9821 | if (!hlua) { |
| 9822 | return 0; |
| 9823 | } |
| 9824 | |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 9825 | MAY_LJMP(check_args(L, 1, "wake_time")); |
| 9826 | |
| 9827 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 9828 | wakeup_ms = tick_add(now_ms, delay); |
| 9829 | hlua->wake_time = wakeup_ms; |
| 9830 | return 0; |
| 9831 | } |
| 9832 | |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9833 | /* This function is a wrapper to execute each LUA function declared as an action |
| 9834 | * wrapper during the initialisation period. This function may return any |
| 9835 | * ACT_RET_* value. On error ACT_RET_CONT is returned and the action is |
| 9836 | * ignored. If the lua action yields, ACT_RET_YIELD is returned. On success, the |
| 9837 | * return value is the first element on the stack. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9838 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9839 | 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] | 9840 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9841 | { |
| 9842 | char **arg; |
Christopher Faulet | 8c9e6bb | 2021-08-06 16:29:41 +0200 | [diff] [blame] | 9843 | unsigned int hflags = HLUA_TXN_ACT_CTX; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9844 | int dir, act_ret = ACT_RET_CONT; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 9845 | const char *error; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9846 | |
| 9847 | switch (rule->from) { |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 9848 | case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break; |
| 9849 | case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break; |
| 9850 | case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break; |
| 9851 | case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9852 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9853 | SEND_ERR(px, "Lua: internal error while execute action.\n"); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9854 | goto end; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9855 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9856 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 9857 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 9858 | * Lua context can be not initialized. This behavior |
| 9859 | * permits to save performances because a systematic |
| 9860 | * Lua initialization cause 5% performances loss. |
| 9861 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9862 | if (!s->hlua) { |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 9863 | struct hlua *hlua; |
| 9864 | |
| 9865 | hlua = pool_alloc(pool_head_hlua); |
| 9866 | if (!hlua) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9867 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9868 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9869 | goto end; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9870 | } |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 9871 | HLUA_INIT(hlua); |
| 9872 | s->hlua = hlua; |
Aurelien DARRAGON | 6b0b9bd | 2023-03-21 14:02:16 +0100 | [diff] [blame] | 9873 | 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] | 9874 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9875 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9876 | goto end; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9877 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 9878 | } |
| 9879 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9880 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9881 | if (!HLUA_IS_RUNNING(s->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 9882 | |
| 9883 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9884 | if (!SET_SAFE_LJMP(s->hlua)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9885 | if (lua_type(s->hlua->T, -1) == LUA_TSTRING) |
| 9886 | error = lua_tostring(s->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 9887 | else |
| 9888 | error = "critical error"; |
| 9889 | SEND_ERR(px, "Lua function '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9890 | rule->arg.hlua_rule->fcn->name, error); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9891 | goto end; |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 9892 | } |
| 9893 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9894 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9895 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9896 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9897 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9898 | RESET_SAFE_LJMP(s->hlua); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9899 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9900 | } |
| 9901 | |
| 9902 | /* Restore the function in the stack. */ |
Aurelien DARRAGON | 4fdf8b5 | 2023-03-20 17:22:37 +0100 | [diff] [blame] | 9903 | 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] | 9904 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 9905 | /* Create and and push object stream in the stack. */ |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 9906 | if (!hlua_txn_new(s->hlua->T, s, px, dir, hflags)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9907 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9908 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9909 | RESET_SAFE_LJMP(s->hlua); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9910 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9911 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9912 | s->hlua->nargs = 1; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9913 | |
| 9914 | /* push keywords in the stack. */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9915 | for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9916 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9917 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9918 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9919 | RESET_SAFE_LJMP(s->hlua); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9920 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9921 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9922 | lua_pushstring(s->hlua->T, *arg); |
| 9923 | s->hlua->nargs++; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9924 | } |
| 9925 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 9926 | /* Now the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9927 | RESET_SAFE_LJMP(s->hlua); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 9928 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 9929 | /* We must initialize the execution timeouts. */ |
Aurelien DARRAGON | da9503c | 2022-11-25 09:10:07 +0100 | [diff] [blame] | 9930 | hlua_timer_init(&s->hlua->timer, hlua_timeout_session); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9931 | } |
| 9932 | |
| 9933 | /* Execute the function. */ |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 9934 | switch (hlua_ctx_resume(s->hlua, !(flags & ACT_OPT_FINAL))) { |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9935 | /* finished. */ |
| 9936 | case HLUA_E_OK: |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9937 | /* Catch the return value */ |
| 9938 | if (lua_gettop(s->hlua->T) > 0) |
| 9939 | act_ret = lua_tointeger(s->hlua->T, -1); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9940 | |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 9941 | /* Set timeout in the required channel. */ |
Christopher Faulet | 498c483 | 2020-07-28 11:59:58 +0200 | [diff] [blame] | 9942 | if (act_ret == ACT_RET_YIELD) { |
| 9943 | if (flags & ACT_OPT_FINAL) |
| 9944 | goto err_yield; |
| 9945 | |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 9946 | if (dir == SMP_OPT_DIR_REQ) |
| 9947 | s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp), |
| 9948 | s->hlua->wake_time); |
| 9949 | else |
| 9950 | s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp), |
| 9951 | s->hlua->wake_time); |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 9952 | } |
| 9953 | goto end; |
| 9954 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9955 | /* yield. */ |
| 9956 | case HLUA_E_AGAIN: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 9957 | /* Set timeout in the required channel. */ |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 9958 | if (dir == SMP_OPT_DIR_REQ) |
| 9959 | s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp), |
| 9960 | s->hlua->wake_time); |
| 9961 | else |
| 9962 | s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp), |
| 9963 | s->hlua->wake_time); |
| 9964 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9965 | /* Some actions can be wake up when a "write" event |
| 9966 | * is detected on a response channel. This is useful |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 9967 | * only for actions targeted on the requests. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9968 | */ |
Christopher Faulet | 51fa358 | 2019-07-26 14:54:52 +0200 | [diff] [blame] | 9969 | if (HLUA_IS_WAKERESWR(s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 9970 | s->res.flags |= CF_WAKE_WRITE; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9971 | if (HLUA_IS_WAKEREQWR(s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 9972 | s->req.flags |= CF_WAKE_WRITE; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9973 | act_ret = ACT_RET_YIELD; |
| 9974 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9975 | |
| 9976 | /* finished with error. */ |
| 9977 | case HLUA_E_ERRMSG: |
| 9978 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9979 | SEND_ERR(px, "Lua function '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9980 | rule->arg.hlua_rule->fcn->name, lua_tostring(s->hlua->T, -1)); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9981 | lua_pop(s->hlua->T, 1); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9982 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9983 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9984 | case HLUA_E_ETMOUT: |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9985 | 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] | 9986 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9987 | |
| 9988 | case HLUA_E_NOMEM: |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9989 | 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] | 9990 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9991 | |
| 9992 | case HLUA_E_YIELD: |
Christopher Faulet | 498c483 | 2020-07-28 11:59:58 +0200 | [diff] [blame] | 9993 | err_yield: |
| 9994 | act_ret = ACT_RET_CONT; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9995 | 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] | 9996 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9997 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9998 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9999 | case HLUA_E_ERR: |
| 10000 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 10001 | SEND_ERR(px, "Lua function '%s' return an unknown error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 10002 | rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 10003 | |
| 10004 | default: |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 10005 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 10006 | } |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 10007 | |
| 10008 | end: |
Christopher Faulet | 2361fd9 | 2020-07-30 10:40:58 +0200 | [diff] [blame] | 10009 | if (act_ret != ACT_RET_YIELD && s->hlua) |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 10010 | s->hlua->wake_time = TICK_ETERNITY; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 10011 | return act_ret; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 10012 | } |
| 10013 | |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 10014 | 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] | 10015 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 10016 | struct appctx *ctx = context; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10017 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10018 | appctx_wakeup(ctx); |
Willy Tarreau | d958741 | 2017-08-23 16:07:33 +0200 | [diff] [blame] | 10019 | t->expire = TICK_ETERNITY; |
Willy Tarreau | d1aa41f | 2017-07-21 16:41:56 +0200 | [diff] [blame] | 10020 | return t; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10021 | } |
| 10022 | |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 10023 | static int hlua_applet_tcp_init(struct appctx *ctx) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10024 | { |
Willy Tarreau | e23f33b | 2022-05-06 14:07:13 +0200 | [diff] [blame] | 10025 | 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] | 10026 | struct stconn *sc = appctx_sc(ctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 10027 | struct stream *strm = __sc_strm(sc); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 10028 | struct hlua *hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10029 | struct task *task; |
| 10030 | char **arg; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 10031 | const char *error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10032 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 10033 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 10034 | if (!hlua) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 10035 | SEND_ERR(strm->be, "Lua applet tcp '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 10036 | ctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 10037 | return -1; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 10038 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10039 | HLUA_INIT(hlua); |
Willy Tarreau | e23f33b | 2022-05-06 14:07:13 +0200 | [diff] [blame] | 10040 | tcp_ctx->hlua = hlua; |
| 10041 | tcp_ctx->flags = 0; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10042 | |
| 10043 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | beeabf5 | 2021-10-01 18:23:30 +0200 | [diff] [blame] | 10044 | task = task_new_here(); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10045 | if (!task) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 10046 | SEND_ERR(strm->be, "Lua applet tcp '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 10047 | ctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 10048 | return -1; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10049 | } |
| 10050 | task->nice = 0; |
| 10051 | task->context = ctx; |
| 10052 | task->process = hlua_applet_wakeup; |
Willy Tarreau | e23f33b | 2022-05-06 14:07:13 +0200 | [diff] [blame] | 10053 | tcp_ctx->task = task; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10054 | |
| 10055 | /* In the execution wrappers linked with a stream, the |
| 10056 | * Lua context can be not initialized. This behavior |
| 10057 | * permits to save performances because a systematic |
| 10058 | * Lua initialization cause 5% performances loss. |
| 10059 | */ |
Aurelien DARRAGON | 6b0b9bd | 2023-03-21 14:02:16 +0100 | [diff] [blame] | 10060 | 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] | 10061 | 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] | 10062 | ctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 10063 | return -1; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10064 | } |
| 10065 | |
| 10066 | /* Set timeout according with the applet configuration. */ |
Aurelien DARRAGON | da9503c | 2022-11-25 09:10:07 +0100 | [diff] [blame] | 10067 | hlua_timer_init(&hlua->timer, ctx->applet->timeout); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10068 | |
| 10069 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 10070 | if (!SET_SAFE_LJMP(hlua)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 10071 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 10072 | error = lua_tostring(hlua->T, -1); |
| 10073 | else |
| 10074 | error = "critical error"; |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 10075 | SEND_ERR(strm->be, "Lua applet tcp '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 10076 | ctx->rule->arg.hlua_rule->fcn->name, error); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 10077 | return -1; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10078 | } |
| 10079 | |
| 10080 | /* Check stack available size. */ |
| 10081 | if (!lua_checkstack(hlua->T, 1)) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 10082 | SEND_ERR(strm->be, "Lua applet tcp '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 10083 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 10084 | RESET_SAFE_LJMP(hlua); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 10085 | return -1; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10086 | } |
| 10087 | |
| 10088 | /* Restore the function in the stack. */ |
Aurelien DARRAGON | 4fdf8b5 | 2023-03-20 17:22:37 +0100 | [diff] [blame] | 10089 | 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] | 10090 | |
| 10091 | /* Create and and push object stream in the stack. */ |
| 10092 | if (!hlua_applet_tcp_new(hlua->T, ctx)) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 10093 | SEND_ERR(strm->be, "Lua applet tcp '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 10094 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 10095 | RESET_SAFE_LJMP(hlua); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 10096 | return -1; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10097 | } |
| 10098 | hlua->nargs = 1; |
| 10099 | |
| 10100 | /* push keywords in the stack. */ |
| 10101 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 10102 | if (!lua_checkstack(hlua->T, 1)) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 10103 | SEND_ERR(strm->be, "Lua applet tcp '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 10104 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 10105 | RESET_SAFE_LJMP(hlua); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 10106 | return -1; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10107 | } |
| 10108 | lua_pushstring(hlua->T, *arg); |
| 10109 | hlua->nargs++; |
| 10110 | } |
| 10111 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 10112 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10113 | |
| 10114 | /* Wakeup the applet ASAP. */ |
Willy Tarreau | 90e8b45 | 2022-05-25 18:21:43 +0200 | [diff] [blame] | 10115 | applet_need_more_data(ctx); |
Willy Tarreau | 4164eb9 | 2022-05-25 15:42:03 +0200 | [diff] [blame] | 10116 | applet_have_more_data(ctx); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10117 | |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 10118 | return 0; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10119 | } |
| 10120 | |
Willy Tarreau | 60409db | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 10121 | void hlua_applet_tcp_fct(struct appctx *ctx) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10122 | { |
Willy Tarreau | e23f33b | 2022-05-06 14:07:13 +0200 | [diff] [blame] | 10123 | struct hlua_tcp_ctx *tcp_ctx = ctx->svcctx; |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 10124 | struct stconn *sc = appctx_sc(ctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 10125 | struct stream *strm = __sc_strm(sc); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10126 | struct act_rule *rule = ctx->rule; |
| 10127 | struct proxy *px = strm->be; |
Willy Tarreau | e23f33b | 2022-05-06 14:07:13 +0200 | [diff] [blame] | 10128 | struct hlua *hlua = tcp_ctx->hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10129 | |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 10130 | if (unlikely(se_fl_test(ctx->sedesc, (SE_FL_EOS|SE_FL_ERROR|SE_FL_SHR|SE_FL_SHW)))) |
| 10131 | goto out; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10132 | |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 10133 | /* The applet execution is already done. */ |
| 10134 | if (tcp_ctx->flags & APPLET_DONE) |
| 10135 | goto out; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10136 | |
| 10137 | /* Execute the function. */ |
| 10138 | switch (hlua_ctx_resume(hlua, 1)) { |
| 10139 | /* finished. */ |
| 10140 | case HLUA_E_OK: |
Willy Tarreau | e23f33b | 2022-05-06 14:07:13 +0200 | [diff] [blame] | 10141 | tcp_ctx->flags |= APPLET_DONE; |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 10142 | se_fl_set(ctx->sedesc, SE_FL_EOI|SE_FL_EOS); |
| 10143 | break; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10144 | |
| 10145 | /* yield. */ |
| 10146 | case HLUA_E_AGAIN: |
Thierry Fournier | 0164f20 | 2016-02-20 17:47:43 +0100 | [diff] [blame] | 10147 | if (hlua->wake_time != TICK_ETERNITY) |
Willy Tarreau | e23f33b | 2022-05-06 14:07:13 +0200 | [diff] [blame] | 10148 | task_schedule(tcp_ctx->task, hlua->wake_time); |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 10149 | break; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10150 | |
| 10151 | /* finished with error. */ |
| 10152 | case HLUA_E_ERRMSG: |
| 10153 | /* Display log. */ |
| 10154 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 10155 | rule->arg.hlua_rule->fcn->name, lua_tostring(hlua->T, -1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10156 | lua_pop(hlua->T, 1); |
| 10157 | goto error; |
| 10158 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 10159 | case HLUA_E_ETMOUT: |
| 10160 | SEND_ERR(px, "Lua applet tcp '%s': execution timeout.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 10161 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 10162 | goto error; |
| 10163 | |
| 10164 | case HLUA_E_NOMEM: |
| 10165 | SEND_ERR(px, "Lua applet tcp '%s': out of memory error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 10166 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 10167 | goto error; |
| 10168 | |
| 10169 | case HLUA_E_YIELD: /* unexpected */ |
| 10170 | SEND_ERR(px, "Lua applet tcp '%s': yield not allowed.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 10171 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 10172 | goto error; |
| 10173 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10174 | case HLUA_E_ERR: |
| 10175 | /* Display log. */ |
| 10176 | SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 10177 | rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10178 | goto error; |
| 10179 | |
| 10180 | default: |
| 10181 | goto error; |
| 10182 | } |
| 10183 | |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 10184 | out: |
| 10185 | /* eat the whole request */ |
| 10186 | co_skip(sc_oc(sc), co_data(sc_oc(sc))); |
| 10187 | return; |
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 | error: |
| 10190 | se_fl_set(ctx->sedesc, SE_FL_ERROR); |
Willy Tarreau | e23f33b | 2022-05-06 14:07:13 +0200 | [diff] [blame] | 10191 | tcp_ctx->flags |= APPLET_DONE; |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 10192 | goto out; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10193 | } |
| 10194 | |
| 10195 | static void hlua_applet_tcp_release(struct appctx *ctx) |
| 10196 | { |
Willy Tarreau | e23f33b | 2022-05-06 14:07:13 +0200 | [diff] [blame] | 10197 | struct hlua_tcp_ctx *tcp_ctx = ctx->svcctx; |
| 10198 | |
| 10199 | task_destroy(tcp_ctx->task); |
| 10200 | tcp_ctx->task = NULL; |
| 10201 | hlua_ctx_destroy(tcp_ctx->hlua); |
| 10202 | tcp_ctx->hlua = NULL; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10203 | } |
| 10204 | |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 10205 | /* The function returns 0 if the initialisation is complete or -1 if |
| 10206 | * 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] | 10207 | */ |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 10208 | static int hlua_applet_http_init(struct appctx *ctx) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 10209 | { |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 10210 | 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] | 10211 | struct stconn *sc = appctx_sc(ctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 10212 | struct stream *strm = __sc_strm(sc); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 10213 | struct http_txn *txn; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 10214 | struct hlua *hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 10215 | char **arg; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 10216 | struct task *task; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 10217 | const char *error; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 10218 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 10219 | txn = strm->txn; |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 10220 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 10221 | if (!hlua) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 10222 | SEND_ERR(strm->be, "Lua applet http '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 10223 | ctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 10224 | return -1; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 10225 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 10226 | HLUA_INIT(hlua); |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 10227 | http_ctx->hlua = hlua; |
| 10228 | http_ctx->left_bytes = -1; |
| 10229 | http_ctx->flags = 0; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 10230 | |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 10231 | if (txn->req.flags & HTTP_MSGF_VER_11) |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 10232 | http_ctx->flags |= APPLET_HTTP11; |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 10233 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 10234 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | beeabf5 | 2021-10-01 18:23:30 +0200 | [diff] [blame] | 10235 | task = task_new_here(); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 10236 | if (!task) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 10237 | SEND_ERR(strm->be, "Lua applet http '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 10238 | ctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 10239 | return -1; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 10240 | } |
| 10241 | task->nice = 0; |
| 10242 | task->context = ctx; |
| 10243 | task->process = hlua_applet_wakeup; |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 10244 | http_ctx->task = task; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 10245 | |
| 10246 | /* In the execution wrappers linked with a stream, the |
| 10247 | * Lua context can be not initialized. This behavior |
| 10248 | * permits to save performances because a systematic |
| 10249 | * Lua initialization cause 5% performances loss. |
| 10250 | */ |
Aurelien DARRAGON | 6b0b9bd | 2023-03-21 14:02:16 +0100 | [diff] [blame] | 10251 | 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] | 10252 | 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] | 10253 | ctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 10254 | return -1; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 10255 | } |
| 10256 | |
| 10257 | /* Set timeout according with the applet configuration. */ |
Aurelien DARRAGON | da9503c | 2022-11-25 09:10:07 +0100 | [diff] [blame] | 10258 | hlua_timer_init(&hlua->timer, ctx->applet->timeout); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 10259 | |
| 10260 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 10261 | if (!SET_SAFE_LJMP(hlua)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 10262 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 10263 | error = lua_tostring(hlua->T, -1); |
| 10264 | else |
| 10265 | error = "critical error"; |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 10266 | SEND_ERR(strm->be, "Lua applet http '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 10267 | ctx->rule->arg.hlua_rule->fcn->name, error); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 10268 | return -1; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 10269 | } |
| 10270 | |
| 10271 | /* Check stack available size. */ |
| 10272 | if (!lua_checkstack(hlua->T, 1)) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 10273 | SEND_ERR(strm->be, "Lua applet http '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 10274 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 10275 | RESET_SAFE_LJMP(hlua); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 10276 | return -1; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 10277 | } |
| 10278 | |
| 10279 | /* Restore the function in the stack. */ |
Aurelien DARRAGON | 4fdf8b5 | 2023-03-20 17:22:37 +0100 | [diff] [blame] | 10280 | 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] | 10281 | |
| 10282 | /* Create and and push object stream in the stack. */ |
| 10283 | if (!hlua_applet_http_new(hlua->T, ctx)) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 10284 | SEND_ERR(strm->be, "Lua applet http '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 10285 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 10286 | RESET_SAFE_LJMP(hlua); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 10287 | return -1; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 10288 | } |
| 10289 | hlua->nargs = 1; |
| 10290 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 10291 | /* push keywords in the stack. */ |
| 10292 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 10293 | if (!lua_checkstack(hlua->T, 1)) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 10294 | SEND_ERR(strm->be, "Lua applet http '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 10295 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 10296 | RESET_SAFE_LJMP(hlua); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 10297 | return -1; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 10298 | } |
| 10299 | lua_pushstring(hlua->T, *arg); |
| 10300 | hlua->nargs++; |
| 10301 | } |
| 10302 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 10303 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 10304 | |
| 10305 | /* Wakeup the applet when data is ready for read. */ |
Willy Tarreau | 90e8b45 | 2022-05-25 18:21:43 +0200 | [diff] [blame] | 10306 | applet_need_more_data(ctx); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 10307 | |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 10308 | return 0; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 10309 | } |
| 10310 | |
Willy Tarreau | 60409db | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 10311 | void hlua_applet_http_fct(struct appctx *ctx) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 10312 | { |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 10313 | struct hlua_http_ctx *http_ctx = ctx->svcctx; |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 10314 | struct stconn *sc = appctx_sc(ctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 10315 | struct stream *strm = __sc_strm(sc); |
| 10316 | struct channel *req = sc_oc(sc); |
| 10317 | struct channel *res = sc_ic(sc); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 10318 | struct act_rule *rule = ctx->rule; |
| 10319 | struct proxy *px = strm->be; |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 10320 | struct hlua *hlua = http_ctx->hlua; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 10321 | struct htx *req_htx, *res_htx; |
| 10322 | |
| 10323 | res_htx = htx_from_buf(&res->buf); |
| 10324 | |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 10325 | if (unlikely(se_fl_test(ctx->sedesc, (SE_FL_EOS|SE_FL_ERROR|SE_FL_SHR|SE_FL_SHW)))) |
| 10326 | goto out; |
| 10327 | |
| 10328 | /* The applet execution is already done. */ |
| 10329 | if (http_ctx->flags & APPLET_DONE) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 10330 | goto out; |
| 10331 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 10332 | /* Check if the input buffer is available. */ |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 10333 | if (!b_size(&res->buf)) { |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 10334 | sc_need_room(sc); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 10335 | goto out; |
| 10336 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 10337 | |
| 10338 | /* Set the currently running flag. */ |
| 10339 | if (!HLUA_IS_RUNNING(hlua) && |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 10340 | !(http_ctx->flags & APPLET_DONE)) { |
Christopher Faulet | bd878d2 | 2021-04-28 10:50:21 +0200 | [diff] [blame] | 10341 | if (!co_data(req)) { |
Willy Tarreau | 90e8b45 | 2022-05-25 18:21:43 +0200 | [diff] [blame] | 10342 | applet_need_more_data(ctx); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 10343 | goto out; |
| 10344 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 10345 | } |
| 10346 | |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 10347 | /* Execute the function. */ |
| 10348 | switch (hlua_ctx_resume(hlua, 1)) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 10349 | /* finished. */ |
| 10350 | case HLUA_E_OK: |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 10351 | http_ctx->flags |= APPLET_DONE; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 10352 | break; |
| 10353 | |
| 10354 | /* yield. */ |
| 10355 | case HLUA_E_AGAIN: |
| 10356 | if (hlua->wake_time != TICK_ETERNITY) |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 10357 | task_schedule(http_ctx->task, hlua->wake_time); |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 10358 | goto out; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 10359 | |
| 10360 | /* finished with error. */ |
| 10361 | case HLUA_E_ERRMSG: |
| 10362 | /* Display log. */ |
| 10363 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 10364 | rule->arg.hlua_rule->fcn->name, lua_tostring(hlua->T, -1)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 10365 | lua_pop(hlua->T, 1); |
| 10366 | goto error; |
| 10367 | |
| 10368 | case HLUA_E_ETMOUT: |
| 10369 | SEND_ERR(px, "Lua applet http '%s': execution timeout.\n", |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 10370 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 10371 | goto error; |
| 10372 | |
| 10373 | case HLUA_E_NOMEM: |
| 10374 | SEND_ERR(px, "Lua applet http '%s': out of memory error.\n", |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 10375 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 10376 | goto error; |
| 10377 | |
| 10378 | case HLUA_E_YIELD: /* unexpected */ |
| 10379 | SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n", |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 10380 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 10381 | goto error; |
| 10382 | |
| 10383 | case HLUA_E_ERR: |
| 10384 | /* Display log. */ |
| 10385 | SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n", |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 10386 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 10387 | goto error; |
| 10388 | |
| 10389 | default: |
| 10390 | goto error; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 10391 | } |
| 10392 | |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 10393 | if (http_ctx->flags & APPLET_DONE) { |
| 10394 | if (http_ctx->flags & APPLET_RSP_SENT) |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 10395 | goto out; |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 10396 | |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 10397 | if (!(http_ctx->flags & APPLET_HDR_SENT)) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 10398 | goto error; |
| 10399 | |
Christopher Faulet | f89af9c | 2022-04-07 10:07:18 +0200 | [diff] [blame] | 10400 | /* no more data are expected. If the response buffer is empty |
| 10401 | * for a chunked message, be sure to add something (EOT block in |
| 10402 | * this case) to have something to send. It is important to be |
| 10403 | * sure the EOM flags will be handled by the endpoint. |
| 10404 | */ |
| 10405 | if (htx_is_empty(res_htx) && (strm->txn->rsp.flags & (HTTP_MSGF_XFER_LEN|HTTP_MSGF_CNT_LEN)) == HTTP_MSGF_XFER_LEN) { |
| 10406 | if (!htx_add_endof(res_htx, HTX_BLK_EOT)) { |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 10407 | sc_need_room(sc); |
Christopher Faulet | f89af9c | 2022-04-07 10:07:18 +0200 | [diff] [blame] | 10408 | goto out; |
| 10409 | } |
| 10410 | channel_add_input(res, 1); |
| 10411 | } |
| 10412 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 10413 | res_htx->flags |= HTX_FL_EOM; |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 10414 | se_fl_set(ctx->sedesc, SE_FL_EOI|SE_FL_EOS); |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 10415 | strm->txn->status = http_ctx->status; |
| 10416 | http_ctx->flags |= APPLET_RSP_SENT; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 10417 | } |
| 10418 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 10419 | out: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 10420 | htx_to_buf(res_htx, &res->buf); |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 10421 | /* eat the whole request */ |
| 10422 | if (co_data(req)) { |
| 10423 | req_htx = htx_from_buf(&req->buf); |
| 10424 | co_htx_skip(req, req_htx, co_data(req)); |
| 10425 | htx_to_buf(req_htx, &req->buf); |
| 10426 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 10427 | return; |
| 10428 | |
| 10429 | error: |
| 10430 | |
| 10431 | /* If we are in HTTP mode, and we are not send any |
| 10432 | * data, return a 500 server error in best effort: |
| 10433 | * if there is no room available in the buffer, |
| 10434 | * just close the connection. |
| 10435 | */ |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 10436 | if (!(http_ctx->flags & APPLET_HDR_SENT)) { |
Christopher Faulet | f734638 | 2019-07-17 22:02:08 +0200 | [diff] [blame] | 10437 | struct buffer *err = &http_err_chunks[HTTP_ERR_500]; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 10438 | |
| 10439 | channel_erase(res); |
| 10440 | res->buf.data = b_data(err); |
| 10441 | memcpy(res->buf.area, b_head(err), b_data(err)); |
| 10442 | res_htx = htx_from_buf(&res->buf); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 10443 | channel_add_input(res, res_htx->data); |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 10444 | se_fl_set(ctx->sedesc, SE_FL_EOI|SE_FL_EOS); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 10445 | } |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 10446 | else |
| 10447 | se_fl_set(ctx->sedesc, SE_FL_ERROR); |
| 10448 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 10449 | if (!(strm->flags & SF_ERR_MASK)) |
| 10450 | strm->flags |= SF_ERR_RESOURCE; |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 10451 | http_ctx->flags |= APPLET_DONE; |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 10452 | goto out; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 10453 | } |
| 10454 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 10455 | static void hlua_applet_http_release(struct appctx *ctx) |
| 10456 | { |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 10457 | struct hlua_http_ctx *http_ctx = ctx->svcctx; |
| 10458 | |
| 10459 | task_destroy(http_ctx->task); |
| 10460 | http_ctx->task = NULL; |
| 10461 | hlua_ctx_destroy(http_ctx->hlua); |
| 10462 | http_ctx->hlua = NULL; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 10463 | } |
| 10464 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 10465 | /* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 10466 | * success case, else return ACT_RET_PRS_ERR. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 10467 | * |
| 10468 | * This function can fail with an abort() due to an Lua critical error. |
| 10469 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 10470 | * tolerated. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 10471 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 10472 | static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px, |
| 10473 | struct act_rule *rule, char **err) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 10474 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 10475 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 10476 | int i; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 10477 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 10478 | /* Memory for the rule. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 10479 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 10480 | if (!rule->arg.hlua_rule) { |
| 10481 | memprintf(err, "out of memory error"); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 10482 | goto error; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 10483 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 10484 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 10485 | /* Memory for arguments. */ |
Tim Duesterhus | e52b6e5 | 2020-09-12 20:26:43 +0200 | [diff] [blame] | 10486 | rule->arg.hlua_rule->args = calloc(fcn->nargs + 1, |
| 10487 | sizeof(*rule->arg.hlua_rule->args)); |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 10488 | if (!rule->arg.hlua_rule->args) { |
| 10489 | memprintf(err, "out of memory error"); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 10490 | goto error; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 10491 | } |
| 10492 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 10493 | /* Reference the Lua function and store the reference. */ |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 10494 | rule->arg.hlua_rule->fcn = fcn; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 10495 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 10496 | /* Expect some arguments */ |
| 10497 | for (i = 0; i < fcn->nargs; i++) { |
Thierry FOURNIER | 1725c2e | 2019-01-06 19:38:49 +0100 | [diff] [blame] | 10498 | if (*args[*cur_arg] == '\0') { |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 10499 | memprintf(err, "expect %d arguments", fcn->nargs); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 10500 | goto error; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 10501 | } |
Thierry FOURNIER | 1725c2e | 2019-01-06 19:38:49 +0100 | [diff] [blame] | 10502 | rule->arg.hlua_rule->args[i] = strdup(args[*cur_arg]); |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 10503 | if (!rule->arg.hlua_rule->args[i]) { |
| 10504 | memprintf(err, "out of memory error"); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 10505 | goto error; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 10506 | } |
| 10507 | (*cur_arg)++; |
| 10508 | } |
| 10509 | rule->arg.hlua_rule->args[i] = NULL; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 10510 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 10511 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 10512 | rule->action_ptr = hlua_action; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 10513 | return ACT_RET_PRS_OK; |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 10514 | |
| 10515 | error: |
| 10516 | if (rule->arg.hlua_rule) { |
| 10517 | if (rule->arg.hlua_rule->args) { |
| 10518 | for (i = 0; i < fcn->nargs; i++) |
| 10519 | ha_free(&rule->arg.hlua_rule->args[i]); |
| 10520 | ha_free(&rule->arg.hlua_rule->args); |
| 10521 | } |
| 10522 | ha_free(&rule->arg.hlua_rule); |
| 10523 | } |
| 10524 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 10525 | } |
| 10526 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 10527 | static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px, |
| 10528 | struct act_rule *rule, char **err) |
| 10529 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 10530 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 10531 | |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 10532 | /* HTTP applets are forbidden in tcp-request rules. |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 10533 | * HTTP applet request requires everything initialized by |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 10534 | * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER). |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 10535 | * The applet will be immediately initialized, but its before |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 10536 | * the call of this analyzer. |
| 10537 | */ |
| 10538 | if (rule->from != ACT_F_HTTP_REQ) { |
| 10539 | memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets"); |
| 10540 | return ACT_RET_PRS_ERR; |
| 10541 | } |
| 10542 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 10543 | /* Memory for the rule. */ |
| 10544 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 10545 | if (!rule->arg.hlua_rule) { |
| 10546 | memprintf(err, "out of memory error"); |
| 10547 | return ACT_RET_PRS_ERR; |
| 10548 | } |
| 10549 | |
| 10550 | /* Reference the Lua function and store the reference. */ |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 10551 | rule->arg.hlua_rule->fcn = fcn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 10552 | |
| 10553 | /* TODO: later accept arguments. */ |
| 10554 | rule->arg.hlua_rule->args = NULL; |
| 10555 | |
| 10556 | /* Add applet pointer in the rule. */ |
| 10557 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 10558 | rule->applet.name = fcn->name; |
| 10559 | rule->applet.init = hlua_applet_http_init; |
| 10560 | rule->applet.fct = hlua_applet_http_fct; |
| 10561 | rule->applet.release = hlua_applet_http_release; |
| 10562 | rule->applet.timeout = hlua_timeout_applet; |
| 10563 | |
| 10564 | return ACT_RET_PRS_OK; |
| 10565 | } |
| 10566 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 10567 | /* This function is an LUA binding used for registering |
| 10568 | * "sample-conv" functions. It expects a converter name used |
| 10569 | * in the haproxy configuration file, and an LUA function. |
| 10570 | */ |
| 10571 | __LJMP static int hlua_register_action(lua_State *L) |
| 10572 | { |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 10573 | struct action_kw_list *akl = NULL; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 10574 | const char *name; |
| 10575 | int ref; |
| 10576 | int len; |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 10577 | struct hlua_function *fcn = NULL; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 10578 | int nargs; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 10579 | struct buffer *trash; |
| 10580 | struct action_kw *akw; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 10581 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 10582 | /* Initialise the number of expected arguments at 0. */ |
| 10583 | nargs = 0; |
| 10584 | |
| 10585 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 10586 | 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] | 10587 | |
Aurelien DARRAGON | 87f5297 | 2023-02-24 09:43:54 +0100 | [diff] [blame] | 10588 | if (hlua_gethlua(L)) { |
| 10589 | /* runtime processing */ |
| 10590 | WILL_LJMP(luaL_error(L, "register_action: not available outside of body context")); |
| 10591 | } |
| 10592 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 10593 | /* First argument : converter name. */ |
| 10594 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 10595 | |
| 10596 | /* Second argument : environment. */ |
| 10597 | if (lua_type(L, 2) != LUA_TTABLE) |
| 10598 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 10599 | |
| 10600 | /* Third argument : lua function. */ |
| 10601 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 10602 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 10603 | /* 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] | 10604 | if (lua_gettop(L) >= 4) |
| 10605 | nargs = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 10606 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 10607 | /* browse the second argument as an array. */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 10608 | lua_pushnil(L); |
| 10609 | while (lua_next(L, 2) != 0) { |
Aurelien DARRAGON | 55afbed | 2023-03-02 18:42:06 +0100 | [diff] [blame] | 10610 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 10611 | hlua_unref(L, ref); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 10612 | 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] | 10613 | } |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 10614 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 10615 | /* Check if action exists */ |
| 10616 | trash = get_trash_chunk(); |
| 10617 | chunk_printf(trash, "lua.%s", name); |
| 10618 | if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) { |
| 10619 | akw = tcp_req_cont_action(trash->area); |
| 10620 | } else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) { |
| 10621 | akw = tcp_res_cont_action(trash->area); |
| 10622 | } else if (strcmp(lua_tostring(L, -1), "http-req") == 0) { |
| 10623 | akw = action_http_req_custom(trash->area); |
| 10624 | } else if (strcmp(lua_tostring(L, -1), "http-res") == 0) { |
| 10625 | akw = action_http_res_custom(trash->area); |
| 10626 | } else { |
| 10627 | akw = NULL; |
| 10628 | } |
| 10629 | if (akw != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 10630 | fcn = akw->private; |
| 10631 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 10632 | ha_warning("Trying to register action 'lua.%s' more than once. " |
| 10633 | "This will become a hard error in version 2.5.\n", name); |
Aurelien DARRAGON | 55afbed | 2023-03-02 18:42:06 +0100 | [diff] [blame] | 10634 | hlua_unref(L, fcn->function_ref[hlua_state_id]); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 10635 | } |
| 10636 | fcn->function_ref[hlua_state_id] = ref; |
| 10637 | |
| 10638 | /* pop the environment string. */ |
| 10639 | lua_pop(L, 1); |
| 10640 | continue; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 10641 | } |
| 10642 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 10643 | /* Check required environment. Only accepted "http" or "tcp". */ |
| 10644 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 10645 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 10646 | if (!akl) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 10647 | goto alloc_error;; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 10648 | fcn = new_hlua_function(); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 10649 | if (!fcn) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 10650 | goto alloc_error; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 10651 | |
| 10652 | /* Fill fcn. */ |
| 10653 | fcn->name = strdup(name); |
| 10654 | if (!fcn->name) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 10655 | goto alloc_error; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 10656 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 10657 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 10658 | /* Set the expected number of arguments. */ |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 10659 | fcn->nargs = nargs; |
| 10660 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 10661 | /* List head */ |
| 10662 | akl->list.n = akl->list.p = NULL; |
| 10663 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 10664 | /* action keyword. */ |
| 10665 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 10666 | akl->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 10667 | if (!akl->kw[0].kw) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 10668 | goto alloc_error; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 10669 | |
| 10670 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 10671 | |
Amaury Denoyelle | e4a617c | 2021-05-06 15:33:09 +0200 | [diff] [blame] | 10672 | akl->kw[0].flags = 0; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 10673 | akl->kw[0].private = fcn; |
| 10674 | akl->kw[0].parse = action_register_lua; |
| 10675 | |
| 10676 | /* select the action registering point. */ |
| 10677 | if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) |
| 10678 | tcp_req_cont_keywords_register(akl); |
| 10679 | else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) |
| 10680 | tcp_res_cont_keywords_register(akl); |
| 10681 | else if (strcmp(lua_tostring(L, -1), "http-req") == 0) |
| 10682 | http_req_keywords_register(akl); |
| 10683 | else if (strcmp(lua_tostring(L, -1), "http-res") == 0) |
| 10684 | http_res_keywords_register(akl); |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 10685 | else { |
| 10686 | release_hlua_function(fcn); |
Aurelien DARRAGON | 55afbed | 2023-03-02 18:42:06 +0100 | [diff] [blame] | 10687 | hlua_unref(L, ref); |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 10688 | if (akl) |
| 10689 | ha_free((char **)&(akl->kw[0].kw)); |
| 10690 | ha_free(&akl); |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 10691 | WILL_LJMP(luaL_error(L, "Lua action environment '%s' is unknown. " |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 10692 | "'tcp-req', 'tcp-res', 'http-req' or 'http-res' " |
| 10693 | "are expected.", lua_tostring(L, -1))); |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 10694 | } |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 10695 | |
| 10696 | /* pop the environment string. */ |
| 10697 | lua_pop(L, 1); |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 10698 | |
| 10699 | /* reset for next loop */ |
| 10700 | akl = NULL; |
| 10701 | fcn = NULL; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 10702 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10703 | return ACT_RET_PRS_OK; |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 10704 | |
| 10705 | alloc_error: |
| 10706 | release_hlua_function(fcn); |
Aurelien DARRAGON | 55afbed | 2023-03-02 18:42:06 +0100 | [diff] [blame] | 10707 | hlua_unref(L, ref); |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 10708 | ha_free(&akl); |
| 10709 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 10710 | return 0; /* Never reached */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10711 | } |
| 10712 | |
| 10713 | static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px, |
| 10714 | struct act_rule *rule, char **err) |
| 10715 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 10716 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10717 | |
Christopher Faulet | 280f85b | 2019-07-15 15:02:04 +0200 | [diff] [blame] | 10718 | if (px->mode == PR_MODE_HTTP) { |
| 10719 | memprintf(err, "Lua TCP services cannot be used on HTTP proxies"); |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 10720 | return ACT_RET_PRS_ERR; |
| 10721 | } |
| 10722 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10723 | /* Memory for the rule. */ |
| 10724 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 10725 | if (!rule->arg.hlua_rule) { |
| 10726 | memprintf(err, "out of memory error"); |
| 10727 | return ACT_RET_PRS_ERR; |
| 10728 | } |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 10729 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10730 | /* Reference the Lua function and store the reference. */ |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 10731 | rule->arg.hlua_rule->fcn = fcn; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10732 | |
| 10733 | /* TODO: later accept arguments. */ |
| 10734 | rule->arg.hlua_rule->args = NULL; |
| 10735 | |
| 10736 | /* Add applet pointer in the rule. */ |
| 10737 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 10738 | rule->applet.name = fcn->name; |
| 10739 | rule->applet.init = hlua_applet_tcp_init; |
| 10740 | rule->applet.fct = hlua_applet_tcp_fct; |
| 10741 | rule->applet.release = hlua_applet_tcp_release; |
| 10742 | rule->applet.timeout = hlua_timeout_applet; |
| 10743 | |
| 10744 | return 0; |
| 10745 | } |
| 10746 | |
| 10747 | /* This function is an LUA binding used for registering |
| 10748 | * "sample-conv" functions. It expects a converter name used |
| 10749 | * in the haproxy configuration file, and an LUA function. |
| 10750 | */ |
| 10751 | __LJMP static int hlua_register_service(lua_State *L) |
| 10752 | { |
| 10753 | struct action_kw_list *akl; |
| 10754 | const char *name; |
| 10755 | const char *env; |
| 10756 | int ref; |
| 10757 | int len; |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 10758 | struct hlua_function *fcn = NULL; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 10759 | struct buffer *trash; |
| 10760 | struct action_kw *akw; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10761 | |
| 10762 | MAY_LJMP(check_args(L, 3, "register_service")); |
| 10763 | |
Aurelien DARRAGON | 87f5297 | 2023-02-24 09:43:54 +0100 | [diff] [blame] | 10764 | if (hlua_gethlua(L)) { |
| 10765 | /* runtime processing */ |
| 10766 | WILL_LJMP(luaL_error(L, "register_service: not available outside of body context")); |
| 10767 | } |
| 10768 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10769 | /* First argument : converter name. */ |
| 10770 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 10771 | |
| 10772 | /* Second argument : environment. */ |
| 10773 | env = MAY_LJMP(luaL_checkstring(L, 2)); |
| 10774 | |
| 10775 | /* Third argument : lua function. */ |
| 10776 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 10777 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 10778 | /* Check for service already registered */ |
| 10779 | trash = get_trash_chunk(); |
| 10780 | chunk_printf(trash, "lua.%s", name); |
| 10781 | akw = service_find(trash->area); |
| 10782 | if (akw != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 10783 | fcn = akw->private; |
| 10784 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 10785 | ha_warning("Trying to register service 'lua.%s' more than once. " |
| 10786 | "This will become a hard error in version 2.5.\n", name); |
Aurelien DARRAGON | 55afbed | 2023-03-02 18:42:06 +0100 | [diff] [blame] | 10787 | hlua_unref(L, fcn->function_ref[hlua_state_id]); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 10788 | } |
| 10789 | fcn->function_ref[hlua_state_id] = ref; |
| 10790 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 10791 | } |
| 10792 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10793 | /* Allocate and fill the sample fetch keyword struct. */ |
| 10794 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
| 10795 | if (!akl) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 10796 | goto alloc_error; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 10797 | fcn = new_hlua_function(); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10798 | if (!fcn) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 10799 | goto alloc_error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10800 | |
| 10801 | /* Fill fcn. */ |
| 10802 | len = strlen("<lua.>") + strlen(name) + 1; |
| 10803 | fcn->name = calloc(1, len); |
| 10804 | if (!fcn->name) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 10805 | goto alloc_error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10806 | snprintf((char *)fcn->name, len, "<lua.%s>", name); |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 10807 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10808 | |
| 10809 | /* List head */ |
| 10810 | akl->list.n = akl->list.p = NULL; |
| 10811 | |
| 10812 | /* converter keyword. */ |
| 10813 | len = strlen("lua.") + strlen(name) + 1; |
| 10814 | akl->kw[0].kw = calloc(1, len); |
| 10815 | if (!akl->kw[0].kw) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 10816 | goto alloc_error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10817 | |
| 10818 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 10819 | |
Thierry FOURNIER / OZON.IO | 02564fd | 2016-11-12 11:07:05 +0100 | [diff] [blame] | 10820 | /* Check required environment. Only accepted "http" or "tcp". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10821 | if (strcmp(env, "tcp") == 0) |
| 10822 | akl->kw[0].parse = action_register_service_tcp; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 10823 | else if (strcmp(env, "http") == 0) |
| 10824 | akl->kw[0].parse = action_register_service_http; |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 10825 | else { |
| 10826 | release_hlua_function(fcn); |
Aurelien DARRAGON | 55afbed | 2023-03-02 18:42:06 +0100 | [diff] [blame] | 10827 | hlua_unref(L, ref); |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 10828 | if (akl) |
| 10829 | ha_free((char **)&(akl->kw[0].kw)); |
| 10830 | ha_free(&akl); |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 10831 | WILL_LJMP(luaL_error(L, "Lua service environment '%s' is unknown. " |
Eric Salama | fe7456f | 2017-12-21 14:30:07 +0100 | [diff] [blame] | 10832 | "'tcp' or 'http' are expected.", env)); |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 10833 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10834 | |
Amaury Denoyelle | e4a617c | 2021-05-06 15:33:09 +0200 | [diff] [blame] | 10835 | akl->kw[0].flags = 0; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10836 | akl->kw[0].private = fcn; |
| 10837 | |
| 10838 | /* End of array. */ |
| 10839 | memset(&akl->kw[1], 0, sizeof(*akl->kw)); |
| 10840 | |
| 10841 | /* Register this new converter */ |
| 10842 | service_keywords_register(akl); |
| 10843 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 10844 | return 0; |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 10845 | |
| 10846 | alloc_error: |
| 10847 | release_hlua_function(fcn); |
Aurelien DARRAGON | 55afbed | 2023-03-02 18:42:06 +0100 | [diff] [blame] | 10848 | hlua_unref(L, ref); |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 10849 | ha_free(&akl); |
| 10850 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 10851 | return 0; /* Never reached */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 10852 | } |
| 10853 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10854 | /* This function initialises Lua cli handler. It copies the |
| 10855 | * arguments in the Lua stack and create channel IO objects. |
| 10856 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 10857 | 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] | 10858 | { |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 10859 | 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] | 10860 | struct hlua *hlua; |
| 10861 | struct hlua_function *fcn; |
| 10862 | int i; |
| 10863 | const char *error; |
| 10864 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10865 | fcn = private; |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 10866 | ctx->fcn = private; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 10867 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 10868 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 10869 | if (!hlua) { |
| 10870 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 10871 | return 1; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 10872 | } |
| 10873 | HLUA_INIT(hlua); |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 10874 | ctx->hlua = hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10875 | |
| 10876 | /* Create task used by signal to wakeup applets. |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 10877 | * 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] | 10878 | * applet_http. It is absolutely compatible. |
| 10879 | */ |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 10880 | ctx->task = task_new_here(); |
| 10881 | if (!ctx->task) { |
Thierry FOURNIER | ffbf569 | 2016-12-16 11:14:06 +0100 | [diff] [blame] | 10882 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 10883 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10884 | } |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 10885 | ctx->task->nice = 0; |
| 10886 | ctx->task->context = appctx; |
| 10887 | ctx->task->process = hlua_applet_wakeup; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10888 | |
| 10889 | /* Initialises the Lua context */ |
Aurelien DARRAGON | 6b0b9bd | 2023-03-21 14:02:16 +0100 | [diff] [blame] | 10890 | 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] | 10891 | 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] | 10892 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10893 | } |
| 10894 | |
| 10895 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 10896 | if (!SET_SAFE_LJMP(hlua)) { |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10897 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 10898 | error = lua_tostring(hlua->T, -1); |
| 10899 | else |
| 10900 | error = "critical error"; |
| 10901 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error); |
| 10902 | goto error; |
| 10903 | } |
| 10904 | |
| 10905 | /* Check stack available size. */ |
| 10906 | if (!lua_checkstack(hlua->T, 2)) { |
| 10907 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 10908 | goto error; |
| 10909 | } |
| 10910 | |
| 10911 | /* Restore the function in the stack. */ |
Aurelien DARRAGON | 4fdf8b5 | 2023-03-20 17:22:37 +0100 | [diff] [blame] | 10912 | hlua_pushref(hlua->T, fcn->function_ref[hlua->state_id]); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10913 | |
| 10914 | /* Once the arguments parsed, the CLI is like an AppletTCP, |
| 10915 | * so push AppletTCP in the stack. |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10916 | */ |
| 10917 | if (!hlua_applet_tcp_new(hlua->T, appctx)) { |
| 10918 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 10919 | goto error; |
| 10920 | } |
| 10921 | hlua->nargs = 1; |
| 10922 | |
| 10923 | /* push keywords in the stack. */ |
| 10924 | for (i = 0; *args[i]; i++) { |
| 10925 | /* Check stack available size. */ |
| 10926 | if (!lua_checkstack(hlua->T, 1)) { |
| 10927 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 10928 | goto error; |
| 10929 | } |
| 10930 | lua_pushstring(hlua->T, args[i]); |
| 10931 | hlua->nargs++; |
| 10932 | } |
| 10933 | |
| 10934 | /* We must initialize the execution timeouts. */ |
Aurelien DARRAGON | da9503c | 2022-11-25 09:10:07 +0100 | [diff] [blame] | 10935 | hlua_timer_init(&hlua->timer, hlua_timeout_session); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10936 | |
| 10937 | /* At this point the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 10938 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10939 | |
| 10940 | /* It's ok */ |
| 10941 | return 0; |
| 10942 | |
| 10943 | /* It's not ok. */ |
| 10944 | error: |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 10945 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10946 | hlua_ctx_destroy(hlua); |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 10947 | ctx->hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10948 | return 1; |
| 10949 | } |
| 10950 | |
| 10951 | static int hlua_cli_io_handler_fct(struct appctx *appctx) |
| 10952 | { |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 10953 | struct hlua_cli_ctx *ctx = appctx->svcctx; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10954 | struct hlua *hlua; |
Willy Tarreau | 475e463 | 2022-05-27 10:26:46 +0200 | [diff] [blame] | 10955 | struct stconn *sc; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10956 | struct hlua_function *fcn; |
| 10957 | |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 10958 | hlua = ctx->hlua; |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 10959 | sc = appctx_sc(appctx); |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 10960 | fcn = ctx->fcn; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10961 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10962 | /* Execute the function. */ |
| 10963 | switch (hlua_ctx_resume(hlua, 1)) { |
| 10964 | |
| 10965 | /* finished. */ |
| 10966 | case HLUA_E_OK: |
| 10967 | return 1; |
| 10968 | |
| 10969 | /* yield. */ |
| 10970 | case HLUA_E_AGAIN: |
| 10971 | /* We want write. */ |
| 10972 | if (HLUA_IS_WAKERESWR(hlua)) |
Willy Tarreau | 475e463 | 2022-05-27 10:26:46 +0200 | [diff] [blame] | 10973 | sc_need_room(sc); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10974 | /* Set the timeout. */ |
| 10975 | if (hlua->wake_time != TICK_ETERNITY) |
| 10976 | task_schedule(hlua->task, hlua->wake_time); |
| 10977 | return 0; |
| 10978 | |
| 10979 | /* finished with error. */ |
| 10980 | case HLUA_E_ERRMSG: |
| 10981 | /* Display log. */ |
| 10982 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", |
| 10983 | fcn->name, lua_tostring(hlua->T, -1)); |
| 10984 | lua_pop(hlua->T, 1); |
| 10985 | return 1; |
| 10986 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 10987 | case HLUA_E_ETMOUT: |
| 10988 | SEND_ERR(NULL, "Lua converter '%s': execution timeout.\n", |
| 10989 | fcn->name); |
| 10990 | return 1; |
| 10991 | |
| 10992 | case HLUA_E_NOMEM: |
| 10993 | SEND_ERR(NULL, "Lua converter '%s': out of memory error.\n", |
| 10994 | fcn->name); |
| 10995 | return 1; |
| 10996 | |
| 10997 | case HLUA_E_YIELD: /* unexpected */ |
| 10998 | SEND_ERR(NULL, "Lua converter '%s': yield not allowed.\n", |
| 10999 | fcn->name); |
| 11000 | return 1; |
| 11001 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 11002 | case HLUA_E_ERR: |
| 11003 | /* Display log. */ |
| 11004 | SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n", |
| 11005 | fcn->name); |
| 11006 | return 1; |
| 11007 | |
| 11008 | default: |
| 11009 | return 1; |
| 11010 | } |
| 11011 | |
| 11012 | return 1; |
| 11013 | } |
| 11014 | |
| 11015 | static void hlua_cli_io_release_fct(struct appctx *appctx) |
| 11016 | { |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 11017 | struct hlua_cli_ctx *ctx = appctx->svcctx; |
| 11018 | |
| 11019 | hlua_ctx_destroy(ctx->hlua); |
| 11020 | ctx->hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 11021 | } |
| 11022 | |
| 11023 | /* This function is an LUA binding used for registering |
| 11024 | * new keywords in the cli. It expects a list of keywords |
| 11025 | * which are the "path". It is limited to 5 keywords. A |
| 11026 | * description of the command, a function to be executed |
| 11027 | * for the parsing and a function for io handlers. |
| 11028 | */ |
| 11029 | __LJMP static int hlua_register_cli(lua_State *L) |
| 11030 | { |
| 11031 | struct cli_kw_list *cli_kws; |
| 11032 | const char *message; |
| 11033 | int ref_io; |
| 11034 | int len; |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 11035 | struct hlua_function *fcn = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 11036 | int index; |
| 11037 | int i; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 11038 | struct buffer *trash; |
| 11039 | const char *kw[5]; |
| 11040 | struct cli_kw *cli_kw; |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 11041 | const char *errmsg; |
Willy Tarreau | 22450af | 2023-04-07 15:27:55 +0200 | [diff] [blame] | 11042 | char *end; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 11043 | |
| 11044 | MAY_LJMP(check_args(L, 3, "register_cli")); |
| 11045 | |
Aurelien DARRAGON | 87f5297 | 2023-02-24 09:43:54 +0100 | [diff] [blame] | 11046 | if (hlua_gethlua(L)) { |
| 11047 | /* runtime processing */ |
| 11048 | WILL_LJMP(luaL_error(L, "register_cli: not available outside of body context")); |
| 11049 | } |
| 11050 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 11051 | /* First argument : an array of maximum 5 keywords. */ |
| 11052 | if (!lua_istable(L, 1)) |
| 11053 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table")); |
| 11054 | |
| 11055 | /* Second argument : string with contextual message. */ |
| 11056 | message = MAY_LJMP(luaL_checkstring(L, 2)); |
| 11057 | |
| 11058 | /* Third and fourth argument : lua function. */ |
| 11059 | ref_io = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 11060 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 11061 | /* Check for CLI service already registered */ |
| 11062 | trash = get_trash_chunk(); |
| 11063 | index = 0; |
| 11064 | lua_pushnil(L); |
| 11065 | memset(kw, 0, sizeof(kw)); |
| 11066 | while (lua_next(L, 1) != 0) { |
Aurelien DARRAGON | 55afbed | 2023-03-02 18:42:06 +0100 | [diff] [blame] | 11067 | if (index >= CLI_PREFIX_KW_NB) { |
| 11068 | hlua_unref(L, ref_io); |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 11069 | 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] | 11070 | } |
| 11071 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 11072 | hlua_unref(L, ref_io); |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 11073 | 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] | 11074 | } |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 11075 | kw[index] = lua_tostring(L, -1); |
| 11076 | if (index == 0) |
| 11077 | chunk_printf(trash, "%s", kw[index]); |
| 11078 | else |
| 11079 | chunk_appendf(trash, " %s", kw[index]); |
| 11080 | index++; |
| 11081 | lua_pop(L, 1); |
| 11082 | } |
| 11083 | cli_kw = cli_find_kw_exact((char **)kw); |
| 11084 | if (cli_kw != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11085 | fcn = cli_kw->private; |
| 11086 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 11087 | ha_warning("Trying to register CLI keyword 'lua.%s' more than once. " |
| 11088 | "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] | 11089 | hlua_unref(L, fcn->function_ref[hlua_state_id]); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11090 | } |
| 11091 | fcn->function_ref[hlua_state_id] = ref_io; |
| 11092 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 11093 | } |
| 11094 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 11095 | /* Allocate and fill the sample fetch keyword struct. */ |
| 11096 | cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 11097 | if (!cli_kws) { |
| 11098 | errmsg = "Lua out of memory error."; |
| 11099 | goto error; |
| 11100 | } |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 11101 | fcn = new_hlua_function(); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 11102 | if (!fcn) { |
| 11103 | errmsg = "Lua out of memory error."; |
| 11104 | goto error; |
| 11105 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 11106 | |
| 11107 | /* Fill path. */ |
| 11108 | index = 0; |
| 11109 | lua_pushnil(L); |
| 11110 | while(lua_next(L, 1) != 0) { |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 11111 | if (index >= 5) { |
| 11112 | errmsg = "1st argument must be a table with a maximum of 5 entries"; |
| 11113 | goto error; |
| 11114 | } |
| 11115 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 11116 | errmsg = "1st argument must be a table filled with strings"; |
| 11117 | goto error; |
| 11118 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 11119 | cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1)); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 11120 | if (!cli_kws->kw[0].str_kw[index]) { |
| 11121 | errmsg = "Lua out of memory error."; |
| 11122 | goto error; |
| 11123 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 11124 | index++; |
| 11125 | lua_pop(L, 1); |
| 11126 | } |
| 11127 | |
| 11128 | /* Copy help message. */ |
| 11129 | cli_kws->kw[0].usage = strdup(message); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 11130 | if (!cli_kws->kw[0].usage) { |
| 11131 | errmsg = "Lua out of memory error."; |
| 11132 | goto error; |
| 11133 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 11134 | |
| 11135 | /* Fill fcn io handler. */ |
| 11136 | len = strlen("<lua.cli>") + 1; |
| 11137 | for (i = 0; i < index; i++) |
| 11138 | len += strlen(cli_kws->kw[0].str_kw[i]) + 1; |
| 11139 | fcn->name = calloc(1, len); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 11140 | if (!fcn->name) { |
| 11141 | errmsg = "Lua out of memory error."; |
| 11142 | goto error; |
| 11143 | } |
Willy Tarreau | 22450af | 2023-04-07 15:27:55 +0200 | [diff] [blame] | 11144 | |
| 11145 | end = fcn->name; |
| 11146 | len = 8; |
| 11147 | memcpy(end, "<lua.cli", len); |
| 11148 | end += len; |
| 11149 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 11150 | for (i = 0; i < index; i++) { |
Willy Tarreau | 22450af | 2023-04-07 15:27:55 +0200 | [diff] [blame] | 11151 | *(end++) = '.'; |
| 11152 | len = strlen(cli_kws->kw[0].str_kw[i]); |
| 11153 | memcpy(end, cli_kws->kw[0].str_kw[i], len); |
| 11154 | end += len; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 11155 | } |
Willy Tarreau | 22450af | 2023-04-07 15:27:55 +0200 | [diff] [blame] | 11156 | *(end++) = '>'; |
| 11157 | *(end++) = 0; |
| 11158 | |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 11159 | fcn->function_ref[hlua_state_id] = ref_io; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 11160 | |
| 11161 | /* Fill last entries. */ |
| 11162 | cli_kws->kw[0].private = fcn; |
| 11163 | cli_kws->kw[0].parse = hlua_cli_parse_fct; |
| 11164 | cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct; |
| 11165 | cli_kws->kw[0].io_release = hlua_cli_io_release_fct; |
| 11166 | |
| 11167 | /* Register this new converter */ |
| 11168 | cli_register_kw(cli_kws); |
| 11169 | |
| 11170 | return 0; |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 11171 | |
| 11172 | error: |
| 11173 | release_hlua_function(fcn); |
Aurelien DARRAGON | 55afbed | 2023-03-02 18:42:06 +0100 | [diff] [blame] | 11174 | hlua_unref(L, ref_io); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 11175 | if (cli_kws) { |
| 11176 | for (i = 0; i < index; i++) |
| 11177 | ha_free((char **)&(cli_kws->kw[0].str_kw[i])); |
| 11178 | ha_free((char **)&(cli_kws->kw[0].usage)); |
| 11179 | } |
| 11180 | ha_free(&cli_kws); |
| 11181 | WILL_LJMP(luaL_error(L, errmsg)); |
| 11182 | return 0; /* Never reached */ |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 11183 | } |
| 11184 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11185 | static int hlua_filter_init_per_thread(struct proxy *px, struct flt_conf *fconf) |
| 11186 | { |
| 11187 | struct hlua_flt_config *conf = fconf->conf; |
| 11188 | lua_State *L; |
| 11189 | int error, pos, state_id, flt_ref; |
| 11190 | |
| 11191 | state_id = reg_flt_to_stack_id(conf->reg); |
| 11192 | L = hlua_states[state_id]; |
| 11193 | pos = lua_gettop(L); |
| 11194 | |
| 11195 | /* The filter parsing function */ |
Aurelien DARRAGON | 4fdf8b5 | 2023-03-20 17:22:37 +0100 | [diff] [blame] | 11196 | hlua_pushref(L, conf->reg->fun_ref[state_id]); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11197 | |
| 11198 | /* Push the filter class on the stack and resolve all callbacks */ |
Aurelien DARRAGON | 4fdf8b5 | 2023-03-20 17:22:37 +0100 | [diff] [blame] | 11199 | hlua_pushref(L, conf->reg->flt_ref[state_id]); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11200 | |
| 11201 | /* Duplicate the filter class so each filter will have its own copy */ |
| 11202 | lua_newtable(L); |
| 11203 | lua_pushnil(L); |
| 11204 | |
| 11205 | while (lua_next(L, pos+2)) { |
| 11206 | lua_pushvalue(L, -2); |
| 11207 | lua_insert(L, -2); |
| 11208 | lua_settable(L, -4); |
| 11209 | } |
Aurelien DARRAGON | 73d1a98 | 2023-03-20 17:42:08 +0100 | [diff] [blame] | 11210 | flt_ref = hlua_ref(L); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11211 | |
| 11212 | /* Remove the original lua filter class from the stack */ |
| 11213 | lua_pop(L, 1); |
| 11214 | |
| 11215 | /* Push the copy on the stack */ |
Aurelien DARRAGON | 4fdf8b5 | 2023-03-20 17:22:37 +0100 | [diff] [blame] | 11216 | hlua_pushref(L, flt_ref); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11217 | |
| 11218 | /* extra args are pushed in a table */ |
| 11219 | lua_newtable(L); |
| 11220 | for (pos = 0; conf->args[pos]; pos++) { |
| 11221 | /* Check stack available size. */ |
| 11222 | if (!lua_checkstack(L, 1)) { |
| 11223 | ha_alert("Lua filter '%s' : Lua error : full stack.", conf->reg->name); |
| 11224 | goto error; |
| 11225 | } |
| 11226 | lua_pushstring(L, conf->args[pos]); |
| 11227 | lua_rawseti(L, -2, lua_rawlen(L, -2) + 1); |
| 11228 | } |
| 11229 | |
| 11230 | error = lua_pcall(L, 2, LUA_MULTRET, 0); |
| 11231 | switch (error) { |
| 11232 | case LUA_OK: |
| 11233 | /* replace the filter ref */ |
| 11234 | conf->ref[state_id] = flt_ref; |
| 11235 | break; |
| 11236 | case LUA_ERRRUN: |
| 11237 | ha_alert("Lua filter '%s' : runtime error : %s", conf->reg->name, lua_tostring(L, -1)); |
| 11238 | goto error; |
| 11239 | case LUA_ERRMEM: |
| 11240 | ha_alert("Lua filter '%s' : out of memory error", conf->reg->name); |
| 11241 | goto error; |
| 11242 | case LUA_ERRERR: |
| 11243 | ha_alert("Lua filter '%s' : message handler error : %s", conf->reg->name, lua_tostring(L, -1)); |
| 11244 | goto error; |
| 11245 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM <= 503 |
| 11246 | case LUA_ERRGCMM: |
| 11247 | ha_alert("Lua filter '%s' : garbage collector error : %s", conf->reg->name, lua_tostring(L, -1)); |
| 11248 | goto error; |
| 11249 | #endif |
| 11250 | default: |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 11251 | 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] | 11252 | goto error; |
| 11253 | } |
| 11254 | |
| 11255 | lua_settop(L, 0); |
| 11256 | return 0; |
| 11257 | |
| 11258 | error: |
| 11259 | lua_settop(L, 0); |
| 11260 | return -1; |
| 11261 | } |
| 11262 | |
| 11263 | static void hlua_filter_deinit_per_thread(struct proxy *px, struct flt_conf *fconf) |
| 11264 | { |
| 11265 | struct hlua_flt_config *conf = fconf->conf; |
| 11266 | lua_State *L; |
| 11267 | int state_id; |
| 11268 | |
| 11269 | if (!conf) |
| 11270 | return; |
| 11271 | |
| 11272 | state_id = reg_flt_to_stack_id(conf->reg); |
| 11273 | L = hlua_states[state_id]; |
Aurelien DARRAGON | fde199d | 2023-03-20 15:09:33 +0100 | [diff] [blame] | 11274 | hlua_unref(L, conf->ref[state_id]); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11275 | } |
| 11276 | |
| 11277 | static int hlua_filter_init(struct proxy *px, struct flt_conf *fconf) |
| 11278 | { |
| 11279 | struct hlua_flt_config *conf = fconf->conf; |
| 11280 | int state_id = reg_flt_to_stack_id(conf->reg); |
| 11281 | |
| 11282 | /* Rely on per-thread init for global scripts */ |
| 11283 | if (!state_id) |
| 11284 | return hlua_filter_init_per_thread(px, fconf); |
| 11285 | return 0; |
| 11286 | } |
| 11287 | |
| 11288 | static void hlua_filter_deinit(struct proxy *px, struct flt_conf *fconf) |
| 11289 | { |
| 11290 | |
| 11291 | if (fconf->conf) { |
| 11292 | struct hlua_flt_config *conf = fconf->conf; |
| 11293 | int state_id = reg_flt_to_stack_id(conf->reg); |
| 11294 | int pos; |
| 11295 | |
| 11296 | /* Rely on per-thread deinit for global scripts */ |
| 11297 | if (!state_id) |
| 11298 | hlua_filter_deinit_per_thread(px, fconf); |
| 11299 | |
| 11300 | for (pos = 0; conf->args[pos]; pos++) |
| 11301 | free(conf->args[pos]); |
| 11302 | free(conf->args); |
| 11303 | } |
| 11304 | ha_free(&fconf->conf); |
| 11305 | ha_free((char **)&fconf->id); |
| 11306 | ha_free(&fconf->ops); |
| 11307 | } |
| 11308 | |
| 11309 | static int hlua_filter_new(struct stream *s, struct filter *filter) |
| 11310 | { |
| 11311 | struct hlua_flt_config *conf = FLT_CONF(filter); |
| 11312 | struct hlua_flt_ctx *flt_ctx = NULL; |
| 11313 | int ret = 1; |
| 11314 | |
| 11315 | /* In the execution wrappers linked with a stream, the |
| 11316 | * Lua context can be not initialized. This behavior |
| 11317 | * permits to save performances because a systematic |
| 11318 | * Lua initialization cause 5% performances loss. |
| 11319 | */ |
| 11320 | if (!s->hlua) { |
| 11321 | struct hlua *hlua; |
| 11322 | |
| 11323 | hlua = pool_alloc(pool_head_hlua); |
| 11324 | if (!hlua) { |
| 11325 | SEND_ERR(s->be, "Lua filter '%s': can't initialize Lua context.\n", |
| 11326 | conf->reg->name); |
| 11327 | ret = 0; |
| 11328 | goto end; |
| 11329 | } |
| 11330 | HLUA_INIT(hlua); |
| 11331 | s->hlua = hlua; |
Aurelien DARRAGON | 6b0b9bd | 2023-03-21 14:02:16 +0100 | [diff] [blame] | 11332 | 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] | 11333 | SEND_ERR(s->be, "Lua filter '%s': can't initialize Lua context.\n", |
| 11334 | conf->reg->name); |
| 11335 | ret = 0; |
| 11336 | goto end; |
| 11337 | } |
| 11338 | } |
| 11339 | |
| 11340 | flt_ctx = pool_zalloc(pool_head_hlua_flt_ctx); |
| 11341 | if (!flt_ctx) { |
| 11342 | SEND_ERR(s->be, "Lua filter '%s': can't initialize filter Lua context.\n", |
| 11343 | conf->reg->name); |
| 11344 | ret = 0; |
| 11345 | goto end; |
| 11346 | } |
| 11347 | flt_ctx->hlua[0] = pool_alloc(pool_head_hlua); |
| 11348 | flt_ctx->hlua[1] = pool_alloc(pool_head_hlua); |
| 11349 | if (!flt_ctx->hlua[0] || !flt_ctx->hlua[1]) { |
| 11350 | SEND_ERR(s->be, "Lua filter '%s': can't initialize filter Lua context.\n", |
| 11351 | conf->reg->name); |
| 11352 | ret = 0; |
| 11353 | goto end; |
| 11354 | } |
| 11355 | HLUA_INIT(flt_ctx->hlua[0]); |
| 11356 | HLUA_INIT(flt_ctx->hlua[1]); |
Aurelien DARRAGON | 6b0b9bd | 2023-03-21 14:02:16 +0100 | [diff] [blame] | 11357 | if (!hlua_ctx_init(flt_ctx->hlua[0], reg_flt_to_stack_id(conf->reg), s->task) || |
| 11358 | !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] | 11359 | SEND_ERR(s->be, "Lua filter '%s': can't initialize filter Lua context.\n", |
| 11360 | conf->reg->name); |
| 11361 | ret = 0; |
| 11362 | goto end; |
| 11363 | } |
| 11364 | |
| 11365 | if (!HLUA_IS_RUNNING(s->hlua)) { |
| 11366 | /* The following Lua calls can fail. */ |
| 11367 | if (!SET_SAFE_LJMP(s->hlua)) { |
| 11368 | const char *error; |
| 11369 | |
| 11370 | if (lua_type(s->hlua->T, -1) == LUA_TSTRING) |
| 11371 | error = lua_tostring(s->hlua->T, -1); |
| 11372 | else |
| 11373 | error = "critical error"; |
| 11374 | SEND_ERR(s->be, "Lua filter '%s': %s.\n", conf->reg->name, error); |
| 11375 | ret = 0; |
| 11376 | goto end; |
| 11377 | } |
| 11378 | |
| 11379 | /* Check stack size. */ |
| 11380 | if (!lua_checkstack(s->hlua->T, 1)) { |
| 11381 | 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] | 11382 | RESET_SAFE_LJMP(s->hlua); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11383 | ret = 0; |
| 11384 | goto end; |
| 11385 | } |
| 11386 | |
Aurelien DARRAGON | 4fdf8b5 | 2023-03-20 17:22:37 +0100 | [diff] [blame] | 11387 | hlua_pushref(s->hlua->T, conf->ref[s->hlua->state_id]); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11388 | if (lua_getfield(s->hlua->T, -1, "new") != LUA_TFUNCTION) { |
| 11389 | SEND_ERR(s->be, "Lua filter '%s': 'new' field is not a function.\n", |
| 11390 | conf->reg->name); |
| 11391 | RESET_SAFE_LJMP(s->hlua); |
| 11392 | ret = 0; |
| 11393 | goto end; |
| 11394 | } |
| 11395 | lua_insert(s->hlua->T, -2); |
| 11396 | |
| 11397 | /* Push the copy on the stack */ |
| 11398 | s->hlua->nargs = 1; |
| 11399 | |
| 11400 | /* We must initialize the execution timeouts. */ |
Aurelien DARRAGON | da9503c | 2022-11-25 09:10:07 +0100 | [diff] [blame] | 11401 | hlua_timer_init(&s->hlua->timer, hlua_timeout_session); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11402 | |
| 11403 | /* At this point the execution is safe. */ |
| 11404 | RESET_SAFE_LJMP(s->hlua); |
| 11405 | } |
| 11406 | |
| 11407 | switch (hlua_ctx_resume(s->hlua, 0)) { |
| 11408 | case HLUA_E_OK: |
| 11409 | /* Nothing returned or not a table, ignore the filter for current stream */ |
| 11410 | if (!lua_gettop(s->hlua->T) || !lua_istable(s->hlua->T, 1)) { |
| 11411 | ret = 0; |
| 11412 | goto end; |
| 11413 | } |
| 11414 | |
| 11415 | /* Attached the filter pointer to the ctx */ |
| 11416 | lua_pushstring(s->hlua->T, "__filter"); |
| 11417 | lua_pushlightuserdata(s->hlua->T, filter); |
| 11418 | lua_settable(s->hlua->T, -3); |
| 11419 | |
| 11420 | /* Save a ref on the filter ctx */ |
| 11421 | lua_pushvalue(s->hlua->T, 1); |
Aurelien DARRAGON | 73d1a98 | 2023-03-20 17:42:08 +0100 | [diff] [blame] | 11422 | flt_ctx->ref = hlua_ref(s->hlua->T); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11423 | filter->ctx = flt_ctx; |
| 11424 | break; |
| 11425 | case HLUA_E_ERRMSG: |
| 11426 | SEND_ERR(s->be, "Lua filter '%s' : %s.\n", conf->reg->name, lua_tostring(s->hlua->T, -1)); |
| 11427 | ret = -1; |
| 11428 | goto end; |
| 11429 | case HLUA_E_ETMOUT: |
| 11430 | SEND_ERR(s->be, "Lua filter '%s' : 'new' execution timeout.\n", conf->reg->name); |
| 11431 | ret = 0; |
| 11432 | goto end; |
| 11433 | case HLUA_E_NOMEM: |
| 11434 | SEND_ERR(s->be, "Lua filter '%s' : out of memory error.\n", conf->reg->name); |
| 11435 | ret = 0; |
| 11436 | goto end; |
| 11437 | case HLUA_E_AGAIN: |
| 11438 | case HLUA_E_YIELD: |
| 11439 | SEND_ERR(s->be, "Lua filter '%s': yield functions like core.tcp() or core.sleep()" |
| 11440 | " are not allowed from 'new' function.\n", conf->reg->name); |
| 11441 | ret = 0; |
| 11442 | goto end; |
| 11443 | case HLUA_E_ERR: |
| 11444 | SEND_ERR(s->be, "Lua filter '%s': 'new' returns an unknown error.\n", conf->reg->name); |
| 11445 | ret = 0; |
| 11446 | goto end; |
| 11447 | default: |
| 11448 | ret = 0; |
| 11449 | goto end; |
| 11450 | } |
| 11451 | |
| 11452 | end: |
| 11453 | if (s->hlua) |
| 11454 | lua_settop(s->hlua->T, 0); |
| 11455 | if (ret <= 0) { |
| 11456 | if (flt_ctx) { |
| 11457 | hlua_ctx_destroy(flt_ctx->hlua[0]); |
| 11458 | hlua_ctx_destroy(flt_ctx->hlua[1]); |
| 11459 | pool_free(pool_head_hlua_flt_ctx, flt_ctx); |
| 11460 | } |
| 11461 | } |
| 11462 | return ret; |
| 11463 | } |
| 11464 | |
| 11465 | static void hlua_filter_delete(struct stream *s, struct filter *filter) |
| 11466 | { |
| 11467 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 11468 | |
Aurelien DARRAGON | fde199d | 2023-03-20 15:09:33 +0100 | [diff] [blame] | 11469 | hlua_unref(s->hlua->T, flt_ctx->ref); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11470 | hlua_ctx_destroy(flt_ctx->hlua[0]); |
| 11471 | hlua_ctx_destroy(flt_ctx->hlua[1]); |
| 11472 | pool_free(pool_head_hlua_flt_ctx, flt_ctx); |
| 11473 | filter->ctx = NULL; |
| 11474 | } |
| 11475 | |
Christopher Faulet | 9f55a50 | 2020-02-25 15:21:02 +0100 | [diff] [blame] | 11476 | static int hlua_filter_from_payload(struct filter *filter) |
| 11477 | { |
| 11478 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 11479 | |
| 11480 | return (flt_ctx && !!(flt_ctx->flags & HLUA_FLT_CTX_FL_PAYLOAD)); |
| 11481 | } |
| 11482 | |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 11483 | static int hlua_filter_callback(struct stream *s, struct filter *filter, const char *fun, |
| 11484 | int dir, unsigned int flags) |
| 11485 | { |
| 11486 | struct hlua *flt_hlua; |
| 11487 | struct hlua_flt_config *conf = FLT_CONF(filter); |
| 11488 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 11489 | unsigned int hflags = HLUA_TXN_FLT_CTX; |
| 11490 | int ret = 1; |
| 11491 | |
| 11492 | flt_hlua = flt_ctx->hlua[(dir == SMP_OPT_DIR_REQ ? 0 : 1)]; |
| 11493 | if (!flt_hlua) |
| 11494 | goto end; |
| 11495 | |
| 11496 | if (!HLUA_IS_RUNNING(flt_hlua)) { |
| 11497 | int extra_idx = lua_gettop(flt_hlua->T); |
| 11498 | |
| 11499 | /* The following Lua calls can fail. */ |
| 11500 | if (!SET_SAFE_LJMP(flt_hlua)) { |
| 11501 | const char *error; |
| 11502 | |
| 11503 | if (lua_type(flt_hlua->T, -1) == LUA_TSTRING) |
| 11504 | error = lua_tostring(flt_hlua->T, -1); |
| 11505 | else |
| 11506 | error = "critical error"; |
| 11507 | SEND_ERR(s->be, "Lua filter '%s': %s.\n", conf->reg->name, error); |
| 11508 | goto end; |
| 11509 | } |
| 11510 | |
| 11511 | /* Check stack size. */ |
| 11512 | if (!lua_checkstack(flt_hlua->T, 3)) { |
| 11513 | SEND_ERR(s->be, "Lua filter '%s': full stack.\n", conf->reg->name); |
| 11514 | RESET_SAFE_LJMP(flt_hlua); |
| 11515 | goto end; |
| 11516 | } |
| 11517 | |
Aurelien DARRAGON | 4fdf8b5 | 2023-03-20 17:22:37 +0100 | [diff] [blame] | 11518 | hlua_pushref(flt_hlua->T, flt_ctx->ref); |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 11519 | if (lua_getfield(flt_hlua->T, -1, fun) != LUA_TFUNCTION) { |
| 11520 | RESET_SAFE_LJMP(flt_hlua); |
| 11521 | goto end; |
| 11522 | } |
| 11523 | lua_insert(flt_hlua->T, -2); |
| 11524 | |
| 11525 | if (!hlua_txn_new(flt_hlua->T, s, s->be, dir, hflags)) { |
| 11526 | SEND_ERR(s->be, "Lua filter '%s': full stack.\n", conf->reg->name); |
| 11527 | RESET_SAFE_LJMP(flt_hlua); |
| 11528 | goto end; |
| 11529 | } |
| 11530 | flt_hlua->nargs = 2; |
| 11531 | |
| 11532 | if (flags & HLUA_FLT_CB_ARG_CHN) { |
| 11533 | if (dir == SMP_OPT_DIR_REQ) |
| 11534 | lua_getfield(flt_hlua->T, -1, "req"); |
| 11535 | else |
| 11536 | lua_getfield(flt_hlua->T, -1, "res"); |
| 11537 | if (lua_type(flt_hlua->T, -1) == LUA_TTABLE) { |
| 11538 | lua_pushstring(flt_hlua->T, "__filter"); |
| 11539 | lua_pushlightuserdata(flt_hlua->T, filter); |
| 11540 | lua_settable(flt_hlua->T, -3); |
| 11541 | } |
| 11542 | flt_hlua->nargs++; |
| 11543 | } |
| 11544 | else if (flags & HLUA_FLT_CB_ARG_HTTP_MSG) { |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 11545 | if (dir == SMP_OPT_DIR_REQ) |
| 11546 | lua_getfield(flt_hlua->T, -1, "http_req"); |
| 11547 | else |
| 11548 | lua_getfield(flt_hlua->T, -1, "http_res"); |
| 11549 | if (lua_type(flt_hlua->T, -1) == LUA_TTABLE) { |
| 11550 | lua_pushstring(flt_hlua->T, "__filter"); |
| 11551 | lua_pushlightuserdata(flt_hlua->T, filter); |
| 11552 | lua_settable(flt_hlua->T, -3); |
| 11553 | } |
| 11554 | flt_hlua->nargs++; |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 11555 | } |
| 11556 | |
| 11557 | /* Check stack size. */ |
| 11558 | if (!lua_checkstack(flt_hlua->T, 1)) { |
| 11559 | SEND_ERR(s->be, "Lua filter '%s': full stack.\n", conf->reg->name); |
| 11560 | RESET_SAFE_LJMP(flt_hlua); |
| 11561 | goto end; |
| 11562 | } |
| 11563 | |
| 11564 | while (extra_idx--) { |
| 11565 | lua_pushvalue(flt_hlua->T, 1); |
| 11566 | lua_remove(flt_hlua->T, 1); |
| 11567 | flt_hlua->nargs++; |
| 11568 | } |
| 11569 | |
| 11570 | /* We must initialize the execution timeouts. */ |
Aurelien DARRAGON | da9503c | 2022-11-25 09:10:07 +0100 | [diff] [blame] | 11571 | hlua_timer_init(&flt_hlua->timer, hlua_timeout_session); |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 11572 | |
| 11573 | /* At this point the execution is safe. */ |
| 11574 | RESET_SAFE_LJMP(flt_hlua); |
| 11575 | } |
| 11576 | |
| 11577 | switch (hlua_ctx_resume(flt_hlua, !(flags & HLUA_FLT_CB_FINAL))) { |
| 11578 | case HLUA_E_OK: |
| 11579 | /* Catch the return value if it required */ |
| 11580 | if ((flags & HLUA_FLT_CB_RETVAL) && lua_gettop(flt_hlua->T) > 0) { |
| 11581 | ret = lua_tointeger(flt_hlua->T, -1); |
| 11582 | lua_settop(flt_hlua->T, 0); /* Empty the stack. */ |
| 11583 | } |
| 11584 | |
| 11585 | /* Set timeout in the required channel. */ |
| 11586 | if (flt_hlua->wake_time != TICK_ETERNITY) { |
| 11587 | if (dir == SMP_OPT_DIR_REQ) |
| 11588 | s->req.analyse_exp = flt_hlua->wake_time; |
| 11589 | else |
| 11590 | s->res.analyse_exp = flt_hlua->wake_time; |
| 11591 | } |
| 11592 | break; |
| 11593 | case HLUA_E_AGAIN: |
| 11594 | /* Set timeout in the required channel. */ |
| 11595 | if (flt_hlua->wake_time != TICK_ETERNITY) { |
| 11596 | if (dir == SMP_OPT_DIR_REQ) |
| 11597 | s->req.analyse_exp = flt_hlua->wake_time; |
| 11598 | else |
| 11599 | s->res.analyse_exp = flt_hlua->wake_time; |
| 11600 | } |
| 11601 | /* Some actions can be wake up when a "write" event |
| 11602 | * is detected on a response channel. This is useful |
| 11603 | * only for actions targeted on the requests. |
| 11604 | */ |
| 11605 | if (HLUA_IS_WAKERESWR(flt_hlua)) |
| 11606 | s->res.flags |= CF_WAKE_WRITE; |
| 11607 | if (HLUA_IS_WAKEREQWR(flt_hlua)) |
| 11608 | s->req.flags |= CF_WAKE_WRITE; |
| 11609 | ret = 0; |
| 11610 | goto end; |
| 11611 | case HLUA_E_ERRMSG: |
| 11612 | SEND_ERR(s->be, "Lua filter '%s' : %s.\n", conf->reg->name, lua_tostring(flt_hlua->T, -1)); |
| 11613 | ret = -1; |
| 11614 | goto end; |
| 11615 | case HLUA_E_ETMOUT: |
| 11616 | SEND_ERR(s->be, "Lua filter '%s' : '%s' callback execution timeout.\n", conf->reg->name, fun); |
| 11617 | goto end; |
| 11618 | case HLUA_E_NOMEM: |
| 11619 | SEND_ERR(s->be, "Lua filter '%s' : out of memory error.\n", conf->reg->name); |
| 11620 | goto end; |
| 11621 | case HLUA_E_YIELD: |
| 11622 | SEND_ERR(s->be, "Lua filter '%s': yield functions like core.tcp() or core.sleep()" |
| 11623 | " are not allowed from '%s' callback.\n", conf->reg->name, fun); |
| 11624 | goto end; |
| 11625 | case HLUA_E_ERR: |
| 11626 | SEND_ERR(s->be, "Lua filter '%s': '%s' returns an unknown error.\n", conf->reg->name, fun); |
| 11627 | goto end; |
| 11628 | default: |
| 11629 | goto end; |
| 11630 | } |
| 11631 | |
| 11632 | |
| 11633 | end: |
| 11634 | return ret; |
| 11635 | } |
| 11636 | |
| 11637 | static int hlua_filter_start_analyze(struct stream *s, struct filter *filter, struct channel *chn) |
| 11638 | { |
| 11639 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 11640 | |
| 11641 | flt_ctx->flags = 0; |
| 11642 | return hlua_filter_callback(s, filter, "start_analyze", |
| 11643 | (!(chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES), |
| 11644 | (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_RETVAL | HLUA_FLT_CB_ARG_CHN)); |
| 11645 | } |
| 11646 | |
| 11647 | static int hlua_filter_end_analyze(struct stream *s, struct filter *filter, struct channel *chn) |
| 11648 | { |
| 11649 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 11650 | |
| 11651 | flt_ctx->flags &= ~HLUA_FLT_CTX_FL_PAYLOAD; |
| 11652 | return hlua_filter_callback(s, filter, "end_analyze", |
| 11653 | (!(chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES), |
| 11654 | (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_RETVAL | HLUA_FLT_CB_ARG_CHN)); |
| 11655 | } |
| 11656 | |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 11657 | static int hlua_filter_http_headers(struct stream *s, struct filter *filter, struct http_msg *msg) |
| 11658 | { |
| 11659 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 11660 | |
| 11661 | flt_ctx->flags &= ~HLUA_FLT_CTX_FL_PAYLOAD; |
| 11662 | return hlua_filter_callback(s, filter, "http_headers", |
| 11663 | (!(msg->chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES), |
| 11664 | (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_RETVAL | HLUA_FLT_CB_ARG_HTTP_MSG)); |
| 11665 | } |
| 11666 | |
| 11667 | static int hlua_filter_http_payload(struct stream *s, struct filter *filter, struct http_msg *msg, |
| 11668 | unsigned int offset, unsigned int len) |
| 11669 | { |
| 11670 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 11671 | struct hlua *flt_hlua; |
| 11672 | int dir = (!(msg->chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES); |
| 11673 | int idx = (dir == SMP_OPT_DIR_REQ ? 0 : 1); |
| 11674 | int ret; |
| 11675 | |
| 11676 | flt_hlua = flt_ctx->hlua[idx]; |
| 11677 | flt_ctx->cur_off[idx] = offset; |
| 11678 | flt_ctx->cur_len[idx] = len; |
| 11679 | flt_ctx->flags |= HLUA_FLT_CTX_FL_PAYLOAD; |
| 11680 | ret = hlua_filter_callback(s, filter, "http_payload", dir, (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_ARG_HTTP_MSG)); |
| 11681 | if (ret != -1) { |
| 11682 | ret = flt_ctx->cur_len[idx]; |
| 11683 | if (lua_gettop(flt_hlua->T) > 0) { |
| 11684 | ret = lua_tointeger(flt_hlua->T, -1); |
| 11685 | if (ret > flt_ctx->cur_len[idx]) |
| 11686 | ret = flt_ctx->cur_len[idx]; |
| 11687 | lua_settop(flt_hlua->T, 0); /* Empty the stack. */ |
| 11688 | } |
| 11689 | } |
| 11690 | return ret; |
| 11691 | } |
| 11692 | |
| 11693 | static int hlua_filter_http_end(struct stream *s, struct filter *filter, struct http_msg *msg) |
| 11694 | { |
| 11695 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 11696 | |
| 11697 | flt_ctx->flags &= ~HLUA_FLT_CTX_FL_PAYLOAD; |
| 11698 | return hlua_filter_callback(s, filter, "http_end", |
| 11699 | (!(msg->chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES), |
| 11700 | (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_RETVAL | HLUA_FLT_CB_ARG_HTTP_MSG)); |
| 11701 | } |
| 11702 | |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 11703 | static int hlua_filter_tcp_payload(struct stream *s, struct filter *filter, struct channel *chn, |
| 11704 | unsigned int offset, unsigned int len) |
| 11705 | { |
| 11706 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 11707 | struct hlua *flt_hlua; |
| 11708 | int dir = (!(chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES); |
| 11709 | int idx = (dir == SMP_OPT_DIR_REQ ? 0 : 1); |
| 11710 | int ret; |
| 11711 | |
| 11712 | flt_hlua = flt_ctx->hlua[idx]; |
| 11713 | flt_ctx->cur_off[idx] = offset; |
| 11714 | flt_ctx->cur_len[idx] = len; |
| 11715 | flt_ctx->flags |= HLUA_FLT_CTX_FL_PAYLOAD; |
| 11716 | ret = hlua_filter_callback(s, filter, "tcp_payload", dir, (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_ARG_CHN)); |
| 11717 | if (ret != -1) { |
| 11718 | ret = flt_ctx->cur_len[idx]; |
| 11719 | if (lua_gettop(flt_hlua->T) > 0) { |
| 11720 | ret = lua_tointeger(flt_hlua->T, -1); |
| 11721 | if (ret > flt_ctx->cur_len[idx]) |
| 11722 | ret = flt_ctx->cur_len[idx]; |
| 11723 | lua_settop(flt_hlua->T, 0); /* Empty the stack. */ |
| 11724 | } |
| 11725 | } |
| 11726 | return ret; |
| 11727 | } |
| 11728 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11729 | static int hlua_filter_parse_fct(char **args, int *cur_arg, struct proxy *px, |
| 11730 | struct flt_conf *fconf, char **err, void *private) |
| 11731 | { |
| 11732 | struct hlua_reg_filter *reg_flt = private; |
| 11733 | lua_State *L; |
| 11734 | struct hlua_flt_config *conf = NULL; |
| 11735 | const char *flt_id = NULL; |
| 11736 | int state_id, pos, flt_flags = 0; |
| 11737 | struct flt_ops *hlua_flt_ops = NULL; |
| 11738 | |
| 11739 | state_id = reg_flt_to_stack_id(reg_flt); |
| 11740 | L = hlua_states[state_id]; |
| 11741 | |
| 11742 | /* Initialize the filter ops with default callbacks */ |
| 11743 | hlua_flt_ops = calloc(1, sizeof(*hlua_flt_ops)); |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 11744 | if (!hlua_flt_ops) |
| 11745 | goto error; |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11746 | hlua_flt_ops->init = hlua_filter_init; |
| 11747 | hlua_flt_ops->deinit = hlua_filter_deinit; |
| 11748 | if (state_id) { |
| 11749 | /* Set per-thread callback if script is loaded per-thread */ |
| 11750 | hlua_flt_ops->init_per_thread = hlua_filter_init_per_thread; |
| 11751 | hlua_flt_ops->deinit_per_thread = hlua_filter_deinit_per_thread; |
| 11752 | } |
| 11753 | hlua_flt_ops->attach = hlua_filter_new; |
| 11754 | hlua_flt_ops->detach = hlua_filter_delete; |
| 11755 | |
| 11756 | /* Push the filter class on the stack and resolve all callbacks */ |
Aurelien DARRAGON | 4fdf8b5 | 2023-03-20 17:22:37 +0100 | [diff] [blame] | 11757 | hlua_pushref(L, reg_flt->flt_ref[state_id]); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11758 | |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 11759 | if (lua_getfield(L, -1, "start_analyze") == LUA_TFUNCTION) |
| 11760 | hlua_flt_ops->channel_start_analyze = hlua_filter_start_analyze; |
| 11761 | lua_pop(L, 1); |
| 11762 | if (lua_getfield(L, -1, "end_analyze") == LUA_TFUNCTION) |
| 11763 | hlua_flt_ops->channel_end_analyze = hlua_filter_end_analyze; |
| 11764 | lua_pop(L, 1); |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 11765 | if (lua_getfield(L, -1, "http_headers") == LUA_TFUNCTION) |
| 11766 | hlua_flt_ops->http_headers = hlua_filter_http_headers; |
| 11767 | lua_pop(L, 1); |
| 11768 | if (lua_getfield(L, -1, "http_payload") == LUA_TFUNCTION) |
| 11769 | hlua_flt_ops->http_payload = hlua_filter_http_payload; |
| 11770 | lua_pop(L, 1); |
| 11771 | if (lua_getfield(L, -1, "http_end") == LUA_TFUNCTION) |
| 11772 | hlua_flt_ops->http_end = hlua_filter_http_end; |
| 11773 | lua_pop(L, 1); |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 11774 | if (lua_getfield(L, -1, "tcp_payload") == LUA_TFUNCTION) |
| 11775 | hlua_flt_ops->tcp_payload = hlua_filter_tcp_payload; |
| 11776 | lua_pop(L, 1); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11777 | |
| 11778 | /* Get id and flags of the filter class */ |
| 11779 | if (lua_getfield(L, -1, "id") == LUA_TSTRING) |
| 11780 | flt_id = lua_tostring(L, -1); |
| 11781 | lua_pop(L, 1); |
| 11782 | if (lua_getfield(L, -1, "flags") == LUA_TNUMBER) |
| 11783 | flt_flags = lua_tointeger(L, -1); |
| 11784 | lua_pop(L, 1); |
| 11785 | |
| 11786 | /* Create the filter config */ |
| 11787 | conf = calloc(1, sizeof(*conf)); |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 11788 | if (!conf) |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11789 | goto error; |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11790 | conf->reg = reg_flt; |
| 11791 | |
| 11792 | /* duplicate args */ |
| 11793 | for (pos = 0; *args[*cur_arg + 1 + pos]; pos++); |
| 11794 | conf->args = calloc(pos + 1, sizeof(*conf->args)); |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 11795 | if (!conf->args) |
| 11796 | goto error; |
| 11797 | for (pos = 0; *args[*cur_arg + 1 + pos]; pos++) { |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11798 | conf->args[pos] = strdup(args[*cur_arg + 1 + pos]); |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 11799 | if (!conf->args[pos]) |
| 11800 | goto error; |
| 11801 | } |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11802 | conf->args[pos] = NULL; |
| 11803 | *cur_arg += pos + 1; |
| 11804 | |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 11805 | if (flt_id) { |
| 11806 | fconf->id = strdup(flt_id); |
| 11807 | if (!fconf->id) |
| 11808 | goto error; |
| 11809 | } |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11810 | fconf->flags = flt_flags; |
| 11811 | fconf->conf = conf; |
| 11812 | fconf->ops = hlua_flt_ops; |
| 11813 | |
| 11814 | lua_settop(L, 0); |
| 11815 | return 0; |
| 11816 | |
| 11817 | error: |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 11818 | 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] | 11819 | free(hlua_flt_ops); |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 11820 | if (conf && conf->args) { |
| 11821 | for (pos = 0; conf->args[pos]; pos++) |
| 11822 | free(conf->args[pos]); |
| 11823 | free(conf->args); |
| 11824 | } |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11825 | free(conf); |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 11826 | free((char *)fconf->id); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11827 | lua_settop(L, 0); |
| 11828 | return -1; |
| 11829 | } |
| 11830 | |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 11831 | __LJMP static int hlua_register_data_filter(lua_State *L) |
| 11832 | { |
| 11833 | struct filter *filter; |
| 11834 | struct channel *chn; |
| 11835 | |
| 11836 | MAY_LJMP(check_args(L, 2, "register_data_filter")); |
| 11837 | MAY_LJMP(luaL_checktype(L, 1, LUA_TTABLE)); |
| 11838 | chn = MAY_LJMP(hlua_checkchannel(L, 2)); |
| 11839 | |
| 11840 | lua_getfield(L, 1, "__filter"); |
| 11841 | MAY_LJMP(luaL_checktype(L, -1, LUA_TLIGHTUSERDATA)); |
| 11842 | filter = lua_touserdata (L, -1); |
| 11843 | lua_pop(L, 1); |
| 11844 | |
| 11845 | register_data_filter(chn_strm(chn), chn, filter); |
| 11846 | return 1; |
| 11847 | } |
| 11848 | |
| 11849 | __LJMP static int hlua_unregister_data_filter(lua_State *L) |
| 11850 | { |
| 11851 | struct filter *filter; |
| 11852 | struct channel *chn; |
| 11853 | |
| 11854 | MAY_LJMP(check_args(L, 2, "unregister_data_filter")); |
| 11855 | MAY_LJMP(luaL_checktype(L, 1, LUA_TTABLE)); |
| 11856 | chn = MAY_LJMP(hlua_checkchannel(L, 2)); |
| 11857 | |
| 11858 | lua_getfield(L, 1, "__filter"); |
| 11859 | MAY_LJMP(luaL_checktype(L, -1, LUA_TLIGHTUSERDATA)); |
| 11860 | filter = lua_touserdata (L, -1); |
| 11861 | lua_pop(L, 1); |
| 11862 | |
| 11863 | unregister_data_filter(chn_strm(chn), chn, filter); |
| 11864 | return 1; |
| 11865 | } |
| 11866 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11867 | /* 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] | 11868 | * 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] | 11869 | * parse configuration arguments. |
| 11870 | */ |
| 11871 | __LJMP static int hlua_register_filter(lua_State *L) |
| 11872 | { |
| 11873 | struct buffer *trash; |
| 11874 | struct flt_kw_list *fkl; |
| 11875 | struct flt_kw *fkw; |
| 11876 | const char *name; |
| 11877 | struct hlua_reg_filter *reg_flt= NULL; |
| 11878 | int flt_ref, fun_ref; |
| 11879 | int len; |
| 11880 | |
| 11881 | MAY_LJMP(check_args(L, 3, "register_filter")); |
| 11882 | |
Aurelien DARRAGON | 87f5297 | 2023-02-24 09:43:54 +0100 | [diff] [blame] | 11883 | if (hlua_gethlua(L)) { |
| 11884 | /* runtime processing */ |
| 11885 | WILL_LJMP(luaL_error(L, "register_filter: not available outside of body context")); |
| 11886 | } |
| 11887 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11888 | /* First argument : filter name. */ |
| 11889 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 11890 | |
| 11891 | /* Second argument : The filter class */ |
| 11892 | flt_ref = MAY_LJMP(hlua_checktable(L, 2)); |
| 11893 | |
| 11894 | /* Third argument : lua function. */ |
| 11895 | fun_ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 11896 | |
| 11897 | trash = get_trash_chunk(); |
| 11898 | chunk_printf(trash, "lua.%s", name); |
| 11899 | fkw = flt_find_kw(trash->area); |
| 11900 | if (fkw != NULL) { |
| 11901 | reg_flt = fkw->private; |
| 11902 | if (reg_flt->flt_ref[hlua_state_id] != -1 || reg_flt->fun_ref[hlua_state_id] != -1) { |
| 11903 | ha_warning("Trying to register filter 'lua.%s' more than once. " |
| 11904 | "This will become a hard error in version 2.5.\n", name); |
Aurelien DARRAGON | 55afbed | 2023-03-02 18:42:06 +0100 | [diff] [blame] | 11905 | if (reg_flt->flt_ref[hlua_state_id] != -1) |
| 11906 | hlua_unref(L, reg_flt->flt_ref[hlua_state_id]); |
| 11907 | if (reg_flt->fun_ref[hlua_state_id] != -1) |
| 11908 | hlua_unref(L, reg_flt->fun_ref[hlua_state_id]); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11909 | } |
| 11910 | reg_flt->flt_ref[hlua_state_id] = flt_ref; |
| 11911 | reg_flt->fun_ref[hlua_state_id] = fun_ref; |
| 11912 | return 0; |
| 11913 | } |
| 11914 | |
| 11915 | fkl = calloc(1, sizeof(*fkl) + sizeof(struct flt_kw) * 2); |
| 11916 | if (!fkl) |
| 11917 | goto alloc_error; |
| 11918 | fkl->scope = "HLUA"; |
| 11919 | |
| 11920 | reg_flt = new_hlua_reg_filter(name); |
| 11921 | if (!reg_flt) |
| 11922 | goto alloc_error; |
| 11923 | |
| 11924 | reg_flt->flt_ref[hlua_state_id] = flt_ref; |
| 11925 | reg_flt->fun_ref[hlua_state_id] = fun_ref; |
| 11926 | |
| 11927 | /* The filter keyword */ |
| 11928 | len = strlen("lua.") + strlen(name) + 1; |
| 11929 | fkl->kw[0].kw = calloc(1, len); |
| 11930 | if (!fkl->kw[0].kw) |
| 11931 | goto alloc_error; |
| 11932 | |
| 11933 | snprintf((char *)fkl->kw[0].kw, len, "lua.%s", name); |
| 11934 | |
| 11935 | fkl->kw[0].parse = hlua_filter_parse_fct; |
| 11936 | fkl->kw[0].private = reg_flt; |
| 11937 | memset(&fkl->kw[1], 0, sizeof(*fkl->kw)); |
| 11938 | |
| 11939 | /* Register this new filter */ |
| 11940 | flt_register_keywords(fkl); |
| 11941 | |
| 11942 | return 0; |
| 11943 | |
| 11944 | alloc_error: |
| 11945 | release_hlua_reg_filter(reg_flt); |
Aurelien DARRAGON | 55afbed | 2023-03-02 18:42:06 +0100 | [diff] [blame] | 11946 | hlua_unref(L, flt_ref); |
| 11947 | hlua_unref(L, fun_ref); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11948 | ha_free(&fkl); |
| 11949 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 11950 | return 0; /* Never reached */ |
| 11951 | } |
| 11952 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 11953 | 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] | 11954 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 11955 | char **err, unsigned int *timeout) |
| 11956 | { |
| 11957 | const char *error; |
| 11958 | |
| 11959 | error = parse_time_err(args[1], timeout, TIME_UNIT_MS); |
Willy Tarreau | 9faebe3 | 2019-06-07 19:00:37 +0200 | [diff] [blame] | 11960 | if (error == PARSE_TIME_OVER) { |
| 11961 | memprintf(err, "timer overflow in argument <%s> to <%s> (maximum value is 2147483647 ms or ~24.8 days)", |
| 11962 | args[1], args[0]); |
| 11963 | return -1; |
| 11964 | } |
| 11965 | else if (error == PARSE_TIME_UNDER) { |
| 11966 | memprintf(err, "timer underflow in argument <%s> to <%s> (minimum non-null value is 1 ms)", |
| 11967 | args[1], args[0]); |
| 11968 | return -1; |
| 11969 | } |
| 11970 | else if (error) { |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 11971 | memprintf(err, "%s: invalid timeout", args[0]); |
| 11972 | return -1; |
| 11973 | } |
| 11974 | return 0; |
| 11975 | } |
| 11976 | |
Aurelien DARRAGON | 58e36e5 | 2023-04-06 22:51:56 +0200 | [diff] [blame^] | 11977 | static int hlua_burst_timeout(char **args, int section_type, struct proxy *curpx, |
| 11978 | const struct proxy *defpx, const char *file, int line, |
| 11979 | char **err) |
| 11980 | { |
| 11981 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 11982 | file, line, err, &hlua_timeout_burst); |
| 11983 | } |
| 11984 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 11985 | 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] | 11986 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 11987 | char **err) |
| 11988 | { |
| 11989 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 11990 | file, line, err, &hlua_timeout_session); |
| 11991 | } |
| 11992 | |
| 11993 | 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] | 11994 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 11995 | char **err) |
| 11996 | { |
| 11997 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 11998 | file, line, err, &hlua_timeout_task); |
| 11999 | } |
| 12000 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 12001 | 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] | 12002 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 12003 | char **err) |
| 12004 | { |
| 12005 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 12006 | file, line, err, &hlua_timeout_applet); |
| 12007 | } |
| 12008 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 12009 | 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] | 12010 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 12011 | char **err) |
| 12012 | { |
| 12013 | char *error; |
| 12014 | |
| 12015 | hlua_nb_instruction = strtoll(args[1], &error, 10); |
| 12016 | if (*error != '\0') { |
| 12017 | memprintf(err, "%s: invalid number", args[0]); |
| 12018 | return -1; |
| 12019 | } |
| 12020 | return 0; |
| 12021 | } |
| 12022 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 12023 | 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] | 12024 | const struct proxy *defpx, const char *file, int line, |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 12025 | char **err) |
| 12026 | { |
| 12027 | char *error; |
| 12028 | |
| 12029 | if (*(args[1]) == 0) { |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 12030 | 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] | 12031 | return -1; |
| 12032 | } |
| 12033 | hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L; |
| 12034 | if (*error != '\0') { |
| 12035 | memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error); |
| 12036 | return -1; |
| 12037 | } |
| 12038 | return 0; |
| 12039 | } |
| 12040 | |
| 12041 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 12042 | /* This function is called by the main configuration key "lua-load". It loads and |
| 12043 | * execute an lua file during the parsing of the HAProxy configuration file. It is |
| 12044 | * the main lua entry point. |
| 12045 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 12046 | * This function runs with the HAProxy keywords API. It returns -1 if an error |
| 12047 | * occurs, otherwise it returns 0. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 12048 | * |
| 12049 | * In some error case, LUA set an error message in top of the stack. This function |
| 12050 | * 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] | 12051 | * |
| 12052 | * This function can fail with an abort() due to an Lua critical error. |
| 12053 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 12054 | * tolerated. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 12055 | */ |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 12056 | static int hlua_load_state(char **args, lua_State *L, char **err) |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 12057 | { |
| 12058 | int error; |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 12059 | int nargs; |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 12060 | |
| 12061 | /* Just load and compile the file. */ |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 12062 | error = luaL_loadfile(L, args[0]); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 12063 | if (error) { |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 12064 | 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] | 12065 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 12066 | return -1; |
| 12067 | } |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 12068 | |
| 12069 | /* Push args in the Lua stack, except the first one which is the filename */ |
| 12070 | for (nargs = 1; *(args[nargs]) != 0; nargs++) { |
Aurelien DARRAGON | 4d7aefe | 2022-09-23 10:22:14 +0200 | [diff] [blame] | 12071 | /* Check stack size. */ |
| 12072 | if (!lua_checkstack(L, 1)) { |
| 12073 | memprintf(err, "Lua runtime error while loading arguments: stack is full."); |
| 12074 | return -1; |
| 12075 | } |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 12076 | lua_pushstring(L, args[nargs]); |
| 12077 | } |
| 12078 | nargs--; |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 12079 | |
| 12080 | /* If no syntax error where detected, execute the code. */ |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 12081 | error = lua_pcall(L, nargs, LUA_MULTRET, 0); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 12082 | switch (error) { |
| 12083 | case LUA_OK: |
| 12084 | break; |
| 12085 | case LUA_ERRRUN: |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 12086 | memprintf(err, "Lua runtime error: %s", lua_tostring(L, -1)); |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 12087 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 12088 | return -1; |
| 12089 | case LUA_ERRMEM: |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 12090 | memprintf(err, "Lua out of memory error"); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 12091 | return -1; |
| 12092 | case LUA_ERRERR: |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 12093 | memprintf(err, "Lua message handler error: %s", lua_tostring(L, -1)); |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 12094 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 12095 | return -1; |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 12096 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM <= 503 |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 12097 | case LUA_ERRGCMM: |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 12098 | memprintf(err, "Lua garbage collector error: %s", lua_tostring(L, -1)); |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 12099 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 12100 | return -1; |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 12101 | #endif |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 12102 | default: |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 12103 | memprintf(err, "Lua unknown error: %s", lua_tostring(L, -1)); |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 12104 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 12105 | return -1; |
| 12106 | } |
| 12107 | |
| 12108 | return 0; |
| 12109 | } |
| 12110 | |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 12111 | static int hlua_load(char **args, int section_type, struct proxy *curpx, |
Willy Tarreau | 0182516 | 2021-03-09 09:53:46 +0100 | [diff] [blame] | 12112 | const struct proxy *defpx, const char *file, int line, |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 12113 | char **err) |
| 12114 | { |
| 12115 | if (*(args[1]) == 0) { |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 12116 | memprintf(err, "'%s' expects a file name as parameter.", args[0]); |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 12117 | return -1; |
| 12118 | } |
| 12119 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 12120 | /* loading for global state */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 12121 | hlua_state_id = 0; |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 12122 | ha_set_thread(NULL); |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 12123 | return hlua_load_state(&args[1], hlua_states[0], err); |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 12124 | } |
| 12125 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 12126 | 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] | 12127 | const struct proxy *defpx, const char *file, int line, |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 12128 | char **err) |
| 12129 | { |
| 12130 | int len; |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 12131 | int i; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 12132 | |
| 12133 | if (*(args[1]) == 0) { |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 12134 | memprintf(err, "'%s' expects a file as parameter.", args[0]); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 12135 | return -1; |
| 12136 | } |
| 12137 | |
| 12138 | if (per_thread_load == NULL) { |
| 12139 | /* allocate the first entry large enough to store the final NULL */ |
| 12140 | per_thread_load = calloc(1, sizeof(*per_thread_load)); |
| 12141 | if (per_thread_load == NULL) { |
| 12142 | memprintf(err, "out of memory error"); |
| 12143 | return -1; |
| 12144 | } |
| 12145 | } |
| 12146 | |
| 12147 | /* count used entries */ |
| 12148 | for (len = 0; per_thread_load[len] != NULL; len++) |
| 12149 | ; |
| 12150 | |
| 12151 | per_thread_load = realloc(per_thread_load, (len + 2) * sizeof(*per_thread_load)); |
| 12152 | if (per_thread_load == NULL) { |
| 12153 | memprintf(err, "out of memory error"); |
| 12154 | return -1; |
| 12155 | } |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 12156 | per_thread_load[len + 1] = NULL; |
| 12157 | |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 12158 | /* count args excepting the first, allocate array and copy args */ |
| 12159 | for (i = 0; *(args[i + 1]) != 0; i++); |
Aurelien DARRAGON | b12d169 | 2022-09-23 08:48:34 +0200 | [diff] [blame] | 12160 | per_thread_load[len] = calloc(i + 1, sizeof(*per_thread_load[len])); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 12161 | if (per_thread_load[len] == NULL) { |
| 12162 | memprintf(err, "out of memory error"); |
| 12163 | return -1; |
| 12164 | } |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 12165 | for (i = 1; *(args[i]) != 0; i++) { |
| 12166 | per_thread_load[len][i - 1] = strdup(args[i]); |
| 12167 | if (per_thread_load[len][i - 1] == NULL) { |
| 12168 | memprintf(err, "out of memory error"); |
| 12169 | return -1; |
| 12170 | } |
| 12171 | } |
| 12172 | per_thread_load[len][i - 1] = strdup(""); |
| 12173 | if (per_thread_load[len][i - 1] == NULL) { |
| 12174 | memprintf(err, "out of memory error"); |
| 12175 | return -1; |
| 12176 | } |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 12177 | |
| 12178 | /* loading for thread 1 only */ |
| 12179 | hlua_state_id = 1; |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 12180 | ha_set_thread(NULL); |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 12181 | return hlua_load_state(per_thread_load[len], hlua_states[1], err); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 12182 | } |
| 12183 | |
Tim Duesterhus | c9fc9f2 | 2020-01-12 13:55:39 +0100 | [diff] [blame] | 12184 | /* Prepend the given <path> followed by a semicolon to the `package.<type>` variable |
| 12185 | * in the given <ctx>. |
| 12186 | */ |
Thierry Fournier | 3fb9e51 | 2020-11-28 10:13:12 +0100 | [diff] [blame] | 12187 | static int hlua_prepend_path(lua_State *L, char *type, char *path) |
Tim Duesterhus | c9fc9f2 | 2020-01-12 13:55:39 +0100 | [diff] [blame] | 12188 | { |
Thierry Fournier | 3fb9e51 | 2020-11-28 10:13:12 +0100 | [diff] [blame] | 12189 | lua_getglobal(L, "package"); /* push package variable */ |
| 12190 | lua_pushstring(L, path); /* push given path */ |
| 12191 | lua_pushstring(L, ";"); /* push semicolon */ |
| 12192 | lua_getfield(L, -3, type); /* push old path */ |
| 12193 | lua_concat(L, 3); /* concatenate to new path */ |
| 12194 | lua_setfield(L, -2, type); /* store new path */ |
| 12195 | lua_pop(L, 1); /* pop package variable */ |
Tim Duesterhus | c9fc9f2 | 2020-01-12 13:55:39 +0100 | [diff] [blame] | 12196 | |
| 12197 | return 0; |
| 12198 | } |
| 12199 | |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 12200 | 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] | 12201 | const struct proxy *defpx, const char *file, int line, |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 12202 | char **err) |
| 12203 | { |
| 12204 | char *path; |
| 12205 | char *type = "path"; |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 12206 | struct prepend_path *p = NULL; |
Bertrand Jacquin | 7fbc770 | 2021-11-24 21:16:06 +0000 | [diff] [blame] | 12207 | size_t i; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 12208 | |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 12209 | if (too_many_args(2, args, err, NULL)) { |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 12210 | goto err; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 12211 | } |
| 12212 | |
| 12213 | if (!(*args[1])) { |
| 12214 | memprintf(err, "'%s' expects to receive a <path> as argument", args[0]); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 12215 | goto err; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 12216 | } |
| 12217 | path = args[1]; |
| 12218 | |
| 12219 | if (*args[2]) { |
| 12220 | if (strcmp(args[2], "path") != 0 && strcmp(args[2], "cpath") != 0) { |
| 12221 | 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] | 12222 | goto err; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 12223 | } |
| 12224 | type = args[2]; |
| 12225 | } |
| 12226 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 12227 | p = calloc(1, sizeof(*p)); |
| 12228 | if (p == NULL) { |
Tim Duesterhus | f89d43a | 2021-01-03 20:04:37 +0100 | [diff] [blame] | 12229 | memprintf(err, "memory allocation failed"); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 12230 | goto err; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 12231 | } |
| 12232 | p->path = strdup(path); |
| 12233 | if (p->path == NULL) { |
Tim Duesterhus | f89d43a | 2021-01-03 20:04:37 +0100 | [diff] [blame] | 12234 | memprintf(err, "memory allocation failed"); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 12235 | goto err2; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 12236 | } |
| 12237 | p->type = strdup(type); |
| 12238 | if (p->type == NULL) { |
Tim Duesterhus | f89d43a | 2021-01-03 20:04:37 +0100 | [diff] [blame] | 12239 | memprintf(err, "memory allocation failed"); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 12240 | goto err2; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 12241 | } |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 12242 | LIST_APPEND(&prepend_path_list, &p->l); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 12243 | |
Tim Duesterhus | 9e5e586 | 2021-10-11 18:51:08 +0200 | [diff] [blame] | 12244 | /* Handle the global state and the per-thread state for the first |
| 12245 | * thread. The remaining threads will be initialized based on |
| 12246 | * prepend_path_list. |
| 12247 | */ |
Bertrand Jacquin | 7fbc770 | 2021-11-24 21:16:06 +0000 | [diff] [blame] | 12248 | for (i = 0; i < 2; i++) { |
Tim Duesterhus | 9e5e586 | 2021-10-11 18:51:08 +0200 | [diff] [blame] | 12249 | lua_State *L = hlua_states[i]; |
| 12250 | const char *error; |
| 12251 | |
| 12252 | if (setjmp(safe_ljmp_env) != 0) { |
| 12253 | lua_atpanic(L, hlua_panic_safe); |
| 12254 | if (lua_type(L, -1) == LUA_TSTRING) |
| 12255 | error = lua_tostring(L, -1); |
| 12256 | else |
| 12257 | error = "critical error"; |
| 12258 | fprintf(stderr, "lua-prepend-path: %s.\n", error); |
| 12259 | exit(1); |
| 12260 | } else { |
| 12261 | lua_atpanic(L, hlua_panic_ljmp); |
| 12262 | } |
| 12263 | |
| 12264 | hlua_prepend_path(L, type, path); |
| 12265 | |
| 12266 | lua_atpanic(L, hlua_panic_safe); |
| 12267 | } |
| 12268 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 12269 | return 0; |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 12270 | |
| 12271 | err2: |
| 12272 | free(p->type); |
| 12273 | free(p->path); |
| 12274 | err: |
| 12275 | free(p); |
| 12276 | return -1; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 12277 | } |
| 12278 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 12279 | /* configuration keywords declaration */ |
| 12280 | static struct cfg_kw_list cfg_kws = {{ },{ |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 12281 | { CFG_GLOBAL, "lua-prepend-path", hlua_config_prepend_path }, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 12282 | { CFG_GLOBAL, "lua-load", hlua_load }, |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 12283 | { CFG_GLOBAL, "lua-load-per-thread", hlua_load_per_thread }, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 12284 | { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout }, |
| 12285 | { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout }, |
Thierry FOURNIER | 56da101 | 2015-10-01 08:42:31 +0200 | [diff] [blame] | 12286 | { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout }, |
Aurelien DARRAGON | 58e36e5 | 2023-04-06 22:51:56 +0200 | [diff] [blame^] | 12287 | { CFG_GLOBAL, "tune.lua.burst-timeout", hlua_burst_timeout }, |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 12288 | { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield }, |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 12289 | { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem }, |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 12290 | { 0, NULL, NULL }, |
| 12291 | }}; |
| 12292 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 12293 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |
| 12294 | |
William Lallemand | 5213918 | 2022-03-30 15:05:42 +0200 | [diff] [blame] | 12295 | #ifdef USE_OPENSSL |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 12296 | |
William Lallemand | 30fcca1 | 2022-03-30 12:03:12 +0200 | [diff] [blame] | 12297 | /* |
| 12298 | * This function replace a ckch_store by another one, and rebuild the ckch_inst and all its dependencies. |
| 12299 | * It does the sam as "cli_io_handler_commit_cert" but for lua, the major |
| 12300 | * difference is that the yield in lua and for the CLI is not handled the same |
| 12301 | * way. |
| 12302 | */ |
| 12303 | __LJMP static int hlua_ckch_commit_yield(lua_State *L, int status, lua_KContext ctx) |
| 12304 | { |
| 12305 | struct ckch_inst **lua_ckchi = lua_touserdata(L, -1); |
| 12306 | struct ckch_store **lua_ckchs = lua_touserdata(L, -2); |
| 12307 | struct ckch_inst *ckchi = *lua_ckchi; |
| 12308 | struct ckch_store *old_ckchs = lua_ckchs[0]; |
| 12309 | struct ckch_store *new_ckchs = lua_ckchs[1]; |
| 12310 | struct hlua *hlua; |
| 12311 | char *err = NULL; |
| 12312 | int y = 1; |
| 12313 | |
| 12314 | hlua = hlua_gethlua(L); |
| 12315 | |
| 12316 | /* get the first ckchi to copy */ |
| 12317 | if (ckchi == NULL) |
| 12318 | ckchi = LIST_ELEM(old_ckchs->ckch_inst.n, typeof(ckchi), by_ckchs); |
| 12319 | |
| 12320 | /* walk through the old ckch_inst and creates new ckch_inst using the updated ckchs */ |
| 12321 | list_for_each_entry_from(ckchi, &old_ckchs->ckch_inst, by_ckchs) { |
| 12322 | struct ckch_inst *new_inst; |
| 12323 | |
| 12324 | /* it takes a lot of CPU to creates SSL_CTXs, so we yield every 10 CKCH instances */ |
| 12325 | if (y % 10 == 0) { |
| 12326 | |
| 12327 | *lua_ckchi = ckchi; |
| 12328 | |
| 12329 | task_wakeup(hlua->task, TASK_WOKEN_MSG); |
| 12330 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_ckch_commit_yield, TICK_ETERNITY, 0)); |
| 12331 | } |
| 12332 | |
| 12333 | if (ckch_inst_rebuild(new_ckchs, ckchi, &new_inst, &err)) |
| 12334 | goto error; |
| 12335 | |
| 12336 | /* link the new ckch_inst to the duplicate */ |
| 12337 | LIST_APPEND(&new_ckchs->ckch_inst, &new_inst->by_ckchs); |
| 12338 | y++; |
| 12339 | } |
| 12340 | |
| 12341 | /* The generation is finished, we can insert everything */ |
| 12342 | ckch_store_replace(old_ckchs, new_ckchs); |
| 12343 | |
| 12344 | lua_pop(L, 2); /* pop the lua_ckchs and ckchi */ |
| 12345 | |
| 12346 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 12347 | |
| 12348 | return 0; |
| 12349 | |
| 12350 | error: |
| 12351 | ckch_store_free(new_ckchs); |
| 12352 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 12353 | WILL_LJMP(luaL_error(L, "%s", err)); |
| 12354 | free(err); |
| 12355 | |
| 12356 | return 0; |
| 12357 | } |
| 12358 | |
| 12359 | /* |
| 12360 | * Replace a ckch_store <filename> in the ckchs_tree with a ckch_store created |
| 12361 | * from the table in parameter. |
| 12362 | * |
| 12363 | * This is equivalent to "set ssl cert" + "commit ssl cert" over the CLI, which |
| 12364 | * means it does not need to have a transaction since everything is done in the |
| 12365 | * same function. |
| 12366 | * |
| 12367 | * CertCache.set{filename="", crt="", key="", sctl="", ocsp="", issuer=""} |
| 12368 | * |
| 12369 | */ |
| 12370 | __LJMP static int hlua_ckch_set(lua_State *L) |
| 12371 | { |
| 12372 | struct hlua *hlua; |
| 12373 | struct ckch_inst **lua_ckchi; |
| 12374 | struct ckch_store **lua_ckchs; |
| 12375 | struct ckch_store *old_ckchs = NULL; |
| 12376 | struct ckch_store *new_ckchs = NULL; |
| 12377 | int errcode = 0; |
| 12378 | char *err = NULL; |
| 12379 | struct cert_exts *cert_ext = NULL; |
| 12380 | char *filename; |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 12381 | struct ckch_data *data; |
William Lallemand | 30fcca1 | 2022-03-30 12:03:12 +0200 | [diff] [blame] | 12382 | int ret; |
| 12383 | |
| 12384 | if (lua_type(L, -1) != LUA_TTABLE) |
| 12385 | WILL_LJMP(luaL_error(L, "'CertCache.set' needs a table as argument")); |
| 12386 | |
| 12387 | hlua = hlua_gethlua(L); |
| 12388 | |
| 12389 | /* FIXME: this should not return an error but should come back later */ |
| 12390 | if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock)) |
| 12391 | WILL_LJMP(luaL_error(L, "CertCache already under lock")); |
| 12392 | |
| 12393 | ret = lua_getfield(L, -1, "filename"); |
| 12394 | if (ret != LUA_TSTRING) { |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 12395 | memprintf(&err, "%sNo filename specified!", err ? err : ""); |
William Lallemand | 30fcca1 | 2022-03-30 12:03:12 +0200 | [diff] [blame] | 12396 | errcode |= ERR_ALERT | ERR_FATAL; |
| 12397 | goto end; |
| 12398 | } |
| 12399 | filename = (char *)lua_tostring(L, -1); |
| 12400 | |
| 12401 | |
| 12402 | /* look for the filename in the tree */ |
| 12403 | old_ckchs = ckchs_lookup(filename); |
| 12404 | if (!old_ckchs) { |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 12405 | 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] | 12406 | errcode |= ERR_ALERT | ERR_FATAL; |
| 12407 | goto end; |
| 12408 | } |
| 12409 | /* TODO: handle extra_files_noext */ |
| 12410 | |
| 12411 | new_ckchs = ckchs_dup(old_ckchs); |
| 12412 | if (!new_ckchs) { |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 12413 | memprintf(&err, "%sCannot allocate memory!", err ? err : ""); |
William Lallemand | 30fcca1 | 2022-03-30 12:03:12 +0200 | [diff] [blame] | 12414 | errcode |= ERR_ALERT | ERR_FATAL; |
| 12415 | goto end; |
| 12416 | } |
| 12417 | |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 12418 | data = new_ckchs->data; |
William Lallemand | 30fcca1 | 2022-03-30 12:03:12 +0200 | [diff] [blame] | 12419 | |
| 12420 | /* loop on the field in the table, which have the same name as the |
| 12421 | * possible extensions of files */ |
| 12422 | lua_pushnil(L); |
| 12423 | while (lua_next(L, 1)) { |
| 12424 | int i; |
| 12425 | const char *field = lua_tostring(L, -2); |
| 12426 | char *payload = (char *)lua_tostring(L, -1); |
| 12427 | |
| 12428 | if (!field || strcmp(field, "filename") == 0) { |
| 12429 | lua_pop(L, 1); |
| 12430 | continue; |
| 12431 | } |
| 12432 | |
| 12433 | for (i = 0; field && cert_exts[i].ext != NULL; i++) { |
| 12434 | if (strcmp(field, cert_exts[i].ext) == 0) { |
| 12435 | cert_ext = &cert_exts[i]; |
| 12436 | break; |
| 12437 | } |
| 12438 | } |
| 12439 | |
| 12440 | /* this is the default type, the field is not supported */ |
| 12441 | if (cert_ext == NULL) { |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 12442 | memprintf(&err, "%sUnsupported field '%s'", err ? err : "", field); |
William Lallemand | 30fcca1 | 2022-03-30 12:03:12 +0200 | [diff] [blame] | 12443 | errcode |= ERR_ALERT | ERR_FATAL; |
| 12444 | goto end; |
| 12445 | } |
| 12446 | |
| 12447 | /* appply the change on the duplicate */ |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 12448 | if (cert_ext->load(filename, payload, data, &err) != 0) { |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 12449 | 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] | 12450 | errcode |= ERR_ALERT | ERR_FATAL; |
| 12451 | goto end; |
| 12452 | } |
| 12453 | lua_pop(L, 1); |
| 12454 | } |
| 12455 | |
| 12456 | /* store the pointers on the lua stack */ |
| 12457 | lua_ckchs = lua_newuserdata(L, sizeof(struct ckch_store *) * 2); |
| 12458 | lua_ckchs[0] = old_ckchs; |
| 12459 | lua_ckchs[1] = new_ckchs; |
| 12460 | lua_ckchi = lua_newuserdata(L, sizeof(struct ckch_inst *)); |
| 12461 | *lua_ckchi = NULL; |
| 12462 | |
| 12463 | task_wakeup(hlua->task, TASK_WOKEN_MSG); |
| 12464 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_ckch_commit_yield, TICK_ETERNITY, 0)); |
| 12465 | |
| 12466 | end: |
| 12467 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 12468 | |
| 12469 | if (errcode & ERR_CODE) { |
| 12470 | ckch_store_free(new_ckchs); |
| 12471 | WILL_LJMP(luaL_error(L, "%s", err)); |
| 12472 | } |
| 12473 | free(err); |
| 12474 | |
| 12475 | return 0; |
| 12476 | } |
| 12477 | |
William Lallemand | 5213918 | 2022-03-30 15:05:42 +0200 | [diff] [blame] | 12478 | #else |
| 12479 | |
| 12480 | __LJMP static int hlua_ckch_set(lua_State *L) |
| 12481 | { |
| 12482 | WILL_LJMP(luaL_error(L, "'CertCache.set' needs an HAProxy built with OpenSSL")); |
| 12483 | |
| 12484 | return 0; |
| 12485 | } |
| 12486 | #endif /* ! USE_OPENSSL */ |
| 12487 | |
| 12488 | |
| 12489 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 12490 | /* This function can fail with an abort() due to an Lua critical error. |
| 12491 | * We are in the initialisation process of HAProxy, this abort() is |
| 12492 | * tolerated. |
| 12493 | */ |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 12494 | int hlua_post_init_state(lua_State *L) |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 12495 | { |
| 12496 | struct hlua_init_function *init; |
| 12497 | const char *msg; |
| 12498 | enum hlua_exec ret; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 12499 | const char *error; |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 12500 | const char *kind; |
| 12501 | const char *trace; |
| 12502 | int return_status = 1; |
| 12503 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
| 12504 | int nres; |
| 12505 | #endif |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 12506 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 12507 | /* disable memory limit checks if limit is not set */ |
| 12508 | if (!hlua_global_allocator.limit) |
| 12509 | hlua_global_allocator.limit = ~hlua_global_allocator.limit; |
| 12510 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 12511 | /* Call post initialisation function in safe environment. */ |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 12512 | if (setjmp(safe_ljmp_env) != 0) { |
| 12513 | lua_atpanic(L, hlua_panic_safe); |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 12514 | if (lua_type(L, -1) == LUA_TSTRING) |
| 12515 | error = lua_tostring(L, -1); |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 12516 | else |
| 12517 | error = "critical error"; |
| 12518 | fprintf(stderr, "Lua post-init: %s.\n", error); |
| 12519 | exit(1); |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 12520 | } else { |
| 12521 | lua_atpanic(L, hlua_panic_ljmp); |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 12522 | } |
Frédéric Lécaille | 54f2bcf | 2018-08-29 13:46:24 +0200 | [diff] [blame] | 12523 | |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 12524 | list_for_each_entry(init, &hlua_init_functions[hlua_state_id], l) { |
Aurelien DARRAGON | 4fdf8b5 | 2023-03-20 17:22:37 +0100 | [diff] [blame] | 12525 | hlua_pushref(L, init->function_ref); |
Aurelien DARRAGON | 16d047b | 2023-03-20 16:29:55 +0100 | [diff] [blame] | 12526 | /* function ref should be released right away since it was pushed |
| 12527 | * on the stack and will not be used anymore |
| 12528 | */ |
| 12529 | hlua_unref(L, init->function_ref); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 12530 | |
| 12531 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 12532 | ret = lua_resume(L, L, 0, &nres); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 12533 | #else |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 12534 | ret = lua_resume(L, L, 0); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 12535 | #endif |
| 12536 | kind = NULL; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 12537 | switch (ret) { |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 12538 | |
| 12539 | case LUA_OK: |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 12540 | lua_pop(L, -1); |
Thierry Fournier | 13d08b7 | 2020-11-28 11:02:58 +0100 | [diff] [blame] | 12541 | break; |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 12542 | |
| 12543 | case LUA_ERRERR: |
| 12544 | kind = "message handler error"; |
Willy Tarreau | 14de395 | 2022-11-14 07:08:28 +0100 | [diff] [blame] | 12545 | __fallthrough; |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 12546 | case LUA_ERRRUN: |
| 12547 | if (!kind) |
| 12548 | kind = "runtime error"; |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 12549 | msg = lua_tostring(L, -1); |
| 12550 | lua_settop(L, 0); /* Empty the stack. */ |
| 12551 | lua_pop(L, 1); |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 12552 | trace = hlua_traceback(L, ", "); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 12553 | if (msg) |
| 12554 | ha_alert("Lua init: %s: '%s' from %s\n", kind, msg, trace); |
| 12555 | else |
| 12556 | ha_alert("Lua init: unknown %s from %s\n", kind, trace); |
| 12557 | return_status = 0; |
| 12558 | break; |
| 12559 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 12560 | default: |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 12561 | /* Unknown error */ |
| 12562 | kind = "Unknown error"; |
Willy Tarreau | 14de395 | 2022-11-14 07:08:28 +0100 | [diff] [blame] | 12563 | __fallthrough; |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 12564 | case LUA_YIELD: |
| 12565 | /* yield is not configured at this step, this state doesn't happen */ |
| 12566 | if (!kind) |
| 12567 | kind = "yield not allowed"; |
Willy Tarreau | 14de395 | 2022-11-14 07:08:28 +0100 | [diff] [blame] | 12568 | __fallthrough; |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 12569 | case LUA_ERRMEM: |
| 12570 | if (!kind) |
| 12571 | kind = "out of memory error"; |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 12572 | lua_settop(L, 0); |
| 12573 | lua_pop(L, 1); |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 12574 | trace = hlua_traceback(L, ", "); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 12575 | ha_alert("Lua init: %s: %s\n", kind, trace); |
| 12576 | return_status = 0; |
| 12577 | break; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 12578 | } |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 12579 | if (!return_status) |
| 12580 | break; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 12581 | } |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 12582 | |
| 12583 | lua_atpanic(L, hlua_panic_safe); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 12584 | return return_status; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 12585 | } |
| 12586 | |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 12587 | int hlua_post_init() |
| 12588 | { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 12589 | int ret; |
| 12590 | int i; |
| 12591 | int errors; |
| 12592 | char *err = NULL; |
| 12593 | struct hlua_function *fcn; |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 12594 | struct hlua_reg_filter *reg_flt; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 12595 | |
Willy Tarreau | b131049 | 2021-08-30 09:35:18 +0200 | [diff] [blame] | 12596 | #if defined(USE_OPENSSL) |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 12597 | /* Initialize SSL server. */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 12598 | if (socket_ssl->xprt->prepare_srv) { |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 12599 | int saved_used_backed = global.ssl_used_backend; |
| 12600 | // don't affect maxconn automatic computation |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 12601 | socket_ssl->xprt->prepare_srv(socket_ssl); |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 12602 | global.ssl_used_backend = saved_used_backed; |
| 12603 | } |
| 12604 | #endif |
| 12605 | |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 12606 | /* Perform post init of common thread */ |
| 12607 | hlua_state_id = 0; |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 12608 | ha_set_thread(&ha_thread_info[0]); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 12609 | ret = hlua_post_init_state(hlua_states[hlua_state_id]); |
| 12610 | if (ret == 0) |
| 12611 | return 0; |
| 12612 | |
| 12613 | /* init remaining lua states and load files */ |
| 12614 | for (hlua_state_id = 2; hlua_state_id < global.nbthread + 1; hlua_state_id++) { |
| 12615 | |
| 12616 | /* set thread context */ |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 12617 | ha_set_thread(&ha_thread_info[hlua_state_id - 1]); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 12618 | |
| 12619 | /* Init lua state */ |
| 12620 | hlua_states[hlua_state_id] = hlua_init_state(hlua_state_id); |
| 12621 | |
| 12622 | /* Load lua files */ |
| 12623 | for (i = 0; per_thread_load && per_thread_load[i]; i++) { |
| 12624 | ret = hlua_load_state(per_thread_load[i], hlua_states[hlua_state_id], &err); |
| 12625 | if (ret != 0) { |
| 12626 | ha_alert("Lua init: %s\n", err); |
| 12627 | return 0; |
| 12628 | } |
| 12629 | } |
| 12630 | } |
| 12631 | |
| 12632 | /* Reset thread context */ |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 12633 | ha_set_thread(NULL); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 12634 | |
| 12635 | /* Execute post init for all states */ |
| 12636 | for (hlua_state_id = 1; hlua_state_id < global.nbthread + 1; hlua_state_id++) { |
| 12637 | |
| 12638 | /* set thread context */ |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 12639 | ha_set_thread(&ha_thread_info[hlua_state_id - 1]); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 12640 | |
| 12641 | /* run post init */ |
| 12642 | ret = hlua_post_init_state(hlua_states[hlua_state_id]); |
| 12643 | if (ret == 0) |
| 12644 | return 0; |
| 12645 | } |
| 12646 | |
| 12647 | /* Reset thread context */ |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 12648 | ha_set_thread(NULL); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 12649 | |
| 12650 | /* control functions registering. Each function must have: |
| 12651 | * - only the function_ref[0] set positive and all other to -1 |
| 12652 | * - only the function_ref[0] set to -1 and all other positive |
| 12653 | * This ensure a same reference is not used both in shared |
| 12654 | * lua state and thread dedicated lua state. Note: is the case |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 12655 | * reach, the shared state is priority, but the bug will be |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 12656 | * complicated to found for the end user. |
| 12657 | */ |
| 12658 | errors = 0; |
| 12659 | list_for_each_entry(fcn, &referenced_functions, l) { |
| 12660 | ret = 0; |
| 12661 | for (i = 1; i < global.nbthread + 1; i++) { |
| 12662 | if (fcn->function_ref[i] == -1) |
| 12663 | ret--; |
| 12664 | else |
| 12665 | ret++; |
| 12666 | } |
| 12667 | if (abs(ret) != global.nbthread) { |
| 12668 | ha_alert("Lua function '%s' is not referenced in all thread. " |
| 12669 | "Expect function in all thread or in none thread.\n", fcn->name); |
| 12670 | errors++; |
| 12671 | continue; |
| 12672 | } |
| 12673 | |
| 12674 | if ((fcn->function_ref[0] == -1) == (ret < 0)) { |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 12675 | ha_alert("Lua function '%s' is referenced both ins shared Lua context (through lua-load) " |
| 12676 | "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] | 12677 | "exclusive.\n", fcn->name); |
| 12678 | errors++; |
| 12679 | } |
| 12680 | } |
| 12681 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 12682 | /* Do the same with registered filters */ |
| 12683 | list_for_each_entry(reg_flt, &referenced_filters, l) { |
| 12684 | ret = 0; |
| 12685 | for (i = 1; i < global.nbthread + 1; i++) { |
| 12686 | if (reg_flt->flt_ref[i] == -1) |
| 12687 | ret--; |
| 12688 | else |
| 12689 | ret++; |
| 12690 | } |
| 12691 | if (abs(ret) != global.nbthread) { |
| 12692 | ha_alert("Lua filter '%s' is not referenced in all thread. " |
| 12693 | "Expect function in all thread or in none thread.\n", reg_flt->name); |
| 12694 | errors++; |
| 12695 | continue; |
| 12696 | } |
| 12697 | |
| 12698 | if ((reg_flt->flt_ref[0] == -1) == (ret < 0)) { |
| 12699 | ha_alert("Lua filter '%s' is referenced both ins shared Lua context (through lua-load) " |
| 12700 | "and per-thread Lua context (through lua-load-per-thread). these two context " |
| 12701 | "exclusive.\n", fcn->name); |
| 12702 | errors++; |
| 12703 | } |
| 12704 | } |
| 12705 | |
| 12706 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 12707 | if (errors > 0) |
| 12708 | return 0; |
| 12709 | |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 12710 | /* 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] | 12711 | * -1 in order to have probably a segfault if someone use it |
| 12712 | */ |
| 12713 | hlua_state_id = -1; |
| 12714 | |
| 12715 | return 1; |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 12716 | } |
| 12717 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 12718 | /* The memory allocator used by the Lua stack. <ud> is a pointer to the |
| 12719 | * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize> |
| 12720 | * 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] | 12721 | * allocation. <nsize> is the requested new size. A new allocation is |
| 12722 | * indicated by <ptr> being NULL. A free is indicated by <nsize> being |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 12723 | * zero. This one verifies that the limits are respected but is optimized |
| 12724 | * 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] | 12725 | * |
| 12726 | * Warning: while this API ressembles glibc's realloc() a lot, glibc surpasses |
| 12727 | * POSIX by making realloc(ptr,0) an effective free(), but others do not do |
| 12728 | * that and will simply allocate zero as if it were the result of malloc(0), |
| 12729 | * so mapping this onto realloc() will lead to memory leaks on non-glibc |
| 12730 | * systems. |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 12731 | */ |
| 12732 | static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize) |
| 12733 | { |
| 12734 | struct hlua_mem_allocator *zone = ud; |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 12735 | size_t limit, old, new; |
| 12736 | |
| 12737 | /* a limit of ~0 means unlimited and boot complete, so there's no need |
| 12738 | * for accounting anymore. |
| 12739 | */ |
Willy Tarreau | a5efdff | 2021-10-22 16:00:02 +0200 | [diff] [blame] | 12740 | if (likely(~zone->limit == 0)) { |
| 12741 | if (!nsize) |
| 12742 | ha_free(&ptr); |
| 12743 | else |
| 12744 | ptr = realloc(ptr, nsize); |
| 12745 | return ptr; |
| 12746 | } |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 12747 | |
Willy Tarreau | d36c7fa | 2020-12-02 12:26:29 +0100 | [diff] [blame] | 12748 | if (!ptr) |
| 12749 | osize = 0; |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 12750 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 12751 | /* enforce strict limits across all threads */ |
| 12752 | limit = zone->limit; |
| 12753 | old = _HA_ATOMIC_LOAD(&zone->allocated); |
| 12754 | do { |
| 12755 | new = old + nsize - osize; |
| 12756 | if (unlikely(nsize && limit && new > limit)) |
| 12757 | return NULL; |
| 12758 | } while (!_HA_ATOMIC_CAS(&zone->allocated, &old, new)); |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 12759 | |
Willy Tarreau | a5efdff | 2021-10-22 16:00:02 +0200 | [diff] [blame] | 12760 | if (!nsize) |
| 12761 | ha_free(&ptr); |
| 12762 | else |
| 12763 | ptr = realloc(ptr, nsize); |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 12764 | |
| 12765 | if (unlikely(!ptr && nsize)) // failed |
| 12766 | _HA_ATOMIC_SUB(&zone->allocated, nsize - osize); |
| 12767 | |
| 12768 | __ha_barrier_atomic_store(); |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 12769 | return ptr; |
| 12770 | } |
| 12771 | |
Thierry Fournier | ecb83c2 | 2020-11-28 15:49:44 +0100 | [diff] [blame] | 12772 | /* 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] | 12773 | * We are in the initialisation process of HAProxy, this abort() is |
| 12774 | * tolerated. |
| 12775 | */ |
Thierry Fournier | ecb83c2 | 2020-11-28 15:49:44 +0100 | [diff] [blame] | 12776 | lua_State *hlua_init_state(int thread_num) |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 12777 | { |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 12778 | int i; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 12779 | int idx; |
| 12780 | struct sample_fetch *sf; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 12781 | struct sample_conv *sc; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 12782 | char *p; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 12783 | const char *error_msg; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 12784 | void **context; |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12785 | lua_State *L; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 12786 | struct prepend_path *pp; |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 12787 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 12788 | /* Init main lua stack. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12789 | L = lua_newstate(hlua_alloc, &hlua_global_allocator); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 12790 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 12791 | /* Initialise Lua context to NULL */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12792 | context = lua_getextraspace(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 12793 | *context = NULL; |
| 12794 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 12795 | /* From this point, until the end of the initialisation function, |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 12796 | * the Lua function can fail with an abort. We are in the initialisation |
| 12797 | * process of HAProxy, this abort() is tolerated. |
| 12798 | */ |
| 12799 | |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 12800 | /* Call post initialisation function in safe environment. */ |
| 12801 | if (setjmp(safe_ljmp_env) != 0) { |
| 12802 | lua_atpanic(L, hlua_panic_safe); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12803 | if (lua_type(L, -1) == LUA_TSTRING) |
| 12804 | error_msg = lua_tostring(L, -1); |
Thierry Fournier | 2f05cc6 | 2020-11-28 16:08:02 +0100 | [diff] [blame] | 12805 | else |
| 12806 | error_msg = "critical error"; |
| 12807 | fprintf(stderr, "Lua init: %s.\n", error_msg); |
| 12808 | exit(1); |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 12809 | } else { |
| 12810 | lua_atpanic(L, hlua_panic_ljmp); |
Thierry Fournier | 2f05cc6 | 2020-11-28 16:08:02 +0100 | [diff] [blame] | 12811 | } |
| 12812 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 12813 | /* Initialise lua. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12814 | luaL_openlibs(L); |
Tim Duesterhus | 541fe1e | 2020-01-12 13:55:41 +0100 | [diff] [blame] | 12815 | #define HLUA_PREPEND_PATH_TOSTRING1(x) #x |
| 12816 | #define HLUA_PREPEND_PATH_TOSTRING(x) HLUA_PREPEND_PATH_TOSTRING1(x) |
| 12817 | #ifdef HLUA_PREPEND_PATH |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12818 | hlua_prepend_path(L, "path", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_PATH)); |
Tim Duesterhus | 541fe1e | 2020-01-12 13:55:41 +0100 | [diff] [blame] | 12819 | #endif |
| 12820 | #ifdef HLUA_PREPEND_CPATH |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12821 | hlua_prepend_path(L, "cpath", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_CPATH)); |
Tim Duesterhus | 541fe1e | 2020-01-12 13:55:41 +0100 | [diff] [blame] | 12822 | #endif |
| 12823 | #undef HLUA_PREPEND_PATH_TOSTRING |
| 12824 | #undef HLUA_PREPEND_PATH_TOSTRING1 |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 12825 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 12826 | /* Apply configured prepend path */ |
| 12827 | list_for_each_entry(pp, &prepend_path_list, l) |
| 12828 | hlua_prepend_path(L, pp->type, pp->path); |
| 12829 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 12830 | /* |
| 12831 | * |
| 12832 | * Create "core" object. |
| 12833 | * |
| 12834 | */ |
| 12835 | |
Thierry FOURNIER | a2d8c65 | 2015-03-11 17:29:39 +0100 | [diff] [blame] | 12836 | /* This table entry is the object "core" base. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12837 | lua_newtable(L); |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 12838 | |
Thierry Fournier | ecb83c2 | 2020-11-28 15:49:44 +0100 | [diff] [blame] | 12839 | /* set the thread id */ |
| 12840 | hlua_class_const_int(L, "thread", thread_num); |
| 12841 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 12842 | /* Push the loglevel constants. */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 12843 | for (i = 0; i < NB_LOG_LEVELS; i++) |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12844 | hlua_class_const_int(L, log_levels[i], i); |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 12845 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 12846 | /* Register special functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12847 | hlua_class_function(L, "register_init", hlua_register_init); |
| 12848 | hlua_class_function(L, "register_task", hlua_register_task); |
| 12849 | hlua_class_function(L, "register_fetches", hlua_register_fetches); |
| 12850 | hlua_class_function(L, "register_converters", hlua_register_converters); |
| 12851 | hlua_class_function(L, "register_action", hlua_register_action); |
| 12852 | hlua_class_function(L, "register_service", hlua_register_service); |
| 12853 | hlua_class_function(L, "register_cli", hlua_register_cli); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 12854 | hlua_class_function(L, "register_filter", hlua_register_filter); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12855 | hlua_class_function(L, "yield", hlua_yield); |
| 12856 | hlua_class_function(L, "set_nice", hlua_set_nice); |
| 12857 | hlua_class_function(L, "sleep", hlua_sleep); |
| 12858 | hlua_class_function(L, "msleep", hlua_msleep); |
| 12859 | hlua_class_function(L, "add_acl", hlua_add_acl); |
| 12860 | hlua_class_function(L, "del_acl", hlua_del_acl); |
| 12861 | hlua_class_function(L, "set_map", hlua_set_map); |
| 12862 | hlua_class_function(L, "del_map", hlua_del_map); |
| 12863 | hlua_class_function(L, "tcp", hlua_socket_new); |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 12864 | hlua_class_function(L, "httpclient", hlua_httpclient_new); |
Aurelien DARRAGON | c84899c | 2023-02-20 18:18:59 +0100 | [diff] [blame] | 12865 | hlua_class_function(L, "event_sub", hlua_event_global_sub); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12866 | hlua_class_function(L, "log", hlua_log); |
| 12867 | hlua_class_function(L, "Debug", hlua_log_debug); |
| 12868 | hlua_class_function(L, "Info", hlua_log_info); |
| 12869 | hlua_class_function(L, "Warning", hlua_log_warning); |
| 12870 | hlua_class_function(L, "Alert", hlua_log_alert); |
| 12871 | hlua_class_function(L, "done", hlua_done); |
| 12872 | hlua_fcn_reg_core_fcn(L); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 12873 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12874 | lua_setglobal(L, "core"); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 12875 | |
| 12876 | /* |
| 12877 | * |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 12878 | * Create "act" object. |
| 12879 | * |
| 12880 | */ |
| 12881 | |
| 12882 | /* This table entry is the object "act" base. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12883 | lua_newtable(L); |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 12884 | |
| 12885 | /* push action return constants */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12886 | hlua_class_const_int(L, "CONTINUE", ACT_RET_CONT); |
| 12887 | hlua_class_const_int(L, "STOP", ACT_RET_STOP); |
| 12888 | hlua_class_const_int(L, "YIELD", ACT_RET_YIELD); |
| 12889 | hlua_class_const_int(L, "ERROR", ACT_RET_ERR); |
| 12890 | hlua_class_const_int(L, "DONE", ACT_RET_DONE); |
| 12891 | hlua_class_const_int(L, "DENY", ACT_RET_DENY); |
| 12892 | hlua_class_const_int(L, "ABORT", ACT_RET_ABRT); |
| 12893 | hlua_class_const_int(L, "INVALID", ACT_RET_INV); |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 12894 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12895 | hlua_class_function(L, "wake_time", hlua_set_wake_time); |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 12896 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12897 | lua_setglobal(L, "act"); |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 12898 | |
| 12899 | /* |
| 12900 | * |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 12901 | * Create "Filter" object. |
| 12902 | * |
| 12903 | */ |
| 12904 | |
| 12905 | /* This table entry is the object "filter" base. */ |
| 12906 | lua_newtable(L); |
| 12907 | |
| 12908 | /* push flags and constants */ |
| 12909 | hlua_class_const_int(L, "CONTINUE", 1); |
| 12910 | hlua_class_const_int(L, "WAIT", 0); |
| 12911 | hlua_class_const_int(L, "ERROR", -1); |
| 12912 | |
| 12913 | hlua_class_const_int(L, "FLT_CFG_FL_HTX", FLT_CFG_FL_HTX); |
| 12914 | |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 12915 | hlua_class_function(L, "wake_time", hlua_set_wake_time); |
| 12916 | hlua_class_function(L, "register_data_filter", hlua_register_data_filter); |
| 12917 | hlua_class_function(L, "unregister_data_filter", hlua_unregister_data_filter); |
| 12918 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 12919 | lua_setglobal(L, "filter"); |
| 12920 | |
| 12921 | /* |
| 12922 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12923 | * Register class Map |
| 12924 | * |
| 12925 | */ |
| 12926 | |
| 12927 | /* This table entry is the object "Map" base. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12928 | lua_newtable(L); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12929 | |
| 12930 | /* register pattern types. */ |
| 12931 | for (i=0; i<PAT_MATCH_NUM; i++) |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12932 | hlua_class_const_int(L, pat_match_names[i], i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 12933 | for (i=0; i<PAT_MATCH_NUM; i++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 12934 | snprintf(trash.area, trash.size, "_%s", pat_match_names[i]); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12935 | hlua_class_const_int(L, trash.area, i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 12936 | } |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12937 | |
| 12938 | /* register constructor. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12939 | hlua_class_function(L, "new", hlua_map_new); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12940 | |
| 12941 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12942 | lua_newtable(L); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12943 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 12944 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12945 | lua_pushstring(L, "__index"); |
| 12946 | lua_newtable(L); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12947 | |
| 12948 | /* Register . */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12949 | hlua_class_function(L, "lookup", hlua_map_lookup); |
| 12950 | hlua_class_function(L, "slookup", hlua_map_slookup); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12951 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12952 | lua_rawset(L, -3); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12953 | |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 12954 | /* Register previous table in the registry with reference and named entry. |
| 12955 | * The function hlua_register_metatable() pops the stack, so we |
| 12956 | * previously create a copy of the table. |
| 12957 | */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12958 | lua_pushvalue(L, -1); /* Copy the -1 entry and push it on the stack. */ |
| 12959 | class_map_ref = hlua_register_metatable(L, CLASS_MAP); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12960 | |
| 12961 | /* Assign the metatable to the mai Map object. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12962 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12963 | |
| 12964 | /* Set a name to the table. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12965 | lua_setglobal(L, "Map"); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12966 | |
| 12967 | /* |
| 12968 | * |
William Lallemand | 30fcca1 | 2022-03-30 12:03:12 +0200 | [diff] [blame] | 12969 | * Register "CertCache" class |
| 12970 | * |
| 12971 | */ |
| 12972 | |
| 12973 | /* Create and fill the metatable. */ |
| 12974 | lua_newtable(L); |
| 12975 | /* Register */ |
| 12976 | hlua_class_function(L, "set", hlua_ckch_set); |
| 12977 | lua_setglobal(L, CLASS_CERTCACHE); /* Create global object called Regex */ |
| 12978 | |
| 12979 | /* |
| 12980 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 12981 | * Register class Channel |
| 12982 | * |
| 12983 | */ |
| 12984 | |
| 12985 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12986 | lua_newtable(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 12987 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 12988 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12989 | lua_pushstring(L, "__index"); |
| 12990 | lua_newtable(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 12991 | |
| 12992 | /* Register . */ |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 12993 | hlua_class_function(L, "data", hlua_channel_get_data); |
| 12994 | hlua_class_function(L, "line", hlua_channel_get_line); |
| 12995 | hlua_class_function(L, "set", hlua_channel_set_data); |
| 12996 | hlua_class_function(L, "remove", hlua_channel_del_data); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12997 | hlua_class_function(L, "append", hlua_channel_append); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 12998 | hlua_class_function(L, "prepend", hlua_channel_prepend); |
| 12999 | hlua_class_function(L, "insert", hlua_channel_insert_data); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13000 | hlua_class_function(L, "send", hlua_channel_send); |
| 13001 | hlua_class_function(L, "forward", hlua_channel_forward); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 13002 | hlua_class_function(L, "input", hlua_channel_get_in_len); |
| 13003 | hlua_class_function(L, "output", hlua_channel_get_out_len); |
| 13004 | hlua_class_function(L, "may_recv", hlua_channel_may_recv); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13005 | hlua_class_function(L, "is_full", hlua_channel_is_full); |
| 13006 | hlua_class_function(L, "is_resp", hlua_channel_is_resp); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 13007 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 13008 | /* Deprecated API */ |
| 13009 | hlua_class_function(L, "get", hlua_channel_get); |
| 13010 | hlua_class_function(L, "dup", hlua_channel_dup); |
| 13011 | hlua_class_function(L, "getline", hlua_channel_getline); |
| 13012 | hlua_class_function(L, "get_in_len", hlua_channel_get_in_len); |
| 13013 | hlua_class_function(L, "get_out_len", hlua_channel_get_out_len); |
| 13014 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13015 | lua_rawset(L, -3); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 13016 | |
| 13017 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13018 | class_channel_ref = hlua_register_metatable(L, CLASS_CHANNEL); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 13019 | |
| 13020 | /* |
| 13021 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 13022 | * Register class Fetches |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 13023 | * |
| 13024 | */ |
| 13025 | |
| 13026 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13027 | lua_newtable(L); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 13028 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 13029 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13030 | lua_pushstring(L, "__index"); |
| 13031 | lua_newtable(L); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 13032 | |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 13033 | /* Browse existing fetches and create the associated |
| 13034 | * object method. |
| 13035 | */ |
| 13036 | sf = NULL; |
| 13037 | while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) { |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 13038 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 13039 | * by an underscore. |
| 13040 | */ |
Willy Tarreau | 5ef9656 | 2021-08-26 16:48:53 +0200 | [diff] [blame] | 13041 | strlcpy2(trash.area, sf->kw, trash.size); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 13042 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 13043 | if (*p == '.' || *p == '-' || *p == '+') |
| 13044 | *p = '_'; |
| 13045 | |
| 13046 | /* Register the function. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13047 | lua_pushstring(L, trash.area); |
| 13048 | lua_pushlightuserdata(L, sf); |
| 13049 | lua_pushcclosure(L, hlua_run_sample_fetch, 1); |
| 13050 | lua_rawset(L, -3); |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 13051 | } |
| 13052 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13053 | lua_rawset(L, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 13054 | |
| 13055 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13056 | class_fetches_ref = hlua_register_metatable(L, CLASS_FETCHES); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 13057 | |
| 13058 | /* |
| 13059 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 13060 | * Register class Converters |
| 13061 | * |
| 13062 | */ |
| 13063 | |
| 13064 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13065 | lua_newtable(L); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 13066 | |
| 13067 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13068 | lua_pushstring(L, "__index"); |
| 13069 | lua_newtable(L); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 13070 | |
| 13071 | /* Browse existing converters and create the associated |
| 13072 | * object method. |
| 13073 | */ |
| 13074 | sc = NULL; |
| 13075 | while ((sc = sample_conv_getnext(sc, &idx)) != NULL) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 13076 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 13077 | * by an underscore. |
| 13078 | */ |
Willy Tarreau | 5ef9656 | 2021-08-26 16:48:53 +0200 | [diff] [blame] | 13079 | strlcpy2(trash.area, sc->kw, trash.size); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 13080 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 13081 | if (*p == '.' || *p == '-' || *p == '+') |
| 13082 | *p = '_'; |
| 13083 | |
| 13084 | /* Register the function. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13085 | lua_pushstring(L, trash.area); |
| 13086 | lua_pushlightuserdata(L, sc); |
| 13087 | lua_pushcclosure(L, hlua_run_sample_conv, 1); |
| 13088 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 13089 | } |
| 13090 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13091 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 13092 | |
| 13093 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13094 | class_converters_ref = hlua_register_metatable(L, CLASS_CONVERTERS); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 13095 | |
| 13096 | /* |
| 13097 | * |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 13098 | * Register class HTTP |
| 13099 | * |
| 13100 | */ |
| 13101 | |
| 13102 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13103 | lua_newtable(L); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 13104 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 13105 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13106 | lua_pushstring(L, "__index"); |
| 13107 | lua_newtable(L); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 13108 | |
| 13109 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13110 | hlua_class_function(L, "req_get_headers",hlua_http_req_get_headers); |
| 13111 | hlua_class_function(L, "req_del_header", hlua_http_req_del_hdr); |
| 13112 | hlua_class_function(L, "req_rep_header", hlua_http_req_rep_hdr); |
| 13113 | hlua_class_function(L, "req_rep_value", hlua_http_req_rep_val); |
| 13114 | hlua_class_function(L, "req_add_header", hlua_http_req_add_hdr); |
| 13115 | hlua_class_function(L, "req_set_header", hlua_http_req_set_hdr); |
| 13116 | hlua_class_function(L, "req_set_method", hlua_http_req_set_meth); |
| 13117 | hlua_class_function(L, "req_set_path", hlua_http_req_set_path); |
| 13118 | hlua_class_function(L, "req_set_query", hlua_http_req_set_query); |
| 13119 | hlua_class_function(L, "req_set_uri", hlua_http_req_set_uri); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 13120 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13121 | hlua_class_function(L, "res_get_headers",hlua_http_res_get_headers); |
| 13122 | hlua_class_function(L, "res_del_header", hlua_http_res_del_hdr); |
| 13123 | hlua_class_function(L, "res_rep_header", hlua_http_res_rep_hdr); |
| 13124 | hlua_class_function(L, "res_rep_value", hlua_http_res_rep_val); |
| 13125 | hlua_class_function(L, "res_add_header", hlua_http_res_add_hdr); |
| 13126 | hlua_class_function(L, "res_set_header", hlua_http_res_set_hdr); |
| 13127 | hlua_class_function(L, "res_set_status", hlua_http_res_set_status); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 13128 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13129 | lua_rawset(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 13130 | |
| 13131 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13132 | class_http_ref = hlua_register_metatable(L, CLASS_HTTP); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 13133 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 13134 | /* |
| 13135 | * |
| 13136 | * Register class HTTPMessage |
| 13137 | * |
| 13138 | */ |
| 13139 | |
| 13140 | /* Create and fill the metatable. */ |
| 13141 | lua_newtable(L); |
| 13142 | |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 13143 | /* Create and fill the __index entry. */ |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 13144 | lua_pushstring(L, "__index"); |
| 13145 | lua_newtable(L); |
| 13146 | |
| 13147 | /* Register Lua functions. */ |
| 13148 | hlua_class_function(L, "is_resp", hlua_http_msg_is_resp); |
| 13149 | hlua_class_function(L, "get_stline", hlua_http_msg_get_stline); |
| 13150 | hlua_class_function(L, "get_headers", hlua_http_msg_get_headers); |
| 13151 | hlua_class_function(L, "del_header", hlua_http_msg_del_hdr); |
| 13152 | hlua_class_function(L, "rep_header", hlua_http_msg_rep_hdr); |
| 13153 | hlua_class_function(L, "rep_value", hlua_http_msg_rep_val); |
| 13154 | hlua_class_function(L, "add_header", hlua_http_msg_add_hdr); |
| 13155 | hlua_class_function(L, "set_header", hlua_http_msg_set_hdr); |
| 13156 | hlua_class_function(L, "set_method", hlua_http_msg_set_meth); |
| 13157 | hlua_class_function(L, "set_path", hlua_http_msg_set_path); |
| 13158 | hlua_class_function(L, "set_query", hlua_http_msg_set_query); |
| 13159 | hlua_class_function(L, "set_uri", hlua_http_msg_set_uri); |
| 13160 | hlua_class_function(L, "set_status", hlua_http_msg_set_status); |
| 13161 | hlua_class_function(L, "is_full", hlua_http_msg_is_full); |
| 13162 | hlua_class_function(L, "may_recv", hlua_http_msg_may_recv); |
| 13163 | hlua_class_function(L, "eom", hlua_http_msg_is_eom); |
| 13164 | hlua_class_function(L, "input", hlua_http_msg_get_in_len); |
| 13165 | hlua_class_function(L, "output", hlua_http_msg_get_out_len); |
| 13166 | |
| 13167 | hlua_class_function(L, "body", hlua_http_msg_get_body); |
| 13168 | hlua_class_function(L, "set", hlua_http_msg_set_data); |
| 13169 | hlua_class_function(L, "remove", hlua_http_msg_del_data); |
| 13170 | hlua_class_function(L, "append", hlua_http_msg_append); |
| 13171 | hlua_class_function(L, "prepend", hlua_http_msg_prepend); |
| 13172 | hlua_class_function(L, "insert", hlua_http_msg_insert_data); |
| 13173 | hlua_class_function(L, "set_eom", hlua_http_msg_set_eom); |
| 13174 | hlua_class_function(L, "unset_eom", hlua_http_msg_unset_eom); |
| 13175 | |
| 13176 | hlua_class_function(L, "send", hlua_http_msg_send); |
| 13177 | hlua_class_function(L, "forward", hlua_http_msg_forward); |
| 13178 | |
| 13179 | lua_rawset(L, -3); |
| 13180 | |
| 13181 | /* Register previous table in the registry with reference and named entry. */ |
| 13182 | class_http_msg_ref = hlua_register_metatable(L, CLASS_HTTP_MSG); |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 13183 | |
| 13184 | /* |
| 13185 | * |
| 13186 | * Register class HTTPClient |
| 13187 | * |
| 13188 | */ |
| 13189 | |
| 13190 | /* Create and fill the metatable. */ |
| 13191 | lua_newtable(L); |
| 13192 | lua_pushstring(L, "__index"); |
| 13193 | lua_newtable(L); |
| 13194 | hlua_class_function(L, "get", hlua_httpclient_get); |
William Lallemand | dc2cc90 | 2021-10-26 11:43:26 +0200 | [diff] [blame] | 13195 | hlua_class_function(L, "head", hlua_httpclient_head); |
| 13196 | hlua_class_function(L, "put", hlua_httpclient_put); |
| 13197 | hlua_class_function(L, "post", hlua_httpclient_post); |
| 13198 | hlua_class_function(L, "delete", hlua_httpclient_delete); |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 13199 | lua_settable(L, -3); /* Sets the __index entry. */ |
William Lallemand | f77f1de | 2021-09-28 19:10:38 +0200 | [diff] [blame] | 13200 | /* Register the garbage collector entry. */ |
| 13201 | lua_pushstring(L, "__gc"); |
| 13202 | lua_pushcclosure(L, hlua_httpclient_gc, 0); |
| 13203 | lua_settable(L, -3); /* Push the last 2 entries in the table at index -3 */ |
| 13204 | |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 13205 | |
| 13206 | |
| 13207 | class_httpclient_ref = hlua_register_metatable(L, CLASS_HTTPCLIENT); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 13208 | /* |
| 13209 | * |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 13210 | * Register class AppletTCP |
| 13211 | * |
| 13212 | */ |
| 13213 | |
| 13214 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13215 | lua_newtable(L); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 13216 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 13217 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13218 | lua_pushstring(L, "__index"); |
| 13219 | lua_newtable(L); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 13220 | |
| 13221 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13222 | hlua_class_function(L, "getline", hlua_applet_tcp_getline); |
| 13223 | hlua_class_function(L, "receive", hlua_applet_tcp_recv); |
| 13224 | hlua_class_function(L, "send", hlua_applet_tcp_send); |
| 13225 | hlua_class_function(L, "set_priv", hlua_applet_tcp_set_priv); |
| 13226 | hlua_class_function(L, "get_priv", hlua_applet_tcp_get_priv); |
| 13227 | hlua_class_function(L, "set_var", hlua_applet_tcp_set_var); |
| 13228 | hlua_class_function(L, "unset_var", hlua_applet_tcp_unset_var); |
| 13229 | hlua_class_function(L, "get_var", hlua_applet_tcp_get_var); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 13230 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13231 | lua_settable(L, -3); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 13232 | |
| 13233 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13234 | class_applet_tcp_ref = hlua_register_metatable(L, CLASS_APPLET_TCP); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 13235 | |
| 13236 | /* |
| 13237 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 13238 | * Register class AppletHTTP |
| 13239 | * |
| 13240 | */ |
| 13241 | |
| 13242 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13243 | lua_newtable(L); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 13244 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 13245 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13246 | lua_pushstring(L, "__index"); |
| 13247 | lua_newtable(L); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 13248 | |
| 13249 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13250 | hlua_class_function(L, "set_priv", hlua_applet_http_set_priv); |
| 13251 | hlua_class_function(L, "get_priv", hlua_applet_http_get_priv); |
| 13252 | hlua_class_function(L, "set_var", hlua_applet_http_set_var); |
| 13253 | hlua_class_function(L, "unset_var", hlua_applet_http_unset_var); |
| 13254 | hlua_class_function(L, "get_var", hlua_applet_http_get_var); |
| 13255 | hlua_class_function(L, "getline", hlua_applet_http_getline); |
| 13256 | hlua_class_function(L, "receive", hlua_applet_http_recv); |
| 13257 | hlua_class_function(L, "send", hlua_applet_http_send); |
| 13258 | hlua_class_function(L, "add_header", hlua_applet_http_addheader); |
| 13259 | hlua_class_function(L, "set_status", hlua_applet_http_status); |
| 13260 | hlua_class_function(L, "start_response", hlua_applet_http_start_response); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 13261 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13262 | lua_settable(L, -3); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 13263 | |
| 13264 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13265 | class_applet_http_ref = hlua_register_metatable(L, CLASS_APPLET_HTTP); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 13266 | |
| 13267 | /* |
| 13268 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 13269 | * Register class TXN |
| 13270 | * |
| 13271 | */ |
| 13272 | |
| 13273 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13274 | lua_newtable(L); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 13275 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 13276 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13277 | lua_pushstring(L, "__index"); |
| 13278 | lua_newtable(L); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 13279 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 13280 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13281 | hlua_class_function(L, "set_priv", hlua_set_priv); |
| 13282 | hlua_class_function(L, "get_priv", hlua_get_priv); |
| 13283 | hlua_class_function(L, "set_var", hlua_set_var); |
| 13284 | hlua_class_function(L, "unset_var", hlua_unset_var); |
| 13285 | hlua_class_function(L, "get_var", hlua_get_var); |
| 13286 | hlua_class_function(L, "done", hlua_txn_done); |
| 13287 | hlua_class_function(L, "reply", hlua_txn_reply_new); |
| 13288 | hlua_class_function(L, "set_loglevel", hlua_txn_set_loglevel); |
| 13289 | hlua_class_function(L, "set_tos", hlua_txn_set_tos); |
| 13290 | hlua_class_function(L, "set_mark", hlua_txn_set_mark); |
| 13291 | hlua_class_function(L, "set_priority_class", hlua_txn_set_priority_class); |
| 13292 | hlua_class_function(L, "set_priority_offset", hlua_txn_set_priority_offset); |
| 13293 | hlua_class_function(L, "deflog", hlua_txn_deflog); |
| 13294 | hlua_class_function(L, "log", hlua_txn_log); |
| 13295 | hlua_class_function(L, "Debug", hlua_txn_log_debug); |
| 13296 | hlua_class_function(L, "Info", hlua_txn_log_info); |
| 13297 | hlua_class_function(L, "Warning", hlua_txn_log_warning); |
| 13298 | hlua_class_function(L, "Alert", hlua_txn_log_alert); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 13299 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13300 | lua_rawset(L, -3); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [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_txn_ref = hlua_register_metatable(L, CLASS_TXN); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 13304 | |
| 13305 | /* |
| 13306 | * |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 13307 | * Register class reply |
| 13308 | * |
| 13309 | */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13310 | lua_newtable(L); |
| 13311 | lua_pushstring(L, "__index"); |
| 13312 | lua_newtable(L); |
| 13313 | hlua_class_function(L, "set_status", hlua_txn_reply_set_status); |
| 13314 | hlua_class_function(L, "add_header", hlua_txn_reply_add_header); |
| 13315 | hlua_class_function(L, "del_header", hlua_txn_reply_del_header); |
| 13316 | hlua_class_function(L, "set_body", hlua_txn_reply_set_body); |
| 13317 | lua_settable(L, -3); /* Sets the __index entry. */ |
| 13318 | class_txn_reply_ref = luaL_ref(L, LUA_REGISTRYINDEX); |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 13319 | |
| 13320 | |
| 13321 | /* |
| 13322 | * |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 13323 | * Register class Socket |
| 13324 | * |
| 13325 | */ |
| 13326 | |
| 13327 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13328 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 13329 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 13330 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13331 | lua_pushstring(L, "__index"); |
| 13332 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 13333 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 13334 | #ifdef USE_OPENSSL |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13335 | hlua_class_function(L, "connect_ssl", hlua_socket_connect_ssl); |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 13336 | #endif |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13337 | hlua_class_function(L, "connect", hlua_socket_connect); |
| 13338 | hlua_class_function(L, "send", hlua_socket_send); |
| 13339 | hlua_class_function(L, "receive", hlua_socket_receive); |
| 13340 | hlua_class_function(L, "close", hlua_socket_close); |
| 13341 | hlua_class_function(L, "getpeername", hlua_socket_getpeername); |
| 13342 | hlua_class_function(L, "getsockname", hlua_socket_getsockname); |
| 13343 | hlua_class_function(L, "setoption", hlua_socket_setoption); |
| 13344 | hlua_class_function(L, "settimeout", hlua_socket_settimeout); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 13345 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13346 | 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] | 13347 | |
| 13348 | /* Register the garbage collector entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13349 | lua_pushstring(L, "__gc"); |
| 13350 | lua_pushcclosure(L, hlua_socket_gc, 0); |
| 13351 | 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] | 13352 | |
| 13353 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13354 | class_socket_ref = hlua_register_metatable(L, CLASS_SOCKET); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 13355 | |
Thierry Fournier | aafc777 | 2020-12-04 11:47:47 +0100 | [diff] [blame] | 13356 | lua_atpanic(L, hlua_panic_safe); |
| 13357 | |
| 13358 | return L; |
| 13359 | } |
| 13360 | |
| 13361 | void hlua_init(void) { |
| 13362 | int i; |
Amaury Denoyelle | 239fdbf | 2021-03-24 10:22:03 +0100 | [diff] [blame] | 13363 | char *errmsg; |
Thierry Fournier | aafc777 | 2020-12-04 11:47:47 +0100 | [diff] [blame] | 13364 | #ifdef USE_OPENSSL |
| 13365 | struct srv_kw *kw; |
| 13366 | int tmp_error; |
| 13367 | char *error; |
| 13368 | char *args[] = { /* SSL client configuration. */ |
| 13369 | "ssl", |
| 13370 | "verify", |
| 13371 | "none", |
| 13372 | NULL |
| 13373 | }; |
| 13374 | #endif |
| 13375 | |
| 13376 | /* Init post init function list head */ |
| 13377 | for (i = 0; i < MAX_THREADS + 1; i++) |
| 13378 | LIST_INIT(&hlua_init_functions[i]); |
| 13379 | |
| 13380 | /* Init state for common/shared lua parts */ |
| 13381 | hlua_state_id = 0; |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 13382 | ha_set_thread(NULL); |
Thierry Fournier | aafc777 | 2020-12-04 11:47:47 +0100 | [diff] [blame] | 13383 | hlua_states[0] = hlua_init_state(0); |
| 13384 | |
| 13385 | /* Init state 1 for thread 0. We have at least one thread. */ |
| 13386 | hlua_state_id = 1; |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 13387 | ha_set_thread(NULL); |
Thierry Fournier | aafc777 | 2020-12-04 11:47:47 +0100 | [diff] [blame] | 13388 | hlua_states[1] = hlua_init_state(1); |
| 13389 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 13390 | /* Proxy and server configuration initialisation. */ |
William Lallemand | 6bb77b9 | 2021-07-28 15:48:16 +0200 | [diff] [blame] | 13391 | 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] | 13392 | if (!socket_proxy) { |
| 13393 | fprintf(stderr, "Lua init: %s\n", errmsg); |
| 13394 | exit(1); |
| 13395 | } |
| 13396 | proxy_preset_defaults(socket_proxy); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 13397 | |
| 13398 | /* Init TCP server: unchanged parameters */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 13399 | socket_tcp = new_server(socket_proxy); |
| 13400 | if (!socket_tcp) { |
| 13401 | fprintf(stderr, "Lua init: failed to allocate tcp server socket\n"); |
| 13402 | exit(1); |
| 13403 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 13404 | |
| 13405 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 13406 | /* Init TCP server: unchanged parameters */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 13407 | socket_ssl = new_server(socket_proxy); |
| 13408 | if (!socket_ssl) { |
| 13409 | fprintf(stderr, "Lua init: failed to allocate ssl server socket\n"); |
| 13410 | exit(1); |
| 13411 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 13412 | |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 13413 | socket_ssl->use_ssl = 1; |
| 13414 | socket_ssl->xprt = xprt_get(XPRT_SSL); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 13415 | |
Bertrand Jacquin | 80839ff | 2021-01-21 19:14:46 +0000 | [diff] [blame] | 13416 | for (i = 0; args[i] != NULL; i++) { |
| 13417 | 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] | 13418 | /* |
| 13419 | * |
| 13420 | * If the keyword is not known, we can search in the registered |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 13421 | * server keywords. This is useful to configure special SSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 13422 | * features like client certificates and ssl_verify. |
| 13423 | * |
| 13424 | */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 13425 | tmp_error = kw->parse(args, &i, socket_proxy, socket_ssl, &error); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 13426 | if (tmp_error != 0) { |
| 13427 | fprintf(stderr, "INTERNAL ERROR: %s\n", error); |
| 13428 | abort(); /* This must be never arrives because the command line |
| 13429 | not editable by the user. */ |
| 13430 | } |
Bertrand Jacquin | 80839ff | 2021-01-21 19:14:46 +0000 | [diff] [blame] | 13431 | i += kw->skip; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 13432 | } |
| 13433 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 13434 | #endif |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 13435 | |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 13436 | } |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 13437 | |
Tim Duesterhus | d0c0ca2 | 2020-07-04 11:53:26 +0200 | [diff] [blame] | 13438 | static void hlua_deinit() |
| 13439 | { |
Willy Tarreau | 186f376 | 2020-12-04 11:48:12 +0100 | [diff] [blame] | 13440 | int thr; |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 13441 | struct hlua_reg_filter *reg_flt, *reg_flt_bck; |
| 13442 | |
| 13443 | list_for_each_entry_safe(reg_flt, reg_flt_bck, &referenced_filters, l) |
| 13444 | release_hlua_reg_filter(reg_flt); |
Willy Tarreau | 186f376 | 2020-12-04 11:48:12 +0100 | [diff] [blame] | 13445 | |
| 13446 | for (thr = 0; thr < MAX_THREADS+1; thr++) { |
| 13447 | if (hlua_states[thr]) |
| 13448 | lua_close(hlua_states[thr]); |
| 13449 | } |
Willy Tarreau | 430bf4a | 2021-03-04 09:45:32 +0100 | [diff] [blame] | 13450 | |
Amaury Denoyelle | bc2ebfa | 2021-08-25 15:34:53 +0200 | [diff] [blame] | 13451 | srv_drop(socket_tcp); |
Willy Tarreau | 430bf4a | 2021-03-04 09:45:32 +0100 | [diff] [blame] | 13452 | |
Willy Tarreau | 0f143af | 2021-03-05 10:41:48 +0100 | [diff] [blame] | 13453 | #ifdef USE_OPENSSL |
Amaury Denoyelle | bc2ebfa | 2021-08-25 15:34:53 +0200 | [diff] [blame] | 13454 | srv_drop(socket_ssl); |
Willy Tarreau | 0f143af | 2021-03-05 10:41:48 +0100 | [diff] [blame] | 13455 | #endif |
Amaury Denoyelle | 239fdbf | 2021-03-24 10:22:03 +0100 | [diff] [blame] | 13456 | |
| 13457 | free_proxy(socket_proxy); |
Tim Duesterhus | d0c0ca2 | 2020-07-04 11:53:26 +0200 | [diff] [blame] | 13458 | } |
| 13459 | |
| 13460 | REGISTER_POST_DEINIT(hlua_deinit); |
| 13461 | |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 13462 | static void hlua_register_build_options(void) |
| 13463 | { |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 13464 | char *ptr = NULL; |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 13465 | |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 13466 | memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE); |
| 13467 | hap_register_build_opts(ptr, 1); |
| 13468 | } |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 13469 | |
| 13470 | INITCALL0(STG_REGISTER, hlua_register_build_options); |