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 | */ |
| 357 | static uint32_t hlua_timeout_session = 4000; /* session timeout. */ |
| 358 | static uint32_t hlua_timeout_task = 0; /* task timeout. */ |
| 359 | static uint32_t hlua_timeout_applet = 4000; /* applet timeout. */ |
| 360 | |
| 361 | /* hlua multipurpose timer: |
| 362 | * used to compute burst lua time (within a single hlua_ctx_resume()) |
| 363 | * and cumulative lua time for a given coroutine, and to check |
| 364 | * the lua coroutine against the configured timeouts |
| 365 | */ |
| 366 | |
| 367 | /* fetch per-thread cpu_time with ms precision (may wrap) */ |
| 368 | static inline uint32_t _hlua_time_ms() |
| 369 | { |
| 370 | /* We're interested in the current cpu time in ms, which will be returned |
| 371 | * as a uint32_t to save some space. |
| 372 | * We must take the following into account: |
| 373 | * |
| 374 | * - now_cpu_time_fast() which returns the time in nanoseconds as a uint64_t |
| 375 | * will wrap every 585 years. |
| 376 | * - uint32_t may only contain 4294967295ms (~=49.7 days), so _hlua_time_ms() |
| 377 | * itself will also wrap every 49.7 days. |
| 378 | * |
| 379 | * While we can safely ignore the now_cpu_time_fast() wrap, we must |
| 380 | * take care of the uint32_t wrap by making sure to exclusively |
| 381 | * manipulate the time using uint32_t everywhere _hlua_time_ms() |
| 382 | * is involved. |
| 383 | */ |
| 384 | return (uint32_t)(now_cpu_time_fast() / 1000000ULL); |
| 385 | } |
| 386 | |
| 387 | /* computes time spent in a single lua execution (in ms) */ |
| 388 | static inline uint32_t _hlua_time_burst(const struct hlua_timer *timer) |
| 389 | { |
| 390 | uint32_t burst_ms; |
| 391 | |
| 392 | /* wrapping is expected and properly |
| 393 | * handled thanks to _hlua_time_ms() and burst_ms |
| 394 | * being of the same type |
| 395 | */ |
| 396 | burst_ms = _hlua_time_ms() - timer->start; |
| 397 | return burst_ms; |
| 398 | } |
| 399 | |
| 400 | static inline void hlua_timer_init(struct hlua_timer *timer, unsigned int max) |
| 401 | { |
| 402 | timer->cumulative = 0; |
| 403 | timer->burst = 0; |
| 404 | timer->max = max; |
| 405 | } |
| 406 | |
| 407 | /* reset the timer ctx between 2 yields */ |
| 408 | static inline void hlua_timer_reset(struct hlua_timer *timer) |
| 409 | { |
| 410 | timer->cumulative += timer->burst; |
| 411 | timer->burst = 0; |
| 412 | } |
| 413 | |
| 414 | /* start the timer right before a new execution */ |
| 415 | static inline void hlua_timer_start(struct hlua_timer *timer) |
| 416 | { |
| 417 | timer->start = _hlua_time_ms(); |
| 418 | } |
| 419 | |
| 420 | /* update hlua timer when finishing an execution */ |
| 421 | static inline void hlua_timer_stop(struct hlua_timer *timer) |
| 422 | { |
| 423 | timer->burst += _hlua_time_burst(timer); |
| 424 | } |
| 425 | |
| 426 | /* check the timers for current hlua context |
| 427 | * Returns 1 if the check succeeded and 0 if it failed |
| 428 | * (ie: timeout exceeded) |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 429 | */ |
Aurelien DARRAGON | da9503c | 2022-11-25 09:10:07 +0100 | [diff] [blame^] | 430 | static inline int hlua_timer_check(const struct hlua_timer *timer) |
| 431 | { |
| 432 | uint32_t pburst = _hlua_time_burst(timer); /* pending burst time in ms */ |
| 433 | |
| 434 | if (timer->max && (timer->cumulative + timer->burst + pburst) > timer->max) |
| 435 | return 0; /* cumulative timeout exceeded */ |
| 436 | return 1; /* ok */ |
| 437 | } |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 438 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 439 | /* Interrupts the Lua processing each "hlua_nb_instruction" instructions. |
| 440 | * it is used for preventing infinite loops. |
| 441 | * |
| 442 | * I test the scheer with an infinite loop containing one incrementation |
| 443 | * and one test. I run this loop between 10 seconds, I raise a ceil of |
| 444 | * 710M loops from one interrupt each 9000 instructions, so I fix the value |
| 445 | * to one interrupt each 10 000 instructions. |
| 446 | * |
| 447 | * configured | Number of |
| 448 | * instructions | loops executed |
| 449 | * between two | in milions |
| 450 | * forced yields | |
| 451 | * ---------------+--------------- |
| 452 | * 10 | 160 |
| 453 | * 500 | 670 |
| 454 | * 1000 | 680 |
| 455 | * 5000 | 700 |
| 456 | * 7000 | 700 |
| 457 | * 8000 | 700 |
| 458 | * 9000 | 710 <- ceil |
| 459 | * 10000 | 710 |
| 460 | * 100000 | 710 |
| 461 | * 1000000 | 710 |
| 462 | * |
| 463 | */ |
| 464 | static unsigned int hlua_nb_instruction = 10000; |
| 465 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 466 | /* Descriptor for the memory allocation state. The limit is pre-initialised to |
| 467 | * 0 until it is replaced by "tune.lua.maxmem" during the config parsing, or it |
| 468 | * is replaced with ~0 during post_init after everything was loaded. This way |
| 469 | * it is guaranteed that if limit is ~0 the boot is complete and that if it's |
| 470 | * zero it's not yet limited and proper accounting is required. |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 471 | */ |
| 472 | struct hlua_mem_allocator { |
| 473 | size_t allocated; |
| 474 | size_t limit; |
| 475 | }; |
| 476 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 477 | static struct hlua_mem_allocator hlua_global_allocator THREAD_ALIGNED(64); |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 478 | |
Aurelien DARRAGON | c84899c | 2023-02-20 18:18:59 +0100 | [diff] [blame] | 479 | /* hlua event subscription */ |
| 480 | struct hlua_event_sub { |
| 481 | int fcn_ref; |
| 482 | int state_id; |
| 483 | struct hlua *hlua; |
| 484 | struct task *task; |
| 485 | event_hdl_async_equeue equeue; |
| 486 | struct event_hdl_sub *sub; |
| 487 | uint8_t paused; |
| 488 | }; |
| 489 | |
| 490 | /* This is the memory pool containing struct hlua_event_sub |
| 491 | * for event subscriptions from lua |
| 492 | */ |
| 493 | DECLARE_STATIC_POOL(pool_head_hlua_event_sub, "hlua_esub", sizeof(struct hlua_event_sub)); |
| 494 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 495 | /* These functions converts types between HAProxy internal args or |
| 496 | * sample and LUA types. Another function permits to check if the |
| 497 | * LUA stack contains arguments according with an required ARG_T |
| 498 | * format. |
| 499 | */ |
| 500 | static int hlua_arg2lua(lua_State *L, const struct arg *arg); |
| 501 | static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 502 | __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] | 503 | uint64_t mask, struct proxy *p); |
Willy Tarreau | 5eadada | 2015-03-10 17:28:54 +0100 | [diff] [blame] | 504 | static int hlua_smp2lua(lua_State *L, struct sample *smp); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 505 | static int hlua_smp2lua_str(lua_State *L, struct sample *smp); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 506 | static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp); |
| 507 | |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 508 | __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] | 509 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 510 | struct prepend_path { |
| 511 | struct list l; |
| 512 | char *type; |
| 513 | char *path; |
| 514 | }; |
| 515 | |
| 516 | static struct list prepend_path_list = LIST_HEAD_INIT(prepend_path_list); |
| 517 | |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 518 | #define SEND_ERR(__be, __fmt, __args...) \ |
| 519 | do { \ |
| 520 | send_log(__be, LOG_ERR, __fmt, ## __args); \ |
| 521 | if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) \ |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 522 | ha_alert(__fmt, ## __args); \ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 523 | } while (0) |
| 524 | |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 525 | static inline struct hlua_function *new_hlua_function() |
| 526 | { |
| 527 | struct hlua_function *fcn; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 528 | int i; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 529 | |
| 530 | fcn = calloc(1, sizeof(*fcn)); |
| 531 | if (!fcn) |
| 532 | return NULL; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 533 | LIST_APPEND(&referenced_functions, &fcn->l); |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 534 | for (i = 0; i < MAX_THREADS + 1; i++) |
| 535 | fcn->function_ref[i] = -1; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 536 | return fcn; |
| 537 | } |
| 538 | |
Christopher Faulet | dda4444 | 2021-04-12 14:05:43 +0200 | [diff] [blame] | 539 | static inline void release_hlua_function(struct hlua_function *fcn) |
| 540 | { |
| 541 | if (!fcn) |
| 542 | return; |
| 543 | if (fcn->name) |
| 544 | ha_free(&fcn->name); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 545 | LIST_DELETE(&fcn->l); |
Christopher Faulet | dda4444 | 2021-04-12 14:05:43 +0200 | [diff] [blame] | 546 | ha_free(&fcn); |
| 547 | } |
| 548 | |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 549 | /* If the common state is set, the stack id is 0, otherwise it is the tid + 1 */ |
| 550 | static inline int fcn_ref_to_stack_id(struct hlua_function *fcn) |
| 551 | { |
| 552 | if (fcn->function_ref[0] == -1) |
| 553 | return tid + 1; |
| 554 | return 0; |
| 555 | } |
| 556 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 557 | /* Create a new registered filter. Only its name is filled */ |
| 558 | static inline struct hlua_reg_filter *new_hlua_reg_filter(const char *name) |
| 559 | { |
| 560 | struct hlua_reg_filter *reg_flt; |
| 561 | int i; |
| 562 | |
| 563 | reg_flt = calloc(1, sizeof(*reg_flt)); |
| 564 | if (!reg_flt) |
| 565 | return NULL; |
| 566 | reg_flt->name = strdup(name); |
| 567 | if (!reg_flt->name) { |
| 568 | free(reg_flt); |
| 569 | return NULL; |
| 570 | } |
| 571 | LIST_APPEND(&referenced_filters, ®_flt->l); |
| 572 | for (i = 0; i < MAX_THREADS + 1; i++) { |
| 573 | reg_flt->flt_ref[i] = -1; |
| 574 | reg_flt->fun_ref[i] = -1; |
| 575 | } |
| 576 | return reg_flt; |
| 577 | } |
| 578 | |
| 579 | /* Release a registered filter */ |
| 580 | static inline void release_hlua_reg_filter(struct hlua_reg_filter *reg_flt) |
| 581 | { |
| 582 | if (!reg_flt) |
| 583 | return; |
| 584 | if (reg_flt->name) |
| 585 | ha_free(®_flt->name); |
| 586 | LIST_DELETE(®_flt->l); |
| 587 | ha_free(®_flt); |
| 588 | } |
| 589 | |
| 590 | /* If the common state is set, the stack id is 0, otherwise it is the tid + 1 */ |
| 591 | static inline int reg_flt_to_stack_id(struct hlua_reg_filter *reg_flt) |
| 592 | { |
| 593 | if (reg_flt->fun_ref[0] == -1) |
| 594 | return tid + 1; |
| 595 | return 0; |
| 596 | } |
| 597 | |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 598 | /* Used to check an Lua function type in the stack. It creates and |
| 599 | * returns a reference of the function. This function throws an |
Aurelien DARRAGON | f8f8a2b | 2023-03-02 17:50:49 +0100 | [diff] [blame] | 600 | * error if the argument is not a "function". |
| 601 | * When no longer used, the ref must be released with hlua_unref() |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 602 | */ |
Aurelien DARRAGON | 9ee0d04 | 2023-03-20 18:36:08 +0100 | [diff] [blame] | 603 | __LJMP int hlua_checkfunction(lua_State *L, int argno) |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 604 | { |
| 605 | if (!lua_isfunction(L, argno)) { |
Thierry FOURNIER | fd1e955 | 2018-02-23 18:41:18 +0100 | [diff] [blame] | 606 | 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] | 607 | WILL_LJMP(luaL_argerror(L, argno, msg)); |
| 608 | } |
| 609 | lua_pushvalue(L, argno); |
| 610 | return luaL_ref(L, LUA_REGISTRYINDEX); |
| 611 | } |
| 612 | |
Christopher Faulet | ba9e21d | 2020-02-25 10:20:04 +0100 | [diff] [blame] | 613 | /* Used to check an Lua table type in the stack. It creates and |
| 614 | * returns a reference of the table. This function throws an |
Aurelien DARRAGON | f8f8a2b | 2023-03-02 17:50:49 +0100 | [diff] [blame] | 615 | * error if the argument is not a "table". |
| 616 | * When no longer used, the ref must be released with hlua_unref() |
Christopher Faulet | ba9e21d | 2020-02-25 10:20:04 +0100 | [diff] [blame] | 617 | */ |
Aurelien DARRAGON | 9ee0d04 | 2023-03-20 18:36:08 +0100 | [diff] [blame] | 618 | __LJMP int hlua_checktable(lua_State *L, int argno) |
Christopher Faulet | ba9e21d | 2020-02-25 10:20:04 +0100 | [diff] [blame] | 619 | { |
| 620 | if (!lua_istable(L, argno)) { |
| 621 | const char *msg = lua_pushfstring(L, "table expected, got %s", luaL_typename(L, argno)); |
| 622 | WILL_LJMP(luaL_argerror(L, argno, msg)); |
| 623 | } |
| 624 | lua_pushvalue(L, argno); |
| 625 | return luaL_ref(L, LUA_REGISTRYINDEX); |
| 626 | } |
| 627 | |
Aurelien DARRAGON | f8f8a2b | 2023-03-02 17:50:49 +0100 | [diff] [blame] | 628 | /* Get a reference to the object that is at the top of the stack |
| 629 | * The referenced object will be popped from the stack |
| 630 | * |
| 631 | * The function returns the reference to the object which must |
| 632 | * be cleared using hlua_unref() when no longer used |
| 633 | */ |
| 634 | __LJMP int hlua_ref(lua_State *L) |
| 635 | { |
| 636 | return MAY_LJMP(luaL_ref(L, LUA_REGISTRYINDEX)); |
| 637 | } |
| 638 | |
| 639 | /* Pushes a reference previously created using luaL_ref(L, LUA_REGISTRYINDEX) |
| 640 | * on <L> stack |
| 641 | * (ie: hlua_checkfunction(), hlua_checktable() or hlua_ref()) |
| 642 | * |
| 643 | * When the reference is no longer used, it should be released by calling |
| 644 | * hlua_unref() |
| 645 | * |
| 646 | * <L> can be from any co-routine as long as it belongs to the same lua |
| 647 | * parent state that the one used to get the reference. |
| 648 | */ |
| 649 | void hlua_pushref(lua_State *L, int ref) |
| 650 | { |
| 651 | lua_rawgeti(L, LUA_REGISTRYINDEX, ref); |
| 652 | } |
| 653 | |
| 654 | /* Releases a reference previously created using luaL_ref(L, LUA_REGISTRYINDEX) |
| 655 | * (ie: hlua_checkfunction(), hlua_checktable() or hlua_ref()) |
| 656 | * |
| 657 | * This will allow the reference to be reused and the referred object |
| 658 | * to be garbage collected. |
| 659 | * |
| 660 | * <L> can be from any co-routine as long as it belongs to the same lua |
| 661 | * parent state that the one used to get the reference. |
| 662 | */ |
| 663 | void hlua_unref(lua_State *L, int ref) |
| 664 | { |
| 665 | luaL_unref(L, LUA_REGISTRYINDEX, ref); |
| 666 | } |
| 667 | |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 668 | __LJMP const char *hlua_traceback(lua_State *L, const char* sep) |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 669 | { |
| 670 | lua_Debug ar; |
| 671 | int level = 0; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 672 | struct buffer *msg = get_trash_chunk(); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 673 | |
| 674 | while (lua_getstack(L, level++, &ar)) { |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 675 | /* Fill fields: |
| 676 | * 'S': fills in the fields source, short_src, linedefined, lastlinedefined, and what; |
| 677 | * 'l': fills in the field currentline; |
| 678 | * 'n': fills in the field name and namewhat; |
| 679 | * 't': fills in the field istailcall; |
| 680 | */ |
| 681 | lua_getinfo(L, "Slnt", &ar); |
| 682 | |
Willy Tarreau | 5c14340 | 2022-06-19 17:35:53 +0200 | [diff] [blame] | 683 | /* skip these empty entries, usually they come from deep C functions */ |
| 684 | if (ar.currentline < 0 && *ar.what == 'C' && !*ar.namewhat && !ar.name) |
| 685 | continue; |
| 686 | |
| 687 | /* Add separator */ |
| 688 | if (b_data(msg)) |
| 689 | chunk_appendf(msg, "%s", sep); |
| 690 | |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 691 | /* Append code localisation */ |
| 692 | if (ar.currentline > 0) |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 693 | chunk_appendf(msg, "%s:%d: ", ar.short_src, ar.currentline); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 694 | else |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 695 | chunk_appendf(msg, "%s: ", ar.short_src); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 696 | |
| 697 | /* |
| 698 | * Get function name |
| 699 | * |
| 700 | * if namewhat is no empty, name is defined. |
| 701 | * what contains "Lua" for Lua function, "C" for C function, |
| 702 | * or "main" for main code. |
| 703 | */ |
| 704 | 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] | 705 | chunk_appendf(msg, "in %s '%s'", ar.namewhat, ar.name); /* use it */ |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 706 | |
| 707 | 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] | 708 | chunk_appendf(msg, "in main chunk"); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 709 | |
| 710 | else if (*ar.what != 'C') /* for Lua functions, use <file:line> */ |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 711 | chunk_appendf(msg, "in function line %d", ar.linedefined); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 712 | |
| 713 | else /* nothing left... */ |
| 714 | chunk_appendf(msg, "?"); |
| 715 | |
| 716 | |
| 717 | /* Display tailed call */ |
| 718 | if (ar.istailcall) |
| 719 | chunk_appendf(msg, " ..."); |
| 720 | } |
| 721 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 722 | return msg->area; |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 723 | } |
| 724 | |
| 725 | |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 726 | /* This function check the number of arguments available in the |
| 727 | * stack. If the number of arguments available is not the same |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 728 | * then <nb> an error is thrown. |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 729 | */ |
| 730 | __LJMP static inline void check_args(lua_State *L, int nb, char *fcn) |
| 731 | { |
| 732 | if (lua_gettop(L) == nb) |
| 733 | return; |
| 734 | WILL_LJMP(luaL_error(L, "'%s' needs %d arguments", fcn, nb)); |
| 735 | } |
| 736 | |
Mark Lakes | 22154b4 | 2018-01-29 14:38:40 -0800 | [diff] [blame] | 737 | /* This function pushes an error string prefixed by the file name |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 738 | * and the line number where the error is encountered. |
| 739 | */ |
| 740 | static int hlua_pusherror(lua_State *L, const char *fmt, ...) |
| 741 | { |
| 742 | va_list argp; |
| 743 | va_start(argp, fmt); |
| 744 | luaL_where(L, 1); |
| 745 | lua_pushvfstring(L, fmt, argp); |
| 746 | va_end(argp); |
| 747 | lua_concat(L, 2); |
| 748 | return 1; |
| 749 | } |
| 750 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 751 | /* This functions is used with sample fetch and converters. It |
| 752 | * converts the HAProxy configuration argument in a lua stack |
| 753 | * values. |
| 754 | * |
| 755 | * It takes an array of "arg", and each entry of the array is |
| 756 | * converted and pushed in the LUA stack. |
| 757 | */ |
| 758 | static int hlua_arg2lua(lua_State *L, const struct arg *arg) |
| 759 | { |
| 760 | switch (arg->type) { |
| 761 | case ARGT_SINT: |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 762 | case ARGT_TIME: |
| 763 | case ARGT_SIZE: |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 764 | lua_pushinteger(L, arg->data.sint); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 765 | break; |
| 766 | |
| 767 | case ARGT_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 768 | lua_pushlstring(L, arg->data.str.area, arg->data.str.data); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 769 | break; |
| 770 | |
| 771 | case ARGT_IPV4: |
| 772 | case ARGT_IPV6: |
| 773 | case ARGT_MSK4: |
| 774 | case ARGT_MSK6: |
| 775 | case ARGT_FE: |
| 776 | case ARGT_BE: |
| 777 | case ARGT_TAB: |
| 778 | case ARGT_SRV: |
| 779 | case ARGT_USR: |
| 780 | case ARGT_MAP: |
| 781 | default: |
| 782 | lua_pushnil(L); |
| 783 | break; |
| 784 | } |
| 785 | return 1; |
| 786 | } |
| 787 | |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 788 | /* 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] | 789 | * 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] | 790 | * with sample fetch wrappers. The input arguments are given to the |
| 791 | * lua wrapper and converted as arg list by the function. |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 792 | */ |
| 793 | static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg) |
| 794 | { |
| 795 | switch (lua_type(L, ud)) { |
| 796 | |
| 797 | case LUA_TNUMBER: |
| 798 | case LUA_TBOOLEAN: |
| 799 | arg->type = ARGT_SINT; |
| 800 | arg->data.sint = lua_tointeger(L, ud); |
| 801 | break; |
| 802 | |
| 803 | case LUA_TSTRING: |
| 804 | arg->type = ARGT_STR; |
Tim Duesterhus | 2e89dec | 2019-09-29 23:03:08 +0200 | [diff] [blame] | 805 | 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] | 806 | /* 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] | 807 | 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] | 808 | arg->data.str.head = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 809 | break; |
| 810 | |
| 811 | case LUA_TUSERDATA: |
| 812 | case LUA_TNIL: |
| 813 | case LUA_TTABLE: |
| 814 | case LUA_TFUNCTION: |
| 815 | case LUA_TTHREAD: |
| 816 | case LUA_TLIGHTUSERDATA: |
| 817 | arg->type = ARGT_SINT; |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 818 | arg->data.sint = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 819 | break; |
| 820 | } |
| 821 | return 1; |
| 822 | } |
| 823 | |
| 824 | /* the following functions are used to convert a struct sample |
| 825 | * in Lua type. This useful to convert the return of the |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 826 | * fetches or converters. |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 827 | */ |
Willy Tarreau | 5eadada | 2015-03-10 17:28:54 +0100 | [diff] [blame] | 828 | static int hlua_smp2lua(lua_State *L, struct sample *smp) |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 829 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 830 | switch (smp->data.type) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 831 | case SMP_T_SINT: |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 832 | case SMP_T_BOOL: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 833 | lua_pushinteger(L, smp->data.u.sint); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 834 | break; |
| 835 | |
| 836 | case SMP_T_BIN: |
| 837 | case SMP_T_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 838 | 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] | 839 | break; |
| 840 | |
| 841 | case SMP_T_METH: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 842 | switch (smp->data.u.meth.meth) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 843 | case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break; |
| 844 | case HTTP_METH_GET: lua_pushstring(L, "GET"); break; |
| 845 | case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break; |
| 846 | case HTTP_METH_POST: lua_pushstring(L, "POST"); break; |
| 847 | case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break; |
| 848 | case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break; |
| 849 | case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break; |
| 850 | case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break; |
| 851 | case HTTP_METH_OTHER: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 852 | 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] | 853 | break; |
| 854 | default: |
| 855 | lua_pushnil(L); |
| 856 | break; |
| 857 | } |
| 858 | break; |
| 859 | |
| 860 | case SMP_T_IPV4: |
| 861 | case SMP_T_IPV6: |
| 862 | 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] | 863 | if (sample_casts[smp->data.type][SMP_T_STR] && |
| 864 | sample_casts[smp->data.type][SMP_T_STR](smp)) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 865 | 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] | 866 | else |
| 867 | lua_pushnil(L); |
| 868 | break; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 869 | default: |
| 870 | lua_pushnil(L); |
| 871 | break; |
| 872 | } |
| 873 | return 1; |
| 874 | } |
| 875 | |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 876 | /* the following functions are used to convert a struct sample |
| 877 | * in Lua strings. This is useful to convert the return of the |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 878 | * fetches or converters. |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 879 | */ |
| 880 | static int hlua_smp2lua_str(lua_State *L, struct sample *smp) |
| 881 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 882 | switch (smp->data.type) { |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 883 | |
| 884 | case SMP_T_BIN: |
| 885 | case SMP_T_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 886 | 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] | 887 | break; |
| 888 | |
| 889 | case SMP_T_METH: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 890 | switch (smp->data.u.meth.meth) { |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 891 | case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break; |
| 892 | case HTTP_METH_GET: lua_pushstring(L, "GET"); break; |
| 893 | case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break; |
| 894 | case HTTP_METH_POST: lua_pushstring(L, "POST"); break; |
| 895 | case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break; |
| 896 | case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break; |
| 897 | case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break; |
| 898 | case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break; |
| 899 | case HTTP_METH_OTHER: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 900 | 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] | 901 | break; |
| 902 | default: |
| 903 | lua_pushstring(L, ""); |
| 904 | break; |
| 905 | } |
| 906 | break; |
| 907 | |
| 908 | case SMP_T_SINT: |
| 909 | case SMP_T_BOOL: |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 910 | case SMP_T_IPV4: |
| 911 | case SMP_T_IPV6: |
| 912 | 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] | 913 | if (sample_casts[smp->data.type][SMP_T_STR] && |
| 914 | sample_casts[smp->data.type][SMP_T_STR](smp)) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 915 | 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] | 916 | else |
| 917 | lua_pushstring(L, ""); |
| 918 | break; |
| 919 | default: |
| 920 | lua_pushstring(L, ""); |
| 921 | break; |
| 922 | } |
| 923 | return 1; |
| 924 | } |
| 925 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 926 | /* the following functions are used to convert an Lua type in a |
| 927 | * struct sample. This is useful to provide data from a converter |
| 928 | * to the LUA code. |
| 929 | */ |
| 930 | static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp) |
| 931 | { |
| 932 | switch (lua_type(L, ud)) { |
| 933 | |
| 934 | case LUA_TNUMBER: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 935 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 936 | smp->data.u.sint = lua_tointeger(L, ud); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 937 | break; |
| 938 | |
| 939 | |
| 940 | case LUA_TBOOLEAN: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 941 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 942 | smp->data.u.sint = lua_toboolean(L, ud); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 943 | break; |
| 944 | |
| 945 | case LUA_TSTRING: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 946 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 947 | smp->flags |= SMP_F_CONST; |
Tim Duesterhus | 2e89dec | 2019-09-29 23:03:08 +0200 | [diff] [blame] | 948 | 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] | 949 | /* 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] | 950 | 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] | 951 | smp->data.u.str.head = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 952 | break; |
| 953 | |
| 954 | case LUA_TUSERDATA: |
| 955 | case LUA_TNIL: |
| 956 | case LUA_TTABLE: |
| 957 | case LUA_TFUNCTION: |
| 958 | case LUA_TTHREAD: |
| 959 | case LUA_TLIGHTUSERDATA: |
Thierry FOURNIER | 93405e1 | 2015-08-26 14:19:03 +0200 | [diff] [blame] | 960 | case LUA_TNONE: |
| 961 | default: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 962 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 963 | smp->data.u.sint = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 964 | break; |
| 965 | } |
| 966 | return 1; |
| 967 | } |
| 968 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 969 | /* This function check the "argp" built by another conversion function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 970 | * 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] | 971 | * 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] | 972 | * |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 973 | * This function assumes that the argp argument contains ARGM_NBARGS + 1 |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 974 | * 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] | 975 | */ |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 976 | __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] | 977 | uint64_t mask, struct proxy *p) |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 978 | { |
| 979 | int min_arg; |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 980 | int idx; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 981 | struct proxy *px; |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 982 | struct userlist *ul; |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 983 | struct my_regex *reg; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 984 | const char *msg = NULL; |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 985 | char *sname, *pname, *err = NULL; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 986 | |
| 987 | idx = 0; |
| 988 | min_arg = ARGM(mask); |
| 989 | mask >>= ARGM_BITS; |
| 990 | |
| 991 | while (1) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 992 | struct buffer tmp = BUF_NULL; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 993 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 994 | /* Check for mandatory arguments. */ |
| 995 | if (argp[idx].type == ARGT_STOP) { |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 996 | if (idx < min_arg) { |
| 997 | |
| 998 | /* If miss other argument than the first one, we return an error. */ |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 999 | if (idx > 0) { |
| 1000 | msg = "Mandatory argument expected"; |
| 1001 | goto error; |
| 1002 | } |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 1003 | |
| 1004 | /* If first argument have a certain type, some default values |
| 1005 | * may be used. See the function smp_resolve_args(). |
| 1006 | */ |
| 1007 | switch (mask & ARGT_MASK) { |
| 1008 | |
| 1009 | case ARGT_FE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1010 | if (!(p->cap & PR_CAP_FE)) { |
| 1011 | msg = "Mandatory argument expected"; |
| 1012 | goto error; |
| 1013 | } |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 1014 | argp[idx].data.prx = p; |
| 1015 | argp[idx].type = ARGT_FE; |
| 1016 | argp[idx+1].type = ARGT_STOP; |
| 1017 | break; |
| 1018 | |
| 1019 | case ARGT_BE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1020 | if (!(p->cap & PR_CAP_BE)) { |
| 1021 | msg = "Mandatory argument expected"; |
| 1022 | goto error; |
| 1023 | } |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 1024 | argp[idx].data.prx = p; |
| 1025 | argp[idx].type = ARGT_BE; |
| 1026 | argp[idx+1].type = ARGT_STOP; |
| 1027 | break; |
| 1028 | |
| 1029 | case ARGT_TAB: |
Olivier Houchard | 14f6268 | 2022-09-13 00:35:53 +0200 | [diff] [blame] | 1030 | if (!p->table) { |
| 1031 | msg = "Mandatory argument expected"; |
| 1032 | goto error; |
| 1033 | } |
| 1034 | argp[idx].data.t = p->table; |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 1035 | argp[idx].type = ARGT_TAB; |
| 1036 | argp[idx+1].type = ARGT_STOP; |
| 1037 | break; |
| 1038 | |
| 1039 | default: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1040 | msg = "Mandatory argument expected"; |
| 1041 | goto error; |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 1042 | break; |
| 1043 | } |
| 1044 | } |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1045 | break; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 1046 | } |
| 1047 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1048 | /* Check for exceed the number of required argument. */ |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 1049 | if ((mask & ARGT_MASK) == ARGT_STOP && |
| 1050 | argp[idx].type != ARGT_STOP) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1051 | msg = "Last argument expected"; |
| 1052 | goto error; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 1053 | } |
| 1054 | |
| 1055 | if ((mask & ARGT_MASK) == ARGT_STOP && |
| 1056 | argp[idx].type == ARGT_STOP) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1057 | break; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 1058 | } |
| 1059 | |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1060 | /* Convert some argument types. All string in argp[] are for not |
| 1061 | * duplicated yet. |
| 1062 | */ |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 1063 | switch (mask & ARGT_MASK) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 1064 | case ARGT_SINT: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1065 | if (argp[idx].type != ARGT_SINT) { |
| 1066 | msg = "integer expected"; |
| 1067 | goto error; |
| 1068 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 1069 | argp[idx].type = ARGT_SINT; |
| 1070 | break; |
| 1071 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 1072 | case ARGT_TIME: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1073 | if (argp[idx].type != ARGT_SINT) { |
| 1074 | msg = "integer expected"; |
| 1075 | goto error; |
| 1076 | } |
Thierry FOURNIER | 29176f3 | 2015-07-07 00:41:29 +0200 | [diff] [blame] | 1077 | argp[idx].type = ARGT_TIME; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 1078 | break; |
| 1079 | |
| 1080 | case ARGT_SIZE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1081 | if (argp[idx].type != ARGT_SINT) { |
| 1082 | msg = "integer expected"; |
| 1083 | goto error; |
| 1084 | } |
Thierry FOURNIER | 29176f3 | 2015-07-07 00:41:29 +0200 | [diff] [blame] | 1085 | argp[idx].type = ARGT_SIZE; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 1086 | break; |
| 1087 | |
| 1088 | case ARGT_FE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1089 | if (argp[idx].type != ARGT_STR) { |
| 1090 | msg = "string expected"; |
| 1091 | goto error; |
| 1092 | } |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 1093 | 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] | 1094 | if (!argp[idx].data.prx) { |
| 1095 | msg = "frontend doesn't exist"; |
| 1096 | goto error; |
| 1097 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 1098 | argp[idx].type = ARGT_FE; |
| 1099 | break; |
| 1100 | |
| 1101 | case ARGT_BE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1102 | if (argp[idx].type != ARGT_STR) { |
| 1103 | msg = "string expected"; |
| 1104 | goto error; |
| 1105 | } |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 1106 | 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] | 1107 | if (!argp[idx].data.prx) { |
| 1108 | msg = "backend doesn't exist"; |
| 1109 | goto error; |
| 1110 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 1111 | argp[idx].type = ARGT_BE; |
| 1112 | break; |
| 1113 | |
| 1114 | case ARGT_TAB: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1115 | if (argp[idx].type != ARGT_STR) { |
| 1116 | msg = "string expected"; |
| 1117 | goto error; |
| 1118 | } |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 1119 | 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] | 1120 | if (!argp[idx].data.t) { |
| 1121 | msg = "table doesn't exist"; |
| 1122 | goto error; |
| 1123 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 1124 | argp[idx].type = ARGT_TAB; |
| 1125 | break; |
| 1126 | |
| 1127 | case ARGT_SRV: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1128 | if (argp[idx].type != ARGT_STR) { |
| 1129 | msg = "string expected"; |
| 1130 | goto error; |
| 1131 | } |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 1132 | sname = strrchr(argp[idx].data.str.area, '/'); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 1133 | if (sname) { |
| 1134 | *sname++ = '\0'; |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 1135 | pname = argp[idx].data.str.area; |
Willy Tarreau | 9e0bb10 | 2015-05-26 11:24:42 +0200 | [diff] [blame] | 1136 | px = proxy_be_by_name(pname); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1137 | if (!px) { |
| 1138 | msg = "backend doesn't exist"; |
| 1139 | goto error; |
| 1140 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 1141 | } |
| 1142 | else { |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 1143 | sname = argp[idx].data.str.area; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 1144 | px = p; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 1145 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 1146 | argp[idx].data.srv = findserver(px, sname); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1147 | if (!argp[idx].data.srv) { |
| 1148 | msg = "server doesn't exist"; |
| 1149 | goto error; |
| 1150 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 1151 | argp[idx].type = ARGT_SRV; |
| 1152 | break; |
| 1153 | |
| 1154 | case ARGT_IPV4: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1155 | if (argp[idx].type != ARGT_STR) { |
| 1156 | msg = "string expected"; |
| 1157 | goto error; |
| 1158 | } |
| 1159 | if (inet_pton(AF_INET, argp[idx].data.str.area, &argp[idx].data.ipv4)) { |
| 1160 | msg = "invalid IPv4 address"; |
| 1161 | goto error; |
| 1162 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 1163 | argp[idx].type = ARGT_IPV4; |
| 1164 | break; |
| 1165 | |
| 1166 | case ARGT_MSK4: |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 1167 | if (argp[idx].type == ARGT_SINT) |
| 1168 | len2mask4(argp[idx].data.sint, &argp[idx].data.ipv4); |
| 1169 | else if (argp[idx].type == ARGT_STR) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1170 | if (!str2mask(argp[idx].data.str.area, &argp[idx].data.ipv4)) { |
| 1171 | msg = "invalid IPv4 mask"; |
| 1172 | goto error; |
| 1173 | } |
| 1174 | } |
| 1175 | else { |
| 1176 | msg = "integer or string expected"; |
| 1177 | goto error; |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 1178 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 1179 | argp[idx].type = ARGT_MSK4; |
| 1180 | break; |
| 1181 | |
| 1182 | case ARGT_IPV6: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1183 | if (argp[idx].type != ARGT_STR) { |
| 1184 | msg = "string expected"; |
| 1185 | goto error; |
| 1186 | } |
| 1187 | if (inet_pton(AF_INET6, argp[idx].data.str.area, &argp[idx].data.ipv6)) { |
| 1188 | msg = "invalid IPv6 address"; |
| 1189 | goto error; |
| 1190 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 1191 | argp[idx].type = ARGT_IPV6; |
| 1192 | break; |
| 1193 | |
| 1194 | case ARGT_MSK6: |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 1195 | if (argp[idx].type == ARGT_SINT) |
| 1196 | len2mask6(argp[idx].data.sint, &argp[idx].data.ipv6); |
| 1197 | else if (argp[idx].type == ARGT_STR) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1198 | if (!str2mask6(argp[idx].data.str.area, &argp[idx].data.ipv6)) { |
| 1199 | msg = "invalid IPv6 mask"; |
| 1200 | goto error; |
| 1201 | } |
| 1202 | } |
| 1203 | else { |
| 1204 | msg = "integer or string expected"; |
| 1205 | goto error; |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 1206 | } |
Tim Duesterhus | b814da6 | 2018-01-25 16:24:50 +0100 | [diff] [blame] | 1207 | argp[idx].type = ARGT_MSK6; |
| 1208 | break; |
| 1209 | |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 1210 | case ARGT_REG: |
| 1211 | if (argp[idx].type != ARGT_STR) { |
| 1212 | msg = "string expected"; |
| 1213 | goto error; |
| 1214 | } |
| 1215 | reg = regex_comp(argp[idx].data.str.area, !(argp[idx].type_flags & ARGF_REG_ICASE), 1, &err); |
| 1216 | if (!reg) { |
| 1217 | msg = lua_pushfstring(L, "error compiling regex '%s' : '%s'", |
| 1218 | argp[idx].data.str.area, err); |
| 1219 | free(err); |
| 1220 | goto error; |
| 1221 | } |
| 1222 | argp[idx].type = ARGT_REG; |
| 1223 | argp[idx].data.reg = reg; |
| 1224 | break; |
| 1225 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 1226 | case ARGT_USR: |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 1227 | if (argp[idx].type != ARGT_STR) { |
| 1228 | msg = "string expected"; |
| 1229 | goto error; |
| 1230 | } |
| 1231 | if (p->uri_auth && p->uri_auth->userlist && |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 1232 | strcmp(p->uri_auth->userlist->name, argp[idx].data.str.area) == 0) |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 1233 | ul = p->uri_auth->userlist; |
| 1234 | else |
| 1235 | ul = auth_find_userlist(argp[idx].data.str.area); |
| 1236 | |
| 1237 | if (!ul) { |
| 1238 | msg = lua_pushfstring(L, "unable to find userlist '%s'", argp[idx].data.str.area); |
| 1239 | goto error; |
| 1240 | } |
| 1241 | argp[idx].type = ARGT_USR; |
| 1242 | argp[idx].data.usr = ul; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1243 | break; |
| 1244 | |
| 1245 | case ARGT_STR: |
| 1246 | if (!chunk_dup(&tmp, &argp[idx].data.str)) { |
| 1247 | msg = "unable to duplicate string arg"; |
| 1248 | goto error; |
| 1249 | } |
| 1250 | argp[idx].data.str = tmp; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 1251 | break; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1252 | |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 1253 | case ARGT_MAP: |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 1254 | msg = "type not yet supported"; |
| 1255 | goto error; |
| 1256 | break; |
| 1257 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 1258 | } |
| 1259 | |
| 1260 | /* Check for type of argument. */ |
| 1261 | if ((mask & ARGT_MASK) != argp[idx].type) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1262 | msg = lua_pushfstring(L, "'%s' expected, got '%s'", |
| 1263 | arg_type_names[(mask & ARGT_MASK)], |
| 1264 | arg_type_names[argp[idx].type & ARGT_MASK]); |
| 1265 | goto error; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 1266 | } |
| 1267 | |
| 1268 | /* Next argument. */ |
| 1269 | mask >>= ARGT_BITS; |
| 1270 | idx++; |
| 1271 | } |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1272 | return 0; |
| 1273 | |
| 1274 | error: |
Olivier Houchard | ca43161 | 2022-09-13 00:31:17 +0200 | [diff] [blame] | 1275 | argp[idx].type = ARGT_STOP; |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 1276 | free_args(argp); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1277 | WILL_LJMP(luaL_argerror(L, first + idx, msg)); |
| 1278 | return 0; /* Never reached */ |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 1279 | } |
| 1280 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1281 | /* |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 1282 | * The following functions are used to make correspondence between the the |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1283 | * executed lua pointer and the "struct hlua *" that contain the context. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1284 | * |
| 1285 | * - hlua_gethlua : return the hlua context associated with an lua_State. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1286 | * - hlua_sethlua : create the association between hlua context and lua_state. |
| 1287 | */ |
| 1288 | static inline struct hlua *hlua_gethlua(lua_State *L) |
| 1289 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 1290 | struct hlua **hlua = lua_getextraspace(L); |
| 1291 | return *hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1292 | } |
| 1293 | static inline void hlua_sethlua(struct hlua *hlua) |
| 1294 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 1295 | struct hlua **hlua_store = lua_getextraspace(hlua->T); |
| 1296 | *hlua_store = hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1297 | } |
| 1298 | |
Willy Tarreau | 0b7b639 | 2022-06-19 17:39:33 +0200 | [diff] [blame] | 1299 | /* Will return a non-NULL string indicating the Lua call trace if the caller |
| 1300 | * currently is executing from within a Lua function. One line per entry will |
| 1301 | * be emitted, and each extra line will be prefixed with <pfx>. If a current |
| 1302 | * Lua function is not detected, NULL is returned. |
| 1303 | */ |
| 1304 | const char *hlua_show_current_location(const char *pfx) |
| 1305 | { |
| 1306 | lua_State *L; |
| 1307 | lua_Debug ar; |
| 1308 | |
| 1309 | /* global or per-thread stack initializing ? */ |
| 1310 | if (hlua_state_id != -1 && (L = hlua_states[hlua_state_id]) && lua_getstack(L, 0, &ar)) |
| 1311 | return hlua_traceback(L, pfx); |
| 1312 | |
| 1313 | /* per-thread stack running ? */ |
| 1314 | if (hlua_states[tid + 1] && (L = hlua_states[tid + 1]) && lua_getstack(L, 0, &ar)) |
| 1315 | return hlua_traceback(L, pfx); |
| 1316 | |
| 1317 | /* global stack running ? */ |
| 1318 | if (hlua_states[0] && (L = hlua_states[0]) && lua_getstack(L, 0, &ar)) |
| 1319 | return hlua_traceback(L, pfx); |
| 1320 | |
| 1321 | return NULL; |
| 1322 | } |
| 1323 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1324 | /* This function is used to send logs. It try to send on screen (stderr) |
| 1325 | * and on the default syslog server. |
| 1326 | */ |
| 1327 | static inline void hlua_sendlog(struct proxy *px, int level, const char *msg) |
| 1328 | { |
| 1329 | struct tm tm; |
| 1330 | char *p; |
| 1331 | |
| 1332 | /* Cleanup the log message. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1333 | p = trash.area; |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1334 | for (; *msg != '\0'; msg++, p++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1335 | if (p >= trash.area + trash.size - 1) { |
Thierry FOURNIER | ccf0063 | 2015-09-16 12:47:03 +0200 | [diff] [blame] | 1336 | /* Break the message if exceed the buffer size. */ |
| 1337 | *(p-4) = ' '; |
| 1338 | *(p-3) = '.'; |
| 1339 | *(p-2) = '.'; |
| 1340 | *(p-1) = '.'; |
| 1341 | break; |
| 1342 | } |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 1343 | if (isprint((unsigned char)*msg)) |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1344 | *p = *msg; |
| 1345 | else |
| 1346 | *p = '.'; |
| 1347 | } |
| 1348 | *p = '\0'; |
| 1349 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1350 | send_log(px, level, "%s\n", trash.area); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1351 | if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) { |
Christopher Faulet | f98d821 | 2020-10-02 18:13:52 +0200 | [diff] [blame] | 1352 | if (level == LOG_DEBUG && !(global.mode & MODE_DEBUG)) |
| 1353 | return; |
| 1354 | |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 1355 | get_localtime(date.tv_sec, &tm); |
| 1356 | fprintf(stderr, "[%s] %03d/%02d%02d%02d (%d) : %s\n", |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1357 | 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] | 1358 | (int)getpid(), trash.area); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1359 | fflush(stderr); |
| 1360 | } |
| 1361 | } |
| 1362 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1363 | /* This function just ensure that the yield will be always |
| 1364 | * returned with a timeout and permit to set some flags |
| 1365 | */ |
| 1366 | __LJMP void hlua_yieldk(lua_State *L, int nresults, int ctx, |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1367 | lua_KFunction k, int timeout, unsigned int flags) |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1368 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 1369 | struct hlua *hlua; |
| 1370 | |
| 1371 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 1372 | hlua = hlua_gethlua(L); |
| 1373 | if (!hlua) { |
| 1374 | return; |
| 1375 | } |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1376 | |
| 1377 | /* Set the wake timeout. If timeout is required, we set |
| 1378 | * the expiration time. |
| 1379 | */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1380 | hlua->wake_time = timeout; |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1381 | |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 1382 | hlua->flags |= flags; |
| 1383 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1384 | /* Process the yield. */ |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 1385 | MAY_LJMP(lua_yieldk(L, nresults, ctx, k)); |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1386 | } |
| 1387 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1388 | /* This function initialises the Lua environment stored in the stream. |
| 1389 | * 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] | 1390 | * 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] | 1391 | * |
| 1392 | * This function is particular. it initialises a new Lua thread. If the |
| 1393 | * initialisation fails (example: out of memory error), the lua function |
| 1394 | * throws an error (longjmp). |
| 1395 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1396 | * This function manipulates two Lua stacks: the main and the thread. Only |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 1397 | * the main stack can fail. The thread is not manipulated. This function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1398 | * MUST NOT manipulate the created thread stack state, because it is not |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1399 | * protected against errors thrown by the thread stack. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1400 | */ |
Aurelien DARRAGON | 6b0b9bd | 2023-03-21 14:02:16 +0100 | [diff] [blame] | 1401 | 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] | 1402 | { |
| 1403 | lua->Mref = LUA_REFNIL; |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1404 | lua->flags = 0; |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 1405 | lua->gc_count = 0; |
Christopher Faulet | bc275a9 | 2020-02-26 14:55:16 +0100 | [diff] [blame] | 1406 | lua->wake_time = TICK_ETERNITY; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1407 | lua->state_id = state_id; |
Aurelien DARRAGON | da9503c | 2022-11-25 09:10:07 +0100 | [diff] [blame^] | 1408 | hlua_timer_init(&lua->timer, 0); /* default value, no timeout */ |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 1409 | LIST_INIT(&lua->com); |
Aurelien DARRAGON | 3ffbf38 | 2023-02-09 17:02:57 +0100 | [diff] [blame] | 1410 | MT_LIST_INIT(&lua->hc_list); |
Aurelien DARRAGON | 6b0b9bd | 2023-03-21 14:02:16 +0100 | [diff] [blame] | 1411 | if (!SET_SAFE_LJMP_PARENT(lua)) { |
| 1412 | lua->Tref = LUA_REFNIL; |
| 1413 | return 0; |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1414 | } |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1415 | lua->T = lua_newthread(hlua_states[state_id]); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1416 | if (!lua->T) { |
| 1417 | lua->Tref = LUA_REFNIL; |
Aurelien DARRAGON | 6b0b9bd | 2023-03-21 14:02:16 +0100 | [diff] [blame] | 1418 | RESET_SAFE_LJMP_PARENT(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1419 | return 0; |
| 1420 | } |
| 1421 | hlua_sethlua(lua); |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1422 | lua->Tref = luaL_ref(hlua_states[state_id], LUA_REGISTRYINDEX); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1423 | lua->task = task; |
Aurelien DARRAGON | 6b0b9bd | 2023-03-21 14:02:16 +0100 | [diff] [blame] | 1424 | RESET_SAFE_LJMP_PARENT(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1425 | return 1; |
| 1426 | } |
| 1427 | |
Aurelien DARRAGON | 3ffbf38 | 2023-02-09 17:02:57 +0100 | [diff] [blame] | 1428 | /* kill all associated httpclient to this hlua task |
| 1429 | * We must take extra precautions as we're manipulating lua-exposed |
| 1430 | * objects without the main lua lock. |
| 1431 | */ |
William Lallemand | bb58142 | 2022-10-20 10:57:28 +0200 | [diff] [blame] | 1432 | static void hlua_httpclient_destroy_all(struct hlua *hlua) |
| 1433 | { |
Aurelien DARRAGON | 3ffbf38 | 2023-02-09 17:02:57 +0100 | [diff] [blame] | 1434 | struct hlua_httpclient *hlua_hc; |
William Lallemand | bb58142 | 2022-10-20 10:57:28 +0200 | [diff] [blame] | 1435 | |
Aurelien DARRAGON | 3ffbf38 | 2023-02-09 17:02:57 +0100 | [diff] [blame] | 1436 | /* use thread-safe accessors for hc_list since GC cycle initiated by |
| 1437 | * another thread sharing the same main lua stack (lua coroutine) |
| 1438 | * could execute hlua_httpclient_gc() on the hlua->hc_list items |
| 1439 | * in parallel: Lua GC applies on the main stack, it is not limited to |
| 1440 | * a single coroutine stack, see Github issue #2037 for reference. |
| 1441 | * Remember, coroutines created using lua_newthread() are not meant to |
| 1442 | * be thread safe in Lua. (From lua co-author: |
| 1443 | * http://lua-users.org/lists/lua-l/2011-07/msg00072.html) |
| 1444 | * |
| 1445 | * This security measure is superfluous when 'lua-load-per-thread' is used |
| 1446 | * since in this case coroutines exclusively run on the same thread |
| 1447 | * (main stack is not shared between OS threads). |
| 1448 | */ |
| 1449 | while ((hlua_hc = MT_LIST_POP(&hlua->hc_list, typeof(hlua_hc), by_hlua))) { |
| 1450 | httpclient_stop_and_destroy(hlua_hc->hc); |
William Lallemand | bb58142 | 2022-10-20 10:57:28 +0200 | [diff] [blame] | 1451 | hlua_hc->hc = NULL; |
William Lallemand | bb58142 | 2022-10-20 10:57:28 +0200 | [diff] [blame] | 1452 | } |
| 1453 | } |
| 1454 | |
| 1455 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1456 | /* Used to destroy the Lua coroutine when the attached stream or task |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1457 | * is destroyed. The destroy also the memory context. The struct "lua" |
Aurelien DARRAGON | 60ab0f7 | 2023-03-01 16:45:50 +0100 | [diff] [blame] | 1458 | * will be freed. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1459 | */ |
| 1460 | void hlua_ctx_destroy(struct hlua *lua) |
| 1461 | { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 1462 | if (!lua) |
Thierry FOURNIER | a718b29 | 2015-03-04 16:48:34 +0100 | [diff] [blame] | 1463 | return; |
| 1464 | |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 1465 | if (!lua->T) |
| 1466 | goto end; |
| 1467 | |
William Lallemand | bb58142 | 2022-10-20 10:57:28 +0200 | [diff] [blame] | 1468 | /* clean all running httpclient */ |
| 1469 | hlua_httpclient_destroy_all(lua); |
| 1470 | |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 1471 | /* Purge all the pending signals. */ |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1472 | notification_purge(&lua->com); |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 1473 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1474 | if (!SET_SAFE_LJMP(lua)) |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 1475 | return; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1476 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1477 | RESET_SAFE_LJMP(lua); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 1478 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1479 | if (!SET_SAFE_LJMP_PARENT(lua)) |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 1480 | return; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1481 | luaL_unref(hlua_states[lua->state_id], LUA_REGISTRYINDEX, lua->Tref); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1482 | RESET_SAFE_LJMP_PARENT(lua); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 1483 | /* Forces a garbage collecting process. If the Lua program is finished |
| 1484 | * without error, we run the GC on the thread pointer. Its freed all |
| 1485 | * the unused memory. |
| 1486 | * If the thread is finnish with an error or is currently yielded, |
| 1487 | * it seems that the GC applied on the thread doesn't clean anything, |
| 1488 | * so e run the GC on the main thread. |
| 1489 | * NOTE: maybe this action locks all the Lua threads untiml the en of |
| 1490 | * the garbage collection. |
| 1491 | */ |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 1492 | if (lua->gc_count) { |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1493 | if (!SET_SAFE_LJMP_PARENT(lua)) |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 1494 | return; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1495 | lua_gc(hlua_states[lua->state_id], LUA_GCCOLLECT, 0); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1496 | RESET_SAFE_LJMP_PARENT(lua); |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 1497 | } |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 1498 | |
Thierry FOURNIER | a7b536b | 2015-09-21 22:50:24 +0200 | [diff] [blame] | 1499 | lua->T = NULL; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 1500 | |
| 1501 | end: |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 1502 | pool_free(pool_head_hlua, lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1503 | } |
| 1504 | |
| 1505 | /* This function is used to restore the Lua context when a coroutine |
| 1506 | * fails. This function copy the common memory between old coroutine |
| 1507 | * and the new coroutine. The old coroutine is destroyed, and its |
| 1508 | * replaced by the new coroutine. |
| 1509 | * If the flag "keep_msg" is set, the last entry of the old is assumed |
| 1510 | * as string error message and it is copied in the new stack. |
| 1511 | */ |
| 1512 | static int hlua_ctx_renew(struct hlua *lua, int keep_msg) |
| 1513 | { |
| 1514 | lua_State *T; |
| 1515 | int new_ref; |
| 1516 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1517 | /* New Lua coroutine. */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1518 | T = lua_newthread(hlua_states[lua->state_id]); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1519 | if (!T) |
| 1520 | return 0; |
| 1521 | |
| 1522 | /* Copy last error message. */ |
| 1523 | if (keep_msg) |
| 1524 | lua_xmove(lua->T, T, 1); |
| 1525 | |
| 1526 | /* Copy data between the coroutines. */ |
| 1527 | lua_rawgeti(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 1528 | lua_xmove(lua->T, T, 1); |
Ilya Shipitsin | 46a030c | 2020-07-05 16:36:08 +0500 | [diff] [blame] | 1529 | new_ref = luaL_ref(T, LUA_REGISTRYINDEX); /* Value popped. */ |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1530 | |
| 1531 | /* Destroy old data. */ |
| 1532 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 1533 | |
| 1534 | /* The thread is garbage collected by Lua. */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1535 | luaL_unref(hlua_states[lua->state_id], LUA_REGISTRYINDEX, lua->Tref); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1536 | |
| 1537 | /* Fill the struct with the new coroutine values. */ |
| 1538 | lua->Mref = new_ref; |
| 1539 | lua->T = T; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1540 | lua->Tref = luaL_ref(hlua_states[lua->state_id], LUA_REGISTRYINDEX); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1541 | |
| 1542 | /* Set context. */ |
| 1543 | hlua_sethlua(lua); |
| 1544 | |
| 1545 | return 1; |
| 1546 | } |
| 1547 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1548 | void hlua_hook(lua_State *L, lua_Debug *ar) |
| 1549 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 1550 | struct hlua *hlua; |
| 1551 | |
| 1552 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 1553 | hlua = hlua_gethlua(L); |
| 1554 | if (!hlua) |
| 1555 | return; |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1556 | |
| 1557 | /* Lua cannot yield when its returning from a function, |
| 1558 | * so, we can fix the interrupt hook to 1 instruction, |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1559 | * expecting that the function is finished. |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1560 | */ |
| 1561 | if (lua_gethookmask(L) & LUA_MASKRET) { |
| 1562 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, 1); |
| 1563 | return; |
| 1564 | } |
| 1565 | |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1566 | /* If we interrupt the Lua processing in yieldable state, we yield. |
| 1567 | * If the state is not yieldable, trying yield causes an error. |
| 1568 | */ |
Aurelien DARRAGON | 0ebd41f | 2022-11-24 09:51:40 +0100 | [diff] [blame] | 1569 | if (lua_isyieldable(L)) { |
| 1570 | /* note: for converters/fetches.. where yielding is not allowed |
| 1571 | * hlua_ctx_resume() will simply perform a goto resume_execution |
| 1572 | * instead of rescheduling hlua->task. |
| 1573 | * also: hlua_ctx_resume() will take care of checking execution |
| 1574 | * timeout and re-applying the hook as needed. |
| 1575 | */ |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 1576 | MAY_LJMP(hlua_yieldk(L, 0, 0, NULL, TICK_ETERNITY, HLUA_CTRLYIELD)); |
Aurelien DARRAGON | 0ebd41f | 2022-11-24 09:51:40 +0100 | [diff] [blame] | 1577 | /* lua docs says that the hook should return immediately after lua_yieldk |
| 1578 | * |
| 1579 | * From: https://www.lua.org/manual/5.3/manual.html#lua_yieldk |
| 1580 | * |
| 1581 | * Moreover, it seems that we don't want to continue after the yield |
| 1582 | * because the end of the function is about handling unyieldable function, |
| 1583 | * which is not the case here. |
| 1584 | * |
| 1585 | * ->if we don't return lua_sethook gets incorrectly set with MASKRET later |
| 1586 | * in the function. |
| 1587 | */ |
| 1588 | return; |
| 1589 | } |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1590 | |
Aurelien DARRAGON | da9503c | 2022-11-25 09:10:07 +0100 | [diff] [blame^] | 1591 | /* If we cannot yield, check the timeout. */ |
| 1592 | if (!hlua_timer_check(&hlua->timer)) { |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1593 | lua_pushfstring(L, "execution timeout"); |
| 1594 | WILL_LJMP(lua_error(L)); |
| 1595 | } |
| 1596 | |
| 1597 | /* Try to interrupt the process at the end of the current |
| 1598 | * unyieldable function. |
| 1599 | */ |
| 1600 | 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] | 1601 | } |
| 1602 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1603 | /* This function start or resumes the Lua stack execution. If the flag |
| 1604 | * "yield_allowed" if no set and the LUA stack execution returns a yield |
| 1605 | * The function return an error. |
| 1606 | * |
| 1607 | * The function can returns 4 values: |
| 1608 | * - HLUA_E_OK : The execution is terminated without any errors. |
| 1609 | * - HLUA_E_AGAIN : The execution must continue at the next associated |
| 1610 | * task wakeup. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1611 | * - 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] | 1612 | * the top of the stack. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1613 | * - HLUA_E_ERR : An error has occurred without error message. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1614 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1615 | * 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] | 1616 | * LUA code. |
| 1617 | */ |
| 1618 | static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed) |
| 1619 | { |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1620 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
| 1621 | int nres; |
| 1622 | #endif |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1623 | int ret; |
| 1624 | const char *msg; |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1625 | const char *trace; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1626 | |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1627 | /* Lock the whole Lua execution. This lock must be before the |
| 1628 | * label "resume_execution". |
| 1629 | */ |
Aurelien DARRAGON | e36f803 | 2023-03-21 13:22:33 +0100 | [diff] [blame] | 1630 | hlua_lock(lua); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1631 | |
Aurelien DARRAGON | da9503c | 2022-11-25 09:10:07 +0100 | [diff] [blame^] | 1632 | /* reset the timer as we might be re-entering the function to |
| 1633 | * resume the coroutine after a successful yield |
| 1634 | * (cumulative time will be updated) |
| 1635 | */ |
| 1636 | hlua_timer_reset(&lua->timer); |
| 1637 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1638 | resume_execution: |
| 1639 | |
| 1640 | /* This hook interrupts the Lua processing each 'hlua_nb_instruction' |
| 1641 | * instructions. it is used for preventing infinite loops. |
| 1642 | */ |
| 1643 | lua_sethook(lua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 1644 | |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1645 | /* Remove all flags except the running flags. */ |
Thierry FOURNIER | 2f3867f | 2015-09-28 01:02:01 +0200 | [diff] [blame] | 1646 | HLUA_SET_RUN(lua); |
| 1647 | HLUA_CLR_CTRLYIELD(lua); |
| 1648 | HLUA_CLR_WAKERESWR(lua); |
| 1649 | HLUA_CLR_WAKEREQWR(lua); |
Christopher Faulet | 1f43a34 | 2021-08-04 17:58:21 +0200 | [diff] [blame] | 1650 | HLUA_CLR_NOYIELD(lua); |
| 1651 | if (!yield_allowed) |
| 1652 | HLUA_SET_NOYIELD(lua); |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1653 | |
Aurelien DARRAGON | da9503c | 2022-11-25 09:10:07 +0100 | [diff] [blame^] | 1654 | /* reset wake_time. */ |
Christopher Faulet | bc275a9 | 2020-02-26 14:55:16 +0100 | [diff] [blame] | 1655 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1656 | |
Aurelien DARRAGON | da9503c | 2022-11-25 09:10:07 +0100 | [diff] [blame^] | 1657 | /* start the timer as we're about to start lua processing */ |
| 1658 | hlua_timer_start(&lua->timer); |
| 1659 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1660 | /* Call the function. */ |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1661 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1662 | 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] | 1663 | #else |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1664 | ret = lua_resume(lua->T, hlua_states[lua->state_id], lua->nargs); |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1665 | #endif |
Aurelien DARRAGON | da9503c | 2022-11-25 09:10:07 +0100 | [diff] [blame^] | 1666 | |
| 1667 | /* out of lua processing, stop the timer */ |
| 1668 | hlua_timer_stop(&lua->timer); |
| 1669 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1670 | switch (ret) { |
| 1671 | |
| 1672 | case LUA_OK: |
| 1673 | ret = HLUA_E_OK; |
| 1674 | break; |
| 1675 | |
| 1676 | case LUA_YIELD: |
Aurelien DARRAGON | da9503c | 2022-11-25 09:10:07 +0100 | [diff] [blame^] | 1677 | /* 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] | 1678 | * break the Lua execution. |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1679 | */ |
Aurelien DARRAGON | da9503c | 2022-11-25 09:10:07 +0100 | [diff] [blame^] | 1680 | if (!hlua_timer_check(&lua->timer)) { |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1681 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1682 | ret = HLUA_E_ETMOUT; |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1683 | break; |
| 1684 | } |
| 1685 | /* Process the forced yield. if the general yield is not allowed or |
| 1686 | * if no task were associated this the current Lua execution |
| 1687 | * coroutine, we resume the execution. Else we want to return in the |
| 1688 | * scheduler and we want to be waked up again, to continue the |
| 1689 | * current Lua execution. So we schedule our own task. |
| 1690 | */ |
| 1691 | if (HLUA_IS_CTRLYIELDING(lua)) { |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1692 | if (!yield_allowed || !lua->task) |
| 1693 | goto resume_execution; |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1694 | task_wakeup(lua->task, TASK_WOKEN_MSG); |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1695 | } |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1696 | if (!yield_allowed) { |
| 1697 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1698 | ret = HLUA_E_YIELD; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1699 | break; |
| 1700 | } |
| 1701 | ret = HLUA_E_AGAIN; |
| 1702 | break; |
| 1703 | |
| 1704 | case LUA_ERRRUN: |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1705 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1706 | /* Special exit case. The traditional exit is returned as an error |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1707 | * because the errors ares the only one mean to return immediately |
| 1708 | * from and lua execution. |
| 1709 | */ |
| 1710 | if (lua->flags & HLUA_EXIT) { |
| 1711 | ret = HLUA_E_OK; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 1712 | hlua_ctx_renew(lua, 1); |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1713 | break; |
| 1714 | } |
| 1715 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1716 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1717 | if (!lua_checkstack(lua->T, 1)) { |
| 1718 | ret = HLUA_E_ERR; |
| 1719 | break; |
| 1720 | } |
| 1721 | msg = lua_tostring(lua->T, -1); |
| 1722 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1723 | lua_pop(lua->T, 1); |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 1724 | trace = hlua_traceback(lua->T, ", "); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1725 | if (msg) |
Thierry Fournier | 46278ff | 2020-11-29 11:48:12 +0100 | [diff] [blame] | 1726 | 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] | 1727 | else |
Thierry Fournier | 46278ff | 2020-11-29 11:48:12 +0100 | [diff] [blame] | 1728 | 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] | 1729 | ret = HLUA_E_ERRMSG; |
| 1730 | break; |
| 1731 | |
| 1732 | case LUA_ERRMEM: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1733 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1734 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1735 | ret = HLUA_E_NOMEM; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1736 | break; |
| 1737 | |
| 1738 | case LUA_ERRERR: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1739 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1740 | if (!lua_checkstack(lua->T, 1)) { |
| 1741 | ret = HLUA_E_ERR; |
| 1742 | break; |
| 1743 | } |
| 1744 | msg = lua_tostring(lua->T, -1); |
| 1745 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1746 | lua_pop(lua->T, 1); |
| 1747 | if (msg) |
Thierry Fournier | 46278ff | 2020-11-29 11:48:12 +0100 | [diff] [blame] | 1748 | 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] | 1749 | else |
Thierry Fournier | 46278ff | 2020-11-29 11:48:12 +0100 | [diff] [blame] | 1750 | 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] | 1751 | ret = HLUA_E_ERRMSG; |
| 1752 | break; |
| 1753 | |
| 1754 | default: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1755 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1756 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1757 | ret = HLUA_E_ERR; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1758 | break; |
| 1759 | } |
| 1760 | |
| 1761 | switch (ret) { |
| 1762 | case HLUA_E_AGAIN: |
| 1763 | break; |
| 1764 | |
| 1765 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1766 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1767 | hlua_ctx_renew(lua, 1); |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1768 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1769 | break; |
| 1770 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1771 | case HLUA_E_ETMOUT: |
| 1772 | case HLUA_E_NOMEM: |
| 1773 | case HLUA_E_YIELD: |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1774 | case HLUA_E_ERR: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1775 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1776 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1777 | hlua_ctx_renew(lua, 0); |
| 1778 | break; |
| 1779 | |
| 1780 | case HLUA_E_OK: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1781 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1782 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1783 | break; |
| 1784 | } |
| 1785 | |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1786 | /* This is the main exit point, remove the Lua lock. */ |
Aurelien DARRAGON | e36f803 | 2023-03-21 13:22:33 +0100 | [diff] [blame] | 1787 | hlua_unlock(lua); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1788 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1789 | return ret; |
| 1790 | } |
| 1791 | |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1792 | /* This function exit the current code. */ |
| 1793 | __LJMP static int hlua_done(lua_State *L) |
| 1794 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 1795 | struct hlua *hlua; |
| 1796 | |
| 1797 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 1798 | hlua = hlua_gethlua(L); |
| 1799 | if (!hlua) |
| 1800 | return 0; |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1801 | |
| 1802 | hlua->flags |= HLUA_EXIT; |
| 1803 | WILL_LJMP(lua_error(L)); |
| 1804 | |
| 1805 | return 0; |
| 1806 | } |
| 1807 | |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1808 | /* This function is an LUA binding. It provides a function |
| 1809 | * for deleting ACL from a referenced ACL file. |
| 1810 | */ |
| 1811 | __LJMP static int hlua_del_acl(lua_State *L) |
| 1812 | { |
| 1813 | const char *name; |
| 1814 | const char *key; |
| 1815 | struct pat_ref *ref; |
| 1816 | |
| 1817 | MAY_LJMP(check_args(L, 2, "del_acl")); |
| 1818 | |
| 1819 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1820 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1821 | |
| 1822 | ref = pat_ref_lookup(name); |
| 1823 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1824 | WILL_LJMP(luaL_error(L, "'del_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1825 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1826 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1827 | pat_ref_delete(ref, key); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1828 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1829 | return 0; |
| 1830 | } |
| 1831 | |
| 1832 | /* This function is an LUA binding. It provides a function |
| 1833 | * for deleting map entry from a referenced map file. |
| 1834 | */ |
| 1835 | static int hlua_del_map(lua_State *L) |
| 1836 | { |
| 1837 | const char *name; |
| 1838 | const char *key; |
| 1839 | struct pat_ref *ref; |
| 1840 | |
| 1841 | MAY_LJMP(check_args(L, 2, "del_map")); |
| 1842 | |
| 1843 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1844 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1845 | |
| 1846 | ref = pat_ref_lookup(name); |
| 1847 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1848 | WILL_LJMP(luaL_error(L, "'del_map': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1849 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1850 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1851 | pat_ref_delete(ref, key); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1852 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1853 | return 0; |
| 1854 | } |
| 1855 | |
| 1856 | /* This function is an LUA binding. It provides a function |
| 1857 | * for adding ACL pattern from a referenced ACL file. |
| 1858 | */ |
| 1859 | static int hlua_add_acl(lua_State *L) |
| 1860 | { |
| 1861 | const char *name; |
| 1862 | const char *key; |
| 1863 | struct pat_ref *ref; |
| 1864 | |
| 1865 | MAY_LJMP(check_args(L, 2, "add_acl")); |
| 1866 | |
| 1867 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1868 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1869 | |
| 1870 | ref = pat_ref_lookup(name); |
| 1871 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1872 | WILL_LJMP(luaL_error(L, "'add_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1873 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1874 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1875 | if (pat_ref_find_elt(ref, key) == NULL) |
| 1876 | pat_ref_add(ref, key, NULL, NULL); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1877 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1878 | return 0; |
| 1879 | } |
| 1880 | |
| 1881 | /* This function is an LUA binding. It provides a function |
| 1882 | * for setting map pattern and sample from a referenced map |
| 1883 | * file. |
| 1884 | */ |
| 1885 | static int hlua_set_map(lua_State *L) |
| 1886 | { |
| 1887 | const char *name; |
| 1888 | const char *key; |
| 1889 | const char *value; |
| 1890 | struct pat_ref *ref; |
| 1891 | |
| 1892 | MAY_LJMP(check_args(L, 3, "set_map")); |
| 1893 | |
| 1894 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1895 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1896 | value = MAY_LJMP(luaL_checkstring(L, 3)); |
| 1897 | |
| 1898 | ref = pat_ref_lookup(name); |
| 1899 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1900 | WILL_LJMP(luaL_error(L, "'set_map': unknown map file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1901 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1902 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1903 | if (pat_ref_find_elt(ref, key) != NULL) |
| 1904 | pat_ref_set(ref, key, value, NULL); |
| 1905 | else |
| 1906 | pat_ref_add(ref, key, value, NULL); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1907 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1908 | return 0; |
| 1909 | } |
| 1910 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1911 | /* A class is a lot of memory that contain data. This data can be a table, |
| 1912 | * 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] | 1913 | * metatable have an original version registered in the global context with |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1914 | * the name of the object (_G[<name>] = <metable> ). |
| 1915 | * |
| 1916 | * A metable is a table that modify the standard behavior of a standard |
| 1917 | * access to the associated data. The entries of this new metatable are |
| 1918 | * defined as is: |
| 1919 | * |
| 1920 | * http://lua-users.org/wiki/MetatableEvents |
| 1921 | * |
| 1922 | * __index |
| 1923 | * |
| 1924 | * we access an absent field in a table, the result is nil. This is |
| 1925 | * true, but it is not the whole truth. Actually, such access triggers |
| 1926 | * the interpreter to look for an __index metamethod: If there is no |
| 1927 | * such method, as usually happens, then the access results in nil; |
| 1928 | * otherwise, the metamethod will provide the result. |
| 1929 | * |
| 1930 | * Control 'prototype' inheritance. When accessing "myTable[key]" and |
| 1931 | * the key does not appear in the table, but the metatable has an __index |
| 1932 | * property: |
| 1933 | * |
| 1934 | * - if the value is a function, the function is called, passing in the |
| 1935 | * table and the key; the return value of that function is returned as |
| 1936 | * the result. |
| 1937 | * |
| 1938 | * - if the value is another table, the value of the key in that table is |
| 1939 | * asked for and returned (and if it doesn't exist in that table, but that |
| 1940 | * table's metatable has an __index property, then it continues on up) |
| 1941 | * |
| 1942 | * - Use "rawget(myTable,key)" to skip this metamethod. |
| 1943 | * |
| 1944 | * http://www.lua.org/pil/13.4.1.html |
| 1945 | * |
| 1946 | * __newindex |
| 1947 | * |
| 1948 | * Like __index, but control property assignment. |
| 1949 | * |
| 1950 | * __mode - Control weak references. A string value with one or both |
| 1951 | * of the characters 'k' and 'v' which specifies that the the |
| 1952 | * keys and/or values in the table are weak references. |
| 1953 | * |
| 1954 | * __call - Treat a table like a function. When a table is followed by |
| 1955 | * parenthesis such as "myTable( 'foo' )" and the metatable has |
| 1956 | * a __call key pointing to a function, that function is invoked |
| 1957 | * (passing any specified arguments) and the return value is |
| 1958 | * returned. |
| 1959 | * |
| 1960 | * __metatable - Hide the metatable. When "getmetatable( myTable )" is |
| 1961 | * called, if the metatable for myTable has a __metatable |
| 1962 | * key, the value of that key is returned instead of the |
| 1963 | * actual metatable. |
| 1964 | * |
| 1965 | * __tostring - Control string representation. When the builtin |
| 1966 | * "tostring( myTable )" function is called, if the metatable |
| 1967 | * for myTable has a __tostring property set to a function, |
| 1968 | * that function is invoked (passing myTable to it) and the |
| 1969 | * return value is used as the string representation. |
| 1970 | * |
| 1971 | * __len - Control table length. When the table length is requested using |
| 1972 | * the length operator ( '#' ), if the metatable for myTable has |
| 1973 | * a __len key pointing to a function, that function is invoked |
| 1974 | * (passing myTable to it) and the return value used as the value |
| 1975 | * of "#myTable". |
| 1976 | * |
| 1977 | * __gc - Userdata finalizer code. When userdata is set to be garbage |
| 1978 | * collected, if the metatable has a __gc field pointing to a |
| 1979 | * function, that function is first invoked, passing the userdata |
| 1980 | * to it. The __gc metamethod is not called for tables. |
| 1981 | * (See http://lua-users.org/lists/lua-l/2006-11/msg00508.html) |
| 1982 | * |
| 1983 | * Special metamethods for redefining standard operators: |
| 1984 | * http://www.lua.org/pil/13.1.html |
| 1985 | * |
| 1986 | * __add "+" |
| 1987 | * __sub "-" |
| 1988 | * __mul "*" |
| 1989 | * __div "/" |
| 1990 | * __unm "!" |
| 1991 | * __pow "^" |
| 1992 | * __concat ".." |
| 1993 | * |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1994 | * Special methods for redefining standard relations |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1995 | * http://www.lua.org/pil/13.2.html |
| 1996 | * |
| 1997 | * __eq "==" |
| 1998 | * __lt "<" |
| 1999 | * __le "<=" |
| 2000 | */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2001 | |
| 2002 | /* |
| 2003 | * |
| 2004 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2005 | * Class Map |
| 2006 | * |
| 2007 | * |
| 2008 | */ |
| 2009 | |
| 2010 | /* Returns a struct hlua_map if the stack entry "ud" is |
| 2011 | * a class session, otherwise it throws an error. |
| 2012 | */ |
| 2013 | __LJMP static struct map_descriptor *hlua_checkmap(lua_State *L, int ud) |
| 2014 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 2015 | return MAY_LJMP(hlua_checkudata(L, ud, class_map_ref)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2016 | } |
| 2017 | |
| 2018 | /* This function is the map constructor. It don't need |
| 2019 | * the class Map object. It creates and return a new Map |
| 2020 | * object. It must be called only during "body" or "init" |
| 2021 | * context because it process some filesystem accesses. |
| 2022 | */ |
| 2023 | __LJMP static int hlua_map_new(struct lua_State *L) |
| 2024 | { |
| 2025 | const char *fn; |
| 2026 | int match = PAT_MATCH_STR; |
| 2027 | struct sample_conv conv; |
| 2028 | const char *file = ""; |
| 2029 | int line = 0; |
| 2030 | lua_Debug ar; |
| 2031 | char *err = NULL; |
| 2032 | struct arg args[2]; |
| 2033 | |
| 2034 | if (lua_gettop(L) < 1 || lua_gettop(L) > 2) |
| 2035 | WILL_LJMP(luaL_error(L, "'new' needs at least 1 argument.")); |
| 2036 | |
| 2037 | fn = MAY_LJMP(luaL_checkstring(L, 1)); |
| 2038 | |
| 2039 | if (lua_gettop(L) >= 2) { |
| 2040 | match = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 2041 | if (match < 0 || match >= PAT_MATCH_NUM) |
| 2042 | WILL_LJMP(luaL_error(L, "'new' needs a valid match method.")); |
| 2043 | } |
| 2044 | |
| 2045 | /* Get Lua filename and line number. */ |
| 2046 | if (lua_getstack(L, 1, &ar)) { /* check function at level */ |
| 2047 | lua_getinfo(L, "Sl", &ar); /* get info about it */ |
| 2048 | if (ar.currentline > 0) { /* is there info? */ |
| 2049 | file = ar.short_src; |
| 2050 | line = ar.currentline; |
| 2051 | } |
| 2052 | } |
| 2053 | |
| 2054 | /* fill fake sample_conv struct. */ |
| 2055 | conv.kw = ""; /* unused. */ |
| 2056 | conv.process = NULL; /* unused. */ |
| 2057 | conv.arg_mask = 0; /* unused. */ |
| 2058 | conv.val_args = NULL; /* unused. */ |
| 2059 | conv.out_type = SMP_T_STR; |
| 2060 | conv.private = (void *)(long)match; |
| 2061 | switch (match) { |
| 2062 | case PAT_MATCH_STR: conv.in_type = SMP_T_STR; break; |
| 2063 | case PAT_MATCH_BEG: conv.in_type = SMP_T_STR; break; |
| 2064 | case PAT_MATCH_SUB: conv.in_type = SMP_T_STR; break; |
| 2065 | case PAT_MATCH_DIR: conv.in_type = SMP_T_STR; break; |
| 2066 | case PAT_MATCH_DOM: conv.in_type = SMP_T_STR; break; |
| 2067 | case PAT_MATCH_END: conv.in_type = SMP_T_STR; break; |
| 2068 | case PAT_MATCH_REG: conv.in_type = SMP_T_STR; break; |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 2069 | case PAT_MATCH_INT: conv.in_type = SMP_T_SINT; break; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2070 | case PAT_MATCH_IP: conv.in_type = SMP_T_ADDR; break; |
| 2071 | default: |
| 2072 | WILL_LJMP(luaL_error(L, "'new' doesn't support this match mode.")); |
| 2073 | } |
| 2074 | |
| 2075 | /* fill fake args. */ |
| 2076 | args[0].type = ARGT_STR; |
Christopher Faulet | 73292e9 | 2020-08-06 08:40:09 +0200 | [diff] [blame] | 2077 | args[0].data.str.area = strdup(fn); |
| 2078 | args[0].data.str.data = strlen(fn); |
| 2079 | args[0].data.str.size = args[0].data.str.data+1; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2080 | args[1].type = ARGT_STOP; |
| 2081 | |
| 2082 | /* load the map. */ |
| 2083 | if (!sample_load_map(args, &conv, file, line, &err)) { |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 2084 | /* error case: we can't use luaL_error because we must |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2085 | * free the err variable. |
| 2086 | */ |
| 2087 | luaL_where(L, 1); |
| 2088 | lua_pushfstring(L, "'new': %s.", err); |
| 2089 | lua_concat(L, 2); |
| 2090 | free(err); |
Christopher Faulet | 6ad7df4 | 2020-08-07 11:45:18 +0200 | [diff] [blame] | 2091 | chunk_destroy(&args[0].data.str); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2092 | WILL_LJMP(lua_error(L)); |
| 2093 | } |
| 2094 | |
| 2095 | /* create the lua object. */ |
| 2096 | lua_newtable(L); |
| 2097 | lua_pushlightuserdata(L, args[0].data.map); |
| 2098 | lua_rawseti(L, -2, 0); |
| 2099 | |
| 2100 | /* Pop a class Map metatable and affect it to the userdata. */ |
| 2101 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_map_ref); |
| 2102 | lua_setmetatable(L, -2); |
| 2103 | |
| 2104 | |
| 2105 | return 1; |
| 2106 | } |
| 2107 | |
| 2108 | __LJMP static inline int _hlua_map_lookup(struct lua_State *L, int str) |
| 2109 | { |
| 2110 | struct map_descriptor *desc; |
| 2111 | struct pattern *pat; |
| 2112 | struct sample smp; |
| 2113 | |
| 2114 | MAY_LJMP(check_args(L, 2, "lookup")); |
| 2115 | desc = MAY_LJMP(hlua_checkmap(L, 1)); |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 2116 | if (desc->pat.expect_type == SMP_T_SINT) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 2117 | smp.data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 2118 | smp.data.u.sint = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2119 | } |
| 2120 | else { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 2121 | smp.data.type = SMP_T_STR; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2122 | smp.flags = SMP_F_CONST; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2123 | 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] | 2124 | smp.data.u.str.size = smp.data.u.str.data + 1; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2125 | } |
| 2126 | |
| 2127 | pat = pattern_exec_match(&desc->pat, &smp, 1); |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 2128 | if (!pat || !pat->data) { |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2129 | if (str) |
| 2130 | lua_pushstring(L, ""); |
| 2131 | else |
| 2132 | lua_pushnil(L); |
| 2133 | return 1; |
| 2134 | } |
| 2135 | |
| 2136 | /* 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] | 2137 | 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] | 2138 | return 1; |
| 2139 | } |
| 2140 | |
| 2141 | __LJMP static int hlua_map_lookup(struct lua_State *L) |
| 2142 | { |
| 2143 | return _hlua_map_lookup(L, 0); |
| 2144 | } |
| 2145 | |
| 2146 | __LJMP static int hlua_map_slookup(struct lua_State *L) |
| 2147 | { |
| 2148 | return _hlua_map_lookup(L, 1); |
| 2149 | } |
| 2150 | |
| 2151 | /* |
| 2152 | * |
| 2153 | * |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2154 | * Class Socket |
| 2155 | * |
| 2156 | * |
| 2157 | */ |
| 2158 | |
| 2159 | __LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud) |
| 2160 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 2161 | return MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2162 | } |
| 2163 | |
| 2164 | /* 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] | 2165 | * connection. It is used for notify space available to send or data |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2166 | * received. |
| 2167 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 2168 | static void hlua_socket_handler(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2169 | { |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2170 | struct hlua_csk_ctx *ctx = appctx->svcctx; |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 2171 | struct stconn *sc = appctx_sc(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2172 | |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 2173 | if (unlikely(se_fl_test(appctx->sedesc, (SE_FL_EOS|SE_FL_ERROR|SE_FL_SHR|SE_FL_SHW)))) { |
| 2174 | notification_wake(&ctx->wake_on_read); |
| 2175 | notification_wake(&ctx->wake_on_write); |
| 2176 | return; |
| 2177 | } |
| 2178 | |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2179 | if (ctx->die) { |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 2180 | se_fl_set(appctx->sedesc, SE_FL_EOI|SE_FL_EOS); |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2181 | notification_wake(&ctx->wake_on_read); |
| 2182 | notification_wake(&ctx->wake_on_write); |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 2183 | return; |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 2184 | } |
| 2185 | |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 2186 | /* If we can't write, wakeup the pending write signals. */ |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 2187 | if (channel_output_closed(sc_ic(sc))) |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2188 | notification_wake(&ctx->wake_on_write); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 2189 | |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 2190 | /* If we can't read, wakeup the pending read signals. */ |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 2191 | if (channel_input_closed(sc_oc(sc))) |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2192 | notification_wake(&ctx->wake_on_read); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 2193 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2194 | /* if the connection is not established, inform the stream that we want |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 2195 | * to be notified whenever the connection completes. |
| 2196 | */ |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 2197 | if (sc_opposite(sc)->state < SC_ST_EST) { |
Willy Tarreau | 90e8b45 | 2022-05-25 18:21:43 +0200 | [diff] [blame] | 2198 | applet_need_more_data(appctx); |
Willy Tarreau | b23edc8 | 2022-05-24 16:49:03 +0200 | [diff] [blame] | 2199 | se_need_remote_conn(appctx->sedesc); |
Willy Tarreau | 4164eb9 | 2022-05-25 15:42:03 +0200 | [diff] [blame] | 2200 | applet_have_more_data(appctx); |
Willy Tarreau | d4da196 | 2015-04-20 01:31:23 +0200 | [diff] [blame] | 2201 | return; |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 2202 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2203 | |
| 2204 | /* This function is called after the connect. */ |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2205 | ctx->connected = 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2206 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2207 | /* 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] | 2208 | if (channel_may_recv(sc_ic(sc))) |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2209 | notification_wake(&ctx->wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2210 | |
| 2211 | /* Wake the tasks which wants to read if the buffer contains data. */ |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 2212 | if (!channel_is_empty(sc_oc(sc))) |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2213 | notification_wake(&ctx->wake_on_read); |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 2214 | |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 2215 | /* If write notifications are registered, we considers we want |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 2216 | * to write, so we clear the blocking flag. |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 2217 | */ |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2218 | if (notification_registered(&ctx->wake_on_write)) |
Willy Tarreau | 4164eb9 | 2022-05-25 15:42:03 +0200 | [diff] [blame] | 2219 | applet_have_more_data(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2220 | } |
| 2221 | |
Christopher Faulet | 5f35a3e | 2022-05-12 15:31:05 +0200 | [diff] [blame] | 2222 | static int hlua_socket_init(struct appctx *appctx) |
| 2223 | { |
| 2224 | struct hlua_csk_ctx *ctx = appctx->svcctx; |
| 2225 | struct stream *s; |
| 2226 | |
| 2227 | if (appctx_finalize_startup(appctx, socket_proxy, &BUF_NULL) == -1) |
| 2228 | goto error; |
| 2229 | |
| 2230 | s = appctx_strm(appctx); |
| 2231 | |
Willy Tarreau | 4596fe2 | 2022-05-17 19:07:51 +0200 | [diff] [blame] | 2232 | /* Configure "right" stream connector. This stconn is used to connect |
Christopher Faulet | 5f35a3e | 2022-05-12 15:31:05 +0200 | [diff] [blame] | 2233 | * and retrieve data from the server. The connection is initialized |
| 2234 | * with the "struct server". |
| 2235 | */ |
Willy Tarreau | 74568cf | 2022-05-27 09:03:30 +0200 | [diff] [blame] | 2236 | sc_set_state(s->scb, SC_ST_ASS); |
Christopher Faulet | 5f35a3e | 2022-05-12 15:31:05 +0200 | [diff] [blame] | 2237 | |
| 2238 | /* Force destination server. */ |
| 2239 | s->flags |= SF_DIRECT | SF_ASSIGNED | SF_BE_ASSIGNED; |
| 2240 | s->target = &socket_tcp->obj_type; |
| 2241 | |
| 2242 | ctx->appctx = appctx; |
| 2243 | return 0; |
| 2244 | |
| 2245 | error: |
| 2246 | return -1; |
| 2247 | } |
| 2248 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2249 | /* This function is called when the "struct stream" is destroyed. |
| 2250 | * Remove the link from the object to this stream. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2251 | * Wake all the pending signals. |
| 2252 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 2253 | static void hlua_socket_release(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2254 | { |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2255 | struct hlua_csk_ctx *ctx = appctx->svcctx; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2256 | struct xref *peer; |
| 2257 | |
Christopher Faulet | 5f35a3e | 2022-05-12 15:31:05 +0200 | [diff] [blame] | 2258 | /* Remove my link in the original objects. */ |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2259 | peer = xref_get_peer_and_lock(&ctx->xref); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2260 | if (peer) |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2261 | xref_disconnect(&ctx->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2262 | |
| 2263 | /* Wake all the task waiting for me. */ |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2264 | notification_wake(&ctx->wake_on_read); |
| 2265 | notification_wake(&ctx->wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2266 | } |
| 2267 | |
| 2268 | /* If the garbage collectio of the object is launch, nobody |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2269 | * uses this object. If the stream does not exists, just quit. |
| 2270 | * Send the shutdown signal to the stream. In some cases, |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2271 | * pending signal can rest in the read and write lists. destroy |
| 2272 | * it. |
| 2273 | */ |
| 2274 | __LJMP static int hlua_socket_gc(lua_State *L) |
| 2275 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2276 | struct hlua_socket *socket; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2277 | struct hlua_csk_ctx *ctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2278 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2279 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2280 | MAY_LJMP(check_args(L, 1, "__gc")); |
| 2281 | |
| 2282 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2283 | peer = xref_get_peer_and_lock(&socket->xref); |
| 2284 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2285 | return 0; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2286 | |
| 2287 | ctx = container_of(peer, struct hlua_csk_ctx, xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2288 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2289 | /* Set the flag which destroy the session. */ |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2290 | ctx->die = 1; |
| 2291 | appctx_wakeup(ctx->appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2292 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2293 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2294 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2295 | return 0; |
| 2296 | } |
| 2297 | |
| 2298 | /* The close function send shutdown signal and break the |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2299 | * links between the stream and the object. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2300 | */ |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 2301 | __LJMP static int hlua_socket_close_helper(lua_State *L) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2302 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2303 | struct hlua_socket *socket; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2304 | struct hlua_csk_ctx *ctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2305 | struct xref *peer; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2306 | struct hlua *hlua; |
| 2307 | |
| 2308 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 2309 | hlua = hlua_gethlua(L); |
| 2310 | if (!hlua) |
| 2311 | return 0; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2312 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2313 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2314 | |
| 2315 | /* Check if we run on the same thread than the xreator thread. |
| 2316 | * We cannot access to the socket if the thread is different. |
| 2317 | */ |
| 2318 | if (socket->tid != tid) |
| 2319 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2320 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2321 | peer = xref_get_peer_and_lock(&socket->xref); |
| 2322 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2323 | return 0; |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 2324 | |
| 2325 | hlua->gc_count--; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2326 | ctx = container_of(peer, struct hlua_csk_ctx, xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2327 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2328 | /* Set the flag which destroy the session. */ |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2329 | ctx->die = 1; |
| 2330 | appctx_wakeup(ctx->appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2331 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2332 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2333 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2334 | return 0; |
| 2335 | } |
| 2336 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 2337 | /* The close function calls close_helper. |
| 2338 | */ |
| 2339 | __LJMP static int hlua_socket_close(lua_State *L) |
| 2340 | { |
| 2341 | MAY_LJMP(check_args(L, 1, "close")); |
| 2342 | return hlua_socket_close_helper(L); |
| 2343 | } |
| 2344 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2345 | /* This Lua function assumes that the stack contain three parameters. |
| 2346 | * 1 - USERDATA containing a struct socket |
| 2347 | * 2 - INTEGER with values of the macro defined below |
| 2348 | * If the integer is -1, we must read at most one line. |
| 2349 | * If the integer is -2, we ust read all the data until the |
| 2350 | * end of the stream. |
| 2351 | * If the integer is positive value, we must read a number of |
| 2352 | * bytes corresponding to this value. |
| 2353 | */ |
| 2354 | #define HLSR_READ_LINE (-1) |
| 2355 | #define HLSR_READ_ALL (-2) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2356 | __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] | 2357 | { |
| 2358 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2359 | int wanted = lua_tointeger(L, 2); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2360 | struct hlua *hlua; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2361 | struct hlua_csk_ctx *csk_ctx; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2362 | struct appctx *appctx; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2363 | size_t len; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2364 | int nblk; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 2365 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2366 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 2367 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2368 | size_t len2; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 2369 | int skip_at_end = 0; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 2370 | struct channel *oc; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2371 | struct stream *s; |
| 2372 | struct xref *peer; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 2373 | int missing_bytes; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2374 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2375 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 2376 | hlua = hlua_gethlua(L); |
| 2377 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2378 | /* Check if this lua stack is schedulable. */ |
| 2379 | if (!hlua || !hlua->task) |
| 2380 | WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in " |
| 2381 | "'frontend', 'backend' or 'task'")); |
| 2382 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2383 | /* Check if we run on the same thread than the xreator thread. |
| 2384 | * We cannot access to the socket if the thread is different. |
| 2385 | */ |
| 2386 | if (socket->tid != tid) |
| 2387 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2388 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2389 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2390 | peer = xref_get_peer_and_lock(&socket->xref); |
| 2391 | if (!peer) |
| 2392 | goto no_peer; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2393 | |
| 2394 | csk_ctx = container_of(peer, struct hlua_csk_ctx, xref); |
| 2395 | appctx = csk_ctx->appctx; |
Willy Tarreau | 0698c80 | 2022-05-11 14:09:57 +0200 | [diff] [blame] | 2396 | s = appctx_strm(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2397 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2398 | oc = &s->res; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2399 | if (wanted == HLSR_READ_LINE) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2400 | /* Read line. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2401 | nblk = co_getline_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2402 | if (nblk < 0) /* Connection close. */ |
| 2403 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2404 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2405 | goto connection_empty; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 2406 | |
| 2407 | /* remove final \r\n. */ |
| 2408 | if (nblk == 1) { |
| 2409 | if (blk1[len1-1] == '\n') { |
| 2410 | len1--; |
| 2411 | skip_at_end++; |
| 2412 | if (blk1[len1-1] == '\r') { |
| 2413 | len1--; |
| 2414 | skip_at_end++; |
| 2415 | } |
| 2416 | } |
| 2417 | } |
| 2418 | else { |
| 2419 | if (blk2[len2-1] == '\n') { |
| 2420 | len2--; |
| 2421 | skip_at_end++; |
| 2422 | if (blk2[len2-1] == '\r') { |
| 2423 | len2--; |
| 2424 | skip_at_end++; |
| 2425 | } |
| 2426 | } |
| 2427 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2428 | } |
| 2429 | |
| 2430 | else if (wanted == HLSR_READ_ALL) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2431 | /* Read all the available data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2432 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2433 | if (nblk < 0) /* Connection close. */ |
| 2434 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2435 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2436 | goto connection_empty; |
| 2437 | } |
| 2438 | |
| 2439 | else { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2440 | /* Read a block of data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2441 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2442 | if (nblk < 0) /* Connection close. */ |
| 2443 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2444 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2445 | goto connection_empty; |
| 2446 | |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 2447 | missing_bytes = wanted - socket->b.n; |
| 2448 | if (len1 > missing_bytes) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2449 | nblk = 1; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 2450 | len1 = missing_bytes; |
| 2451 | } if (nblk == 2 && len1 + len2 > missing_bytes) |
| 2452 | len2 = missing_bytes - len1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2453 | } |
| 2454 | |
| 2455 | len = len1; |
| 2456 | |
| 2457 | luaL_addlstring(&socket->b, blk1, len1); |
| 2458 | if (nblk == 2) { |
| 2459 | len += len2; |
| 2460 | luaL_addlstring(&socket->b, blk2, len2); |
| 2461 | } |
| 2462 | |
| 2463 | /* Consume data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2464 | co_skip(oc, len + skip_at_end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2465 | |
| 2466 | /* Don't wait anything. */ |
Thierry FOURNIER | 7e4ee47 | 2018-05-25 15:03:50 +0200 | [diff] [blame] | 2467 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2468 | |
| 2469 | /* If the pattern reclaim to read all the data |
| 2470 | * in the connection, got out. |
| 2471 | */ |
| 2472 | if (wanted == HLSR_READ_ALL) |
| 2473 | goto connection_empty; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 2474 | else if (wanted >= 0 && socket->b.n < wanted) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2475 | goto connection_empty; |
| 2476 | |
| 2477 | /* Return result. */ |
| 2478 | luaL_pushresult(&socket->b); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2479 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2480 | return 1; |
| 2481 | |
| 2482 | connection_closed: |
| 2483 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2484 | xref_unlock(&socket->xref, peer); |
| 2485 | |
| 2486 | no_peer: |
| 2487 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2488 | /* If the buffer containds data. */ |
| 2489 | if (socket->b.n > 0) { |
| 2490 | luaL_pushresult(&socket->b); |
| 2491 | return 1; |
| 2492 | } |
| 2493 | lua_pushnil(L); |
| 2494 | lua_pushstring(L, "connection closed."); |
| 2495 | return 2; |
| 2496 | |
| 2497 | connection_empty: |
| 2498 | |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2499 | if (!notification_new(&hlua->com, &csk_ctx->wake_on_read, hlua->task)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2500 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2501 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2502 | } |
| 2503 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2504 | 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] | 2505 | return 0; |
| 2506 | } |
| 2507 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2508 | /* This Lua function gets two parameters. The first one can be string |
| 2509 | * or a number. If the string is "*l", the user requires one line. If |
| 2510 | * 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] | 2511 | * If the value is a number, the user require a number of bytes equal |
| 2512 | * to the value. The default value is "*l" (a line). |
| 2513 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2514 | * This parameter with a variable type is converted in integer. This |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2515 | * integer takes this values: |
| 2516 | * -1 : read a line |
| 2517 | * -2 : read all the stream |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2518 | * >0 : amount of bytes. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2519 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2520 | * The second parameter is optional. It contains a string that must be |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2521 | * concatenated with the read data. |
| 2522 | */ |
| 2523 | __LJMP static int hlua_socket_receive(struct lua_State *L) |
| 2524 | { |
| 2525 | int wanted = HLSR_READ_LINE; |
| 2526 | const char *pattern; |
Christopher Faulet | c31b200 | 2021-05-03 10:11:13 +0200 | [diff] [blame] | 2527 | int lastarg, type; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2528 | char *error; |
| 2529 | size_t len; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2530 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2531 | |
| 2532 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 2533 | WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments.")); |
| 2534 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2535 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2536 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2537 | /* Check if we run on the same thread than the xreator thread. |
| 2538 | * We cannot access to the socket if the thread is different. |
| 2539 | */ |
| 2540 | if (socket->tid != tid) |
| 2541 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2542 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2543 | /* check for pattern. */ |
| 2544 | if (lua_gettop(L) >= 2) { |
| 2545 | type = lua_type(L, 2); |
| 2546 | if (type == LUA_TSTRING) { |
| 2547 | pattern = lua_tostring(L, 2); |
| 2548 | if (strcmp(pattern, "*a") == 0) |
| 2549 | wanted = HLSR_READ_ALL; |
| 2550 | else if (strcmp(pattern, "*l") == 0) |
| 2551 | wanted = HLSR_READ_LINE; |
| 2552 | else { |
| 2553 | wanted = strtoll(pattern, &error, 10); |
| 2554 | if (*error != '\0') |
| 2555 | WILL_LJMP(luaL_error(L, "Unsupported pattern.")); |
| 2556 | } |
| 2557 | } |
| 2558 | else if (type == LUA_TNUMBER) { |
| 2559 | wanted = lua_tointeger(L, 2); |
| 2560 | if (wanted < 0) |
| 2561 | WILL_LJMP(luaL_error(L, "Unsupported size.")); |
| 2562 | } |
| 2563 | } |
| 2564 | |
| 2565 | /* Set pattern. */ |
| 2566 | lua_pushinteger(L, wanted); |
Tim Duesterhus | c6e377e | 2018-01-04 19:32:13 +0100 | [diff] [blame] | 2567 | |
| 2568 | /* Check if we would replace the top by itself. */ |
| 2569 | if (lua_gettop(L) != 2) |
| 2570 | lua_replace(L, 2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2571 | |
Christopher Faulet | c31b200 | 2021-05-03 10:11:13 +0200 | [diff] [blame] | 2572 | /* Save index of the top of the stack because since buffers are used, it |
| 2573 | * may change |
| 2574 | */ |
| 2575 | lastarg = lua_gettop(L); |
| 2576 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2577 | /* init buffer, and fill it with prefix. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2578 | luaL_buffinit(L, &socket->b); |
| 2579 | |
| 2580 | /* Check prefix. */ |
Christopher Faulet | c31b200 | 2021-05-03 10:11:13 +0200 | [diff] [blame] | 2581 | if (lastarg >= 3) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2582 | if (lua_type(L, 3) != LUA_TSTRING) |
| 2583 | WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix")); |
| 2584 | pattern = lua_tolstring(L, 3, &len); |
| 2585 | luaL_addlstring(&socket->b, pattern, len); |
| 2586 | } |
| 2587 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2588 | return __LJMP(hlua_socket_receive_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2589 | } |
| 2590 | |
| 2591 | /* Write the Lua input string in the output buffer. |
Mark Lakes | 22154b4 | 2018-01-29 14:38:40 -0800 | [diff] [blame] | 2592 | * This function returns a yield if no space is available. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2593 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2594 | 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] | 2595 | { |
| 2596 | struct hlua_socket *socket; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2597 | struct hlua *hlua; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2598 | struct hlua_csk_ctx *csk_ctx; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2599 | struct appctx *appctx; |
| 2600 | size_t buf_len; |
| 2601 | const char *buf; |
| 2602 | int len; |
| 2603 | int send_len; |
| 2604 | int sent; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2605 | struct xref *peer; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2606 | struct stream *s; |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 2607 | struct stconn *sc; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2608 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2609 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 2610 | hlua = hlua_gethlua(L); |
| 2611 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2612 | /* Check if this lua stack is schedulable. */ |
| 2613 | if (!hlua || !hlua->task) |
| 2614 | WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in " |
| 2615 | "'frontend', 'backend' or 'task'")); |
| 2616 | |
| 2617 | /* Get object */ |
| 2618 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2619 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2620 | sent = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2621 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2622 | /* Check if we run on the same thread than the xreator thread. |
| 2623 | * We cannot access to the socket if the thread is different. |
| 2624 | */ |
| 2625 | if (socket->tid != tid) |
| 2626 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2627 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2628 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2629 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2630 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2631 | lua_pushinteger(L, -1); |
| 2632 | return 1; |
| 2633 | } |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2634 | |
| 2635 | csk_ctx = container_of(peer, struct hlua_csk_ctx, xref); |
| 2636 | appctx = csk_ctx->appctx; |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 2637 | sc = appctx_sc(appctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 2638 | s = __sc_strm(sc); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2639 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2640 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2641 | if (channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2642 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2643 | lua_pushinteger(L, -1); |
| 2644 | return 1; |
| 2645 | } |
| 2646 | |
| 2647 | /* Update the input buffer data. */ |
| 2648 | buf += sent; |
| 2649 | send_len = buf_len - sent; |
| 2650 | |
| 2651 | /* All the data are sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2652 | if (sent >= buf_len) { |
| 2653 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2654 | return 1; /* Implicitly return the length sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2655 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2656 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2657 | /* Check if the buffer is available because HAProxy doesn't allocate |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 2658 | * the request buffer if its not required. |
| 2659 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2660 | if (s->req.buf.size == 0) { |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 2661 | if (!sc_alloc_ibuf(sc, &appctx->buffer_wait)) |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2662 | goto hlua_socket_write_yield_return; |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 2663 | } |
| 2664 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2665 | /* Check for available space. */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2666 | len = b_room(&s->req.buf); |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2667 | if (len <= 0) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2668 | goto hlua_socket_write_yield_return; |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2669 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2670 | |
| 2671 | /* send data */ |
| 2672 | if (len < send_len) |
| 2673 | send_len = len; |
Thierry FOURNIER | 66b8919 | 2018-05-27 01:14:47 +0200 | [diff] [blame] | 2674 | len = ci_putblk(&s->req, buf, send_len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2675 | |
| 2676 | /* "Not enough space" (-1), "Buffer too little to contain |
| 2677 | * the data" (-2) are not expected because the available length |
| 2678 | * is tested. |
| 2679 | * Other unknown error are also not expected. |
| 2680 | */ |
| 2681 | if (len <= 0) { |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2682 | if (len == -1) |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2683 | s->req.flags |= CF_WAKE_WRITE; |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2684 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 2685 | MAY_LJMP(hlua_socket_close_helper(L)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2686 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2687 | lua_pushinteger(L, -1); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2688 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2689 | return 1; |
| 2690 | } |
| 2691 | |
| 2692 | /* update buffers. */ |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 2693 | appctx_wakeup(appctx); |
Willy Tarreau | de70fa1 | 2015-09-26 11:25:05 +0200 | [diff] [blame] | 2694 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2695 | /* Update length sent. */ |
| 2696 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2697 | lua_pushinteger(L, sent + len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2698 | |
| 2699 | /* All the data buffer is sent ? */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2700 | if (sent + len >= buf_len) { |
| 2701 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2702 | return 1; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2703 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2704 | |
| 2705 | hlua_socket_write_yield_return: |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2706 | if (!notification_new(&hlua->com, &csk_ctx->wake_on_write, hlua->task)) { |
Thierry FOURNIER | ba42fcd | 2018-05-27 00:59:48 +0200 | [diff] [blame] | 2707 | xref_unlock(&socket->xref, peer); |
| 2708 | WILL_LJMP(luaL_error(L, "out of memory")); |
| 2709 | } |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2710 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2711 | 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] | 2712 | return 0; |
| 2713 | } |
| 2714 | |
| 2715 | /* This function initiate the send of data. It just check the input |
| 2716 | * parameters and push an integer in the Lua stack that contain the |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2717 | * 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] | 2718 | * "hlua_socket_write_yield" that can yield. |
| 2719 | * |
| 2720 | * The Lua function gets between 3 and 4 parameters. The first one is |
| 2721 | * the associated object. The second is a string buffer. The third is |
| 2722 | * a facultative integer that represents where is the buffer position |
| 2723 | * of the start of the data that can send. The first byte is the |
| 2724 | * position "1". The default value is "1". The fourth argument is a |
| 2725 | * facultative integer that represents where is the buffer position |
| 2726 | * of the end of the data that can send. The default is the last byte. |
| 2727 | */ |
| 2728 | static int hlua_socket_send(struct lua_State *L) |
| 2729 | { |
| 2730 | int i; |
| 2731 | int j; |
| 2732 | const char *buf; |
| 2733 | size_t buf_len; |
| 2734 | |
| 2735 | /* Check number of arguments. */ |
| 2736 | if (lua_gettop(L) < 2 || lua_gettop(L) > 4) |
| 2737 | WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments")); |
| 2738 | |
| 2739 | /* Get the string. */ |
| 2740 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
| 2741 | |
| 2742 | /* Get and check j. */ |
| 2743 | if (lua_gettop(L) == 4) { |
| 2744 | j = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 2745 | if (j < 0) |
| 2746 | j = buf_len + j + 1; |
| 2747 | if (j > buf_len) |
| 2748 | j = buf_len + 1; |
| 2749 | lua_pop(L, 1); |
| 2750 | } |
| 2751 | else |
| 2752 | j = buf_len; |
| 2753 | |
| 2754 | /* Get and check i. */ |
| 2755 | if (lua_gettop(L) == 3) { |
| 2756 | i = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2757 | if (i < 0) |
| 2758 | i = buf_len + i + 1; |
| 2759 | if (i > buf_len) |
| 2760 | i = buf_len + 1; |
| 2761 | lua_pop(L, 1); |
| 2762 | } else |
| 2763 | i = 1; |
| 2764 | |
| 2765 | /* Check bth i and j. */ |
| 2766 | if (i > j) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2767 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2768 | return 1; |
| 2769 | } |
| 2770 | if (i == 0 && j == 0) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2771 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2772 | return 1; |
| 2773 | } |
| 2774 | if (i == 0) |
| 2775 | i = 1; |
| 2776 | if (j == 0) |
| 2777 | j = 1; |
| 2778 | |
| 2779 | /* Pop the string. */ |
| 2780 | lua_pop(L, 1); |
| 2781 | |
| 2782 | /* Update the buffer length. */ |
| 2783 | buf += i - 1; |
| 2784 | buf_len = j - i + 1; |
| 2785 | lua_pushlstring(L, buf, buf_len); |
| 2786 | |
| 2787 | /* This unsigned is used to remember the amount of sent data. */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2788 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2789 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2790 | return MAY_LJMP(hlua_socket_write_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2791 | } |
| 2792 | |
Willy Tarreau | 22b0a68 | 2015-06-17 19:43:49 +0200 | [diff] [blame] | 2793 | #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] | 2794 | __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] | 2795 | { |
| 2796 | static char buffer[SOCKET_INFO_MAX_LEN]; |
| 2797 | int ret; |
| 2798 | int len; |
| 2799 | char *p; |
| 2800 | |
| 2801 | ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1); |
| 2802 | if (ret <= 0) { |
| 2803 | lua_pushnil(L); |
| 2804 | return 1; |
| 2805 | } |
| 2806 | |
| 2807 | if (ret == AF_UNIX) { |
| 2808 | lua_pushstring(L, buffer+1); |
| 2809 | return 1; |
| 2810 | } |
| 2811 | else if (ret == AF_INET6) { |
| 2812 | buffer[0] = '['; |
| 2813 | len = strlen(buffer); |
| 2814 | buffer[len] = ']'; |
| 2815 | len++; |
| 2816 | buffer[len] = ':'; |
| 2817 | len++; |
| 2818 | p = buffer; |
| 2819 | } |
| 2820 | else if (ret == AF_INET) { |
| 2821 | p = buffer + 1; |
| 2822 | len = strlen(p); |
| 2823 | p[len] = ':'; |
| 2824 | len++; |
| 2825 | } |
| 2826 | else { |
| 2827 | lua_pushnil(L); |
| 2828 | return 1; |
| 2829 | } |
| 2830 | |
| 2831 | if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) { |
| 2832 | lua_pushnil(L); |
| 2833 | return 1; |
| 2834 | } |
| 2835 | |
| 2836 | lua_pushstring(L, p); |
| 2837 | return 1; |
| 2838 | } |
| 2839 | |
| 2840 | /* Returns information about the peer of the connection. */ |
| 2841 | __LJMP static int hlua_socket_getpeername(struct lua_State *L) |
| 2842 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2843 | struct hlua_socket *socket; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2844 | struct xref *peer; |
| 2845 | struct appctx *appctx; |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 2846 | struct stconn *sc; |
Christopher Faulet | 16f16af | 2021-10-27 09:34:56 +0200 | [diff] [blame] | 2847 | const struct sockaddr_storage *dst; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2848 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2849 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2850 | MAY_LJMP(check_args(L, 1, "getpeername")); |
| 2851 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2852 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2853 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2854 | /* Check if we run on the same thread than the xreator thread. |
| 2855 | * We cannot access to the socket if the thread is different. |
| 2856 | */ |
| 2857 | if (socket->tid != tid) |
| 2858 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2859 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2860 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2861 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2862 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2863 | lua_pushnil(L); |
| 2864 | return 1; |
| 2865 | } |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2866 | |
| 2867 | appctx = container_of(peer, struct hlua_csk_ctx, xref)->appctx; |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 2868 | sc = appctx_sc(appctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 2869 | dst = sc_dst(sc_opposite(sc)); |
Christopher Faulet | 16f16af | 2021-10-27 09:34:56 +0200 | [diff] [blame] | 2870 | if (!dst) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2871 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2872 | lua_pushnil(L); |
| 2873 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2874 | } |
| 2875 | |
Christopher Faulet | 16f16af | 2021-10-27 09:34:56 +0200 | [diff] [blame] | 2876 | ret = MAY_LJMP(hlua_socket_info(L, dst)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2877 | xref_unlock(&socket->xref, peer); |
| 2878 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2879 | } |
| 2880 | |
| 2881 | /* Returns information about my connection side. */ |
| 2882 | static int hlua_socket_getsockname(struct lua_State *L) |
| 2883 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2884 | struct hlua_socket *socket; |
| 2885 | struct connection *conn; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2886 | struct appctx *appctx; |
| 2887 | struct xref *peer; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2888 | struct stream *s; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2889 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2890 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2891 | MAY_LJMP(check_args(L, 1, "getsockname")); |
| 2892 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2893 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2894 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2895 | /* Check if we run on the same thread than the xreator thread. |
| 2896 | * We cannot access to the socket if the thread is different. |
| 2897 | */ |
| 2898 | if (socket->tid != tid) |
| 2899 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2900 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2901 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2902 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2903 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2904 | lua_pushnil(L); |
| 2905 | return 1; |
| 2906 | } |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2907 | |
| 2908 | appctx = container_of(peer, struct hlua_csk_ctx, xref)->appctx; |
Willy Tarreau | 0698c80 | 2022-05-11 14:09:57 +0200 | [diff] [blame] | 2909 | s = appctx_strm(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2910 | |
Willy Tarreau | fd9417b | 2022-05-18 16:23:22 +0200 | [diff] [blame] | 2911 | conn = sc_conn(s->scb); |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2912 | if (!conn || !conn_get_src(conn)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2913 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2914 | lua_pushnil(L); |
| 2915 | return 1; |
| 2916 | } |
| 2917 | |
Willy Tarreau | 9da9a6f | 2019-07-17 14:49:44 +0200 | [diff] [blame] | 2918 | ret = hlua_socket_info(L, conn->src); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2919 | xref_unlock(&socket->xref, peer); |
| 2920 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2921 | } |
| 2922 | |
| 2923 | /* This struct define the applet. */ |
Willy Tarreau | 3057645 | 2015-04-13 13:50:30 +0200 | [diff] [blame] | 2924 | static struct applet update_applet = { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2925 | .obj_type = OBJ_TYPE_APPLET, |
| 2926 | .name = "<LUA_TCP>", |
| 2927 | .fct = hlua_socket_handler, |
Christopher Faulet | 5f35a3e | 2022-05-12 15:31:05 +0200 | [diff] [blame] | 2928 | .init = hlua_socket_init, |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2929 | .release = hlua_socket_release, |
| 2930 | }; |
| 2931 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2932 | __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] | 2933 | { |
| 2934 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2935 | struct hlua *hlua; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2936 | struct xref *peer; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2937 | struct hlua_csk_ctx *csk_ctx; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2938 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2939 | struct stream *s; |
| 2940 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2941 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 2942 | hlua = hlua_gethlua(L); |
| 2943 | if (!hlua) |
| 2944 | return 0; |
| 2945 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2946 | /* Check if we run on the same thread than the xreator thread. |
| 2947 | * We cannot access to the socket if the thread is different. |
| 2948 | */ |
| 2949 | if (socket->tid != tid) |
| 2950 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2951 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2952 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2953 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2954 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2955 | lua_pushnil(L); |
| 2956 | lua_pushstring(L, "Can't connect"); |
| 2957 | return 2; |
| 2958 | } |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2959 | |
| 2960 | csk_ctx = container_of(peer, struct hlua_csk_ctx, xref); |
| 2961 | appctx = csk_ctx->appctx; |
Willy Tarreau | 0698c80 | 2022-05-11 14:09:57 +0200 | [diff] [blame] | 2962 | s = appctx_strm(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2963 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2964 | /* Check if we run on the same thread than the xreator thread. |
| 2965 | * We cannot access to the socket if the thread is different. |
| 2966 | */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2967 | if (socket->tid != tid) { |
| 2968 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2969 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2970 | } |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2971 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2972 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2973 | if (!hlua || channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2974 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2975 | lua_pushnil(L); |
| 2976 | lua_pushstring(L, "Can't connect"); |
| 2977 | return 2; |
| 2978 | } |
| 2979 | |
Willy Tarreau | 8e7c6e6 | 2022-05-18 17:58:02 +0200 | [diff] [blame] | 2980 | appctx = __sc_appctx(s->scf); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2981 | |
| 2982 | /* Check for connection established. */ |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2983 | if (csk_ctx->connected) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2984 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2985 | lua_pushinteger(L, 1); |
| 2986 | return 1; |
| 2987 | } |
| 2988 | |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2989 | if (!notification_new(&hlua->com, &csk_ctx->wake_on_write, hlua->task)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2990 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2991 | WILL_LJMP(luaL_error(L, "out of memory error")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2992 | } |
| 2993 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2994 | 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] | 2995 | return 0; |
| 2996 | } |
| 2997 | |
| 2998 | /* This function fail or initite the connection. */ |
| 2999 | __LJMP static int hlua_socket_connect(struct lua_State *L) |
| 3000 | { |
| 3001 | struct hlua_socket *socket; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 3002 | int port = -1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3003 | const char *ip; |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 3004 | struct hlua *hlua; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 3005 | struct hlua_csk_ctx *csk_ctx; |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 3006 | struct appctx *appctx; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 3007 | int low, high; |
| 3008 | struct sockaddr_storage *addr; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 3009 | struct xref *peer; |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 3010 | struct stconn *sc; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 3011 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3012 | |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 3013 | if (lua_gettop(L) < 2) |
| 3014 | WILL_LJMP(luaL_error(L, "connect: need at least 2 arguments")); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3015 | |
| 3016 | /* Get args. */ |
| 3017 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 3018 | |
| 3019 | /* Check if we run on the same thread than the xreator thread. |
| 3020 | * We cannot access to the socket if the thread is different. |
| 3021 | */ |
| 3022 | if (socket->tid != tid) |
| 3023 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 3024 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3025 | ip = MAY_LJMP(luaL_checkstring(L, 2)); |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 3026 | if (lua_gettop(L) >= 3) { |
| 3027 | luaL_Buffer b; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 3028 | port = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3029 | |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 3030 | /* Force the ip to end with a colon, to support IPv6 addresses |
| 3031 | * that are not enclosed within square brackets. |
| 3032 | */ |
| 3033 | if (port > 0) { |
| 3034 | luaL_buffinit(L, &b); |
| 3035 | luaL_addstring(&b, ip); |
| 3036 | luaL_addchar(&b, ':'); |
| 3037 | luaL_pushresult(&b); |
| 3038 | ip = lua_tolstring(L, lua_gettop(L), NULL); |
| 3039 | } |
| 3040 | } |
| 3041 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 3042 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 3043 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 3044 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 3045 | lua_pushnil(L); |
| 3046 | return 1; |
| 3047 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3048 | |
| 3049 | /* Parse ip address. */ |
Willy Tarreau | 5fc9328 | 2020-09-16 18:25:03 +0200 | [diff] [blame] | 3050 | 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] | 3051 | if (!addr) { |
| 3052 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 3053 | WILL_LJMP(luaL_error(L, "connect: cannot parse destination address '%s'", ip)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 3054 | } |
Willy Tarreau | 9da9a6f | 2019-07-17 14:49:44 +0200 | [diff] [blame] | 3055 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3056 | /* Set port. */ |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 3057 | if (low == 0) { |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 3058 | if (addr->ss_family == AF_INET) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 3059 | if (port == -1) { |
| 3060 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 3061 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 3062 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 3063 | ((struct sockaddr_in *)addr)->sin_port = htons(port); |
| 3064 | } else if (addr->ss_family == AF_INET6) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 3065 | if (port == -1) { |
| 3066 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 3067 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 3068 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 3069 | ((struct sockaddr_in6 *)addr)->sin6_port = htons(port); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 3070 | } |
| 3071 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 3072 | |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 3073 | csk_ctx = container_of(peer, struct hlua_csk_ctx, xref); |
| 3074 | appctx = csk_ctx->appctx; |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 3075 | sc = appctx_sc(appctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 3076 | s = __sc_strm(sc); |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 3077 | |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 3078 | if (!sockaddr_alloc(&sc_opposite(sc)->dst, addr, sizeof(*addr))) { |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 3079 | xref_unlock(&socket->xref, peer); |
| 3080 | WILL_LJMP(luaL_error(L, "connect: internal error")); |
| 3081 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3082 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 3083 | /* Get hlua struct, or NULL if we execute from main lua state */ |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 3084 | hlua = hlua_gethlua(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 3085 | if (!hlua) |
| 3086 | return 0; |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 3087 | |
| 3088 | /* inform the stream that we want to be notified whenever the |
| 3089 | * connection completes. |
| 3090 | */ |
Willy Tarreau | 90e8b45 | 2022-05-25 18:21:43 +0200 | [diff] [blame] | 3091 | applet_need_more_data(appctx); |
Willy Tarreau | 4164eb9 | 2022-05-25 15:42:03 +0200 | [diff] [blame] | 3092 | applet_have_more_data(appctx); |
Thierry FOURNIER | 8c8fbbe | 2015-09-26 17:02:35 +0200 | [diff] [blame] | 3093 | appctx_wakeup(appctx); |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 3094 | |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 3095 | hlua->gc_count++; |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 3096 | |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 3097 | if (!notification_new(&hlua->com, &csk_ctx->wake_on_write, hlua->task)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 3098 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 3099 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 3100 | } |
| 3101 | xref_unlock(&socket->xref, peer); |
PiBa-NL | 706d5ee | 2018-05-05 23:51:42 +0200 | [diff] [blame] | 3102 | |
| 3103 | task_wakeup(s->task, TASK_WOKEN_INIT); |
| 3104 | /* Return yield waiting for connection. */ |
| 3105 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3106 | 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] | 3107 | |
| 3108 | return 0; |
| 3109 | } |
| 3110 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 3111 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3112 | __LJMP static int hlua_socket_connect_ssl(struct lua_State *L) |
| 3113 | { |
| 3114 | struct hlua_socket *socket; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 3115 | struct xref *peer; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 3116 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3117 | |
| 3118 | MAY_LJMP(check_args(L, 3, "connect_ssl")); |
| 3119 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 3120 | |
| 3121 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 3122 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 3123 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 3124 | lua_pushnil(L); |
| 3125 | return 1; |
| 3126 | } |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 3127 | |
Willy Tarreau | 0698c80 | 2022-05-11 14:09:57 +0200 | [diff] [blame] | 3128 | s = appctx_strm(container_of(peer, struct hlua_csk_ctx, xref)->appctx); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 3129 | |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 3130 | s->target = &socket_ssl->obj_type; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 3131 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3132 | return MAY_LJMP(hlua_socket_connect(L)); |
| 3133 | } |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 3134 | #endif |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3135 | |
| 3136 | __LJMP static int hlua_socket_setoption(struct lua_State *L) |
| 3137 | { |
| 3138 | return 0; |
| 3139 | } |
| 3140 | |
| 3141 | __LJMP static int hlua_socket_settimeout(struct lua_State *L) |
| 3142 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3143 | struct hlua_socket *socket; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3144 | int tmout; |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 3145 | double dtmout; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 3146 | struct xref *peer; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 3147 | struct stream *s; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3148 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3149 | MAY_LJMP(check_args(L, 2, "settimeout")); |
| 3150 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3151 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 3152 | |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 3153 | /* convert the timeout to millis */ |
| 3154 | dtmout = MAY_LJMP(luaL_checknumber(L, 2)) * 1000; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3155 | |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 3156 | /* Check for negative values */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 3157 | if (dtmout < 0) |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 3158 | WILL_LJMP(luaL_error(L, "settimeout: cannot set negatives values")); |
| 3159 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 3160 | if (dtmout > INT_MAX) /* overflow check */ |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 3161 | 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] | 3162 | |
| 3163 | tmout = MS_TO_TICKS((int)dtmout); |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 3164 | if (tmout == 0) |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3165 | tmout++; /* very small timeouts are adjusted to a minimum of 1ms */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 3166 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 3167 | /* Check if we run on the same thread than the xreator thread. |
| 3168 | * We cannot access to the socket if the thread is different. |
| 3169 | */ |
| 3170 | if (socket->tid != tid) |
| 3171 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 3172 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 3173 | /* check for connection break. If some data were read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 3174 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 3175 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 3176 | hlua_pusherror(L, "socket: not yet initialised, you can't set timeouts."); |
| 3177 | WILL_LJMP(lua_error(L)); |
| 3178 | return 0; |
| 3179 | } |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 3180 | |
Willy Tarreau | 0698c80 | 2022-05-11 14:09:57 +0200 | [diff] [blame] | 3181 | s = appctx_strm(container_of(peer, struct hlua_csk_ctx, xref)->appctx); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 3182 | |
Cyril Bonté | 7bb6345 | 2018-08-17 23:51:02 +0200 | [diff] [blame] | 3183 | s->sess->fe->timeout.connect = tmout; |
Christopher Faulet | 5aaacfb | 2023-02-15 08:13:33 +0100 | [diff] [blame] | 3184 | s->scf->ioto = tmout; |
| 3185 | s->scb->ioto = tmout; |
Cyril Bonté | 7bb6345 | 2018-08-17 23:51:02 +0200 | [diff] [blame] | 3186 | |
| 3187 | s->task->expire = tick_add_ifset(now_ms, tmout); |
| 3188 | task_queue(s->task); |
| 3189 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 3190 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3191 | |
Thierry Fournier | e9636f1 | 2018-03-08 09:54:32 +0100 | [diff] [blame] | 3192 | lua_pushinteger(L, 1); |
Tim Duesterhus | 119a5f1 | 2018-01-06 19:16:25 +0100 | [diff] [blame] | 3193 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3194 | } |
| 3195 | |
| 3196 | __LJMP static int hlua_socket_new(lua_State *L) |
| 3197 | { |
| 3198 | struct hlua_socket *socket; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 3199 | struct hlua_csk_ctx *ctx; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3200 | struct appctx *appctx; |
| 3201 | |
| 3202 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 3203 | if (!lua_checkstack(L, 3)) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3204 | hlua_pusherror(L, "socket: full stack"); |
| 3205 | goto out_fail_conf; |
| 3206 | } |
| 3207 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 3208 | /* Create the object: obj[0] = userdata. */ |
| 3209 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3210 | socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket))); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 3211 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3212 | memset(socket, 0, sizeof(*socket)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 3213 | socket->tid = tid; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3214 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3215 | /* Check if the various memory pools are initialized. */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 3216 | if (!pool_head_stream || !pool_head_buffer) { |
Thierry FOURNIER | 4a6170c | 2015-03-09 17:07:10 +0100 | [diff] [blame] | 3217 | hlua_pusherror(L, "socket: uninitialized pools."); |
| 3218 | goto out_fail_conf; |
| 3219 | } |
| 3220 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3221 | /* Pop a class stream metatable and affect it to the userdata. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3222 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref); |
| 3223 | lua_setmetatable(L, -2); |
| 3224 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 3225 | /* Create the applet context */ |
Christopher Faulet | 6095d57 | 2022-05-16 17:09:48 +0200 | [diff] [blame] | 3226 | appctx = appctx_new_here(&update_applet, NULL); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 3227 | if (!appctx) { |
| 3228 | hlua_pusherror(L, "socket: out of memory"); |
Christopher Faulet | a9e8b39 | 2022-03-23 11:01:09 +0100 | [diff] [blame] | 3229 | goto out_fail_conf; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 3230 | } |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 3231 | ctx = applet_reserve_svcctx(appctx, sizeof(*ctx)); |
| 3232 | ctx->connected = 0; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 3233 | ctx->die = 0; |
| 3234 | LIST_INIT(&ctx->wake_on_write); |
| 3235 | LIST_INIT(&ctx->wake_on_read); |
Willy Tarreau | b2bf833 | 2015-04-04 15:58:58 +0200 | [diff] [blame] | 3236 | |
Christopher Faulet | 5f35a3e | 2022-05-12 15:31:05 +0200 | [diff] [blame] | 3237 | if (appctx_init(appctx) == -1) { |
| 3238 | hlua_pusherror(L, "socket: fail to init applet."); |
Christopher Faulet | 13a35e5 | 2021-12-20 15:34:16 +0100 | [diff] [blame] | 3239 | goto out_fail_appctx; |
| 3240 | } |
| 3241 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 3242 | /* Initialise cross reference between stream and Lua socket object. */ |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 3243 | xref_create(&socket->xref, &ctx->xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3244 | return 1; |
| 3245 | |
Christopher Faulet | 13a35e5 | 2021-12-20 15:34:16 +0100 | [diff] [blame] | 3246 | out_fail_appctx: |
Christopher Faulet | 5f35a3e | 2022-05-12 15:31:05 +0200 | [diff] [blame] | 3247 | appctx_free_on_early_error(appctx); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 3248 | out_fail_conf: |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3249 | WILL_LJMP(lua_error(L)); |
| 3250 | return 0; |
| 3251 | } |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 3252 | |
| 3253 | /* |
| 3254 | * |
| 3255 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3256 | * Class Channel |
| 3257 | * |
| 3258 | * |
| 3259 | */ |
| 3260 | |
| 3261 | /* Returns the struct hlua_channel join to the class channel in the |
| 3262 | * stack entry "ud" or throws an argument error. |
| 3263 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3264 | __LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3265 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3266 | return MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3267 | } |
| 3268 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3269 | /* Pushes the channel onto the top of the stack. If the stask does not have a |
| 3270 | * free slots, the function fails and returns 0; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3271 | */ |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 3272 | static int hlua_channel_new(lua_State *L, struct channel *channel) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3273 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3274 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 3275 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3276 | return 0; |
| 3277 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 3278 | lua_newtable(L); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3279 | lua_pushlightuserdata(L, channel); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 3280 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3281 | |
| 3282 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 3283 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref); |
| 3284 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3285 | return 1; |
| 3286 | } |
| 3287 | |
Christopher Faulet | 9f55a50 | 2020-02-25 15:21:02 +0100 | [diff] [blame] | 3288 | /* Helper function returning a filter attached to a channel at the position <ud> |
| 3289 | * 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] | 3290 | * filter is attached, NULL is returned and <offset> and <len> are not |
Christopher Faulet | 9f55a50 | 2020-02-25 15:21:02 +0100 | [diff] [blame] | 3291 | * initialized. |
| 3292 | */ |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3293 | 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] | 3294 | { |
| 3295 | struct filter *filter = NULL; |
| 3296 | |
| 3297 | if (lua_getfield(L, ud, "__filter") == LUA_TLIGHTUSERDATA) { |
| 3298 | struct hlua_flt_ctx *flt_ctx; |
| 3299 | |
| 3300 | filter = lua_touserdata (L, -1); |
| 3301 | flt_ctx = filter->ctx; |
| 3302 | if (hlua_filter_from_payload(filter)) { |
| 3303 | *offset = flt_ctx->cur_off[CHN_IDX(chn)]; |
| 3304 | *len = flt_ctx->cur_len[CHN_IDX(chn)]; |
| 3305 | } |
| 3306 | } |
| 3307 | |
| 3308 | lua_pop(L, 1); |
| 3309 | return filter; |
| 3310 | } |
| 3311 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3312 | /* Copies <len> bytes of data present in the channel's buffer, starting at the |
| 3313 | * 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] | 3314 | * responsibility to ensure <len> and <offset> are valid. It always return the |
| 3315 | * length of the built string. <len> may be 0, in this case, an empty string is |
| 3316 | * created and 0 is returned. |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3317 | */ |
| 3318 | 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] | 3319 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3320 | size_t block1, block2; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3321 | luaL_Buffer b; |
| 3322 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3323 | block1 = len; |
| 3324 | if (block1 > b_contig_data(&chn->buf, b_peek_ofs(&chn->buf, offset))) |
| 3325 | block1 = b_contig_data(&chn->buf, b_peek_ofs(&chn->buf, offset)); |
| 3326 | block2 = len - block1; |
| 3327 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3328 | luaL_buffinit(L, &b); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3329 | luaL_addlstring(&b, b_peek(&chn->buf, offset), block1); |
| 3330 | if (block2) |
| 3331 | luaL_addlstring(&b, b_orig(&chn->buf), block2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3332 | luaL_pushresult(&b); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3333 | return len; |
| 3334 | } |
| 3335 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3336 | /* Inserts the string <str> to the channel's buffer at the offset <offset>. This |
| 3337 | * function returns -1 if data cannot be copied. Otherwise, it returns the |
| 3338 | * number of bytes copied. |
| 3339 | */ |
| 3340 | static int _hlua_channel_insert(struct channel *chn, lua_State *L, struct ist str, size_t offset) |
| 3341 | { |
| 3342 | int ret = 0; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3343 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3344 | /* Nothing to do, just return */ |
| 3345 | if (unlikely(istlen(str) == 0)) |
| 3346 | goto end; |
| 3347 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3348 | if (istlen(str) > c_room(chn)) { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3349 | ret = -1; |
| 3350 | goto end; |
| 3351 | } |
| 3352 | ret = b_insert_blk(&chn->buf, offset, istptr(str), istlen(str)); |
| 3353 | |
| 3354 | end: |
| 3355 | return ret; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3356 | } |
| 3357 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3358 | /* Removes <len> bytes of data at the absolute position <offset>. |
| 3359 | */ |
| 3360 | static void _hlua_channel_delete(struct channel *chn, size_t offset, size_t len) |
| 3361 | { |
| 3362 | size_t end = offset + len; |
| 3363 | |
| 3364 | if (b_peek(&chn->buf, end) != b_tail(&chn->buf)) |
| 3365 | b_move(&chn->buf, b_peek_ofs(&chn->buf, end), |
| 3366 | b_data(&chn->buf) - end, -len); |
| 3367 | b_sub(&chn->buf, len); |
| 3368 | } |
| 3369 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3370 | /* Copies input data in the channel's buffer. It is possible to set a specific |
| 3371 | * offset (0 by default) and a length (all remaining input data starting for the |
| 3372 | * offset by default). If there is not enough input data and more data can be |
| 3373 | * received, this function yields. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3374 | * |
| 3375 | * From an action, All input data are considered. For a filter, the offset and |
| 3376 | * 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] | 3377 | */ |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3378 | __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] | 3379 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3380 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3381 | struct filter *filter; |
| 3382 | size_t input, output; |
| 3383 | int offset, len; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3384 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3385 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3386 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3387 | output = co_data(chn); |
| 3388 | input = ci_data(chn); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3389 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3390 | filter = hlua_channel_filter(L, 1, chn, &output, &input); |
| 3391 | if (filter && !hlua_filter_from_payload(filter)) |
| 3392 | WILL_LJMP(lua_error(L)); |
| 3393 | |
| 3394 | offset = output; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3395 | if (lua_gettop(L) > 1) { |
| 3396 | offset = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3397 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 3398 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3399 | offset += output; |
| 3400 | if (offset < output || offset > input + output) { |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3401 | lua_pushfstring(L, "offset out of range."); |
| 3402 | WILL_LJMP(lua_error(L)); |
| 3403 | } |
| 3404 | } |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3405 | len = output + input - offset; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3406 | if (lua_gettop(L) == 3) { |
| 3407 | len = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3408 | if (!len) |
| 3409 | goto dup; |
| 3410 | if (len == -1) |
| 3411 | len = global.tune.bufsize; |
| 3412 | if (len < 0) { |
| 3413 | lua_pushfstring(L, "length out of range."); |
| 3414 | WILL_LJMP(lua_error(L)); |
| 3415 | } |
| 3416 | } |
| 3417 | |
Christopher Faulet | 0ae2e63 | 2023-01-10 15:29:54 +0100 | [diff] [blame] | 3418 | /* Wait for more data if possible if no length was specified and there |
| 3419 | * is no data or not enough data was received. |
| 3420 | */ |
| 3421 | if (!len || offset + len > output + input) { |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3422 | if (!HLUA_CANT_YIELD(hlua_gethlua(L)) && !channel_input_closed(chn) && channel_may_recv(chn)) { |
| 3423 | /* Yield waiting for more data, as requested */ |
| 3424 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_get_data_yield, TICK_ETERNITY, 0)); |
| 3425 | } |
Christopher Faulet | 0ae2e63 | 2023-01-10 15:29:54 +0100 | [diff] [blame] | 3426 | |
| 3427 | /* Return 'nil' if there is no data and the channel can't receive more data */ |
| 3428 | if (!len) { |
| 3429 | lua_pushnil(L); |
| 3430 | return -1; |
| 3431 | } |
| 3432 | |
| 3433 | /* Otherwise, return all data */ |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3434 | len = output + input - offset; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3435 | } |
| 3436 | |
| 3437 | dup: |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3438 | _hlua_channel_dup(chn, L, offset, len); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3439 | return 1; |
| 3440 | } |
| 3441 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3442 | /* Copies the first line (including the trailing LF) of input data in the |
| 3443 | * channel's buffer. It is possible to set a specific offset (0 by default) and |
| 3444 | * a length (all remaining input data starting for the offset by default). If |
| 3445 | * there is not enough input data and more data can be received, the function |
| 3446 | * yields. If a length is explicitly specified, no more data are |
| 3447 | * copied. Otherwise, if no LF is found and more data can be received, this |
| 3448 | * function yields. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3449 | * |
| 3450 | * From an action, All input data are considered. For a filter, the offset and |
| 3451 | * 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] | 3452 | */ |
| 3453 | __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] | 3454 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3455 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3456 | struct filter *filter; |
| 3457 | size_t l, input, output; |
| 3458 | int offset, len; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3459 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3460 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3461 | output = co_data(chn); |
| 3462 | input = ci_data(chn); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3463 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3464 | filter = hlua_channel_filter(L, 1, chn, &output, &input); |
| 3465 | if (filter && !hlua_filter_from_payload(filter)) |
| 3466 | WILL_LJMP(lua_error(L)); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3467 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3468 | offset = output; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3469 | if (lua_gettop(L) > 1) { |
| 3470 | offset = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3471 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 3472 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3473 | offset += output; |
| 3474 | if (offset < output || offset > input + output) { |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3475 | lua_pushfstring(L, "offset out of range."); |
| 3476 | WILL_LJMP(lua_error(L)); |
| 3477 | } |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3478 | } |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3479 | |
| 3480 | len = output + input - offset; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3481 | if (lua_gettop(L) == 3) { |
| 3482 | len = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3483 | if (!len) |
| 3484 | goto dup; |
| 3485 | if (len == -1) |
| 3486 | len = global.tune.bufsize; |
| 3487 | if (len < 0) { |
| 3488 | lua_pushfstring(L, "length out of range."); |
| 3489 | WILL_LJMP(lua_error(L)); |
| 3490 | } |
| 3491 | } |
| 3492 | |
| 3493 | for (l = 0; l < len; l++) { |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3494 | if (l + offset >= output + input) |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3495 | break; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3496 | if (*(b_peek(&chn->buf, offset + l)) == '\n') { |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3497 | len = l+1; |
| 3498 | goto dup; |
| 3499 | } |
| 3500 | } |
| 3501 | |
Christopher Faulet | 0ae2e63 | 2023-01-10 15:29:54 +0100 | [diff] [blame] | 3502 | /* Wait for more data if possible if no line is found and no length was |
| 3503 | * specified or not enough data was received. |
| 3504 | */ |
| 3505 | if (lua_gettop(L) != 3 || offset + len > output + input) { |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3506 | if (!HLUA_CANT_YIELD(hlua_gethlua(L)) && !channel_input_closed(chn) && channel_may_recv(chn)) { |
| 3507 | /* Yield waiting for more data */ |
| 3508 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_get_line_yield, TICK_ETERNITY, 0)); |
| 3509 | } |
Christopher Faulet | 0ae2e63 | 2023-01-10 15:29:54 +0100 | [diff] [blame] | 3510 | |
| 3511 | /* Return 'nil' if there is no data and the channel can't receive more data */ |
| 3512 | if (!len) { |
| 3513 | lua_pushnil(L); |
| 3514 | return -1; |
| 3515 | } |
| 3516 | |
| 3517 | /* Otherwise, return all data */ |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3518 | len = output + input - offset; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3519 | } |
| 3520 | |
| 3521 | dup: |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3522 | _hlua_channel_dup(chn, L, offset, len); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3523 | return 1; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3524 | } |
| 3525 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3526 | /* [ DEPRECATED ] |
| 3527 | * |
| 3528 | * Duplicate all input data foud in the channel's buffer. The data are not |
| 3529 | * removed from the buffer. This function relies on _hlua_channel_dup(). |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3530 | * |
| 3531 | * From an action, All input data are considered. For a filter, the offset and |
| 3532 | * 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] | 3533 | */ |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3534 | __LJMP static int hlua_channel_dup(lua_State *L) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3535 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3536 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3537 | struct filter *filter; |
| 3538 | size_t offset, len; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3539 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3540 | MAY_LJMP(check_args(L, 1, "dup")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3541 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3542 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3543 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3544 | WILL_LJMP(lua_error(L)); |
| 3545 | } |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3546 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3547 | offset = co_data(chn); |
| 3548 | len = ci_data(chn); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3549 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3550 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 3551 | if (filter && !hlua_filter_from_payload(filter)) |
| 3552 | WILL_LJMP(lua_error(L)); |
| 3553 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3554 | if (!ci_data(chn) && channel_input_closed(chn)) { |
| 3555 | lua_pushnil(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3556 | return 1; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3557 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3558 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3559 | _hlua_channel_dup(chn, L, offset, len); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3560 | return 1; |
| 3561 | } |
| 3562 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3563 | /* [ DEPRECATED ] |
| 3564 | * |
| 3565 | * Get all input data foud in the channel's buffer. The data are removed from |
| 3566 | * the buffer after the copy. This function relies on _hlua_channel_dup() and |
| 3567 | * _hlua_channel_delete(). |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3568 | * |
| 3569 | * From an action, All input data are considered. For a filter, the offset and |
| 3570 | * 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] | 3571 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3572 | __LJMP static int hlua_channel_get(lua_State *L) |
| 3573 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3574 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3575 | struct filter *filter; |
| 3576 | size_t offset, len; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3577 | int ret; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3578 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3579 | MAY_LJMP(check_args(L, 1, "get")); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3580 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3581 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3582 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3583 | WILL_LJMP(lua_error(L)); |
| 3584 | } |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3585 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3586 | offset = co_data(chn); |
| 3587 | len = ci_data(chn); |
| 3588 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3589 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 3590 | if (filter && !hlua_filter_from_payload(filter)) |
| 3591 | WILL_LJMP(lua_error(L)); |
| 3592 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3593 | if (!ci_data(chn) && channel_input_closed(chn)) { |
| 3594 | lua_pushnil(L); |
| 3595 | return 1; |
| 3596 | } |
| 3597 | |
| 3598 | ret = _hlua_channel_dup(chn, L, offset, len); |
| 3599 | _hlua_channel_delete(chn, offset, ret); |
| 3600 | return 1; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3601 | } |
| 3602 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3603 | /* This functions consumes and returns one line. If the channel is closed, |
| 3604 | * 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] | 3605 | * 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] | 3606 | * value. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3607 | * |
| 3608 | * From an action, All input data are considered. For a filter, the offset and |
| 3609 | * 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] | 3610 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3611 | __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] | 3612 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3613 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3614 | struct filter *filter; |
| 3615 | size_t l, offset, len; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3616 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3617 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3618 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3619 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3620 | offset = co_data(chn); |
| 3621 | len = ci_data(chn); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3622 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3623 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 3624 | if (filter && !hlua_filter_from_payload(filter)) |
| 3625 | WILL_LJMP(lua_error(L)); |
| 3626 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3627 | if (!ci_data(chn) && channel_input_closed(chn)) { |
| 3628 | lua_pushnil(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3629 | return 1; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3630 | } |
| 3631 | |
| 3632 | for (l = 0; l < len; l++) { |
| 3633 | if (*(b_peek(&chn->buf, offset+l)) == '\n') { |
| 3634 | len = l+1; |
| 3635 | goto dup; |
| 3636 | } |
| 3637 | } |
| 3638 | |
| 3639 | if (!HLUA_CANT_YIELD(hlua_gethlua(L)) && !channel_input_closed(chn) && channel_may_recv(chn)) { |
| 3640 | /* Yield waiting for more data */ |
| 3641 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_getline_yield, TICK_ETERNITY, 0)); |
| 3642 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3643 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3644 | dup: |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3645 | ret = _hlua_channel_dup(chn, L, offset, len); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3646 | _hlua_channel_delete(chn, offset, ret); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3647 | return 1; |
| 3648 | } |
| 3649 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3650 | /* [ DEPRECATED ] |
| 3651 | * |
| 3652 | * Check arguments for the function "hlua_channel_getline_yield". |
| 3653 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3654 | __LJMP static int hlua_channel_getline(lua_State *L) |
| 3655 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3656 | struct channel *chn; |
| 3657 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3658 | MAY_LJMP(check_args(L, 1, "getline")); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3659 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3660 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3661 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3662 | WILL_LJMP(lua_error(L)); |
| 3663 | } |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3664 | return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0)); |
| 3665 | } |
| 3666 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3667 | /* Retrieves a given amount of input data at the given offset. By default all |
| 3668 | * available input data are returned. The offset may be negactive to start from |
| 3669 | * the end of input data. The length may be -1 to set it to the maximum buffer |
| 3670 | * size. |
| 3671 | */ |
| 3672 | __LJMP static int hlua_channel_get_data(lua_State *L) |
| 3673 | { |
| 3674 | struct channel *chn; |
| 3675 | |
| 3676 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 3677 | WILL_LJMP(luaL_error(L, "'data' expects at most 2 arguments")); |
| 3678 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3679 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3680 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3681 | WILL_LJMP(lua_error(L)); |
| 3682 | } |
| 3683 | return MAY_LJMP(hlua_channel_get_data_yield(L, 0, 0)); |
| 3684 | } |
| 3685 | |
| 3686 | /* Retrieves a given amount of input data at the given offset. By default all |
| 3687 | * available input data are returned. The offset may be negactive to start from |
| 3688 | * the end of input data. The length may be -1 to set it to the maximum buffer |
| 3689 | * size. |
| 3690 | */ |
| 3691 | __LJMP static int hlua_channel_get_line(lua_State *L) |
| 3692 | { |
| 3693 | struct channel *chn; |
| 3694 | |
| 3695 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 3696 | WILL_LJMP(luaL_error(L, "'line' expects at most 2 arguments")); |
| 3697 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3698 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3699 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3700 | WILL_LJMP(lua_error(L)); |
| 3701 | } |
| 3702 | return MAY_LJMP(hlua_channel_get_line_yield(L, 0, 0)); |
| 3703 | } |
| 3704 | |
| 3705 | /* Appends a string into the input side of channel. It returns the length of the |
| 3706 | * written string, or -1 if the channel is closed or if the buffer size is too |
| 3707 | * little for the data. 0 may be returned if nothing is copied. This function |
| 3708 | * does not yield. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3709 | * |
| 3710 | * For a filter, the context is updated on success. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3711 | */ |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3712 | __LJMP static int hlua_channel_append(lua_State *L) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3713 | { |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3714 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3715 | struct filter *filter; |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3716 | const char *str; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3717 | size_t sz, offset, len; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3718 | int ret; |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3719 | |
| 3720 | MAY_LJMP(check_args(L, 2, "append")); |
| 3721 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3722 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 3723 | if (IS_HTX_STRM(chn_strm(chn))) { |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3724 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3725 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3726 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3727 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3728 | offset = co_data(chn); |
| 3729 | len = ci_data(chn); |
| 3730 | |
| 3731 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 3732 | if (filter && !hlua_filter_from_payload(filter)) |
| 3733 | WILL_LJMP(lua_error(L)); |
| 3734 | |
| 3735 | ret = _hlua_channel_insert(chn, L, ist2(str, sz), offset); |
| 3736 | if (ret > 0 && filter) { |
| 3737 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 3738 | |
| 3739 | flt_update_offsets(filter, chn, ret); |
| 3740 | flt_ctx->cur_len[CHN_IDX(chn)] += ret; |
| 3741 | } |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3742 | lua_pushinteger(L, ret); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3743 | return 1; |
| 3744 | } |
| 3745 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3746 | /* Prepends a string into the input side of channel. It returns the length of the |
| 3747 | * written string, or -1 if the channel is closed or if the buffer size is too |
| 3748 | * little for the data. 0 may be returned if nothing is copied. This function |
| 3749 | * does not yield. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3750 | * |
| 3751 | * For a filter, the context is updated on success. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3752 | */ |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3753 | __LJMP static int hlua_channel_prepend(lua_State *L) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3754 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3755 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3756 | struct filter *filter; |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3757 | const char *str; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3758 | size_t sz, offset, len; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3759 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3760 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3761 | MAY_LJMP(check_args(L, 2, "prepend")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3762 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3763 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 3764 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3765 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3766 | WILL_LJMP(lua_error(L)); |
| 3767 | } |
| 3768 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3769 | offset = co_data(chn); |
| 3770 | len = ci_data(chn); |
| 3771 | |
| 3772 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 3773 | if (filter && !hlua_filter_from_payload(filter)) |
| 3774 | WILL_LJMP(lua_error(L)); |
| 3775 | |
| 3776 | ret = _hlua_channel_insert(chn, L, ist2(str, sz), offset); |
| 3777 | if (ret > 0 && filter) { |
| 3778 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 3779 | |
| 3780 | flt_update_offsets(filter, chn, ret); |
| 3781 | flt_ctx->cur_len[CHN_IDX(chn)] += ret; |
| 3782 | } |
| 3783 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3784 | lua_pushinteger(L, ret); |
| 3785 | return 1; |
| 3786 | } |
| 3787 | |
| 3788 | /* 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] | 3789 | * 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] | 3790 | * returns the length of the written string, or -1 if the channel is closed or |
| 3791 | * if the buffer size is too little for the data. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3792 | * |
| 3793 | * For a filter, the context is updated on success. |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3794 | */ |
| 3795 | __LJMP static int hlua_channel_insert_data(lua_State *L) |
| 3796 | { |
| 3797 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3798 | struct filter *filter; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3799 | const char *str; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3800 | size_t sz, input, output; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3801 | int ret, offset; |
| 3802 | |
| 3803 | if (lua_gettop(L) < 2 || lua_gettop(L) > 3) |
| 3804 | WILL_LJMP(luaL_error(L, "'insert' expects at least 1 argument and at most 2 arguments")); |
| 3805 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3806 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3807 | |
| 3808 | output = co_data(chn); |
| 3809 | input = ci_data(chn); |
| 3810 | |
| 3811 | filter = hlua_channel_filter(L, 1, chn, &output, &input); |
| 3812 | if (filter && !hlua_filter_from_payload(filter)) |
| 3813 | WILL_LJMP(lua_error(L)); |
| 3814 | |
Aurelien DARRAGON | afb7daf | 2022-10-04 12:16:05 +0200 | [diff] [blame] | 3815 | offset = output; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3816 | if (lua_gettop(L) > 2) { |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3817 | offset = MAY_LJMP(luaL_checkinteger(L, 3)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3818 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 3819 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3820 | offset += output; |
Aurelien DARRAGON | afb7daf | 2022-10-04 12:16:05 +0200 | [diff] [blame] | 3821 | if (offset > output + input) { |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3822 | lua_pushfstring(L, "offset out of range."); |
| 3823 | WILL_LJMP(lua_error(L)); |
| 3824 | } |
| 3825 | } |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3826 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3827 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3828 | WILL_LJMP(lua_error(L)); |
| 3829 | } |
| 3830 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3831 | ret = _hlua_channel_insert(chn, L, ist2(str, sz), offset); |
| 3832 | if (ret > 0 && filter) { |
| 3833 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3834 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3835 | flt_update_offsets(filter, chn, ret); |
| 3836 | flt_ctx->cur_len[CHN_IDX(chn)] += ret; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3837 | } |
| 3838 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3839 | lua_pushinteger(L, ret); |
| 3840 | return 1; |
| 3841 | } |
| 3842 | /* Replaces a given amount of input data at the given offset by a string |
| 3843 | * content. By default all remaining data are removed (offset = 0 and len = |
| 3844 | * -1). It returns the length of the written string, or -1 if the channel is |
| 3845 | * closed or if the buffer size is too little for the data. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3846 | * |
| 3847 | * For a filter, the context is updated on success. |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3848 | */ |
| 3849 | __LJMP static int hlua_channel_set_data(lua_State *L) |
| 3850 | { |
| 3851 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3852 | struct filter *filter; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3853 | const char *str; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3854 | size_t sz, input, output; |
| 3855 | int ret, offset, len; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3856 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3857 | if (lua_gettop(L) < 2 || lua_gettop(L) > 4) |
| 3858 | WILL_LJMP(luaL_error(L, "'set' expects at least 1 argument and at most 3 arguments")); |
| 3859 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3860 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3861 | |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 3862 | if (IS_HTX_STRM(chn_strm(chn))) { |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3863 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3864 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3865 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3866 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3867 | output = co_data(chn); |
| 3868 | input = ci_data(chn); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3869 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3870 | filter = hlua_channel_filter(L, 1, chn, &output, &input); |
| 3871 | if (filter && !hlua_filter_from_payload(filter)) |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3872 | WILL_LJMP(lua_error(L)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3873 | |
| 3874 | offset = output; |
| 3875 | if (lua_gettop(L) > 2) { |
| 3876 | offset = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3877 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 3878 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3879 | offset += output; |
| 3880 | if (offset < output || offset > input + output) { |
| 3881 | lua_pushfstring(L, "offset out of range."); |
| 3882 | WILL_LJMP(lua_error(L)); |
| 3883 | } |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3884 | } |
| 3885 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3886 | len = output + input - offset; |
| 3887 | if (lua_gettop(L) == 4) { |
| 3888 | len = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 3889 | if (!len) |
| 3890 | goto set; |
| 3891 | if (len == -1) |
| 3892 | len = output + input - offset; |
| 3893 | if (len < 0 || offset + len > output + input) { |
| 3894 | lua_pushfstring(L, "length out of range."); |
| 3895 | WILL_LJMP(lua_error(L)); |
| 3896 | } |
| 3897 | } |
| 3898 | |
| 3899 | set: |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3900 | /* 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] | 3901 | if (sz > c_room(chn) + len) |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3902 | lua_pushinteger(L, -1); |
| 3903 | else { |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3904 | _hlua_channel_delete(chn, offset, len); |
| 3905 | ret = _hlua_channel_insert(chn, L, ist2(str, sz), offset); |
| 3906 | if (filter) { |
| 3907 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 3908 | |
| 3909 | len -= (ret > 0 ? ret : 0); |
| 3910 | flt_update_offsets(filter, chn, -len); |
| 3911 | flt_ctx->cur_len[CHN_IDX(chn)] -= len; |
| 3912 | } |
| 3913 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3914 | lua_pushinteger(L, ret); |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3915 | } |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3916 | return 1; |
| 3917 | } |
| 3918 | |
| 3919 | /* 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] | 3920 | * 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] | 3921 | * the removed data. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3922 | * |
| 3923 | * For a filter, the context is updated on success. |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3924 | */ |
| 3925 | __LJMP static int hlua_channel_del_data(lua_State *L) |
| 3926 | { |
| 3927 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3928 | struct filter *filter; |
| 3929 | size_t input, output; |
| 3930 | int offset, len; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3931 | |
| 3932 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 3933 | WILL_LJMP(luaL_error(L, "'remove' expects at most 2 arguments")); |
| 3934 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3935 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3936 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3937 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3938 | WILL_LJMP(lua_error(L)); |
| 3939 | } |
| 3940 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3941 | output = co_data(chn); |
| 3942 | input = ci_data(chn); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3943 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3944 | filter = hlua_channel_filter(L, 1, chn, &output, &input); |
| 3945 | if (filter && !hlua_filter_from_payload(filter)) |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3946 | WILL_LJMP(lua_error(L)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3947 | |
| 3948 | offset = output; |
Boyang Li | e0c5435 | 2022-05-10 17:47:23 +0000 | [diff] [blame] | 3949 | if (lua_gettop(L) > 1) { |
| 3950 | offset = MAY_LJMP(luaL_checkinteger(L, 2)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3951 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 3952 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3953 | offset += output; |
| 3954 | if (offset < output || offset > input + output) { |
| 3955 | lua_pushfstring(L, "offset out of range."); |
| 3956 | WILL_LJMP(lua_error(L)); |
| 3957 | } |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3958 | } |
| 3959 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3960 | len = output + input - offset; |
Boyang Li | e0c5435 | 2022-05-10 17:47:23 +0000 | [diff] [blame] | 3961 | if (lua_gettop(L) == 3) { |
| 3962 | len = MAY_LJMP(luaL_checkinteger(L, 3)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3963 | if (!len) |
| 3964 | goto end; |
| 3965 | if (len == -1) |
| 3966 | len = output + input - offset; |
| 3967 | if (len < 0 || offset + len > output + input) { |
| 3968 | lua_pushfstring(L, "length out of range."); |
| 3969 | WILL_LJMP(lua_error(L)); |
| 3970 | } |
| 3971 | } |
| 3972 | |
| 3973 | _hlua_channel_delete(chn, offset, len); |
| 3974 | if (filter) { |
| 3975 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 3976 | |
| 3977 | flt_update_offsets(filter, chn, -len); |
| 3978 | flt_ctx->cur_len[CHN_IDX(chn)] -= len; |
| 3979 | } |
| 3980 | |
| 3981 | end: |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3982 | lua_pushinteger(L, len); |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3983 | return 1; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3984 | } |
| 3985 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3986 | /* Append data in the output side of the buffer. This data is immediately |
| 3987 | * sent. The function returns the amount of data written. If the buffer |
| 3988 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3989 | * if the channel is closed. |
| 3990 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3991 | __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] | 3992 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3993 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3994 | struct filter *filter; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3995 | const char *str; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3996 | size_t offset, len, sz; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3997 | int l, ret; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 3998 | struct hlua *hlua; |
| 3999 | |
| 4000 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 4001 | hlua = hlua_gethlua(L); |
| 4002 | if (!hlua) { |
| 4003 | lua_pushnil(L); |
| 4004 | return 1; |
| 4005 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4006 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 4007 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 4008 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 4009 | l = MAY_LJMP(luaL_checkinteger(L, 3)); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 4010 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4011 | offset = co_data(chn); |
| 4012 | len = ci_data(chn); |
| 4013 | |
| 4014 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 4015 | if (filter && !hlua_filter_from_payload(filter)) |
| 4016 | WILL_LJMP(lua_error(L)); |
| 4017 | |
| 4018 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 4019 | if (unlikely(channel_output_closed(chn))) { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4020 | lua_pushinteger(L, -1); |
| 4021 | return 1; |
| 4022 | } |
| 4023 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 4024 | len = c_room(chn); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4025 | if (len > sz -l) { |
| 4026 | if (filter) { |
| 4027 | lua_pushinteger(L, -1); |
| 4028 | return 1; |
| 4029 | } |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 4030 | len = sz - l; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4031 | } |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 4032 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4033 | ret = _hlua_channel_insert(chn, L, ist2(str, len), offset); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 4034 | if (ret == -1) { |
| 4035 | lua_pop(L, 1); |
| 4036 | lua_pushinteger(L, -1); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 4037 | return 1; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 4038 | } |
| 4039 | if (ret) { |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4040 | if (filter) { |
| 4041 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 4042 | |
| 4043 | |
| 4044 | flt_update_offsets(filter, chn, ret); |
| 4045 | FLT_OFF(filter, chn) += ret; |
| 4046 | flt_ctx->cur_off[CHN_IDX(chn)] += ret; |
| 4047 | } |
| 4048 | else |
| 4049 | c_adv(chn, ret); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4050 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 4051 | l += ret; |
| 4052 | lua_pop(L, 1); |
| 4053 | lua_pushinteger(L, l); |
| 4054 | } |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4055 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 4056 | if (l < sz) { |
| 4057 | /* Yield only if the channel's output is not empty. |
| 4058 | * Otherwise it means we cannot add more data. */ |
| 4059 | if (co_data(chn) == 0 || HLUA_CANT_YIELD(hlua_gethlua(L))) |
Christopher Faulet | 2e60aa4 | 2021-08-05 11:58:37 +0200 | [diff] [blame] | 4060 | return 1; |
| 4061 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 4062 | /* If we are waiting for space in the response buffer, we |
| 4063 | * must set the flag WAKERESWR. This flag required the task |
| 4064 | * wake up if any activity is detected on the response buffer. |
| 4065 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 4066 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 4067 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 4068 | else |
| 4069 | HLUA_SET_WAKEREQWR(hlua); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4070 | 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] | 4071 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4072 | |
| 4073 | return 1; |
| 4074 | } |
| 4075 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4076 | /* Just a wrapper of "_hlua_channel_send". This wrapper permits |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4077 | * yield the LUA process, and resume it without checking the |
| 4078 | * input arguments. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4079 | * |
| 4080 | * This function cannot be called from a filter. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4081 | */ |
| 4082 | __LJMP static int hlua_channel_send(lua_State *L) |
| 4083 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 4084 | struct channel *chn; |
| 4085 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4086 | MAY_LJMP(check_args(L, 2, "send")); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 4087 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 4088 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 4089 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 4090 | WILL_LJMP(lua_error(L)); |
| 4091 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4092 | lua_pushinteger(L, 0); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 4093 | return MAY_LJMP(hlua_channel_send_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4094 | } |
| 4095 | |
| 4096 | /* This function forward and amount of butes. The data pass from |
| 4097 | * the input side of the buffer to the output side, and can be |
| 4098 | * forwarded. This function never fails. |
| 4099 | * |
| 4100 | * The Lua function takes an amount of bytes to be forwarded in |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4101 | * input. It returns the number of bytes forwarded. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4102 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 4103 | __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] | 4104 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 4105 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4106 | struct filter *filter; |
| 4107 | size_t offset, len, fwd; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 4108 | int l, max; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 4109 | struct hlua *hlua; |
| 4110 | |
| 4111 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 4112 | hlua = hlua_gethlua(L); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 4113 | if (!hlua) { |
| 4114 | lua_pushnil(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 4115 | return 1; |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 4116 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 4117 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 4118 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4119 | fwd = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4120 | l = MAY_LJMP(luaL_checkinteger(L, -1)); |
| 4121 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4122 | offset = co_data(chn); |
| 4123 | len = ci_data(chn); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 4124 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4125 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 4126 | if (filter && !hlua_filter_from_payload(filter)) |
| 4127 | WILL_LJMP(lua_error(L)); |
| 4128 | |
| 4129 | max = fwd - l; |
| 4130 | if (max > len) |
| 4131 | max = len; |
| 4132 | |
| 4133 | if (filter) { |
| 4134 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 4135 | |
| 4136 | FLT_OFF(filter, chn) += max; |
| 4137 | flt_ctx->cur_off[CHN_IDX(chn)] += max; |
| 4138 | flt_ctx->cur_len[CHN_IDX(chn)] -= max; |
| 4139 | } |
| 4140 | else |
| 4141 | channel_forward(chn, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4142 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 4143 | l += max; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4144 | lua_pop(L, 1); |
| 4145 | lua_pushinteger(L, l); |
| 4146 | |
| 4147 | /* Check if it miss bytes to forward. */ |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4148 | if (l < fwd) { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4149 | /* The the input channel or the output channel are closed, we |
| 4150 | * must return the amount of data forwarded. |
| 4151 | */ |
Christopher Faulet | 2e60aa4 | 2021-08-05 11:58:37 +0200 | [diff] [blame] | 4152 | 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] | 4153 | return 1; |
| 4154 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 4155 | /* If we are waiting for space data in the response buffer, we |
| 4156 | * must set the flag WAKERESWR. This flag required the task |
| 4157 | * wake up if any activity is detected on the response buffer. |
| 4158 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 4159 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 4160 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 4161 | else |
| 4162 | HLUA_SET_WAKEREQWR(hlua); |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 4163 | |
Ilya Shipitsin | 4a689da | 2022-10-29 09:34:32 +0500 | [diff] [blame] | 4164 | /* Otherwise, we can yield waiting for new data in the input side. */ |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4165 | 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] | 4166 | } |
| 4167 | |
| 4168 | return 1; |
| 4169 | } |
| 4170 | |
| 4171 | /* Just check the input and prepare the stack for the previous |
| 4172 | * function "hlua_channel_forward_yield" |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4173 | * |
| 4174 | * This function cannot be called from a filter. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4175 | */ |
| 4176 | __LJMP static int hlua_channel_forward(lua_State *L) |
| 4177 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 4178 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4179 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 4180 | MAY_LJMP(check_args(L, 2, "forward")); |
| 4181 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 4182 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 4183 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 4184 | WILL_LJMP(lua_error(L)); |
| 4185 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4186 | lua_pushinteger(L, 0); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 4187 | return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4188 | } |
| 4189 | |
| 4190 | /* Just returns the number of bytes available in the input |
| 4191 | * side of the buffer. This function never fails. |
| 4192 | */ |
| 4193 | __LJMP static int hlua_channel_get_in_len(lua_State *L) |
| 4194 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 4195 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4196 | struct filter *filter; |
| 4197 | size_t output, input; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 4198 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 4199 | MAY_LJMP(check_args(L, 1, "input")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 4200 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4201 | |
| 4202 | output = co_data(chn); |
| 4203 | input = ci_data(chn); |
| 4204 | filter = hlua_channel_filter(L, 1, chn, &output, &input); |
| 4205 | if (filter || !IS_HTX_STRM(chn_strm(chn))) |
| 4206 | lua_pushinteger(L, input); |
| 4207 | else { |
Christopher Faulet | a3ceac1 | 2018-12-14 13:39:09 +0100 | [diff] [blame] | 4208 | struct htx *htx = htxbuf(&chn->buf); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4209 | |
Christopher Faulet | a3ceac1 | 2018-12-14 13:39:09 +0100 | [diff] [blame] | 4210 | lua_pushinteger(L, htx->data - co_data(chn)); |
| 4211 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4212 | return 1; |
| 4213 | } |
| 4214 | |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 4215 | /* Returns true if the channel is full. */ |
| 4216 | __LJMP static int hlua_channel_is_full(lua_State *L) |
| 4217 | { |
| 4218 | struct channel *chn; |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 4219 | |
| 4220 | MAY_LJMP(check_args(L, 1, "is_full")); |
| 4221 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 0ec740e | 2020-02-26 11:59:19 +0100 | [diff] [blame] | 4222 | /* ignore the reserve, we are not on a producer side (ie in an |
| 4223 | * applet). |
| 4224 | */ |
| 4225 | lua_pushboolean(L, channel_full(chn, 0)); |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 4226 | return 1; |
| 4227 | } |
| 4228 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 4229 | /* Returns true if the channel may still receive data. */ |
| 4230 | __LJMP static int hlua_channel_may_recv(lua_State *L) |
| 4231 | { |
| 4232 | struct channel *chn; |
| 4233 | |
| 4234 | MAY_LJMP(check_args(L, 1, "may_recv")); |
| 4235 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 4236 | lua_pushboolean(L, (!channel_input_closed(chn) && channel_may_recv(chn))); |
| 4237 | return 1; |
| 4238 | } |
| 4239 | |
Christopher Faulet | 2ac9ba2 | 2020-02-25 10:15:50 +0100 | [diff] [blame] | 4240 | /* Returns true if the channel is the response channel. */ |
| 4241 | __LJMP static int hlua_channel_is_resp(lua_State *L) |
| 4242 | { |
| 4243 | struct channel *chn; |
| 4244 | |
| 4245 | MAY_LJMP(check_args(L, 1, "is_resp")); |
| 4246 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 4247 | |
| 4248 | lua_pushboolean(L, !!(chn->flags & CF_ISRESP)); |
| 4249 | return 1; |
| 4250 | } |
| 4251 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4252 | /* Just returns the number of bytes available in the output |
| 4253 | * side of the buffer. This function never fails. |
| 4254 | */ |
| 4255 | __LJMP static int hlua_channel_get_out_len(lua_State *L) |
| 4256 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 4257 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4258 | size_t output, input; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 4259 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 4260 | MAY_LJMP(check_args(L, 1, "output")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 4261 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4262 | |
| 4263 | output = co_data(chn); |
| 4264 | input = ci_data(chn); |
| 4265 | hlua_channel_filter(L, 1, chn, &output, &input); |
| 4266 | |
| 4267 | lua_pushinteger(L, output); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4268 | return 1; |
| 4269 | } |
| 4270 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4271 | /* |
| 4272 | * |
| 4273 | * |
| 4274 | * Class Fetches |
| 4275 | * |
| 4276 | * |
| 4277 | */ |
| 4278 | |
| 4279 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4280 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4281 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4282 | __LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud) |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4283 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4284 | return MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4285 | } |
| 4286 | |
| 4287 | /* This function creates and push in the stack a fetch object according |
| 4288 | * with a current TXN. |
| 4289 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4290 | 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] | 4291 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 4292 | struct hlua_smp *hsmp; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4293 | |
| 4294 | /* Check stack size. */ |
| 4295 | if (!lua_checkstack(L, 3)) |
| 4296 | return 0; |
| 4297 | |
| 4298 | /* Create the object: obj[0] = userdata. |
| 4299 | * Note that the base of the Fetches object is the |
| 4300 | * transaction object. |
| 4301 | */ |
| 4302 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 4303 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4304 | lua_rawseti(L, -2, 0); |
| 4305 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 4306 | hsmp->s = txn->s; |
| 4307 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 4308 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4309 | hsmp->flags = flags; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4310 | |
| 4311 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 4312 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref); |
| 4313 | lua_setmetatable(L, -2); |
| 4314 | |
| 4315 | return 1; |
| 4316 | } |
| 4317 | |
| 4318 | /* This function is an LUA binding. It is called with each sample-fetch. |
| 4319 | * It uses closure argument to store the associated sample-fetch. It |
| 4320 | * returns only one argument or throws an error. An error is thrown |
| 4321 | * only if an error is encountered during the argument parsing. If |
| 4322 | * the "sample-fetch" function fails, nil is returned. |
| 4323 | */ |
| 4324 | __LJMP static int hlua_run_sample_fetch(lua_State *L) |
| 4325 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 4326 | struct hlua_smp *hsmp; |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 4327 | struct sample_fetch *f; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 4328 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4329 | int i; |
| 4330 | struct sample smp; |
| 4331 | |
| 4332 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4333 | f = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4334 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4335 | /* Get traditional arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 4336 | hsmp = MAY_LJMP(hlua_checkfetches(L, 1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4337 | |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 4338 | /* Check execution authorization. */ |
| 4339 | if (f->use & SMP_USE_HTTP_ANY && |
| 4340 | !(hsmp->flags & HLUA_F_MAY_USE_HTTP)) { |
| 4341 | lua_pushfstring(L, "the sample-fetch '%s' needs an HTTP parser which " |
| 4342 | "is not available in Lua services", f->kw); |
| 4343 | WILL_LJMP(lua_error(L)); |
| 4344 | } |
| 4345 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4346 | /* Get extra arguments. */ |
| 4347 | for (i = 0; i < lua_gettop(L) - 1; i++) { |
| 4348 | if (i >= ARGM_NBARGS) |
| 4349 | break; |
| 4350 | hlua_lua2arg(L, i + 2, &args[i]); |
| 4351 | } |
| 4352 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4353 | args[i].data.str.area = NULL; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4354 | |
| 4355 | /* Check arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 4356 | 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] | 4357 | |
| 4358 | /* Run the special args checker. */ |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 4359 | if (f->val_args && !f->val_args(args, NULL)) { |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4360 | lua_pushfstring(L, "error in arguments"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4361 | goto error; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4362 | } |
| 4363 | |
| 4364 | /* Initialise the sample. */ |
| 4365 | memset(&smp, 0, sizeof(smp)); |
| 4366 | |
| 4367 | /* Run the sample fetch process. */ |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 4368 | 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] | 4369 | if (!f->process(args, &smp, f->kw, f->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4370 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4371 | lua_pushstring(L, ""); |
| 4372 | else |
| 4373 | lua_pushnil(L); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4374 | goto end; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4375 | } |
| 4376 | |
| 4377 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4378 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4379 | hlua_smp2lua_str(L, &smp); |
| 4380 | else |
| 4381 | hlua_smp2lua(L, &smp); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4382 | |
| 4383 | end: |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 4384 | free_args(args); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4385 | return 1; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4386 | |
| 4387 | error: |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 4388 | free_args(args); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4389 | WILL_LJMP(lua_error(L)); |
| 4390 | return 0; /* Never reached */ |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4391 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4392 | |
| 4393 | /* |
| 4394 | * |
| 4395 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4396 | * Class Converters |
| 4397 | * |
| 4398 | * |
| 4399 | */ |
| 4400 | |
| 4401 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4402 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4403 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4404 | __LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4405 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4406 | return MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4407 | } |
| 4408 | |
| 4409 | /* This function creates and push in the stack a Converters object |
| 4410 | * according with a current TXN. |
| 4411 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4412 | 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] | 4413 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 4414 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4415 | |
| 4416 | /* Check stack size. */ |
| 4417 | if (!lua_checkstack(L, 3)) |
| 4418 | return 0; |
| 4419 | |
| 4420 | /* Create the object: obj[0] = userdata. |
| 4421 | * Note that the base of the Converters object is the |
| 4422 | * same than the TXN object. |
| 4423 | */ |
| 4424 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 4425 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4426 | lua_rawseti(L, -2, 0); |
| 4427 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 4428 | hsmp->s = txn->s; |
| 4429 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 4430 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4431 | hsmp->flags = flags; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4432 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4433 | /* Pop a class stream metatable and affect it to the table. */ |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4434 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref); |
| 4435 | lua_setmetatable(L, -2); |
| 4436 | |
| 4437 | return 1; |
| 4438 | } |
| 4439 | |
| 4440 | /* This function is an LUA binding. It is called with each converter. |
| 4441 | * It uses closure argument to store the associated converter. It |
| 4442 | * returns only one argument or throws an error. An error is thrown |
| 4443 | * only if an error is encountered during the argument parsing. If |
| 4444 | * the converter function function fails, nil is returned. |
| 4445 | */ |
| 4446 | __LJMP static int hlua_run_sample_conv(lua_State *L) |
| 4447 | { |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 4448 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4449 | struct sample_conv *conv; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 4450 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4451 | int i; |
| 4452 | struct sample smp; |
| 4453 | |
| 4454 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4455 | conv = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4456 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4457 | /* Get traditional arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 4458 | hsmp = MAY_LJMP(hlua_checkconverters(L, 1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4459 | |
| 4460 | /* Get extra arguments. */ |
| 4461 | for (i = 0; i < lua_gettop(L) - 2; i++) { |
| 4462 | if (i >= ARGM_NBARGS) |
| 4463 | break; |
| 4464 | hlua_lua2arg(L, i + 3, &args[i]); |
| 4465 | } |
| 4466 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4467 | args[i].data.str.area = NULL; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4468 | |
| 4469 | /* Check arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 4470 | 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] | 4471 | |
| 4472 | /* Run the special args checker. */ |
| 4473 | if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) { |
| 4474 | hlua_pusherror(L, "error in arguments"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4475 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4476 | } |
| 4477 | |
| 4478 | /* Initialise the sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 4479 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4480 | if (!hlua_lua2smp(L, 2, &smp)) { |
| 4481 | hlua_pusherror(L, "error in the input argument"); |
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 | |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 4485 | smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR); |
| 4486 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4487 | /* Apply expected cast. */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 4488 | if (!sample_casts[smp.data.type][conv->in_type]) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4489 | hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'", |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 4490 | smp_to_type[smp.data.type], smp_to_type[conv->in_type]); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4491 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4492 | } |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 4493 | if (sample_casts[smp.data.type][conv->in_type] != c_none && |
| 4494 | !sample_casts[smp.data.type][conv->in_type](&smp)) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4495 | hlua_pusherror(L, "error during the input argument casting"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4496 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4497 | } |
| 4498 | |
| 4499 | /* Run the sample conversion process. */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 4500 | if (!conv->process(args, &smp, conv->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4501 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4502 | lua_pushstring(L, ""); |
| 4503 | else |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 4504 | lua_pushnil(L); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4505 | goto end; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4506 | } |
| 4507 | |
| 4508 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4509 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4510 | hlua_smp2lua_str(L, &smp); |
| 4511 | else |
| 4512 | hlua_smp2lua(L, &smp); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4513 | end: |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 4514 | free_args(args); |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 4515 | return 1; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4516 | |
| 4517 | error: |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 4518 | free_args(args); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4519 | WILL_LJMP(lua_error(L)); |
| 4520 | return 0; /* Never reached */ |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4521 | } |
| 4522 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4523 | /* |
| 4524 | * |
| 4525 | * |
| 4526 | * Class AppletTCP |
| 4527 | * |
| 4528 | * |
| 4529 | */ |
| 4530 | |
| 4531 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 4532 | * a class stream, otherwise it throws an error. |
| 4533 | */ |
| 4534 | __LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud) |
| 4535 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4536 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4537 | } |
| 4538 | |
| 4539 | /* This function creates and push in the stack an Applet object |
| 4540 | * according with a current TXN. |
| 4541 | */ |
| 4542 | static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx) |
| 4543 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4544 | struct hlua_appctx *luactx; |
Willy Tarreau | 0698c80 | 2022-05-11 14:09:57 +0200 | [diff] [blame] | 4545 | struct stream *s = appctx_strm(ctx); |
Christopher Faulet | 2da02ae | 2022-02-24 13:45:27 +0100 | [diff] [blame] | 4546 | struct proxy *p; |
| 4547 | |
| 4548 | ALREADY_CHECKED(s); |
| 4549 | p = s->be; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4550 | |
| 4551 | /* Check stack size. */ |
| 4552 | if (!lua_checkstack(L, 3)) |
| 4553 | return 0; |
| 4554 | |
| 4555 | /* Create the object: obj[0] = userdata. |
| 4556 | * Note that the base of the Converters object is the |
| 4557 | * same than the TXN object. |
| 4558 | */ |
| 4559 | lua_newtable(L); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4560 | luactx = lua_newuserdata(L, sizeof(*luactx)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4561 | lua_rawseti(L, -2, 0); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4562 | luactx->appctx = ctx; |
| 4563 | luactx->htxn.s = s; |
| 4564 | luactx->htxn.p = p; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4565 | |
| 4566 | /* Create the "f" field that contains a list of fetches. */ |
| 4567 | lua_pushstring(L, "f"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4568 | if (!hlua_fetches_new(L, &luactx->htxn, 0)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4569 | return 0; |
| 4570 | lua_settable(L, -3); |
| 4571 | |
| 4572 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 4573 | lua_pushstring(L, "sf"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4574 | if (!hlua_fetches_new(L, &luactx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4575 | return 0; |
| 4576 | lua_settable(L, -3); |
| 4577 | |
| 4578 | /* Create the "c" field that contains a list of converters. */ |
| 4579 | lua_pushstring(L, "c"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4580 | if (!hlua_converters_new(L, &luactx->htxn, 0)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4581 | return 0; |
| 4582 | lua_settable(L, -3); |
| 4583 | |
| 4584 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 4585 | lua_pushstring(L, "sc"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4586 | if (!hlua_converters_new(L, &luactx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4587 | return 0; |
| 4588 | lua_settable(L, -3); |
| 4589 | |
| 4590 | /* Pop a class stream metatable and affect it to the table. */ |
| 4591 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref); |
| 4592 | lua_setmetatable(L, -2); |
| 4593 | |
| 4594 | return 1; |
| 4595 | } |
| 4596 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4597 | __LJMP static int hlua_applet_tcp_set_var(lua_State *L) |
| 4598 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4599 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4600 | struct stream *s; |
| 4601 | const char *name; |
| 4602 | size_t len; |
| 4603 | struct sample smp; |
| 4604 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 4605 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 4606 | 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] | 4607 | |
| 4608 | /* It is useles to retrieve the stream, but this function |
| 4609 | * runs only in a stream context. |
| 4610 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4611 | luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4612 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4613 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4614 | |
| 4615 | /* Converts the third argument in a sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 4616 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4617 | hlua_lua2smp(L, 3, &smp); |
| 4618 | |
| 4619 | /* Store the sample in a variable. */ |
| 4620 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 4621 | |
| 4622 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 4623 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 4624 | else |
| 4625 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 4626 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 4627 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4628 | } |
| 4629 | |
| 4630 | __LJMP static int hlua_applet_tcp_unset_var(lua_State *L) |
| 4631 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4632 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4633 | struct stream *s; |
| 4634 | const char *name; |
| 4635 | size_t len; |
| 4636 | struct sample smp; |
| 4637 | |
| 4638 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 4639 | |
| 4640 | /* It is useles to retrieve the stream, but this function |
| 4641 | * runs only in a stream context. |
| 4642 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4643 | luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4644 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4645 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4646 | |
| 4647 | /* Unset the variable. */ |
| 4648 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 4649 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 4650 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4651 | } |
| 4652 | |
| 4653 | __LJMP static int hlua_applet_tcp_get_var(lua_State *L) |
| 4654 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4655 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4656 | struct stream *s; |
| 4657 | const char *name; |
| 4658 | size_t len; |
| 4659 | struct sample smp; |
| 4660 | |
| 4661 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 4662 | |
| 4663 | /* It is useles to retrieve the stream, but this function |
| 4664 | * runs only in a stream context. |
| 4665 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4666 | luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4667 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4668 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4669 | |
| 4670 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Willy Tarreau | e352b9d | 2021-09-03 11:52:38 +0200 | [diff] [blame] | 4671 | if (!vars_get_by_name(name, len, &smp, NULL)) { |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4672 | lua_pushnil(L); |
| 4673 | return 1; |
| 4674 | } |
| 4675 | |
| 4676 | return hlua_smp2lua(L, &smp); |
| 4677 | } |
| 4678 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4679 | __LJMP static int hlua_applet_tcp_set_priv(lua_State *L) |
| 4680 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4681 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 4682 | struct stream *s = luactx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4683 | struct hlua *hlua; |
| 4684 | |
| 4685 | /* 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] | 4686 | if (!s->hlua) |
| 4687 | return 0; |
| 4688 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4689 | |
| 4690 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 4691 | |
| 4692 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 4693 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4694 | |
| 4695 | /* Get and store new value. */ |
| 4696 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 4697 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 4698 | |
| 4699 | return 0; |
| 4700 | } |
| 4701 | |
| 4702 | __LJMP static int hlua_applet_tcp_get_priv(lua_State *L) |
| 4703 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4704 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 4705 | struct stream *s = luactx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4706 | struct hlua *hlua; |
| 4707 | |
| 4708 | /* 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] | 4709 | if (!s->hlua) { |
| 4710 | lua_pushnil(L); |
| 4711 | return 1; |
| 4712 | } |
| 4713 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4714 | |
| 4715 | /* Push configuration index in the stack. */ |
| 4716 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 4717 | |
| 4718 | return 1; |
| 4719 | } |
| 4720 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4721 | /* If expected data not yet available, it returns a yield. This function |
| 4722 | * consumes the data in the buffer. It returns a string containing the |
| 4723 | * data. This string can be empty. |
| 4724 | */ |
| 4725 | __LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx) |
| 4726 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4727 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 4728 | struct stconn *sc = appctx_sc(luactx->appctx); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4729 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4730 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4731 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4732 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4733 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4734 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4735 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 4736 | ret = co_getline_nc(sc_oc(sc), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4737 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4738 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4739 | if (ret == 0) { |
Willy Tarreau | 90e8b45 | 2022-05-25 18:21:43 +0200 | [diff] [blame] | 4740 | applet_need_more_data(luactx->appctx); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4741 | 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] | 4742 | } |
| 4743 | |
| 4744 | /* End of data: commit the total strings and return. */ |
| 4745 | if (ret < 0) { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4746 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4747 | return 1; |
| 4748 | } |
| 4749 | |
| 4750 | /* Ensure that the block 2 length is usable. */ |
| 4751 | if (ret == 1) |
| 4752 | len2 = 0; |
| 4753 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 4754 | /* don't check the max length read and don't check. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4755 | luaL_addlstring(&luactx->b, blk1, len1); |
| 4756 | luaL_addlstring(&luactx->b, blk2, len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4757 | |
| 4758 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 4759 | co_skip(sc_oc(sc), len1 + len2); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4760 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4761 | return 1; |
| 4762 | } |
| 4763 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4764 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4765 | __LJMP static int hlua_applet_tcp_getline(lua_State *L) |
| 4766 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4767 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4768 | |
| 4769 | /* Initialise the string catenation. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4770 | luaL_buffinit(L, &luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4771 | |
| 4772 | return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0)); |
| 4773 | } |
| 4774 | |
| 4775 | /* If expected data not yet available, it returns a yield. This function |
| 4776 | * consumes the data in the buffer. It returns a string containing the |
| 4777 | * data. This string can be empty. |
| 4778 | */ |
| 4779 | __LJMP static int hlua_applet_tcp_recv_yield(lua_State *L, int status, lua_KContext ctx) |
| 4780 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4781 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 4782 | struct stconn *sc = appctx_sc(luactx->appctx); |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4783 | size_t len = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4784 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4785 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4786 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4787 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4788 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4789 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4790 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 4791 | ret = co_getblk_nc(sc_oc(sc), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4792 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4793 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4794 | if (ret == 0) { |
Willy Tarreau | 90e8b45 | 2022-05-25 18:21:43 +0200 | [diff] [blame] | 4795 | applet_need_more_data(luactx->appctx); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4796 | 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] | 4797 | } |
| 4798 | |
| 4799 | /* End of data: commit the total strings and return. */ |
| 4800 | if (ret < 0) { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4801 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4802 | return 1; |
| 4803 | } |
| 4804 | |
| 4805 | /* Ensure that the block 2 length is usable. */ |
| 4806 | if (ret == 1) |
| 4807 | len2 = 0; |
| 4808 | |
| 4809 | if (len == -1) { |
| 4810 | |
| 4811 | /* If len == -1, catenate all the data avalaile and |
| 4812 | * yield because we want to get all the data until |
| 4813 | * the end of data stream. |
| 4814 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4815 | luaL_addlstring(&luactx->b, blk1, len1); |
| 4816 | luaL_addlstring(&luactx->b, blk2, len2); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 4817 | co_skip(sc_oc(sc), len1 + len2); |
Willy Tarreau | 90e8b45 | 2022-05-25 18:21:43 +0200 | [diff] [blame] | 4818 | applet_need_more_data(luactx->appctx); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4819 | 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] | 4820 | |
| 4821 | } else { |
| 4822 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4823 | /* Copy the first block caping to the length required. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4824 | if (len1 > len) |
| 4825 | len1 = len; |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4826 | luaL_addlstring(&luactx->b, blk1, len1); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4827 | len -= len1; |
| 4828 | |
| 4829 | /* Copy the second block. */ |
| 4830 | if (len2 > len) |
| 4831 | len2 = len; |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4832 | luaL_addlstring(&luactx->b, blk2, len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4833 | len -= len2; |
| 4834 | |
| 4835 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 4836 | co_skip(sc_oc(sc), len1 + len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4837 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4838 | /* If there is no other data available, yield waiting for new data. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4839 | if (len > 0) { |
| 4840 | lua_pushinteger(L, len); |
| 4841 | lua_replace(L, 2); |
Willy Tarreau | 90e8b45 | 2022-05-25 18:21:43 +0200 | [diff] [blame] | 4842 | applet_need_more_data(luactx->appctx); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4843 | 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] | 4844 | } |
| 4845 | |
| 4846 | /* return the result. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4847 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4848 | return 1; |
| 4849 | } |
| 4850 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4851 | /* we never execute this */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4852 | hlua_pusherror(L, "Lua: internal error"); |
| 4853 | WILL_LJMP(lua_error(L)); |
| 4854 | return 0; |
| 4855 | } |
| 4856 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4857 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4858 | __LJMP static int hlua_applet_tcp_recv(lua_State *L) |
| 4859 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4860 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4861 | int len = -1; |
| 4862 | |
| 4863 | if (lua_gettop(L) > 2) |
| 4864 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 4865 | if (lua_gettop(L) >= 2) { |
| 4866 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4867 | lua_pop(L, 1); |
| 4868 | } |
| 4869 | |
| 4870 | /* Confirm or set the required length */ |
| 4871 | lua_pushinteger(L, len); |
| 4872 | |
| 4873 | /* Initialise the string catenation. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4874 | luaL_buffinit(L, &luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4875 | |
| 4876 | return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0)); |
| 4877 | } |
| 4878 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4879 | /* Append data in the output side of the buffer. This data is immediately |
| 4880 | * sent. The function returns the amount of data written. If the buffer |
| 4881 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4882 | * if the channel is closed. |
| 4883 | */ |
| 4884 | __LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx) |
| 4885 | { |
| 4886 | size_t len; |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4887 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4888 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4889 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 4890 | struct stconn *sc = appctx_sc(luactx->appctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 4891 | struct channel *chn = sc_ic(sc); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4892 | int max; |
| 4893 | |
| 4894 | /* Get the max amount of data which can write as input in the channel. */ |
| 4895 | max = channel_recv_max(chn); |
| 4896 | if (max > (len - l)) |
| 4897 | max = len - l; |
| 4898 | |
| 4899 | /* Copy data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4900 | ci_putblk(chn, str + l, max); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4901 | |
| 4902 | /* update counters. */ |
| 4903 | l += max; |
| 4904 | lua_pop(L, 1); |
| 4905 | lua_pushinteger(L, l); |
| 4906 | |
| 4907 | /* If some data is not send, declares the situation to the |
| 4908 | * applet, and returns a yield. |
| 4909 | */ |
| 4910 | if (l < len) { |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 4911 | sc_need_room(sc); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4912 | 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] | 4913 | } |
| 4914 | |
| 4915 | return 1; |
| 4916 | } |
| 4917 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4918 | /* Just a wrapper of "hlua_applet_tcp_send_yield". This wrapper permits |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4919 | * yield the LUA process, and resume it without checking the |
| 4920 | * input arguments. |
| 4921 | */ |
| 4922 | __LJMP static int hlua_applet_tcp_send(lua_State *L) |
| 4923 | { |
| 4924 | MAY_LJMP(check_args(L, 2, "send")); |
| 4925 | lua_pushinteger(L, 0); |
| 4926 | |
| 4927 | return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0)); |
| 4928 | } |
| 4929 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4930 | /* |
| 4931 | * |
| 4932 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4933 | * Class AppletHTTP |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4934 | * |
| 4935 | * |
| 4936 | */ |
| 4937 | |
| 4938 | /* Returns a struct hlua_txn if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4939 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4940 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4941 | __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] | 4942 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4943 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_http_ref)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4944 | } |
| 4945 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4946 | /* This function creates and push in the stack an Applet object |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4947 | * according with a current TXN. |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 4948 | * It relies on the caller to have already reserved the room in ctx->svcctx |
| 4949 | * for the local storage of hlua_http_ctx. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4950 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4951 | static int hlua_applet_http_new(lua_State *L, struct appctx *ctx) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4952 | { |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 4953 | struct hlua_http_ctx *http_ctx = ctx->svcctx; |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4954 | struct hlua_appctx *luactx; |
Thierry FOURNIER | 841475e | 2015-12-11 17:10:09 +0100 | [diff] [blame] | 4955 | struct hlua_txn htxn; |
Willy Tarreau | 0698c80 | 2022-05-11 14:09:57 +0200 | [diff] [blame] | 4956 | struct stream *s = appctx_strm(ctx); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4957 | struct proxy *px = s->be; |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4958 | struct htx *htx; |
| 4959 | struct htx_blk *blk; |
| 4960 | struct htx_sl *sl; |
| 4961 | struct ist path; |
| 4962 | unsigned long long len = 0; |
| 4963 | int32_t pos; |
Amaury Denoyelle | c453f95 | 2021-07-06 11:40:12 +0200 | [diff] [blame] | 4964 | struct http_uri_parser parser; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4965 | |
| 4966 | /* Check stack size. */ |
| 4967 | if (!lua_checkstack(L, 3)) |
| 4968 | return 0; |
| 4969 | |
| 4970 | /* Create the object: obj[0] = userdata. |
| 4971 | * Note that the base of the Converters object is the |
| 4972 | * same than the TXN object. |
| 4973 | */ |
| 4974 | lua_newtable(L); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4975 | luactx = lua_newuserdata(L, sizeof(*luactx)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4976 | lua_rawseti(L, -2, 0); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4977 | luactx->appctx = ctx; |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 4978 | http_ctx->status = 200; /* Default status code returned. */ |
| 4979 | http_ctx->reason = NULL; /* Use default reason based on status */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4980 | luactx->htxn.s = s; |
| 4981 | luactx->htxn.p = px; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4982 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4983 | /* Create the "f" field that contains a list of fetches. */ |
| 4984 | lua_pushstring(L, "f"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4985 | if (!hlua_fetches_new(L, &luactx->htxn, 0)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4986 | return 0; |
| 4987 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4988 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4989 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 4990 | lua_pushstring(L, "sf"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4991 | if (!hlua_fetches_new(L, &luactx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4992 | return 0; |
| 4993 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4994 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4995 | /* Create the "c" field that contains a list of converters. */ |
| 4996 | lua_pushstring(L, "c"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4997 | if (!hlua_converters_new(L, &luactx->htxn, 0)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4998 | return 0; |
| 4999 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5000 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5001 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 5002 | lua_pushstring(L, "sc"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5003 | if (!hlua_converters_new(L, &luactx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5004 | return 0; |
| 5005 | lua_settable(L, -3); |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5006 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5007 | htx = htxbuf(&s->req.buf); |
| 5008 | blk = htx_get_first_blk(htx); |
Christopher Faulet | ea00973 | 2019-11-18 15:50:25 +0100 | [diff] [blame] | 5009 | BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5010 | sl = htx_get_blk_ptr(htx, blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5011 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5012 | /* Stores the request method. */ |
| 5013 | lua_pushstring(L, "method"); |
| 5014 | lua_pushlstring(L, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); |
| 5015 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5016 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5017 | /* Stores the http version. */ |
| 5018 | lua_pushstring(L, "version"); |
| 5019 | lua_pushlstring(L, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); |
| 5020 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5021 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5022 | /* creates an array of headers. hlua_http_get_headers() crates and push |
| 5023 | * the array on the top of the stack. |
| 5024 | */ |
| 5025 | lua_pushstring(L, "headers"); |
| 5026 | htxn.s = s; |
| 5027 | htxn.p = px; |
| 5028 | htxn.dir = SMP_OPT_DIR_REQ; |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 5029 | if (!hlua_http_get_headers(L, &htxn.s->txn->req)) |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5030 | return 0; |
| 5031 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5032 | |
Amaury Denoyelle | c453f95 | 2021-07-06 11:40:12 +0200 | [diff] [blame] | 5033 | parser = http_uri_parser_init(htx_sl_req_uri(sl)); |
| 5034 | path = http_parse_path(&parser); |
Tim Duesterhus | ed52637 | 2020-03-05 17:56:33 +0100 | [diff] [blame] | 5035 | if (isttest(path)) { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5036 | char *p, *q, *end; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5037 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5038 | p = path.ptr; |
Tim Duesterhus | 4c8f75f | 2021-11-06 15:14:44 +0100 | [diff] [blame] | 5039 | end = istend(path); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5040 | q = p; |
| 5041 | while (q < end && *q != '?') |
| 5042 | q++; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5043 | |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 5044 | /* Stores the request path. */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5045 | lua_pushstring(L, "path"); |
| 5046 | lua_pushlstring(L, p, q - p); |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 5047 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 5048 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5049 | /* Stores the query string. */ |
| 5050 | lua_pushstring(L, "qs"); |
| 5051 | if (*q == '?') |
| 5052 | q++; |
| 5053 | lua_pushlstring(L, q, end - q); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5054 | lua_settable(L, -3); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5055 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5056 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5057 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 5058 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 5059 | enum htx_blk_type type = htx_get_blk_type(blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5060 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 5061 | if (type == HTX_BLK_TLR || type == HTX_BLK_EOT) |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5062 | break; |
| 5063 | if (type == HTX_BLK_DATA) |
| 5064 | len += htx_get_blksz(blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5065 | } |
Christopher Faulet | 2e47e3a | 2023-01-13 11:40:24 +0100 | [diff] [blame] | 5066 | if (htx->extra != HTX_UNKOWN_PAYLOAD_LENGTH) |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5067 | len += htx->extra; |
| 5068 | |
| 5069 | /* Stores the request path. */ |
| 5070 | lua_pushstring(L, "length"); |
| 5071 | lua_pushinteger(L, len); |
| 5072 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 5073 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5074 | /* Create an empty array of HTTP request headers. */ |
| 5075 | lua_pushstring(L, "response"); |
| 5076 | lua_newtable(L); |
| 5077 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 5078 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5079 | /* Pop a class stream metatable and affect it to the table. */ |
| 5080 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref); |
| 5081 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5082 | |
| 5083 | return 1; |
| 5084 | } |
| 5085 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 5086 | __LJMP static int hlua_applet_http_set_var(lua_State *L) |
| 5087 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5088 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 5089 | struct stream *s; |
| 5090 | const char *name; |
| 5091 | size_t len; |
| 5092 | struct sample smp; |
| 5093 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 5094 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 5095 | 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] | 5096 | |
| 5097 | /* It is useles to retrieve the stream, but this function |
| 5098 | * runs only in a stream context. |
| 5099 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5100 | luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 5101 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5102 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 5103 | |
| 5104 | /* Converts the third argument in a sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 5105 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 5106 | hlua_lua2smp(L, 3, &smp); |
| 5107 | |
| 5108 | /* Store the sample in a variable. */ |
| 5109 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 5110 | |
| 5111 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 5112 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 5113 | else |
| 5114 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 5115 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 5116 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 5117 | } |
| 5118 | |
| 5119 | __LJMP static int hlua_applet_http_unset_var(lua_State *L) |
| 5120 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5121 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 5122 | struct stream *s; |
| 5123 | const char *name; |
| 5124 | size_t len; |
| 5125 | struct sample smp; |
| 5126 | |
| 5127 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 5128 | |
| 5129 | /* It is useles to retrieve the stream, but this function |
| 5130 | * runs only in a stream context. |
| 5131 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5132 | luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 5133 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5134 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 5135 | |
| 5136 | /* Unset the variable. */ |
| 5137 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 5138 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 5139 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 5140 | } |
| 5141 | |
| 5142 | __LJMP static int hlua_applet_http_get_var(lua_State *L) |
| 5143 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5144 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 5145 | struct stream *s; |
| 5146 | const char *name; |
| 5147 | size_t len; |
| 5148 | struct sample smp; |
| 5149 | |
| 5150 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 5151 | |
| 5152 | /* It is useles to retrieve the stream, but this function |
| 5153 | * runs only in a stream context. |
| 5154 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5155 | luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 5156 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5157 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 5158 | |
| 5159 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Willy Tarreau | e352b9d | 2021-09-03 11:52:38 +0200 | [diff] [blame] | 5160 | if (!vars_get_by_name(name, len, &smp, NULL)) { |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 5161 | lua_pushnil(L); |
| 5162 | return 1; |
| 5163 | } |
| 5164 | |
| 5165 | return hlua_smp2lua(L, &smp); |
| 5166 | } |
| 5167 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 5168 | __LJMP static int hlua_applet_http_set_priv(lua_State *L) |
| 5169 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5170 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 5171 | struct stream *s = luactx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 5172 | struct hlua *hlua; |
| 5173 | |
| 5174 | /* 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] | 5175 | if (!s->hlua) |
| 5176 | return 0; |
| 5177 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 5178 | |
| 5179 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 5180 | |
| 5181 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 5182 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 5183 | |
| 5184 | /* Get and store new value. */ |
| 5185 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 5186 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 5187 | |
| 5188 | return 0; |
| 5189 | } |
| 5190 | |
| 5191 | __LJMP static int hlua_applet_http_get_priv(lua_State *L) |
| 5192 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5193 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 5194 | struct stream *s = luactx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 5195 | struct hlua *hlua; |
| 5196 | |
| 5197 | /* 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] | 5198 | if (!s->hlua) { |
| 5199 | lua_pushnil(L); |
| 5200 | return 1; |
| 5201 | } |
| 5202 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 5203 | |
| 5204 | /* Push configuration index in the stack. */ |
| 5205 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 5206 | |
| 5207 | return 1; |
| 5208 | } |
| 5209 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5210 | /* If expected data not yet available, it returns a yield. This function |
| 5211 | * consumes the data in the buffer. It returns a string containing the |
| 5212 | * data. This string can be empty. |
| 5213 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5214 | __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] | 5215 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5216 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 5217 | struct stconn *sc = appctx_sc(luactx->appctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 5218 | struct channel *req = sc_oc(sc); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5219 | struct htx *htx; |
| 5220 | struct htx_blk *blk; |
| 5221 | size_t count; |
| 5222 | int stop = 0; |
| 5223 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5224 | htx = htx_from_buf(&req->buf); |
| 5225 | count = co_data(req); |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 5226 | blk = htx_get_first_blk(htx); |
Christopher Faulet | cc26b13 | 2018-12-18 21:20:57 +0100 | [diff] [blame] | 5227 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5228 | while (count && !stop && blk) { |
| 5229 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 5230 | uint32_t sz = htx_get_blksz(blk); |
| 5231 | struct ist v; |
| 5232 | uint32_t vlen; |
| 5233 | char *nl; |
| 5234 | |
| 5235 | vlen = sz; |
| 5236 | if (vlen > count) { |
| 5237 | if (type != HTX_BLK_DATA) |
| 5238 | break; |
| 5239 | vlen = count; |
| 5240 | } |
| 5241 | |
| 5242 | switch (type) { |
| 5243 | case HTX_BLK_UNUSED: |
| 5244 | break; |
| 5245 | |
| 5246 | case HTX_BLK_DATA: |
| 5247 | v = htx_get_blk_value(htx, blk); |
| 5248 | v.len = vlen; |
| 5249 | nl = istchr(v, '\n'); |
| 5250 | if (nl != NULL) { |
| 5251 | stop = 1; |
| 5252 | vlen = nl - v.ptr + 1; |
| 5253 | } |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5254 | luaL_addlstring(&luactx->b, v.ptr, vlen); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5255 | break; |
| 5256 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5257 | case HTX_BLK_TLR: |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 5258 | case HTX_BLK_EOT: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5259 | stop = 1; |
| 5260 | break; |
| 5261 | |
| 5262 | default: |
| 5263 | break; |
| 5264 | } |
| 5265 | |
Willy Tarreau | 8424004 | 2022-02-28 16:51:23 +0100 | [diff] [blame] | 5266 | c_rew(req, vlen); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5267 | count -= vlen; |
| 5268 | if (sz == vlen) |
| 5269 | blk = htx_remove_blk(htx, blk); |
| 5270 | else { |
| 5271 | htx_cut_data_blk(htx, blk, vlen); |
| 5272 | break; |
| 5273 | } |
| 5274 | } |
| 5275 | |
Christopher Faulet | eccb31c | 2021-04-02 14:24:56 +0200 | [diff] [blame] | 5276 | /* The message was fully consumed and no more data are expected |
| 5277 | * (EOM flag set). |
| 5278 | */ |
Christopher Faulet | 904763f | 2023-03-22 14:53:11 +0100 | [diff] [blame] | 5279 | if (htx_is_empty(htx) && (sc->flags & SC_FL_EOI)) |
Christopher Faulet | eccb31c | 2021-04-02 14:24:56 +0200 | [diff] [blame] | 5280 | stop = 1; |
| 5281 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 5282 | htx_to_buf(htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5283 | if (!stop) { |
Willy Tarreau | 90e8b45 | 2022-05-25 18:21:43 +0200 | [diff] [blame] | 5284 | applet_need_more_data(luactx->appctx); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5285 | 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] | 5286 | } |
| 5287 | |
| 5288 | /* return the result. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5289 | luaL_pushresult(&luactx->b); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5290 | return 1; |
| 5291 | } |
| 5292 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5293 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5294 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5295 | __LJMP static int hlua_applet_http_getline(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5296 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5297 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5298 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5299 | /* Initialise the string catenation. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5300 | luaL_buffinit(L, &luactx->b); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5301 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5302 | return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5303 | } |
| 5304 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5305 | /* If expected data not yet available, it returns a yield. This function |
| 5306 | * consumes the data in the buffer. It returns a string containing the |
| 5307 | * data. This string can be empty. |
| 5308 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5309 | __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] | 5310 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5311 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 5312 | struct stconn *sc = appctx_sc(luactx->appctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 5313 | struct channel *req = sc_oc(sc); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5314 | struct htx *htx; |
| 5315 | struct htx_blk *blk; |
| 5316 | size_t count; |
| 5317 | int len; |
| 5318 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5319 | htx = htx_from_buf(&req->buf); |
| 5320 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5321 | count = co_data(req); |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 5322 | blk = htx_get_head_blk(htx); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5323 | while (count && len && blk) { |
| 5324 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 5325 | uint32_t sz = htx_get_blksz(blk); |
| 5326 | struct ist v; |
| 5327 | uint32_t vlen; |
| 5328 | |
| 5329 | vlen = sz; |
| 5330 | if (len > 0 && vlen > len) |
| 5331 | vlen = len; |
| 5332 | if (vlen > count) { |
| 5333 | if (type != HTX_BLK_DATA) |
| 5334 | break; |
| 5335 | vlen = count; |
| 5336 | } |
| 5337 | |
| 5338 | switch (type) { |
| 5339 | case HTX_BLK_UNUSED: |
| 5340 | break; |
| 5341 | |
| 5342 | case HTX_BLK_DATA: |
| 5343 | v = htx_get_blk_value(htx, blk); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5344 | luaL_addlstring(&luactx->b, v.ptr, vlen); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5345 | break; |
| 5346 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5347 | case HTX_BLK_TLR: |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 5348 | case HTX_BLK_EOT: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5349 | len = 0; |
| 5350 | break; |
| 5351 | |
| 5352 | default: |
| 5353 | break; |
| 5354 | } |
| 5355 | |
Willy Tarreau | 8424004 | 2022-02-28 16:51:23 +0100 | [diff] [blame] | 5356 | c_rew(req, vlen); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5357 | count -= vlen; |
| 5358 | if (len > 0) |
| 5359 | len -= vlen; |
| 5360 | if (sz == vlen) |
| 5361 | blk = htx_remove_blk(htx, blk); |
| 5362 | else { |
| 5363 | htx_cut_data_blk(htx, blk, vlen); |
| 5364 | break; |
| 5365 | } |
| 5366 | } |
| 5367 | |
Christopher Faulet | eccb31c | 2021-04-02 14:24:56 +0200 | [diff] [blame] | 5368 | /* The message was fully consumed and no more data are expected |
| 5369 | * (EOM flag set). |
| 5370 | */ |
Christopher Faulet | 904763f | 2023-03-22 14:53:11 +0100 | [diff] [blame] | 5371 | if (htx_is_empty(htx) && (sc->flags & SC_FL_EOI)) |
Christopher Faulet | eccb31c | 2021-04-02 14:24:56 +0200 | [diff] [blame] | 5372 | len = 0; |
| 5373 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 5374 | htx_to_buf(htx, &req->buf); |
| 5375 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5376 | /* If we are no other data available, yield waiting for new data. */ |
| 5377 | if (len) { |
| 5378 | if (len > 0) { |
| 5379 | lua_pushinteger(L, len); |
| 5380 | lua_replace(L, 2); |
| 5381 | } |
Willy Tarreau | 90e8b45 | 2022-05-25 18:21:43 +0200 | [diff] [blame] | 5382 | applet_need_more_data(luactx->appctx); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 5383 | 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] | 5384 | } |
| 5385 | |
| 5386 | /* return the result. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5387 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5388 | return 1; |
| 5389 | } |
| 5390 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5391 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5392 | __LJMP static int hlua_applet_http_recv(lua_State *L) |
| 5393 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5394 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5395 | int len = -1; |
| 5396 | |
| 5397 | /* Check arguments. */ |
| 5398 | if (lua_gettop(L) > 2) |
| 5399 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 5400 | if (lua_gettop(L) >= 2) { |
| 5401 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5402 | lua_pop(L, 1); |
| 5403 | } |
| 5404 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5405 | lua_pushinteger(L, len); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5406 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5407 | /* Initialise the string catenation. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5408 | luaL_buffinit(L, &luactx->b); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5409 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5410 | return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5411 | } |
| 5412 | |
| 5413 | /* Append data in the output side of the buffer. This data is immediately |
| 5414 | * sent. The function returns the amount of data written. If the buffer |
| 5415 | * cannot contain the data, the function yields. The function returns -1 |
| 5416 | * if the channel is closed. |
| 5417 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5418 | __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] | 5419 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5420 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 5421 | struct stconn *sc = appctx_sc(luactx->appctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 5422 | struct channel *res = sc_ic(sc); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5423 | struct htx *htx = htx_from_buf(&res->buf); |
| 5424 | const char *data; |
| 5425 | size_t len; |
| 5426 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 5427 | int max; |
| 5428 | |
Christopher Faulet | 9060fc0 | 2019-07-03 11:39:30 +0200 | [diff] [blame] | 5429 | max = htx_get_max_blksz(htx, channel_htx_recv_max(res, htx)); |
Christopher Faulet | 4b0e9b2 | 2019-01-09 12:16:58 +0100 | [diff] [blame] | 5430 | if (!max) |
| 5431 | goto snd_yield; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5432 | |
| 5433 | data = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5434 | |
| 5435 | /* Get the max amount of data which can write as input in the channel. */ |
| 5436 | if (max > (len - l)) |
| 5437 | max = len - l; |
| 5438 | |
| 5439 | /* Copy data. */ |
Willy Tarreau | 0a7ef02 | 2019-05-28 10:30:11 +0200 | [diff] [blame] | 5440 | max = htx_add_data(htx, ist2(data + l, max)); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 5441 | channel_add_input(res, max); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5442 | |
| 5443 | /* update counters. */ |
| 5444 | l += max; |
| 5445 | lua_pop(L, 1); |
| 5446 | lua_pushinteger(L, l); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5447 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5448 | /* If some data is not send, declares the situation to the |
| 5449 | * applet, and returns a yield. |
| 5450 | */ |
| 5451 | if (l < len) { |
Christopher Faulet | 4b0e9b2 | 2019-01-09 12:16:58 +0100 | [diff] [blame] | 5452 | snd_yield: |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 5453 | htx_to_buf(htx, &res->buf); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 5454 | sc_need_room(sc); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 5455 | 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] | 5456 | } |
| 5457 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5458 | htx_to_buf(htx, &res->buf); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5459 | return 1; |
| 5460 | } |
| 5461 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 5462 | /* Just a wrapper of "hlua_applet_send_yield". This wrapper permits |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5463 | * yield the LUA process, and resume it without checking the |
| 5464 | * input arguments. |
| 5465 | */ |
| 5466 | __LJMP static int hlua_applet_http_send(lua_State *L) |
| 5467 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5468 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 5469 | struct hlua_http_ctx *http_ctx = luactx->appctx->svcctx; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5470 | |
| 5471 | /* We want to send some data. Headers must be sent. */ |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 5472 | if (!(http_ctx->flags & APPLET_HDR_SENT)) { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5473 | hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data."); |
| 5474 | WILL_LJMP(lua_error(L)); |
| 5475 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5476 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 5477 | /* This integer is used for followinf the amount of data sent. */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5478 | lua_pushinteger(L, 0); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5479 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5480 | return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5481 | } |
| 5482 | |
| 5483 | __LJMP static int hlua_applet_http_addheader(lua_State *L) |
| 5484 | { |
| 5485 | const char *name; |
| 5486 | int ret; |
| 5487 | |
| 5488 | MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 5489 | name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5490 | MAY_LJMP(luaL_checkstring(L, 3)); |
| 5491 | |
| 5492 | /* Push in the stack the "response" entry. */ |
| 5493 | ret = lua_getfield(L, 1, "response"); |
| 5494 | if (ret != LUA_TTABLE) { |
| 5495 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] " |
| 5496 | "is expected as an array. %s found", lua_typename(L, ret)); |
| 5497 | WILL_LJMP(lua_error(L)); |
| 5498 | } |
| 5499 | |
| 5500 | /* check if the header is already registered if it is not |
| 5501 | * the case, register it. |
| 5502 | */ |
| 5503 | ret = lua_getfield(L, -1, name); |
| 5504 | if (ret == LUA_TNIL) { |
| 5505 | |
| 5506 | /* Entry not found. */ |
| 5507 | lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */ |
| 5508 | |
| 5509 | /* Insert the new header name in the array in the top of the stack. |
| 5510 | * It left the new array in the top of the stack. |
| 5511 | */ |
| 5512 | lua_newtable(L); |
| 5513 | lua_pushvalue(L, 2); |
| 5514 | lua_pushvalue(L, -2); |
| 5515 | lua_settable(L, -4); |
| 5516 | |
| 5517 | } else if (ret != LUA_TTABLE) { |
| 5518 | |
| 5519 | /* corruption error. */ |
| 5520 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] " |
| 5521 | "is expected as an array. %s found", name, lua_typename(L, ret)); |
| 5522 | WILL_LJMP(lua_error(L)); |
| 5523 | } |
| 5524 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 5525 | /* Now the top of thestack is an array of values. We push |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5526 | * the header value as new entry. |
| 5527 | */ |
| 5528 | lua_pushvalue(L, 3); |
| 5529 | ret = lua_rawlen(L, -2); |
| 5530 | lua_rawseti(L, -2, ret + 1); |
| 5531 | lua_pushboolean(L, 1); |
| 5532 | return 1; |
| 5533 | } |
| 5534 | |
| 5535 | __LJMP static int hlua_applet_http_status(lua_State *L) |
| 5536 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5537 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5538 | int status = MAY_LJMP(luaL_checkinteger(L, 2)); |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5539 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 5540 | struct hlua_http_ctx *http_ctx = luactx->appctx->svcctx; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5541 | |
| 5542 | if (status < 100 || status > 599) { |
| 5543 | lua_pushboolean(L, 0); |
| 5544 | return 1; |
| 5545 | } |
| 5546 | |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 5547 | http_ctx->status = status; |
| 5548 | http_ctx->reason = reason; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5549 | lua_pushboolean(L, 1); |
| 5550 | return 1; |
| 5551 | } |
| 5552 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5553 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5554 | __LJMP static int hlua_applet_http_send_response(lua_State *L) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5555 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5556 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 5557 | struct hlua_http_ctx *http_ctx = luactx->appctx->svcctx; |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 5558 | struct stconn *sc = appctx_sc(luactx->appctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 5559 | struct channel *res = sc_ic(sc); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5560 | struct htx *htx; |
| 5561 | struct htx_sl *sl; |
| 5562 | struct h1m h1m; |
| 5563 | const char *status, *reason; |
| 5564 | const char *name, *value; |
| 5565 | size_t nlen, vlen; |
| 5566 | unsigned int flags; |
| 5567 | |
| 5568 | /* Send the message at once. */ |
| 5569 | htx = htx_from_buf(&res->buf); |
| 5570 | h1m_init_res(&h1m); |
| 5571 | |
| 5572 | /* Use the same http version than the request. */ |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 5573 | status = ultoa_r(http_ctx->status, trash.area, trash.size); |
| 5574 | reason = http_ctx->reason; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5575 | if (reason == NULL) |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 5576 | reason = http_get_reason(http_ctx->status); |
| 5577 | if (http_ctx->flags & APPLET_HTTP11) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5578 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11); |
| 5579 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist(status), ist(reason)); |
| 5580 | } |
| 5581 | else { |
| 5582 | flags = HTX_SL_F_IS_RESP; |
| 5583 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), ist(status), ist(reason)); |
| 5584 | } |
| 5585 | if (!sl) { |
| 5586 | hlua_pusherror(L, "Lua applet http '%s': Failed to create response.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5587 | luactx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5588 | WILL_LJMP(lua_error(L)); |
| 5589 | } |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 5590 | sl->info.res.status = http_ctx->status; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5591 | |
| 5592 | /* Get the array associated to the field "response" in the object AppletHTTP. */ |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5593 | if (lua_getfield(L, 1, "response") != LUA_TTABLE) { |
| 5594 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5595 | luactx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5596 | WILL_LJMP(lua_error(L)); |
| 5597 | } |
| 5598 | |
| 5599 | /* Browse the list of headers. */ |
| 5600 | lua_pushnil(L); |
| 5601 | while(lua_next(L, -2) != 0) { |
| 5602 | /* We expect a string as -2. */ |
| 5603 | if (lua_type(L, -2) != LUA_TSTRING) { |
| 5604 | 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] | 5605 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5606 | lua_typename(L, lua_type(L, -2))); |
| 5607 | WILL_LJMP(lua_error(L)); |
| 5608 | } |
| 5609 | name = lua_tolstring(L, -2, &nlen); |
| 5610 | |
| 5611 | /* We expect an array as -1. */ |
| 5612 | if (lua_type(L, -1) != LUA_TTABLE) { |
| 5613 | 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] | 5614 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5615 | name, |
| 5616 | lua_typename(L, lua_type(L, -1))); |
| 5617 | WILL_LJMP(lua_error(L)); |
| 5618 | } |
| 5619 | |
| 5620 | /* Browse the table who is on the top of the stack. */ |
| 5621 | lua_pushnil(L); |
| 5622 | while(lua_next(L, -2) != 0) { |
| 5623 | int id; |
| 5624 | |
| 5625 | /* We expect a number as -2. */ |
| 5626 | if (lua_type(L, -2) != LUA_TNUMBER) { |
| 5627 | 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] | 5628 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5629 | name, |
| 5630 | lua_typename(L, lua_type(L, -2))); |
| 5631 | WILL_LJMP(lua_error(L)); |
| 5632 | } |
| 5633 | id = lua_tointeger(L, -2); |
| 5634 | |
| 5635 | /* We expect a string as -2. */ |
| 5636 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 5637 | 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] | 5638 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5639 | name, id, |
| 5640 | lua_typename(L, lua_type(L, -1))); |
| 5641 | WILL_LJMP(lua_error(L)); |
| 5642 | } |
| 5643 | value = lua_tolstring(L, -1, &vlen); |
| 5644 | |
| 5645 | /* Simple Protocol checks. */ |
Christopher Faulet | 545fbba | 2021-09-28 09:36:25 +0200 | [diff] [blame] | 5646 | if (isteqi(ist2(name, nlen), ist("transfer-encoding"))) { |
| 5647 | int ret; |
| 5648 | |
| 5649 | ret = h1_parse_xfer_enc_header(&h1m, ist2(value, vlen)); |
| 5650 | if (ret < 0) { |
| 5651 | hlua_pusherror(L, "Lua applet http '%s': Invalid '%s' header.\n", |
| 5652 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
| 5653 | name); |
| 5654 | WILL_LJMP(lua_error(L)); |
| 5655 | } |
| 5656 | else if (ret == 0) |
| 5657 | goto next; /* Skip it */ |
| 5658 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5659 | else if (isteqi(ist2(name, nlen), ist("content-length"))) { |
| 5660 | struct ist v = ist2(value, vlen); |
| 5661 | int ret; |
| 5662 | |
| 5663 | ret = h1_parse_cont_len_header(&h1m, &v); |
| 5664 | if (ret < 0) { |
| 5665 | hlua_pusherror(L, "Lua applet http '%s': Invalid '%s' header.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5666 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5667 | name); |
| 5668 | WILL_LJMP(lua_error(L)); |
| 5669 | } |
| 5670 | else if (ret == 0) |
| 5671 | goto next; /* Skip it */ |
| 5672 | } |
| 5673 | |
| 5674 | /* Add a new header */ |
| 5675 | if (!htx_add_header(htx, ist2(name, nlen), ist2(value, vlen))) { |
| 5676 | 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] | 5677 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5678 | name); |
| 5679 | WILL_LJMP(lua_error(L)); |
| 5680 | } |
| 5681 | next: |
| 5682 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 5683 | lua_pop(L, 1); |
| 5684 | } |
| 5685 | |
| 5686 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 5687 | lua_pop(L, 1); |
| 5688 | } |
| 5689 | |
| 5690 | if (h1m.flags & H1_MF_CHNK) |
| 5691 | h1m.flags &= ~H1_MF_CLEN; |
| 5692 | if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK)) |
| 5693 | h1m.flags |= H1_MF_XFER_LEN; |
| 5694 | |
| 5695 | /* Uset HTX start-line flags */ |
| 5696 | if (h1m.flags & H1_MF_XFER_ENC) |
| 5697 | flags |= HTX_SL_F_XFER_ENC; |
| 5698 | if (h1m.flags & H1_MF_XFER_LEN) { |
| 5699 | flags |= HTX_SL_F_XFER_LEN; |
| 5700 | if (h1m.flags & H1_MF_CHNK) |
| 5701 | flags |= HTX_SL_F_CHNK; |
| 5702 | else if (h1m.flags & H1_MF_CLEN) |
| 5703 | flags |= HTX_SL_F_CLEN; |
| 5704 | if (h1m.body_len == 0) |
| 5705 | flags |= HTX_SL_F_BODYLESS; |
| 5706 | } |
| 5707 | sl->flags |= flags; |
| 5708 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 5709 | /* 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] | 5710 | * and the status code implies the presence of a message body, we must |
| 5711 | * announce a transfer encoding chunked. This is required by haproxy |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 5712 | * for the keepalive compliance. If the applet announces a transfer-encoding |
| 5713 | * chunked itself, don't do anything. |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5714 | */ |
| 5715 | 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] | 5716 | http_ctx->status >= 200 && http_ctx->status != 204 && http_ctx->status != 304) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5717 | /* Add a new header */ |
| 5718 | sl->flags |= (HTX_SL_F_XFER_ENC|H1_MF_CHNK|H1_MF_XFER_LEN); |
| 5719 | if (!htx_add_header(htx, ist("transfer-encoding"), ist("chunked"))) { |
| 5720 | 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] | 5721 | luactx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5722 | WILL_LJMP(lua_error(L)); |
| 5723 | } |
| 5724 | } |
| 5725 | |
| 5726 | /* Finalize headers. */ |
| 5727 | if (!htx_add_endof(htx, HTX_BLK_EOH)) { |
| 5728 | hlua_pusherror(L, "Lua applet http '%s': Failed create the response.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5729 | luactx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5730 | WILL_LJMP(lua_error(L)); |
| 5731 | } |
| 5732 | |
| 5733 | if (htx_used_space(htx) > b_size(&res->buf) - global.tune.maxrewrite) { |
| 5734 | b_reset(&res->buf); |
| 5735 | hlua_pusherror(L, "Lua: 'start_response': response header block too big"); |
| 5736 | WILL_LJMP(lua_error(L)); |
| 5737 | } |
| 5738 | |
| 5739 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 5740 | channel_add_input(res, htx->data); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5741 | |
| 5742 | /* Headers sent, set the flag. */ |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 5743 | http_ctx->flags |= APPLET_HDR_SENT; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5744 | return 0; |
| 5745 | |
| 5746 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5747 | /* We will build the status line and the headers of the HTTP response. |
| 5748 | * We will try send at once if its not possible, we give back the hand |
| 5749 | * waiting for more room. |
| 5750 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5751 | __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] | 5752 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5753 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 5754 | struct stconn *sc = appctx_sc(luactx->appctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 5755 | struct channel *res = sc_ic(sc); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5756 | |
| 5757 | if (co_data(res)) { |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 5758 | sc_need_room(sc); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5759 | 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] | 5760 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5761 | return MAY_LJMP(hlua_applet_http_send_response(L)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5762 | } |
| 5763 | |
| 5764 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5765 | __LJMP static int hlua_applet_http_start_response(lua_State *L) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5766 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5767 | return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5768 | } |
| 5769 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5770 | /* |
| 5771 | * |
| 5772 | * |
| 5773 | * Class HTTP |
| 5774 | * |
| 5775 | * |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5776 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5777 | |
| 5778 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 5779 | * a class stream, otherwise it throws an error. |
| 5780 | */ |
| 5781 | __LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5782 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5783 | return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref)); |
| 5784 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5785 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5786 | /* This function creates and push in the stack a HTTP object |
| 5787 | * according with a current TXN. |
| 5788 | */ |
| 5789 | static int hlua_http_new(lua_State *L, struct hlua_txn *txn) |
| 5790 | { |
| 5791 | struct hlua_txn *htxn; |
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 | /* Check stack size. */ |
| 5794 | if (!lua_checkstack(L, 3)) |
| 5795 | return 0; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5796 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5797 | /* Create the object: obj[0] = userdata. |
| 5798 | * Note that the base of the Converters object is the |
| 5799 | * same than the TXN object. |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5800 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5801 | lua_newtable(L); |
| 5802 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
| 5803 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5804 | |
| 5805 | htxn->s = txn->s; |
| 5806 | htxn->p = txn->p; |
Christopher Faulet | 256b69a | 2019-05-23 11:14:21 +0200 | [diff] [blame] | 5807 | htxn->dir = txn->dir; |
| 5808 | htxn->flags = txn->flags; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5809 | |
| 5810 | /* Pop a class stream metatable and affect it to the table. */ |
| 5811 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref); |
| 5812 | lua_setmetatable(L, -2); |
| 5813 | |
| 5814 | return 1; |
| 5815 | } |
| 5816 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 5817 | /* This function creates and returns an array containing the status-line |
| 5818 | * elements. This function does not fails. |
| 5819 | */ |
| 5820 | __LJMP static int hlua_http_get_stline(lua_State *L, struct htx_sl *sl) |
| 5821 | { |
| 5822 | /* Create the table. */ |
| 5823 | lua_newtable(L); |
| 5824 | |
| 5825 | if (sl->flags & HTX_SL_F_IS_RESP) { |
| 5826 | lua_pushstring(L, "version"); |
| 5827 | lua_pushlstring(L, HTX_SL_RES_VPTR(sl), HTX_SL_RES_VLEN(sl)); |
| 5828 | lua_settable(L, -3); |
| 5829 | lua_pushstring(L, "code"); |
| 5830 | lua_pushlstring(L, HTX_SL_RES_CPTR(sl), HTX_SL_RES_CLEN(sl)); |
| 5831 | lua_settable(L, -3); |
| 5832 | lua_pushstring(L, "reason"); |
| 5833 | lua_pushlstring(L, HTX_SL_RES_RPTR(sl), HTX_SL_RES_RLEN(sl)); |
| 5834 | lua_settable(L, -3); |
| 5835 | } |
| 5836 | else { |
| 5837 | lua_pushstring(L, "method"); |
| 5838 | lua_pushlstring(L, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); |
| 5839 | lua_settable(L, -3); |
| 5840 | lua_pushstring(L, "uri"); |
| 5841 | lua_pushlstring(L, HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl)); |
| 5842 | lua_settable(L, -3); |
| 5843 | lua_pushstring(L, "version"); |
| 5844 | lua_pushlstring(L, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); |
| 5845 | lua_settable(L, -3); |
| 5846 | } |
| 5847 | return 1; |
| 5848 | } |
| 5849 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5850 | /* This function creates ans returns an array of HTTP headers. |
| 5851 | * This function does not fails. It is used as wrapper with the |
| 5852 | * 2 following functions. |
| 5853 | */ |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 5854 | __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] | 5855 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5856 | struct htx *htx; |
| 5857 | int32_t pos; |
| 5858 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5859 | /* Create the table. */ |
| 5860 | lua_newtable(L); |
| 5861 | |
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 | htx = htxbuf(&msg->chn->buf); |
| 5864 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 5865 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 5866 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 5867 | struct ist n, v; |
| 5868 | int len; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5869 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5870 | if (type == HTX_BLK_HDR) { |
| 5871 | n = htx_get_blk_name(htx,blk); |
| 5872 | v = htx_get_blk_value(htx, blk); |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5873 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5874 | else if (type == HTX_BLK_EOH) |
| 5875 | break; |
| 5876 | else |
| 5877 | continue; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5878 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5879 | /* Check for existing entry: |
| 5880 | * assume that the table is on the top of the stack, and |
| 5881 | * push the key in the stack, the function lua_gettable() |
| 5882 | * perform the lookup. |
| 5883 | */ |
| 5884 | lua_pushlstring(L, n.ptr, n.len); |
| 5885 | lua_gettable(L, -2); |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5886 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5887 | switch (lua_type(L, -1)) { |
| 5888 | case LUA_TNIL: |
| 5889 | /* Table not found, create it. */ |
| 5890 | lua_pop(L, 1); /* remove the nil value. */ |
| 5891 | lua_pushlstring(L, n.ptr, n.len); /* push the header name as key. */ |
| 5892 | lua_newtable(L); /* create and push empty table. */ |
| 5893 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 5894 | lua_rawseti(L, -2, 0); /* index header value (pop it). */ |
| 5895 | 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] | 5896 | break; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5897 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5898 | case LUA_TTABLE: |
| 5899 | /* Entry found: push the value in the table. */ |
| 5900 | len = lua_rawlen(L, -1); |
| 5901 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 5902 | lua_rawseti(L, -2, len+1); /* index header value (pop it). */ |
| 5903 | lua_pop(L, 1); /* remove the table (it is stored in the main table). */ |
| 5904 | break; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5905 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5906 | default: |
| 5907 | /* Other cases are errors. */ |
| 5908 | hlua_pusherror(L, "internal error during the parsing of headers."); |
| 5909 | WILL_LJMP(lua_error(L)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5910 | } |
| 5911 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5912 | return 1; |
| 5913 | } |
| 5914 | |
| 5915 | __LJMP static int hlua_http_req_get_headers(lua_State *L) |
| 5916 | { |
| 5917 | struct hlua_txn *htxn; |
| 5918 | |
| 5919 | MAY_LJMP(check_args(L, 1, "req_get_headers")); |
| 5920 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5921 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5922 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5923 | WILL_LJMP(lua_error(L)); |
| 5924 | |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 5925 | return hlua_http_get_headers(L, &htxn->s->txn->req); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5926 | } |
| 5927 | |
| 5928 | __LJMP static int hlua_http_res_get_headers(lua_State *L) |
| 5929 | { |
| 5930 | struct hlua_txn *htxn; |
| 5931 | |
| 5932 | MAY_LJMP(check_args(L, 1, "res_get_headers")); |
| 5933 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5934 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5935 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5936 | WILL_LJMP(lua_error(L)); |
| 5937 | |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 5938 | return hlua_http_get_headers(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5939 | } |
| 5940 | |
| 5941 | /* This function replace full header, or just a value in |
| 5942 | * the request or in the response. It is a wrapper fir the |
| 5943 | * 4 following functions. |
| 5944 | */ |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5945 | __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] | 5946 | { |
| 5947 | size_t name_len; |
| 5948 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5949 | const char *reg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 5950 | const char *value = MAY_LJMP(luaL_checkstring(L, 4)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5951 | struct htx *htx; |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 5952 | struct my_regex *re; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5953 | |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 5954 | if (!(re = regex_comp(reg, 1, 1, NULL))) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5955 | WILL_LJMP(luaL_argerror(L, 3, "invalid regex")); |
| 5956 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5957 | htx = htxbuf(&msg->chn->buf); |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5958 | 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] | 5959 | regex_free(re); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5960 | return 0; |
| 5961 | } |
| 5962 | |
| 5963 | __LJMP static int hlua_http_req_rep_hdr(lua_State *L) |
| 5964 | { |
| 5965 | struct hlua_txn *htxn; |
| 5966 | |
| 5967 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 5968 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5969 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5970 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5971 | WILL_LJMP(lua_error(L)); |
| 5972 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5973 | 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] | 5974 | } |
| 5975 | |
| 5976 | __LJMP static int hlua_http_res_rep_hdr(lua_State *L) |
| 5977 | { |
| 5978 | struct hlua_txn *htxn; |
| 5979 | |
| 5980 | MAY_LJMP(check_args(L, 4, "res_rep_hdr")); |
| 5981 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5982 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5983 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5984 | WILL_LJMP(lua_error(L)); |
| 5985 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5986 | 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] | 5987 | } |
| 5988 | |
| 5989 | __LJMP static int hlua_http_req_rep_val(lua_State *L) |
| 5990 | { |
| 5991 | struct hlua_txn *htxn; |
| 5992 | |
| 5993 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 5994 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5995 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5996 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5997 | WILL_LJMP(lua_error(L)); |
| 5998 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5999 | 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] | 6000 | } |
| 6001 | |
| 6002 | __LJMP static int hlua_http_res_rep_val(lua_State *L) |
| 6003 | { |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6004 | struct hlua_txn *htxn; |
| 6005 | |
| 6006 | MAY_LJMP(check_args(L, 4, "res_rep_val")); |
| 6007 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 6008 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 6009 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6010 | WILL_LJMP(lua_error(L)); |
| 6011 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 6012 | 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] | 6013 | } |
| 6014 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6015 | /* This function deletes all the occurrences of an header. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6016 | * It is a wrapper for the 2 following functions. |
| 6017 | */ |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 6018 | __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] | 6019 | { |
| 6020 | size_t len; |
| 6021 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 6022 | struct htx *htx = htxbuf(&msg->chn->buf); |
| 6023 | struct http_hdr_ctx ctx; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6024 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 6025 | ctx.blk = NULL; |
| 6026 | while (http_find_header(htx, ist2(name, len), &ctx, 1)) |
| 6027 | http_remove_header(htx, &ctx); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6028 | return 0; |
| 6029 | } |
| 6030 | |
| 6031 | __LJMP static int hlua_http_req_del_hdr(lua_State *L) |
| 6032 | { |
| 6033 | struct hlua_txn *htxn; |
| 6034 | |
| 6035 | MAY_LJMP(check_args(L, 2, "req_del_hdr")); |
| 6036 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 6037 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 6038 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6039 | WILL_LJMP(lua_error(L)); |
| 6040 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 6041 | return hlua_http_del_hdr(L, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6042 | } |
| 6043 | |
| 6044 | __LJMP static int hlua_http_res_del_hdr(lua_State *L) |
| 6045 | { |
| 6046 | struct hlua_txn *htxn; |
| 6047 | |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6048 | MAY_LJMP(check_args(L, 2, "res_del_hdr")); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6049 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 6050 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 6051 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6052 | WILL_LJMP(lua_error(L)); |
| 6053 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 6054 | return hlua_http_del_hdr(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6055 | } |
| 6056 | |
| 6057 | /* This function adds an header. It is a wrapper used by |
| 6058 | * the 2 following functions. |
| 6059 | */ |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 6060 | __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] | 6061 | { |
| 6062 | size_t name_len; |
| 6063 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 6064 | size_t value_len; |
| 6065 | const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 6066 | struct htx *htx = htxbuf(&msg->chn->buf); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6067 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 6068 | lua_pushboolean(L, http_add_header(htx, ist2(name, name_len), |
| 6069 | ist2(value, value_len))); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6070 | return 0; |
| 6071 | } |
| 6072 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6073 | __LJMP static int hlua_http_req_add_hdr(lua_State *L) |
| 6074 | { |
| 6075 | struct hlua_txn *htxn; |
| 6076 | |
| 6077 | MAY_LJMP(check_args(L, 3, "req_add_hdr")); |
| 6078 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 6079 | |
| 6080 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
| 6081 | WILL_LJMP(lua_error(L)); |
| 6082 | |
| 6083 | return hlua_http_add_hdr(L, &htxn->s->txn->req); |
| 6084 | } |
| 6085 | |
| 6086 | __LJMP static int hlua_http_res_add_hdr(lua_State *L) |
| 6087 | { |
| 6088 | struct hlua_txn *htxn; |
| 6089 | |
| 6090 | MAY_LJMP(check_args(L, 3, "res_add_hdr")); |
| 6091 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 6092 | |
| 6093 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
| 6094 | WILL_LJMP(lua_error(L)); |
| 6095 | |
| 6096 | return hlua_http_add_hdr(L, &htxn->s->txn->rsp); |
| 6097 | } |
| 6098 | |
| 6099 | static int hlua_http_req_set_hdr(lua_State *L) |
| 6100 | { |
| 6101 | struct hlua_txn *htxn; |
| 6102 | |
| 6103 | MAY_LJMP(check_args(L, 3, "req_set_hdr")); |
| 6104 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 6105 | |
| 6106 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
| 6107 | WILL_LJMP(lua_error(L)); |
| 6108 | |
| 6109 | hlua_http_del_hdr(L, &htxn->s->txn->req); |
| 6110 | return hlua_http_add_hdr(L, &htxn->s->txn->req); |
| 6111 | } |
| 6112 | |
| 6113 | static int hlua_http_res_set_hdr(lua_State *L) |
| 6114 | { |
| 6115 | struct hlua_txn *htxn; |
| 6116 | |
| 6117 | MAY_LJMP(check_args(L, 3, "res_set_hdr")); |
| 6118 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 6119 | |
| 6120 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
| 6121 | WILL_LJMP(lua_error(L)); |
| 6122 | |
| 6123 | hlua_http_del_hdr(L, &htxn->s->txn->rsp); |
| 6124 | return hlua_http_add_hdr(L, &htxn->s->txn->rsp); |
| 6125 | } |
| 6126 | |
| 6127 | /* This function set the method. */ |
| 6128 | static int hlua_http_req_set_meth(lua_State *L) |
| 6129 | { |
| 6130 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 6131 | size_t name_len; |
| 6132 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 6133 | |
| 6134 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
| 6135 | WILL_LJMP(lua_error(L)); |
| 6136 | |
| 6137 | lua_pushboolean(L, http_req_replace_stline(0, name, name_len, htxn->p, htxn->s) != -1); |
| 6138 | return 1; |
| 6139 | } |
| 6140 | |
| 6141 | /* This function set the method. */ |
| 6142 | static int hlua_http_req_set_path(lua_State *L) |
| 6143 | { |
| 6144 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 6145 | size_t name_len; |
| 6146 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 6147 | |
| 6148 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
| 6149 | WILL_LJMP(lua_error(L)); |
| 6150 | |
| 6151 | lua_pushboolean(L, http_req_replace_stline(1, name, name_len, htxn->p, htxn->s) != -1); |
| 6152 | return 1; |
| 6153 | } |
| 6154 | |
| 6155 | /* This function set the query-string. */ |
| 6156 | static int hlua_http_req_set_query(lua_State *L) |
| 6157 | { |
| 6158 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 6159 | size_t name_len; |
| 6160 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 6161 | |
| 6162 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
| 6163 | WILL_LJMP(lua_error(L)); |
| 6164 | |
| 6165 | /* Check length. */ |
| 6166 | if (name_len > trash.size - 1) { |
| 6167 | lua_pushboolean(L, 0); |
| 6168 | return 1; |
| 6169 | } |
| 6170 | |
| 6171 | /* Add the mark question as prefix. */ |
| 6172 | chunk_reset(&trash); |
| 6173 | trash.area[trash.data++] = '?'; |
| 6174 | memcpy(trash.area + trash.data, name, name_len); |
| 6175 | trash.data += name_len; |
| 6176 | |
| 6177 | lua_pushboolean(L, |
| 6178 | http_req_replace_stline(2, trash.area, trash.data, htxn->p, htxn->s) != -1); |
| 6179 | return 1; |
| 6180 | } |
| 6181 | |
| 6182 | /* This function set the uri. */ |
| 6183 | static int hlua_http_req_set_uri(lua_State *L) |
| 6184 | { |
| 6185 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 6186 | size_t name_len; |
| 6187 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 6188 | |
| 6189 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
| 6190 | WILL_LJMP(lua_error(L)); |
| 6191 | |
| 6192 | lua_pushboolean(L, http_req_replace_stline(3, name, name_len, htxn->p, htxn->s) != -1); |
| 6193 | return 1; |
| 6194 | } |
| 6195 | |
| 6196 | /* This function set the response code & optionally reason. */ |
| 6197 | static int hlua_http_res_set_status(lua_State *L) |
| 6198 | { |
| 6199 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 6200 | unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 6201 | const char *str = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
| 6202 | const struct ist reason = ist2(str, (str ? strlen(str) : 0)); |
| 6203 | |
| 6204 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
| 6205 | WILL_LJMP(lua_error(L)); |
| 6206 | |
| 6207 | http_res_set_status(code, reason, htxn->s); |
| 6208 | return 0; |
| 6209 | } |
| 6210 | |
| 6211 | /* |
| 6212 | * |
| 6213 | * |
| 6214 | * Class HTTPMessage |
| 6215 | * |
| 6216 | * |
| 6217 | */ |
| 6218 | |
| 6219 | /* Returns a struct http_msg if the stack entry "ud" is a class HTTPMessage, |
| 6220 | * otherwise it throws an error. |
| 6221 | */ |
| 6222 | __LJMP static struct http_msg *hlua_checkhttpmsg(lua_State *L, int ud) |
| 6223 | { |
| 6224 | return MAY_LJMP(hlua_checkudata(L, ud, class_http_msg_ref)); |
| 6225 | } |
| 6226 | |
| 6227 | /* Creates and pushes on the stack a HTTP object according with a current TXN. |
| 6228 | */ |
Christopher Faulet | 78c3547 | 2020-02-26 17:14:08 +0100 | [diff] [blame] | 6229 | 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] | 6230 | { |
| 6231 | /* Check stack size. */ |
| 6232 | if (!lua_checkstack(L, 3)) |
| 6233 | return 0; |
| 6234 | |
| 6235 | lua_newtable(L); |
| 6236 | lua_pushlightuserdata(L, msg); |
| 6237 | lua_rawseti(L, -2, 0); |
| 6238 | |
| 6239 | /* Create the "channel" field that contains the request channel object. */ |
| 6240 | lua_pushstring(L, "channel"); |
| 6241 | if (!hlua_channel_new(L, msg->chn)) |
| 6242 | return 0; |
| 6243 | lua_rawset(L, -3); |
| 6244 | |
| 6245 | /* Pop a class stream metatable and affect it to the table. */ |
| 6246 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_msg_ref); |
| 6247 | lua_setmetatable(L, -2); |
| 6248 | |
| 6249 | return 1; |
| 6250 | } |
| 6251 | |
| 6252 | /* Helper function returning a filter attached to the HTTP message at the |
| 6253 | * 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] | 6254 | * 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] | 6255 | * filled with output and input length respectively. |
| 6256 | */ |
| 6257 | static struct filter *hlua_http_msg_filter(lua_State *L, int ud, struct http_msg *msg, size_t *offset, size_t *len) |
| 6258 | { |
| 6259 | struct channel *chn = msg->chn; |
| 6260 | struct htx *htx = htxbuf(&chn->buf); |
| 6261 | struct filter *filter = NULL; |
| 6262 | |
| 6263 | *offset = co_data(msg->chn); |
| 6264 | *len = htx->data - co_data(msg->chn); |
| 6265 | |
| 6266 | if (lua_getfield(L, ud, "__filter") == LUA_TLIGHTUSERDATA) { |
| 6267 | filter = lua_touserdata (L, -1); |
| 6268 | if (msg->msg_state >= HTTP_MSG_DATA) { |
| 6269 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 6270 | |
| 6271 | *offset = flt_ctx->cur_off[CHN_IDX(chn)]; |
| 6272 | *len = flt_ctx->cur_len[CHN_IDX(chn)]; |
| 6273 | } |
| 6274 | } |
| 6275 | |
| 6276 | lua_pop(L, 1); |
| 6277 | return filter; |
| 6278 | } |
| 6279 | |
| 6280 | /* Returns true if the channel attached to the HTTP message is the response |
| 6281 | * channel. |
| 6282 | */ |
| 6283 | __LJMP static int hlua_http_msg_is_resp(lua_State *L) |
| 6284 | { |
| 6285 | struct http_msg *msg; |
| 6286 | |
| 6287 | MAY_LJMP(check_args(L, 1, "is_resp")); |
| 6288 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6289 | |
| 6290 | lua_pushboolean(L, !!(msg->chn->flags & CF_ISRESP)); |
| 6291 | return 1; |
| 6292 | } |
| 6293 | |
| 6294 | /* Returns an array containing the elements status-line of the HTTP message. It relies |
| 6295 | * on hlua_http_get_stline(). |
| 6296 | */ |
| 6297 | __LJMP static int hlua_http_msg_get_stline(lua_State *L) |
| 6298 | { |
| 6299 | struct http_msg *msg; |
| 6300 | struct htx *htx; |
| 6301 | struct htx_sl *sl; |
| 6302 | |
| 6303 | MAY_LJMP(check_args(L, 1, "get_stline")); |
| 6304 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6305 | |
| 6306 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6307 | WILL_LJMP(lua_error(L)); |
| 6308 | |
| 6309 | htx = htxbuf(&msg->chn->buf); |
| 6310 | sl = http_get_stline(htx); |
| 6311 | if (!sl) |
| 6312 | return 0; |
| 6313 | return hlua_http_get_stline(L, sl); |
| 6314 | } |
| 6315 | |
| 6316 | /* Returns an array containing all headers of the HTTP message. it relies on |
| 6317 | * hlua_http_get_headers(). |
| 6318 | */ |
| 6319 | __LJMP static int hlua_http_msg_get_headers(lua_State *L) |
| 6320 | { |
| 6321 | struct http_msg *msg; |
| 6322 | |
| 6323 | MAY_LJMP(check_args(L, 1, "get_headers")); |
| 6324 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6325 | |
| 6326 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6327 | WILL_LJMP(lua_error(L)); |
| 6328 | |
| 6329 | return hlua_http_get_headers(L, msg); |
| 6330 | } |
| 6331 | |
| 6332 | /* Deletes all occurrences of an header in the HTTP message matching on its |
| 6333 | * name. It relies on hlua_http_del_hdr(). |
| 6334 | */ |
| 6335 | __LJMP static int hlua_http_msg_del_hdr(lua_State *L) |
| 6336 | { |
| 6337 | struct http_msg *msg; |
| 6338 | |
| 6339 | MAY_LJMP(check_args(L, 2, "del_header")); |
| 6340 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6341 | |
| 6342 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6343 | WILL_LJMP(lua_error(L)); |
| 6344 | |
| 6345 | return hlua_http_del_hdr(L, msg); |
| 6346 | } |
| 6347 | |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 6348 | /* 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] | 6349 | * message given its name against a regex and replaces it if it matches. It |
| 6350 | * relies on hlua_http_rep_hdr(). |
| 6351 | */ |
| 6352 | __LJMP static int hlua_http_msg_rep_hdr(lua_State *L) |
| 6353 | { |
| 6354 | struct http_msg *msg; |
| 6355 | |
| 6356 | MAY_LJMP(check_args(L, 4, "rep_header")); |
| 6357 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6358 | |
| 6359 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6360 | WILL_LJMP(lua_error(L)); |
| 6361 | |
| 6362 | return hlua_http_rep_hdr(L, msg, 1); |
| 6363 | } |
| 6364 | |
Ilya Shipitsin | bd6b4be | 2021-10-15 16:18:21 +0500 | [diff] [blame] | 6365 | /* 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] | 6366 | * message given its name against a regex and replaces it if it matches. It |
| 6367 | * relies on hlua_http_rep_hdr(). |
| 6368 | */ |
| 6369 | __LJMP static int hlua_http_msg_rep_val(lua_State *L) |
| 6370 | { |
| 6371 | struct http_msg *msg; |
| 6372 | |
| 6373 | MAY_LJMP(check_args(L, 4, "rep_value")); |
| 6374 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6375 | |
| 6376 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6377 | WILL_LJMP(lua_error(L)); |
| 6378 | |
| 6379 | return hlua_http_rep_hdr(L, msg, 0); |
| 6380 | } |
| 6381 | |
| 6382 | /* Add an header in the HTTP message. It relies on hlua_http_add_hdr() */ |
| 6383 | __LJMP static int hlua_http_msg_add_hdr(lua_State *L) |
| 6384 | { |
| 6385 | struct http_msg *msg; |
| 6386 | |
| 6387 | MAY_LJMP(check_args(L, 3, "add_header")); |
| 6388 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6389 | |
| 6390 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6391 | WILL_LJMP(lua_error(L)); |
| 6392 | |
| 6393 | return hlua_http_add_hdr(L, msg); |
| 6394 | } |
| 6395 | |
| 6396 | /* Add an header in the HTTP message removing existing headers with the same |
| 6397 | * name. It relies on hlua_http_del_hdr() and hlua_http_add_hdr(). |
| 6398 | */ |
| 6399 | __LJMP static int hlua_http_msg_set_hdr(lua_State *L) |
| 6400 | { |
| 6401 | struct http_msg *msg; |
| 6402 | |
| 6403 | MAY_LJMP(check_args(L, 3, "set_header")); |
| 6404 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6405 | |
| 6406 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6407 | WILL_LJMP(lua_error(L)); |
| 6408 | |
| 6409 | hlua_http_del_hdr(L, msg); |
| 6410 | return hlua_http_add_hdr(L, msg); |
| 6411 | } |
| 6412 | |
| 6413 | /* Rewrites the request method. It relies on http_req_replace_stline(). */ |
| 6414 | __LJMP static int hlua_http_msg_set_meth(lua_State *L) |
| 6415 | { |
| 6416 | struct stream *s; |
| 6417 | struct http_msg *msg; |
| 6418 | const char *name; |
| 6419 | size_t name_len; |
| 6420 | |
| 6421 | MAY_LJMP(check_args(L, 2, "set_method")); |
| 6422 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6423 | name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 6424 | |
| 6425 | if ((msg->chn->flags & CF_ISRESP) || msg->msg_state > HTTP_MSG_BODY) |
| 6426 | WILL_LJMP(lua_error(L)); |
| 6427 | |
| 6428 | s = chn_strm(msg->chn); |
| 6429 | lua_pushboolean(L, http_req_replace_stline(0, name, name_len, s->be, s) != -1); |
| 6430 | return 1; |
| 6431 | } |
| 6432 | |
| 6433 | /* Rewrites the request path. It relies on http_req_replace_stline(). */ |
| 6434 | __LJMP static int hlua_http_msg_set_path(lua_State *L) |
| 6435 | { |
| 6436 | struct stream *s; |
| 6437 | struct http_msg *msg; |
| 6438 | const char *name; |
| 6439 | size_t name_len; |
| 6440 | |
| 6441 | MAY_LJMP(check_args(L, 2, "set_path")); |
| 6442 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6443 | name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 6444 | |
| 6445 | if ((msg->chn->flags & CF_ISRESP) || msg->msg_state > HTTP_MSG_BODY) |
| 6446 | WILL_LJMP(lua_error(L)); |
| 6447 | |
| 6448 | s = chn_strm(msg->chn); |
| 6449 | lua_pushboolean(L, http_req_replace_stline(1, name, name_len, s->be, s) != -1); |
| 6450 | return 1; |
| 6451 | } |
| 6452 | |
| 6453 | /* Rewrites the request query-string. It relies on http_req_replace_stline(). */ |
| 6454 | __LJMP static int hlua_http_msg_set_query(lua_State *L) |
| 6455 | { |
| 6456 | struct stream *s; |
| 6457 | struct http_msg *msg; |
| 6458 | const char *name; |
| 6459 | size_t name_len; |
| 6460 | |
| 6461 | MAY_LJMP(check_args(L, 2, "set_query")); |
| 6462 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6463 | name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 6464 | |
| 6465 | if ((msg->chn->flags & CF_ISRESP) || msg->msg_state > HTTP_MSG_BODY) |
| 6466 | WILL_LJMP(lua_error(L)); |
| 6467 | |
| 6468 | /* Check length. */ |
| 6469 | if (name_len > trash.size - 1) { |
| 6470 | lua_pushboolean(L, 0); |
| 6471 | return 1; |
| 6472 | } |
| 6473 | |
| 6474 | /* Add the mark question as prefix. */ |
| 6475 | chunk_reset(&trash); |
| 6476 | trash.area[trash.data++] = '?'; |
| 6477 | memcpy(trash.area + trash.data, name, name_len); |
| 6478 | trash.data += name_len; |
| 6479 | |
| 6480 | s = chn_strm(msg->chn); |
| 6481 | lua_pushboolean(L, http_req_replace_stline(2, trash.area, trash.data, s->be, s) != -1); |
| 6482 | return 1; |
| 6483 | } |
| 6484 | |
| 6485 | /* Rewrites the request URI. It relies on http_req_replace_stline(). */ |
| 6486 | __LJMP static int hlua_http_msg_set_uri(lua_State *L) |
| 6487 | { |
| 6488 | struct stream *s; |
| 6489 | struct http_msg *msg; |
| 6490 | const char *name; |
| 6491 | size_t name_len; |
| 6492 | |
| 6493 | MAY_LJMP(check_args(L, 2, "set_uri")); |
| 6494 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6495 | name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 6496 | |
| 6497 | if ((msg->chn->flags & CF_ISRESP) || msg->msg_state > HTTP_MSG_BODY) |
| 6498 | WILL_LJMP(lua_error(L)); |
| 6499 | |
| 6500 | s = chn_strm(msg->chn); |
| 6501 | lua_pushboolean(L, http_req_replace_stline(3, name, name_len, s->be, s) != -1); |
| 6502 | return 1; |
| 6503 | } |
| 6504 | |
| 6505 | /* Rewrites the response status code. It relies on http_res_set_status(). */ |
| 6506 | __LJMP static int hlua_http_msg_set_status(lua_State *L) |
| 6507 | { |
| 6508 | struct http_msg *msg; |
| 6509 | unsigned int code; |
| 6510 | const char *reason; |
| 6511 | size_t reason_len; |
| 6512 | |
| 6513 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6514 | code = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 6515 | reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, &reason_len)); |
| 6516 | |
| 6517 | if (!(msg->chn->flags & CF_ISRESP) || msg->msg_state > HTTP_MSG_BODY) |
| 6518 | WILL_LJMP(lua_error(L)); |
| 6519 | |
| 6520 | lua_pushboolean(L, http_res_set_status(code, ist2(reason, reason_len), chn_strm(msg->chn)) != -1); |
| 6521 | return 1; |
| 6522 | } |
| 6523 | |
| 6524 | /* Returns true if the HTTP message is full. */ |
| 6525 | __LJMP static int hlua_http_msg_is_full(lua_State *L) |
| 6526 | { |
| 6527 | struct http_msg *msg; |
| 6528 | |
| 6529 | MAY_LJMP(check_args(L, 1, "is_full")); |
| 6530 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6531 | lua_pushboolean(L, channel_full(msg->chn, 0)); |
| 6532 | return 1; |
| 6533 | } |
| 6534 | |
| 6535 | /* Returns true if the HTTP message may still receive data. */ |
| 6536 | __LJMP static int hlua_http_msg_may_recv(lua_State *L) |
| 6537 | { |
| 6538 | struct http_msg *msg; |
| 6539 | struct htx *htx; |
| 6540 | |
| 6541 | MAY_LJMP(check_args(L, 1, "may_recv")); |
| 6542 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6543 | htx = htxbuf(&msg->chn->buf); |
| 6544 | lua_pushboolean(L, (htx_expect_more(htx) && !channel_input_closed(msg->chn) && channel_may_recv(msg->chn))); |
| 6545 | return 1; |
| 6546 | } |
| 6547 | |
| 6548 | /* Returns true if the HTTP message EOM was received */ |
| 6549 | __LJMP static int hlua_http_msg_is_eom(lua_State *L) |
| 6550 | { |
| 6551 | struct http_msg *msg; |
| 6552 | struct htx *htx; |
| 6553 | |
| 6554 | MAY_LJMP(check_args(L, 1, "may_recv")); |
| 6555 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6556 | htx = htxbuf(&msg->chn->buf); |
| 6557 | lua_pushboolean(L, !htx_expect_more(htx)); |
| 6558 | return 1; |
| 6559 | } |
| 6560 | |
| 6561 | /* Returns the number of bytes available in the input side of the HTTP |
| 6562 | * message. This function never fails. |
| 6563 | */ |
| 6564 | __LJMP static int hlua_http_msg_get_in_len(lua_State *L) |
| 6565 | { |
| 6566 | struct http_msg *msg; |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 6567 | size_t output, input; |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6568 | |
| 6569 | MAY_LJMP(check_args(L, 1, "input")); |
| 6570 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 6571 | hlua_http_msg_filter(L, 1, msg, &output, &input); |
| 6572 | lua_pushinteger(L, input); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6573 | return 1; |
| 6574 | } |
| 6575 | |
| 6576 | /* Returns the number of bytes available in the output side of the HTTP |
| 6577 | * message. This function never fails. |
| 6578 | */ |
| 6579 | __LJMP static int hlua_http_msg_get_out_len(lua_State *L) |
| 6580 | { |
| 6581 | struct http_msg *msg; |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 6582 | size_t output, input; |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6583 | |
| 6584 | MAY_LJMP(check_args(L, 1, "output")); |
| 6585 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 6586 | hlua_http_msg_filter(L, 1, msg, &output, &input); |
| 6587 | lua_pushinteger(L, output); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6588 | return 1; |
| 6589 | } |
| 6590 | |
| 6591 | /* Copies at most <len> bytes of DATA blocks from the HTTP message <msg> |
| 6592 | * 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] | 6593 | * 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] | 6594 | * block. This function is called during the payload filtering, so the headers |
| 6595 | * are already scheduled for output (from the filter point of view). |
| 6596 | */ |
| 6597 | static int _hlua_http_msg_dup(struct http_msg *msg, lua_State *L, size_t offset, size_t len) |
| 6598 | { |
| 6599 | struct htx *htx = htxbuf(&msg->chn->buf); |
| 6600 | struct htx_blk *blk; |
| 6601 | struct htx_ret htxret; |
| 6602 | luaL_Buffer b; |
| 6603 | int ret = 0; |
| 6604 | |
| 6605 | luaL_buffinit(L, &b); |
| 6606 | htxret = htx_find_offset(htx, offset); |
| 6607 | for (blk = htxret.blk, offset = htxret.ret; blk && len; blk = htx_get_next_blk(htx, blk)) { |
| 6608 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 6609 | struct ist v; |
| 6610 | |
| 6611 | switch (type) { |
| 6612 | case HTX_BLK_UNUSED: |
| 6613 | break; |
| 6614 | |
| 6615 | case HTX_BLK_DATA: |
| 6616 | v = htx_get_blk_value(htx, blk); |
Tim Duesterhus | b113b5c | 2021-09-15 13:58:44 +0200 | [diff] [blame] | 6617 | v = istadv(v, offset); |
Tim Duesterhus | 2471f5c | 2021-11-08 09:05:01 +0100 | [diff] [blame] | 6618 | v = isttrim(v, len); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6619 | |
| 6620 | luaL_addlstring(&b, v.ptr, v.len); |
| 6621 | ret += v.len; |
| 6622 | break; |
| 6623 | |
| 6624 | default: |
vishnu | 0af4bd7 | 2021-10-24 06:46:24 +0530 | [diff] [blame] | 6625 | if (!ret) |
| 6626 | goto no_data; |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6627 | goto end; |
| 6628 | } |
| 6629 | offset = 0; |
| 6630 | } |
| 6631 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6632 | end: |
vishnu | 0af4bd7 | 2021-10-24 06:46:24 +0530 | [diff] [blame] | 6633 | if (!ret && (htx->flags & HTX_FL_EOM)) |
| 6634 | goto no_data; |
| 6635 | luaL_pushresult(&b); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6636 | return ret; |
vishnu | 0af4bd7 | 2021-10-24 06:46:24 +0530 | [diff] [blame] | 6637 | |
| 6638 | no_data: |
| 6639 | /* Remove the empty string and push nil on the stack */ |
| 6640 | lua_pop(L, 1); |
| 6641 | lua_pushnil(L); |
| 6642 | return 0; |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6643 | } |
| 6644 | |
| 6645 | /* Copies the string <str> to the HTTP message <msg> at the offset |
| 6646 | * <offset>. This function returns -1 if data cannot be copied. Otherwise, it |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 6647 | * returns the amount of data written. This function is responsible to update |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6648 | * the filter context. |
| 6649 | */ |
| 6650 | static int _hlua_http_msg_insert(struct http_msg *msg, struct filter *filter, struct ist str, size_t offset) |
| 6651 | { |
| 6652 | struct htx *htx = htx_from_buf(&msg->chn->buf); |
| 6653 | struct htx_ret htxret; |
| 6654 | int /*max, */ret = 0; |
| 6655 | |
| 6656 | /* Nothing to do, just return */ |
| 6657 | if (unlikely(istlen(str) == 0)) |
| 6658 | goto end; |
| 6659 | |
| 6660 | if (istlen(str) > htx_free_data_space(htx)) { |
| 6661 | ret = -1; |
| 6662 | goto end; |
| 6663 | } |
| 6664 | |
| 6665 | htxret = htx_find_offset(htx, offset); |
| 6666 | if (!htxret.blk || htx_get_blk_type(htxret.blk) != HTX_BLK_DATA) { |
| 6667 | if (!htx_add_last_data(htx, str)) |
| 6668 | goto end; |
| 6669 | } |
| 6670 | else { |
| 6671 | struct ist v = htx_get_blk_value(htx, htxret.blk); |
| 6672 | v.ptr += htxret.ret; |
| 6673 | v.len = 0; |
| 6674 | if (!htx_replace_blk_value(htx, htxret.blk, v, str)) |
| 6675 | goto end; |
| 6676 | } |
| 6677 | ret = str.len; |
| 6678 | if (ret) { |
| 6679 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 6680 | flt_update_offsets(filter, msg->chn, ret); |
| 6681 | flt_ctx->cur_len[CHN_IDX(msg->chn)] += ret; |
| 6682 | } |
| 6683 | |
| 6684 | end: |
| 6685 | htx_to_buf(htx, &msg->chn->buf); |
| 6686 | return ret; |
| 6687 | } |
| 6688 | |
| 6689 | /* Helper function removing at most <len> bytes of DATA blocks at the absolute |
| 6690 | * position <offset>. It stops on the first non-DATA HTX block. This function is |
| 6691 | * called during the payload filtering, so the headers are already scheduled for |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 6692 | * output (from the filter point of view). This function is responsible to |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6693 | * update the filter context. |
| 6694 | */ |
| 6695 | static void _hlua_http_msg_delete(struct http_msg *msg, struct filter *filter, size_t offset, size_t len) |
| 6696 | { |
| 6697 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 6698 | struct htx *htx = htx_from_buf(&msg->chn->buf); |
| 6699 | struct htx_blk *blk; |
| 6700 | struct htx_ret htxret; |
| 6701 | size_t ret = 0; |
| 6702 | |
| 6703 | /* Be sure <len> is always the amount of DATA to remove */ |
| 6704 | 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] | 6705 | /* When htx tail type == HTX_BLK_DATA, no need to take care |
| 6706 | * of special blocks like HTX_BLK_EOT. |
| 6707 | * We simply truncate after offset |
| 6708 | * (truncate targeted blk and discard the following ones) |
| 6709 | */ |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6710 | htx_truncate(htx, offset); |
| 6711 | ret = len; |
| 6712 | goto end; |
| 6713 | } |
| 6714 | |
| 6715 | htxret = htx_find_offset(htx, offset); |
| 6716 | blk = htxret.blk; |
| 6717 | if (htxret.ret) { |
Aurelien DARRAGON | bcbcf98 | 2022-09-28 19:08:15 +0200 | [diff] [blame] | 6718 | /* dealing with offset: we need to trim targeted blk */ |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6719 | struct ist v; |
| 6720 | |
| 6721 | if (htx_get_blk_type(blk) != HTX_BLK_DATA) |
| 6722 | goto end; |
Aurelien DARRAGON | bcbcf98 | 2022-09-28 19:08:15 +0200 | [diff] [blame] | 6723 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6724 | v = htx_get_blk_value(htx, blk); |
Tim Duesterhus | a029d78 | 2022-10-08 12:33:18 +0200 | [diff] [blame] | 6725 | v = istadv(v, htxret.ret); |
Aurelien DARRAGON | bcbcf98 | 2022-09-28 19:08:15 +0200 | [diff] [blame] | 6726 | |
Tim Duesterhus | 2471f5c | 2021-11-08 09:05:01 +0100 | [diff] [blame] | 6727 | v = isttrim(v, len); |
Aurelien DARRAGON | bcbcf98 | 2022-09-28 19:08:15 +0200 | [diff] [blame] | 6728 | /* trimming data in blk: discard everything after the offset |
| 6729 | * (replace 'v' with 'IST_NULL') |
| 6730 | */ |
Tim Duesterhus | b113b5c | 2021-09-15 13:58:44 +0200 | [diff] [blame] | 6731 | blk = htx_replace_blk_value(htx, blk, v, IST_NULL); |
Aurelien DARRAGON | bcbcf98 | 2022-09-28 19:08:15 +0200 | [diff] [blame] | 6732 | if (blk && v.len < len) { |
| 6733 | /* In this case, caller wants to keep removing data, |
| 6734 | * but we need to spare current blk |
| 6735 | * because it was already trimmed |
| 6736 | */ |
| 6737 | blk = htx_get_next_blk(htx, blk); |
| 6738 | } |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6739 | len -= v.len; |
| 6740 | ret += v.len; |
| 6741 | } |
| 6742 | |
| 6743 | |
| 6744 | while (blk && len) { |
Aurelien DARRAGON | bcbcf98 | 2022-09-28 19:08:15 +0200 | [diff] [blame] | 6745 | /* there is more data that needs to be discarded */ |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6746 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 6747 | uint32_t sz = htx_get_blksz(blk); |
| 6748 | |
| 6749 | switch (type) { |
| 6750 | case HTX_BLK_UNUSED: |
| 6751 | break; |
| 6752 | |
| 6753 | case HTX_BLK_DATA: |
| 6754 | if (len < sz) { |
Aurelien DARRAGON | bcbcf98 | 2022-09-28 19:08:15 +0200 | [diff] [blame] | 6755 | /* don't discard whole blk, only part of it |
| 6756 | * (from the beginning) |
| 6757 | */ |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6758 | htx_cut_data_blk(htx, blk, len); |
| 6759 | ret += len; |
| 6760 | goto end; |
| 6761 | } |
| 6762 | break; |
| 6763 | |
| 6764 | default: |
Aurelien DARRAGON | bcbcf98 | 2022-09-28 19:08:15 +0200 | [diff] [blame] | 6765 | /* HTX_BLK_EOT blk won't be removed */ |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6766 | goto end; |
| 6767 | } |
| 6768 | |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 6769 | /* Remove all the data block */ |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6770 | len -= sz; |
| 6771 | ret += sz; |
| 6772 | blk = htx_remove_blk(htx, blk); |
| 6773 | } |
| 6774 | |
| 6775 | end: |
| 6776 | flt_update_offsets(filter, msg->chn, -ret); |
| 6777 | flt_ctx->cur_len[CHN_IDX(msg->chn)] -= ret; |
| 6778 | /* WARNING: we don't call htx_to_buf() on purpose, because we don't want |
| 6779 | * to loose the EOM flag if the message is empty. |
| 6780 | */ |
| 6781 | } |
| 6782 | |
| 6783 | /* Copies input data found in an HTTP message. Unlike the channel function used |
| 6784 | * to duplicate raw data, this one can only be called inside a filter, from |
| 6785 | * http_payload callback. So it cannot yield. An exception is returned if it is |
| 6786 | * called from another callback. If nothing was copied, a nil value is pushed on |
| 6787 | * the stack. |
| 6788 | */ |
| 6789 | __LJMP static int hlua_http_msg_get_body(lua_State *L) |
| 6790 | { |
| 6791 | struct http_msg *msg; |
| 6792 | struct filter *filter; |
| 6793 | size_t output, input; |
| 6794 | int offset, len; |
| 6795 | |
| 6796 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 6797 | WILL_LJMP(luaL_error(L, "'data' expects at most 2 arguments")); |
| 6798 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6799 | |
| 6800 | if (msg->msg_state < HTTP_MSG_DATA) |
| 6801 | WILL_LJMP(lua_error(L)); |
| 6802 | |
| 6803 | filter = hlua_http_msg_filter(L, 1, msg, &output, &input); |
| 6804 | if (!filter || !hlua_filter_from_payload(filter)) |
| 6805 | WILL_LJMP(lua_error(L)); |
| 6806 | |
| 6807 | if (!ci_data(msg->chn) && channel_input_closed(msg->chn)) { |
| 6808 | lua_pushnil(L); |
| 6809 | return 1; |
| 6810 | } |
| 6811 | |
| 6812 | offset = output; |
| 6813 | if (lua_gettop(L) > 1) { |
| 6814 | offset = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 6815 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 6816 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6817 | offset += output; |
| 6818 | if (offset < output || offset > input + output) { |
| 6819 | lua_pushfstring(L, "offset out of range."); |
| 6820 | WILL_LJMP(lua_error(L)); |
| 6821 | } |
| 6822 | } |
| 6823 | len = output + input - offset; |
| 6824 | if (lua_gettop(L) == 3) { |
| 6825 | len = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 6826 | if (!len) |
| 6827 | goto dup; |
| 6828 | if (len == -1) |
| 6829 | len = global.tune.bufsize; |
| 6830 | if (len < 0) { |
| 6831 | lua_pushfstring(L, "length out of range."); |
| 6832 | WILL_LJMP(lua_error(L)); |
| 6833 | } |
| 6834 | } |
| 6835 | |
| 6836 | dup: |
| 6837 | _hlua_http_msg_dup(msg, L, offset, len); |
| 6838 | return 1; |
| 6839 | } |
| 6840 | |
| 6841 | /* Appends a string to the HTTP message, after all existing DATA blocks but |
| 6842 | * before the trailers, if any. It returns the amount of data written or -1 if |
| 6843 | * nothing was copied. Unlike the channel function used to append data, this one |
| 6844 | * can only be called inside a filter, from http_payload callback. So it cannot |
| 6845 | * yield. An exception is returned if it is called from another callback. |
| 6846 | */ |
| 6847 | __LJMP static int hlua_http_msg_append(lua_State *L) |
| 6848 | { |
| 6849 | struct http_msg *msg; |
| 6850 | struct filter *filter; |
| 6851 | const char *str; |
| 6852 | size_t offset, len, sz; |
| 6853 | int ret; |
| 6854 | |
| 6855 | MAY_LJMP(check_args(L, 2, "append")); |
| 6856 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6857 | |
| 6858 | if (msg->msg_state < HTTP_MSG_DATA) |
| 6859 | WILL_LJMP(lua_error(L)); |
| 6860 | |
| 6861 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 6862 | filter = hlua_http_msg_filter(L, 1, msg, &offset, &len); |
| 6863 | if (!filter || !hlua_filter_from_payload(filter)) |
| 6864 | WILL_LJMP(lua_error(L)); |
| 6865 | |
| 6866 | ret = _hlua_http_msg_insert(msg, filter, ist2(str, sz), offset+len); |
| 6867 | lua_pushinteger(L, ret); |
| 6868 | return 1; |
| 6869 | } |
| 6870 | |
| 6871 | /* Prepends a string to the HTTP message, before all existing DATA blocks. It |
| 6872 | * returns the amount of data written or -1 if nothing was copied. Unlike the |
| 6873 | * channel function used to prepend data, this one can only be called inside a |
| 6874 | * filter, from http_payload callback. So it cannot yield. An exception is |
| 6875 | * returned if it is called from another callback. |
| 6876 | */ |
| 6877 | __LJMP static int hlua_http_msg_prepend(lua_State *L) |
| 6878 | { |
| 6879 | struct http_msg *msg; |
| 6880 | struct filter *filter; |
| 6881 | const char *str; |
| 6882 | size_t offset, len, sz; |
| 6883 | int ret; |
| 6884 | |
| 6885 | MAY_LJMP(check_args(L, 2, "prepend")); |
| 6886 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6887 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6888 | if (msg->msg_state < HTTP_MSG_DATA) |
| 6889 | WILL_LJMP(lua_error(L)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6890 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6891 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 6892 | filter = hlua_http_msg_filter(L, 1, msg, &offset, &len); |
| 6893 | if (!filter || !hlua_filter_from_payload(filter)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6894 | WILL_LJMP(lua_error(L)); |
| 6895 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6896 | ret = _hlua_http_msg_insert(msg, filter, ist2(str, sz), offset); |
| 6897 | lua_pushinteger(L, ret); |
| 6898 | return 1; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6899 | } |
| 6900 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6901 | /* Inserts a string to the HTTP message at a given offset. By default the string |
| 6902 | * is appended at the end of DATA blocks. It returns the amount of data written |
| 6903 | * or -1 if nothing was copied. Unlike the channel function used to insert data, |
| 6904 | * this one can only be called inside a filter, from http_payload callback. So |
| 6905 | * it cannot yield. An exception is returned if it is called from another |
| 6906 | * callback. |
| 6907 | */ |
| 6908 | __LJMP static int hlua_http_msg_insert_data(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6909 | { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6910 | struct http_msg *msg; |
| 6911 | struct filter *filter; |
| 6912 | const char *str; |
| 6913 | size_t input, output, sz; |
| 6914 | int offset; |
| 6915 | int ret; |
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 | if (lua_gettop(L) < 2 || lua_gettop(L) > 3) |
| 6918 | 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] | 6919 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6920 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6921 | if (msg->msg_state < HTTP_MSG_DATA) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6922 | WILL_LJMP(lua_error(L)); |
| 6923 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6924 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
Aurelien DARRAGON | 7fdba0a | 2022-09-28 16:03:45 +0200 | [diff] [blame] | 6925 | filter = hlua_http_msg_filter(L, 1, msg, &output, &input); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6926 | if (!filter || !hlua_filter_from_payload(filter)) |
| 6927 | WILL_LJMP(lua_error(L)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6928 | |
Aurelien DARRAGON | 7fdba0a | 2022-09-28 16:03:45 +0200 | [diff] [blame] | 6929 | offset = output; |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6930 | if (lua_gettop(L) > 2) { |
| 6931 | offset = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 6932 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 6933 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6934 | offset += output; |
Aurelien DARRAGON | 7fdba0a | 2022-09-28 16:03:45 +0200 | [diff] [blame] | 6935 | if (offset > output + input) { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6936 | lua_pushfstring(L, "offset out of range."); |
| 6937 | WILL_LJMP(lua_error(L)); |
| 6938 | } |
| 6939 | } |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6940 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6941 | ret = _hlua_http_msg_insert(msg, filter, ist2(str, sz), offset); |
| 6942 | lua_pushinteger(L, ret); |
| 6943 | return 1; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6944 | } |
| 6945 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6946 | /* Removes a given amount of data from the HTTP message at a given offset. By |
| 6947 | * default all DATA blocks are removed. It returns the amount of data |
| 6948 | * removed. Unlike the channel function used to remove data, this one can only |
| 6949 | * be called inside a filter, from http_payload callback. So it cannot yield. An |
| 6950 | * exception is returned if it is called from another callback. |
| 6951 | */ |
| 6952 | __LJMP static int hlua_http_msg_del_data(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6953 | { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6954 | struct http_msg *msg; |
| 6955 | struct filter *filter; |
| 6956 | size_t input, output; |
| 6957 | int offset, len; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6958 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6959 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
Boyang Li | e0c5435 | 2022-05-10 17:47:23 +0000 | [diff] [blame] | 6960 | WILL_LJMP(luaL_error(L, "'remove' expects at most 2 arguments")); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6961 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6962 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6963 | if (msg->msg_state < HTTP_MSG_DATA) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6964 | WILL_LJMP(lua_error(L)); |
| 6965 | |
Aurelien DARRAGON | d7c71b0 | 2022-09-28 15:52:18 +0200 | [diff] [blame] | 6966 | filter = hlua_http_msg_filter(L, 1, msg, &output, &input); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6967 | if (!filter || !hlua_filter_from_payload(filter)) |
| 6968 | WILL_LJMP(lua_error(L)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6969 | |
Aurelien DARRAGON | d7c71b0 | 2022-09-28 15:52:18 +0200 | [diff] [blame] | 6970 | offset = output; |
Boyang Li | e0c5435 | 2022-05-10 17:47:23 +0000 | [diff] [blame] | 6971 | if (lua_gettop(L) > 1) { |
| 6972 | offset = MAY_LJMP(luaL_checkinteger(L, 2)); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6973 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 6974 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6975 | offset += output; |
Aurelien DARRAGON | d7c71b0 | 2022-09-28 15:52:18 +0200 | [diff] [blame] | 6976 | if (offset > output + input) { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6977 | lua_pushfstring(L, "offset out of range."); |
| 6978 | WILL_LJMP(lua_error(L)); |
| 6979 | } |
| 6980 | } |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6981 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6982 | len = output + input - offset; |
Boyang Li | e0c5435 | 2022-05-10 17:47:23 +0000 | [diff] [blame] | 6983 | if (lua_gettop(L) == 3) { |
| 6984 | len = MAY_LJMP(luaL_checkinteger(L, 3)); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6985 | if (!len) |
| 6986 | goto end; |
| 6987 | if (len == -1) |
| 6988 | len = output + input - offset; |
| 6989 | if (len < 0 || offset + len > output + input) { |
| 6990 | lua_pushfstring(L, "length out of range."); |
| 6991 | WILL_LJMP(lua_error(L)); |
| 6992 | } |
| 6993 | } |
| 6994 | |
| 6995 | _hlua_http_msg_delete(msg, filter, offset, len); |
| 6996 | |
| 6997 | end: |
| 6998 | lua_pushinteger(L, len); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6999 | return 1; |
| 7000 | } |
| 7001 | |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 7002 | /* 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] | 7003 | * all remaining data are removed, accordingly to the filter context. It returns |
| 7004 | * the amount of data written or -1 if nothing was copied. Unlike the channel |
| 7005 | * function used to replace data, this one can only be called inside a filter, |
| 7006 | * from http_payload callback. So it cannot yield. An exception is returned if |
| 7007 | * it is called from another callback. |
| 7008 | */ |
| 7009 | __LJMP static int hlua_http_msg_set_data(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7010 | { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 7011 | struct http_msg *msg; |
| 7012 | struct filter *filter; |
| 7013 | struct htx *htx; |
| 7014 | const char *str; |
| 7015 | size_t input, output, sz; |
| 7016 | int offset, len; |
| 7017 | int ret; |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 7018 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 7019 | if (lua_gettop(L) < 2 || lua_gettop(L) > 4) |
| 7020 | WILL_LJMP(luaL_error(L, "'set' expects at least 1 argument and at most 3 arguments")); |
| 7021 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 7022 | |
| 7023 | if (msg->msg_state < HTTP_MSG_DATA) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 7024 | WILL_LJMP(lua_error(L)); |
| 7025 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 7026 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 7027 | filter = hlua_http_msg_filter(L, 1, msg, &output, &input); |
| 7028 | if (!filter || !hlua_filter_from_payload(filter)) |
| 7029 | WILL_LJMP(lua_error(L)); |
| 7030 | |
| 7031 | offset = output; |
| 7032 | if (lua_gettop(L) > 2) { |
| 7033 | offset = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 7034 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 7035 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 7036 | offset += output; |
| 7037 | if (offset < output || offset > input + output) { |
| 7038 | lua_pushfstring(L, "offset out of range."); |
| 7039 | WILL_LJMP(lua_error(L)); |
| 7040 | } |
| 7041 | } |
| 7042 | |
| 7043 | len = output + input - offset; |
| 7044 | if (lua_gettop(L) == 4) { |
| 7045 | len = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 7046 | if (!len) |
| 7047 | goto set; |
| 7048 | if (len == -1) |
| 7049 | len = output + input - offset; |
| 7050 | if (len < 0 || offset + len > output + input) { |
| 7051 | lua_pushfstring(L, "length out of range."); |
| 7052 | WILL_LJMP(lua_error(L)); |
| 7053 | } |
| 7054 | } |
| 7055 | |
| 7056 | set: |
| 7057 | /* Be sure we can copied the string once input data will be removed. */ |
| 7058 | htx = htx_from_buf(&msg->chn->buf); |
| 7059 | if (sz > htx_free_data_space(htx) + len) |
| 7060 | lua_pushinteger(L, -1); |
| 7061 | else { |
| 7062 | _hlua_http_msg_delete(msg, filter, offset, len); |
| 7063 | ret = _hlua_http_msg_insert(msg, filter, ist2(str, sz), offset); |
| 7064 | lua_pushinteger(L, ret); |
| 7065 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7066 | return 1; |
| 7067 | } |
| 7068 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 7069 | /* Prepends data into an HTTP message and forward it, from the filter point of |
| 7070 | * view. It returns the amount of data written or -1 if nothing was sent. Unlike |
| 7071 | * the channel function used to send data, this one can only be called inside a |
| 7072 | * filter, from http_payload callback. So it cannot yield. An exception is |
| 7073 | * returned if it is called from another callback. |
| 7074 | */ |
| 7075 | __LJMP static int hlua_http_msg_send(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7076 | { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 7077 | struct http_msg *msg; |
| 7078 | struct filter *filter; |
| 7079 | struct htx *htx; |
| 7080 | const char *str; |
| 7081 | size_t offset, len, sz; |
| 7082 | int ret; |
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 | MAY_LJMP(check_args(L, 2, "send")); |
| 7085 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 7086 | |
| 7087 | if (msg->msg_state < HTTP_MSG_DATA) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 7088 | WILL_LJMP(lua_error(L)); |
| 7089 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 7090 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 7091 | filter = hlua_http_msg_filter(L, 1, msg, &offset, &len); |
| 7092 | if (!filter || !hlua_filter_from_payload(filter)) |
| 7093 | WILL_LJMP(lua_error(L)); |
| 7094 | |
| 7095 | /* Return an error if the channel's output is closed */ |
| 7096 | if (unlikely(channel_output_closed(msg->chn))) { |
| 7097 | lua_pushinteger(L, -1); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7098 | return 1; |
| 7099 | } |
| 7100 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 7101 | htx = htx_from_buf(&msg->chn->buf); |
| 7102 | if (sz > htx_free_data_space(htx)) { |
| 7103 | lua_pushinteger(L, -1); |
| 7104 | return 1; |
| 7105 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7106 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 7107 | ret = _hlua_http_msg_insert(msg, filter, ist2(str, sz), offset); |
| 7108 | if (ret > 0) { |
| 7109 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 7110 | |
| 7111 | FLT_OFF(filter, msg->chn) += ret; |
| 7112 | flt_ctx->cur_len[CHN_IDX(msg->chn)] -= ret; |
| 7113 | flt_ctx->cur_off[CHN_IDX(msg->chn)] += ret; |
| 7114 | } |
| 7115 | |
| 7116 | lua_pushinteger(L, ret); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7117 | return 1; |
| 7118 | } |
| 7119 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 7120 | /* Forwards a given amount of bytes. It return -1 if the channel's output is |
| 7121 | * closed. Otherwise, it returns the number of bytes forwarded. Unlike the |
| 7122 | * channel function used to forward data, this one can only be called inside a |
| 7123 | * filter, from http_payload callback. So it cannot yield. An exception is |
| 7124 | * returned if it is called from another callback. All other functions deal with |
| 7125 | * DATA block, this one not. |
| 7126 | */ |
| 7127 | __LJMP static int hlua_http_msg_forward(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7128 | { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 7129 | struct http_msg *msg; |
| 7130 | struct filter *filter; |
| 7131 | size_t offset, len; |
| 7132 | int fwd, ret = 0; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7133 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 7134 | MAY_LJMP(check_args(L, 2, "forward")); |
| 7135 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 7136 | |
| 7137 | if (msg->msg_state < HTTP_MSG_DATA) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 7138 | WILL_LJMP(lua_error(L)); |
| 7139 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 7140 | fwd = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 7141 | filter = hlua_http_msg_filter(L, 1, msg, &offset, &len); |
| 7142 | if (!filter || !hlua_filter_from_payload(filter)) |
| 7143 | WILL_LJMP(lua_error(L)); |
| 7144 | |
| 7145 | /* Nothing to do, just return */ |
| 7146 | if (!fwd) |
| 7147 | goto end; |
| 7148 | |
| 7149 | /* Return an error if the channel's output is closed */ |
| 7150 | if (unlikely(channel_output_closed(msg->chn))) { |
| 7151 | ret = -1; |
| 7152 | goto end; |
| 7153 | } |
| 7154 | |
| 7155 | ret = fwd; |
| 7156 | if (ret > len) |
| 7157 | ret = len; |
| 7158 | |
| 7159 | if (ret) { |
| 7160 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 7161 | |
| 7162 | FLT_OFF(filter, msg->chn) += ret; |
| 7163 | flt_ctx->cur_off[CHN_IDX(msg->chn)] += ret; |
| 7164 | flt_ctx->cur_len[CHN_IDX(msg->chn)] -= ret; |
| 7165 | } |
| 7166 | |
| 7167 | end: |
| 7168 | lua_pushinteger(L, ret); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7169 | return 1; |
| 7170 | } |
| 7171 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 7172 | /* Set EOM flag on the HTX message. |
| 7173 | * |
| 7174 | * NOTE: Not sure it is a good idea to manipulate this flag but for now I don't |
| 7175 | * really know how to do without this feature. |
| 7176 | */ |
| 7177 | __LJMP static int hlua_http_msg_set_eom(lua_State *L) |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 7178 | { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 7179 | struct http_msg *msg; |
| 7180 | struct htx *htx; |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 7181 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 7182 | MAY_LJMP(check_args(L, 1, "set_eom")); |
| 7183 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 7184 | htx = htxbuf(&msg->chn->buf); |
| 7185 | htx->flags |= HTX_FL_EOM; |
| 7186 | return 0; |
| 7187 | } |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 7188 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 7189 | /* Unset EOM flag on the HTX message. |
| 7190 | * |
| 7191 | * NOTE: Not sure it is a good idea to manipulate this flag but for now I don't |
| 7192 | * really know how to do without this feature. |
| 7193 | */ |
| 7194 | __LJMP static int hlua_http_msg_unset_eom(lua_State *L) |
| 7195 | { |
| 7196 | struct http_msg *msg; |
| 7197 | struct htx *htx; |
| 7198 | |
| 7199 | MAY_LJMP(check_args(L, 1, "set_eom")); |
| 7200 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 7201 | htx = htxbuf(&msg->chn->buf); |
| 7202 | htx->flags &= ~HTX_FL_EOM; |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 7203 | return 0; |
| 7204 | } |
| 7205 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7206 | /* |
| 7207 | * |
| 7208 | * |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7209 | * Class HTTPClient |
| 7210 | * |
| 7211 | * |
| 7212 | */ |
| 7213 | __LJMP static struct hlua_httpclient *hlua_checkhttpclient(lua_State *L, int ud) |
| 7214 | { |
| 7215 | return MAY_LJMP(hlua_checkudata(L, ud, class_httpclient_ref)); |
| 7216 | } |
| 7217 | |
William Lallemand | f77f1de | 2021-09-28 19:10:38 +0200 | [diff] [blame] | 7218 | |
| 7219 | /* stops the httpclient and ask it to kill itself */ |
| 7220 | __LJMP static int hlua_httpclient_gc(lua_State *L) |
| 7221 | { |
| 7222 | struct hlua_httpclient *hlua_hc; |
| 7223 | |
| 7224 | MAY_LJMP(check_args(L, 1, "__gc")); |
| 7225 | |
| 7226 | hlua_hc = MAY_LJMP(hlua_checkhttpclient(L, 1)); |
| 7227 | |
Aurelien DARRAGON | 3ffbf38 | 2023-02-09 17:02:57 +0100 | [diff] [blame] | 7228 | if (MT_LIST_DELETE(&hlua_hc->by_hlua)) { |
| 7229 | /* we won the race against hlua_httpclient_destroy_all() */ |
William Lallemand | bb58142 | 2022-10-20 10:57:28 +0200 | [diff] [blame] | 7230 | httpclient_stop_and_destroy(hlua_hc->hc); |
Aurelien DARRAGON | 3ffbf38 | 2023-02-09 17:02:57 +0100 | [diff] [blame] | 7231 | hlua_hc->hc = NULL; |
| 7232 | } |
William Lallemand | f77f1de | 2021-09-28 19:10:38 +0200 | [diff] [blame] | 7233 | |
| 7234 | return 0; |
| 7235 | } |
| 7236 | |
| 7237 | |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7238 | __LJMP static int hlua_httpclient_new(lua_State *L) |
| 7239 | { |
| 7240 | struct hlua_httpclient *hlua_hc; |
| 7241 | struct hlua *hlua; |
| 7242 | |
| 7243 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 7244 | hlua = hlua_gethlua(L); |
| 7245 | if (!hlua) |
| 7246 | return 0; |
| 7247 | |
| 7248 | /* Check stack size. */ |
| 7249 | if (!lua_checkstack(L, 3)) { |
| 7250 | hlua_pusherror(L, "httpclient: full stack"); |
| 7251 | goto err; |
| 7252 | } |
| 7253 | /* Create the object: obj[0] = userdata. */ |
| 7254 | lua_newtable(L); |
| 7255 | hlua_hc = MAY_LJMP(lua_newuserdata(L, sizeof(*hlua_hc))); |
| 7256 | lua_rawseti(L, -2, 0); |
| 7257 | memset(hlua_hc, 0, sizeof(*hlua_hc)); |
| 7258 | |
| 7259 | hlua_hc->hc = httpclient_new(hlua, 0, IST_NULL); |
| 7260 | if (!hlua_hc->hc) |
| 7261 | goto err; |
| 7262 | |
Aurelien DARRAGON | 3ffbf38 | 2023-02-09 17:02:57 +0100 | [diff] [blame] | 7263 | MT_LIST_APPEND(&hlua->hc_list, &hlua_hc->by_hlua); |
William Lallemand | bb58142 | 2022-10-20 10:57:28 +0200 | [diff] [blame] | 7264 | |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7265 | /* Pop a class stream metatable and affect it to the userdata. */ |
| 7266 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_httpclient_ref); |
| 7267 | lua_setmetatable(L, -2); |
| 7268 | |
| 7269 | return 1; |
| 7270 | |
| 7271 | err: |
| 7272 | WILL_LJMP(lua_error(L)); |
| 7273 | return 0; |
| 7274 | } |
| 7275 | |
| 7276 | |
| 7277 | /* |
| 7278 | * Callback of the httpclient, this callback wakes the lua task up, once the |
| 7279 | * httpclient receives some data |
| 7280 | * |
| 7281 | */ |
| 7282 | |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7283 | static void hlua_httpclient_cb(struct httpclient *hc) |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7284 | { |
| 7285 | struct hlua *hlua = hc->caller; |
| 7286 | |
| 7287 | if (!hlua || !hlua->task) |
| 7288 | return; |
| 7289 | |
| 7290 | task_wakeup(hlua->task, TASK_WOKEN_MSG); |
| 7291 | } |
| 7292 | |
| 7293 | /* |
William Lallemand | d7df73a | 2021-09-23 17:54:00 +0200 | [diff] [blame] | 7294 | * Fill the lua stack with headers from the httpclient response |
| 7295 | * This works the same way as the hlua_http_get_headers() function |
| 7296 | */ |
| 7297 | __LJMP static int hlua_httpclient_get_headers(lua_State *L, struct hlua_httpclient *hlua_hc) |
| 7298 | { |
| 7299 | struct http_hdr *hdr; |
| 7300 | |
| 7301 | lua_newtable(L); |
| 7302 | |
William Lallemand | ef574b2 | 2021-10-05 16:19:31 +0200 | [diff] [blame] | 7303 | for (hdr = hlua_hc->hc->res.hdrs; hdr && isttest(hdr->n); hdr++) { |
William Lallemand | d7df73a | 2021-09-23 17:54:00 +0200 | [diff] [blame] | 7304 | struct ist n, v; |
| 7305 | int len; |
| 7306 | |
| 7307 | n = hdr->n; |
| 7308 | v = hdr->v; |
| 7309 | |
| 7310 | /* Check for existing entry: |
| 7311 | * assume that the table is on the top of the stack, and |
| 7312 | * push the key in the stack, the function lua_gettable() |
| 7313 | * perform the lookup. |
| 7314 | */ |
| 7315 | |
| 7316 | lua_pushlstring(L, n.ptr, n.len); |
| 7317 | lua_gettable(L, -2); |
| 7318 | |
| 7319 | switch (lua_type(L, -1)) { |
| 7320 | case LUA_TNIL: |
| 7321 | /* Table not found, create it. */ |
| 7322 | lua_pop(L, 1); /* remove the nil value. */ |
| 7323 | lua_pushlstring(L, n.ptr, n.len); /* push the header name as key. */ |
| 7324 | lua_newtable(L); /* create and push empty table. */ |
| 7325 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 7326 | lua_rawseti(L, -2, 0); /* index header value (pop it). */ |
| 7327 | lua_rawset(L, -3); /* index new table with header name (pop the values). */ |
| 7328 | break; |
| 7329 | |
| 7330 | case LUA_TTABLE: |
| 7331 | /* Entry found: push the value in the table. */ |
| 7332 | len = lua_rawlen(L, -1); |
| 7333 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 7334 | lua_rawseti(L, -2, len+1); /* index header value (pop it). */ |
| 7335 | lua_pop(L, 1); /* remove the table (it is stored in the main table). */ |
| 7336 | break; |
| 7337 | |
| 7338 | default: |
| 7339 | /* Other cases are errors. */ |
| 7340 | hlua_pusherror(L, "internal error during the parsing of headers."); |
| 7341 | WILL_LJMP(lua_error(L)); |
| 7342 | } |
| 7343 | } |
| 7344 | return 1; |
| 7345 | } |
| 7346 | |
| 7347 | /* |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7348 | * Allocate and return an array of http_hdr ist extracted from the <headers> lua table |
| 7349 | * |
| 7350 | * Caller must free the result |
| 7351 | */ |
| 7352 | struct http_hdr *hlua_httpclient_table_to_hdrs(lua_State *L) |
| 7353 | { |
| 7354 | struct http_hdr hdrs[global.tune.max_http_hdr]; |
| 7355 | struct http_hdr *result = NULL; |
| 7356 | uint32_t hdr_num = 0; |
| 7357 | |
| 7358 | lua_pushnil(L); |
| 7359 | while (lua_next(L, -2) != 0) { |
| 7360 | struct ist name, value; |
| 7361 | const char *n, *v; |
| 7362 | size_t nlen, vlen; |
| 7363 | |
| 7364 | if (!lua_isstring(L, -2) || !lua_istable(L, -1)) { |
| 7365 | /* Skip element if the key is not a string or if the value is not a table */ |
| 7366 | goto next_hdr; |
| 7367 | } |
| 7368 | |
| 7369 | n = lua_tolstring(L, -2, &nlen); |
| 7370 | name = ist2(n, nlen); |
| 7371 | |
| 7372 | /* Loop on header's values */ |
| 7373 | lua_pushnil(L); |
| 7374 | while (lua_next(L, -2)) { |
| 7375 | if (!lua_isstring(L, -1)) { |
| 7376 | /* Skip the value if it is not a string */ |
| 7377 | goto next_value; |
| 7378 | } |
| 7379 | |
| 7380 | v = lua_tolstring(L, -1, &vlen); |
| 7381 | value = ist2(v, vlen); |
| 7382 | name = ist2(n, nlen); |
| 7383 | |
| 7384 | hdrs[hdr_num].n = istdup(name); |
| 7385 | hdrs[hdr_num].v = istdup(value); |
| 7386 | |
| 7387 | hdr_num++; |
| 7388 | |
| 7389 | next_value: |
| 7390 | lua_pop(L, 1); |
| 7391 | } |
| 7392 | |
| 7393 | next_hdr: |
| 7394 | lua_pop(L, 1); |
| 7395 | |
| 7396 | } |
| 7397 | |
| 7398 | if (hdr_num) { |
| 7399 | /* alloc and copy the headers in the httpclient struct */ |
Tim Duesterhus | 16cc16d | 2021-11-06 15:14:45 +0100 | [diff] [blame] | 7400 | result = calloc((hdr_num + 1), sizeof(*result)); |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7401 | if (!result) |
| 7402 | goto skip_headers; |
| 7403 | memcpy(result, hdrs, sizeof(struct http_hdr) * (hdr_num + 1)); |
| 7404 | |
| 7405 | result[hdr_num].n = IST_NULL; |
| 7406 | result[hdr_num].v = IST_NULL; |
| 7407 | } |
| 7408 | |
| 7409 | skip_headers: |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7410 | |
| 7411 | return result; |
| 7412 | } |
| 7413 | |
| 7414 | |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7415 | /* |
| 7416 | * For each yield, checks if there is some data in the httpclient and push them |
| 7417 | * in the lua buffer, once the httpclient finished its job, push the result on |
| 7418 | * the stack |
| 7419 | */ |
| 7420 | __LJMP static int hlua_httpclient_rcv_yield(lua_State *L, int status, lua_KContext ctx) |
| 7421 | { |
| 7422 | struct buffer *tr; |
| 7423 | int res; |
| 7424 | struct hlua *hlua = hlua_gethlua(L); |
| 7425 | struct hlua_httpclient *hlua_hc = hlua_checkhttpclient(L, 1); |
| 7426 | |
| 7427 | |
| 7428 | tr = get_trash_chunk(); |
| 7429 | |
| 7430 | res = httpclient_res_xfer(hlua_hc->hc, tr); |
| 7431 | luaL_addlstring(&hlua_hc->b, b_orig(tr), res); |
| 7432 | |
| 7433 | if (!httpclient_data(hlua_hc->hc) && httpclient_ended(hlua_hc->hc)) { |
| 7434 | |
| 7435 | luaL_pushresult(&hlua_hc->b); |
| 7436 | lua_settable(L, -3); |
| 7437 | |
| 7438 | lua_pushstring(L, "status"); |
| 7439 | lua_pushinteger(L, hlua_hc->hc->res.status); |
| 7440 | lua_settable(L, -3); |
| 7441 | |
| 7442 | |
| 7443 | lua_pushstring(L, "reason"); |
| 7444 | lua_pushlstring(L, hlua_hc->hc->res.reason.ptr, hlua_hc->hc->res.reason.len); |
| 7445 | lua_settable(L, -3); |
| 7446 | |
| 7447 | lua_pushstring(L, "headers"); |
| 7448 | hlua_httpclient_get_headers(L, hlua_hc); |
| 7449 | lua_settable(L, -3); |
| 7450 | |
| 7451 | return 1; |
| 7452 | } |
| 7453 | |
| 7454 | if (httpclient_data(hlua_hc->hc)) |
| 7455 | task_wakeup(hlua->task, TASK_WOKEN_MSG); |
| 7456 | |
| 7457 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_httpclient_rcv_yield, TICK_ETERNITY, 0)); |
| 7458 | |
| 7459 | return 0; |
| 7460 | } |
| 7461 | |
| 7462 | /* |
| 7463 | * Call this when trying to stream a body during a request |
| 7464 | */ |
| 7465 | __LJMP static int hlua_httpclient_snd_yield(lua_State *L, int status, lua_KContext ctx) |
| 7466 | { |
| 7467 | struct hlua *hlua; |
| 7468 | struct hlua_httpclient *hlua_hc = hlua_checkhttpclient(L, 1); |
| 7469 | const char *body_str = NULL; |
| 7470 | int ret; |
| 7471 | int end = 0; |
| 7472 | size_t buf_len; |
| 7473 | size_t to_send = 0; |
| 7474 | |
| 7475 | hlua = hlua_gethlua(L); |
| 7476 | |
| 7477 | if (!hlua || !hlua->task) |
| 7478 | WILL_LJMP(luaL_error(L, "The 'get' function is only allowed in " |
| 7479 | "'frontend', 'backend' or 'task'")); |
| 7480 | |
| 7481 | ret = lua_getfield(L, -1, "body"); |
| 7482 | if (ret != LUA_TSTRING) |
| 7483 | goto rcv; |
| 7484 | |
| 7485 | body_str = lua_tolstring(L, -1, &buf_len); |
| 7486 | lua_pop(L, 1); |
| 7487 | |
Christopher Faulet | fc59129 | 2022-01-12 14:46:03 +0100 | [diff] [blame] | 7488 | to_send = buf_len - hlua_hc->sent; |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7489 | |
| 7490 | if ((hlua_hc->sent + to_send) >= buf_len) |
| 7491 | end = 1; |
| 7492 | |
| 7493 | /* the end flag is always set since we are using the whole remaining size */ |
| 7494 | hlua_hc->sent += httpclient_req_xfer(hlua_hc->hc, ist2(body_str + hlua_hc->sent, to_send), end); |
| 7495 | |
| 7496 | if (buf_len > hlua_hc->sent) { |
| 7497 | /* still need to process the buffer */ |
| 7498 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_httpclient_snd_yield, TICK_ETERNITY, 0)); |
| 7499 | } else { |
| 7500 | goto rcv; |
| 7501 | /* we sent the whole request buffer we can recv */ |
| 7502 | } |
| 7503 | return 0; |
| 7504 | |
| 7505 | rcv: |
| 7506 | |
| 7507 | /* we return a "res" object */ |
| 7508 | lua_newtable(L); |
| 7509 | |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7510 | lua_pushstring(L, "body"); |
William Lallemand | d1187eb | 2021-11-02 10:40:06 +0100 | [diff] [blame] | 7511 | luaL_buffinit(L, &hlua_hc->b); |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7512 | |
William Lallemand | 933fe39 | 2021-11-04 09:45:58 +0100 | [diff] [blame] | 7513 | task_wakeup(hlua->task, TASK_WOKEN_MSG); |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7514 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_httpclient_rcv_yield, TICK_ETERNITY, 0)); |
| 7515 | |
| 7516 | return 1; |
| 7517 | } |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7518 | |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7519 | /* |
William Lallemand | dc2cc90 | 2021-10-26 11:43:26 +0200 | [diff] [blame] | 7520 | * Send an HTTP request and wait for a response |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7521 | */ |
| 7522 | |
William Lallemand | dc2cc90 | 2021-10-26 11:43:26 +0200 | [diff] [blame] | 7523 | __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] | 7524 | { |
| 7525 | struct hlua_httpclient *hlua_hc; |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7526 | struct http_hdr *hdrs = NULL; |
| 7527 | struct http_hdr *hdrs_i = NULL; |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7528 | struct hlua *hlua; |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7529 | const char *url_str = NULL; |
William Lallemand | dec25c3 | 2021-10-25 19:48:37 +0200 | [diff] [blame] | 7530 | const char *body_str = NULL; |
Christopher Faulet | 380ae9c | 2022-10-14 14:57:04 +0200 | [diff] [blame] | 7531 | size_t buf_len = 0; |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7532 | int ret; |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7533 | |
| 7534 | hlua = hlua_gethlua(L); |
| 7535 | |
| 7536 | if (!hlua || !hlua->task) |
| 7537 | WILL_LJMP(luaL_error(L, "The 'get' function is only allowed in " |
| 7538 | "'frontend', 'backend' or 'task'")); |
| 7539 | |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7540 | if (lua_gettop(L) != 2 || lua_type(L, -1) != LUA_TTABLE) |
| 7541 | WILL_LJMP(luaL_error(L, "'get' needs a table as argument")); |
| 7542 | |
William Lallemand | 4f4f2b7 | 2022-02-17 20:00:23 +0100 | [diff] [blame] | 7543 | hlua_hc = hlua_checkhttpclient(L, 1); |
| 7544 | |
William Lallemand | 7177a95 | 2022-03-03 15:33:12 +0100 | [diff] [blame] | 7545 | lua_pushnil(L); /* first key */ |
| 7546 | while (lua_next(L, 2)) { |
| 7547 | if (strcmp(lua_tostring(L, -2), "dst") == 0) { |
| 7548 | if (httpclient_set_dst(hlua_hc->hc, lua_tostring(L, -1)) < 0) |
| 7549 | WILL_LJMP(luaL_error(L, "Can't use the 'dst' argument")); |
William Lallemand | 4f4f2b7 | 2022-02-17 20:00:23 +0100 | [diff] [blame] | 7550 | |
William Lallemand | 7177a95 | 2022-03-03 15:33:12 +0100 | [diff] [blame] | 7551 | } else if (strcmp(lua_tostring(L, -2), "url") == 0) { |
| 7552 | if (lua_type(L, -1) != LUA_TSTRING) |
| 7553 | WILL_LJMP(luaL_error(L, "invalid parameter in 'url', must be a string")); |
| 7554 | url_str = lua_tostring(L, -1); |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7555 | |
William Lallemand | 7177a95 | 2022-03-03 15:33:12 +0100 | [diff] [blame] | 7556 | } else if (strcmp(lua_tostring(L, -2), "timeout") == 0) { |
| 7557 | if (lua_type(L, -1) != LUA_TNUMBER) |
| 7558 | WILL_LJMP(luaL_error(L, "invalid parameter in 'timeout', must be a number")); |
| 7559 | httpclient_set_timeout(hlua_hc->hc, lua_tointeger(L, -1)); |
William Lallemand | b4a4ef6 | 2022-02-23 14:18:16 +0100 | [diff] [blame] | 7560 | |
William Lallemand | 7177a95 | 2022-03-03 15:33:12 +0100 | [diff] [blame] | 7561 | } else if (strcmp(lua_tostring(L, -2), "headers") == 0) { |
| 7562 | if (lua_type(L, -1) != LUA_TTABLE) |
| 7563 | WILL_LJMP(luaL_error(L, "invalid parameter in 'headers', must be a table")); |
| 7564 | hdrs = hlua_httpclient_table_to_hdrs(L); |
William Lallemand | 79416cb | 2021-09-24 14:51:44 +0200 | [diff] [blame] | 7565 | |
William Lallemand | 7177a95 | 2022-03-03 15:33:12 +0100 | [diff] [blame] | 7566 | } else if (strcmp(lua_tostring(L, -2), "body") == 0) { |
| 7567 | if (lua_type(L, -1) != LUA_TSTRING) |
| 7568 | WILL_LJMP(luaL_error(L, "invalid parameter in 'body', must be a string")); |
| 7569 | body_str = lua_tolstring(L, -1, &buf_len); |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7570 | |
William Lallemand | 7177a95 | 2022-03-03 15:33:12 +0100 | [diff] [blame] | 7571 | } else { |
| 7572 | WILL_LJMP(luaL_error(L, "'%s' invalid parameter name", lua_tostring(L, -2))); |
| 7573 | } |
| 7574 | /* removes 'value'; keeps 'key' for next iteration */ |
| 7575 | lua_pop(L, 1); |
| 7576 | } |
William Lallemand | dec25c3 | 2021-10-25 19:48:37 +0200 | [diff] [blame] | 7577 | |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7578 | if (!url_str) { |
| 7579 | WILL_LJMP(luaL_error(L, "'get' need a 'url' argument")); |
| 7580 | return 0; |
| 7581 | } |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7582 | |
William Lallemand | 10a3736 | 2022-03-02 16:18:26 +0100 | [diff] [blame] | 7583 | hlua_hc->sent = 0; |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7584 | |
Aurelien DARRAGON | 0356407 | 2023-02-09 15:26:25 +0100 | [diff] [blame] | 7585 | istfree(&hlua_hc->hc->req.url); |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7586 | hlua_hc->hc->req.url = istdup(ist(url_str)); |
William Lallemand | dc2cc90 | 2021-10-26 11:43:26 +0200 | [diff] [blame] | 7587 | hlua_hc->hc->req.meth = meth; |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7588 | |
| 7589 | /* update the httpclient callbacks */ |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7590 | hlua_hc->hc->ops.res_stline = hlua_httpclient_cb; |
| 7591 | hlua_hc->hc->ops.res_headers = hlua_httpclient_cb; |
| 7592 | hlua_hc->hc->ops.res_payload = hlua_httpclient_cb; |
William Lallemand | 8f170c7 | 2022-03-15 10:52:07 +0100 | [diff] [blame] | 7593 | hlua_hc->hc->ops.res_end = hlua_httpclient_cb; |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7594 | |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7595 | /* a body is available, it will use the request callback */ |
Christopher Faulet | 380ae9c | 2022-10-14 14:57:04 +0200 | [diff] [blame] | 7596 | if (body_str && buf_len) { |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7597 | hlua_hc->hc->ops.req_payload = hlua_httpclient_cb; |
| 7598 | } |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7599 | |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7600 | 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] | 7601 | |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7602 | /* free the temporary headers array */ |
| 7603 | hdrs_i = hdrs; |
| 7604 | while (hdrs_i && isttest(hdrs_i->n)) { |
| 7605 | istfree(&hdrs_i->n); |
| 7606 | istfree(&hdrs_i->v); |
| 7607 | hdrs_i++; |
| 7608 | } |
| 7609 | ha_free(&hdrs); |
| 7610 | |
| 7611 | |
William Lallemand | 6137a9e | 2021-10-26 15:01:53 +0200 | [diff] [blame] | 7612 | if (ret != ERR_NONE) { |
| 7613 | WILL_LJMP(luaL_error(L, "Can't generate the HTTP request")); |
| 7614 | return 0; |
| 7615 | } |
| 7616 | |
William Lallemand | c2d3db4 | 2022-04-26 11:46:13 +0200 | [diff] [blame] | 7617 | if (!httpclient_start(hlua_hc->hc)) |
| 7618 | WILL_LJMP(luaL_error(L, "couldn't start the httpclient")); |
William Lallemand | 6137a9e | 2021-10-26 15:01:53 +0200 | [diff] [blame] | 7619 | |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7620 | 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] | 7621 | |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7622 | return 0; |
| 7623 | } |
| 7624 | |
| 7625 | /* |
William Lallemand | dc2cc90 | 2021-10-26 11:43:26 +0200 | [diff] [blame] | 7626 | * Sends an HTTP HEAD request and wait for a response |
| 7627 | * |
| 7628 | * httpclient:head(url, headers, payload) |
| 7629 | */ |
| 7630 | __LJMP static int hlua_httpclient_head(lua_State *L) |
| 7631 | { |
| 7632 | return hlua_httpclient_send(L, HTTP_METH_HEAD); |
| 7633 | } |
| 7634 | |
| 7635 | /* |
| 7636 | * Send an HTTP GET request and wait for a response |
| 7637 | * |
| 7638 | * httpclient:get(url, headers, payload) |
| 7639 | */ |
| 7640 | __LJMP static int hlua_httpclient_get(lua_State *L) |
| 7641 | { |
| 7642 | return hlua_httpclient_send(L, HTTP_METH_GET); |
| 7643 | |
| 7644 | } |
| 7645 | |
| 7646 | /* |
| 7647 | * Sends an HTTP PUT request and wait for a response |
| 7648 | * |
| 7649 | * httpclient:put(url, headers, payload) |
| 7650 | */ |
| 7651 | __LJMP static int hlua_httpclient_put(lua_State *L) |
| 7652 | { |
| 7653 | return hlua_httpclient_send(L, HTTP_METH_PUT); |
| 7654 | } |
| 7655 | |
| 7656 | /* |
| 7657 | * Send an HTTP POST request and wait for a response |
| 7658 | * |
| 7659 | * httpclient:post(url, headers, payload) |
| 7660 | */ |
| 7661 | __LJMP static int hlua_httpclient_post(lua_State *L) |
| 7662 | { |
| 7663 | return hlua_httpclient_send(L, HTTP_METH_POST); |
| 7664 | } |
| 7665 | |
| 7666 | |
| 7667 | /* |
| 7668 | * Sends an HTTP DELETE request and wait for a response |
| 7669 | * |
| 7670 | * httpclient:delete(url, headers, payload) |
| 7671 | */ |
| 7672 | __LJMP static int hlua_httpclient_delete(lua_State *L) |
| 7673 | { |
| 7674 | return hlua_httpclient_send(L, HTTP_METH_DELETE); |
| 7675 | } |
| 7676 | |
| 7677 | /* |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7678 | * |
| 7679 | * |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7680 | * Class TXN |
| 7681 | * |
| 7682 | * |
| 7683 | */ |
| 7684 | |
| 7685 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7686 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7687 | */ |
| 7688 | __LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud) |
| 7689 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7690 | return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref)); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7691 | } |
| 7692 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 7693 | __LJMP static int hlua_set_var(lua_State *L) |
| 7694 | { |
| 7695 | struct hlua_txn *htxn; |
| 7696 | const char *name; |
| 7697 | size_t len; |
| 7698 | struct sample smp; |
| 7699 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 7700 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 7701 | 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] | 7702 | |
| 7703 | /* It is useles to retrieve the stream, but this function |
| 7704 | * runs only in a stream context. |
| 7705 | */ |
| 7706 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7707 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 7708 | |
| 7709 | /* Converts the third argument in a sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 7710 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 7711 | hlua_lua2smp(L, 3, &smp); |
| 7712 | |
| 7713 | /* Store the sample in a variable. */ |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 7714 | 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] | 7715 | |
| 7716 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 7717 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 7718 | else |
| 7719 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 7720 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 7721 | return 1; |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 7722 | } |
| 7723 | |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 7724 | __LJMP static int hlua_unset_var(lua_State *L) |
| 7725 | { |
| 7726 | struct hlua_txn *htxn; |
| 7727 | const char *name; |
| 7728 | size_t len; |
| 7729 | struct sample smp; |
| 7730 | |
| 7731 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 7732 | |
| 7733 | /* It is useles to retrieve the stream, but this function |
| 7734 | * runs only in a stream context. |
| 7735 | */ |
| 7736 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7737 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 7738 | |
| 7739 | /* Unset the variable. */ |
| 7740 | 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] | 7741 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 7742 | return 1; |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 7743 | } |
| 7744 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 7745 | __LJMP static int hlua_get_var(lua_State *L) |
| 7746 | { |
| 7747 | struct hlua_txn *htxn; |
| 7748 | const char *name; |
| 7749 | size_t len; |
| 7750 | struct sample smp; |
| 7751 | |
| 7752 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 7753 | |
| 7754 | /* It is useles to retrieve the stream, but this function |
| 7755 | * runs only in a stream context. |
| 7756 | */ |
| 7757 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7758 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 7759 | |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 7760 | 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] | 7761 | if (!vars_get_by_name(name, len, &smp, NULL)) { |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 7762 | lua_pushnil(L); |
| 7763 | return 1; |
| 7764 | } |
| 7765 | |
| 7766 | return hlua_smp2lua(L, &smp); |
| 7767 | } |
| 7768 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 7769 | __LJMP static int hlua_set_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7770 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7771 | struct hlua *hlua; |
| 7772 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7773 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 7774 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7775 | /* It is useles to retrieve the stream, but this function |
| 7776 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7777 | */ |
| 7778 | MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 7779 | |
| 7780 | /* Get hlua struct, or NULL if we execute from main lua state */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7781 | hlua = hlua_gethlua(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 7782 | if (!hlua) |
| 7783 | return 0; |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7784 | |
| 7785 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 7786 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7787 | |
| 7788 | /* Get and store new value. */ |
| 7789 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 7790 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 7791 | |
| 7792 | return 0; |
| 7793 | } |
| 7794 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 7795 | __LJMP static int hlua_get_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7796 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7797 | struct hlua *hlua; |
| 7798 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7799 | MAY_LJMP(check_args(L, 1, "get_priv")); |
| 7800 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7801 | /* It is useles to retrieve the stream, but this function |
| 7802 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7803 | */ |
| 7804 | MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 7805 | |
| 7806 | /* Get hlua struct, or NULL if we execute from main lua state */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7807 | hlua = hlua_gethlua(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 7808 | if (!hlua) { |
| 7809 | lua_pushnil(L); |
| 7810 | return 1; |
| 7811 | } |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7812 | |
| 7813 | /* Push configuration index in the stack. */ |
| 7814 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 7815 | |
| 7816 | return 1; |
| 7817 | } |
| 7818 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7819 | /* Create stack entry containing a class TXN. This function |
| 7820 | * return 0 if the stack does not contains free slots, |
| 7821 | * otherwise it returns 1. |
| 7822 | */ |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 7823 | 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] | 7824 | { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 7825 | struct hlua_txn *htxn; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7826 | |
| 7827 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 7828 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7829 | return 0; |
| 7830 | |
| 7831 | /* NOTE: The allocation never fails. The failure |
| 7832 | * throw an error, and the function never returns. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7833 | * if the throw is not available, the process is aborted. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7834 | */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 7835 | /* Create the object: obj[0] = userdata. */ |
| 7836 | lua_newtable(L); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 7837 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 7838 | lua_rawseti(L, -2, 0); |
| 7839 | |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 7840 | htxn->s = s; |
| 7841 | htxn->p = p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 7842 | htxn->dir = dir; |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 7843 | htxn->flags = flags; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7844 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 7845 | /* Create the "f" field that contains a list of fetches. */ |
| 7846 | lua_pushstring(L, "f"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 7847 | if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 7848 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7849 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 7850 | |
| 7851 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 7852 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 7853 | 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] | 7854 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7855 | lua_rawset(L, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 7856 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7857 | /* Create the "c" field that contains a list of converters. */ |
| 7858 | lua_pushstring(L, "c"); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 7859 | if (!hlua_converters_new(L, htxn, 0)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 7860 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7861 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 7862 | |
| 7863 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 7864 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 7865 | if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7866 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7867 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7868 | |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 7869 | /* Create the "req" field that contains the request channel object. */ |
| 7870 | lua_pushstring(L, "req"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 7871 | if (!hlua_channel_new(L, &s->req)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 7872 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7873 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 7874 | |
| 7875 | /* Create the "res" field that contains the response channel object. */ |
| 7876 | lua_pushstring(L, "res"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 7877 | if (!hlua_channel_new(L, &s->res)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 7878 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7879 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 7880 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7881 | /* Creates the HTTP object is the current proxy allows http. */ |
| 7882 | lua_pushstring(L, "http"); |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 7883 | if (IS_HTX_STRM(s)) { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 7884 | if (!hlua_http_new(L, htxn)) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7885 | return 0; |
| 7886 | } |
| 7887 | else |
| 7888 | lua_pushnil(L); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7889 | lua_rawset(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7890 | |
Christopher Faulet | 78c3547 | 2020-02-26 17:14:08 +0100 | [diff] [blame] | 7891 | if ((htxn->flags & HLUA_TXN_CTX_MASK) == HLUA_TXN_FLT_CTX) { |
| 7892 | /* HTTPMessage object are created when a lua TXN is created from |
| 7893 | * a filter context only |
| 7894 | */ |
| 7895 | |
| 7896 | /* Creates the HTTP-Request object is the current proxy allows http. */ |
| 7897 | lua_pushstring(L, "http_req"); |
| 7898 | if (p->mode == PR_MODE_HTTP) { |
| 7899 | if (!hlua_http_msg_new(L, &s->txn->req)) |
| 7900 | return 0; |
| 7901 | } |
| 7902 | else |
| 7903 | lua_pushnil(L); |
| 7904 | lua_rawset(L, -3); |
| 7905 | |
| 7906 | /* Creates the HTTP-Response object is the current proxy allows http. */ |
| 7907 | lua_pushstring(L, "http_res"); |
| 7908 | if (p->mode == PR_MODE_HTTP) { |
| 7909 | if (!hlua_http_msg_new(L, &s->txn->rsp)) |
| 7910 | return 0; |
| 7911 | } |
| 7912 | else |
| 7913 | lua_pushnil(L); |
| 7914 | lua_rawset(L, -3); |
| 7915 | } |
| 7916 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7917 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 7918 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref); |
| 7919 | lua_setmetatable(L, -2); |
| 7920 | |
| 7921 | return 1; |
| 7922 | } |
| 7923 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 7924 | __LJMP static int hlua_txn_deflog(lua_State *L) |
| 7925 | { |
| 7926 | const char *msg; |
| 7927 | struct hlua_txn *htxn; |
| 7928 | |
| 7929 | MAY_LJMP(check_args(L, 2, "deflog")); |
| 7930 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7931 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7932 | |
| 7933 | hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg); |
| 7934 | return 0; |
| 7935 | } |
| 7936 | |
| 7937 | __LJMP static int hlua_txn_log(lua_State *L) |
| 7938 | { |
| 7939 | int level; |
| 7940 | const char *msg; |
| 7941 | struct hlua_txn *htxn; |
| 7942 | |
| 7943 | MAY_LJMP(check_args(L, 3, "log")); |
| 7944 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7945 | level = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 7946 | msg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 7947 | |
| 7948 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 7949 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 7950 | |
| 7951 | hlua_sendlog(htxn->s->be, level, msg); |
| 7952 | return 0; |
| 7953 | } |
| 7954 | |
| 7955 | __LJMP static int hlua_txn_log_debug(lua_State *L) |
| 7956 | { |
| 7957 | const char *msg; |
| 7958 | struct hlua_txn *htxn; |
| 7959 | |
| 7960 | MAY_LJMP(check_args(L, 2, "Debug")); |
| 7961 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7962 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7963 | hlua_sendlog(htxn->s->be, LOG_DEBUG, msg); |
| 7964 | return 0; |
| 7965 | } |
| 7966 | |
| 7967 | __LJMP static int hlua_txn_log_info(lua_State *L) |
| 7968 | { |
| 7969 | const char *msg; |
| 7970 | struct hlua_txn *htxn; |
| 7971 | |
| 7972 | MAY_LJMP(check_args(L, 2, "Info")); |
| 7973 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7974 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7975 | hlua_sendlog(htxn->s->be, LOG_INFO, msg); |
| 7976 | return 0; |
| 7977 | } |
| 7978 | |
| 7979 | __LJMP static int hlua_txn_log_warning(lua_State *L) |
| 7980 | { |
| 7981 | const char *msg; |
| 7982 | struct hlua_txn *htxn; |
| 7983 | |
| 7984 | MAY_LJMP(check_args(L, 2, "Warning")); |
| 7985 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7986 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7987 | hlua_sendlog(htxn->s->be, LOG_WARNING, msg); |
| 7988 | return 0; |
| 7989 | } |
| 7990 | |
| 7991 | __LJMP static int hlua_txn_log_alert(lua_State *L) |
| 7992 | { |
| 7993 | const char *msg; |
| 7994 | struct hlua_txn *htxn; |
| 7995 | |
| 7996 | MAY_LJMP(check_args(L, 2, "Alert")); |
| 7997 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7998 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7999 | hlua_sendlog(htxn->s->be, LOG_ALERT, msg); |
| 8000 | return 0; |
| 8001 | } |
| 8002 | |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 8003 | __LJMP static int hlua_txn_set_loglevel(lua_State *L) |
| 8004 | { |
| 8005 | struct hlua_txn *htxn; |
| 8006 | int ll; |
| 8007 | |
| 8008 | MAY_LJMP(check_args(L, 2, "set_loglevel")); |
| 8009 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 8010 | ll = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 8011 | |
| 8012 | if (ll < 0 || ll > 7) |
| 8013 | WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7")); |
| 8014 | |
| 8015 | htxn->s->logs.level = ll; |
| 8016 | return 0; |
| 8017 | } |
| 8018 | |
| 8019 | __LJMP static int hlua_txn_set_tos(lua_State *L) |
| 8020 | { |
| 8021 | struct hlua_txn *htxn; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 8022 | int tos; |
| 8023 | |
| 8024 | MAY_LJMP(check_args(L, 2, "set_tos")); |
| 8025 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 8026 | tos = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 8027 | |
Willy Tarreau | 1a18b54 | 2018-12-11 16:37:42 +0100 | [diff] [blame] | 8028 | conn_set_tos(objt_conn(htxn->s->sess->origin), tos); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 8029 | return 0; |
| 8030 | } |
| 8031 | |
| 8032 | __LJMP static int hlua_txn_set_mark(lua_State *L) |
| 8033 | { |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 8034 | struct hlua_txn *htxn; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 8035 | int mark; |
| 8036 | |
| 8037 | MAY_LJMP(check_args(L, 2, "set_mark")); |
| 8038 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 8039 | mark = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 8040 | |
Lukas Tribus | 579e3e3 | 2019-08-11 18:03:45 +0200 | [diff] [blame] | 8041 | conn_set_mark(objt_conn(htxn->s->sess->origin), mark); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 8042 | return 0; |
| 8043 | } |
| 8044 | |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 8045 | __LJMP static int hlua_txn_set_priority_class(lua_State *L) |
| 8046 | { |
| 8047 | struct hlua_txn *htxn; |
| 8048 | |
| 8049 | MAY_LJMP(check_args(L, 2, "set_priority_class")); |
| 8050 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 8051 | htxn->s->priority_class = queue_limit_class(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 8052 | return 0; |
| 8053 | } |
| 8054 | |
| 8055 | __LJMP static int hlua_txn_set_priority_offset(lua_State *L) |
| 8056 | { |
| 8057 | struct hlua_txn *htxn; |
| 8058 | |
| 8059 | MAY_LJMP(check_args(L, 2, "set_priority_offset")); |
| 8060 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 8061 | htxn->s->priority_offset = queue_limit_offset(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 8062 | return 0; |
| 8063 | } |
| 8064 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8065 | /* 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] | 8066 | * 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] | 8067 | * message and terminate the transaction. It returns 1 on success and 0 on |
| 8068 | * error. The Reply must be on top of the stack. |
| 8069 | */ |
| 8070 | __LJMP static int hlua_txn_forward_reply(lua_State *L, struct stream *s) |
| 8071 | { |
| 8072 | struct htx *htx; |
| 8073 | struct htx_sl *sl; |
| 8074 | struct h1m h1m; |
| 8075 | const char *status, *reason, *body; |
| 8076 | size_t status_len, reason_len, body_len; |
| 8077 | int ret, code, flags; |
| 8078 | |
| 8079 | code = 200; |
| 8080 | status = "200"; |
| 8081 | status_len = 3; |
| 8082 | ret = lua_getfield(L, -1, "status"); |
| 8083 | if (ret == LUA_TNUMBER) { |
| 8084 | code = lua_tointeger(L, -1); |
| 8085 | status = lua_tolstring(L, -1, &status_len); |
| 8086 | } |
| 8087 | lua_pop(L, 1); |
| 8088 | |
| 8089 | reason = http_get_reason(code); |
| 8090 | reason_len = strlen(reason); |
| 8091 | ret = lua_getfield(L, -1, "reason"); |
| 8092 | if (ret == LUA_TSTRING) |
| 8093 | reason = lua_tolstring(L, -1, &reason_len); |
| 8094 | lua_pop(L, 1); |
| 8095 | |
| 8096 | body = NULL; |
| 8097 | body_len = 0; |
| 8098 | ret = lua_getfield(L, -1, "body"); |
| 8099 | if (ret == LUA_TSTRING) |
| 8100 | body = lua_tolstring(L, -1, &body_len); |
| 8101 | lua_pop(L, 1); |
| 8102 | |
| 8103 | /* Prepare the response before inserting the headers */ |
| 8104 | h1m_init_res(&h1m); |
| 8105 | htx = htx_from_buf(&s->res.buf); |
| 8106 | channel_htx_truncate(&s->res, htx); |
| 8107 | if (s->txn->req.flags & HTTP_MSGF_VER_11) { |
| 8108 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11); |
| 8109 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), |
| 8110 | ist2(status, status_len), ist2(reason, reason_len)); |
| 8111 | } |
| 8112 | else { |
| 8113 | flags = HTX_SL_F_IS_RESP; |
| 8114 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), |
| 8115 | ist2(status, status_len), ist2(reason, reason_len)); |
| 8116 | } |
| 8117 | if (!sl) |
| 8118 | goto fail; |
| 8119 | sl->info.res.status = code; |
| 8120 | |
| 8121 | /* Push in the stack the "headers" entry. */ |
| 8122 | ret = lua_getfield(L, -1, "headers"); |
| 8123 | if (ret != LUA_TTABLE) |
| 8124 | goto skip_headers; |
| 8125 | |
| 8126 | lua_pushnil(L); |
| 8127 | while (lua_next(L, -2) != 0) { |
| 8128 | struct ist name, value; |
| 8129 | const char *n, *v; |
| 8130 | size_t nlen, vlen; |
| 8131 | |
| 8132 | if (!lua_isstring(L, -2) || !lua_istable(L, -1)) { |
| 8133 | /* Skip element if the key is not a string or if the value is not a table */ |
| 8134 | goto next_hdr; |
| 8135 | } |
| 8136 | |
| 8137 | n = lua_tolstring(L, -2, &nlen); |
| 8138 | name = ist2(n, nlen); |
| 8139 | if (isteqi(name, ist("content-length"))) { |
| 8140 | /* Always skip content-length header. It will be added |
| 8141 | * later with the correct len |
| 8142 | */ |
| 8143 | goto next_hdr; |
| 8144 | } |
| 8145 | |
| 8146 | /* Loop on header's values */ |
| 8147 | lua_pushnil(L); |
| 8148 | while (lua_next(L, -2)) { |
| 8149 | if (!lua_isstring(L, -1)) { |
| 8150 | /* Skip the value if it is not a string */ |
| 8151 | goto next_value; |
| 8152 | } |
| 8153 | |
| 8154 | v = lua_tolstring(L, -1, &vlen); |
| 8155 | value = ist2(v, vlen); |
| 8156 | |
| 8157 | if (isteqi(name, ist("transfer-encoding"))) |
| 8158 | h1_parse_xfer_enc_header(&h1m, value); |
| 8159 | if (!htx_add_header(htx, ist2(n, nlen), ist2(v, vlen))) |
| 8160 | goto fail; |
| 8161 | |
| 8162 | next_value: |
| 8163 | lua_pop(L, 1); |
| 8164 | } |
| 8165 | |
| 8166 | next_hdr: |
| 8167 | lua_pop(L, 1); |
| 8168 | } |
| 8169 | skip_headers: |
| 8170 | lua_pop(L, 1); |
| 8171 | |
| 8172 | /* Update h1m flags: CLEN is set if CHNK is not present */ |
| 8173 | if (!(h1m.flags & H1_MF_CHNK)) { |
| 8174 | const char *clen = ultoa(body_len); |
| 8175 | |
| 8176 | h1m.flags |= H1_MF_CLEN; |
| 8177 | if (!htx_add_header(htx, ist("content-length"), ist(clen))) |
| 8178 | goto fail; |
| 8179 | } |
| 8180 | if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK)) |
| 8181 | h1m.flags |= H1_MF_XFER_LEN; |
| 8182 | |
| 8183 | /* Update HTX start-line flags */ |
| 8184 | if (h1m.flags & H1_MF_XFER_ENC) |
| 8185 | flags |= HTX_SL_F_XFER_ENC; |
| 8186 | if (h1m.flags & H1_MF_XFER_LEN) { |
| 8187 | flags |= HTX_SL_F_XFER_LEN; |
| 8188 | if (h1m.flags & H1_MF_CHNK) |
| 8189 | flags |= HTX_SL_F_CHNK; |
| 8190 | else if (h1m.flags & H1_MF_CLEN) |
| 8191 | flags |= HTX_SL_F_CLEN; |
| 8192 | if (h1m.body_len == 0) |
| 8193 | flags |= HTX_SL_F_BODYLESS; |
| 8194 | } |
| 8195 | sl->flags |= flags; |
| 8196 | |
| 8197 | |
| 8198 | if (!htx_add_endof(htx, HTX_BLK_EOH) || |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 8199 | (body_len && !htx_add_data_atonce(htx, ist2(body, body_len)))) |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8200 | goto fail; |
| 8201 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 8202 | htx->flags |= HTX_FL_EOM; |
| 8203 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8204 | /* Now, forward the response and terminate the transaction */ |
| 8205 | s->txn->status = code; |
| 8206 | htx_to_buf(htx, &s->res.buf); |
| 8207 | if (!http_forward_proxy_resp(s, 1)) |
| 8208 | goto fail; |
| 8209 | |
| 8210 | return 1; |
| 8211 | |
| 8212 | fail: |
| 8213 | channel_htx_truncate(&s->res, htx); |
| 8214 | return 0; |
| 8215 | } |
| 8216 | |
| 8217 | /* Terminate a transaction if called from a lua action. For TCP streams, |
| 8218 | * processing is just aborted. Nothing is returned to the client and all |
| 8219 | * arguments are ignored. For HTTP streams, if a reply is passed as argument, it |
| 8220 | * is forwarded to the client before terminating the transaction. On success, |
| 8221 | * the function exits with ACT_RET_DONE code. If an error occurred, it exits |
| 8222 | * with ACT_RET_ERR code. If this function is not called from a lua action, it |
| 8223 | * just exits without any processing. |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 8224 | */ |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 8225 | __LJMP static int hlua_txn_done(lua_State *L) |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 8226 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 8227 | struct hlua_txn *htxn; |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8228 | struct stream *s; |
| 8229 | int finst; |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 8230 | |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 8231 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 8232 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8233 | /* If the flags NOTERM is set, we cannot terminate the session, so we |
| 8234 | * just end the execution of the current lua code. */ |
| 8235 | if (htxn->flags & HLUA_TXN_NOTERM) |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 8236 | WILL_LJMP(hlua_done(L)); |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 8237 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8238 | s = htxn->s; |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 8239 | if (!IS_HTX_STRM(htxn->s)) { |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8240 | struct channel *req = &s->req; |
| 8241 | struct channel *res = &s->res; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 8242 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8243 | channel_auto_read(req); |
| 8244 | channel_abort(req); |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8245 | channel_erase(req); |
| 8246 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8247 | channel_auto_read(res); |
| 8248 | channel_auto_close(res); |
Christopher Faulet | 12762f0 | 2023-04-13 15:40:10 +0200 | [diff] [blame] | 8249 | sc_schedule_abort(s->scb); |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8250 | |
| 8251 | finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_D); |
| 8252 | goto done; |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 8253 | } |
Thierry FOURNIER | 10ec214 | 2015-08-24 17:23:45 +0200 | [diff] [blame] | 8254 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8255 | if (lua_gettop(L) == 1 || !lua_istable(L, 2)) { |
| 8256 | /* No reply or invalid reply */ |
| 8257 | s->txn->status = 0; |
| 8258 | http_reply_and_close(s, 0, NULL); |
| 8259 | } |
| 8260 | else { |
| 8261 | /* Remove extra args to have the reply on top of the stack */ |
| 8262 | if (lua_gettop(L) > 2) |
| 8263 | lua_pop(L, lua_gettop(L) - 2); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 8264 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8265 | if (!hlua_txn_forward_reply(L, s)) { |
| 8266 | if (!(s->flags & SF_ERR_MASK)) |
| 8267 | s->flags |= SF_ERR_PRXCOND; |
| 8268 | lua_pushinteger(L, ACT_RET_ERR); |
| 8269 | WILL_LJMP(hlua_done(L)); |
| 8270 | return 0; /* Never reached */ |
| 8271 | } |
Christopher Faulet | 4d0e263 | 2019-07-16 10:52:40 +0200 | [diff] [blame] | 8272 | } |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 8273 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8274 | finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_H); |
| 8275 | if (htxn->dir == SMP_OPT_DIR_REQ) { |
| 8276 | /* let's log the request time */ |
| 8277 | s->logs.tv_request = now; |
| 8278 | 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] | 8279 | _HA_ATOMIC_INC(&s->sess->fe->fe_counters.intercepted_req); |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8280 | } |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 8281 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8282 | done: |
| 8283 | if (!(s->flags & SF_ERR_MASK)) |
| 8284 | s->flags |= SF_ERR_LOCAL; |
| 8285 | if (!(s->flags & SF_FINST_MASK)) |
| 8286 | s->flags |= finst; |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 8287 | |
Christopher Faulet | e48d1dc | 2021-08-13 14:11:17 +0200 | [diff] [blame] | 8288 | if ((htxn->flags & HLUA_TXN_CTX_MASK) == HLUA_TXN_FLT_CTX) |
| 8289 | lua_pushinteger(L, -1); |
| 8290 | else |
| 8291 | lua_pushinteger(L, ACT_RET_ABRT); |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 8292 | WILL_LJMP(hlua_done(L)); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 8293 | return 0; |
| 8294 | } |
| 8295 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8296 | /* |
| 8297 | * |
| 8298 | * |
| 8299 | * Class REPLY |
| 8300 | * |
| 8301 | * |
| 8302 | */ |
| 8303 | |
| 8304 | /* Pushes the TXN reply onto the top of the stack. If the stask does not have a |
| 8305 | * free slots, the function fails and returns 0; |
| 8306 | */ |
| 8307 | static int hlua_txn_reply_new(lua_State *L) |
| 8308 | { |
| 8309 | struct hlua_txn *htxn; |
| 8310 | const char *reason, *body = NULL; |
| 8311 | int ret, status; |
| 8312 | |
| 8313 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 8314 | if (!IS_HTX_STRM(htxn->s)) { |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8315 | hlua_pusherror(L, "txn object is not an HTTP transaction."); |
| 8316 | WILL_LJMP(lua_error(L)); |
| 8317 | } |
| 8318 | |
| 8319 | /* Default value */ |
| 8320 | status = 200; |
| 8321 | reason = http_get_reason(status); |
| 8322 | |
| 8323 | if (lua_istable(L, 2)) { |
| 8324 | /* load status and reason from the table argument at index 2 */ |
| 8325 | ret = lua_getfield(L, 2, "status"); |
| 8326 | if (ret == LUA_TNIL) |
| 8327 | goto reason; |
| 8328 | else if (ret != LUA_TNUMBER) { |
| 8329 | /* invalid status: ignore the reason */ |
| 8330 | goto body; |
| 8331 | } |
| 8332 | status = lua_tointeger(L, -1); |
| 8333 | |
| 8334 | reason: |
| 8335 | lua_pop(L, 1); /* restore the stack: remove status */ |
| 8336 | ret = lua_getfield(L, 2, "reason"); |
| 8337 | if (ret == LUA_TSTRING) |
| 8338 | reason = lua_tostring(L, -1); |
| 8339 | |
| 8340 | body: |
| 8341 | lua_pop(L, 1); /* restore the stack: remove invalid status or reason */ |
| 8342 | ret = lua_getfield(L, 2, "body"); |
| 8343 | if (ret == LUA_TSTRING) |
| 8344 | body = lua_tostring(L, -1); |
| 8345 | lua_pop(L, 1); /* restore the stack: remove body */ |
| 8346 | } |
| 8347 | |
| 8348 | /* Create the Reply table */ |
| 8349 | lua_newtable(L); |
| 8350 | |
| 8351 | /* Add status element */ |
| 8352 | lua_pushstring(L, "status"); |
| 8353 | lua_pushinteger(L, status); |
| 8354 | lua_settable(L, -3); |
| 8355 | |
| 8356 | /* Add reason element */ |
| 8357 | reason = http_get_reason(status); |
| 8358 | lua_pushstring(L, "reason"); |
| 8359 | lua_pushstring(L, reason); |
| 8360 | lua_settable(L, -3); |
| 8361 | |
| 8362 | /* Add body element, nil if undefined */ |
| 8363 | lua_pushstring(L, "body"); |
| 8364 | if (body) |
| 8365 | lua_pushstring(L, body); |
| 8366 | else |
| 8367 | lua_pushnil(L); |
| 8368 | lua_settable(L, -3); |
| 8369 | |
| 8370 | /* Add headers element */ |
| 8371 | lua_pushstring(L, "headers"); |
| 8372 | lua_newtable(L); |
| 8373 | |
| 8374 | /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */ |
| 8375 | if (lua_istable(L, 2)) { |
| 8376 | /* load headers from the table argument at index 2. If it is a table, copy it. */ |
| 8377 | ret = lua_getfield(L, 2, "headers"); |
| 8378 | if (ret == LUA_TTABLE) { |
| 8379 | /* stack: [ ... <headers:table>, <table> ] */ |
| 8380 | lua_pushnil(L); |
| 8381 | while (lua_next(L, -2) != 0) { |
| 8382 | /* stack: [ ... <headers:table>, <table>, k, v] */ |
| 8383 | if (!lua_isstring(L, -1) && !lua_istable(L, -1)) { |
| 8384 | /* invalid value type, skip it */ |
| 8385 | lua_pop(L, 1); |
| 8386 | continue; |
| 8387 | } |
| 8388 | |
| 8389 | |
| 8390 | /* Duplicate the key and swap it with the value. */ |
| 8391 | lua_pushvalue(L, -2); |
| 8392 | lua_insert(L, -2); |
| 8393 | /* stack: [ ... <headers:table>, <table>, k, k, v ] */ |
| 8394 | |
| 8395 | lua_newtable(L); |
| 8396 | lua_insert(L, -2); |
| 8397 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, v ] */ |
| 8398 | |
| 8399 | if (lua_isstring(L, -1)) { |
| 8400 | /* push the value in the inner table */ |
| 8401 | lua_rawseti(L, -2, 1); |
| 8402 | } |
| 8403 | else { /* table */ |
| 8404 | lua_pushnil(L); |
| 8405 | while (lua_next(L, -2) != 0) { |
| 8406 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2, v2 ] */ |
| 8407 | if (!lua_isstring(L, -1)) { |
| 8408 | /* invalid value type, skip it*/ |
| 8409 | lua_pop(L, 1); |
| 8410 | continue; |
| 8411 | } |
| 8412 | /* push the value in the inner table */ |
| 8413 | lua_rawseti(L, -4, lua_rawlen(L, -4) + 1); |
| 8414 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2 ] */ |
| 8415 | } |
| 8416 | lua_pop(L, 1); |
| 8417 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table> ] */ |
| 8418 | } |
| 8419 | |
| 8420 | /* push (k,v) on the stack in the headers table: |
| 8421 | * stack: [ ... <headers:table>, <table>, k, k, v ] |
| 8422 | */ |
| 8423 | lua_settable(L, -5); |
| 8424 | /* stack: [ ... <headers:table>, <table>, k ] */ |
| 8425 | } |
| 8426 | } |
| 8427 | lua_pop(L, 1); |
| 8428 | } |
| 8429 | /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */ |
| 8430 | lua_settable(L, -3); |
| 8431 | /* stack: [ txn, <Arg:table>, <Reply:table> ] */ |
| 8432 | |
| 8433 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 8434 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_reply_ref); |
| 8435 | lua_setmetatable(L, -2); |
| 8436 | return 1; |
| 8437 | } |
| 8438 | |
| 8439 | /* Set the reply status code, and optionally the reason. If no reason is |
| 8440 | * provided, the default one corresponding to the status code is used. |
| 8441 | */ |
| 8442 | __LJMP static int hlua_txn_reply_set_status(lua_State *L) |
| 8443 | { |
| 8444 | int status = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 8445 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
| 8446 | |
| 8447 | /* First argument (self) must be a table */ |
Aurelien DARRAGON | d83d045 | 2022-10-05 11:46:45 +0200 | [diff] [blame] | 8448 | MAY_LJMP(luaL_checktype(L, 1, LUA_TTABLE)); |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8449 | |
| 8450 | if (status < 100 || status > 599) { |
| 8451 | lua_pushboolean(L, 0); |
| 8452 | return 1; |
| 8453 | } |
| 8454 | if (!reason) |
| 8455 | reason = http_get_reason(status); |
| 8456 | |
| 8457 | lua_pushinteger(L, status); |
| 8458 | lua_setfield(L, 1, "status"); |
| 8459 | |
| 8460 | lua_pushstring(L, reason); |
| 8461 | lua_setfield(L, 1, "reason"); |
| 8462 | |
| 8463 | lua_pushboolean(L, 1); |
| 8464 | return 1; |
| 8465 | } |
| 8466 | |
| 8467 | /* Add a header into the reply object. Each header name is associated to an |
| 8468 | * array of values in the "headers" table. If the header name is not found, a |
| 8469 | * new entry is created. |
| 8470 | */ |
| 8471 | __LJMP static int hlua_txn_reply_add_header(lua_State *L) |
| 8472 | { |
| 8473 | const char *name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 8474 | const char *value = MAY_LJMP(luaL_checkstring(L, 3)); |
| 8475 | int ret; |
| 8476 | |
| 8477 | /* First argument (self) must be a table */ |
Aurelien DARRAGON | d83d045 | 2022-10-05 11:46:45 +0200 | [diff] [blame] | 8478 | MAY_LJMP(luaL_checktype(L, 1, LUA_TTABLE)); |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8479 | |
| 8480 | /* Push in the stack the "headers" entry. */ |
| 8481 | ret = lua_getfield(L, 1, "headers"); |
| 8482 | if (ret != LUA_TTABLE) { |
| 8483 | hlua_pusherror(L, "Reply['headers'] is expected to a an array. %s found", lua_typename(L, ret)); |
| 8484 | WILL_LJMP(lua_error(L)); |
| 8485 | } |
| 8486 | |
| 8487 | /* check if the header is already registered. If not, register it. */ |
| 8488 | ret = lua_getfield(L, -1, name); |
| 8489 | if (ret == LUA_TNIL) { |
| 8490 | /* Entry not found. */ |
| 8491 | lua_pop(L, 1); /* remove the nil. The "headers" table is the top of the stack. */ |
| 8492 | |
| 8493 | /* Insert the new header name in the array in the top of the stack. |
| 8494 | * It left the new array in the top of the stack. |
| 8495 | */ |
| 8496 | lua_newtable(L); |
| 8497 | lua_pushstring(L, name); |
| 8498 | lua_pushvalue(L, -2); |
| 8499 | lua_settable(L, -4); |
| 8500 | } |
| 8501 | else if (ret != LUA_TTABLE) { |
| 8502 | hlua_pusherror(L, "Reply['headers']['%s'] is expected to be an array. %s found", name, lua_typename(L, ret)); |
| 8503 | WILL_LJMP(lua_error(L)); |
| 8504 | } |
| 8505 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 8506 | /* Now the top of thestack is an array of values. We push |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8507 | * the header value as new entry. |
| 8508 | */ |
| 8509 | lua_pushstring(L, value); |
| 8510 | ret = lua_rawlen(L, -2); |
| 8511 | lua_rawseti(L, -2, ret + 1); |
| 8512 | |
| 8513 | lua_pushboolean(L, 1); |
| 8514 | return 1; |
| 8515 | } |
| 8516 | |
| 8517 | /* Remove all occurrences of a given header name. */ |
| 8518 | __LJMP static int hlua_txn_reply_del_header(lua_State *L) |
| 8519 | { |
| 8520 | const char *name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 8521 | int ret; |
| 8522 | |
| 8523 | /* First argument (self) must be a table */ |
Aurelien DARRAGON | d83d045 | 2022-10-05 11:46:45 +0200 | [diff] [blame] | 8524 | MAY_LJMP(luaL_checktype(L, 1, LUA_TTABLE)); |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8525 | |
| 8526 | /* Push in the stack the "headers" entry. */ |
| 8527 | ret = lua_getfield(L, 1, "headers"); |
| 8528 | if (ret != LUA_TTABLE) { |
| 8529 | hlua_pusherror(L, "Reply['headers'] is expected to be an array. %s found", lua_typename(L, ret)); |
| 8530 | WILL_LJMP(lua_error(L)); |
| 8531 | } |
| 8532 | |
| 8533 | lua_pushstring(L, name); |
| 8534 | lua_pushnil(L); |
| 8535 | lua_settable(L, -3); |
| 8536 | |
| 8537 | lua_pushboolean(L, 1); |
| 8538 | return 1; |
| 8539 | } |
| 8540 | |
| 8541 | /* Set the reply's body. Overwrite any existing entry. */ |
| 8542 | __LJMP static int hlua_txn_reply_set_body(lua_State *L) |
| 8543 | { |
| 8544 | const char *payload = MAY_LJMP(luaL_checkstring(L, 2)); |
| 8545 | |
| 8546 | /* First argument (self) must be a table */ |
Aurelien DARRAGON | d83d045 | 2022-10-05 11:46:45 +0200 | [diff] [blame] | 8547 | MAY_LJMP(luaL_checktype(L, 1, LUA_TTABLE)); |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8548 | |
| 8549 | lua_pushstring(L, payload); |
| 8550 | lua_setfield(L, 1, "body"); |
| 8551 | |
| 8552 | lua_pushboolean(L, 1); |
| 8553 | return 1; |
| 8554 | } |
| 8555 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 8556 | __LJMP static int hlua_log(lua_State *L) |
| 8557 | { |
| 8558 | int level; |
| 8559 | const char *msg; |
| 8560 | |
| 8561 | MAY_LJMP(check_args(L, 2, "log")); |
| 8562 | level = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 8563 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 8564 | |
| 8565 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 8566 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 8567 | |
| 8568 | hlua_sendlog(NULL, level, msg); |
| 8569 | return 0; |
| 8570 | } |
| 8571 | |
| 8572 | __LJMP static int hlua_log_debug(lua_State *L) |
| 8573 | { |
| 8574 | const char *msg; |
| 8575 | |
| 8576 | MAY_LJMP(check_args(L, 1, "debug")); |
| 8577 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 8578 | hlua_sendlog(NULL, LOG_DEBUG, msg); |
| 8579 | return 0; |
| 8580 | } |
| 8581 | |
| 8582 | __LJMP static int hlua_log_info(lua_State *L) |
| 8583 | { |
| 8584 | const char *msg; |
| 8585 | |
| 8586 | MAY_LJMP(check_args(L, 1, "info")); |
| 8587 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 8588 | hlua_sendlog(NULL, LOG_INFO, msg); |
| 8589 | return 0; |
| 8590 | } |
| 8591 | |
| 8592 | __LJMP static int hlua_log_warning(lua_State *L) |
| 8593 | { |
| 8594 | const char *msg; |
| 8595 | |
| 8596 | MAY_LJMP(check_args(L, 1, "warning")); |
| 8597 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 8598 | hlua_sendlog(NULL, LOG_WARNING, msg); |
| 8599 | return 0; |
| 8600 | } |
| 8601 | |
| 8602 | __LJMP static int hlua_log_alert(lua_State *L) |
| 8603 | { |
| 8604 | const char *msg; |
| 8605 | |
| 8606 | MAY_LJMP(check_args(L, 1, "alert")); |
| 8607 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 8608 | hlua_sendlog(NULL, LOG_ALERT, msg); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 8609 | return 0; |
| 8610 | } |
| 8611 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 8612 | __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] | 8613 | { |
| 8614 | int wakeup_ms = lua_tointeger(L, -1); |
Willy Tarreau | 12c0270 | 2021-09-30 16:12:31 +0200 | [diff] [blame] | 8615 | if (!tick_is_expired(wakeup_ms, now_ms)) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 8616 | 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] | 8617 | return 0; |
| 8618 | } |
| 8619 | |
| 8620 | __LJMP static int hlua_sleep(lua_State *L) |
| 8621 | { |
| 8622 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 8623 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8624 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 8625 | MAY_LJMP(check_args(L, 1, "sleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8626 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 8627 | delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 8628 | wakeup_ms = tick_add(now_ms, delay); |
| 8629 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8630 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 8631 | 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] | 8632 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8633 | } |
| 8634 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 8635 | __LJMP static int hlua_msleep(lua_State *L) |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8636 | { |
| 8637 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 8638 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8639 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 8640 | MAY_LJMP(check_args(L, 1, "msleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8641 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 8642 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 8643 | wakeup_ms = tick_add(now_ms, delay); |
| 8644 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8645 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 8646 | 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] | 8647 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8648 | } |
| 8649 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 8650 | /* This functionis an LUA binding. it permits to give back |
| 8651 | * the hand at the HAProxy scheduler. It is used when the |
| 8652 | * LUA processing consumes a lot of time. |
| 8653 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 8654 | __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] | 8655 | { |
| 8656 | return 0; |
| 8657 | } |
| 8658 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 8659 | __LJMP static int hlua_yield(lua_State *L) |
| 8660 | { |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 8661 | 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] | 8662 | return 0; |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 8663 | } |
| 8664 | |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 8665 | /* This function change the nice of the currently executed |
| 8666 | * task. It is used set low or high priority at the current |
| 8667 | * task. |
| 8668 | */ |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 8669 | __LJMP static int hlua_set_nice(lua_State *L) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 8670 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8671 | struct hlua *hlua; |
| 8672 | int nice; |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 8673 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8674 | MAY_LJMP(check_args(L, 1, "set_nice")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8675 | nice = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 8676 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 8677 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 8678 | hlua = hlua_gethlua(L); |
| 8679 | |
| 8680 | /* If the task is not set, I'm in a start mode. */ |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 8681 | if (!hlua || !hlua->task) |
| 8682 | return 0; |
| 8683 | |
| 8684 | if (nice < -1024) |
| 8685 | nice = -1024; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8686 | else if (nice > 1024) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 8687 | nice = 1024; |
| 8688 | |
| 8689 | hlua->task->nice = nice; |
| 8690 | return 0; |
| 8691 | } |
| 8692 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 8693 | /* 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] | 8694 | * HAProxy task subsystem when the task is awaked. The LUA runtime can |
| 8695 | * return an E_AGAIN signal, the emmiter of this signal must set a |
| 8696 | * signal to wake the task. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8697 | * |
| 8698 | * Task wrapper are longjmp safe because the only one Lua code |
| 8699 | * executed is the safe hlua_ctx_resume(); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8700 | */ |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 8701 | 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] | 8702 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 8703 | struct hlua *hlua = context; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8704 | enum hlua_exec status; |
| 8705 | |
Willy Tarreau | 6ef52f4 | 2022-06-15 14:19:48 +0200 | [diff] [blame] | 8706 | if (task->tid < 0) |
| 8707 | task->tid = tid; |
| 8708 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8709 | /* If it is the first call to the task, we must initialize the |
| 8710 | * execution timeouts. |
| 8711 | */ |
| 8712 | if (!HLUA_IS_RUNNING(hlua)) |
Aurelien DARRAGON | da9503c | 2022-11-25 09:10:07 +0100 | [diff] [blame^] | 8713 | hlua_timer_init(&hlua->timer, hlua_timeout_task); |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8714 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8715 | /* Execute the Lua code. */ |
| 8716 | status = hlua_ctx_resume(hlua, 1); |
| 8717 | |
| 8718 | switch (status) { |
| 8719 | /* finished or yield */ |
| 8720 | case HLUA_E_OK: |
| 8721 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 8722 | task_destroy(task); |
Tim Duesterhus | cd235c6 | 2018-04-24 13:56:01 +0200 | [diff] [blame] | 8723 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8724 | break; |
| 8725 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 8726 | case HLUA_E_AGAIN: /* co process or timeout wake me later. */ |
Thierry FOURNIER | cb14688 | 2017-12-10 17:10:57 +0100 | [diff] [blame] | 8727 | notification_gc(&hlua->com); |
PiBa-NL | fe971b3 | 2018-05-02 22:27:14 +0200 | [diff] [blame] | 8728 | task->expire = hlua->wake_time; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8729 | break; |
| 8730 | |
| 8731 | /* finished with error. */ |
Aurelien DARRAGON | 7954410 | 2022-11-24 14:27:15 +0100 | [diff] [blame] | 8732 | case HLUA_E_ETMOUT: |
| 8733 | SEND_ERR(NULL, "Lua task: execution timeout.\n"); |
| 8734 | goto err_task_abort; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8735 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8736 | SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1)); |
Aurelien DARRAGON | 7954410 | 2022-11-24 14:27:15 +0100 | [diff] [blame] | 8737 | goto err_task_abort; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8738 | case HLUA_E_ERR: |
| 8739 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8740 | SEND_ERR(NULL, "Lua task: unknown error.\n"); |
Aurelien DARRAGON | 7954410 | 2022-11-24 14:27:15 +0100 | [diff] [blame] | 8741 | err_task_abort: |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8742 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 8743 | task_destroy(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 8744 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8745 | break; |
| 8746 | } |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 8747 | return task; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8748 | } |
| 8749 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8750 | /* This function is an LUA binding that register LUA function to be |
| 8751 | * executed after the HAProxy configuration parsing and before the |
| 8752 | * HAProxy scheduler starts. This function expect only one LUA |
| 8753 | * argument that is a function. This function returns nothing, but |
| 8754 | * throws if an error is encountered. |
| 8755 | */ |
| 8756 | __LJMP static int hlua_register_init(lua_State *L) |
| 8757 | { |
| 8758 | struct hlua_init_function *init; |
| 8759 | int ref; |
| 8760 | |
| 8761 | MAY_LJMP(check_args(L, 1, "register_init")); |
| 8762 | |
Aurelien DARRAGON | 87f5297 | 2023-02-24 09:43:54 +0100 | [diff] [blame] | 8763 | if (hlua_gethlua(L)) { |
| 8764 | /* runtime processing */ |
| 8765 | WILL_LJMP(luaL_error(L, "register_init: not available outside of body context")); |
| 8766 | } |
| 8767 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8768 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 8769 | |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 8770 | init = calloc(1, sizeof(*init)); |
Aurelien DARRAGON | 55afbed | 2023-03-02 18:42:06 +0100 | [diff] [blame] | 8771 | if (!init) { |
| 8772 | hlua_unref(L, ref); |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8773 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Aurelien DARRAGON | 55afbed | 2023-03-02 18:42:06 +0100 | [diff] [blame] | 8774 | } |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8775 | |
| 8776 | init->function_ref = ref; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 8777 | LIST_APPEND(&hlua_init_functions[hlua_state_id], &init->l); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8778 | return 0; |
| 8779 | } |
| 8780 | |
Ilya Shipitsin | 6f86eaa | 2022-11-30 16:22:42 +0500 | [diff] [blame] | 8781 | /* This function is an LUA binding. It permits to register a task |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8782 | * executed in parallel of the main HAroxy activity. The task is |
| 8783 | * created and it is set in the HAProxy scheduler. It can be called |
| 8784 | * from the "init" section, "post init" or during the runtime. |
| 8785 | * |
| 8786 | * Lua prototype: |
| 8787 | * |
Aurelien DARRAGON | b803899 | 2023-03-09 16:48:30 +0100 | [diff] [blame] | 8788 | * <none> core.register_task(<function>[, <arg1>[, <arg2>[, ...[, <arg4>]]]]) |
| 8789 | * |
| 8790 | * <arg1..4> are optional arguments that will be provided to <function> |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8791 | */ |
| 8792 | static int hlua_register_task(lua_State *L) |
| 8793 | { |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 8794 | struct hlua *hlua = NULL; |
| 8795 | struct task *task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8796 | int ref; |
Aurelien DARRAGON | b803899 | 2023-03-09 16:48:30 +0100 | [diff] [blame] | 8797 | int nb_arg; |
| 8798 | int it; |
| 8799 | int arg_ref[4]; /* optional arguments */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 8800 | int state_id; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8801 | |
Aurelien DARRAGON | b803899 | 2023-03-09 16:48:30 +0100 | [diff] [blame] | 8802 | nb_arg = lua_gettop(L); |
| 8803 | if (nb_arg < 1) |
| 8804 | WILL_LJMP(luaL_error(L, "register_task: <func> argument is required")); |
| 8805 | else if (nb_arg > 5) |
| 8806 | 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] | 8807 | |
Aurelien DARRAGON | b803899 | 2023-03-09 16:48:30 +0100 | [diff] [blame] | 8808 | /* first arg: function ref */ |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8809 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 8810 | |
Aurelien DARRAGON | b803899 | 2023-03-09 16:48:30 +0100 | [diff] [blame] | 8811 | /* extract optional args (if any) */ |
| 8812 | it = 0; |
| 8813 | while (--nb_arg) { |
| 8814 | lua_pushvalue(L, 2 + it); |
| 8815 | arg_ref[it] = hlua_ref(L); /* get arg reference */ |
| 8816 | it += 1; |
| 8817 | } |
| 8818 | nb_arg = it; |
| 8819 | |
Thierry Fournier | 75fc029 | 2020-11-28 13:18:56 +0100 | [diff] [blame] | 8820 | /* Get the reference state. If the reference is NULL, L is the master |
| 8821 | * state, otherwise hlua->T is. |
| 8822 | */ |
| 8823 | hlua = hlua_gethlua(L); |
| 8824 | if (hlua) |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 8825 | /* we are in runtime processing */ |
| 8826 | state_id = hlua->state_id; |
Thierry Fournier | 75fc029 | 2020-11-28 13:18:56 +0100 | [diff] [blame] | 8827 | else |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 8828 | /* we are in initialization mode */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8829 | state_id = hlua_state_id; |
Thierry Fournier | 75fc029 | 2020-11-28 13:18:56 +0100 | [diff] [blame] | 8830 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 8831 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8832 | if (!hlua) |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 8833 | goto alloc_error; |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 8834 | HLUA_INIT(hlua); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8835 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8836 | /* We are in the common lua state, execute the task anywhere, |
| 8837 | * otherwise, inherit the current thread identifier |
| 8838 | */ |
| 8839 | if (state_id == 0) |
Willy Tarreau | beeabf5 | 2021-10-01 18:23:30 +0200 | [diff] [blame] | 8840 | task = task_new_anywhere(); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8841 | else |
Willy Tarreau | beeabf5 | 2021-10-01 18:23:30 +0200 | [diff] [blame] | 8842 | task = task_new_here(); |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 8843 | if (!task) |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 8844 | goto alloc_error; |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 8845 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8846 | task->context = hlua; |
| 8847 | task->process = hlua_process_task; |
| 8848 | |
Aurelien DARRAGON | 6b0b9bd | 2023-03-21 14:02:16 +0100 | [diff] [blame] | 8849 | if (!hlua_ctx_init(hlua, state_id, task)) |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 8850 | goto alloc_error; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8851 | |
Aurelien DARRAGON | b803899 | 2023-03-09 16:48:30 +0100 | [diff] [blame] | 8852 | /* Ensure there is enough space on the stack for the function |
| 8853 | * plus optional arguments |
| 8854 | */ |
| 8855 | if (!lua_checkstack(hlua->T, (1 + nb_arg))) |
| 8856 | goto alloc_error; |
| 8857 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8858 | /* Restore the function in the stack. */ |
Aurelien DARRAGON | 4fdf8b5 | 2023-03-20 17:22:37 +0100 | [diff] [blame] | 8859 | hlua_pushref(hlua->T, ref); |
Aurelien DARRAGON | be58d66 | 2023-03-13 14:09:21 +0100 | [diff] [blame] | 8860 | /* function ref not needed anymore since it was pushed to the substack */ |
| 8861 | hlua_unref(L, ref); |
| 8862 | |
Aurelien DARRAGON | b803899 | 2023-03-09 16:48:30 +0100 | [diff] [blame] | 8863 | hlua->nargs = nb_arg; |
| 8864 | |
| 8865 | /* push optional arguments to the function */ |
| 8866 | for (it = 0; it < nb_arg; it++) { |
| 8867 | /* push arg to the stack */ |
| 8868 | hlua_pushref(hlua->T, arg_ref[it]); |
| 8869 | /* arg ref not needed anymore since it was pushed to the substack */ |
| 8870 | hlua_unref(L, arg_ref[it]); |
| 8871 | } |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8872 | |
| 8873 | /* Schedule task. */ |
Willy Tarreau | e3957f8 | 2021-09-30 16:17:37 +0200 | [diff] [blame] | 8874 | task_wakeup(task, TASK_WOKEN_INIT); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8875 | |
| 8876 | return 0; |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 8877 | |
| 8878 | alloc_error: |
| 8879 | task_destroy(task); |
Aurelien DARRAGON | 55afbed | 2023-03-02 18:42:06 +0100 | [diff] [blame] | 8880 | hlua_unref(L, ref); |
Aurelien DARRAGON | b803899 | 2023-03-09 16:48:30 +0100 | [diff] [blame] | 8881 | for (it = 0; it < nb_arg; it++) { |
| 8882 | hlua_unref(L, arg_ref[it]); |
| 8883 | } |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 8884 | hlua_ctx_destroy(hlua); |
| 8885 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 8886 | return 0; /* Never reached */ |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8887 | } |
| 8888 | |
Aurelien DARRAGON | c84899c | 2023-02-20 18:18:59 +0100 | [diff] [blame] | 8889 | /* called from unsafe location */ |
| 8890 | static void hlua_event_subscription_destroy(struct hlua_event_sub *hlua_sub) |
| 8891 | { |
| 8892 | /* hlua cleanup */ |
| 8893 | |
| 8894 | hlua_lock(hlua_sub->hlua); |
| 8895 | /* registry is shared between coroutines */ |
| 8896 | hlua_unref(hlua_sub->hlua->T, hlua_sub->fcn_ref); |
| 8897 | hlua_unlock(hlua_sub->hlua); |
| 8898 | |
| 8899 | hlua_ctx_destroy(hlua_sub->hlua); |
| 8900 | |
| 8901 | /* free */ |
| 8902 | pool_free(pool_head_hlua_event_sub, hlua_sub); |
| 8903 | } |
| 8904 | |
| 8905 | /* single event handler: hlua ctx is shared between multiple events handlers |
| 8906 | * issued from the same subscription. Thus, it is not destroyed when the event |
| 8907 | * is processed: it is destroyed when no more events are expected for the |
| 8908 | * subscription (ie: when the subscription ends). |
| 8909 | * |
| 8910 | * Moreover, events are processed sequentially within the subscription: |
| 8911 | * one event must be fully processed before another one may be processed. |
| 8912 | * This ensures proper consistency for lua event handling from an ordering |
| 8913 | * point of view. This is especially useful with server events for example |
| 8914 | * where ADD/DEL/UP/DOWN events ordering really matters to trigger specific |
| 8915 | * actions from lua (e.g.: sending emails or making API calls). |
| 8916 | * |
| 8917 | * Due to this design, each lua event handler is pleased to process the event |
| 8918 | * as fast as possible to prevent the event queue from growing up. |
| 8919 | * Strictly speaking, there is no runtime limit for the callback function |
| 8920 | * (timeout set to default task timeout), but if the event queue goes past |
| 8921 | * the limit of unconsumed events an error will be reported and the |
| 8922 | * susbscription will pause itself for as long as it takes for the handler to |
| 8923 | * catch up (events will be lost as a result). |
| 8924 | * If the event handler does not need the sequential ordering and wants to |
| 8925 | * process multiple events at a time, it may spawn a new side-task using |
| 8926 | * 'core.register_task' to delegate the event handling and make parallel event |
| 8927 | * processing within the same subscription set. |
| 8928 | */ |
| 8929 | static void hlua_event_handler(struct hlua *hlua) |
| 8930 | { |
| 8931 | enum hlua_exec status; |
| 8932 | |
| 8933 | /* If it is the first call to the task, we must initialize the |
| 8934 | * execution timeouts. |
| 8935 | */ |
Aurelien DARRAGON | da9503c | 2022-11-25 09:10:07 +0100 | [diff] [blame^] | 8936 | if (!HLUA_IS_RUNNING(hlua)) |
| 8937 | hlua_timer_init(&hlua->timer, hlua_timeout_task); |
Aurelien DARRAGON | c84899c | 2023-02-20 18:18:59 +0100 | [diff] [blame] | 8938 | |
| 8939 | /* make sure to reset the task expiry before each hlua_ctx_resume() |
| 8940 | * since the task is re-used for multiple cb function calls |
| 8941 | * We couldn't risk to have t->expire pointing to a past date because |
| 8942 | * it was set during last function invocation but was never reset since |
| 8943 | * (ie: E_AGAIN) |
| 8944 | */ |
| 8945 | hlua->task->expire = TICK_ETERNITY; |
| 8946 | |
| 8947 | /* Execute the Lua code. */ |
| 8948 | status = hlua_ctx_resume(hlua, 1); |
| 8949 | |
| 8950 | switch (status) { |
| 8951 | /* finished or yield */ |
| 8952 | case HLUA_E_OK: |
| 8953 | break; |
| 8954 | |
| 8955 | case HLUA_E_AGAIN: /* co process or timeout wake me later. */ |
| 8956 | notification_gc(&hlua->com); |
| 8957 | hlua->task->expire = hlua->wake_time; |
| 8958 | break; |
| 8959 | |
| 8960 | /* finished with error. */ |
| 8961 | case HLUA_E_ETMOUT: |
| 8962 | SEND_ERR(NULL, "Lua event_hdl: execution timeout.\n"); |
| 8963 | break; |
| 8964 | |
| 8965 | case HLUA_E_ERRMSG: |
| 8966 | SEND_ERR(NULL, "Lua event_hdl: %s.\n", lua_tostring(hlua->T, -1)); |
| 8967 | break; |
| 8968 | |
| 8969 | case HLUA_E_ERR: |
| 8970 | default: |
| 8971 | SEND_ERR(NULL, "Lua event_hdl: unknown error.\n"); |
| 8972 | break; |
| 8973 | } |
| 8974 | } |
| 8975 | |
| 8976 | __LJMP static int hlua_event_hdl_cb_data_push_args(struct hlua_event_sub *hlua_sub, |
| 8977 | struct event_hdl_sub_type event, void *data) |
| 8978 | { |
| 8979 | struct hlua *hlua = hlua_sub->hlua; |
| 8980 | |
| 8981 | hlua->nargs = 1; |
| 8982 | lua_pushstring(hlua->T, event_hdl_sub_type_to_string(event)); |
| 8983 | if (event_hdl_sub_family_equal(EVENT_HDL_SUB_SERVER, event)) { |
| 8984 | struct event_hdl_cb_data_server *e_server = data; |
| 8985 | struct proxy *px; |
| 8986 | struct server *server; |
| 8987 | |
| 8988 | hlua->nargs += 1; |
| 8989 | lua_newtable(hlua->T); |
| 8990 | /* Add server name */ |
| 8991 | lua_pushstring(hlua->T, "name"); |
| 8992 | lua_pushstring(hlua->T, e_server->safe.name); |
| 8993 | lua_settable(hlua->T, -3); |
| 8994 | /* Add server puid */ |
| 8995 | lua_pushstring(hlua->T, "puid"); |
| 8996 | lua_pushinteger(hlua->T, e_server->safe.puid); |
| 8997 | lua_settable(hlua->T, -3); |
| 8998 | /* Add server rid */ |
| 8999 | lua_pushstring(hlua->T, "rid"); |
| 9000 | lua_pushinteger(hlua->T, e_server->safe.rid); |
| 9001 | lua_settable(hlua->T, -3); |
| 9002 | /* Add server proxy name */ |
| 9003 | lua_pushstring(hlua->T, "proxy_name"); |
| 9004 | lua_pushstring(hlua->T, e_server->safe.proxy_name); |
| 9005 | lua_settable(hlua->T, -3); |
| 9006 | |
| 9007 | /* attempt to provide reference server object |
| 9008 | * (if it wasn't removed yet, SERVER_DEL will never succeed here) |
| 9009 | */ |
| 9010 | px = proxy_find_by_name(e_server->safe.proxy_name, PR_CAP_BE, 0); |
| 9011 | BUG_ON(!px); |
| 9012 | server = findserver_unique_id(px, e_server->safe.puid, e_server->safe.rid); |
| 9013 | if (server) { |
| 9014 | lua_pushstring(hlua->T, "reference"); |
| 9015 | hlua_fcn_new_server(hlua->T, server); |
| 9016 | lua_settable(hlua->T, -3); |
| 9017 | } |
| 9018 | } |
| 9019 | /* sub mgmt */ |
| 9020 | hlua->nargs += 1; |
| 9021 | hlua_fcn_new_event_sub(hlua->T, hlua_sub->sub); |
| 9022 | |
| 9023 | return 1; |
| 9024 | } |
| 9025 | |
| 9026 | /* events runner: if there's an ongoing hlua event handling process, finish it |
| 9027 | * then, check if there are new events waiting to be processed |
| 9028 | * (events are processed sequentially) |
| 9029 | * |
| 9030 | * We have a safety measure to warn/guard if the event queue is growing up |
| 9031 | * too much due to many events being generated and lua handler is unable to |
| 9032 | * keep up the pace (e.g.: when the event queue grows past 100 unconsumed events). |
| 9033 | * TODO: make it tunable |
| 9034 | */ |
| 9035 | static struct task *hlua_event_runner(struct task *task, void *context, unsigned int state) |
| 9036 | { |
| 9037 | struct hlua_event_sub *hlua_sub = context; |
| 9038 | struct event_hdl_async_event *event; |
| 9039 | const char *error = NULL; |
| 9040 | |
| 9041 | if (!hlua_sub->paused && event_hdl_async_equeue_size(&hlua_sub->equeue) > 100) { |
| 9042 | const char *trace; |
| 9043 | |
| 9044 | /* We reached the limit of pending events in the queue: we should |
| 9045 | * warn the user, and temporarily pause the subscription to give a chance |
| 9046 | * to the handler to catch up? (it also prevents ressource shortage since |
| 9047 | * the queue could grow indefinitely otherwise) |
| 9048 | * TODO: find a way to inform the handler that it missed some events |
| 9049 | * (example: stats within the subscription in event_hdl api exposed via lua api?) |
| 9050 | * |
| 9051 | * Nonetheless, reaching this limit means that the handler is not fast enough |
| 9052 | * and/or that it subscribed to events that happen too frequently and did not |
| 9053 | * expect it. This could come from an inadequate design in the user's script. |
| 9054 | */ |
| 9055 | event_hdl_pause(hlua_sub->sub); |
| 9056 | hlua_sub->paused = 1; |
| 9057 | |
| 9058 | /* The following Lua calls can fail. */ |
| 9059 | if (!SET_SAFE_LJMP(hlua_sub->hlua)) |
| 9060 | trace = NULL; |
| 9061 | trace = hlua_traceback(hlua_sub->hlua->T, ", "); |
| 9062 | /* At this point the execution is safe. */ |
| 9063 | RESET_SAFE_LJMP(hlua_sub->hlua); |
| 9064 | |
| 9065 | 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 : "")); |
| 9066 | } |
| 9067 | |
| 9068 | if (HLUA_IS_RUNNING(hlua_sub->hlua)) { |
| 9069 | /* ongoing hlua event handler, resume it */ |
| 9070 | hlua_event_handler(hlua_sub->hlua); |
| 9071 | } else if ((event = event_hdl_async_equeue_pop(&hlua_sub->equeue))) { /* check for new events */ |
| 9072 | if (event_hdl_sub_type_equal(event->type, EVENT_HDL_SUB_END)) { |
| 9073 | /* ending event: no more events to come */ |
| 9074 | event_hdl_async_free_event(event); |
| 9075 | task_destroy(task); |
| 9076 | hlua_event_subscription_destroy(hlua_sub); |
| 9077 | return NULL; |
| 9078 | } |
| 9079 | /* new event: start processing it */ |
| 9080 | |
| 9081 | /* The following Lua calls can fail. */ |
| 9082 | if (!SET_SAFE_LJMP(hlua_sub->hlua)) { |
| 9083 | if (lua_type(hlua_sub->hlua->T, -1) == LUA_TSTRING) |
| 9084 | error = lua_tostring(hlua_sub->hlua->T, -1); |
| 9085 | else |
| 9086 | error = "critical error"; |
| 9087 | ha_alert("Lua event_hdl: %s.\n", error); |
| 9088 | goto skip_event; |
| 9089 | } |
| 9090 | |
| 9091 | /* Check stack available size. */ |
| 9092 | if (!lua_checkstack(hlua_sub->hlua->T, 5)) { |
| 9093 | ha_alert("Lua event_hdl: full stack.\n"); |
| 9094 | RESET_SAFE_LJMP(hlua_sub->hlua); |
| 9095 | goto skip_event; |
| 9096 | } |
| 9097 | |
| 9098 | /* Restore the function in the stack. */ |
| 9099 | hlua_pushref(hlua_sub->hlua->T, hlua_sub->fcn_ref); |
| 9100 | |
| 9101 | /* push args */ |
| 9102 | hlua_sub->hlua->nargs = 0; |
| 9103 | if (!hlua_event_hdl_cb_data_push_args(hlua_sub, event->type, event->data)) { |
| 9104 | RESET_SAFE_LJMP(hlua_sub->hlua); |
| 9105 | goto skip_event; |
| 9106 | } |
| 9107 | |
| 9108 | /* At this point the execution is safe. */ |
| 9109 | RESET_SAFE_LJMP(hlua_sub->hlua); |
| 9110 | |
| 9111 | /* At this point the event was successfully translated into hlua ctx, |
| 9112 | * or hlua error occured, so we can safely discard it |
| 9113 | */ |
| 9114 | event_hdl_async_free_event(event); |
| 9115 | event = NULL; |
| 9116 | |
| 9117 | hlua_event_handler(hlua_sub->hlua); |
| 9118 | skip_event: |
| 9119 | if (event) |
| 9120 | event_hdl_async_free_event(event); |
| 9121 | |
| 9122 | } |
| 9123 | |
| 9124 | if (!HLUA_IS_RUNNING(hlua_sub->hlua)) { |
| 9125 | /* we just finished the processing of one event.. |
| 9126 | * check for new events before becoming idle |
| 9127 | */ |
| 9128 | if (!event_hdl_async_equeue_isempty(&hlua_sub->equeue)) { |
| 9129 | /* more events to process, make sure the task |
| 9130 | * will be resumed ASAP to process pending events |
| 9131 | */ |
| 9132 | task_wakeup(task, TASK_WOKEN_OTHER); |
| 9133 | } |
| 9134 | else if (hlua_sub->paused) { |
| 9135 | /* empty queue, the handler catched up: resume the subscription */ |
| 9136 | event_hdl_resume(hlua_sub->sub); |
| 9137 | hlua_sub->paused = 0; |
| 9138 | } |
| 9139 | } |
| 9140 | |
| 9141 | return task; |
| 9142 | } |
| 9143 | |
| 9144 | /* Must be called directly under lua protected/safe environment |
| 9145 | * (not from external callback) |
| 9146 | * <fcn_ref> should NOT be dropped after the function successfully returns: |
| 9147 | * it will be done automatically in hlua_event_subscription_destroy() when the |
| 9148 | * subscription ends. |
| 9149 | * |
| 9150 | * Returns the new subscription on success and NULL on failure (memory error) |
| 9151 | */ |
| 9152 | static struct event_hdl_sub *hlua_event_subscribe(event_hdl_sub_list *list, struct event_hdl_sub_type e_type, |
| 9153 | int state_id, int fcn_ref) |
| 9154 | { |
| 9155 | struct hlua_event_sub *hlua_sub; |
| 9156 | struct task *task = NULL; |
| 9157 | |
| 9158 | hlua_sub = pool_alloc(pool_head_hlua_event_sub); |
| 9159 | if (!hlua_sub) |
| 9160 | goto mem_error; |
| 9161 | hlua_sub->task = NULL; |
| 9162 | hlua_sub->hlua = NULL; |
| 9163 | hlua_sub->paused = 0; |
| 9164 | if ((task = task_new_here()) == NULL) { |
| 9165 | ha_alert("out of memory while allocating hlua event task"); |
| 9166 | goto mem_error; |
| 9167 | } |
| 9168 | task->process = hlua_event_runner; |
| 9169 | task->context = hlua_sub; |
| 9170 | event_hdl_async_equeue_init(&hlua_sub->equeue); |
| 9171 | hlua_sub->task = task; |
| 9172 | hlua_sub->fcn_ref = fcn_ref; |
| 9173 | hlua_sub->state_id = state_id; |
| 9174 | hlua_sub->hlua = pool_alloc(pool_head_hlua); |
| 9175 | if (!hlua_sub->hlua) |
| 9176 | goto mem_error; |
| 9177 | HLUA_INIT(hlua_sub->hlua); |
| 9178 | if (!hlua_ctx_init(hlua_sub->hlua, hlua_sub->state_id, task)) |
| 9179 | goto mem_error; |
| 9180 | |
| 9181 | hlua_sub->sub = event_hdl_subscribe_ptr(list, e_type, |
| 9182 | EVENT_HDL_ASYNC_TASK(&hlua_sub->equeue, |
| 9183 | task, |
| 9184 | hlua_sub, |
| 9185 | NULL)); |
| 9186 | if (!hlua_sub->sub) |
| 9187 | goto mem_error; |
| 9188 | |
| 9189 | return hlua_sub->sub; /* returns pointer to event_hdl_sub struct */ |
| 9190 | |
| 9191 | mem_error: |
| 9192 | if (hlua_sub) { |
| 9193 | if (hlua_sub->task) |
| 9194 | task_destroy(hlua_sub->task); |
| 9195 | if (hlua_sub->hlua) |
| 9196 | hlua_ctx_destroy(hlua_sub->hlua); |
| 9197 | pool_free(pool_head_hlua_event_sub, hlua_sub); |
| 9198 | } |
| 9199 | |
| 9200 | return NULL; |
| 9201 | } |
| 9202 | |
| 9203 | /* looks for an array of strings referring to a composition of event_hdl subscription |
| 9204 | * types at <index> in <L> stack |
| 9205 | */ |
| 9206 | __LJMP static struct event_hdl_sub_type hlua_check_event_sub_types(lua_State *L, int index) |
| 9207 | { |
| 9208 | struct event_hdl_sub_type subscriptions; |
| 9209 | const char *msg; |
| 9210 | |
| 9211 | if (lua_type(L, index) != LUA_TTABLE) { |
| 9212 | msg = lua_pushfstring(L, "table of strings expected, got %s", luaL_typename(L, index)); |
| 9213 | luaL_argerror(L, index, msg); |
| 9214 | } |
| 9215 | |
| 9216 | subscriptions = EVENT_HDL_SUB_NONE; |
| 9217 | |
| 9218 | /* browse the argument as an array. */ |
| 9219 | lua_pushnil(L); |
| 9220 | while (lua_next(L, index) != 0) { |
| 9221 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 9222 | msg = lua_pushfstring(L, "table of strings expected, got %s", luaL_typename(L, index)); |
| 9223 | luaL_argerror(L, index, msg); |
| 9224 | } |
| 9225 | |
| 9226 | if (event_hdl_sub_type_equal(EVENT_HDL_SUB_NONE, event_hdl_string_to_sub_type(lua_tostring(L, -1)))) { |
| 9227 | msg = lua_pushfstring(L, "'%s' event type is unknown", lua_tostring(L, -1)); |
| 9228 | luaL_argerror(L, index, msg); |
| 9229 | } |
| 9230 | |
| 9231 | /* perform subscriptions |= current sub */ |
| 9232 | subscriptions = event_hdl_sub_type_add(subscriptions, event_hdl_string_to_sub_type(lua_tostring(L, -1))); |
| 9233 | |
| 9234 | /* pop the current value. */ |
| 9235 | lua_pop(L, 1); |
| 9236 | } |
| 9237 | |
| 9238 | return subscriptions; |
| 9239 | } |
| 9240 | |
| 9241 | /* Wrapper for hlua_fcn_new_event_sub(): catch errors raised by |
| 9242 | * the function to prevent LJMP |
| 9243 | * |
| 9244 | * If no error occured, the function returns 1, else it returns 0 and |
| 9245 | * the error message is pushed at the top of the stack |
| 9246 | */ |
| 9247 | __LJMP static int _hlua_new_event_sub_safe(lua_State *L) |
| 9248 | { |
| 9249 | struct event_hdl_sub *sub = lua_touserdata(L, 1); |
| 9250 | |
| 9251 | /* this function may raise errors */ |
| 9252 | return MAY_LJMP(hlua_fcn_new_event_sub(L, sub)); |
| 9253 | } |
| 9254 | static int hlua_new_event_sub_safe(lua_State *L, struct event_hdl_sub *sub) |
| 9255 | { |
| 9256 | if (!lua_checkstack(L, 2)) |
| 9257 | return 0; |
| 9258 | lua_pushcfunction(L, _hlua_new_event_sub_safe); |
| 9259 | lua_pushlightuserdata(L, sub); |
| 9260 | switch (lua_pcall(L, 1, 1, 0)) { |
| 9261 | case LUA_OK: |
| 9262 | return 1; |
| 9263 | default: |
| 9264 | /* error was catched */ |
| 9265 | return 0; |
| 9266 | } |
| 9267 | } |
| 9268 | |
| 9269 | /* This function is a LUA helper used for registering lua event callbacks. |
| 9270 | * It expects an event subscription array and the function to be executed |
| 9271 | * when subscribed events occur (stack arguments). |
| 9272 | * It can be called from the "init" section, "post init" or during the runtime. |
| 9273 | * |
| 9274 | * <sub_list> is the subscription list where the subscription will be attempted |
| 9275 | * |
| 9276 | * Pushes the newly allocated subscription on the stack on success |
| 9277 | */ |
| 9278 | __LJMP int hlua_event_sub(lua_State *L, event_hdl_sub_list *sub_list) |
| 9279 | { |
| 9280 | struct hlua *hlua; |
| 9281 | struct event_hdl_sub *sub; |
| 9282 | struct event_hdl_sub_type subscriptions; |
| 9283 | int fcn_ref; |
| 9284 | int state_id; |
| 9285 | |
| 9286 | MAY_LJMP(check_args(L, 2, "event_sub")); |
| 9287 | |
| 9288 | /* Get the reference state */ |
| 9289 | hlua = hlua_gethlua(L); |
| 9290 | if (hlua) |
| 9291 | /* we are in runtime processing, any thread may subscribe to events: |
| 9292 | * subscription events will be handled by the thread who performed |
| 9293 | * the registration. |
| 9294 | */ |
| 9295 | state_id = hlua->state_id; |
| 9296 | else { |
| 9297 | /* we are in initialization mode, only thread 0 (actual calling thread) |
| 9298 | * may subscribe to events to prevent the same handler (from different lua |
| 9299 | * stacks) from being registered multiple times |
| 9300 | * |
| 9301 | * hlua_state_id == 0: monostack (lua-load) |
| 9302 | * hlua_state_id > 0: hlua_state_id=tid+1, multi-stack (lua-load-per-thread) |
| 9303 | * (thus if hlua_state_id > 1, it means we are not in primary thread ctx) |
| 9304 | */ |
| 9305 | if (hlua_state_id > 1) |
| 9306 | return 0; /* skip registration */ |
| 9307 | state_id = hlua_state_id; |
| 9308 | } |
| 9309 | |
| 9310 | /* First argument : event subscriptions. */ |
| 9311 | subscriptions = MAY_LJMP(hlua_check_event_sub_types(L, 1)); |
| 9312 | |
| 9313 | if (event_hdl_sub_type_equal(subscriptions, EVENT_HDL_SUB_NONE)) { |
| 9314 | WILL_LJMP(luaL_error(L, "event_sub: no valid event types were provided")); |
| 9315 | return 0; /* Never reached */ |
| 9316 | } |
| 9317 | |
| 9318 | /* Second argument : lua function. */ |
| 9319 | fcn_ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 9320 | |
| 9321 | /* try to subscribe */ |
| 9322 | sub = hlua_event_subscribe(sub_list, subscriptions, state_id, fcn_ref); |
| 9323 | if (!sub) { |
| 9324 | hlua_unref(L, fcn_ref); |
| 9325 | WILL_LJMP(luaL_error(L, "event_sub: lua out of memory error")); |
| 9326 | return 0; /* Never reached */ |
| 9327 | } |
| 9328 | |
| 9329 | /* push the subscription to the stack |
| 9330 | * |
| 9331 | * Here we use the safe function so that lua errors will be |
| 9332 | * handled explicitly to prevent 'sub' from being lost |
| 9333 | */ |
| 9334 | if (!hlua_new_event_sub_safe(L, sub)) { |
| 9335 | /* Some events could already be pending in the handler's queue. |
| 9336 | * However it is wiser to cancel the subscription since we are unable to |
| 9337 | * provide a valid reference to it. |
| 9338 | * Pending events will be delivered (unless lua keeps raising errors). |
| 9339 | */ |
| 9340 | event_hdl_unsubscribe(sub); /* cancel the subscription */ |
| 9341 | WILL_LJMP(luaL_error(L, "event_sub: cannot push the subscription (%s)", lua_tostring(L, -1))); |
| 9342 | return 0; /* Never reached */ |
| 9343 | } |
| 9344 | event_hdl_drop(sub); /* sub has been duplicated, discard old ref */ |
| 9345 | |
| 9346 | return 1; |
| 9347 | } |
| 9348 | |
| 9349 | /* This function is a LUA wrapper used for registering global lua event callbacks |
| 9350 | * The new subscription is pushed onto the stack on success |
| 9351 | * Returns the number of arguments pushed to the stack (1 for success) |
| 9352 | */ |
| 9353 | __LJMP static int hlua_event_global_sub(lua_State *L) |
| 9354 | { |
| 9355 | /* NULL <sub_list> = global subscription list */ |
| 9356 | return MAY_LJMP(hlua_event_sub(L, NULL)); |
| 9357 | } |
| 9358 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 9359 | /* Wrapper called by HAProxy to execute an LUA converter. This wrapper |
| 9360 | * doesn't allow "yield" functions because the HAProxy engine cannot |
| 9361 | * resume converters. |
| 9362 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 9363 | 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] | 9364 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 9365 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 9366 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 9367 | const char *error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 9368 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 9369 | if (!stream) |
| 9370 | return 0; |
| 9371 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 9372 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 9373 | * Lua context can be not initialized. This behavior |
| 9374 | * permits to save performances because a systematic |
| 9375 | * Lua initialization cause 5% performances loss. |
| 9376 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9377 | if (!stream->hlua) { |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 9378 | struct hlua *hlua; |
| 9379 | |
| 9380 | hlua = pool_alloc(pool_head_hlua); |
| 9381 | if (!hlua) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9382 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 9383 | return 0; |
| 9384 | } |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 9385 | HLUA_INIT(hlua); |
| 9386 | stream->hlua = hlua; |
Aurelien DARRAGON | 6b0b9bd | 2023-03-21 14:02:16 +0100 | [diff] [blame] | 9387 | 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] | 9388 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 9389 | return 0; |
| 9390 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 9391 | } |
| 9392 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 9393 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9394 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 9395 | |
| 9396 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9397 | if (!SET_SAFE_LJMP(stream->hlua)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9398 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 9399 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 9400 | else |
| 9401 | error = "critical error"; |
| 9402 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 9403 | return 0; |
| 9404 | } |
| 9405 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 9406 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9407 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9408 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9409 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 9410 | return 0; |
| 9411 | } |
| 9412 | |
| 9413 | /* Restore the function in the stack. */ |
Aurelien DARRAGON | 4fdf8b5 | 2023-03-20 17:22:37 +0100 | [diff] [blame] | 9414 | hlua_pushref(stream->hlua->T, fcn->function_ref[stream->hlua->state_id]); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 9415 | |
| 9416 | /* convert input sample and pust-it in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9417 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9418 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9419 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 9420 | return 0; |
| 9421 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9422 | hlua_smp2lua(stream->hlua->T, smp); |
| 9423 | stream->hlua->nargs = 1; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 9424 | |
| 9425 | /* push keywords in the stack. */ |
| 9426 | if (arg_p) { |
| 9427 | for (; arg_p->type != ARGT_STOP; arg_p++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9428 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9429 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9430 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 9431 | return 0; |
| 9432 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9433 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 9434 | stream->hlua->nargs++; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 9435 | } |
| 9436 | } |
| 9437 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 9438 | /* We must initialize the execution timeouts. */ |
Aurelien DARRAGON | da9503c | 2022-11-25 09:10:07 +0100 | [diff] [blame^] | 9439 | hlua_timer_init(&stream->hlua->timer, hlua_timeout_session); |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 9440 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 9441 | /* At this point the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9442 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 9443 | } |
| 9444 | |
| 9445 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9446 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 9447 | /* finished. */ |
| 9448 | case HLUA_E_OK: |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 9449 | /* If the stack is empty, the function fails. */ |
| 9450 | if (lua_gettop(stream->hlua->T) <= 0) |
| 9451 | return 0; |
| 9452 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 9453 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9454 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 9455 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 9456 | return 1; |
| 9457 | |
| 9458 | /* yield. */ |
| 9459 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9460 | 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] | 9461 | return 0; |
| 9462 | |
| 9463 | /* finished with error. */ |
| 9464 | case HLUA_E_ERRMSG: |
| 9465 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9466 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9467 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 9468 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 9469 | return 0; |
| 9470 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9471 | case HLUA_E_ETMOUT: |
| 9472 | SEND_ERR(stream->be, "Lua converter '%s': execution timeout.\n", fcn->name); |
| 9473 | return 0; |
| 9474 | |
| 9475 | case HLUA_E_NOMEM: |
| 9476 | SEND_ERR(stream->be, "Lua converter '%s': out of memory error.\n", fcn->name); |
| 9477 | return 0; |
| 9478 | |
| 9479 | case HLUA_E_YIELD: |
| 9480 | SEND_ERR(stream->be, "Lua converter '%s': yield functions like core.tcp() or core.sleep() are not allowed.\n", fcn->name); |
| 9481 | return 0; |
| 9482 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 9483 | case HLUA_E_ERR: |
| 9484 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9485 | 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] | 9486 | __fallthrough; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 9487 | |
| 9488 | default: |
| 9489 | return 0; |
| 9490 | } |
| 9491 | } |
| 9492 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9493 | /* Wrapper called by HAProxy to execute a sample-fetch. this wrapper |
| 9494 | * doesn't allow "yield" functions because the HAProxy engine cannot |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 9495 | * resume sample-fetches. This function will be called by the sample |
| 9496 | * fetch engine to call lua-based fetch operations. |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9497 | */ |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 9498 | static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp, |
| 9499 | const char *kw, void *private) |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9500 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 9501 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 9502 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 9503 | const char *error; |
Christopher Faulet | 8c9e6bb | 2021-08-06 16:29:41 +0200 | [diff] [blame] | 9504 | unsigned int hflags = HLUA_TXN_NOTERM | HLUA_TXN_SMP_CTX; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9505 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 9506 | if (!stream) |
| 9507 | return 0; |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 9508 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 9509 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 9510 | * Lua context can be not initialized. This behavior |
| 9511 | * permits to save performances because a systematic |
| 9512 | * Lua initialization cause 5% performances loss. |
| 9513 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9514 | if (!stream->hlua) { |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 9515 | struct hlua *hlua; |
| 9516 | |
| 9517 | hlua = pool_alloc(pool_head_hlua); |
| 9518 | if (!hlua) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9519 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 9520 | return 0; |
| 9521 | } |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 9522 | hlua->T = NULL; |
| 9523 | stream->hlua = hlua; |
Aurelien DARRAGON | 6b0b9bd | 2023-03-21 14:02:16 +0100 | [diff] [blame] | 9524 | 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] | 9525 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 9526 | return 0; |
| 9527 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 9528 | } |
| 9529 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9530 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9531 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 9532 | |
| 9533 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9534 | if (!SET_SAFE_LJMP(stream->hlua)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9535 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 9536 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 9537 | else |
| 9538 | error = "critical error"; |
| 9539 | 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] | 9540 | return 0; |
| 9541 | } |
| 9542 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9543 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9544 | if (!lua_checkstack(stream->hlua->T, 2)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9545 | 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] | 9546 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9547 | return 0; |
| 9548 | } |
| 9549 | |
| 9550 | /* Restore the function in the stack. */ |
Aurelien DARRAGON | 4fdf8b5 | 2023-03-20 17:22:37 +0100 | [diff] [blame] | 9551 | hlua_pushref(stream->hlua->T, fcn->function_ref[stream->hlua->state_id]); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9552 | |
| 9553 | /* push arguments in the stack. */ |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 9554 | 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] | 9555 | 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] | 9556 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9557 | return 0; |
| 9558 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9559 | stream->hlua->nargs = 1; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9560 | |
| 9561 | /* push keywords in the stack. */ |
| 9562 | for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) { |
| 9563 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9564 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9565 | 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] | 9566 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9567 | return 0; |
| 9568 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9569 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 9570 | stream->hlua->nargs++; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9571 | } |
| 9572 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 9573 | /* We must initialize the execution timeouts. */ |
Aurelien DARRAGON | da9503c | 2022-11-25 09:10:07 +0100 | [diff] [blame^] | 9574 | hlua_timer_init(&stream->hlua->timer, hlua_timeout_session); |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 9575 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 9576 | /* At this point the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9577 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9578 | } |
| 9579 | |
| 9580 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9581 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9582 | /* finished. */ |
| 9583 | case HLUA_E_OK: |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 9584 | /* If the stack is empty, the function fails. */ |
| 9585 | if (lua_gettop(stream->hlua->T) <= 0) |
| 9586 | return 0; |
| 9587 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9588 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9589 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 9590 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9591 | |
| 9592 | /* Set the end of execution flag. */ |
| 9593 | smp->flags &= ~SMP_F_MAY_CHANGE; |
| 9594 | return 1; |
| 9595 | |
| 9596 | /* yield. */ |
| 9597 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9598 | 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] | 9599 | return 0; |
| 9600 | |
| 9601 | /* finished with error. */ |
| 9602 | case HLUA_E_ERRMSG: |
| 9603 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9604 | SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9605 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 9606 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9607 | return 0; |
| 9608 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9609 | case HLUA_E_ETMOUT: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9610 | SEND_ERR(smp->px, "Lua sample-fetch '%s': execution timeout.\n", fcn->name); |
| 9611 | return 0; |
| 9612 | |
| 9613 | case HLUA_E_NOMEM: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9614 | SEND_ERR(smp->px, "Lua sample-fetch '%s': out of memory error.\n", fcn->name); |
| 9615 | return 0; |
| 9616 | |
| 9617 | case HLUA_E_YIELD: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9618 | SEND_ERR(smp->px, "Lua sample-fetch '%s': yield not allowed.\n", fcn->name); |
| 9619 | return 0; |
| 9620 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9621 | case HLUA_E_ERR: |
| 9622 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9623 | 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] | 9624 | __fallthrough; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9625 | |
| 9626 | default: |
| 9627 | return 0; |
| 9628 | } |
| 9629 | } |
| 9630 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 9631 | /* This function is an LUA binding used for registering |
| 9632 | * "sample-conv" functions. It expects a converter name used |
| 9633 | * in the haproxy configuration file, and an LUA function. |
| 9634 | */ |
| 9635 | __LJMP static int hlua_register_converters(lua_State *L) |
| 9636 | { |
| 9637 | struct sample_conv_kw_list *sck; |
| 9638 | const char *name; |
| 9639 | int ref; |
| 9640 | int len; |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 9641 | struct hlua_function *fcn = NULL; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 9642 | struct sample_conv *sc; |
| 9643 | struct buffer *trash; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 9644 | |
| 9645 | MAY_LJMP(check_args(L, 2, "register_converters")); |
| 9646 | |
Aurelien DARRAGON | 87f5297 | 2023-02-24 09:43:54 +0100 | [diff] [blame] | 9647 | if (hlua_gethlua(L)) { |
| 9648 | /* runtime processing */ |
| 9649 | WILL_LJMP(luaL_error(L, "register_converters: not available outside of body context")); |
| 9650 | } |
| 9651 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 9652 | /* First argument : converter name. */ |
| 9653 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 9654 | |
| 9655 | /* Second argument : lua function. */ |
| 9656 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 9657 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 9658 | /* Check if the converter is already registered */ |
| 9659 | trash = get_trash_chunk(); |
| 9660 | chunk_printf(trash, "lua.%s", name); |
| 9661 | sc = find_sample_conv(trash->area, trash->data); |
| 9662 | if (sc != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 9663 | fcn = sc->private; |
| 9664 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 9665 | ha_warning("Trying to register converter 'lua.%s' more than once. " |
| 9666 | "This will become a hard error in version 2.5.\n", name); |
Aurelien DARRAGON | 55afbed | 2023-03-02 18:42:06 +0100 | [diff] [blame] | 9667 | hlua_unref(L, fcn->function_ref[hlua_state_id]); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 9668 | } |
| 9669 | fcn->function_ref[hlua_state_id] = ref; |
| 9670 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 9671 | } |
| 9672 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 9673 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 9674 | sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 9675 | if (!sck) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 9676 | goto alloc_error; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 9677 | fcn = new_hlua_function(); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 9678 | if (!fcn) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 9679 | goto alloc_error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 9680 | |
| 9681 | /* Fill fcn. */ |
| 9682 | fcn->name = strdup(name); |
| 9683 | if (!fcn->name) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 9684 | goto alloc_error; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9685 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 9686 | |
| 9687 | /* List head */ |
| 9688 | sck->list.n = sck->list.p = NULL; |
| 9689 | |
| 9690 | /* converter keyword. */ |
| 9691 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 9692 | sck->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 9693 | if (!sck->kw[0].kw) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 9694 | goto alloc_error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 9695 | |
| 9696 | snprintf((char *)sck->kw[0].kw, len, "lua.%s", name); |
| 9697 | sck->kw[0].process = hlua_sample_conv_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 9698 | 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] | 9699 | sck->kw[0].val_args = NULL; |
| 9700 | sck->kw[0].in_type = SMP_T_STR; |
| 9701 | sck->kw[0].out_type = SMP_T_STR; |
| 9702 | sck->kw[0].private = fcn; |
| 9703 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 9704 | /* Register this new converter */ |
| 9705 | sample_register_convs(sck); |
| 9706 | |
| 9707 | return 0; |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 9708 | |
| 9709 | alloc_error: |
| 9710 | release_hlua_function(fcn); |
Aurelien DARRAGON | 55afbed | 2023-03-02 18:42:06 +0100 | [diff] [blame] | 9711 | hlua_unref(L, ref); |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 9712 | ha_free(&sck); |
| 9713 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 9714 | return 0; /* Never reached */ |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 9715 | } |
| 9716 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 9717 | /* This function is an LUA binding used for registering |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9718 | * "sample-fetch" functions. It expects a converter name used |
| 9719 | * in the haproxy configuration file, and an LUA function. |
| 9720 | */ |
| 9721 | __LJMP static int hlua_register_fetches(lua_State *L) |
| 9722 | { |
| 9723 | const char *name; |
| 9724 | int ref; |
| 9725 | int len; |
| 9726 | struct sample_fetch_kw_list *sfk; |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 9727 | struct hlua_function *fcn = NULL; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 9728 | struct sample_fetch *sf; |
| 9729 | struct buffer *trash; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9730 | |
| 9731 | MAY_LJMP(check_args(L, 2, "register_fetches")); |
| 9732 | |
Aurelien DARRAGON | 87f5297 | 2023-02-24 09:43:54 +0100 | [diff] [blame] | 9733 | if (hlua_gethlua(L)) { |
| 9734 | /* runtime processing */ |
| 9735 | WILL_LJMP(luaL_error(L, "register_fetches: not available outside of body context")); |
| 9736 | } |
| 9737 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9738 | /* First argument : sample-fetch name. */ |
| 9739 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 9740 | |
| 9741 | /* Second argument : lua function. */ |
| 9742 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 9743 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 9744 | /* Check if the sample-fetch is already registered */ |
| 9745 | trash = get_trash_chunk(); |
| 9746 | chunk_printf(trash, "lua.%s", name); |
| 9747 | sf = find_sample_fetch(trash->area, trash->data); |
| 9748 | if (sf != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 9749 | fcn = sf->private; |
| 9750 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 9751 | ha_warning("Trying to register sample-fetch 'lua.%s' more than once. " |
| 9752 | "This will become a hard error in version 2.5.\n", name); |
Aurelien DARRAGON | 55afbed | 2023-03-02 18:42:06 +0100 | [diff] [blame] | 9753 | hlua_unref(L, fcn->function_ref[hlua_state_id]); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 9754 | } |
| 9755 | fcn->function_ref[hlua_state_id] = ref; |
| 9756 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 9757 | } |
| 9758 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9759 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 9760 | sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9761 | if (!sfk) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 9762 | goto alloc_error; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 9763 | fcn = new_hlua_function(); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9764 | if (!fcn) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 9765 | goto alloc_error; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9766 | |
| 9767 | /* Fill fcn. */ |
| 9768 | fcn->name = strdup(name); |
| 9769 | if (!fcn->name) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 9770 | goto alloc_error; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9771 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9772 | |
| 9773 | /* List head */ |
| 9774 | sfk->list.n = sfk->list.p = NULL; |
| 9775 | |
| 9776 | /* sample-fetch keyword. */ |
| 9777 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 9778 | sfk->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9779 | if (!sfk->kw[0].kw) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 9780 | goto alloc_error; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9781 | |
| 9782 | snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name); |
| 9783 | sfk->kw[0].process = hlua_sample_fetch_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 9784 | 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] | 9785 | sfk->kw[0].val_args = NULL; |
| 9786 | sfk->kw[0].out_type = SMP_T_STR; |
| 9787 | sfk->kw[0].use = SMP_USE_HTTP_ANY; |
| 9788 | sfk->kw[0].val = 0; |
| 9789 | sfk->kw[0].private = fcn; |
| 9790 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9791 | /* Register this new fetch. */ |
| 9792 | sample_register_fetches(sfk); |
| 9793 | |
| 9794 | return 0; |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 9795 | |
| 9796 | alloc_error: |
| 9797 | release_hlua_function(fcn); |
Aurelien DARRAGON | 55afbed | 2023-03-02 18:42:06 +0100 | [diff] [blame] | 9798 | hlua_unref(L, ref); |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 9799 | ha_free(&sfk); |
| 9800 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 9801 | return 0; /* Never reached */ |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9802 | } |
| 9803 | |
Christopher Faulet | 501465d | 2020-02-26 14:54:16 +0100 | [diff] [blame] | 9804 | /* This function is a lua binding to set the wake_time. |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 9805 | */ |
Christopher Faulet | 501465d | 2020-02-26 14:54:16 +0100 | [diff] [blame] | 9806 | __LJMP static int hlua_set_wake_time(lua_State *L) |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 9807 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 9808 | struct hlua *hlua; |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 9809 | unsigned int delay; |
| 9810 | unsigned int wakeup_ms; |
| 9811 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 9812 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 9813 | hlua = hlua_gethlua(L); |
| 9814 | if (!hlua) { |
| 9815 | return 0; |
| 9816 | } |
| 9817 | |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 9818 | MAY_LJMP(check_args(L, 1, "wake_time")); |
| 9819 | |
| 9820 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 9821 | wakeup_ms = tick_add(now_ms, delay); |
| 9822 | hlua->wake_time = wakeup_ms; |
| 9823 | return 0; |
| 9824 | } |
| 9825 | |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9826 | /* This function is a wrapper to execute each LUA function declared as an action |
| 9827 | * wrapper during the initialisation period. This function may return any |
| 9828 | * ACT_RET_* value. On error ACT_RET_CONT is returned and the action is |
| 9829 | * ignored. If the lua action yields, ACT_RET_YIELD is returned. On success, the |
| 9830 | * return value is the first element on the stack. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9831 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9832 | 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] | 9833 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9834 | { |
| 9835 | char **arg; |
Christopher Faulet | 8c9e6bb | 2021-08-06 16:29:41 +0200 | [diff] [blame] | 9836 | unsigned int hflags = HLUA_TXN_ACT_CTX; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9837 | int dir, act_ret = ACT_RET_CONT; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 9838 | const char *error; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9839 | |
| 9840 | switch (rule->from) { |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 9841 | case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break; |
| 9842 | case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break; |
| 9843 | case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break; |
| 9844 | case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9845 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9846 | SEND_ERR(px, "Lua: internal error while execute action.\n"); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9847 | goto end; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9848 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9849 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 9850 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 9851 | * Lua context can be not initialized. This behavior |
| 9852 | * permits to save performances because a systematic |
| 9853 | * Lua initialization cause 5% performances loss. |
| 9854 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9855 | if (!s->hlua) { |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 9856 | struct hlua *hlua; |
| 9857 | |
| 9858 | hlua = pool_alloc(pool_head_hlua); |
| 9859 | if (!hlua) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9860 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9861 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9862 | goto end; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9863 | } |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 9864 | HLUA_INIT(hlua); |
| 9865 | s->hlua = hlua; |
Aurelien DARRAGON | 6b0b9bd | 2023-03-21 14:02:16 +0100 | [diff] [blame] | 9866 | 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] | 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 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 9871 | } |
| 9872 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9873 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9874 | if (!HLUA_IS_RUNNING(s->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 9875 | |
| 9876 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9877 | if (!SET_SAFE_LJMP(s->hlua)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9878 | if (lua_type(s->hlua->T, -1) == LUA_TSTRING) |
| 9879 | error = lua_tostring(s->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 9880 | else |
| 9881 | error = "critical error"; |
| 9882 | SEND_ERR(px, "Lua function '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9883 | rule->arg.hlua_rule->fcn->name, error); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9884 | goto end; |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 9885 | } |
| 9886 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9887 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9888 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9889 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9890 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9891 | RESET_SAFE_LJMP(s->hlua); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9892 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9893 | } |
| 9894 | |
| 9895 | /* Restore the function in the stack. */ |
Aurelien DARRAGON | 4fdf8b5 | 2023-03-20 17:22:37 +0100 | [diff] [blame] | 9896 | 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] | 9897 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 9898 | /* Create and and push object stream in the stack. */ |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 9899 | if (!hlua_txn_new(s->hlua->T, s, px, dir, hflags)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9900 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9901 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9902 | RESET_SAFE_LJMP(s->hlua); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9903 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9904 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9905 | s->hlua->nargs = 1; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9906 | |
| 9907 | /* push keywords in the stack. */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9908 | for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9909 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9910 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9911 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9912 | RESET_SAFE_LJMP(s->hlua); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9913 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9914 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9915 | lua_pushstring(s->hlua->T, *arg); |
| 9916 | s->hlua->nargs++; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9917 | } |
| 9918 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 9919 | /* Now the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9920 | RESET_SAFE_LJMP(s->hlua); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 9921 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 9922 | /* We must initialize the execution timeouts. */ |
Aurelien DARRAGON | da9503c | 2022-11-25 09:10:07 +0100 | [diff] [blame^] | 9923 | hlua_timer_init(&s->hlua->timer, hlua_timeout_session); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9924 | } |
| 9925 | |
| 9926 | /* Execute the function. */ |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 9927 | switch (hlua_ctx_resume(s->hlua, !(flags & ACT_OPT_FINAL))) { |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9928 | /* finished. */ |
| 9929 | case HLUA_E_OK: |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9930 | /* Catch the return value */ |
| 9931 | if (lua_gettop(s->hlua->T) > 0) |
| 9932 | act_ret = lua_tointeger(s->hlua->T, -1); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9933 | |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 9934 | /* Set timeout in the required channel. */ |
Christopher Faulet | 498c483 | 2020-07-28 11:59:58 +0200 | [diff] [blame] | 9935 | if (act_ret == ACT_RET_YIELD) { |
| 9936 | if (flags & ACT_OPT_FINAL) |
| 9937 | goto err_yield; |
| 9938 | |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 9939 | if (dir == SMP_OPT_DIR_REQ) |
| 9940 | s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp), |
| 9941 | s->hlua->wake_time); |
| 9942 | else |
| 9943 | s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp), |
| 9944 | s->hlua->wake_time); |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 9945 | } |
| 9946 | goto end; |
| 9947 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9948 | /* yield. */ |
| 9949 | case HLUA_E_AGAIN: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 9950 | /* Set timeout in the required channel. */ |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 9951 | if (dir == SMP_OPT_DIR_REQ) |
| 9952 | s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp), |
| 9953 | s->hlua->wake_time); |
| 9954 | else |
| 9955 | s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp), |
| 9956 | s->hlua->wake_time); |
| 9957 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9958 | /* Some actions can be wake up when a "write" event |
| 9959 | * is detected on a response channel. This is useful |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 9960 | * only for actions targeted on the requests. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9961 | */ |
Christopher Faulet | 51fa358 | 2019-07-26 14:54:52 +0200 | [diff] [blame] | 9962 | if (HLUA_IS_WAKERESWR(s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 9963 | s->res.flags |= CF_WAKE_WRITE; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9964 | if (HLUA_IS_WAKEREQWR(s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 9965 | s->req.flags |= CF_WAKE_WRITE; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9966 | act_ret = ACT_RET_YIELD; |
| 9967 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9968 | |
| 9969 | /* finished with error. */ |
| 9970 | case HLUA_E_ERRMSG: |
| 9971 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9972 | SEND_ERR(px, "Lua function '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9973 | rule->arg.hlua_rule->fcn->name, lua_tostring(s->hlua->T, -1)); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9974 | lua_pop(s->hlua->T, 1); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9975 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9976 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9977 | case HLUA_E_ETMOUT: |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9978 | 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] | 9979 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9980 | |
| 9981 | case HLUA_E_NOMEM: |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9982 | 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] | 9983 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9984 | |
| 9985 | case HLUA_E_YIELD: |
Christopher Faulet | 498c483 | 2020-07-28 11:59:58 +0200 | [diff] [blame] | 9986 | err_yield: |
| 9987 | act_ret = ACT_RET_CONT; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9988 | 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] | 9989 | 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 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9992 | case HLUA_E_ERR: |
| 9993 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9994 | SEND_ERR(px, "Lua function '%s' return an unknown error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9995 | rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9996 | |
| 9997 | default: |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9998 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9999 | } |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 10000 | |
| 10001 | end: |
Christopher Faulet | 2361fd9 | 2020-07-30 10:40:58 +0200 | [diff] [blame] | 10002 | if (act_ret != ACT_RET_YIELD && s->hlua) |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 10003 | s->hlua->wake_time = TICK_ETERNITY; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 10004 | return act_ret; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 10005 | } |
| 10006 | |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 10007 | 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] | 10008 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 10009 | struct appctx *ctx = context; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10010 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10011 | appctx_wakeup(ctx); |
Willy Tarreau | d958741 | 2017-08-23 16:07:33 +0200 | [diff] [blame] | 10012 | t->expire = TICK_ETERNITY; |
Willy Tarreau | d1aa41f | 2017-07-21 16:41:56 +0200 | [diff] [blame] | 10013 | return t; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10014 | } |
| 10015 | |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 10016 | static int hlua_applet_tcp_init(struct appctx *ctx) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10017 | { |
Willy Tarreau | e23f33b | 2022-05-06 14:07:13 +0200 | [diff] [blame] | 10018 | 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] | 10019 | struct stconn *sc = appctx_sc(ctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 10020 | struct stream *strm = __sc_strm(sc); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 10021 | struct hlua *hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10022 | struct task *task; |
| 10023 | char **arg; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 10024 | const char *error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10025 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 10026 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 10027 | if (!hlua) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 10028 | SEND_ERR(strm->be, "Lua applet tcp '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 10029 | ctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 10030 | return -1; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 10031 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10032 | HLUA_INIT(hlua); |
Willy Tarreau | e23f33b | 2022-05-06 14:07:13 +0200 | [diff] [blame] | 10033 | tcp_ctx->hlua = hlua; |
| 10034 | tcp_ctx->flags = 0; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10035 | |
| 10036 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | beeabf5 | 2021-10-01 18:23:30 +0200 | [diff] [blame] | 10037 | task = task_new_here(); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10038 | if (!task) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 10039 | SEND_ERR(strm->be, "Lua applet tcp '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 10040 | ctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 10041 | return -1; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10042 | } |
| 10043 | task->nice = 0; |
| 10044 | task->context = ctx; |
| 10045 | task->process = hlua_applet_wakeup; |
Willy Tarreau | e23f33b | 2022-05-06 14:07:13 +0200 | [diff] [blame] | 10046 | tcp_ctx->task = task; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10047 | |
| 10048 | /* In the execution wrappers linked with a stream, the |
| 10049 | * Lua context can be not initialized. This behavior |
| 10050 | * permits to save performances because a systematic |
| 10051 | * Lua initialization cause 5% performances loss. |
| 10052 | */ |
Aurelien DARRAGON | 6b0b9bd | 2023-03-21 14:02:16 +0100 | [diff] [blame] | 10053 | 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] | 10054 | 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] | 10055 | ctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 10056 | return -1; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10057 | } |
| 10058 | |
| 10059 | /* Set timeout according with the applet configuration. */ |
Aurelien DARRAGON | da9503c | 2022-11-25 09:10:07 +0100 | [diff] [blame^] | 10060 | hlua_timer_init(&hlua->timer, ctx->applet->timeout); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10061 | |
| 10062 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 10063 | if (!SET_SAFE_LJMP(hlua)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 10064 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 10065 | error = lua_tostring(hlua->T, -1); |
| 10066 | else |
| 10067 | error = "critical error"; |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 10068 | SEND_ERR(strm->be, "Lua applet tcp '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 10069 | ctx->rule->arg.hlua_rule->fcn->name, error); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 10070 | return -1; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10071 | } |
| 10072 | |
| 10073 | /* Check stack available size. */ |
| 10074 | if (!lua_checkstack(hlua->T, 1)) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 10075 | SEND_ERR(strm->be, "Lua applet tcp '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 10076 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 10077 | RESET_SAFE_LJMP(hlua); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 10078 | return -1; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10079 | } |
| 10080 | |
| 10081 | /* Restore the function in the stack. */ |
Aurelien DARRAGON | 4fdf8b5 | 2023-03-20 17:22:37 +0100 | [diff] [blame] | 10082 | 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] | 10083 | |
| 10084 | /* Create and and push object stream in the stack. */ |
| 10085 | if (!hlua_applet_tcp_new(hlua->T, ctx)) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 10086 | SEND_ERR(strm->be, "Lua applet tcp '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 10087 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 10088 | RESET_SAFE_LJMP(hlua); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 10089 | return -1; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10090 | } |
| 10091 | hlua->nargs = 1; |
| 10092 | |
| 10093 | /* push keywords in the stack. */ |
| 10094 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 10095 | if (!lua_checkstack(hlua->T, 1)) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 10096 | SEND_ERR(strm->be, "Lua applet tcp '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 10097 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 10098 | RESET_SAFE_LJMP(hlua); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 10099 | return -1; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10100 | } |
| 10101 | lua_pushstring(hlua->T, *arg); |
| 10102 | hlua->nargs++; |
| 10103 | } |
| 10104 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 10105 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10106 | |
| 10107 | /* Wakeup the applet ASAP. */ |
Willy Tarreau | 90e8b45 | 2022-05-25 18:21:43 +0200 | [diff] [blame] | 10108 | applet_need_more_data(ctx); |
Willy Tarreau | 4164eb9 | 2022-05-25 15:42:03 +0200 | [diff] [blame] | 10109 | applet_have_more_data(ctx); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10110 | |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 10111 | return 0; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10112 | } |
| 10113 | |
Willy Tarreau | 60409db | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 10114 | void hlua_applet_tcp_fct(struct appctx *ctx) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10115 | { |
Willy Tarreau | e23f33b | 2022-05-06 14:07:13 +0200 | [diff] [blame] | 10116 | struct hlua_tcp_ctx *tcp_ctx = ctx->svcctx; |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 10117 | struct stconn *sc = appctx_sc(ctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 10118 | struct stream *strm = __sc_strm(sc); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10119 | struct act_rule *rule = ctx->rule; |
| 10120 | struct proxy *px = strm->be; |
Willy Tarreau | e23f33b | 2022-05-06 14:07:13 +0200 | [diff] [blame] | 10121 | struct hlua *hlua = tcp_ctx->hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10122 | |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 10123 | if (unlikely(se_fl_test(ctx->sedesc, (SE_FL_EOS|SE_FL_ERROR|SE_FL_SHR|SE_FL_SHW)))) |
| 10124 | goto out; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10125 | |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 10126 | /* The applet execution is already done. */ |
| 10127 | if (tcp_ctx->flags & APPLET_DONE) |
| 10128 | goto out; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10129 | |
| 10130 | /* Execute the function. */ |
| 10131 | switch (hlua_ctx_resume(hlua, 1)) { |
| 10132 | /* finished. */ |
| 10133 | case HLUA_E_OK: |
Willy Tarreau | e23f33b | 2022-05-06 14:07:13 +0200 | [diff] [blame] | 10134 | tcp_ctx->flags |= APPLET_DONE; |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 10135 | se_fl_set(ctx->sedesc, SE_FL_EOI|SE_FL_EOS); |
| 10136 | break; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10137 | |
| 10138 | /* yield. */ |
| 10139 | case HLUA_E_AGAIN: |
Thierry Fournier | 0164f20 | 2016-02-20 17:47:43 +0100 | [diff] [blame] | 10140 | if (hlua->wake_time != TICK_ETERNITY) |
Willy Tarreau | e23f33b | 2022-05-06 14:07:13 +0200 | [diff] [blame] | 10141 | task_schedule(tcp_ctx->task, hlua->wake_time); |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 10142 | break; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10143 | |
| 10144 | /* finished with error. */ |
| 10145 | case HLUA_E_ERRMSG: |
| 10146 | /* Display log. */ |
| 10147 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 10148 | rule->arg.hlua_rule->fcn->name, lua_tostring(hlua->T, -1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10149 | lua_pop(hlua->T, 1); |
| 10150 | goto error; |
| 10151 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 10152 | case HLUA_E_ETMOUT: |
| 10153 | SEND_ERR(px, "Lua applet tcp '%s': execution timeout.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 10154 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 10155 | goto error; |
| 10156 | |
| 10157 | case HLUA_E_NOMEM: |
| 10158 | SEND_ERR(px, "Lua applet tcp '%s': out of memory error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 10159 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 10160 | goto error; |
| 10161 | |
| 10162 | case HLUA_E_YIELD: /* unexpected */ |
| 10163 | SEND_ERR(px, "Lua applet tcp '%s': yield not allowed.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 10164 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 10165 | goto error; |
| 10166 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10167 | case HLUA_E_ERR: |
| 10168 | /* Display log. */ |
| 10169 | SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 10170 | rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10171 | goto error; |
| 10172 | |
| 10173 | default: |
| 10174 | goto error; |
| 10175 | } |
| 10176 | |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 10177 | out: |
| 10178 | /* eat the whole request */ |
| 10179 | co_skip(sc_oc(sc), co_data(sc_oc(sc))); |
| 10180 | return; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10181 | |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 10182 | error: |
| 10183 | se_fl_set(ctx->sedesc, SE_FL_ERROR); |
Willy Tarreau | e23f33b | 2022-05-06 14:07:13 +0200 | [diff] [blame] | 10184 | tcp_ctx->flags |= APPLET_DONE; |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 10185 | goto out; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10186 | } |
| 10187 | |
| 10188 | static void hlua_applet_tcp_release(struct appctx *ctx) |
| 10189 | { |
Willy Tarreau | e23f33b | 2022-05-06 14:07:13 +0200 | [diff] [blame] | 10190 | struct hlua_tcp_ctx *tcp_ctx = ctx->svcctx; |
| 10191 | |
| 10192 | task_destroy(tcp_ctx->task); |
| 10193 | tcp_ctx->task = NULL; |
| 10194 | hlua_ctx_destroy(tcp_ctx->hlua); |
| 10195 | tcp_ctx->hlua = NULL; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10196 | } |
| 10197 | |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 10198 | /* The function returns 0 if the initialisation is complete or -1 if |
| 10199 | * 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] | 10200 | */ |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 10201 | static int hlua_applet_http_init(struct appctx *ctx) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 10202 | { |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 10203 | 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] | 10204 | struct stconn *sc = appctx_sc(ctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 10205 | struct stream *strm = __sc_strm(sc); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 10206 | struct http_txn *txn; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 10207 | struct hlua *hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 10208 | char **arg; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 10209 | struct task *task; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 10210 | const char *error; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 10211 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 10212 | txn = strm->txn; |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 10213 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 10214 | if (!hlua) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 10215 | SEND_ERR(strm->be, "Lua applet http '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 10216 | ctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 10217 | return -1; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 10218 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 10219 | HLUA_INIT(hlua); |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 10220 | http_ctx->hlua = hlua; |
| 10221 | http_ctx->left_bytes = -1; |
| 10222 | http_ctx->flags = 0; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 10223 | |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 10224 | if (txn->req.flags & HTTP_MSGF_VER_11) |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 10225 | http_ctx->flags |= APPLET_HTTP11; |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 10226 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 10227 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | beeabf5 | 2021-10-01 18:23:30 +0200 | [diff] [blame] | 10228 | task = task_new_here(); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 10229 | if (!task) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 10230 | SEND_ERR(strm->be, "Lua applet http '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 10231 | ctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 10232 | return -1; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 10233 | } |
| 10234 | task->nice = 0; |
| 10235 | task->context = ctx; |
| 10236 | task->process = hlua_applet_wakeup; |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 10237 | http_ctx->task = task; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 10238 | |
| 10239 | /* In the execution wrappers linked with a stream, the |
| 10240 | * Lua context can be not initialized. This behavior |
| 10241 | * permits to save performances because a systematic |
| 10242 | * Lua initialization cause 5% performances loss. |
| 10243 | */ |
Aurelien DARRAGON | 6b0b9bd | 2023-03-21 14:02:16 +0100 | [diff] [blame] | 10244 | 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] | 10245 | 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] | 10246 | ctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 10247 | return -1; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 10248 | } |
| 10249 | |
| 10250 | /* Set timeout according with the applet configuration. */ |
Aurelien DARRAGON | da9503c | 2022-11-25 09:10:07 +0100 | [diff] [blame^] | 10251 | hlua_timer_init(&hlua->timer, ctx->applet->timeout); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 10252 | |
| 10253 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 10254 | if (!SET_SAFE_LJMP(hlua)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 10255 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 10256 | error = lua_tostring(hlua->T, -1); |
| 10257 | else |
| 10258 | error = "critical error"; |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 10259 | SEND_ERR(strm->be, "Lua applet http '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 10260 | ctx->rule->arg.hlua_rule->fcn->name, error); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 10261 | return -1; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 10262 | } |
| 10263 | |
| 10264 | /* Check stack available size. */ |
| 10265 | if (!lua_checkstack(hlua->T, 1)) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 10266 | SEND_ERR(strm->be, "Lua applet http '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 10267 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 10268 | RESET_SAFE_LJMP(hlua); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 10269 | return -1; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 10270 | } |
| 10271 | |
| 10272 | /* Restore the function in the stack. */ |
Aurelien DARRAGON | 4fdf8b5 | 2023-03-20 17:22:37 +0100 | [diff] [blame] | 10273 | 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] | 10274 | |
| 10275 | /* Create and and push object stream in the stack. */ |
| 10276 | if (!hlua_applet_http_new(hlua->T, ctx)) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 10277 | SEND_ERR(strm->be, "Lua applet http '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 10278 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 10279 | RESET_SAFE_LJMP(hlua); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 10280 | return -1; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 10281 | } |
| 10282 | hlua->nargs = 1; |
| 10283 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 10284 | /* push keywords in the stack. */ |
| 10285 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 10286 | if (!lua_checkstack(hlua->T, 1)) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 10287 | SEND_ERR(strm->be, "Lua applet http '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 10288 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 10289 | RESET_SAFE_LJMP(hlua); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 10290 | return -1; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 10291 | } |
| 10292 | lua_pushstring(hlua->T, *arg); |
| 10293 | hlua->nargs++; |
| 10294 | } |
| 10295 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 10296 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 10297 | |
| 10298 | /* Wakeup the applet when data is ready for read. */ |
Willy Tarreau | 90e8b45 | 2022-05-25 18:21:43 +0200 | [diff] [blame] | 10299 | applet_need_more_data(ctx); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 10300 | |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 10301 | return 0; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 10302 | } |
| 10303 | |
Willy Tarreau | 60409db | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 10304 | void hlua_applet_http_fct(struct appctx *ctx) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 10305 | { |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 10306 | struct hlua_http_ctx *http_ctx = ctx->svcctx; |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 10307 | struct stconn *sc = appctx_sc(ctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 10308 | struct stream *strm = __sc_strm(sc); |
| 10309 | struct channel *req = sc_oc(sc); |
| 10310 | struct channel *res = sc_ic(sc); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 10311 | struct act_rule *rule = ctx->rule; |
| 10312 | struct proxy *px = strm->be; |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 10313 | struct hlua *hlua = http_ctx->hlua; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 10314 | struct htx *req_htx, *res_htx; |
| 10315 | |
| 10316 | res_htx = htx_from_buf(&res->buf); |
| 10317 | |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 10318 | if (unlikely(se_fl_test(ctx->sedesc, (SE_FL_EOS|SE_FL_ERROR|SE_FL_SHR|SE_FL_SHW)))) |
| 10319 | goto out; |
| 10320 | |
| 10321 | /* The applet execution is already done. */ |
| 10322 | if (http_ctx->flags & APPLET_DONE) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 10323 | goto out; |
| 10324 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 10325 | /* Check if the input buffer is available. */ |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 10326 | if (!b_size(&res->buf)) { |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 10327 | sc_need_room(sc); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 10328 | goto out; |
| 10329 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 10330 | |
| 10331 | /* Set the currently running flag. */ |
| 10332 | if (!HLUA_IS_RUNNING(hlua) && |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 10333 | !(http_ctx->flags & APPLET_DONE)) { |
Christopher Faulet | bd878d2 | 2021-04-28 10:50:21 +0200 | [diff] [blame] | 10334 | if (!co_data(req)) { |
Willy Tarreau | 90e8b45 | 2022-05-25 18:21:43 +0200 | [diff] [blame] | 10335 | applet_need_more_data(ctx); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 10336 | goto out; |
| 10337 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 10338 | } |
| 10339 | |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 10340 | /* Execute the function. */ |
| 10341 | switch (hlua_ctx_resume(hlua, 1)) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 10342 | /* finished. */ |
| 10343 | case HLUA_E_OK: |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 10344 | http_ctx->flags |= APPLET_DONE; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 10345 | break; |
| 10346 | |
| 10347 | /* yield. */ |
| 10348 | case HLUA_E_AGAIN: |
| 10349 | if (hlua->wake_time != TICK_ETERNITY) |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 10350 | task_schedule(http_ctx->task, hlua->wake_time); |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 10351 | goto out; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 10352 | |
| 10353 | /* finished with error. */ |
| 10354 | case HLUA_E_ERRMSG: |
| 10355 | /* Display log. */ |
| 10356 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 10357 | rule->arg.hlua_rule->fcn->name, lua_tostring(hlua->T, -1)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 10358 | lua_pop(hlua->T, 1); |
| 10359 | goto error; |
| 10360 | |
| 10361 | case HLUA_E_ETMOUT: |
| 10362 | SEND_ERR(px, "Lua applet http '%s': execution timeout.\n", |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 10363 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 10364 | goto error; |
| 10365 | |
| 10366 | case HLUA_E_NOMEM: |
| 10367 | SEND_ERR(px, "Lua applet http '%s': out of memory error.\n", |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 10368 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 10369 | goto error; |
| 10370 | |
| 10371 | case HLUA_E_YIELD: /* unexpected */ |
| 10372 | SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n", |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 10373 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 10374 | goto error; |
| 10375 | |
| 10376 | case HLUA_E_ERR: |
| 10377 | /* Display log. */ |
| 10378 | SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n", |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 10379 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 10380 | goto error; |
| 10381 | |
| 10382 | default: |
| 10383 | goto error; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 10384 | } |
| 10385 | |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 10386 | if (http_ctx->flags & APPLET_DONE) { |
| 10387 | if (http_ctx->flags & APPLET_RSP_SENT) |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 10388 | goto out; |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 10389 | |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 10390 | if (!(http_ctx->flags & APPLET_HDR_SENT)) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 10391 | goto error; |
| 10392 | |
Christopher Faulet | f89af9c | 2022-04-07 10:07:18 +0200 | [diff] [blame] | 10393 | /* no more data are expected. If the response buffer is empty |
| 10394 | * for a chunked message, be sure to add something (EOT block in |
| 10395 | * this case) to have something to send. It is important to be |
| 10396 | * sure the EOM flags will be handled by the endpoint. |
| 10397 | */ |
| 10398 | if (htx_is_empty(res_htx) && (strm->txn->rsp.flags & (HTTP_MSGF_XFER_LEN|HTTP_MSGF_CNT_LEN)) == HTTP_MSGF_XFER_LEN) { |
| 10399 | if (!htx_add_endof(res_htx, HTX_BLK_EOT)) { |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 10400 | sc_need_room(sc); |
Christopher Faulet | f89af9c | 2022-04-07 10:07:18 +0200 | [diff] [blame] | 10401 | goto out; |
| 10402 | } |
| 10403 | channel_add_input(res, 1); |
| 10404 | } |
| 10405 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 10406 | res_htx->flags |= HTX_FL_EOM; |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 10407 | se_fl_set(ctx->sedesc, SE_FL_EOI|SE_FL_EOS); |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 10408 | strm->txn->status = http_ctx->status; |
| 10409 | http_ctx->flags |= APPLET_RSP_SENT; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 10410 | } |
| 10411 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 10412 | out: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 10413 | htx_to_buf(res_htx, &res->buf); |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 10414 | /* eat the whole request */ |
| 10415 | if (co_data(req)) { |
| 10416 | req_htx = htx_from_buf(&req->buf); |
| 10417 | co_htx_skip(req, req_htx, co_data(req)); |
| 10418 | htx_to_buf(req_htx, &req->buf); |
| 10419 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 10420 | return; |
| 10421 | |
| 10422 | error: |
| 10423 | |
| 10424 | /* If we are in HTTP mode, and we are not send any |
| 10425 | * data, return a 500 server error in best effort: |
| 10426 | * if there is no room available in the buffer, |
| 10427 | * just close the connection. |
| 10428 | */ |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 10429 | if (!(http_ctx->flags & APPLET_HDR_SENT)) { |
Christopher Faulet | f734638 | 2019-07-17 22:02:08 +0200 | [diff] [blame] | 10430 | struct buffer *err = &http_err_chunks[HTTP_ERR_500]; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 10431 | |
| 10432 | channel_erase(res); |
| 10433 | res->buf.data = b_data(err); |
| 10434 | memcpy(res->buf.area, b_head(err), b_data(err)); |
| 10435 | res_htx = htx_from_buf(&res->buf); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 10436 | channel_add_input(res, res_htx->data); |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 10437 | se_fl_set(ctx->sedesc, SE_FL_EOI|SE_FL_EOS); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 10438 | } |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 10439 | else |
| 10440 | se_fl_set(ctx->sedesc, SE_FL_ERROR); |
| 10441 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 10442 | if (!(strm->flags & SF_ERR_MASK)) |
| 10443 | strm->flags |= SF_ERR_RESOURCE; |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 10444 | http_ctx->flags |= APPLET_DONE; |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 10445 | goto out; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 10446 | } |
| 10447 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 10448 | static void hlua_applet_http_release(struct appctx *ctx) |
| 10449 | { |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 10450 | struct hlua_http_ctx *http_ctx = ctx->svcctx; |
| 10451 | |
| 10452 | task_destroy(http_ctx->task); |
| 10453 | http_ctx->task = NULL; |
| 10454 | hlua_ctx_destroy(http_ctx->hlua); |
| 10455 | http_ctx->hlua = NULL; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 10456 | } |
| 10457 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 10458 | /* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 10459 | * success case, else return ACT_RET_PRS_ERR. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 10460 | * |
| 10461 | * This function can fail with an abort() due to an Lua critical error. |
| 10462 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 10463 | * tolerated. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 10464 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 10465 | static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px, |
| 10466 | struct act_rule *rule, char **err) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 10467 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 10468 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 10469 | int i; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 10470 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 10471 | /* Memory for the rule. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 10472 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 10473 | if (!rule->arg.hlua_rule) { |
| 10474 | memprintf(err, "out of memory error"); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 10475 | goto error; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 10476 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 10477 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 10478 | /* Memory for arguments. */ |
Tim Duesterhus | e52b6e5 | 2020-09-12 20:26:43 +0200 | [diff] [blame] | 10479 | rule->arg.hlua_rule->args = calloc(fcn->nargs + 1, |
| 10480 | sizeof(*rule->arg.hlua_rule->args)); |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 10481 | if (!rule->arg.hlua_rule->args) { |
| 10482 | memprintf(err, "out of memory error"); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 10483 | goto error; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 10484 | } |
| 10485 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 10486 | /* Reference the Lua function and store the reference. */ |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 10487 | rule->arg.hlua_rule->fcn = fcn; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 10488 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 10489 | /* Expect some arguments */ |
| 10490 | for (i = 0; i < fcn->nargs; i++) { |
Thierry FOURNIER | 1725c2e | 2019-01-06 19:38:49 +0100 | [diff] [blame] | 10491 | if (*args[*cur_arg] == '\0') { |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 10492 | memprintf(err, "expect %d arguments", fcn->nargs); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 10493 | goto error; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 10494 | } |
Thierry FOURNIER | 1725c2e | 2019-01-06 19:38:49 +0100 | [diff] [blame] | 10495 | rule->arg.hlua_rule->args[i] = strdup(args[*cur_arg]); |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 10496 | if (!rule->arg.hlua_rule->args[i]) { |
| 10497 | memprintf(err, "out of memory error"); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 10498 | goto error; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 10499 | } |
| 10500 | (*cur_arg)++; |
| 10501 | } |
| 10502 | rule->arg.hlua_rule->args[i] = NULL; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 10503 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 10504 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 10505 | rule->action_ptr = hlua_action; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 10506 | return ACT_RET_PRS_OK; |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 10507 | |
| 10508 | error: |
| 10509 | if (rule->arg.hlua_rule) { |
| 10510 | if (rule->arg.hlua_rule->args) { |
| 10511 | for (i = 0; i < fcn->nargs; i++) |
| 10512 | ha_free(&rule->arg.hlua_rule->args[i]); |
| 10513 | ha_free(&rule->arg.hlua_rule->args); |
| 10514 | } |
| 10515 | ha_free(&rule->arg.hlua_rule); |
| 10516 | } |
| 10517 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 10518 | } |
| 10519 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 10520 | static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px, |
| 10521 | struct act_rule *rule, char **err) |
| 10522 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 10523 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 10524 | |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 10525 | /* HTTP applets are forbidden in tcp-request rules. |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 10526 | * HTTP applet request requires everything initialized by |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 10527 | * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER). |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 10528 | * The applet will be immediately initialized, but its before |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 10529 | * the call of this analyzer. |
| 10530 | */ |
| 10531 | if (rule->from != ACT_F_HTTP_REQ) { |
| 10532 | memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets"); |
| 10533 | return ACT_RET_PRS_ERR; |
| 10534 | } |
| 10535 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 10536 | /* Memory for the rule. */ |
| 10537 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 10538 | if (!rule->arg.hlua_rule) { |
| 10539 | memprintf(err, "out of memory error"); |
| 10540 | return ACT_RET_PRS_ERR; |
| 10541 | } |
| 10542 | |
| 10543 | /* Reference the Lua function and store the reference. */ |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 10544 | rule->arg.hlua_rule->fcn = fcn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 10545 | |
| 10546 | /* TODO: later accept arguments. */ |
| 10547 | rule->arg.hlua_rule->args = NULL; |
| 10548 | |
| 10549 | /* Add applet pointer in the rule. */ |
| 10550 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 10551 | rule->applet.name = fcn->name; |
| 10552 | rule->applet.init = hlua_applet_http_init; |
| 10553 | rule->applet.fct = hlua_applet_http_fct; |
| 10554 | rule->applet.release = hlua_applet_http_release; |
| 10555 | rule->applet.timeout = hlua_timeout_applet; |
| 10556 | |
| 10557 | return ACT_RET_PRS_OK; |
| 10558 | } |
| 10559 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 10560 | /* This function is an LUA binding used for registering |
| 10561 | * "sample-conv" functions. It expects a converter name used |
| 10562 | * in the haproxy configuration file, and an LUA function. |
| 10563 | */ |
| 10564 | __LJMP static int hlua_register_action(lua_State *L) |
| 10565 | { |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 10566 | struct action_kw_list *akl = NULL; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 10567 | const char *name; |
| 10568 | int ref; |
| 10569 | int len; |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 10570 | struct hlua_function *fcn = NULL; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 10571 | int nargs; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 10572 | struct buffer *trash; |
| 10573 | struct action_kw *akw; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 10574 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 10575 | /* Initialise the number of expected arguments at 0. */ |
| 10576 | nargs = 0; |
| 10577 | |
| 10578 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 10579 | 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] | 10580 | |
Aurelien DARRAGON | 87f5297 | 2023-02-24 09:43:54 +0100 | [diff] [blame] | 10581 | if (hlua_gethlua(L)) { |
| 10582 | /* runtime processing */ |
| 10583 | WILL_LJMP(luaL_error(L, "register_action: not available outside of body context")); |
| 10584 | } |
| 10585 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 10586 | /* First argument : converter name. */ |
| 10587 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 10588 | |
| 10589 | /* Second argument : environment. */ |
| 10590 | if (lua_type(L, 2) != LUA_TTABLE) |
| 10591 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 10592 | |
| 10593 | /* Third argument : lua function. */ |
| 10594 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 10595 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 10596 | /* 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] | 10597 | if (lua_gettop(L) >= 4) |
| 10598 | nargs = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 10599 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 10600 | /* browse the second argument as an array. */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 10601 | lua_pushnil(L); |
| 10602 | while (lua_next(L, 2) != 0) { |
Aurelien DARRAGON | 55afbed | 2023-03-02 18:42:06 +0100 | [diff] [blame] | 10603 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 10604 | hlua_unref(L, ref); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 10605 | 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] | 10606 | } |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 10607 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 10608 | /* Check if action exists */ |
| 10609 | trash = get_trash_chunk(); |
| 10610 | chunk_printf(trash, "lua.%s", name); |
| 10611 | if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) { |
| 10612 | akw = tcp_req_cont_action(trash->area); |
| 10613 | } else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) { |
| 10614 | akw = tcp_res_cont_action(trash->area); |
| 10615 | } else if (strcmp(lua_tostring(L, -1), "http-req") == 0) { |
| 10616 | akw = action_http_req_custom(trash->area); |
| 10617 | } else if (strcmp(lua_tostring(L, -1), "http-res") == 0) { |
| 10618 | akw = action_http_res_custom(trash->area); |
| 10619 | } else { |
| 10620 | akw = NULL; |
| 10621 | } |
| 10622 | if (akw != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 10623 | fcn = akw->private; |
| 10624 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 10625 | ha_warning("Trying to register action 'lua.%s' more than once. " |
| 10626 | "This will become a hard error in version 2.5.\n", name); |
Aurelien DARRAGON | 55afbed | 2023-03-02 18:42:06 +0100 | [diff] [blame] | 10627 | hlua_unref(L, fcn->function_ref[hlua_state_id]); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 10628 | } |
| 10629 | fcn->function_ref[hlua_state_id] = ref; |
| 10630 | |
| 10631 | /* pop the environment string. */ |
| 10632 | lua_pop(L, 1); |
| 10633 | continue; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 10634 | } |
| 10635 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 10636 | /* Check required environment. Only accepted "http" or "tcp". */ |
| 10637 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 10638 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 10639 | if (!akl) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 10640 | goto alloc_error;; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 10641 | fcn = new_hlua_function(); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 10642 | if (!fcn) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 10643 | goto alloc_error; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 10644 | |
| 10645 | /* Fill fcn. */ |
| 10646 | fcn->name = strdup(name); |
| 10647 | if (!fcn->name) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 10648 | goto alloc_error; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 10649 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 10650 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 10651 | /* Set the expected number of arguments. */ |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 10652 | fcn->nargs = nargs; |
| 10653 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 10654 | /* List head */ |
| 10655 | akl->list.n = akl->list.p = NULL; |
| 10656 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 10657 | /* action keyword. */ |
| 10658 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 10659 | akl->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 10660 | if (!akl->kw[0].kw) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 10661 | goto alloc_error; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 10662 | |
| 10663 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 10664 | |
Amaury Denoyelle | e4a617c | 2021-05-06 15:33:09 +0200 | [diff] [blame] | 10665 | akl->kw[0].flags = 0; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 10666 | akl->kw[0].private = fcn; |
| 10667 | akl->kw[0].parse = action_register_lua; |
| 10668 | |
| 10669 | /* select the action registering point. */ |
| 10670 | if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) |
| 10671 | tcp_req_cont_keywords_register(akl); |
| 10672 | else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) |
| 10673 | tcp_res_cont_keywords_register(akl); |
| 10674 | else if (strcmp(lua_tostring(L, -1), "http-req") == 0) |
| 10675 | http_req_keywords_register(akl); |
| 10676 | else if (strcmp(lua_tostring(L, -1), "http-res") == 0) |
| 10677 | http_res_keywords_register(akl); |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 10678 | else { |
| 10679 | release_hlua_function(fcn); |
Aurelien DARRAGON | 55afbed | 2023-03-02 18:42:06 +0100 | [diff] [blame] | 10680 | hlua_unref(L, ref); |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 10681 | if (akl) |
| 10682 | ha_free((char **)&(akl->kw[0].kw)); |
| 10683 | ha_free(&akl); |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 10684 | WILL_LJMP(luaL_error(L, "Lua action environment '%s' is unknown. " |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 10685 | "'tcp-req', 'tcp-res', 'http-req' or 'http-res' " |
| 10686 | "are expected.", lua_tostring(L, -1))); |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 10687 | } |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 10688 | |
| 10689 | /* pop the environment string. */ |
| 10690 | lua_pop(L, 1); |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 10691 | |
| 10692 | /* reset for next loop */ |
| 10693 | akl = NULL; |
| 10694 | fcn = NULL; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 10695 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10696 | return ACT_RET_PRS_OK; |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 10697 | |
| 10698 | alloc_error: |
| 10699 | release_hlua_function(fcn); |
Aurelien DARRAGON | 55afbed | 2023-03-02 18:42:06 +0100 | [diff] [blame] | 10700 | hlua_unref(L, ref); |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 10701 | ha_free(&akl); |
| 10702 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 10703 | return 0; /* Never reached */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10704 | } |
| 10705 | |
| 10706 | static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px, |
| 10707 | struct act_rule *rule, char **err) |
| 10708 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 10709 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10710 | |
Christopher Faulet | 280f85b | 2019-07-15 15:02:04 +0200 | [diff] [blame] | 10711 | if (px->mode == PR_MODE_HTTP) { |
| 10712 | memprintf(err, "Lua TCP services cannot be used on HTTP proxies"); |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 10713 | return ACT_RET_PRS_ERR; |
| 10714 | } |
| 10715 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10716 | /* Memory for the rule. */ |
| 10717 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 10718 | if (!rule->arg.hlua_rule) { |
| 10719 | memprintf(err, "out of memory error"); |
| 10720 | return ACT_RET_PRS_ERR; |
| 10721 | } |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 10722 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10723 | /* Reference the Lua function and store the reference. */ |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 10724 | rule->arg.hlua_rule->fcn = fcn; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10725 | |
| 10726 | /* TODO: later accept arguments. */ |
| 10727 | rule->arg.hlua_rule->args = NULL; |
| 10728 | |
| 10729 | /* Add applet pointer in the rule. */ |
| 10730 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 10731 | rule->applet.name = fcn->name; |
| 10732 | rule->applet.init = hlua_applet_tcp_init; |
| 10733 | rule->applet.fct = hlua_applet_tcp_fct; |
| 10734 | rule->applet.release = hlua_applet_tcp_release; |
| 10735 | rule->applet.timeout = hlua_timeout_applet; |
| 10736 | |
| 10737 | return 0; |
| 10738 | } |
| 10739 | |
| 10740 | /* This function is an LUA binding used for registering |
| 10741 | * "sample-conv" functions. It expects a converter name used |
| 10742 | * in the haproxy configuration file, and an LUA function. |
| 10743 | */ |
| 10744 | __LJMP static int hlua_register_service(lua_State *L) |
| 10745 | { |
| 10746 | struct action_kw_list *akl; |
| 10747 | const char *name; |
| 10748 | const char *env; |
| 10749 | int ref; |
| 10750 | int len; |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 10751 | struct hlua_function *fcn = NULL; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 10752 | struct buffer *trash; |
| 10753 | struct action_kw *akw; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10754 | |
| 10755 | MAY_LJMP(check_args(L, 3, "register_service")); |
| 10756 | |
Aurelien DARRAGON | 87f5297 | 2023-02-24 09:43:54 +0100 | [diff] [blame] | 10757 | if (hlua_gethlua(L)) { |
| 10758 | /* runtime processing */ |
| 10759 | WILL_LJMP(luaL_error(L, "register_service: not available outside of body context")); |
| 10760 | } |
| 10761 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10762 | /* First argument : converter name. */ |
| 10763 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 10764 | |
| 10765 | /* Second argument : environment. */ |
| 10766 | env = MAY_LJMP(luaL_checkstring(L, 2)); |
| 10767 | |
| 10768 | /* Third argument : lua function. */ |
| 10769 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 10770 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 10771 | /* Check for service already registered */ |
| 10772 | trash = get_trash_chunk(); |
| 10773 | chunk_printf(trash, "lua.%s", name); |
| 10774 | akw = service_find(trash->area); |
| 10775 | if (akw != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 10776 | fcn = akw->private; |
| 10777 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 10778 | ha_warning("Trying to register service 'lua.%s' more than once. " |
| 10779 | "This will become a hard error in version 2.5.\n", name); |
Aurelien DARRAGON | 55afbed | 2023-03-02 18:42:06 +0100 | [diff] [blame] | 10780 | hlua_unref(L, fcn->function_ref[hlua_state_id]); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 10781 | } |
| 10782 | fcn->function_ref[hlua_state_id] = ref; |
| 10783 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 10784 | } |
| 10785 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10786 | /* Allocate and fill the sample fetch keyword struct. */ |
| 10787 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
| 10788 | if (!akl) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 10789 | goto alloc_error; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 10790 | fcn = new_hlua_function(); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10791 | if (!fcn) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 10792 | goto alloc_error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10793 | |
| 10794 | /* Fill fcn. */ |
| 10795 | len = strlen("<lua.>") + strlen(name) + 1; |
| 10796 | fcn->name = calloc(1, len); |
| 10797 | if (!fcn->name) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 10798 | goto alloc_error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10799 | snprintf((char *)fcn->name, len, "<lua.%s>", name); |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 10800 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10801 | |
| 10802 | /* List head */ |
| 10803 | akl->list.n = akl->list.p = NULL; |
| 10804 | |
| 10805 | /* converter keyword. */ |
| 10806 | len = strlen("lua.") + strlen(name) + 1; |
| 10807 | akl->kw[0].kw = calloc(1, len); |
| 10808 | if (!akl->kw[0].kw) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 10809 | goto alloc_error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10810 | |
| 10811 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 10812 | |
Thierry FOURNIER / OZON.IO | 02564fd | 2016-11-12 11:07:05 +0100 | [diff] [blame] | 10813 | /* Check required environment. Only accepted "http" or "tcp". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10814 | if (strcmp(env, "tcp") == 0) |
| 10815 | akl->kw[0].parse = action_register_service_tcp; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 10816 | else if (strcmp(env, "http") == 0) |
| 10817 | akl->kw[0].parse = action_register_service_http; |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 10818 | else { |
| 10819 | release_hlua_function(fcn); |
Aurelien DARRAGON | 55afbed | 2023-03-02 18:42:06 +0100 | [diff] [blame] | 10820 | hlua_unref(L, ref); |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 10821 | if (akl) |
| 10822 | ha_free((char **)&(akl->kw[0].kw)); |
| 10823 | ha_free(&akl); |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 10824 | WILL_LJMP(luaL_error(L, "Lua service environment '%s' is unknown. " |
Eric Salama | fe7456f | 2017-12-21 14:30:07 +0100 | [diff] [blame] | 10825 | "'tcp' or 'http' are expected.", env)); |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 10826 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10827 | |
Amaury Denoyelle | e4a617c | 2021-05-06 15:33:09 +0200 | [diff] [blame] | 10828 | akl->kw[0].flags = 0; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10829 | akl->kw[0].private = fcn; |
| 10830 | |
| 10831 | /* End of array. */ |
| 10832 | memset(&akl->kw[1], 0, sizeof(*akl->kw)); |
| 10833 | |
| 10834 | /* Register this new converter */ |
| 10835 | service_keywords_register(akl); |
| 10836 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 10837 | return 0; |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 10838 | |
| 10839 | alloc_error: |
| 10840 | release_hlua_function(fcn); |
Aurelien DARRAGON | 55afbed | 2023-03-02 18:42:06 +0100 | [diff] [blame] | 10841 | hlua_unref(L, ref); |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 10842 | ha_free(&akl); |
| 10843 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 10844 | return 0; /* Never reached */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 10845 | } |
| 10846 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10847 | /* This function initialises Lua cli handler. It copies the |
| 10848 | * arguments in the Lua stack and create channel IO objects. |
| 10849 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 10850 | 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] | 10851 | { |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 10852 | 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] | 10853 | struct hlua *hlua; |
| 10854 | struct hlua_function *fcn; |
| 10855 | int i; |
| 10856 | const char *error; |
| 10857 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10858 | fcn = private; |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 10859 | ctx->fcn = private; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 10860 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 10861 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 10862 | if (!hlua) { |
| 10863 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 10864 | return 1; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 10865 | } |
| 10866 | HLUA_INIT(hlua); |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 10867 | ctx->hlua = hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10868 | |
| 10869 | /* Create task used by signal to wakeup applets. |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 10870 | * 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] | 10871 | * applet_http. It is absolutely compatible. |
| 10872 | */ |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 10873 | ctx->task = task_new_here(); |
| 10874 | if (!ctx->task) { |
Thierry FOURNIER | ffbf569 | 2016-12-16 11:14:06 +0100 | [diff] [blame] | 10875 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 10876 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10877 | } |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 10878 | ctx->task->nice = 0; |
| 10879 | ctx->task->context = appctx; |
| 10880 | ctx->task->process = hlua_applet_wakeup; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10881 | |
| 10882 | /* Initialises the Lua context */ |
Aurelien DARRAGON | 6b0b9bd | 2023-03-21 14:02:16 +0100 | [diff] [blame] | 10883 | 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] | 10884 | 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] | 10885 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10886 | } |
| 10887 | |
| 10888 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 10889 | if (!SET_SAFE_LJMP(hlua)) { |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10890 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 10891 | error = lua_tostring(hlua->T, -1); |
| 10892 | else |
| 10893 | error = "critical error"; |
| 10894 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error); |
| 10895 | goto error; |
| 10896 | } |
| 10897 | |
| 10898 | /* Check stack available size. */ |
| 10899 | if (!lua_checkstack(hlua->T, 2)) { |
| 10900 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 10901 | goto error; |
| 10902 | } |
| 10903 | |
| 10904 | /* Restore the function in the stack. */ |
Aurelien DARRAGON | 4fdf8b5 | 2023-03-20 17:22:37 +0100 | [diff] [blame] | 10905 | hlua_pushref(hlua->T, fcn->function_ref[hlua->state_id]); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10906 | |
| 10907 | /* Once the arguments parsed, the CLI is like an AppletTCP, |
| 10908 | * so push AppletTCP in the stack. |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10909 | */ |
| 10910 | if (!hlua_applet_tcp_new(hlua->T, appctx)) { |
| 10911 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 10912 | goto error; |
| 10913 | } |
| 10914 | hlua->nargs = 1; |
| 10915 | |
| 10916 | /* push keywords in the stack. */ |
| 10917 | for (i = 0; *args[i]; i++) { |
| 10918 | /* Check stack available size. */ |
| 10919 | if (!lua_checkstack(hlua->T, 1)) { |
| 10920 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 10921 | goto error; |
| 10922 | } |
| 10923 | lua_pushstring(hlua->T, args[i]); |
| 10924 | hlua->nargs++; |
| 10925 | } |
| 10926 | |
| 10927 | /* We must initialize the execution timeouts. */ |
Aurelien DARRAGON | da9503c | 2022-11-25 09:10:07 +0100 | [diff] [blame^] | 10928 | hlua_timer_init(&hlua->timer, hlua_timeout_session); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10929 | |
| 10930 | /* At this point the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 10931 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10932 | |
| 10933 | /* It's ok */ |
| 10934 | return 0; |
| 10935 | |
| 10936 | /* It's not ok. */ |
| 10937 | error: |
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 | hlua_ctx_destroy(hlua); |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 10940 | ctx->hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10941 | return 1; |
| 10942 | } |
| 10943 | |
| 10944 | static int hlua_cli_io_handler_fct(struct appctx *appctx) |
| 10945 | { |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 10946 | struct hlua_cli_ctx *ctx = appctx->svcctx; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10947 | struct hlua *hlua; |
Willy Tarreau | 475e463 | 2022-05-27 10:26:46 +0200 | [diff] [blame] | 10948 | struct stconn *sc; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10949 | struct hlua_function *fcn; |
| 10950 | |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 10951 | hlua = ctx->hlua; |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 10952 | sc = appctx_sc(appctx); |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 10953 | fcn = ctx->fcn; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10954 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10955 | /* Execute the function. */ |
| 10956 | switch (hlua_ctx_resume(hlua, 1)) { |
| 10957 | |
| 10958 | /* finished. */ |
| 10959 | case HLUA_E_OK: |
| 10960 | return 1; |
| 10961 | |
| 10962 | /* yield. */ |
| 10963 | case HLUA_E_AGAIN: |
| 10964 | /* We want write. */ |
| 10965 | if (HLUA_IS_WAKERESWR(hlua)) |
Willy Tarreau | 475e463 | 2022-05-27 10:26:46 +0200 | [diff] [blame] | 10966 | sc_need_room(sc); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10967 | /* Set the timeout. */ |
| 10968 | if (hlua->wake_time != TICK_ETERNITY) |
| 10969 | task_schedule(hlua->task, hlua->wake_time); |
| 10970 | return 0; |
| 10971 | |
| 10972 | /* finished with error. */ |
| 10973 | case HLUA_E_ERRMSG: |
| 10974 | /* Display log. */ |
| 10975 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", |
| 10976 | fcn->name, lua_tostring(hlua->T, -1)); |
| 10977 | lua_pop(hlua->T, 1); |
| 10978 | return 1; |
| 10979 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 10980 | case HLUA_E_ETMOUT: |
| 10981 | SEND_ERR(NULL, "Lua converter '%s': execution timeout.\n", |
| 10982 | fcn->name); |
| 10983 | return 1; |
| 10984 | |
| 10985 | case HLUA_E_NOMEM: |
| 10986 | SEND_ERR(NULL, "Lua converter '%s': out of memory error.\n", |
| 10987 | fcn->name); |
| 10988 | return 1; |
| 10989 | |
| 10990 | case HLUA_E_YIELD: /* unexpected */ |
| 10991 | SEND_ERR(NULL, "Lua converter '%s': yield not allowed.\n", |
| 10992 | fcn->name); |
| 10993 | return 1; |
| 10994 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10995 | case HLUA_E_ERR: |
| 10996 | /* Display log. */ |
| 10997 | SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n", |
| 10998 | fcn->name); |
| 10999 | return 1; |
| 11000 | |
| 11001 | default: |
| 11002 | return 1; |
| 11003 | } |
| 11004 | |
| 11005 | return 1; |
| 11006 | } |
| 11007 | |
| 11008 | static void hlua_cli_io_release_fct(struct appctx *appctx) |
| 11009 | { |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 11010 | struct hlua_cli_ctx *ctx = appctx->svcctx; |
| 11011 | |
| 11012 | hlua_ctx_destroy(ctx->hlua); |
| 11013 | ctx->hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 11014 | } |
| 11015 | |
| 11016 | /* This function is an LUA binding used for registering |
| 11017 | * new keywords in the cli. It expects a list of keywords |
| 11018 | * which are the "path". It is limited to 5 keywords. A |
| 11019 | * description of the command, a function to be executed |
| 11020 | * for the parsing and a function for io handlers. |
| 11021 | */ |
| 11022 | __LJMP static int hlua_register_cli(lua_State *L) |
| 11023 | { |
| 11024 | struct cli_kw_list *cli_kws; |
| 11025 | const char *message; |
| 11026 | int ref_io; |
| 11027 | int len; |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 11028 | struct hlua_function *fcn = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 11029 | int index; |
| 11030 | int i; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 11031 | struct buffer *trash; |
| 11032 | const char *kw[5]; |
| 11033 | struct cli_kw *cli_kw; |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 11034 | const char *errmsg; |
Willy Tarreau | 22450af | 2023-04-07 15:27:55 +0200 | [diff] [blame] | 11035 | char *end; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 11036 | |
| 11037 | MAY_LJMP(check_args(L, 3, "register_cli")); |
| 11038 | |
Aurelien DARRAGON | 87f5297 | 2023-02-24 09:43:54 +0100 | [diff] [blame] | 11039 | if (hlua_gethlua(L)) { |
| 11040 | /* runtime processing */ |
| 11041 | WILL_LJMP(luaL_error(L, "register_cli: not available outside of body context")); |
| 11042 | } |
| 11043 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 11044 | /* First argument : an array of maximum 5 keywords. */ |
| 11045 | if (!lua_istable(L, 1)) |
| 11046 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table")); |
| 11047 | |
| 11048 | /* Second argument : string with contextual message. */ |
| 11049 | message = MAY_LJMP(luaL_checkstring(L, 2)); |
| 11050 | |
| 11051 | /* Third and fourth argument : lua function. */ |
| 11052 | ref_io = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 11053 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 11054 | /* Check for CLI service already registered */ |
| 11055 | trash = get_trash_chunk(); |
| 11056 | index = 0; |
| 11057 | lua_pushnil(L); |
| 11058 | memset(kw, 0, sizeof(kw)); |
| 11059 | while (lua_next(L, 1) != 0) { |
Aurelien DARRAGON | 55afbed | 2023-03-02 18:42:06 +0100 | [diff] [blame] | 11060 | if (index >= CLI_PREFIX_KW_NB) { |
| 11061 | hlua_unref(L, ref_io); |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 11062 | 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] | 11063 | } |
| 11064 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 11065 | hlua_unref(L, ref_io); |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 11066 | 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] | 11067 | } |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 11068 | kw[index] = lua_tostring(L, -1); |
| 11069 | if (index == 0) |
| 11070 | chunk_printf(trash, "%s", kw[index]); |
| 11071 | else |
| 11072 | chunk_appendf(trash, " %s", kw[index]); |
| 11073 | index++; |
| 11074 | lua_pop(L, 1); |
| 11075 | } |
| 11076 | cli_kw = cli_find_kw_exact((char **)kw); |
| 11077 | if (cli_kw != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11078 | fcn = cli_kw->private; |
| 11079 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 11080 | ha_warning("Trying to register CLI keyword 'lua.%s' more than once. " |
| 11081 | "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] | 11082 | hlua_unref(L, fcn->function_ref[hlua_state_id]); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11083 | } |
| 11084 | fcn->function_ref[hlua_state_id] = ref_io; |
| 11085 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 11086 | } |
| 11087 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 11088 | /* Allocate and fill the sample fetch keyword struct. */ |
| 11089 | cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 11090 | if (!cli_kws) { |
| 11091 | errmsg = "Lua out of memory error."; |
| 11092 | goto error; |
| 11093 | } |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 11094 | fcn = new_hlua_function(); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 11095 | if (!fcn) { |
| 11096 | errmsg = "Lua out of memory error."; |
| 11097 | goto error; |
| 11098 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 11099 | |
| 11100 | /* Fill path. */ |
| 11101 | index = 0; |
| 11102 | lua_pushnil(L); |
| 11103 | while(lua_next(L, 1) != 0) { |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 11104 | if (index >= 5) { |
| 11105 | errmsg = "1st argument must be a table with a maximum of 5 entries"; |
| 11106 | goto error; |
| 11107 | } |
| 11108 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 11109 | errmsg = "1st argument must be a table filled with strings"; |
| 11110 | goto error; |
| 11111 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 11112 | cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1)); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 11113 | if (!cli_kws->kw[0].str_kw[index]) { |
| 11114 | errmsg = "Lua out of memory error."; |
| 11115 | goto error; |
| 11116 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 11117 | index++; |
| 11118 | lua_pop(L, 1); |
| 11119 | } |
| 11120 | |
| 11121 | /* Copy help message. */ |
| 11122 | cli_kws->kw[0].usage = strdup(message); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 11123 | if (!cli_kws->kw[0].usage) { |
| 11124 | errmsg = "Lua out of memory error."; |
| 11125 | goto error; |
| 11126 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 11127 | |
| 11128 | /* Fill fcn io handler. */ |
| 11129 | len = strlen("<lua.cli>") + 1; |
| 11130 | for (i = 0; i < index; i++) |
| 11131 | len += strlen(cli_kws->kw[0].str_kw[i]) + 1; |
| 11132 | fcn->name = calloc(1, len); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 11133 | if (!fcn->name) { |
| 11134 | errmsg = "Lua out of memory error."; |
| 11135 | goto error; |
| 11136 | } |
Willy Tarreau | 22450af | 2023-04-07 15:27:55 +0200 | [diff] [blame] | 11137 | |
| 11138 | end = fcn->name; |
| 11139 | len = 8; |
| 11140 | memcpy(end, "<lua.cli", len); |
| 11141 | end += len; |
| 11142 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 11143 | for (i = 0; i < index; i++) { |
Willy Tarreau | 22450af | 2023-04-07 15:27:55 +0200 | [diff] [blame] | 11144 | *(end++) = '.'; |
| 11145 | len = strlen(cli_kws->kw[0].str_kw[i]); |
| 11146 | memcpy(end, cli_kws->kw[0].str_kw[i], len); |
| 11147 | end += len; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 11148 | } |
Willy Tarreau | 22450af | 2023-04-07 15:27:55 +0200 | [diff] [blame] | 11149 | *(end++) = '>'; |
| 11150 | *(end++) = 0; |
| 11151 | |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 11152 | fcn->function_ref[hlua_state_id] = ref_io; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 11153 | |
| 11154 | /* Fill last entries. */ |
| 11155 | cli_kws->kw[0].private = fcn; |
| 11156 | cli_kws->kw[0].parse = hlua_cli_parse_fct; |
| 11157 | cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct; |
| 11158 | cli_kws->kw[0].io_release = hlua_cli_io_release_fct; |
| 11159 | |
| 11160 | /* Register this new converter */ |
| 11161 | cli_register_kw(cli_kws); |
| 11162 | |
| 11163 | return 0; |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 11164 | |
| 11165 | error: |
| 11166 | release_hlua_function(fcn); |
Aurelien DARRAGON | 55afbed | 2023-03-02 18:42:06 +0100 | [diff] [blame] | 11167 | hlua_unref(L, ref_io); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 11168 | if (cli_kws) { |
| 11169 | for (i = 0; i < index; i++) |
| 11170 | ha_free((char **)&(cli_kws->kw[0].str_kw[i])); |
| 11171 | ha_free((char **)&(cli_kws->kw[0].usage)); |
| 11172 | } |
| 11173 | ha_free(&cli_kws); |
| 11174 | WILL_LJMP(luaL_error(L, errmsg)); |
| 11175 | return 0; /* Never reached */ |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 11176 | } |
| 11177 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11178 | static int hlua_filter_init_per_thread(struct proxy *px, struct flt_conf *fconf) |
| 11179 | { |
| 11180 | struct hlua_flt_config *conf = fconf->conf; |
| 11181 | lua_State *L; |
| 11182 | int error, pos, state_id, flt_ref; |
| 11183 | |
| 11184 | state_id = reg_flt_to_stack_id(conf->reg); |
| 11185 | L = hlua_states[state_id]; |
| 11186 | pos = lua_gettop(L); |
| 11187 | |
| 11188 | /* The filter parsing function */ |
Aurelien DARRAGON | 4fdf8b5 | 2023-03-20 17:22:37 +0100 | [diff] [blame] | 11189 | hlua_pushref(L, conf->reg->fun_ref[state_id]); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11190 | |
| 11191 | /* Push the filter class on the stack and resolve all callbacks */ |
Aurelien DARRAGON | 4fdf8b5 | 2023-03-20 17:22:37 +0100 | [diff] [blame] | 11192 | hlua_pushref(L, conf->reg->flt_ref[state_id]); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11193 | |
| 11194 | /* Duplicate the filter class so each filter will have its own copy */ |
| 11195 | lua_newtable(L); |
| 11196 | lua_pushnil(L); |
| 11197 | |
| 11198 | while (lua_next(L, pos+2)) { |
| 11199 | lua_pushvalue(L, -2); |
| 11200 | lua_insert(L, -2); |
| 11201 | lua_settable(L, -4); |
| 11202 | } |
Aurelien DARRAGON | 73d1a98 | 2023-03-20 17:42:08 +0100 | [diff] [blame] | 11203 | flt_ref = hlua_ref(L); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11204 | |
| 11205 | /* Remove the original lua filter class from the stack */ |
| 11206 | lua_pop(L, 1); |
| 11207 | |
| 11208 | /* Push the copy on the stack */ |
Aurelien DARRAGON | 4fdf8b5 | 2023-03-20 17:22:37 +0100 | [diff] [blame] | 11209 | hlua_pushref(L, flt_ref); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11210 | |
| 11211 | /* extra args are pushed in a table */ |
| 11212 | lua_newtable(L); |
| 11213 | for (pos = 0; conf->args[pos]; pos++) { |
| 11214 | /* Check stack available size. */ |
| 11215 | if (!lua_checkstack(L, 1)) { |
| 11216 | ha_alert("Lua filter '%s' : Lua error : full stack.", conf->reg->name); |
| 11217 | goto error; |
| 11218 | } |
| 11219 | lua_pushstring(L, conf->args[pos]); |
| 11220 | lua_rawseti(L, -2, lua_rawlen(L, -2) + 1); |
| 11221 | } |
| 11222 | |
| 11223 | error = lua_pcall(L, 2, LUA_MULTRET, 0); |
| 11224 | switch (error) { |
| 11225 | case LUA_OK: |
| 11226 | /* replace the filter ref */ |
| 11227 | conf->ref[state_id] = flt_ref; |
| 11228 | break; |
| 11229 | case LUA_ERRRUN: |
| 11230 | ha_alert("Lua filter '%s' : runtime error : %s", conf->reg->name, lua_tostring(L, -1)); |
| 11231 | goto error; |
| 11232 | case LUA_ERRMEM: |
| 11233 | ha_alert("Lua filter '%s' : out of memory error", conf->reg->name); |
| 11234 | goto error; |
| 11235 | case LUA_ERRERR: |
| 11236 | ha_alert("Lua filter '%s' : message handler error : %s", conf->reg->name, lua_tostring(L, -1)); |
| 11237 | goto error; |
| 11238 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM <= 503 |
| 11239 | case LUA_ERRGCMM: |
| 11240 | ha_alert("Lua filter '%s' : garbage collector error : %s", conf->reg->name, lua_tostring(L, -1)); |
| 11241 | goto error; |
| 11242 | #endif |
| 11243 | default: |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 11244 | 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] | 11245 | goto error; |
| 11246 | } |
| 11247 | |
| 11248 | lua_settop(L, 0); |
| 11249 | return 0; |
| 11250 | |
| 11251 | error: |
| 11252 | lua_settop(L, 0); |
| 11253 | return -1; |
| 11254 | } |
| 11255 | |
| 11256 | static void hlua_filter_deinit_per_thread(struct proxy *px, struct flt_conf *fconf) |
| 11257 | { |
| 11258 | struct hlua_flt_config *conf = fconf->conf; |
| 11259 | lua_State *L; |
| 11260 | int state_id; |
| 11261 | |
| 11262 | if (!conf) |
| 11263 | return; |
| 11264 | |
| 11265 | state_id = reg_flt_to_stack_id(conf->reg); |
| 11266 | L = hlua_states[state_id]; |
Aurelien DARRAGON | fde199d | 2023-03-20 15:09:33 +0100 | [diff] [blame] | 11267 | hlua_unref(L, conf->ref[state_id]); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11268 | } |
| 11269 | |
| 11270 | static int hlua_filter_init(struct proxy *px, struct flt_conf *fconf) |
| 11271 | { |
| 11272 | struct hlua_flt_config *conf = fconf->conf; |
| 11273 | int state_id = reg_flt_to_stack_id(conf->reg); |
| 11274 | |
| 11275 | /* Rely on per-thread init for global scripts */ |
| 11276 | if (!state_id) |
| 11277 | return hlua_filter_init_per_thread(px, fconf); |
| 11278 | return 0; |
| 11279 | } |
| 11280 | |
| 11281 | static void hlua_filter_deinit(struct proxy *px, struct flt_conf *fconf) |
| 11282 | { |
| 11283 | |
| 11284 | if (fconf->conf) { |
| 11285 | struct hlua_flt_config *conf = fconf->conf; |
| 11286 | int state_id = reg_flt_to_stack_id(conf->reg); |
| 11287 | int pos; |
| 11288 | |
| 11289 | /* Rely on per-thread deinit for global scripts */ |
| 11290 | if (!state_id) |
| 11291 | hlua_filter_deinit_per_thread(px, fconf); |
| 11292 | |
| 11293 | for (pos = 0; conf->args[pos]; pos++) |
| 11294 | free(conf->args[pos]); |
| 11295 | free(conf->args); |
| 11296 | } |
| 11297 | ha_free(&fconf->conf); |
| 11298 | ha_free((char **)&fconf->id); |
| 11299 | ha_free(&fconf->ops); |
| 11300 | } |
| 11301 | |
| 11302 | static int hlua_filter_new(struct stream *s, struct filter *filter) |
| 11303 | { |
| 11304 | struct hlua_flt_config *conf = FLT_CONF(filter); |
| 11305 | struct hlua_flt_ctx *flt_ctx = NULL; |
| 11306 | int ret = 1; |
| 11307 | |
| 11308 | /* In the execution wrappers linked with a stream, the |
| 11309 | * Lua context can be not initialized. This behavior |
| 11310 | * permits to save performances because a systematic |
| 11311 | * Lua initialization cause 5% performances loss. |
| 11312 | */ |
| 11313 | if (!s->hlua) { |
| 11314 | struct hlua *hlua; |
| 11315 | |
| 11316 | hlua = pool_alloc(pool_head_hlua); |
| 11317 | if (!hlua) { |
| 11318 | SEND_ERR(s->be, "Lua filter '%s': can't initialize Lua context.\n", |
| 11319 | conf->reg->name); |
| 11320 | ret = 0; |
| 11321 | goto end; |
| 11322 | } |
| 11323 | HLUA_INIT(hlua); |
| 11324 | s->hlua = hlua; |
Aurelien DARRAGON | 6b0b9bd | 2023-03-21 14:02:16 +0100 | [diff] [blame] | 11325 | 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] | 11326 | SEND_ERR(s->be, "Lua filter '%s': can't initialize Lua context.\n", |
| 11327 | conf->reg->name); |
| 11328 | ret = 0; |
| 11329 | goto end; |
| 11330 | } |
| 11331 | } |
| 11332 | |
| 11333 | flt_ctx = pool_zalloc(pool_head_hlua_flt_ctx); |
| 11334 | if (!flt_ctx) { |
| 11335 | SEND_ERR(s->be, "Lua filter '%s': can't initialize filter Lua context.\n", |
| 11336 | conf->reg->name); |
| 11337 | ret = 0; |
| 11338 | goto end; |
| 11339 | } |
| 11340 | flt_ctx->hlua[0] = pool_alloc(pool_head_hlua); |
| 11341 | flt_ctx->hlua[1] = pool_alloc(pool_head_hlua); |
| 11342 | if (!flt_ctx->hlua[0] || !flt_ctx->hlua[1]) { |
| 11343 | SEND_ERR(s->be, "Lua filter '%s': can't initialize filter Lua context.\n", |
| 11344 | conf->reg->name); |
| 11345 | ret = 0; |
| 11346 | goto end; |
| 11347 | } |
| 11348 | HLUA_INIT(flt_ctx->hlua[0]); |
| 11349 | HLUA_INIT(flt_ctx->hlua[1]); |
Aurelien DARRAGON | 6b0b9bd | 2023-03-21 14:02:16 +0100 | [diff] [blame] | 11350 | if (!hlua_ctx_init(flt_ctx->hlua[0], reg_flt_to_stack_id(conf->reg), s->task) || |
| 11351 | !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] | 11352 | SEND_ERR(s->be, "Lua filter '%s': can't initialize filter Lua context.\n", |
| 11353 | conf->reg->name); |
| 11354 | ret = 0; |
| 11355 | goto end; |
| 11356 | } |
| 11357 | |
| 11358 | if (!HLUA_IS_RUNNING(s->hlua)) { |
| 11359 | /* The following Lua calls can fail. */ |
| 11360 | if (!SET_SAFE_LJMP(s->hlua)) { |
| 11361 | const char *error; |
| 11362 | |
| 11363 | if (lua_type(s->hlua->T, -1) == LUA_TSTRING) |
| 11364 | error = lua_tostring(s->hlua->T, -1); |
| 11365 | else |
| 11366 | error = "critical error"; |
| 11367 | SEND_ERR(s->be, "Lua filter '%s': %s.\n", conf->reg->name, error); |
| 11368 | ret = 0; |
| 11369 | goto end; |
| 11370 | } |
| 11371 | |
| 11372 | /* Check stack size. */ |
| 11373 | if (!lua_checkstack(s->hlua->T, 1)) { |
| 11374 | 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] | 11375 | RESET_SAFE_LJMP(s->hlua); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11376 | ret = 0; |
| 11377 | goto end; |
| 11378 | } |
| 11379 | |
Aurelien DARRAGON | 4fdf8b5 | 2023-03-20 17:22:37 +0100 | [diff] [blame] | 11380 | hlua_pushref(s->hlua->T, conf->ref[s->hlua->state_id]); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11381 | if (lua_getfield(s->hlua->T, -1, "new") != LUA_TFUNCTION) { |
| 11382 | SEND_ERR(s->be, "Lua filter '%s': 'new' field is not a function.\n", |
| 11383 | conf->reg->name); |
| 11384 | RESET_SAFE_LJMP(s->hlua); |
| 11385 | ret = 0; |
| 11386 | goto end; |
| 11387 | } |
| 11388 | lua_insert(s->hlua->T, -2); |
| 11389 | |
| 11390 | /* Push the copy on the stack */ |
| 11391 | s->hlua->nargs = 1; |
| 11392 | |
| 11393 | /* We must initialize the execution timeouts. */ |
Aurelien DARRAGON | da9503c | 2022-11-25 09:10:07 +0100 | [diff] [blame^] | 11394 | hlua_timer_init(&s->hlua->timer, hlua_timeout_session); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11395 | |
| 11396 | /* At this point the execution is safe. */ |
| 11397 | RESET_SAFE_LJMP(s->hlua); |
| 11398 | } |
| 11399 | |
| 11400 | switch (hlua_ctx_resume(s->hlua, 0)) { |
| 11401 | case HLUA_E_OK: |
| 11402 | /* Nothing returned or not a table, ignore the filter for current stream */ |
| 11403 | if (!lua_gettop(s->hlua->T) || !lua_istable(s->hlua->T, 1)) { |
| 11404 | ret = 0; |
| 11405 | goto end; |
| 11406 | } |
| 11407 | |
| 11408 | /* Attached the filter pointer to the ctx */ |
| 11409 | lua_pushstring(s->hlua->T, "__filter"); |
| 11410 | lua_pushlightuserdata(s->hlua->T, filter); |
| 11411 | lua_settable(s->hlua->T, -3); |
| 11412 | |
| 11413 | /* Save a ref on the filter ctx */ |
| 11414 | lua_pushvalue(s->hlua->T, 1); |
Aurelien DARRAGON | 73d1a98 | 2023-03-20 17:42:08 +0100 | [diff] [blame] | 11415 | flt_ctx->ref = hlua_ref(s->hlua->T); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11416 | filter->ctx = flt_ctx; |
| 11417 | break; |
| 11418 | case HLUA_E_ERRMSG: |
| 11419 | SEND_ERR(s->be, "Lua filter '%s' : %s.\n", conf->reg->name, lua_tostring(s->hlua->T, -1)); |
| 11420 | ret = -1; |
| 11421 | goto end; |
| 11422 | case HLUA_E_ETMOUT: |
| 11423 | SEND_ERR(s->be, "Lua filter '%s' : 'new' execution timeout.\n", conf->reg->name); |
| 11424 | ret = 0; |
| 11425 | goto end; |
| 11426 | case HLUA_E_NOMEM: |
| 11427 | SEND_ERR(s->be, "Lua filter '%s' : out of memory error.\n", conf->reg->name); |
| 11428 | ret = 0; |
| 11429 | goto end; |
| 11430 | case HLUA_E_AGAIN: |
| 11431 | case HLUA_E_YIELD: |
| 11432 | SEND_ERR(s->be, "Lua filter '%s': yield functions like core.tcp() or core.sleep()" |
| 11433 | " are not allowed from 'new' function.\n", conf->reg->name); |
| 11434 | ret = 0; |
| 11435 | goto end; |
| 11436 | case HLUA_E_ERR: |
| 11437 | SEND_ERR(s->be, "Lua filter '%s': 'new' returns an unknown error.\n", conf->reg->name); |
| 11438 | ret = 0; |
| 11439 | goto end; |
| 11440 | default: |
| 11441 | ret = 0; |
| 11442 | goto end; |
| 11443 | } |
| 11444 | |
| 11445 | end: |
| 11446 | if (s->hlua) |
| 11447 | lua_settop(s->hlua->T, 0); |
| 11448 | if (ret <= 0) { |
| 11449 | if (flt_ctx) { |
| 11450 | hlua_ctx_destroy(flt_ctx->hlua[0]); |
| 11451 | hlua_ctx_destroy(flt_ctx->hlua[1]); |
| 11452 | pool_free(pool_head_hlua_flt_ctx, flt_ctx); |
| 11453 | } |
| 11454 | } |
| 11455 | return ret; |
| 11456 | } |
| 11457 | |
| 11458 | static void hlua_filter_delete(struct stream *s, struct filter *filter) |
| 11459 | { |
| 11460 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 11461 | |
Aurelien DARRAGON | fde199d | 2023-03-20 15:09:33 +0100 | [diff] [blame] | 11462 | hlua_unref(s->hlua->T, flt_ctx->ref); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11463 | hlua_ctx_destroy(flt_ctx->hlua[0]); |
| 11464 | hlua_ctx_destroy(flt_ctx->hlua[1]); |
| 11465 | pool_free(pool_head_hlua_flt_ctx, flt_ctx); |
| 11466 | filter->ctx = NULL; |
| 11467 | } |
| 11468 | |
Christopher Faulet | 9f55a50 | 2020-02-25 15:21:02 +0100 | [diff] [blame] | 11469 | static int hlua_filter_from_payload(struct filter *filter) |
| 11470 | { |
| 11471 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 11472 | |
| 11473 | return (flt_ctx && !!(flt_ctx->flags & HLUA_FLT_CTX_FL_PAYLOAD)); |
| 11474 | } |
| 11475 | |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 11476 | static int hlua_filter_callback(struct stream *s, struct filter *filter, const char *fun, |
| 11477 | int dir, unsigned int flags) |
| 11478 | { |
| 11479 | struct hlua *flt_hlua; |
| 11480 | struct hlua_flt_config *conf = FLT_CONF(filter); |
| 11481 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 11482 | unsigned int hflags = HLUA_TXN_FLT_CTX; |
| 11483 | int ret = 1; |
| 11484 | |
| 11485 | flt_hlua = flt_ctx->hlua[(dir == SMP_OPT_DIR_REQ ? 0 : 1)]; |
| 11486 | if (!flt_hlua) |
| 11487 | goto end; |
| 11488 | |
| 11489 | if (!HLUA_IS_RUNNING(flt_hlua)) { |
| 11490 | int extra_idx = lua_gettop(flt_hlua->T); |
| 11491 | |
| 11492 | /* The following Lua calls can fail. */ |
| 11493 | if (!SET_SAFE_LJMP(flt_hlua)) { |
| 11494 | const char *error; |
| 11495 | |
| 11496 | if (lua_type(flt_hlua->T, -1) == LUA_TSTRING) |
| 11497 | error = lua_tostring(flt_hlua->T, -1); |
| 11498 | else |
| 11499 | error = "critical error"; |
| 11500 | SEND_ERR(s->be, "Lua filter '%s': %s.\n", conf->reg->name, error); |
| 11501 | goto end; |
| 11502 | } |
| 11503 | |
| 11504 | /* Check stack size. */ |
| 11505 | if (!lua_checkstack(flt_hlua->T, 3)) { |
| 11506 | SEND_ERR(s->be, "Lua filter '%s': full stack.\n", conf->reg->name); |
| 11507 | RESET_SAFE_LJMP(flt_hlua); |
| 11508 | goto end; |
| 11509 | } |
| 11510 | |
Aurelien DARRAGON | 4fdf8b5 | 2023-03-20 17:22:37 +0100 | [diff] [blame] | 11511 | hlua_pushref(flt_hlua->T, flt_ctx->ref); |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 11512 | if (lua_getfield(flt_hlua->T, -1, fun) != LUA_TFUNCTION) { |
| 11513 | RESET_SAFE_LJMP(flt_hlua); |
| 11514 | goto end; |
| 11515 | } |
| 11516 | lua_insert(flt_hlua->T, -2); |
| 11517 | |
| 11518 | if (!hlua_txn_new(flt_hlua->T, s, s->be, dir, hflags)) { |
| 11519 | SEND_ERR(s->be, "Lua filter '%s': full stack.\n", conf->reg->name); |
| 11520 | RESET_SAFE_LJMP(flt_hlua); |
| 11521 | goto end; |
| 11522 | } |
| 11523 | flt_hlua->nargs = 2; |
| 11524 | |
| 11525 | if (flags & HLUA_FLT_CB_ARG_CHN) { |
| 11526 | if (dir == SMP_OPT_DIR_REQ) |
| 11527 | lua_getfield(flt_hlua->T, -1, "req"); |
| 11528 | else |
| 11529 | lua_getfield(flt_hlua->T, -1, "res"); |
| 11530 | if (lua_type(flt_hlua->T, -1) == LUA_TTABLE) { |
| 11531 | lua_pushstring(flt_hlua->T, "__filter"); |
| 11532 | lua_pushlightuserdata(flt_hlua->T, filter); |
| 11533 | lua_settable(flt_hlua->T, -3); |
| 11534 | } |
| 11535 | flt_hlua->nargs++; |
| 11536 | } |
| 11537 | else if (flags & HLUA_FLT_CB_ARG_HTTP_MSG) { |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 11538 | if (dir == SMP_OPT_DIR_REQ) |
| 11539 | lua_getfield(flt_hlua->T, -1, "http_req"); |
| 11540 | else |
| 11541 | lua_getfield(flt_hlua->T, -1, "http_res"); |
| 11542 | if (lua_type(flt_hlua->T, -1) == LUA_TTABLE) { |
| 11543 | lua_pushstring(flt_hlua->T, "__filter"); |
| 11544 | lua_pushlightuserdata(flt_hlua->T, filter); |
| 11545 | lua_settable(flt_hlua->T, -3); |
| 11546 | } |
| 11547 | flt_hlua->nargs++; |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 11548 | } |
| 11549 | |
| 11550 | /* Check stack size. */ |
| 11551 | if (!lua_checkstack(flt_hlua->T, 1)) { |
| 11552 | SEND_ERR(s->be, "Lua filter '%s': full stack.\n", conf->reg->name); |
| 11553 | RESET_SAFE_LJMP(flt_hlua); |
| 11554 | goto end; |
| 11555 | } |
| 11556 | |
| 11557 | while (extra_idx--) { |
| 11558 | lua_pushvalue(flt_hlua->T, 1); |
| 11559 | lua_remove(flt_hlua->T, 1); |
| 11560 | flt_hlua->nargs++; |
| 11561 | } |
| 11562 | |
| 11563 | /* We must initialize the execution timeouts. */ |
Aurelien DARRAGON | da9503c | 2022-11-25 09:10:07 +0100 | [diff] [blame^] | 11564 | hlua_timer_init(&flt_hlua->timer, hlua_timeout_session); |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 11565 | |
| 11566 | /* At this point the execution is safe. */ |
| 11567 | RESET_SAFE_LJMP(flt_hlua); |
| 11568 | } |
| 11569 | |
| 11570 | switch (hlua_ctx_resume(flt_hlua, !(flags & HLUA_FLT_CB_FINAL))) { |
| 11571 | case HLUA_E_OK: |
| 11572 | /* Catch the return value if it required */ |
| 11573 | if ((flags & HLUA_FLT_CB_RETVAL) && lua_gettop(flt_hlua->T) > 0) { |
| 11574 | ret = lua_tointeger(flt_hlua->T, -1); |
| 11575 | lua_settop(flt_hlua->T, 0); /* Empty the stack. */ |
| 11576 | } |
| 11577 | |
| 11578 | /* Set timeout in the required channel. */ |
| 11579 | if (flt_hlua->wake_time != TICK_ETERNITY) { |
| 11580 | if (dir == SMP_OPT_DIR_REQ) |
| 11581 | s->req.analyse_exp = flt_hlua->wake_time; |
| 11582 | else |
| 11583 | s->res.analyse_exp = flt_hlua->wake_time; |
| 11584 | } |
| 11585 | break; |
| 11586 | case HLUA_E_AGAIN: |
| 11587 | /* Set timeout in the required channel. */ |
| 11588 | if (flt_hlua->wake_time != TICK_ETERNITY) { |
| 11589 | if (dir == SMP_OPT_DIR_REQ) |
| 11590 | s->req.analyse_exp = flt_hlua->wake_time; |
| 11591 | else |
| 11592 | s->res.analyse_exp = flt_hlua->wake_time; |
| 11593 | } |
| 11594 | /* Some actions can be wake up when a "write" event |
| 11595 | * is detected on a response channel. This is useful |
| 11596 | * only for actions targeted on the requests. |
| 11597 | */ |
| 11598 | if (HLUA_IS_WAKERESWR(flt_hlua)) |
| 11599 | s->res.flags |= CF_WAKE_WRITE; |
| 11600 | if (HLUA_IS_WAKEREQWR(flt_hlua)) |
| 11601 | s->req.flags |= CF_WAKE_WRITE; |
| 11602 | ret = 0; |
| 11603 | goto end; |
| 11604 | case HLUA_E_ERRMSG: |
| 11605 | SEND_ERR(s->be, "Lua filter '%s' : %s.\n", conf->reg->name, lua_tostring(flt_hlua->T, -1)); |
| 11606 | ret = -1; |
| 11607 | goto end; |
| 11608 | case HLUA_E_ETMOUT: |
| 11609 | SEND_ERR(s->be, "Lua filter '%s' : '%s' callback execution timeout.\n", conf->reg->name, fun); |
| 11610 | goto end; |
| 11611 | case HLUA_E_NOMEM: |
| 11612 | SEND_ERR(s->be, "Lua filter '%s' : out of memory error.\n", conf->reg->name); |
| 11613 | goto end; |
| 11614 | case HLUA_E_YIELD: |
| 11615 | SEND_ERR(s->be, "Lua filter '%s': yield functions like core.tcp() or core.sleep()" |
| 11616 | " are not allowed from '%s' callback.\n", conf->reg->name, fun); |
| 11617 | goto end; |
| 11618 | case HLUA_E_ERR: |
| 11619 | SEND_ERR(s->be, "Lua filter '%s': '%s' returns an unknown error.\n", conf->reg->name, fun); |
| 11620 | goto end; |
| 11621 | default: |
| 11622 | goto end; |
| 11623 | } |
| 11624 | |
| 11625 | |
| 11626 | end: |
| 11627 | return ret; |
| 11628 | } |
| 11629 | |
| 11630 | static int hlua_filter_start_analyze(struct stream *s, struct filter *filter, struct channel *chn) |
| 11631 | { |
| 11632 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 11633 | |
| 11634 | flt_ctx->flags = 0; |
| 11635 | return hlua_filter_callback(s, filter, "start_analyze", |
| 11636 | (!(chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES), |
| 11637 | (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_RETVAL | HLUA_FLT_CB_ARG_CHN)); |
| 11638 | } |
| 11639 | |
| 11640 | static int hlua_filter_end_analyze(struct stream *s, struct filter *filter, struct channel *chn) |
| 11641 | { |
| 11642 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 11643 | |
| 11644 | flt_ctx->flags &= ~HLUA_FLT_CTX_FL_PAYLOAD; |
| 11645 | return hlua_filter_callback(s, filter, "end_analyze", |
| 11646 | (!(chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES), |
| 11647 | (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_RETVAL | HLUA_FLT_CB_ARG_CHN)); |
| 11648 | } |
| 11649 | |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 11650 | static int hlua_filter_http_headers(struct stream *s, struct filter *filter, struct http_msg *msg) |
| 11651 | { |
| 11652 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 11653 | |
| 11654 | flt_ctx->flags &= ~HLUA_FLT_CTX_FL_PAYLOAD; |
| 11655 | return hlua_filter_callback(s, filter, "http_headers", |
| 11656 | (!(msg->chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES), |
| 11657 | (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_RETVAL | HLUA_FLT_CB_ARG_HTTP_MSG)); |
| 11658 | } |
| 11659 | |
| 11660 | static int hlua_filter_http_payload(struct stream *s, struct filter *filter, struct http_msg *msg, |
| 11661 | unsigned int offset, unsigned int len) |
| 11662 | { |
| 11663 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 11664 | struct hlua *flt_hlua; |
| 11665 | int dir = (!(msg->chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES); |
| 11666 | int idx = (dir == SMP_OPT_DIR_REQ ? 0 : 1); |
| 11667 | int ret; |
| 11668 | |
| 11669 | flt_hlua = flt_ctx->hlua[idx]; |
| 11670 | flt_ctx->cur_off[idx] = offset; |
| 11671 | flt_ctx->cur_len[idx] = len; |
| 11672 | flt_ctx->flags |= HLUA_FLT_CTX_FL_PAYLOAD; |
| 11673 | ret = hlua_filter_callback(s, filter, "http_payload", dir, (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_ARG_HTTP_MSG)); |
| 11674 | if (ret != -1) { |
| 11675 | ret = flt_ctx->cur_len[idx]; |
| 11676 | if (lua_gettop(flt_hlua->T) > 0) { |
| 11677 | ret = lua_tointeger(flt_hlua->T, -1); |
| 11678 | if (ret > flt_ctx->cur_len[idx]) |
| 11679 | ret = flt_ctx->cur_len[idx]; |
| 11680 | lua_settop(flt_hlua->T, 0); /* Empty the stack. */ |
| 11681 | } |
| 11682 | } |
| 11683 | return ret; |
| 11684 | } |
| 11685 | |
| 11686 | static int hlua_filter_http_end(struct stream *s, struct filter *filter, struct http_msg *msg) |
| 11687 | { |
| 11688 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 11689 | |
| 11690 | flt_ctx->flags &= ~HLUA_FLT_CTX_FL_PAYLOAD; |
| 11691 | return hlua_filter_callback(s, filter, "http_end", |
| 11692 | (!(msg->chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES), |
| 11693 | (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_RETVAL | HLUA_FLT_CB_ARG_HTTP_MSG)); |
| 11694 | } |
| 11695 | |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 11696 | static int hlua_filter_tcp_payload(struct stream *s, struct filter *filter, struct channel *chn, |
| 11697 | unsigned int offset, unsigned int len) |
| 11698 | { |
| 11699 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 11700 | struct hlua *flt_hlua; |
| 11701 | int dir = (!(chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES); |
| 11702 | int idx = (dir == SMP_OPT_DIR_REQ ? 0 : 1); |
| 11703 | int ret; |
| 11704 | |
| 11705 | flt_hlua = flt_ctx->hlua[idx]; |
| 11706 | flt_ctx->cur_off[idx] = offset; |
| 11707 | flt_ctx->cur_len[idx] = len; |
| 11708 | flt_ctx->flags |= HLUA_FLT_CTX_FL_PAYLOAD; |
| 11709 | ret = hlua_filter_callback(s, filter, "tcp_payload", dir, (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_ARG_CHN)); |
| 11710 | if (ret != -1) { |
| 11711 | ret = flt_ctx->cur_len[idx]; |
| 11712 | if (lua_gettop(flt_hlua->T) > 0) { |
| 11713 | ret = lua_tointeger(flt_hlua->T, -1); |
| 11714 | if (ret > flt_ctx->cur_len[idx]) |
| 11715 | ret = flt_ctx->cur_len[idx]; |
| 11716 | lua_settop(flt_hlua->T, 0); /* Empty the stack. */ |
| 11717 | } |
| 11718 | } |
| 11719 | return ret; |
| 11720 | } |
| 11721 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11722 | static int hlua_filter_parse_fct(char **args, int *cur_arg, struct proxy *px, |
| 11723 | struct flt_conf *fconf, char **err, void *private) |
| 11724 | { |
| 11725 | struct hlua_reg_filter *reg_flt = private; |
| 11726 | lua_State *L; |
| 11727 | struct hlua_flt_config *conf = NULL; |
| 11728 | const char *flt_id = NULL; |
| 11729 | int state_id, pos, flt_flags = 0; |
| 11730 | struct flt_ops *hlua_flt_ops = NULL; |
| 11731 | |
| 11732 | state_id = reg_flt_to_stack_id(reg_flt); |
| 11733 | L = hlua_states[state_id]; |
| 11734 | |
| 11735 | /* Initialize the filter ops with default callbacks */ |
| 11736 | hlua_flt_ops = calloc(1, sizeof(*hlua_flt_ops)); |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 11737 | if (!hlua_flt_ops) |
| 11738 | goto error; |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11739 | hlua_flt_ops->init = hlua_filter_init; |
| 11740 | hlua_flt_ops->deinit = hlua_filter_deinit; |
| 11741 | if (state_id) { |
| 11742 | /* Set per-thread callback if script is loaded per-thread */ |
| 11743 | hlua_flt_ops->init_per_thread = hlua_filter_init_per_thread; |
| 11744 | hlua_flt_ops->deinit_per_thread = hlua_filter_deinit_per_thread; |
| 11745 | } |
| 11746 | hlua_flt_ops->attach = hlua_filter_new; |
| 11747 | hlua_flt_ops->detach = hlua_filter_delete; |
| 11748 | |
| 11749 | /* Push the filter class on the stack and resolve all callbacks */ |
Aurelien DARRAGON | 4fdf8b5 | 2023-03-20 17:22:37 +0100 | [diff] [blame] | 11750 | hlua_pushref(L, reg_flt->flt_ref[state_id]); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11751 | |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 11752 | if (lua_getfield(L, -1, "start_analyze") == LUA_TFUNCTION) |
| 11753 | hlua_flt_ops->channel_start_analyze = hlua_filter_start_analyze; |
| 11754 | lua_pop(L, 1); |
| 11755 | if (lua_getfield(L, -1, "end_analyze") == LUA_TFUNCTION) |
| 11756 | hlua_flt_ops->channel_end_analyze = hlua_filter_end_analyze; |
| 11757 | lua_pop(L, 1); |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 11758 | if (lua_getfield(L, -1, "http_headers") == LUA_TFUNCTION) |
| 11759 | hlua_flt_ops->http_headers = hlua_filter_http_headers; |
| 11760 | lua_pop(L, 1); |
| 11761 | if (lua_getfield(L, -1, "http_payload") == LUA_TFUNCTION) |
| 11762 | hlua_flt_ops->http_payload = hlua_filter_http_payload; |
| 11763 | lua_pop(L, 1); |
| 11764 | if (lua_getfield(L, -1, "http_end") == LUA_TFUNCTION) |
| 11765 | hlua_flt_ops->http_end = hlua_filter_http_end; |
| 11766 | lua_pop(L, 1); |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 11767 | if (lua_getfield(L, -1, "tcp_payload") == LUA_TFUNCTION) |
| 11768 | hlua_flt_ops->tcp_payload = hlua_filter_tcp_payload; |
| 11769 | lua_pop(L, 1); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11770 | |
| 11771 | /* Get id and flags of the filter class */ |
| 11772 | if (lua_getfield(L, -1, "id") == LUA_TSTRING) |
| 11773 | flt_id = lua_tostring(L, -1); |
| 11774 | lua_pop(L, 1); |
| 11775 | if (lua_getfield(L, -1, "flags") == LUA_TNUMBER) |
| 11776 | flt_flags = lua_tointeger(L, -1); |
| 11777 | lua_pop(L, 1); |
| 11778 | |
| 11779 | /* Create the filter config */ |
| 11780 | conf = calloc(1, sizeof(*conf)); |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 11781 | if (!conf) |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11782 | goto error; |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11783 | conf->reg = reg_flt; |
| 11784 | |
| 11785 | /* duplicate args */ |
| 11786 | for (pos = 0; *args[*cur_arg + 1 + pos]; pos++); |
| 11787 | conf->args = calloc(pos + 1, sizeof(*conf->args)); |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 11788 | if (!conf->args) |
| 11789 | goto error; |
| 11790 | for (pos = 0; *args[*cur_arg + 1 + pos]; pos++) { |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11791 | conf->args[pos] = strdup(args[*cur_arg + 1 + pos]); |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 11792 | if (!conf->args[pos]) |
| 11793 | goto error; |
| 11794 | } |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11795 | conf->args[pos] = NULL; |
| 11796 | *cur_arg += pos + 1; |
| 11797 | |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 11798 | if (flt_id) { |
| 11799 | fconf->id = strdup(flt_id); |
| 11800 | if (!fconf->id) |
| 11801 | goto error; |
| 11802 | } |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11803 | fconf->flags = flt_flags; |
| 11804 | fconf->conf = conf; |
| 11805 | fconf->ops = hlua_flt_ops; |
| 11806 | |
| 11807 | lua_settop(L, 0); |
| 11808 | return 0; |
| 11809 | |
| 11810 | error: |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 11811 | 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] | 11812 | free(hlua_flt_ops); |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 11813 | if (conf && conf->args) { |
| 11814 | for (pos = 0; conf->args[pos]; pos++) |
| 11815 | free(conf->args[pos]); |
| 11816 | free(conf->args); |
| 11817 | } |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11818 | free(conf); |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 11819 | free((char *)fconf->id); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11820 | lua_settop(L, 0); |
| 11821 | return -1; |
| 11822 | } |
| 11823 | |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 11824 | __LJMP static int hlua_register_data_filter(lua_State *L) |
| 11825 | { |
| 11826 | struct filter *filter; |
| 11827 | struct channel *chn; |
| 11828 | |
| 11829 | MAY_LJMP(check_args(L, 2, "register_data_filter")); |
| 11830 | MAY_LJMP(luaL_checktype(L, 1, LUA_TTABLE)); |
| 11831 | chn = MAY_LJMP(hlua_checkchannel(L, 2)); |
| 11832 | |
| 11833 | lua_getfield(L, 1, "__filter"); |
| 11834 | MAY_LJMP(luaL_checktype(L, -1, LUA_TLIGHTUSERDATA)); |
| 11835 | filter = lua_touserdata (L, -1); |
| 11836 | lua_pop(L, 1); |
| 11837 | |
| 11838 | register_data_filter(chn_strm(chn), chn, filter); |
| 11839 | return 1; |
| 11840 | } |
| 11841 | |
| 11842 | __LJMP static int hlua_unregister_data_filter(lua_State *L) |
| 11843 | { |
| 11844 | struct filter *filter; |
| 11845 | struct channel *chn; |
| 11846 | |
| 11847 | MAY_LJMP(check_args(L, 2, "unregister_data_filter")); |
| 11848 | MAY_LJMP(luaL_checktype(L, 1, LUA_TTABLE)); |
| 11849 | chn = MAY_LJMP(hlua_checkchannel(L, 2)); |
| 11850 | |
| 11851 | lua_getfield(L, 1, "__filter"); |
| 11852 | MAY_LJMP(luaL_checktype(L, -1, LUA_TLIGHTUSERDATA)); |
| 11853 | filter = lua_touserdata (L, -1); |
| 11854 | lua_pop(L, 1); |
| 11855 | |
| 11856 | unregister_data_filter(chn_strm(chn), chn, filter); |
| 11857 | return 1; |
| 11858 | } |
| 11859 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11860 | /* 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] | 11861 | * 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] | 11862 | * parse configuration arguments. |
| 11863 | */ |
| 11864 | __LJMP static int hlua_register_filter(lua_State *L) |
| 11865 | { |
| 11866 | struct buffer *trash; |
| 11867 | struct flt_kw_list *fkl; |
| 11868 | struct flt_kw *fkw; |
| 11869 | const char *name; |
| 11870 | struct hlua_reg_filter *reg_flt= NULL; |
| 11871 | int flt_ref, fun_ref; |
| 11872 | int len; |
| 11873 | |
| 11874 | MAY_LJMP(check_args(L, 3, "register_filter")); |
| 11875 | |
Aurelien DARRAGON | 87f5297 | 2023-02-24 09:43:54 +0100 | [diff] [blame] | 11876 | if (hlua_gethlua(L)) { |
| 11877 | /* runtime processing */ |
| 11878 | WILL_LJMP(luaL_error(L, "register_filter: not available outside of body context")); |
| 11879 | } |
| 11880 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11881 | /* First argument : filter name. */ |
| 11882 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 11883 | |
| 11884 | /* Second argument : The filter class */ |
| 11885 | flt_ref = MAY_LJMP(hlua_checktable(L, 2)); |
| 11886 | |
| 11887 | /* Third argument : lua function. */ |
| 11888 | fun_ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 11889 | |
| 11890 | trash = get_trash_chunk(); |
| 11891 | chunk_printf(trash, "lua.%s", name); |
| 11892 | fkw = flt_find_kw(trash->area); |
| 11893 | if (fkw != NULL) { |
| 11894 | reg_flt = fkw->private; |
| 11895 | if (reg_flt->flt_ref[hlua_state_id] != -1 || reg_flt->fun_ref[hlua_state_id] != -1) { |
| 11896 | ha_warning("Trying to register filter 'lua.%s' more than once. " |
| 11897 | "This will become a hard error in version 2.5.\n", name); |
Aurelien DARRAGON | 55afbed | 2023-03-02 18:42:06 +0100 | [diff] [blame] | 11898 | if (reg_flt->flt_ref[hlua_state_id] != -1) |
| 11899 | hlua_unref(L, reg_flt->flt_ref[hlua_state_id]); |
| 11900 | if (reg_flt->fun_ref[hlua_state_id] != -1) |
| 11901 | hlua_unref(L, reg_flt->fun_ref[hlua_state_id]); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11902 | } |
| 11903 | reg_flt->flt_ref[hlua_state_id] = flt_ref; |
| 11904 | reg_flt->fun_ref[hlua_state_id] = fun_ref; |
| 11905 | return 0; |
| 11906 | } |
| 11907 | |
| 11908 | fkl = calloc(1, sizeof(*fkl) + sizeof(struct flt_kw) * 2); |
| 11909 | if (!fkl) |
| 11910 | goto alloc_error; |
| 11911 | fkl->scope = "HLUA"; |
| 11912 | |
| 11913 | reg_flt = new_hlua_reg_filter(name); |
| 11914 | if (!reg_flt) |
| 11915 | goto alloc_error; |
| 11916 | |
| 11917 | reg_flt->flt_ref[hlua_state_id] = flt_ref; |
| 11918 | reg_flt->fun_ref[hlua_state_id] = fun_ref; |
| 11919 | |
| 11920 | /* The filter keyword */ |
| 11921 | len = strlen("lua.") + strlen(name) + 1; |
| 11922 | fkl->kw[0].kw = calloc(1, len); |
| 11923 | if (!fkl->kw[0].kw) |
| 11924 | goto alloc_error; |
| 11925 | |
| 11926 | snprintf((char *)fkl->kw[0].kw, len, "lua.%s", name); |
| 11927 | |
| 11928 | fkl->kw[0].parse = hlua_filter_parse_fct; |
| 11929 | fkl->kw[0].private = reg_flt; |
| 11930 | memset(&fkl->kw[1], 0, sizeof(*fkl->kw)); |
| 11931 | |
| 11932 | /* Register this new filter */ |
| 11933 | flt_register_keywords(fkl); |
| 11934 | |
| 11935 | return 0; |
| 11936 | |
| 11937 | alloc_error: |
| 11938 | release_hlua_reg_filter(reg_flt); |
Aurelien DARRAGON | 55afbed | 2023-03-02 18:42:06 +0100 | [diff] [blame] | 11939 | hlua_unref(L, flt_ref); |
| 11940 | hlua_unref(L, fun_ref); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11941 | ha_free(&fkl); |
| 11942 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 11943 | return 0; /* Never reached */ |
| 11944 | } |
| 11945 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 11946 | 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] | 11947 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 11948 | char **err, unsigned int *timeout) |
| 11949 | { |
| 11950 | const char *error; |
| 11951 | |
| 11952 | error = parse_time_err(args[1], timeout, TIME_UNIT_MS); |
Willy Tarreau | 9faebe3 | 2019-06-07 19:00:37 +0200 | [diff] [blame] | 11953 | if (error == PARSE_TIME_OVER) { |
| 11954 | memprintf(err, "timer overflow in argument <%s> to <%s> (maximum value is 2147483647 ms or ~24.8 days)", |
| 11955 | args[1], args[0]); |
| 11956 | return -1; |
| 11957 | } |
| 11958 | else if (error == PARSE_TIME_UNDER) { |
| 11959 | memprintf(err, "timer underflow in argument <%s> to <%s> (minimum non-null value is 1 ms)", |
| 11960 | args[1], args[0]); |
| 11961 | return -1; |
| 11962 | } |
| 11963 | else if (error) { |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 11964 | memprintf(err, "%s: invalid timeout", args[0]); |
| 11965 | return -1; |
| 11966 | } |
| 11967 | return 0; |
| 11968 | } |
| 11969 | |
| 11970 | 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] | 11971 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 11972 | char **err) |
| 11973 | { |
| 11974 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 11975 | file, line, err, &hlua_timeout_session); |
| 11976 | } |
| 11977 | |
| 11978 | 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] | 11979 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 11980 | char **err) |
| 11981 | { |
| 11982 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 11983 | file, line, err, &hlua_timeout_task); |
| 11984 | } |
| 11985 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 11986 | 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] | 11987 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 11988 | char **err) |
| 11989 | { |
| 11990 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 11991 | file, line, err, &hlua_timeout_applet); |
| 11992 | } |
| 11993 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 11994 | 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] | 11995 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 11996 | char **err) |
| 11997 | { |
| 11998 | char *error; |
| 11999 | |
| 12000 | hlua_nb_instruction = strtoll(args[1], &error, 10); |
| 12001 | if (*error != '\0') { |
| 12002 | memprintf(err, "%s: invalid number", args[0]); |
| 12003 | return -1; |
| 12004 | } |
| 12005 | return 0; |
| 12006 | } |
| 12007 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 12008 | 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] | 12009 | const struct proxy *defpx, const char *file, int line, |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 12010 | char **err) |
| 12011 | { |
| 12012 | char *error; |
| 12013 | |
| 12014 | if (*(args[1]) == 0) { |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 12015 | 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] | 12016 | return -1; |
| 12017 | } |
| 12018 | hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L; |
| 12019 | if (*error != '\0') { |
| 12020 | memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error); |
| 12021 | return -1; |
| 12022 | } |
| 12023 | return 0; |
| 12024 | } |
| 12025 | |
| 12026 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 12027 | /* This function is called by the main configuration key "lua-load". It loads and |
| 12028 | * execute an lua file during the parsing of the HAProxy configuration file. It is |
| 12029 | * the main lua entry point. |
| 12030 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 12031 | * This function runs with the HAProxy keywords API. It returns -1 if an error |
| 12032 | * occurs, otherwise it returns 0. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 12033 | * |
| 12034 | * In some error case, LUA set an error message in top of the stack. This function |
| 12035 | * 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] | 12036 | * |
| 12037 | * This function can fail with an abort() due to an Lua critical error. |
| 12038 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 12039 | * tolerated. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 12040 | */ |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 12041 | static int hlua_load_state(char **args, lua_State *L, char **err) |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 12042 | { |
| 12043 | int error; |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 12044 | int nargs; |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 12045 | |
| 12046 | /* Just load and compile the file. */ |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 12047 | error = luaL_loadfile(L, args[0]); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 12048 | if (error) { |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 12049 | 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] | 12050 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 12051 | return -1; |
| 12052 | } |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 12053 | |
| 12054 | /* Push args in the Lua stack, except the first one which is the filename */ |
| 12055 | for (nargs = 1; *(args[nargs]) != 0; nargs++) { |
Aurelien DARRAGON | 4d7aefe | 2022-09-23 10:22:14 +0200 | [diff] [blame] | 12056 | /* Check stack size. */ |
| 12057 | if (!lua_checkstack(L, 1)) { |
| 12058 | memprintf(err, "Lua runtime error while loading arguments: stack is full."); |
| 12059 | return -1; |
| 12060 | } |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 12061 | lua_pushstring(L, args[nargs]); |
| 12062 | } |
| 12063 | nargs--; |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 12064 | |
| 12065 | /* If no syntax error where detected, execute the code. */ |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 12066 | error = lua_pcall(L, nargs, LUA_MULTRET, 0); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 12067 | switch (error) { |
| 12068 | case LUA_OK: |
| 12069 | break; |
| 12070 | case LUA_ERRRUN: |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 12071 | memprintf(err, "Lua runtime error: %s", lua_tostring(L, -1)); |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 12072 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 12073 | return -1; |
| 12074 | case LUA_ERRMEM: |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 12075 | memprintf(err, "Lua out of memory error"); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 12076 | return -1; |
| 12077 | case LUA_ERRERR: |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 12078 | memprintf(err, "Lua message handler error: %s", lua_tostring(L, -1)); |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 12079 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 12080 | return -1; |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 12081 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM <= 503 |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 12082 | case LUA_ERRGCMM: |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 12083 | memprintf(err, "Lua garbage collector error: %s", lua_tostring(L, -1)); |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 12084 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 12085 | return -1; |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 12086 | #endif |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 12087 | default: |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 12088 | memprintf(err, "Lua unknown error: %s", lua_tostring(L, -1)); |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 12089 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 12090 | return -1; |
| 12091 | } |
| 12092 | |
| 12093 | return 0; |
| 12094 | } |
| 12095 | |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 12096 | static int hlua_load(char **args, int section_type, struct proxy *curpx, |
Willy Tarreau | 0182516 | 2021-03-09 09:53:46 +0100 | [diff] [blame] | 12097 | const struct proxy *defpx, const char *file, int line, |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 12098 | char **err) |
| 12099 | { |
| 12100 | if (*(args[1]) == 0) { |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 12101 | memprintf(err, "'%s' expects a file name as parameter.", args[0]); |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 12102 | return -1; |
| 12103 | } |
| 12104 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 12105 | /* loading for global state */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 12106 | hlua_state_id = 0; |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 12107 | ha_set_thread(NULL); |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 12108 | return hlua_load_state(&args[1], hlua_states[0], err); |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 12109 | } |
| 12110 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 12111 | 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] | 12112 | const struct proxy *defpx, const char *file, int line, |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 12113 | char **err) |
| 12114 | { |
| 12115 | int len; |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 12116 | int i; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 12117 | |
| 12118 | if (*(args[1]) == 0) { |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 12119 | memprintf(err, "'%s' expects a file as parameter.", args[0]); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 12120 | return -1; |
| 12121 | } |
| 12122 | |
| 12123 | if (per_thread_load == NULL) { |
| 12124 | /* allocate the first entry large enough to store the final NULL */ |
| 12125 | per_thread_load = calloc(1, sizeof(*per_thread_load)); |
| 12126 | if (per_thread_load == NULL) { |
| 12127 | memprintf(err, "out of memory error"); |
| 12128 | return -1; |
| 12129 | } |
| 12130 | } |
| 12131 | |
| 12132 | /* count used entries */ |
| 12133 | for (len = 0; per_thread_load[len] != NULL; len++) |
| 12134 | ; |
| 12135 | |
| 12136 | per_thread_load = realloc(per_thread_load, (len + 2) * sizeof(*per_thread_load)); |
| 12137 | if (per_thread_load == NULL) { |
| 12138 | memprintf(err, "out of memory error"); |
| 12139 | return -1; |
| 12140 | } |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 12141 | per_thread_load[len + 1] = NULL; |
| 12142 | |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 12143 | /* count args excepting the first, allocate array and copy args */ |
| 12144 | for (i = 0; *(args[i + 1]) != 0; i++); |
Aurelien DARRAGON | b12d169 | 2022-09-23 08:48:34 +0200 | [diff] [blame] | 12145 | per_thread_load[len] = calloc(i + 1, sizeof(*per_thread_load[len])); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 12146 | if (per_thread_load[len] == NULL) { |
| 12147 | memprintf(err, "out of memory error"); |
| 12148 | return -1; |
| 12149 | } |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 12150 | for (i = 1; *(args[i]) != 0; i++) { |
| 12151 | per_thread_load[len][i - 1] = strdup(args[i]); |
| 12152 | if (per_thread_load[len][i - 1] == NULL) { |
| 12153 | memprintf(err, "out of memory error"); |
| 12154 | return -1; |
| 12155 | } |
| 12156 | } |
| 12157 | per_thread_load[len][i - 1] = strdup(""); |
| 12158 | if (per_thread_load[len][i - 1] == NULL) { |
| 12159 | memprintf(err, "out of memory error"); |
| 12160 | return -1; |
| 12161 | } |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 12162 | |
| 12163 | /* loading for thread 1 only */ |
| 12164 | hlua_state_id = 1; |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 12165 | ha_set_thread(NULL); |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 12166 | return hlua_load_state(per_thread_load[len], hlua_states[1], err); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 12167 | } |
| 12168 | |
Tim Duesterhus | c9fc9f2 | 2020-01-12 13:55:39 +0100 | [diff] [blame] | 12169 | /* Prepend the given <path> followed by a semicolon to the `package.<type>` variable |
| 12170 | * in the given <ctx>. |
| 12171 | */ |
Thierry Fournier | 3fb9e51 | 2020-11-28 10:13:12 +0100 | [diff] [blame] | 12172 | static int hlua_prepend_path(lua_State *L, char *type, char *path) |
Tim Duesterhus | c9fc9f2 | 2020-01-12 13:55:39 +0100 | [diff] [blame] | 12173 | { |
Thierry Fournier | 3fb9e51 | 2020-11-28 10:13:12 +0100 | [diff] [blame] | 12174 | lua_getglobal(L, "package"); /* push package variable */ |
| 12175 | lua_pushstring(L, path); /* push given path */ |
| 12176 | lua_pushstring(L, ";"); /* push semicolon */ |
| 12177 | lua_getfield(L, -3, type); /* push old path */ |
| 12178 | lua_concat(L, 3); /* concatenate to new path */ |
| 12179 | lua_setfield(L, -2, type); /* store new path */ |
| 12180 | lua_pop(L, 1); /* pop package variable */ |
Tim Duesterhus | c9fc9f2 | 2020-01-12 13:55:39 +0100 | [diff] [blame] | 12181 | |
| 12182 | return 0; |
| 12183 | } |
| 12184 | |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 12185 | 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] | 12186 | const struct proxy *defpx, const char *file, int line, |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 12187 | char **err) |
| 12188 | { |
| 12189 | char *path; |
| 12190 | char *type = "path"; |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 12191 | struct prepend_path *p = NULL; |
Bertrand Jacquin | 7fbc770 | 2021-11-24 21:16:06 +0000 | [diff] [blame] | 12192 | size_t i; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 12193 | |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 12194 | if (too_many_args(2, args, err, NULL)) { |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 12195 | goto err; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 12196 | } |
| 12197 | |
| 12198 | if (!(*args[1])) { |
| 12199 | memprintf(err, "'%s' expects to receive a <path> as argument", args[0]); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 12200 | goto err; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 12201 | } |
| 12202 | path = args[1]; |
| 12203 | |
| 12204 | if (*args[2]) { |
| 12205 | if (strcmp(args[2], "path") != 0 && strcmp(args[2], "cpath") != 0) { |
| 12206 | 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] | 12207 | goto err; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 12208 | } |
| 12209 | type = args[2]; |
| 12210 | } |
| 12211 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 12212 | p = calloc(1, sizeof(*p)); |
| 12213 | if (p == NULL) { |
Tim Duesterhus | f89d43a | 2021-01-03 20:04:37 +0100 | [diff] [blame] | 12214 | memprintf(err, "memory allocation failed"); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 12215 | goto err; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 12216 | } |
| 12217 | p->path = strdup(path); |
| 12218 | if (p->path == NULL) { |
Tim Duesterhus | f89d43a | 2021-01-03 20:04:37 +0100 | [diff] [blame] | 12219 | memprintf(err, "memory allocation failed"); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 12220 | goto err2; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 12221 | } |
| 12222 | p->type = strdup(type); |
| 12223 | if (p->type == NULL) { |
Tim Duesterhus | f89d43a | 2021-01-03 20:04:37 +0100 | [diff] [blame] | 12224 | memprintf(err, "memory allocation failed"); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 12225 | goto err2; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 12226 | } |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 12227 | LIST_APPEND(&prepend_path_list, &p->l); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 12228 | |
Tim Duesterhus | 9e5e586 | 2021-10-11 18:51:08 +0200 | [diff] [blame] | 12229 | /* Handle the global state and the per-thread state for the first |
| 12230 | * thread. The remaining threads will be initialized based on |
| 12231 | * prepend_path_list. |
| 12232 | */ |
Bertrand Jacquin | 7fbc770 | 2021-11-24 21:16:06 +0000 | [diff] [blame] | 12233 | for (i = 0; i < 2; i++) { |
Tim Duesterhus | 9e5e586 | 2021-10-11 18:51:08 +0200 | [diff] [blame] | 12234 | lua_State *L = hlua_states[i]; |
| 12235 | const char *error; |
| 12236 | |
| 12237 | if (setjmp(safe_ljmp_env) != 0) { |
| 12238 | lua_atpanic(L, hlua_panic_safe); |
| 12239 | if (lua_type(L, -1) == LUA_TSTRING) |
| 12240 | error = lua_tostring(L, -1); |
| 12241 | else |
| 12242 | error = "critical error"; |
| 12243 | fprintf(stderr, "lua-prepend-path: %s.\n", error); |
| 12244 | exit(1); |
| 12245 | } else { |
| 12246 | lua_atpanic(L, hlua_panic_ljmp); |
| 12247 | } |
| 12248 | |
| 12249 | hlua_prepend_path(L, type, path); |
| 12250 | |
| 12251 | lua_atpanic(L, hlua_panic_safe); |
| 12252 | } |
| 12253 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 12254 | return 0; |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 12255 | |
| 12256 | err2: |
| 12257 | free(p->type); |
| 12258 | free(p->path); |
| 12259 | err: |
| 12260 | free(p); |
| 12261 | return -1; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 12262 | } |
| 12263 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 12264 | /* configuration keywords declaration */ |
| 12265 | static struct cfg_kw_list cfg_kws = {{ },{ |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 12266 | { CFG_GLOBAL, "lua-prepend-path", hlua_config_prepend_path }, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 12267 | { CFG_GLOBAL, "lua-load", hlua_load }, |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 12268 | { CFG_GLOBAL, "lua-load-per-thread", hlua_load_per_thread }, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 12269 | { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout }, |
| 12270 | { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout }, |
Thierry FOURNIER | 56da101 | 2015-10-01 08:42:31 +0200 | [diff] [blame] | 12271 | { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout }, |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 12272 | { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield }, |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 12273 | { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem }, |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 12274 | { 0, NULL, NULL }, |
| 12275 | }}; |
| 12276 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 12277 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |
| 12278 | |
William Lallemand | 5213918 | 2022-03-30 15:05:42 +0200 | [diff] [blame] | 12279 | #ifdef USE_OPENSSL |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 12280 | |
William Lallemand | 30fcca1 | 2022-03-30 12:03:12 +0200 | [diff] [blame] | 12281 | /* |
| 12282 | * This function replace a ckch_store by another one, and rebuild the ckch_inst and all its dependencies. |
| 12283 | * It does the sam as "cli_io_handler_commit_cert" but for lua, the major |
| 12284 | * difference is that the yield in lua and for the CLI is not handled the same |
| 12285 | * way. |
| 12286 | */ |
| 12287 | __LJMP static int hlua_ckch_commit_yield(lua_State *L, int status, lua_KContext ctx) |
| 12288 | { |
| 12289 | struct ckch_inst **lua_ckchi = lua_touserdata(L, -1); |
| 12290 | struct ckch_store **lua_ckchs = lua_touserdata(L, -2); |
| 12291 | struct ckch_inst *ckchi = *lua_ckchi; |
| 12292 | struct ckch_store *old_ckchs = lua_ckchs[0]; |
| 12293 | struct ckch_store *new_ckchs = lua_ckchs[1]; |
| 12294 | struct hlua *hlua; |
| 12295 | char *err = NULL; |
| 12296 | int y = 1; |
| 12297 | |
| 12298 | hlua = hlua_gethlua(L); |
| 12299 | |
| 12300 | /* get the first ckchi to copy */ |
| 12301 | if (ckchi == NULL) |
| 12302 | ckchi = LIST_ELEM(old_ckchs->ckch_inst.n, typeof(ckchi), by_ckchs); |
| 12303 | |
| 12304 | /* walk through the old ckch_inst and creates new ckch_inst using the updated ckchs */ |
| 12305 | list_for_each_entry_from(ckchi, &old_ckchs->ckch_inst, by_ckchs) { |
| 12306 | struct ckch_inst *new_inst; |
| 12307 | |
| 12308 | /* it takes a lot of CPU to creates SSL_CTXs, so we yield every 10 CKCH instances */ |
| 12309 | if (y % 10 == 0) { |
| 12310 | |
| 12311 | *lua_ckchi = ckchi; |
| 12312 | |
| 12313 | task_wakeup(hlua->task, TASK_WOKEN_MSG); |
| 12314 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_ckch_commit_yield, TICK_ETERNITY, 0)); |
| 12315 | } |
| 12316 | |
| 12317 | if (ckch_inst_rebuild(new_ckchs, ckchi, &new_inst, &err)) |
| 12318 | goto error; |
| 12319 | |
| 12320 | /* link the new ckch_inst to the duplicate */ |
| 12321 | LIST_APPEND(&new_ckchs->ckch_inst, &new_inst->by_ckchs); |
| 12322 | y++; |
| 12323 | } |
| 12324 | |
| 12325 | /* The generation is finished, we can insert everything */ |
| 12326 | ckch_store_replace(old_ckchs, new_ckchs); |
| 12327 | |
| 12328 | lua_pop(L, 2); /* pop the lua_ckchs and ckchi */ |
| 12329 | |
| 12330 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 12331 | |
| 12332 | return 0; |
| 12333 | |
| 12334 | error: |
| 12335 | ckch_store_free(new_ckchs); |
| 12336 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 12337 | WILL_LJMP(luaL_error(L, "%s", err)); |
| 12338 | free(err); |
| 12339 | |
| 12340 | return 0; |
| 12341 | } |
| 12342 | |
| 12343 | /* |
| 12344 | * Replace a ckch_store <filename> in the ckchs_tree with a ckch_store created |
| 12345 | * from the table in parameter. |
| 12346 | * |
| 12347 | * This is equivalent to "set ssl cert" + "commit ssl cert" over the CLI, which |
| 12348 | * means it does not need to have a transaction since everything is done in the |
| 12349 | * same function. |
| 12350 | * |
| 12351 | * CertCache.set{filename="", crt="", key="", sctl="", ocsp="", issuer=""} |
| 12352 | * |
| 12353 | */ |
| 12354 | __LJMP static int hlua_ckch_set(lua_State *L) |
| 12355 | { |
| 12356 | struct hlua *hlua; |
| 12357 | struct ckch_inst **lua_ckchi; |
| 12358 | struct ckch_store **lua_ckchs; |
| 12359 | struct ckch_store *old_ckchs = NULL; |
| 12360 | struct ckch_store *new_ckchs = NULL; |
| 12361 | int errcode = 0; |
| 12362 | char *err = NULL; |
| 12363 | struct cert_exts *cert_ext = NULL; |
| 12364 | char *filename; |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 12365 | struct ckch_data *data; |
William Lallemand | 30fcca1 | 2022-03-30 12:03:12 +0200 | [diff] [blame] | 12366 | int ret; |
| 12367 | |
| 12368 | if (lua_type(L, -1) != LUA_TTABLE) |
| 12369 | WILL_LJMP(luaL_error(L, "'CertCache.set' needs a table as argument")); |
| 12370 | |
| 12371 | hlua = hlua_gethlua(L); |
| 12372 | |
| 12373 | /* FIXME: this should not return an error but should come back later */ |
| 12374 | if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock)) |
| 12375 | WILL_LJMP(luaL_error(L, "CertCache already under lock")); |
| 12376 | |
| 12377 | ret = lua_getfield(L, -1, "filename"); |
| 12378 | if (ret != LUA_TSTRING) { |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 12379 | memprintf(&err, "%sNo filename specified!", err ? err : ""); |
William Lallemand | 30fcca1 | 2022-03-30 12:03:12 +0200 | [diff] [blame] | 12380 | errcode |= ERR_ALERT | ERR_FATAL; |
| 12381 | goto end; |
| 12382 | } |
| 12383 | filename = (char *)lua_tostring(L, -1); |
| 12384 | |
| 12385 | |
| 12386 | /* look for the filename in the tree */ |
| 12387 | old_ckchs = ckchs_lookup(filename); |
| 12388 | if (!old_ckchs) { |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 12389 | 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] | 12390 | errcode |= ERR_ALERT | ERR_FATAL; |
| 12391 | goto end; |
| 12392 | } |
| 12393 | /* TODO: handle extra_files_noext */ |
| 12394 | |
| 12395 | new_ckchs = ckchs_dup(old_ckchs); |
| 12396 | if (!new_ckchs) { |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 12397 | memprintf(&err, "%sCannot allocate memory!", err ? err : ""); |
William Lallemand | 30fcca1 | 2022-03-30 12:03:12 +0200 | [diff] [blame] | 12398 | errcode |= ERR_ALERT | ERR_FATAL; |
| 12399 | goto end; |
| 12400 | } |
| 12401 | |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 12402 | data = new_ckchs->data; |
William Lallemand | 30fcca1 | 2022-03-30 12:03:12 +0200 | [diff] [blame] | 12403 | |
| 12404 | /* loop on the field in the table, which have the same name as the |
| 12405 | * possible extensions of files */ |
| 12406 | lua_pushnil(L); |
| 12407 | while (lua_next(L, 1)) { |
| 12408 | int i; |
| 12409 | const char *field = lua_tostring(L, -2); |
| 12410 | char *payload = (char *)lua_tostring(L, -1); |
| 12411 | |
| 12412 | if (!field || strcmp(field, "filename") == 0) { |
| 12413 | lua_pop(L, 1); |
| 12414 | continue; |
| 12415 | } |
| 12416 | |
| 12417 | for (i = 0; field && cert_exts[i].ext != NULL; i++) { |
| 12418 | if (strcmp(field, cert_exts[i].ext) == 0) { |
| 12419 | cert_ext = &cert_exts[i]; |
| 12420 | break; |
| 12421 | } |
| 12422 | } |
| 12423 | |
| 12424 | /* this is the default type, the field is not supported */ |
| 12425 | if (cert_ext == NULL) { |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 12426 | memprintf(&err, "%sUnsupported field '%s'", err ? err : "", field); |
William Lallemand | 30fcca1 | 2022-03-30 12:03:12 +0200 | [diff] [blame] | 12427 | errcode |= ERR_ALERT | ERR_FATAL; |
| 12428 | goto end; |
| 12429 | } |
| 12430 | |
| 12431 | /* appply the change on the duplicate */ |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 12432 | if (cert_ext->load(filename, payload, data, &err) != 0) { |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 12433 | 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] | 12434 | errcode |= ERR_ALERT | ERR_FATAL; |
| 12435 | goto end; |
| 12436 | } |
| 12437 | lua_pop(L, 1); |
| 12438 | } |
| 12439 | |
| 12440 | /* store the pointers on the lua stack */ |
| 12441 | lua_ckchs = lua_newuserdata(L, sizeof(struct ckch_store *) * 2); |
| 12442 | lua_ckchs[0] = old_ckchs; |
| 12443 | lua_ckchs[1] = new_ckchs; |
| 12444 | lua_ckchi = lua_newuserdata(L, sizeof(struct ckch_inst *)); |
| 12445 | *lua_ckchi = NULL; |
| 12446 | |
| 12447 | task_wakeup(hlua->task, TASK_WOKEN_MSG); |
| 12448 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_ckch_commit_yield, TICK_ETERNITY, 0)); |
| 12449 | |
| 12450 | end: |
| 12451 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 12452 | |
| 12453 | if (errcode & ERR_CODE) { |
| 12454 | ckch_store_free(new_ckchs); |
| 12455 | WILL_LJMP(luaL_error(L, "%s", err)); |
| 12456 | } |
| 12457 | free(err); |
| 12458 | |
| 12459 | return 0; |
| 12460 | } |
| 12461 | |
William Lallemand | 5213918 | 2022-03-30 15:05:42 +0200 | [diff] [blame] | 12462 | #else |
| 12463 | |
| 12464 | __LJMP static int hlua_ckch_set(lua_State *L) |
| 12465 | { |
| 12466 | WILL_LJMP(luaL_error(L, "'CertCache.set' needs an HAProxy built with OpenSSL")); |
| 12467 | |
| 12468 | return 0; |
| 12469 | } |
| 12470 | #endif /* ! USE_OPENSSL */ |
| 12471 | |
| 12472 | |
| 12473 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 12474 | /* This function can fail with an abort() due to an Lua critical error. |
| 12475 | * We are in the initialisation process of HAProxy, this abort() is |
| 12476 | * tolerated. |
| 12477 | */ |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 12478 | int hlua_post_init_state(lua_State *L) |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 12479 | { |
| 12480 | struct hlua_init_function *init; |
| 12481 | const char *msg; |
| 12482 | enum hlua_exec ret; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 12483 | const char *error; |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 12484 | const char *kind; |
| 12485 | const char *trace; |
| 12486 | int return_status = 1; |
| 12487 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
| 12488 | int nres; |
| 12489 | #endif |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 12490 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 12491 | /* disable memory limit checks if limit is not set */ |
| 12492 | if (!hlua_global_allocator.limit) |
| 12493 | hlua_global_allocator.limit = ~hlua_global_allocator.limit; |
| 12494 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 12495 | /* Call post initialisation function in safe environment. */ |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 12496 | if (setjmp(safe_ljmp_env) != 0) { |
| 12497 | lua_atpanic(L, hlua_panic_safe); |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 12498 | if (lua_type(L, -1) == LUA_TSTRING) |
| 12499 | error = lua_tostring(L, -1); |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 12500 | else |
| 12501 | error = "critical error"; |
| 12502 | fprintf(stderr, "Lua post-init: %s.\n", error); |
| 12503 | exit(1); |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 12504 | } else { |
| 12505 | lua_atpanic(L, hlua_panic_ljmp); |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 12506 | } |
Frédéric Lécaille | 54f2bcf | 2018-08-29 13:46:24 +0200 | [diff] [blame] | 12507 | |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 12508 | list_for_each_entry(init, &hlua_init_functions[hlua_state_id], l) { |
Aurelien DARRAGON | 4fdf8b5 | 2023-03-20 17:22:37 +0100 | [diff] [blame] | 12509 | hlua_pushref(L, init->function_ref); |
Aurelien DARRAGON | 16d047b | 2023-03-20 16:29:55 +0100 | [diff] [blame] | 12510 | /* function ref should be released right away since it was pushed |
| 12511 | * on the stack and will not be used anymore |
| 12512 | */ |
| 12513 | hlua_unref(L, init->function_ref); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 12514 | |
| 12515 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 12516 | ret = lua_resume(L, L, 0, &nres); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 12517 | #else |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 12518 | ret = lua_resume(L, L, 0); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 12519 | #endif |
| 12520 | kind = NULL; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 12521 | switch (ret) { |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 12522 | |
| 12523 | case LUA_OK: |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 12524 | lua_pop(L, -1); |
Thierry Fournier | 13d08b7 | 2020-11-28 11:02:58 +0100 | [diff] [blame] | 12525 | break; |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 12526 | |
| 12527 | case LUA_ERRERR: |
| 12528 | kind = "message handler error"; |
Willy Tarreau | 14de395 | 2022-11-14 07:08:28 +0100 | [diff] [blame] | 12529 | __fallthrough; |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 12530 | case LUA_ERRRUN: |
| 12531 | if (!kind) |
| 12532 | kind = "runtime error"; |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 12533 | msg = lua_tostring(L, -1); |
| 12534 | lua_settop(L, 0); /* Empty the stack. */ |
| 12535 | lua_pop(L, 1); |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 12536 | trace = hlua_traceback(L, ", "); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 12537 | if (msg) |
| 12538 | ha_alert("Lua init: %s: '%s' from %s\n", kind, msg, trace); |
| 12539 | else |
| 12540 | ha_alert("Lua init: unknown %s from %s\n", kind, trace); |
| 12541 | return_status = 0; |
| 12542 | break; |
| 12543 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 12544 | default: |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 12545 | /* Unknown error */ |
| 12546 | kind = "Unknown error"; |
Willy Tarreau | 14de395 | 2022-11-14 07:08:28 +0100 | [diff] [blame] | 12547 | __fallthrough; |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 12548 | case LUA_YIELD: |
| 12549 | /* yield is not configured at this step, this state doesn't happen */ |
| 12550 | if (!kind) |
| 12551 | kind = "yield not allowed"; |
Willy Tarreau | 14de395 | 2022-11-14 07:08:28 +0100 | [diff] [blame] | 12552 | __fallthrough; |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 12553 | case LUA_ERRMEM: |
| 12554 | if (!kind) |
| 12555 | kind = "out of memory error"; |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 12556 | lua_settop(L, 0); |
| 12557 | lua_pop(L, 1); |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 12558 | trace = hlua_traceback(L, ", "); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 12559 | ha_alert("Lua init: %s: %s\n", kind, trace); |
| 12560 | return_status = 0; |
| 12561 | break; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 12562 | } |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 12563 | if (!return_status) |
| 12564 | break; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 12565 | } |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 12566 | |
| 12567 | lua_atpanic(L, hlua_panic_safe); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 12568 | return return_status; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 12569 | } |
| 12570 | |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 12571 | int hlua_post_init() |
| 12572 | { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 12573 | int ret; |
| 12574 | int i; |
| 12575 | int errors; |
| 12576 | char *err = NULL; |
| 12577 | struct hlua_function *fcn; |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 12578 | struct hlua_reg_filter *reg_flt; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 12579 | |
Willy Tarreau | b131049 | 2021-08-30 09:35:18 +0200 | [diff] [blame] | 12580 | #if defined(USE_OPENSSL) |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 12581 | /* Initialize SSL server. */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 12582 | if (socket_ssl->xprt->prepare_srv) { |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 12583 | int saved_used_backed = global.ssl_used_backend; |
| 12584 | // don't affect maxconn automatic computation |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 12585 | socket_ssl->xprt->prepare_srv(socket_ssl); |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 12586 | global.ssl_used_backend = saved_used_backed; |
| 12587 | } |
| 12588 | #endif |
| 12589 | |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 12590 | /* Perform post init of common thread */ |
| 12591 | hlua_state_id = 0; |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 12592 | ha_set_thread(&ha_thread_info[0]); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 12593 | ret = hlua_post_init_state(hlua_states[hlua_state_id]); |
| 12594 | if (ret == 0) |
| 12595 | return 0; |
| 12596 | |
| 12597 | /* init remaining lua states and load files */ |
| 12598 | for (hlua_state_id = 2; hlua_state_id < global.nbthread + 1; hlua_state_id++) { |
| 12599 | |
| 12600 | /* set thread context */ |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 12601 | ha_set_thread(&ha_thread_info[hlua_state_id - 1]); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 12602 | |
| 12603 | /* Init lua state */ |
| 12604 | hlua_states[hlua_state_id] = hlua_init_state(hlua_state_id); |
| 12605 | |
| 12606 | /* Load lua files */ |
| 12607 | for (i = 0; per_thread_load && per_thread_load[i]; i++) { |
| 12608 | ret = hlua_load_state(per_thread_load[i], hlua_states[hlua_state_id], &err); |
| 12609 | if (ret != 0) { |
| 12610 | ha_alert("Lua init: %s\n", err); |
| 12611 | return 0; |
| 12612 | } |
| 12613 | } |
| 12614 | } |
| 12615 | |
| 12616 | /* Reset thread context */ |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 12617 | ha_set_thread(NULL); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 12618 | |
| 12619 | /* Execute post init for all states */ |
| 12620 | for (hlua_state_id = 1; hlua_state_id < global.nbthread + 1; hlua_state_id++) { |
| 12621 | |
| 12622 | /* set thread context */ |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 12623 | ha_set_thread(&ha_thread_info[hlua_state_id - 1]); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 12624 | |
| 12625 | /* run post init */ |
| 12626 | ret = hlua_post_init_state(hlua_states[hlua_state_id]); |
| 12627 | if (ret == 0) |
| 12628 | return 0; |
| 12629 | } |
| 12630 | |
| 12631 | /* Reset thread context */ |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 12632 | ha_set_thread(NULL); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 12633 | |
| 12634 | /* control functions registering. Each function must have: |
| 12635 | * - only the function_ref[0] set positive and all other to -1 |
| 12636 | * - only the function_ref[0] set to -1 and all other positive |
| 12637 | * This ensure a same reference is not used both in shared |
| 12638 | * lua state and thread dedicated lua state. Note: is the case |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 12639 | * reach, the shared state is priority, but the bug will be |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 12640 | * complicated to found for the end user. |
| 12641 | */ |
| 12642 | errors = 0; |
| 12643 | list_for_each_entry(fcn, &referenced_functions, l) { |
| 12644 | ret = 0; |
| 12645 | for (i = 1; i < global.nbthread + 1; i++) { |
| 12646 | if (fcn->function_ref[i] == -1) |
| 12647 | ret--; |
| 12648 | else |
| 12649 | ret++; |
| 12650 | } |
| 12651 | if (abs(ret) != global.nbthread) { |
| 12652 | ha_alert("Lua function '%s' is not referenced in all thread. " |
| 12653 | "Expect function in all thread or in none thread.\n", fcn->name); |
| 12654 | errors++; |
| 12655 | continue; |
| 12656 | } |
| 12657 | |
| 12658 | if ((fcn->function_ref[0] == -1) == (ret < 0)) { |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 12659 | ha_alert("Lua function '%s' is referenced both ins shared Lua context (through lua-load) " |
| 12660 | "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] | 12661 | "exclusive.\n", fcn->name); |
| 12662 | errors++; |
| 12663 | } |
| 12664 | } |
| 12665 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 12666 | /* Do the same with registered filters */ |
| 12667 | list_for_each_entry(reg_flt, &referenced_filters, l) { |
| 12668 | ret = 0; |
| 12669 | for (i = 1; i < global.nbthread + 1; i++) { |
| 12670 | if (reg_flt->flt_ref[i] == -1) |
| 12671 | ret--; |
| 12672 | else |
| 12673 | ret++; |
| 12674 | } |
| 12675 | if (abs(ret) != global.nbthread) { |
| 12676 | ha_alert("Lua filter '%s' is not referenced in all thread. " |
| 12677 | "Expect function in all thread or in none thread.\n", reg_flt->name); |
| 12678 | errors++; |
| 12679 | continue; |
| 12680 | } |
| 12681 | |
| 12682 | if ((reg_flt->flt_ref[0] == -1) == (ret < 0)) { |
| 12683 | ha_alert("Lua filter '%s' is referenced both ins shared Lua context (through lua-load) " |
| 12684 | "and per-thread Lua context (through lua-load-per-thread). these two context " |
| 12685 | "exclusive.\n", fcn->name); |
| 12686 | errors++; |
| 12687 | } |
| 12688 | } |
| 12689 | |
| 12690 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 12691 | if (errors > 0) |
| 12692 | return 0; |
| 12693 | |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 12694 | /* 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] | 12695 | * -1 in order to have probably a segfault if someone use it |
| 12696 | */ |
| 12697 | hlua_state_id = -1; |
| 12698 | |
| 12699 | return 1; |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 12700 | } |
| 12701 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 12702 | /* The memory allocator used by the Lua stack. <ud> is a pointer to the |
| 12703 | * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize> |
| 12704 | * 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] | 12705 | * allocation. <nsize> is the requested new size. A new allocation is |
| 12706 | * indicated by <ptr> being NULL. A free is indicated by <nsize> being |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 12707 | * zero. This one verifies that the limits are respected but is optimized |
| 12708 | * 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] | 12709 | * |
| 12710 | * Warning: while this API ressembles glibc's realloc() a lot, glibc surpasses |
| 12711 | * POSIX by making realloc(ptr,0) an effective free(), but others do not do |
| 12712 | * that and will simply allocate zero as if it were the result of malloc(0), |
| 12713 | * so mapping this onto realloc() will lead to memory leaks on non-glibc |
| 12714 | * systems. |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 12715 | */ |
| 12716 | static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize) |
| 12717 | { |
| 12718 | struct hlua_mem_allocator *zone = ud; |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 12719 | size_t limit, old, new; |
| 12720 | |
| 12721 | /* a limit of ~0 means unlimited and boot complete, so there's no need |
| 12722 | * for accounting anymore. |
| 12723 | */ |
Willy Tarreau | a5efdff | 2021-10-22 16:00:02 +0200 | [diff] [blame] | 12724 | if (likely(~zone->limit == 0)) { |
| 12725 | if (!nsize) |
| 12726 | ha_free(&ptr); |
| 12727 | else |
| 12728 | ptr = realloc(ptr, nsize); |
| 12729 | return ptr; |
| 12730 | } |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 12731 | |
Willy Tarreau | d36c7fa | 2020-12-02 12:26:29 +0100 | [diff] [blame] | 12732 | if (!ptr) |
| 12733 | osize = 0; |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 12734 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 12735 | /* enforce strict limits across all threads */ |
| 12736 | limit = zone->limit; |
| 12737 | old = _HA_ATOMIC_LOAD(&zone->allocated); |
| 12738 | do { |
| 12739 | new = old + nsize - osize; |
| 12740 | if (unlikely(nsize && limit && new > limit)) |
| 12741 | return NULL; |
| 12742 | } while (!_HA_ATOMIC_CAS(&zone->allocated, &old, new)); |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 12743 | |
Willy Tarreau | a5efdff | 2021-10-22 16:00:02 +0200 | [diff] [blame] | 12744 | if (!nsize) |
| 12745 | ha_free(&ptr); |
| 12746 | else |
| 12747 | ptr = realloc(ptr, nsize); |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 12748 | |
| 12749 | if (unlikely(!ptr && nsize)) // failed |
| 12750 | _HA_ATOMIC_SUB(&zone->allocated, nsize - osize); |
| 12751 | |
| 12752 | __ha_barrier_atomic_store(); |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 12753 | return ptr; |
| 12754 | } |
| 12755 | |
Thierry Fournier | ecb83c2 | 2020-11-28 15:49:44 +0100 | [diff] [blame] | 12756 | /* 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] | 12757 | * We are in the initialisation process of HAProxy, this abort() is |
| 12758 | * tolerated. |
| 12759 | */ |
Thierry Fournier | ecb83c2 | 2020-11-28 15:49:44 +0100 | [diff] [blame] | 12760 | lua_State *hlua_init_state(int thread_num) |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 12761 | { |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 12762 | int i; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 12763 | int idx; |
| 12764 | struct sample_fetch *sf; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 12765 | struct sample_conv *sc; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 12766 | char *p; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 12767 | const char *error_msg; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 12768 | void **context; |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12769 | lua_State *L; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 12770 | struct prepend_path *pp; |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 12771 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 12772 | /* Init main lua stack. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12773 | L = lua_newstate(hlua_alloc, &hlua_global_allocator); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 12774 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 12775 | /* Initialise Lua context to NULL */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12776 | context = lua_getextraspace(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 12777 | *context = NULL; |
| 12778 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 12779 | /* From this point, until the end of the initialisation function, |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 12780 | * the Lua function can fail with an abort. We are in the initialisation |
| 12781 | * process of HAProxy, this abort() is tolerated. |
| 12782 | */ |
| 12783 | |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 12784 | /* Call post initialisation function in safe environment. */ |
| 12785 | if (setjmp(safe_ljmp_env) != 0) { |
| 12786 | lua_atpanic(L, hlua_panic_safe); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12787 | if (lua_type(L, -1) == LUA_TSTRING) |
| 12788 | error_msg = lua_tostring(L, -1); |
Thierry Fournier | 2f05cc6 | 2020-11-28 16:08:02 +0100 | [diff] [blame] | 12789 | else |
| 12790 | error_msg = "critical error"; |
| 12791 | fprintf(stderr, "Lua init: %s.\n", error_msg); |
| 12792 | exit(1); |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 12793 | } else { |
| 12794 | lua_atpanic(L, hlua_panic_ljmp); |
Thierry Fournier | 2f05cc6 | 2020-11-28 16:08:02 +0100 | [diff] [blame] | 12795 | } |
| 12796 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 12797 | /* Initialise lua. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12798 | luaL_openlibs(L); |
Tim Duesterhus | 541fe1e | 2020-01-12 13:55:41 +0100 | [diff] [blame] | 12799 | #define HLUA_PREPEND_PATH_TOSTRING1(x) #x |
| 12800 | #define HLUA_PREPEND_PATH_TOSTRING(x) HLUA_PREPEND_PATH_TOSTRING1(x) |
| 12801 | #ifdef HLUA_PREPEND_PATH |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12802 | hlua_prepend_path(L, "path", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_PATH)); |
Tim Duesterhus | 541fe1e | 2020-01-12 13:55:41 +0100 | [diff] [blame] | 12803 | #endif |
| 12804 | #ifdef HLUA_PREPEND_CPATH |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12805 | hlua_prepend_path(L, "cpath", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_CPATH)); |
Tim Duesterhus | 541fe1e | 2020-01-12 13:55:41 +0100 | [diff] [blame] | 12806 | #endif |
| 12807 | #undef HLUA_PREPEND_PATH_TOSTRING |
| 12808 | #undef HLUA_PREPEND_PATH_TOSTRING1 |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 12809 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 12810 | /* Apply configured prepend path */ |
| 12811 | list_for_each_entry(pp, &prepend_path_list, l) |
| 12812 | hlua_prepend_path(L, pp->type, pp->path); |
| 12813 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 12814 | /* |
| 12815 | * |
| 12816 | * Create "core" object. |
| 12817 | * |
| 12818 | */ |
| 12819 | |
Thierry FOURNIER | a2d8c65 | 2015-03-11 17:29:39 +0100 | [diff] [blame] | 12820 | /* This table entry is the object "core" base. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12821 | lua_newtable(L); |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 12822 | |
Thierry Fournier | ecb83c2 | 2020-11-28 15:49:44 +0100 | [diff] [blame] | 12823 | /* set the thread id */ |
| 12824 | hlua_class_const_int(L, "thread", thread_num); |
| 12825 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 12826 | /* Push the loglevel constants. */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 12827 | for (i = 0; i < NB_LOG_LEVELS; i++) |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12828 | hlua_class_const_int(L, log_levels[i], i); |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 12829 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 12830 | /* Register special functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12831 | hlua_class_function(L, "register_init", hlua_register_init); |
| 12832 | hlua_class_function(L, "register_task", hlua_register_task); |
| 12833 | hlua_class_function(L, "register_fetches", hlua_register_fetches); |
| 12834 | hlua_class_function(L, "register_converters", hlua_register_converters); |
| 12835 | hlua_class_function(L, "register_action", hlua_register_action); |
| 12836 | hlua_class_function(L, "register_service", hlua_register_service); |
| 12837 | hlua_class_function(L, "register_cli", hlua_register_cli); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 12838 | hlua_class_function(L, "register_filter", hlua_register_filter); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12839 | hlua_class_function(L, "yield", hlua_yield); |
| 12840 | hlua_class_function(L, "set_nice", hlua_set_nice); |
| 12841 | hlua_class_function(L, "sleep", hlua_sleep); |
| 12842 | hlua_class_function(L, "msleep", hlua_msleep); |
| 12843 | hlua_class_function(L, "add_acl", hlua_add_acl); |
| 12844 | hlua_class_function(L, "del_acl", hlua_del_acl); |
| 12845 | hlua_class_function(L, "set_map", hlua_set_map); |
| 12846 | hlua_class_function(L, "del_map", hlua_del_map); |
| 12847 | hlua_class_function(L, "tcp", hlua_socket_new); |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 12848 | hlua_class_function(L, "httpclient", hlua_httpclient_new); |
Aurelien DARRAGON | c84899c | 2023-02-20 18:18:59 +0100 | [diff] [blame] | 12849 | hlua_class_function(L, "event_sub", hlua_event_global_sub); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12850 | hlua_class_function(L, "log", hlua_log); |
| 12851 | hlua_class_function(L, "Debug", hlua_log_debug); |
| 12852 | hlua_class_function(L, "Info", hlua_log_info); |
| 12853 | hlua_class_function(L, "Warning", hlua_log_warning); |
| 12854 | hlua_class_function(L, "Alert", hlua_log_alert); |
| 12855 | hlua_class_function(L, "done", hlua_done); |
| 12856 | hlua_fcn_reg_core_fcn(L); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 12857 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12858 | lua_setglobal(L, "core"); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 12859 | |
| 12860 | /* |
| 12861 | * |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 12862 | * Create "act" object. |
| 12863 | * |
| 12864 | */ |
| 12865 | |
| 12866 | /* This table entry is the object "act" base. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12867 | lua_newtable(L); |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 12868 | |
| 12869 | /* push action return constants */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12870 | hlua_class_const_int(L, "CONTINUE", ACT_RET_CONT); |
| 12871 | hlua_class_const_int(L, "STOP", ACT_RET_STOP); |
| 12872 | hlua_class_const_int(L, "YIELD", ACT_RET_YIELD); |
| 12873 | hlua_class_const_int(L, "ERROR", ACT_RET_ERR); |
| 12874 | hlua_class_const_int(L, "DONE", ACT_RET_DONE); |
| 12875 | hlua_class_const_int(L, "DENY", ACT_RET_DENY); |
| 12876 | hlua_class_const_int(L, "ABORT", ACT_RET_ABRT); |
| 12877 | hlua_class_const_int(L, "INVALID", ACT_RET_INV); |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 12878 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12879 | hlua_class_function(L, "wake_time", hlua_set_wake_time); |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 12880 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12881 | lua_setglobal(L, "act"); |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 12882 | |
| 12883 | /* |
| 12884 | * |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 12885 | * Create "Filter" object. |
| 12886 | * |
| 12887 | */ |
| 12888 | |
| 12889 | /* This table entry is the object "filter" base. */ |
| 12890 | lua_newtable(L); |
| 12891 | |
| 12892 | /* push flags and constants */ |
| 12893 | hlua_class_const_int(L, "CONTINUE", 1); |
| 12894 | hlua_class_const_int(L, "WAIT", 0); |
| 12895 | hlua_class_const_int(L, "ERROR", -1); |
| 12896 | |
| 12897 | hlua_class_const_int(L, "FLT_CFG_FL_HTX", FLT_CFG_FL_HTX); |
| 12898 | |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 12899 | hlua_class_function(L, "wake_time", hlua_set_wake_time); |
| 12900 | hlua_class_function(L, "register_data_filter", hlua_register_data_filter); |
| 12901 | hlua_class_function(L, "unregister_data_filter", hlua_unregister_data_filter); |
| 12902 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 12903 | lua_setglobal(L, "filter"); |
| 12904 | |
| 12905 | /* |
| 12906 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12907 | * Register class Map |
| 12908 | * |
| 12909 | */ |
| 12910 | |
| 12911 | /* This table entry is the object "Map" base. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12912 | lua_newtable(L); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12913 | |
| 12914 | /* register pattern types. */ |
| 12915 | for (i=0; i<PAT_MATCH_NUM; i++) |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12916 | hlua_class_const_int(L, pat_match_names[i], i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 12917 | for (i=0; i<PAT_MATCH_NUM; i++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 12918 | snprintf(trash.area, trash.size, "_%s", pat_match_names[i]); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12919 | hlua_class_const_int(L, trash.area, i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 12920 | } |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12921 | |
| 12922 | /* register constructor. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12923 | hlua_class_function(L, "new", hlua_map_new); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12924 | |
| 12925 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12926 | lua_newtable(L); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12927 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 12928 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12929 | lua_pushstring(L, "__index"); |
| 12930 | lua_newtable(L); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12931 | |
| 12932 | /* Register . */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12933 | hlua_class_function(L, "lookup", hlua_map_lookup); |
| 12934 | hlua_class_function(L, "slookup", hlua_map_slookup); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12935 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12936 | lua_rawset(L, -3); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12937 | |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 12938 | /* Register previous table in the registry with reference and named entry. |
| 12939 | * The function hlua_register_metatable() pops the stack, so we |
| 12940 | * previously create a copy of the table. |
| 12941 | */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12942 | lua_pushvalue(L, -1); /* Copy the -1 entry and push it on the stack. */ |
| 12943 | class_map_ref = hlua_register_metatable(L, CLASS_MAP); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12944 | |
| 12945 | /* Assign the metatable to the mai Map object. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12946 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12947 | |
| 12948 | /* Set a name to the table. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12949 | lua_setglobal(L, "Map"); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12950 | |
| 12951 | /* |
| 12952 | * |
William Lallemand | 30fcca1 | 2022-03-30 12:03:12 +0200 | [diff] [blame] | 12953 | * Register "CertCache" class |
| 12954 | * |
| 12955 | */ |
| 12956 | |
| 12957 | /* Create and fill the metatable. */ |
| 12958 | lua_newtable(L); |
| 12959 | /* Register */ |
| 12960 | hlua_class_function(L, "set", hlua_ckch_set); |
| 12961 | lua_setglobal(L, CLASS_CERTCACHE); /* Create global object called Regex */ |
| 12962 | |
| 12963 | /* |
| 12964 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 12965 | * Register class Channel |
| 12966 | * |
| 12967 | */ |
| 12968 | |
| 12969 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12970 | lua_newtable(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 12971 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 12972 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12973 | lua_pushstring(L, "__index"); |
| 12974 | lua_newtable(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 12975 | |
| 12976 | /* Register . */ |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 12977 | hlua_class_function(L, "data", hlua_channel_get_data); |
| 12978 | hlua_class_function(L, "line", hlua_channel_get_line); |
| 12979 | hlua_class_function(L, "set", hlua_channel_set_data); |
| 12980 | hlua_class_function(L, "remove", hlua_channel_del_data); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12981 | hlua_class_function(L, "append", hlua_channel_append); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 12982 | hlua_class_function(L, "prepend", hlua_channel_prepend); |
| 12983 | hlua_class_function(L, "insert", hlua_channel_insert_data); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12984 | hlua_class_function(L, "send", hlua_channel_send); |
| 12985 | hlua_class_function(L, "forward", hlua_channel_forward); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 12986 | hlua_class_function(L, "input", hlua_channel_get_in_len); |
| 12987 | hlua_class_function(L, "output", hlua_channel_get_out_len); |
| 12988 | hlua_class_function(L, "may_recv", hlua_channel_may_recv); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12989 | hlua_class_function(L, "is_full", hlua_channel_is_full); |
| 12990 | hlua_class_function(L, "is_resp", hlua_channel_is_resp); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 12991 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 12992 | /* Deprecated API */ |
| 12993 | hlua_class_function(L, "get", hlua_channel_get); |
| 12994 | hlua_class_function(L, "dup", hlua_channel_dup); |
| 12995 | hlua_class_function(L, "getline", hlua_channel_getline); |
| 12996 | hlua_class_function(L, "get_in_len", hlua_channel_get_in_len); |
| 12997 | hlua_class_function(L, "get_out_len", hlua_channel_get_out_len); |
| 12998 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12999 | lua_rawset(L, -3); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 13000 | |
| 13001 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13002 | class_channel_ref = hlua_register_metatable(L, CLASS_CHANNEL); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 13003 | |
| 13004 | /* |
| 13005 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 13006 | * Register class Fetches |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 13007 | * |
| 13008 | */ |
| 13009 | |
| 13010 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13011 | lua_newtable(L); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 13012 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 13013 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13014 | lua_pushstring(L, "__index"); |
| 13015 | lua_newtable(L); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 13016 | |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 13017 | /* Browse existing fetches and create the associated |
| 13018 | * object method. |
| 13019 | */ |
| 13020 | sf = NULL; |
| 13021 | while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) { |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 13022 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 13023 | * by an underscore. |
| 13024 | */ |
Willy Tarreau | 5ef9656 | 2021-08-26 16:48:53 +0200 | [diff] [blame] | 13025 | strlcpy2(trash.area, sf->kw, trash.size); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 13026 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 13027 | if (*p == '.' || *p == '-' || *p == '+') |
| 13028 | *p = '_'; |
| 13029 | |
| 13030 | /* Register the function. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13031 | lua_pushstring(L, trash.area); |
| 13032 | lua_pushlightuserdata(L, sf); |
| 13033 | lua_pushcclosure(L, hlua_run_sample_fetch, 1); |
| 13034 | lua_rawset(L, -3); |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 13035 | } |
| 13036 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13037 | lua_rawset(L, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 13038 | |
| 13039 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13040 | class_fetches_ref = hlua_register_metatable(L, CLASS_FETCHES); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 13041 | |
| 13042 | /* |
| 13043 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 13044 | * Register class Converters |
| 13045 | * |
| 13046 | */ |
| 13047 | |
| 13048 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13049 | lua_newtable(L); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 13050 | |
| 13051 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13052 | lua_pushstring(L, "__index"); |
| 13053 | lua_newtable(L); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 13054 | |
| 13055 | /* Browse existing converters and create the associated |
| 13056 | * object method. |
| 13057 | */ |
| 13058 | sc = NULL; |
| 13059 | while ((sc = sample_conv_getnext(sc, &idx)) != NULL) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 13060 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 13061 | * by an underscore. |
| 13062 | */ |
Willy Tarreau | 5ef9656 | 2021-08-26 16:48:53 +0200 | [diff] [blame] | 13063 | strlcpy2(trash.area, sc->kw, trash.size); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 13064 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 13065 | if (*p == '.' || *p == '-' || *p == '+') |
| 13066 | *p = '_'; |
| 13067 | |
| 13068 | /* Register the function. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13069 | lua_pushstring(L, trash.area); |
| 13070 | lua_pushlightuserdata(L, sc); |
| 13071 | lua_pushcclosure(L, hlua_run_sample_conv, 1); |
| 13072 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 13073 | } |
| 13074 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13075 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 13076 | |
| 13077 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13078 | class_converters_ref = hlua_register_metatable(L, CLASS_CONVERTERS); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 13079 | |
| 13080 | /* |
| 13081 | * |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 13082 | * Register class HTTP |
| 13083 | * |
| 13084 | */ |
| 13085 | |
| 13086 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13087 | lua_newtable(L); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 13088 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 13089 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13090 | lua_pushstring(L, "__index"); |
| 13091 | lua_newtable(L); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 13092 | |
| 13093 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13094 | hlua_class_function(L, "req_get_headers",hlua_http_req_get_headers); |
| 13095 | hlua_class_function(L, "req_del_header", hlua_http_req_del_hdr); |
| 13096 | hlua_class_function(L, "req_rep_header", hlua_http_req_rep_hdr); |
| 13097 | hlua_class_function(L, "req_rep_value", hlua_http_req_rep_val); |
| 13098 | hlua_class_function(L, "req_add_header", hlua_http_req_add_hdr); |
| 13099 | hlua_class_function(L, "req_set_header", hlua_http_req_set_hdr); |
| 13100 | hlua_class_function(L, "req_set_method", hlua_http_req_set_meth); |
| 13101 | hlua_class_function(L, "req_set_path", hlua_http_req_set_path); |
| 13102 | hlua_class_function(L, "req_set_query", hlua_http_req_set_query); |
| 13103 | hlua_class_function(L, "req_set_uri", hlua_http_req_set_uri); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 13104 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13105 | hlua_class_function(L, "res_get_headers",hlua_http_res_get_headers); |
| 13106 | hlua_class_function(L, "res_del_header", hlua_http_res_del_hdr); |
| 13107 | hlua_class_function(L, "res_rep_header", hlua_http_res_rep_hdr); |
| 13108 | hlua_class_function(L, "res_rep_value", hlua_http_res_rep_val); |
| 13109 | hlua_class_function(L, "res_add_header", hlua_http_res_add_hdr); |
| 13110 | hlua_class_function(L, "res_set_header", hlua_http_res_set_hdr); |
| 13111 | hlua_class_function(L, "res_set_status", hlua_http_res_set_status); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 13112 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13113 | lua_rawset(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 13114 | |
| 13115 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13116 | class_http_ref = hlua_register_metatable(L, CLASS_HTTP); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 13117 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 13118 | /* |
| 13119 | * |
| 13120 | * Register class HTTPMessage |
| 13121 | * |
| 13122 | */ |
| 13123 | |
| 13124 | /* Create and fill the metatable. */ |
| 13125 | lua_newtable(L); |
| 13126 | |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 13127 | /* Create and fill the __index entry. */ |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 13128 | lua_pushstring(L, "__index"); |
| 13129 | lua_newtable(L); |
| 13130 | |
| 13131 | /* Register Lua functions. */ |
| 13132 | hlua_class_function(L, "is_resp", hlua_http_msg_is_resp); |
| 13133 | hlua_class_function(L, "get_stline", hlua_http_msg_get_stline); |
| 13134 | hlua_class_function(L, "get_headers", hlua_http_msg_get_headers); |
| 13135 | hlua_class_function(L, "del_header", hlua_http_msg_del_hdr); |
| 13136 | hlua_class_function(L, "rep_header", hlua_http_msg_rep_hdr); |
| 13137 | hlua_class_function(L, "rep_value", hlua_http_msg_rep_val); |
| 13138 | hlua_class_function(L, "add_header", hlua_http_msg_add_hdr); |
| 13139 | hlua_class_function(L, "set_header", hlua_http_msg_set_hdr); |
| 13140 | hlua_class_function(L, "set_method", hlua_http_msg_set_meth); |
| 13141 | hlua_class_function(L, "set_path", hlua_http_msg_set_path); |
| 13142 | hlua_class_function(L, "set_query", hlua_http_msg_set_query); |
| 13143 | hlua_class_function(L, "set_uri", hlua_http_msg_set_uri); |
| 13144 | hlua_class_function(L, "set_status", hlua_http_msg_set_status); |
| 13145 | hlua_class_function(L, "is_full", hlua_http_msg_is_full); |
| 13146 | hlua_class_function(L, "may_recv", hlua_http_msg_may_recv); |
| 13147 | hlua_class_function(L, "eom", hlua_http_msg_is_eom); |
| 13148 | hlua_class_function(L, "input", hlua_http_msg_get_in_len); |
| 13149 | hlua_class_function(L, "output", hlua_http_msg_get_out_len); |
| 13150 | |
| 13151 | hlua_class_function(L, "body", hlua_http_msg_get_body); |
| 13152 | hlua_class_function(L, "set", hlua_http_msg_set_data); |
| 13153 | hlua_class_function(L, "remove", hlua_http_msg_del_data); |
| 13154 | hlua_class_function(L, "append", hlua_http_msg_append); |
| 13155 | hlua_class_function(L, "prepend", hlua_http_msg_prepend); |
| 13156 | hlua_class_function(L, "insert", hlua_http_msg_insert_data); |
| 13157 | hlua_class_function(L, "set_eom", hlua_http_msg_set_eom); |
| 13158 | hlua_class_function(L, "unset_eom", hlua_http_msg_unset_eom); |
| 13159 | |
| 13160 | hlua_class_function(L, "send", hlua_http_msg_send); |
| 13161 | hlua_class_function(L, "forward", hlua_http_msg_forward); |
| 13162 | |
| 13163 | lua_rawset(L, -3); |
| 13164 | |
| 13165 | /* Register previous table in the registry with reference and named entry. */ |
| 13166 | class_http_msg_ref = hlua_register_metatable(L, CLASS_HTTP_MSG); |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 13167 | |
| 13168 | /* |
| 13169 | * |
| 13170 | * Register class HTTPClient |
| 13171 | * |
| 13172 | */ |
| 13173 | |
| 13174 | /* Create and fill the metatable. */ |
| 13175 | lua_newtable(L); |
| 13176 | lua_pushstring(L, "__index"); |
| 13177 | lua_newtable(L); |
| 13178 | hlua_class_function(L, "get", hlua_httpclient_get); |
William Lallemand | dc2cc90 | 2021-10-26 11:43:26 +0200 | [diff] [blame] | 13179 | hlua_class_function(L, "head", hlua_httpclient_head); |
| 13180 | hlua_class_function(L, "put", hlua_httpclient_put); |
| 13181 | hlua_class_function(L, "post", hlua_httpclient_post); |
| 13182 | hlua_class_function(L, "delete", hlua_httpclient_delete); |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 13183 | lua_settable(L, -3); /* Sets the __index entry. */ |
William Lallemand | f77f1de | 2021-09-28 19:10:38 +0200 | [diff] [blame] | 13184 | /* Register the garbage collector entry. */ |
| 13185 | lua_pushstring(L, "__gc"); |
| 13186 | lua_pushcclosure(L, hlua_httpclient_gc, 0); |
| 13187 | lua_settable(L, -3); /* Push the last 2 entries in the table at index -3 */ |
| 13188 | |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 13189 | |
| 13190 | |
| 13191 | class_httpclient_ref = hlua_register_metatable(L, CLASS_HTTPCLIENT); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 13192 | /* |
| 13193 | * |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 13194 | * Register class AppletTCP |
| 13195 | * |
| 13196 | */ |
| 13197 | |
| 13198 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13199 | lua_newtable(L); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 13200 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 13201 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13202 | lua_pushstring(L, "__index"); |
| 13203 | lua_newtable(L); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 13204 | |
| 13205 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13206 | hlua_class_function(L, "getline", hlua_applet_tcp_getline); |
| 13207 | hlua_class_function(L, "receive", hlua_applet_tcp_recv); |
| 13208 | hlua_class_function(L, "send", hlua_applet_tcp_send); |
| 13209 | hlua_class_function(L, "set_priv", hlua_applet_tcp_set_priv); |
| 13210 | hlua_class_function(L, "get_priv", hlua_applet_tcp_get_priv); |
| 13211 | hlua_class_function(L, "set_var", hlua_applet_tcp_set_var); |
| 13212 | hlua_class_function(L, "unset_var", hlua_applet_tcp_unset_var); |
| 13213 | hlua_class_function(L, "get_var", hlua_applet_tcp_get_var); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 13214 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13215 | lua_settable(L, -3); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 13216 | |
| 13217 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13218 | class_applet_tcp_ref = hlua_register_metatable(L, CLASS_APPLET_TCP); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 13219 | |
| 13220 | /* |
| 13221 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 13222 | * Register class AppletHTTP |
| 13223 | * |
| 13224 | */ |
| 13225 | |
| 13226 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13227 | lua_newtable(L); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 13228 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 13229 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13230 | lua_pushstring(L, "__index"); |
| 13231 | lua_newtable(L); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 13232 | |
| 13233 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13234 | hlua_class_function(L, "set_priv", hlua_applet_http_set_priv); |
| 13235 | hlua_class_function(L, "get_priv", hlua_applet_http_get_priv); |
| 13236 | hlua_class_function(L, "set_var", hlua_applet_http_set_var); |
| 13237 | hlua_class_function(L, "unset_var", hlua_applet_http_unset_var); |
| 13238 | hlua_class_function(L, "get_var", hlua_applet_http_get_var); |
| 13239 | hlua_class_function(L, "getline", hlua_applet_http_getline); |
| 13240 | hlua_class_function(L, "receive", hlua_applet_http_recv); |
| 13241 | hlua_class_function(L, "send", hlua_applet_http_send); |
| 13242 | hlua_class_function(L, "add_header", hlua_applet_http_addheader); |
| 13243 | hlua_class_function(L, "set_status", hlua_applet_http_status); |
| 13244 | hlua_class_function(L, "start_response", hlua_applet_http_start_response); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 13245 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13246 | lua_settable(L, -3); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 13247 | |
| 13248 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13249 | class_applet_http_ref = hlua_register_metatable(L, CLASS_APPLET_HTTP); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 13250 | |
| 13251 | /* |
| 13252 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 13253 | * Register class TXN |
| 13254 | * |
| 13255 | */ |
| 13256 | |
| 13257 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13258 | lua_newtable(L); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 13259 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 13260 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13261 | lua_pushstring(L, "__index"); |
| 13262 | lua_newtable(L); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 13263 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 13264 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13265 | hlua_class_function(L, "set_priv", hlua_set_priv); |
| 13266 | hlua_class_function(L, "get_priv", hlua_get_priv); |
| 13267 | hlua_class_function(L, "set_var", hlua_set_var); |
| 13268 | hlua_class_function(L, "unset_var", hlua_unset_var); |
| 13269 | hlua_class_function(L, "get_var", hlua_get_var); |
| 13270 | hlua_class_function(L, "done", hlua_txn_done); |
| 13271 | hlua_class_function(L, "reply", hlua_txn_reply_new); |
| 13272 | hlua_class_function(L, "set_loglevel", hlua_txn_set_loglevel); |
| 13273 | hlua_class_function(L, "set_tos", hlua_txn_set_tos); |
| 13274 | hlua_class_function(L, "set_mark", hlua_txn_set_mark); |
| 13275 | hlua_class_function(L, "set_priority_class", hlua_txn_set_priority_class); |
| 13276 | hlua_class_function(L, "set_priority_offset", hlua_txn_set_priority_offset); |
| 13277 | hlua_class_function(L, "deflog", hlua_txn_deflog); |
| 13278 | hlua_class_function(L, "log", hlua_txn_log); |
| 13279 | hlua_class_function(L, "Debug", hlua_txn_log_debug); |
| 13280 | hlua_class_function(L, "Info", hlua_txn_log_info); |
| 13281 | hlua_class_function(L, "Warning", hlua_txn_log_warning); |
| 13282 | hlua_class_function(L, "Alert", hlua_txn_log_alert); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 13283 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13284 | lua_rawset(L, -3); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 13285 | |
| 13286 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13287 | class_txn_ref = hlua_register_metatable(L, CLASS_TXN); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 13288 | |
| 13289 | /* |
| 13290 | * |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 13291 | * Register class reply |
| 13292 | * |
| 13293 | */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13294 | lua_newtable(L); |
| 13295 | lua_pushstring(L, "__index"); |
| 13296 | lua_newtable(L); |
| 13297 | hlua_class_function(L, "set_status", hlua_txn_reply_set_status); |
| 13298 | hlua_class_function(L, "add_header", hlua_txn_reply_add_header); |
| 13299 | hlua_class_function(L, "del_header", hlua_txn_reply_del_header); |
| 13300 | hlua_class_function(L, "set_body", hlua_txn_reply_set_body); |
| 13301 | lua_settable(L, -3); /* Sets the __index entry. */ |
| 13302 | class_txn_reply_ref = luaL_ref(L, LUA_REGISTRYINDEX); |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 13303 | |
| 13304 | |
| 13305 | /* |
| 13306 | * |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 13307 | * Register class Socket |
| 13308 | * |
| 13309 | */ |
| 13310 | |
| 13311 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13312 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 13313 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 13314 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13315 | lua_pushstring(L, "__index"); |
| 13316 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 13317 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 13318 | #ifdef USE_OPENSSL |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13319 | hlua_class_function(L, "connect_ssl", hlua_socket_connect_ssl); |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 13320 | #endif |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13321 | hlua_class_function(L, "connect", hlua_socket_connect); |
| 13322 | hlua_class_function(L, "send", hlua_socket_send); |
| 13323 | hlua_class_function(L, "receive", hlua_socket_receive); |
| 13324 | hlua_class_function(L, "close", hlua_socket_close); |
| 13325 | hlua_class_function(L, "getpeername", hlua_socket_getpeername); |
| 13326 | hlua_class_function(L, "getsockname", hlua_socket_getsockname); |
| 13327 | hlua_class_function(L, "setoption", hlua_socket_setoption); |
| 13328 | hlua_class_function(L, "settimeout", hlua_socket_settimeout); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 13329 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13330 | 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] | 13331 | |
| 13332 | /* Register the garbage collector entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13333 | lua_pushstring(L, "__gc"); |
| 13334 | lua_pushcclosure(L, hlua_socket_gc, 0); |
| 13335 | 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] | 13336 | |
| 13337 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 13338 | class_socket_ref = hlua_register_metatable(L, CLASS_SOCKET); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 13339 | |
Thierry Fournier | aafc777 | 2020-12-04 11:47:47 +0100 | [diff] [blame] | 13340 | lua_atpanic(L, hlua_panic_safe); |
| 13341 | |
| 13342 | return L; |
| 13343 | } |
| 13344 | |
| 13345 | void hlua_init(void) { |
| 13346 | int i; |
Amaury Denoyelle | 239fdbf | 2021-03-24 10:22:03 +0100 | [diff] [blame] | 13347 | char *errmsg; |
Thierry Fournier | aafc777 | 2020-12-04 11:47:47 +0100 | [diff] [blame] | 13348 | #ifdef USE_OPENSSL |
| 13349 | struct srv_kw *kw; |
| 13350 | int tmp_error; |
| 13351 | char *error; |
| 13352 | char *args[] = { /* SSL client configuration. */ |
| 13353 | "ssl", |
| 13354 | "verify", |
| 13355 | "none", |
| 13356 | NULL |
| 13357 | }; |
| 13358 | #endif |
| 13359 | |
| 13360 | /* Init post init function list head */ |
| 13361 | for (i = 0; i < MAX_THREADS + 1; i++) |
| 13362 | LIST_INIT(&hlua_init_functions[i]); |
| 13363 | |
| 13364 | /* Init state for common/shared lua parts */ |
| 13365 | hlua_state_id = 0; |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 13366 | ha_set_thread(NULL); |
Thierry Fournier | aafc777 | 2020-12-04 11:47:47 +0100 | [diff] [blame] | 13367 | hlua_states[0] = hlua_init_state(0); |
| 13368 | |
| 13369 | /* Init state 1 for thread 0. We have at least one thread. */ |
| 13370 | hlua_state_id = 1; |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 13371 | ha_set_thread(NULL); |
Thierry Fournier | aafc777 | 2020-12-04 11:47:47 +0100 | [diff] [blame] | 13372 | hlua_states[1] = hlua_init_state(1); |
| 13373 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 13374 | /* Proxy and server configuration initialisation. */ |
William Lallemand | 6bb77b9 | 2021-07-28 15:48:16 +0200 | [diff] [blame] | 13375 | 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] | 13376 | if (!socket_proxy) { |
| 13377 | fprintf(stderr, "Lua init: %s\n", errmsg); |
| 13378 | exit(1); |
| 13379 | } |
| 13380 | proxy_preset_defaults(socket_proxy); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 13381 | |
| 13382 | /* Init TCP server: unchanged parameters */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 13383 | socket_tcp = new_server(socket_proxy); |
| 13384 | if (!socket_tcp) { |
| 13385 | fprintf(stderr, "Lua init: failed to allocate tcp server socket\n"); |
| 13386 | exit(1); |
| 13387 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 13388 | |
| 13389 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 13390 | /* Init TCP server: unchanged parameters */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 13391 | socket_ssl = new_server(socket_proxy); |
| 13392 | if (!socket_ssl) { |
| 13393 | fprintf(stderr, "Lua init: failed to allocate ssl server socket\n"); |
| 13394 | exit(1); |
| 13395 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 13396 | |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 13397 | socket_ssl->use_ssl = 1; |
| 13398 | socket_ssl->xprt = xprt_get(XPRT_SSL); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 13399 | |
Bertrand Jacquin | 80839ff | 2021-01-21 19:14:46 +0000 | [diff] [blame] | 13400 | for (i = 0; args[i] != NULL; i++) { |
| 13401 | 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] | 13402 | /* |
| 13403 | * |
| 13404 | * If the keyword is not known, we can search in the registered |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 13405 | * server keywords. This is useful to configure special SSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 13406 | * features like client certificates and ssl_verify. |
| 13407 | * |
| 13408 | */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 13409 | tmp_error = kw->parse(args, &i, socket_proxy, socket_ssl, &error); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 13410 | if (tmp_error != 0) { |
| 13411 | fprintf(stderr, "INTERNAL ERROR: %s\n", error); |
| 13412 | abort(); /* This must be never arrives because the command line |
| 13413 | not editable by the user. */ |
| 13414 | } |
Bertrand Jacquin | 80839ff | 2021-01-21 19:14:46 +0000 | [diff] [blame] | 13415 | i += kw->skip; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 13416 | } |
| 13417 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 13418 | #endif |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 13419 | |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 13420 | } |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 13421 | |
Tim Duesterhus | d0c0ca2 | 2020-07-04 11:53:26 +0200 | [diff] [blame] | 13422 | static void hlua_deinit() |
| 13423 | { |
Willy Tarreau | 186f376 | 2020-12-04 11:48:12 +0100 | [diff] [blame] | 13424 | int thr; |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 13425 | struct hlua_reg_filter *reg_flt, *reg_flt_bck; |
| 13426 | |
| 13427 | list_for_each_entry_safe(reg_flt, reg_flt_bck, &referenced_filters, l) |
| 13428 | release_hlua_reg_filter(reg_flt); |
Willy Tarreau | 186f376 | 2020-12-04 11:48:12 +0100 | [diff] [blame] | 13429 | |
| 13430 | for (thr = 0; thr < MAX_THREADS+1; thr++) { |
| 13431 | if (hlua_states[thr]) |
| 13432 | lua_close(hlua_states[thr]); |
| 13433 | } |
Willy Tarreau | 430bf4a | 2021-03-04 09:45:32 +0100 | [diff] [blame] | 13434 | |
Amaury Denoyelle | bc2ebfa | 2021-08-25 15:34:53 +0200 | [diff] [blame] | 13435 | srv_drop(socket_tcp); |
Willy Tarreau | 430bf4a | 2021-03-04 09:45:32 +0100 | [diff] [blame] | 13436 | |
Willy Tarreau | 0f143af | 2021-03-05 10:41:48 +0100 | [diff] [blame] | 13437 | #ifdef USE_OPENSSL |
Amaury Denoyelle | bc2ebfa | 2021-08-25 15:34:53 +0200 | [diff] [blame] | 13438 | srv_drop(socket_ssl); |
Willy Tarreau | 0f143af | 2021-03-05 10:41:48 +0100 | [diff] [blame] | 13439 | #endif |
Amaury Denoyelle | 239fdbf | 2021-03-24 10:22:03 +0100 | [diff] [blame] | 13440 | |
| 13441 | free_proxy(socket_proxy); |
Tim Duesterhus | d0c0ca2 | 2020-07-04 11:53:26 +0200 | [diff] [blame] | 13442 | } |
| 13443 | |
| 13444 | REGISTER_POST_DEINIT(hlua_deinit); |
| 13445 | |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 13446 | static void hlua_register_build_options(void) |
| 13447 | { |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 13448 | char *ptr = NULL; |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 13449 | |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 13450 | memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE); |
| 13451 | hap_register_build_opts(ptr, 1); |
| 13452 | } |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 13453 | |
| 13454 | INITCALL0(STG_REGISTER, hlua_register_build_options); |