Thierry Fournier | e726b14 | 2016-02-11 17:57:57 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Lua unsafe core engine |
| 3 | * |
| 4 | * Copyright 2015-2016 Thierry Fournier <tfournier@arpalert.org> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | */ |
| 12 | |
Bertrand Jacquin | f4c12d4 | 2021-01-21 21:14:07 +0000 | [diff] [blame] | 13 | #define _GNU_SOURCE |
| 14 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 15 | #include <ctype.h> |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 16 | #include <setjmp.h> |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 17 | |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 18 | #include <lauxlib.h> |
| 19 | #include <lua.h> |
| 20 | #include <lualib.h> |
| 21 | |
Thierry FOURNIER | 463119c | 2015-03-10 00:35:36 +0100 | [diff] [blame] | 22 | #if !defined(LUA_VERSION_NUM) || LUA_VERSION_NUM < 503 |
| 23 | #error "Requires Lua 5.3 or later." |
Cyril Bonté | dc0306e | 2015-03-02 00:08:40 +0100 | [diff] [blame] | 24 | #endif |
| 25 | |
Willy Tarreau | 8d2b777 | 2020-05-27 10:58:19 +0200 | [diff] [blame] | 26 | #include <import/ebpttree.h> |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 27 | |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 28 | #include <haproxy/api.h> |
| 29 | #include <haproxy/applet.h> |
Willy Tarreau | dfd3de8 | 2020-06-04 23:46:14 +0200 | [diff] [blame] | 30 | #include <haproxy/arg.h> |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 31 | #include <haproxy/auth.h> |
Willy Tarreau | 6be7849 | 2020-06-05 00:00:29 +0200 | [diff] [blame] | 32 | #include <haproxy/cfgparse.h> |
Willy Tarreau | f1d32c4 | 2020-06-04 21:07:02 +0200 | [diff] [blame] | 33 | #include <haproxy/channel.h> |
Willy Tarreau | 83487a8 | 2020-06-04 20:19:54 +0200 | [diff] [blame] | 34 | #include <haproxy/cli.h> |
Willy Tarreau | 7ea393d | 2020-06-04 18:02:10 +0200 | [diff] [blame] | 35 | #include <haproxy/connection.h> |
Willy Tarreau | 5413a87 | 2020-06-02 19:33:08 +0200 | [diff] [blame] | 36 | #include <haproxy/h1.h> |
Willy Tarreau | 8641605 | 2020-06-04 09:20:54 +0200 | [diff] [blame] | 37 | #include <haproxy/hlua.h> |
Willy Tarreau | 8c79400 | 2020-06-04 10:05:25 +0200 | [diff] [blame] | 38 | #include <haproxy/hlua_fcn.h> |
Willy Tarreau | c2b1ff0 | 2020-06-04 21:21:03 +0200 | [diff] [blame] | 39 | #include <haproxy/http_ana.h> |
Willy Tarreau | 126ba3a | 2020-06-04 18:26:43 +0200 | [diff] [blame] | 40 | #include <haproxy/http_fetch.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 41 | #include <haproxy/http_htx.h> |
Willy Tarreau | c761f84 | 2020-06-04 11:40:28 +0200 | [diff] [blame] | 42 | #include <haproxy/http_rules.h> |
Willy Tarreau | 36979d9 | 2020-06-05 17:27:29 +0200 | [diff] [blame] | 43 | #include <haproxy/log.h> |
Willy Tarreau | 2cd5809 | 2020-06-04 15:10:43 +0200 | [diff] [blame] | 44 | #include <haproxy/map.h> |
Willy Tarreau | 8efbdfb | 2020-06-04 11:29:21 +0200 | [diff] [blame] | 45 | #include <haproxy/obj_type.h> |
Willy Tarreau | 225a90a | 2020-06-04 15:06:28 +0200 | [diff] [blame] | 46 | #include <haproxy/pattern.h> |
Willy Tarreau | 469509b | 2020-06-04 15:13:30 +0200 | [diff] [blame] | 47 | #include <haproxy/payload.h> |
Willy Tarreau | 3d6ee40 | 2021-05-08 20:28:07 +0200 | [diff] [blame] | 48 | #include <haproxy/proxy.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 49 | #include <haproxy/regex.h> |
Willy Tarreau | e6ce10b | 2020-06-04 15:33:47 +0200 | [diff] [blame] | 50 | #include <haproxy/sample.h> |
Willy Tarreau | 198e92a | 2021-03-05 10:23:32 +0100 | [diff] [blame] | 51 | #include <haproxy/server.h> |
Willy Tarreau | 48d25b3 | 2020-06-04 18:58:52 +0200 | [diff] [blame] | 52 | #include <haproxy/session.h> |
Willy Tarreau | 2eec9b5 | 2020-06-04 19:58:55 +0200 | [diff] [blame] | 53 | #include <haproxy/stats-t.h> |
Willy Tarreau | dfd3de8 | 2020-06-04 23:46:14 +0200 | [diff] [blame] | 54 | #include <haproxy/stream.h> |
Willy Tarreau | 5e539c9 | 2020-06-04 20:45:39 +0200 | [diff] [blame] | 55 | #include <haproxy/stream_interface.h> |
Willy Tarreau | cea0e1b | 2020-06-04 17:25:40 +0200 | [diff] [blame] | 56 | #include <haproxy/task.h> |
Willy Tarreau | 8b550af | 2020-06-04 17:42:48 +0200 | [diff] [blame] | 57 | #include <haproxy/tcp_rules.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 58 | #include <haproxy/thread.h> |
Willy Tarreau | 48fbcae | 2020-06-03 18:09:46 +0200 | [diff] [blame] | 59 | #include <haproxy/tools.h> |
Willy Tarreau | a171892 | 2020-06-04 16:25:31 +0200 | [diff] [blame] | 60 | #include <haproxy/vars.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 61 | #include <haproxy/xref.h> |
| 62 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 63 | |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 64 | /* Lua uses longjmp to perform yield or throwing errors. This |
| 65 | * macro is used only for identifying the function that can |
| 66 | * not return because a longjmp is executed. |
| 67 | * __LJMP marks a prototype of hlua file that can use longjmp. |
| 68 | * WILL_LJMP() marks an lua function that will use longjmp. |
| 69 | * MAY_LJMP() marks an lua function that may use longjmp. |
| 70 | */ |
| 71 | #define __LJMP |
Willy Tarreau | 4e7cc33 | 2018-10-20 17:45:48 +0200 | [diff] [blame] | 72 | #define WILL_LJMP(func) do { func; my_unreachable(); } while(0) |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 73 | #define MAY_LJMP(func) func |
| 74 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 75 | /* This couple of function executes securely some Lua calls outside of |
| 76 | * the lua runtime environment. Each Lua call can return a longjmp |
| 77 | * if it encounter a memory error. |
| 78 | * |
| 79 | * Lua documentation extract: |
| 80 | * |
| 81 | * If an error happens outside any protected environment, Lua calls |
| 82 | * a panic function (see lua_atpanic) and then calls abort, thus |
| 83 | * exiting the host application. Your panic function can avoid this |
| 84 | * exit by never returning (e.g., doing a long jump to your own |
| 85 | * recovery point outside Lua). |
| 86 | * |
| 87 | * The panic function runs as if it were a message handler (see |
| 88 | * §2.3); in particular, the error message is at the top of the |
| 89 | * stack. However, there is no guarantee about stack space. To push |
| 90 | * anything on the stack, the panic function must first check the |
| 91 | * available space (see §4.2). |
| 92 | * |
| 93 | * We must check all the Lua entry point. This includes: |
| 94 | * - The include/proto/hlua.h exported functions |
| 95 | * - the task wrapper function |
| 96 | * - The action wrapper function |
| 97 | * - The converters wrapper function |
| 98 | * - The sample-fetch wrapper functions |
| 99 | * |
Ilya Shipitsin | 46a030c | 2020-07-05 16:36:08 +0500 | [diff] [blame] | 100 | * It is tolerated that the initialisation function returns an abort. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 101 | * Before each Lua abort, an error message is written on stderr. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 102 | * |
| 103 | * The macro SET_SAFE_LJMP initialise the longjmp. The Macro |
| 104 | * RESET_SAFE_LJMP reset the longjmp. These function must be macro |
| 105 | * because they must be exists in the program stack when the longjmp |
| 106 | * is called. |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 107 | * |
| 108 | * Note that the Lua processing is not really thread safe. It provides |
| 109 | * heavy system which consists to add our own lock function in the Lua |
| 110 | * code and recompile the library. This system will probably not accepted |
| 111 | * by maintainers of various distribs. |
| 112 | * |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 113 | * Our main execution point of the Lua is the function lua_resume(). A |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 114 | * quick looking on the Lua sources displays a lua_lock() a the start |
| 115 | * of function and a lua_unlock() at the end of the function. So I |
| 116 | * conclude that the Lua thread safe mode just perform a mutex around |
| 117 | * all execution. So I prefer to do this in the HAProxy code, it will be |
| 118 | * easier for distro maintainers. |
| 119 | * |
| 120 | * Note that the HAProxy lua functions rounded by the macro SET_SAFE_LJMP |
| 121 | * and RESET_SAFE_LJMP manipulates the Lua stack, so it will be careful |
| 122 | * to set mutex around these functions. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 123 | */ |
Willy Tarreau | 86abe44 | 2018-11-25 20:12:18 +0100 | [diff] [blame] | 124 | __decl_spinlock(hlua_global_lock); |
Thierry FOURNIER | ffbad79 | 2017-07-12 11:39:04 +0200 | [diff] [blame] | 125 | THREAD_LOCAL jmp_buf safe_ljmp_env; |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 126 | static int hlua_panic_safe(lua_State *L) { return 0; } |
Willy Tarreau | 69c66e3 | 2021-07-14 19:41:25 +0200 | [diff] [blame] | 127 | static int hlua_panic_ljmp(lua_State *L) { WILL_LJMP(longjmp(safe_ljmp_env, 1)); return 0; } |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 128 | |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 129 | /* This is the chained list of struct hlua_function referenced |
| 130 | * for haproxy action, sample-fetches, converters, cli and |
| 131 | * applet bindings. It is used for a post-initialisation control. |
| 132 | */ |
| 133 | static struct list referenced_functions = LIST_HEAD_INIT(referenced_functions); |
| 134 | |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 135 | /* This variable is used only during initialization to identify the Lua state |
| 136 | * currently being initialized. 0 is the common lua state, 1 to n are the Lua |
| 137 | * states dedicated to each thread (in this case hlua_state_id==tid+1). |
| 138 | */ |
| 139 | static int hlua_state_id; |
| 140 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 141 | /* This is a NULL-terminated list of lua file which are referenced to load per thread */ |
| 142 | static char **per_thread_load = NULL; |
| 143 | |
| 144 | lua_State *hlua_init_state(int thread_id); |
| 145 | |
Willy Tarreau | 3eb2e47 | 2021-08-20 15:47:25 +0200 | [diff] [blame] | 146 | /* This function takes the Lua global lock. Keep this function's visibility |
| 147 | * global so that it can appear in stack dumps and performance profiles! |
| 148 | */ |
| 149 | void lua_take_global_lock() |
| 150 | { |
| 151 | HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock); |
| 152 | } |
| 153 | |
| 154 | static inline void lua_drop_global_lock() |
| 155 | { |
| 156 | HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); |
| 157 | } |
| 158 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 159 | #define SET_SAFE_LJMP_L(__L, __HLUA) \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 160 | ({ \ |
| 161 | int ret; \ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 162 | if ((__HLUA)->state_id == 0) \ |
Willy Tarreau | 3eb2e47 | 2021-08-20 15:47:25 +0200 | [diff] [blame] | 163 | lua_take_global_lock(); \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 164 | if (setjmp(safe_ljmp_env) != 0) { \ |
| 165 | lua_atpanic(__L, hlua_panic_safe); \ |
| 166 | ret = 0; \ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 167 | if ((__HLUA)->state_id == 0) \ |
Willy Tarreau | 3eb2e47 | 2021-08-20 15:47:25 +0200 | [diff] [blame] | 168 | lua_drop_global_lock(); \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 169 | } else { \ |
| 170 | lua_atpanic(__L, hlua_panic_ljmp); \ |
| 171 | ret = 1; \ |
| 172 | } \ |
| 173 | ret; \ |
| 174 | }) |
| 175 | |
| 176 | /* If we are the last function catching Lua errors, we |
| 177 | * must reset the panic function. |
| 178 | */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 179 | #define RESET_SAFE_LJMP_L(__L, __HLUA) \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 180 | do { \ |
| 181 | lua_atpanic(__L, hlua_panic_safe); \ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 182 | if ((__HLUA)->state_id == 0) \ |
Willy Tarreau | 3eb2e47 | 2021-08-20 15:47:25 +0200 | [diff] [blame] | 183 | lua_drop_global_lock(); \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 184 | } while(0) |
| 185 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 186 | #define SET_SAFE_LJMP(__HLUA) \ |
| 187 | SET_SAFE_LJMP_L((__HLUA)->T, __HLUA) |
| 188 | |
| 189 | #define RESET_SAFE_LJMP(__HLUA) \ |
| 190 | RESET_SAFE_LJMP_L((__HLUA)->T, __HLUA) |
| 191 | |
| 192 | #define SET_SAFE_LJMP_PARENT(__HLUA) \ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 193 | SET_SAFE_LJMP_L(hlua_states[(__HLUA)->state_id], __HLUA) |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 194 | |
| 195 | #define RESET_SAFE_LJMP_PARENT(__HLUA) \ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 196 | RESET_SAFE_LJMP_L(hlua_states[(__HLUA)->state_id], __HLUA) |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 197 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 198 | /* Applet status flags */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 199 | #define APPLET_DONE 0x01 /* applet processing is done. */ |
Christopher Faulet | 18c2e8d | 2019-03-01 12:02:08 +0100 | [diff] [blame] | 200 | /* unused: 0x02 */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 201 | #define APPLET_HDR_SENT 0x04 /* Response header sent. */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 202 | /* unused: 0x08, 0x10 */ |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 203 | #define APPLET_HTTP11 0x20 /* Last chunk sent. */ |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 204 | #define APPLET_RSP_SENT 0x40 /* The response was fully sent */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 205 | |
Thierry Fournier | afc63e2 | 2020-11-28 17:06:51 +0100 | [diff] [blame] | 206 | /* The main Lua execution context. The 0 index is the |
| 207 | * common state shared by all threads. |
| 208 | */ |
Willy Tarreau | 186f376 | 2020-12-04 11:48:12 +0100 | [diff] [blame] | 209 | static lua_State *hlua_states[MAX_THREADS + 1]; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 210 | |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 211 | /* This is the memory pool containing struct lua for applets |
| 212 | * (including cli). |
| 213 | */ |
Willy Tarreau | 8ceae72 | 2018-11-26 11:58:30 +0100 | [diff] [blame] | 214 | DECLARE_STATIC_POOL(pool_head_hlua, "hlua", sizeof(struct hlua)); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 215 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 216 | /* Used for Socket connection. */ |
Amaury Denoyelle | 239fdbf | 2021-03-24 10:22:03 +0100 | [diff] [blame] | 217 | static struct proxy *socket_proxy; |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 218 | static struct server *socket_tcp; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 219 | #ifdef USE_OPENSSL |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 220 | static struct server *socket_ssl; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 221 | #endif |
| 222 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 223 | /* List head of the function called at the initialisation time. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 224 | struct list hlua_init_functions[MAX_THREADS + 1]; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 225 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 226 | /* The following variables contains the reference of the different |
| 227 | * Lua classes. These references are useful for identify metadata |
| 228 | * associated with an object. |
| 229 | */ |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 230 | static int class_txn_ref; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 231 | static int class_socket_ref; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 232 | static int class_channel_ref; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 233 | static int class_fetches_ref; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 234 | static int class_converters_ref; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 235 | static int class_http_ref; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 236 | static int class_map_ref; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 237 | static int class_applet_tcp_ref; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 238 | static int class_applet_http_ref; |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 239 | static int class_txn_reply_ref; |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 240 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 241 | /* Global Lua execution timeout. By default Lua, execution linked |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 242 | * with stream (actions, sample-fetches and converters) have a |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 243 | * short timeout. Lua linked with tasks doesn't have a timeout |
| 244 | * because a task may remain alive during all the haproxy execution. |
| 245 | */ |
| 246 | static unsigned int hlua_timeout_session = 4000; /* session timeout. */ |
| 247 | static unsigned int hlua_timeout_task = TICK_ETERNITY; /* task timeout. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 248 | static unsigned int hlua_timeout_applet = 4000; /* applet timeout. */ |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 249 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 250 | /* Interrupts the Lua processing each "hlua_nb_instruction" instructions. |
| 251 | * it is used for preventing infinite loops. |
| 252 | * |
| 253 | * I test the scheer with an infinite loop containing one incrementation |
| 254 | * and one test. I run this loop between 10 seconds, I raise a ceil of |
| 255 | * 710M loops from one interrupt each 9000 instructions, so I fix the value |
| 256 | * to one interrupt each 10 000 instructions. |
| 257 | * |
| 258 | * configured | Number of |
| 259 | * instructions | loops executed |
| 260 | * between two | in milions |
| 261 | * forced yields | |
| 262 | * ---------------+--------------- |
| 263 | * 10 | 160 |
| 264 | * 500 | 670 |
| 265 | * 1000 | 680 |
| 266 | * 5000 | 700 |
| 267 | * 7000 | 700 |
| 268 | * 8000 | 700 |
| 269 | * 9000 | 710 <- ceil |
| 270 | * 10000 | 710 |
| 271 | * 100000 | 710 |
| 272 | * 1000000 | 710 |
| 273 | * |
| 274 | */ |
| 275 | static unsigned int hlua_nb_instruction = 10000; |
| 276 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 277 | /* Descriptor for the memory allocation state. The limit is pre-initialised to |
| 278 | * 0 until it is replaced by "tune.lua.maxmem" during the config parsing, or it |
| 279 | * is replaced with ~0 during post_init after everything was loaded. This way |
| 280 | * it is guaranteed that if limit is ~0 the boot is complete and that if it's |
| 281 | * zero it's not yet limited and proper accounting is required. |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 282 | */ |
| 283 | struct hlua_mem_allocator { |
| 284 | size_t allocated; |
| 285 | size_t limit; |
| 286 | }; |
| 287 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 288 | static struct hlua_mem_allocator hlua_global_allocator THREAD_ALIGNED(64); |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 289 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 290 | /* These functions converts types between HAProxy internal args or |
| 291 | * sample and LUA types. Another function permits to check if the |
| 292 | * LUA stack contains arguments according with an required ARG_T |
| 293 | * format. |
| 294 | */ |
| 295 | static int hlua_arg2lua(lua_State *L, const struct arg *arg); |
| 296 | static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 297 | __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] | 298 | uint64_t mask, struct proxy *p); |
Willy Tarreau | 5eadada | 2015-03-10 17:28:54 +0100 | [diff] [blame] | 299 | static int hlua_smp2lua(lua_State *L, struct sample *smp); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 300 | static int hlua_smp2lua_str(lua_State *L, struct sample *smp); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 301 | static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp); |
| 302 | |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 303 | __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] | 304 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 305 | struct prepend_path { |
| 306 | struct list l; |
| 307 | char *type; |
| 308 | char *path; |
| 309 | }; |
| 310 | |
| 311 | static struct list prepend_path_list = LIST_HEAD_INIT(prepend_path_list); |
| 312 | |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 313 | #define SEND_ERR(__be, __fmt, __args...) \ |
| 314 | do { \ |
| 315 | send_log(__be, LOG_ERR, __fmt, ## __args); \ |
| 316 | if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) \ |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 317 | ha_alert(__fmt, ## __args); \ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 318 | } while (0) |
| 319 | |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 320 | static inline struct hlua_function *new_hlua_function() |
| 321 | { |
| 322 | struct hlua_function *fcn; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 323 | int i; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 324 | |
| 325 | fcn = calloc(1, sizeof(*fcn)); |
| 326 | if (!fcn) |
| 327 | return NULL; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 328 | LIST_APPEND(&referenced_functions, &fcn->l); |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 329 | for (i = 0; i < MAX_THREADS + 1; i++) |
| 330 | fcn->function_ref[i] = -1; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 331 | return fcn; |
| 332 | } |
| 333 | |
Christopher Faulet | dda4444 | 2021-04-12 14:05:43 +0200 | [diff] [blame] | 334 | static inline void release_hlua_function(struct hlua_function *fcn) |
| 335 | { |
| 336 | if (!fcn) |
| 337 | return; |
| 338 | if (fcn->name) |
| 339 | ha_free(&fcn->name); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 340 | LIST_DELETE(&fcn->l); |
Christopher Faulet | dda4444 | 2021-04-12 14:05:43 +0200 | [diff] [blame] | 341 | ha_free(&fcn); |
| 342 | } |
| 343 | |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 344 | /* If the common state is set, the stack id is 0, otherwise it is the tid + 1 */ |
| 345 | static inline int fcn_ref_to_stack_id(struct hlua_function *fcn) |
| 346 | { |
| 347 | if (fcn->function_ref[0] == -1) |
| 348 | return tid + 1; |
| 349 | return 0; |
| 350 | } |
| 351 | |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 352 | /* Used to check an Lua function type in the stack. It creates and |
| 353 | * returns a reference of the function. This function throws an |
Aurelien DARRAGON | 5ce1152 | 2023-03-02 17:50:49 +0100 | [diff] [blame] | 354 | * error if the argument is not a "function". |
| 355 | * When no longer used, the ref must be released with hlua_unref() |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 356 | */ |
| 357 | __LJMP unsigned int hlua_checkfunction(lua_State *L, int argno) |
| 358 | { |
| 359 | if (!lua_isfunction(L, argno)) { |
Thierry FOURNIER | fd1e955 | 2018-02-23 18:41:18 +0100 | [diff] [blame] | 360 | 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] | 361 | WILL_LJMP(luaL_argerror(L, argno, msg)); |
| 362 | } |
| 363 | lua_pushvalue(L, argno); |
| 364 | return luaL_ref(L, LUA_REGISTRYINDEX); |
| 365 | } |
| 366 | |
Christopher Faulet | ddc9032 | 2020-02-25 10:20:04 +0100 | [diff] [blame] | 367 | /* Used to check an Lua table type in the stack. It creates and |
| 368 | * returns a reference of the table. This function throws an |
Aurelien DARRAGON | 5ce1152 | 2023-03-02 17:50:49 +0100 | [diff] [blame] | 369 | * error if the argument is not a "table". |
| 370 | * When no longer used, the ref must be released with hlua_unref() |
Christopher Faulet | ddc9032 | 2020-02-25 10:20:04 +0100 | [diff] [blame] | 371 | */ |
| 372 | __LJMP unsigned int hlua_checktable(lua_State *L, int argno) |
| 373 | { |
| 374 | if (!lua_istable(L, argno)) { |
| 375 | const char *msg = lua_pushfstring(L, "table expected, got %s", luaL_typename(L, argno)); |
| 376 | WILL_LJMP(luaL_argerror(L, argno, msg)); |
| 377 | } |
| 378 | lua_pushvalue(L, argno); |
| 379 | return luaL_ref(L, LUA_REGISTRYINDEX); |
| 380 | } |
| 381 | |
Aurelien DARRAGON | 5ce1152 | 2023-03-02 17:50:49 +0100 | [diff] [blame] | 382 | /* Get a reference to the object that is at the top of the stack |
| 383 | * The referenced object will be popped from the stack |
| 384 | * |
| 385 | * The function returns the reference to the object which must |
| 386 | * be cleared using hlua_unref() when no longer used |
| 387 | */ |
| 388 | __LJMP int hlua_ref(lua_State *L) |
| 389 | { |
| 390 | return MAY_LJMP(luaL_ref(L, LUA_REGISTRYINDEX)); |
| 391 | } |
| 392 | |
| 393 | /* Pushes a reference previously created using luaL_ref(L, LUA_REGISTRYINDEX) |
| 394 | * on <L> stack |
| 395 | * (ie: hlua_checkfunction(), hlua_checktable() or hlua_ref()) |
| 396 | * |
| 397 | * When the reference is no longer used, it should be released by calling |
| 398 | * hlua_unref() |
| 399 | * |
| 400 | * <L> can be from any co-routine as long as it belongs to the same lua |
| 401 | * parent state that the one used to get the reference. |
| 402 | */ |
| 403 | void hlua_pushref(lua_State *L, int ref) |
| 404 | { |
| 405 | lua_rawgeti(L, LUA_REGISTRYINDEX, ref); |
| 406 | } |
| 407 | |
| 408 | /* Releases a reference previously created using luaL_ref(L, LUA_REGISTRYINDEX) |
| 409 | * (ie: hlua_checkfunction(), hlua_checktable() or hlua_ref()) |
| 410 | * |
| 411 | * This will allow the reference to be reused and the referred object |
| 412 | * to be garbage collected. |
| 413 | * |
| 414 | * <L> can be from any co-routine as long as it belongs to the same lua |
| 415 | * parent state that the one used to get the reference. |
| 416 | */ |
| 417 | void hlua_unref(lua_State *L, int ref) |
| 418 | { |
| 419 | luaL_unref(L, LUA_REGISTRYINDEX, ref); |
| 420 | } |
| 421 | |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 422 | __LJMP const char *hlua_traceback(lua_State *L, const char* sep) |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 423 | { |
| 424 | lua_Debug ar; |
| 425 | int level = 0; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 426 | struct buffer *msg = get_trash_chunk(); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 427 | |
| 428 | while (lua_getstack(L, level++, &ar)) { |
| 429 | |
| 430 | /* Add separator */ |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 431 | if (b_data(msg)) |
| 432 | chunk_appendf(msg, "%s", sep); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 433 | |
| 434 | /* Fill fields: |
| 435 | * 'S': fills in the fields source, short_src, linedefined, lastlinedefined, and what; |
| 436 | * 'l': fills in the field currentline; |
| 437 | * 'n': fills in the field name and namewhat; |
| 438 | * 't': fills in the field istailcall; |
| 439 | */ |
| 440 | lua_getinfo(L, "Slnt", &ar); |
| 441 | |
| 442 | /* Append code localisation */ |
| 443 | if (ar.currentline > 0) |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 444 | chunk_appendf(msg, "%s:%d: ", ar.short_src, ar.currentline); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 445 | else |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 446 | chunk_appendf(msg, "%s: ", ar.short_src); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 447 | |
| 448 | /* |
| 449 | * Get function name |
| 450 | * |
| 451 | * if namewhat is no empty, name is defined. |
| 452 | * what contains "Lua" for Lua function, "C" for C function, |
| 453 | * or "main" for main code. |
| 454 | */ |
| 455 | 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] | 456 | chunk_appendf(msg, "in %s '%s'", ar.namewhat, ar.name); /* use it */ |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 457 | |
| 458 | 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] | 459 | chunk_appendf(msg, "in main chunk"); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 460 | |
| 461 | else if (*ar.what != 'C') /* for Lua functions, use <file:line> */ |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 462 | chunk_appendf(msg, "in function line %d", ar.linedefined); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 463 | |
| 464 | else /* nothing left... */ |
| 465 | chunk_appendf(msg, "?"); |
| 466 | |
| 467 | |
| 468 | /* Display tailed call */ |
| 469 | if (ar.istailcall) |
| 470 | chunk_appendf(msg, " ..."); |
| 471 | } |
| 472 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 473 | return msg->area; |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 474 | } |
| 475 | |
| 476 | |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 477 | /* This function check the number of arguments available in the |
| 478 | * stack. If the number of arguments available is not the same |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 479 | * then <nb> an error is thrown. |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 480 | */ |
| 481 | __LJMP static inline void check_args(lua_State *L, int nb, char *fcn) |
| 482 | { |
| 483 | if (lua_gettop(L) == nb) |
| 484 | return; |
| 485 | WILL_LJMP(luaL_error(L, "'%s' needs %d arguments", fcn, nb)); |
| 486 | } |
| 487 | |
Mark Lakes | 22154b4 | 2018-01-29 14:38:40 -0800 | [diff] [blame] | 488 | /* This function pushes an error string prefixed by the file name |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 489 | * and the line number where the error is encountered. |
| 490 | */ |
| 491 | static int hlua_pusherror(lua_State *L, const char *fmt, ...) |
| 492 | { |
| 493 | va_list argp; |
| 494 | va_start(argp, fmt); |
| 495 | luaL_where(L, 1); |
| 496 | lua_pushvfstring(L, fmt, argp); |
| 497 | va_end(argp); |
| 498 | lua_concat(L, 2); |
| 499 | return 1; |
| 500 | } |
| 501 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 502 | /* This functions is used with sample fetch and converters. It |
| 503 | * converts the HAProxy configuration argument in a lua stack |
| 504 | * values. |
| 505 | * |
| 506 | * It takes an array of "arg", and each entry of the array is |
| 507 | * converted and pushed in the LUA stack. |
| 508 | */ |
| 509 | static int hlua_arg2lua(lua_State *L, const struct arg *arg) |
| 510 | { |
| 511 | switch (arg->type) { |
| 512 | case ARGT_SINT: |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 513 | case ARGT_TIME: |
| 514 | case ARGT_SIZE: |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 515 | lua_pushinteger(L, arg->data.sint); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 516 | break; |
| 517 | |
| 518 | case ARGT_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 519 | lua_pushlstring(L, arg->data.str.area, arg->data.str.data); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 520 | break; |
| 521 | |
| 522 | case ARGT_IPV4: |
| 523 | case ARGT_IPV6: |
| 524 | case ARGT_MSK4: |
| 525 | case ARGT_MSK6: |
| 526 | case ARGT_FE: |
| 527 | case ARGT_BE: |
| 528 | case ARGT_TAB: |
| 529 | case ARGT_SRV: |
| 530 | case ARGT_USR: |
| 531 | case ARGT_MAP: |
| 532 | default: |
| 533 | lua_pushnil(L); |
| 534 | break; |
| 535 | } |
| 536 | return 1; |
| 537 | } |
| 538 | |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 539 | /* 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] | 540 | * 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] | 541 | * with sample fetch wrappers. The input arguments are given to the |
| 542 | * lua wrapper and converted as arg list by the function. |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 543 | */ |
| 544 | static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg) |
| 545 | { |
| 546 | switch (lua_type(L, ud)) { |
| 547 | |
| 548 | case LUA_TNUMBER: |
| 549 | case LUA_TBOOLEAN: |
| 550 | arg->type = ARGT_SINT; |
| 551 | arg->data.sint = lua_tointeger(L, ud); |
| 552 | break; |
| 553 | |
| 554 | case LUA_TSTRING: |
| 555 | arg->type = ARGT_STR; |
Tim Duesterhus | 2e89dec | 2019-09-29 23:03:08 +0200 | [diff] [blame] | 556 | 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] | 557 | /* 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] | 558 | 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] | 559 | arg->data.str.head = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 560 | break; |
| 561 | |
| 562 | case LUA_TUSERDATA: |
| 563 | case LUA_TNIL: |
| 564 | case LUA_TTABLE: |
| 565 | case LUA_TFUNCTION: |
| 566 | case LUA_TTHREAD: |
| 567 | case LUA_TLIGHTUSERDATA: |
| 568 | arg->type = ARGT_SINT; |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 569 | arg->data.sint = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 570 | break; |
| 571 | } |
| 572 | return 1; |
| 573 | } |
| 574 | |
| 575 | /* the following functions are used to convert a struct sample |
| 576 | * in Lua type. This useful to convert the return of the |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 577 | * fetches or converters. |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 578 | */ |
Willy Tarreau | 5eadada | 2015-03-10 17:28:54 +0100 | [diff] [blame] | 579 | static int hlua_smp2lua(lua_State *L, struct sample *smp) |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 580 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 581 | switch (smp->data.type) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 582 | case SMP_T_SINT: |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 583 | case SMP_T_BOOL: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 584 | lua_pushinteger(L, smp->data.u.sint); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 585 | break; |
| 586 | |
| 587 | case SMP_T_BIN: |
| 588 | case SMP_T_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 589 | 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] | 590 | break; |
| 591 | |
| 592 | case SMP_T_METH: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 593 | switch (smp->data.u.meth.meth) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 594 | case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break; |
| 595 | case HTTP_METH_GET: lua_pushstring(L, "GET"); break; |
| 596 | case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break; |
| 597 | case HTTP_METH_POST: lua_pushstring(L, "POST"); break; |
| 598 | case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break; |
| 599 | case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break; |
| 600 | case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break; |
| 601 | case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break; |
| 602 | case HTTP_METH_OTHER: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 603 | 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] | 604 | break; |
| 605 | default: |
| 606 | lua_pushnil(L); |
| 607 | break; |
| 608 | } |
| 609 | break; |
| 610 | |
| 611 | case SMP_T_IPV4: |
| 612 | case SMP_T_IPV6: |
| 613 | 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] | 614 | if (sample_casts[smp->data.type][SMP_T_STR] && |
| 615 | sample_casts[smp->data.type][SMP_T_STR](smp)) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 616 | lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data); |
Willy Tarreau | 5eadada | 2015-03-10 17:28:54 +0100 | [diff] [blame] | 617 | else |
| 618 | lua_pushnil(L); |
| 619 | break; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 620 | default: |
| 621 | lua_pushnil(L); |
| 622 | break; |
| 623 | } |
| 624 | return 1; |
| 625 | } |
| 626 | |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 627 | /* the following functions are used to convert a struct sample |
| 628 | * in Lua strings. This is useful to convert the return of the |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 629 | * fetches or converters. |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 630 | */ |
| 631 | static int hlua_smp2lua_str(lua_State *L, struct sample *smp) |
| 632 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 633 | switch (smp->data.type) { |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 634 | |
| 635 | case SMP_T_BIN: |
| 636 | case SMP_T_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 637 | 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] | 638 | break; |
| 639 | |
| 640 | case SMP_T_METH: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 641 | switch (smp->data.u.meth.meth) { |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 642 | case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break; |
| 643 | case HTTP_METH_GET: lua_pushstring(L, "GET"); break; |
| 644 | case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break; |
| 645 | case HTTP_METH_POST: lua_pushstring(L, "POST"); break; |
| 646 | case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break; |
| 647 | case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break; |
| 648 | case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break; |
| 649 | case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break; |
| 650 | case HTTP_METH_OTHER: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 651 | 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] | 652 | break; |
| 653 | default: |
| 654 | lua_pushstring(L, ""); |
| 655 | break; |
| 656 | } |
| 657 | break; |
| 658 | |
| 659 | case SMP_T_SINT: |
| 660 | case SMP_T_BOOL: |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 661 | case SMP_T_IPV4: |
| 662 | case SMP_T_IPV6: |
| 663 | 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] | 664 | if (sample_casts[smp->data.type][SMP_T_STR] && |
| 665 | sample_casts[smp->data.type][SMP_T_STR](smp)) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 666 | 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] | 667 | else |
| 668 | lua_pushstring(L, ""); |
| 669 | break; |
| 670 | default: |
| 671 | lua_pushstring(L, ""); |
| 672 | break; |
| 673 | } |
| 674 | return 1; |
| 675 | } |
| 676 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 677 | /* the following functions are used to convert an Lua type in a |
| 678 | * struct sample. This is useful to provide data from a converter |
| 679 | * to the LUA code. |
| 680 | */ |
| 681 | static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp) |
| 682 | { |
| 683 | switch (lua_type(L, ud)) { |
| 684 | |
| 685 | case LUA_TNUMBER: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 686 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 687 | smp->data.u.sint = lua_tointeger(L, ud); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 688 | break; |
| 689 | |
| 690 | |
| 691 | case LUA_TBOOLEAN: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 692 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 693 | smp->data.u.sint = lua_toboolean(L, ud); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 694 | break; |
| 695 | |
| 696 | case LUA_TSTRING: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 697 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 698 | smp->flags |= SMP_F_CONST; |
Tim Duesterhus | 2e89dec | 2019-09-29 23:03:08 +0200 | [diff] [blame] | 699 | 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] | 700 | /* 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] | 701 | 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] | 702 | smp->data.u.str.head = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 703 | break; |
| 704 | |
| 705 | case LUA_TUSERDATA: |
| 706 | case LUA_TNIL: |
| 707 | case LUA_TTABLE: |
| 708 | case LUA_TFUNCTION: |
| 709 | case LUA_TTHREAD: |
| 710 | case LUA_TLIGHTUSERDATA: |
Thierry FOURNIER | 93405e1 | 2015-08-26 14:19:03 +0200 | [diff] [blame] | 711 | case LUA_TNONE: |
| 712 | default: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 713 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 714 | smp->data.u.sint = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 715 | break; |
| 716 | } |
| 717 | return 1; |
| 718 | } |
| 719 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 720 | /* This function check the "argp" built by another conversion function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 721 | * 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] | 722 | * 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] | 723 | * |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 724 | * This function assumes that the argp argument contains ARGM_NBARGS + 1 |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 725 | * entries. |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 726 | */ |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 727 | __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] | 728 | uint64_t mask, struct proxy *p) |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 729 | { |
| 730 | int min_arg; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 731 | int i, idx; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 732 | struct proxy *px; |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 733 | struct userlist *ul; |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 734 | struct my_regex *reg; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 735 | const char *msg = NULL; |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 736 | char *sname, *pname, *err = NULL; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 737 | |
| 738 | idx = 0; |
| 739 | min_arg = ARGM(mask); |
| 740 | mask >>= ARGM_BITS; |
| 741 | |
| 742 | while (1) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 743 | struct buffer tmp = BUF_NULL; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 744 | |
| 745 | /* Check oversize. */ |
| 746 | if (idx >= ARGM_NBARGS && argp[idx].type != ARGT_STOP) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 747 | msg = "Malformed argument mask"; |
| 748 | goto error; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 749 | } |
| 750 | |
| 751 | /* Check for mandatory arguments. */ |
| 752 | if (argp[idx].type == ARGT_STOP) { |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 753 | if (idx < min_arg) { |
| 754 | |
| 755 | /* If miss other argument than the first one, we return an error. */ |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 756 | if (idx > 0) { |
| 757 | msg = "Mandatory argument expected"; |
| 758 | goto error; |
| 759 | } |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 760 | |
| 761 | /* If first argument have a certain type, some default values |
| 762 | * may be used. See the function smp_resolve_args(). |
| 763 | */ |
| 764 | switch (mask & ARGT_MASK) { |
| 765 | |
| 766 | case ARGT_FE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 767 | if (!(p->cap & PR_CAP_FE)) { |
| 768 | msg = "Mandatory argument expected"; |
| 769 | goto error; |
| 770 | } |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 771 | argp[idx].data.prx = p; |
| 772 | argp[idx].type = ARGT_FE; |
| 773 | argp[idx+1].type = ARGT_STOP; |
| 774 | break; |
| 775 | |
| 776 | case ARGT_BE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 777 | if (!(p->cap & PR_CAP_BE)) { |
| 778 | msg = "Mandatory argument expected"; |
| 779 | goto error; |
| 780 | } |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 781 | argp[idx].data.prx = p; |
| 782 | argp[idx].type = ARGT_BE; |
| 783 | argp[idx+1].type = ARGT_STOP; |
| 784 | break; |
| 785 | |
| 786 | case ARGT_TAB: |
Olivier Houchard | a265827 | 2022-09-13 00:35:53 +0200 | [diff] [blame] | 787 | if (!p->table) { |
| 788 | msg = "Mandatory argument expected"; |
| 789 | goto error; |
| 790 | } |
| 791 | argp[idx].data.t = p->table; |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 792 | argp[idx].type = ARGT_TAB; |
| 793 | argp[idx+1].type = ARGT_STOP; |
| 794 | break; |
| 795 | |
| 796 | default: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 797 | msg = "Mandatory argument expected"; |
| 798 | goto error; |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 799 | break; |
| 800 | } |
| 801 | } |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 802 | break; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 803 | } |
| 804 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 805 | /* Check for exceed the number of required argument. */ |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 806 | if ((mask & ARGT_MASK) == ARGT_STOP && |
| 807 | argp[idx].type != ARGT_STOP) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 808 | msg = "Last argument expected"; |
| 809 | goto error; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 810 | } |
| 811 | |
| 812 | if ((mask & ARGT_MASK) == ARGT_STOP && |
| 813 | argp[idx].type == ARGT_STOP) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 814 | break; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 815 | } |
| 816 | |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 817 | /* Convert some argument types. All string in argp[] are for not |
| 818 | * duplicated yet. |
| 819 | */ |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 820 | switch (mask & ARGT_MASK) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 821 | case ARGT_SINT: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 822 | if (argp[idx].type != ARGT_SINT) { |
| 823 | msg = "integer expected"; |
| 824 | goto error; |
| 825 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 826 | argp[idx].type = ARGT_SINT; |
| 827 | break; |
| 828 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 829 | case ARGT_TIME: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 830 | if (argp[idx].type != ARGT_SINT) { |
| 831 | msg = "integer expected"; |
| 832 | goto error; |
| 833 | } |
Thierry FOURNIER | 29176f3 | 2015-07-07 00:41:29 +0200 | [diff] [blame] | 834 | argp[idx].type = ARGT_TIME; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 835 | break; |
| 836 | |
| 837 | case ARGT_SIZE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 838 | if (argp[idx].type != ARGT_SINT) { |
| 839 | msg = "integer expected"; |
| 840 | goto error; |
| 841 | } |
Thierry FOURNIER | 29176f3 | 2015-07-07 00:41:29 +0200 | [diff] [blame] | 842 | argp[idx].type = ARGT_SIZE; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 843 | break; |
| 844 | |
| 845 | case ARGT_FE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 846 | if (argp[idx].type != ARGT_STR) { |
| 847 | msg = "string expected"; |
| 848 | goto error; |
| 849 | } |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 850 | 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] | 851 | if (!argp[idx].data.prx) { |
| 852 | msg = "frontend doesn't exist"; |
| 853 | goto error; |
| 854 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 855 | argp[idx].type = ARGT_FE; |
| 856 | break; |
| 857 | |
| 858 | case ARGT_BE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 859 | if (argp[idx].type != ARGT_STR) { |
| 860 | msg = "string expected"; |
| 861 | goto error; |
| 862 | } |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 863 | 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] | 864 | if (!argp[idx].data.prx) { |
| 865 | msg = "backend doesn't exist"; |
| 866 | goto error; |
| 867 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 868 | argp[idx].type = ARGT_BE; |
| 869 | break; |
| 870 | |
| 871 | case ARGT_TAB: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 872 | if (argp[idx].type != ARGT_STR) { |
| 873 | msg = "string expected"; |
| 874 | goto error; |
| 875 | } |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 876 | 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] | 877 | if (!argp[idx].data.t) { |
| 878 | msg = "table doesn't exist"; |
| 879 | goto error; |
| 880 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 881 | argp[idx].type = ARGT_TAB; |
| 882 | break; |
| 883 | |
| 884 | case ARGT_SRV: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 885 | if (argp[idx].type != ARGT_STR) { |
| 886 | msg = "string expected"; |
| 887 | goto error; |
| 888 | } |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 889 | sname = strrchr(argp[idx].data.str.area, '/'); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 890 | if (sname) { |
| 891 | *sname++ = '\0'; |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 892 | pname = argp[idx].data.str.area; |
Willy Tarreau | 9e0bb10 | 2015-05-26 11:24:42 +0200 | [diff] [blame] | 893 | px = proxy_be_by_name(pname); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 894 | if (!px) { |
| 895 | msg = "backend doesn't exist"; |
| 896 | goto error; |
| 897 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 898 | } |
| 899 | else { |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 900 | sname = argp[idx].data.str.area; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 901 | px = p; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 902 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 903 | argp[idx].data.srv = findserver(px, sname); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 904 | if (!argp[idx].data.srv) { |
| 905 | msg = "server doesn't exist"; |
| 906 | goto error; |
| 907 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 908 | argp[idx].type = ARGT_SRV; |
| 909 | break; |
| 910 | |
| 911 | case ARGT_IPV4: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 912 | if (argp[idx].type != ARGT_STR) { |
| 913 | msg = "string expected"; |
| 914 | goto error; |
| 915 | } |
| 916 | if (inet_pton(AF_INET, argp[idx].data.str.area, &argp[idx].data.ipv4)) { |
| 917 | msg = "invalid IPv4 address"; |
| 918 | goto error; |
| 919 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 920 | argp[idx].type = ARGT_IPV4; |
| 921 | break; |
| 922 | |
| 923 | case ARGT_MSK4: |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 924 | if (argp[idx].type == ARGT_SINT) |
| 925 | len2mask4(argp[idx].data.sint, &argp[idx].data.ipv4); |
| 926 | else if (argp[idx].type == ARGT_STR) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 927 | if (!str2mask(argp[idx].data.str.area, &argp[idx].data.ipv4)) { |
| 928 | msg = "invalid IPv4 mask"; |
| 929 | goto error; |
| 930 | } |
| 931 | } |
| 932 | else { |
| 933 | msg = "integer or string expected"; |
| 934 | goto error; |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 935 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 936 | argp[idx].type = ARGT_MSK4; |
| 937 | break; |
| 938 | |
| 939 | case ARGT_IPV6: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 940 | if (argp[idx].type != ARGT_STR) { |
| 941 | msg = "string expected"; |
| 942 | goto error; |
| 943 | } |
| 944 | if (inet_pton(AF_INET6, argp[idx].data.str.area, &argp[idx].data.ipv6)) { |
| 945 | msg = "invalid IPv6 address"; |
| 946 | goto error; |
| 947 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 948 | argp[idx].type = ARGT_IPV6; |
| 949 | break; |
| 950 | |
| 951 | case ARGT_MSK6: |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 952 | if (argp[idx].type == ARGT_SINT) |
| 953 | len2mask6(argp[idx].data.sint, &argp[idx].data.ipv6); |
| 954 | else if (argp[idx].type == ARGT_STR) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 955 | if (!str2mask6(argp[idx].data.str.area, &argp[idx].data.ipv6)) { |
| 956 | msg = "invalid IPv6 mask"; |
| 957 | goto error; |
| 958 | } |
| 959 | } |
| 960 | else { |
| 961 | msg = "integer or string expected"; |
| 962 | goto error; |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 963 | } |
Tim Duesterhus | b814da6 | 2018-01-25 16:24:50 +0100 | [diff] [blame] | 964 | argp[idx].type = ARGT_MSK6; |
| 965 | break; |
| 966 | |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 967 | case ARGT_REG: |
| 968 | if (argp[idx].type != ARGT_STR) { |
| 969 | msg = "string expected"; |
| 970 | goto error; |
| 971 | } |
| 972 | reg = regex_comp(argp[idx].data.str.area, !(argp[idx].type_flags & ARGF_REG_ICASE), 1, &err); |
| 973 | if (!reg) { |
| 974 | msg = lua_pushfstring(L, "error compiling regex '%s' : '%s'", |
| 975 | argp[idx].data.str.area, err); |
| 976 | free(err); |
| 977 | goto error; |
| 978 | } |
| 979 | argp[idx].type = ARGT_REG; |
| 980 | argp[idx].data.reg = reg; |
| 981 | break; |
| 982 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 983 | case ARGT_USR: |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 984 | if (argp[idx].type != ARGT_STR) { |
| 985 | msg = "string expected"; |
| 986 | goto error; |
| 987 | } |
| 988 | if (p->uri_auth && p->uri_auth->userlist && |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 989 | strcmp(p->uri_auth->userlist->name, argp[idx].data.str.area) == 0) |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 990 | ul = p->uri_auth->userlist; |
| 991 | else |
| 992 | ul = auth_find_userlist(argp[idx].data.str.area); |
| 993 | |
| 994 | if (!ul) { |
| 995 | msg = lua_pushfstring(L, "unable to find userlist '%s'", argp[idx].data.str.area); |
| 996 | goto error; |
| 997 | } |
| 998 | argp[idx].type = ARGT_USR; |
| 999 | argp[idx].data.usr = ul; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1000 | break; |
| 1001 | |
| 1002 | case ARGT_STR: |
| 1003 | if (!chunk_dup(&tmp, &argp[idx].data.str)) { |
| 1004 | msg = "unable to duplicate string arg"; |
| 1005 | goto error; |
| 1006 | } |
| 1007 | argp[idx].data.str = tmp; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 1008 | break; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1009 | |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 1010 | case ARGT_MAP: |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 1011 | msg = "type not yet supported"; |
| 1012 | goto error; |
| 1013 | break; |
| 1014 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 1015 | } |
| 1016 | |
| 1017 | /* Check for type of argument. */ |
| 1018 | if ((mask & ARGT_MASK) != argp[idx].type) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1019 | msg = lua_pushfstring(L, "'%s' expected, got '%s'", |
| 1020 | arg_type_names[(mask & ARGT_MASK)], |
| 1021 | arg_type_names[argp[idx].type & ARGT_MASK]); |
| 1022 | goto error; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 1023 | } |
| 1024 | |
| 1025 | /* Next argument. */ |
| 1026 | mask >>= ARGT_BITS; |
| 1027 | idx++; |
| 1028 | } |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1029 | return 0; |
| 1030 | |
| 1031 | error: |
Olivier Houchard | da25dac | 2022-09-13 00:31:17 +0200 | [diff] [blame] | 1032 | argp[idx].type = ARGT_STOP; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1033 | for (i = 0; i < idx; i++) { |
| 1034 | if (argp[i].type == ARGT_STR) |
| 1035 | chunk_destroy(&argp[i].data.str); |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 1036 | else if (argp[i].type == ARGT_REG) |
| 1037 | regex_free(argp[i].data.reg); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1038 | } |
| 1039 | WILL_LJMP(luaL_argerror(L, first + idx, msg)); |
| 1040 | return 0; /* Never reached */ |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 1041 | } |
| 1042 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1043 | /* |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 1044 | * The following functions are used to make correspondence between the the |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1045 | * executed lua pointer and the "struct hlua *" that contain the context. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1046 | * |
| 1047 | * - hlua_gethlua : return the hlua context associated with an lua_State. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1048 | * - hlua_sethlua : create the association between hlua context and lua_state. |
| 1049 | */ |
| 1050 | static inline struct hlua *hlua_gethlua(lua_State *L) |
| 1051 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 1052 | struct hlua **hlua = lua_getextraspace(L); |
| 1053 | return *hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1054 | } |
| 1055 | static inline void hlua_sethlua(struct hlua *hlua) |
| 1056 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 1057 | struct hlua **hlua_store = lua_getextraspace(hlua->T); |
| 1058 | *hlua_store = hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1059 | } |
| 1060 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1061 | /* This function is used to send logs. It try to send on screen (stderr) |
| 1062 | * and on the default syslog server. |
| 1063 | */ |
| 1064 | static inline void hlua_sendlog(struct proxy *px, int level, const char *msg) |
| 1065 | { |
| 1066 | struct tm tm; |
| 1067 | char *p; |
| 1068 | |
| 1069 | /* Cleanup the log message. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1070 | p = trash.area; |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1071 | for (; *msg != '\0'; msg++, p++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1072 | if (p >= trash.area + trash.size - 1) { |
Thierry FOURNIER | ccf0063 | 2015-09-16 12:47:03 +0200 | [diff] [blame] | 1073 | /* Break the message if exceed the buffer size. */ |
| 1074 | *(p-4) = ' '; |
| 1075 | *(p-3) = '.'; |
| 1076 | *(p-2) = '.'; |
| 1077 | *(p-1) = '.'; |
| 1078 | break; |
| 1079 | } |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 1080 | if (isprint((unsigned char)*msg)) |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1081 | *p = *msg; |
| 1082 | else |
| 1083 | *p = '.'; |
| 1084 | } |
| 1085 | *p = '\0'; |
| 1086 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1087 | send_log(px, level, "%s\n", trash.area); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1088 | if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) { |
Christopher Faulet | f98d821 | 2020-10-02 18:13:52 +0200 | [diff] [blame] | 1089 | if (level == LOG_DEBUG && !(global.mode & MODE_DEBUG)) |
| 1090 | return; |
| 1091 | |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 1092 | get_localtime(date.tv_sec, &tm); |
| 1093 | fprintf(stderr, "[%s] %03d/%02d%02d%02d (%d) : %s\n", |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1094 | 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] | 1095 | (int)getpid(), trash.area); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1096 | fflush(stderr); |
| 1097 | } |
| 1098 | } |
| 1099 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1100 | /* This function just ensure that the yield will be always |
| 1101 | * returned with a timeout and permit to set some flags |
| 1102 | */ |
| 1103 | __LJMP void hlua_yieldk(lua_State *L, int nresults, int ctx, |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1104 | lua_KFunction k, int timeout, unsigned int flags) |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1105 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 1106 | struct hlua *hlua; |
| 1107 | |
| 1108 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 1109 | hlua = hlua_gethlua(L); |
| 1110 | if (!hlua) { |
| 1111 | return; |
| 1112 | } |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1113 | |
| 1114 | /* Set the wake timeout. If timeout is required, we set |
| 1115 | * the expiration time. |
| 1116 | */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1117 | hlua->wake_time = timeout; |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1118 | |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 1119 | hlua->flags |= flags; |
| 1120 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1121 | /* Process the yield. */ |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 1122 | MAY_LJMP(lua_yieldk(L, nresults, ctx, k)); |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1123 | } |
| 1124 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1125 | /* This function initialises the Lua environment stored in the stream. |
| 1126 | * 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] | 1127 | * 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] | 1128 | * |
| 1129 | * This function is particular. it initialises a new Lua thread. If the |
| 1130 | * initialisation fails (example: out of memory error), the lua function |
| 1131 | * throws an error (longjmp). |
| 1132 | * |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1133 | * In some case (at least one), this function can be called from safe |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1134 | * environment, so we must not initialise it. While the support of |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1135 | * threads appear, the safe environment set a lock to ensure only one |
| 1136 | * Lua execution at a time. If we initialize safe environment in another |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1137 | * safe environment, we have a dead lock. |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1138 | * |
| 1139 | * set "already_safe" true if the context is initialized form safe |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1140 | * Lua function. |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1141 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1142 | * This function manipulates two Lua stacks: the main and the thread. Only |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 1143 | * the main stack can fail. The thread is not manipulated. This function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1144 | * MUST NOT manipulate the created thread stack state, because it is not |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1145 | * protected against errors thrown by the thread stack. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1146 | */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1147 | int hlua_ctx_init(struct hlua *lua, int state_id, struct task *task, int already_safe) |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1148 | { |
| 1149 | lua->Mref = LUA_REFNIL; |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1150 | lua->flags = 0; |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 1151 | lua->gc_count = 0; |
Christopher Faulet | bc275a9 | 2020-02-26 14:55:16 +0100 | [diff] [blame] | 1152 | lua->wake_time = TICK_ETERNITY; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1153 | lua->state_id = state_id; |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 1154 | LIST_INIT(&lua->com); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1155 | if (!already_safe) { |
| 1156 | if (!SET_SAFE_LJMP_PARENT(lua)) { |
| 1157 | lua->Tref = LUA_REFNIL; |
| 1158 | return 0; |
| 1159 | } |
| 1160 | } |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1161 | lua->T = lua_newthread(hlua_states[state_id]); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1162 | if (!lua->T) { |
| 1163 | lua->Tref = LUA_REFNIL; |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1164 | if (!already_safe) |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1165 | RESET_SAFE_LJMP_PARENT(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1166 | return 0; |
| 1167 | } |
| 1168 | hlua_sethlua(lua); |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1169 | lua->Tref = luaL_ref(hlua_states[state_id], LUA_REGISTRYINDEX); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1170 | lua->task = task; |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1171 | if (!already_safe) |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1172 | RESET_SAFE_LJMP_PARENT(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1173 | return 1; |
| 1174 | } |
| 1175 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1176 | /* Used to destroy the Lua coroutine when the attached stream or task |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1177 | * is destroyed. The destroy also the memory context. The struct "lua" |
Aurelien DARRAGON | d5c8a10 | 2023-03-01 16:45:50 +0100 | [diff] [blame] | 1178 | * will be freed. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1179 | */ |
| 1180 | void hlua_ctx_destroy(struct hlua *lua) |
| 1181 | { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 1182 | if (!lua) |
Thierry FOURNIER | a718b29 | 2015-03-04 16:48:34 +0100 | [diff] [blame] | 1183 | return; |
| 1184 | |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 1185 | if (!lua->T) |
| 1186 | goto end; |
| 1187 | |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 1188 | /* Purge all the pending signals. */ |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1189 | notification_purge(&lua->com); |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 1190 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1191 | if (!SET_SAFE_LJMP(lua)) |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 1192 | return; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1193 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1194 | RESET_SAFE_LJMP(lua); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 1195 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1196 | if (!SET_SAFE_LJMP_PARENT(lua)) |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 1197 | return; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1198 | luaL_unref(hlua_states[lua->state_id], LUA_REGISTRYINDEX, lua->Tref); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1199 | RESET_SAFE_LJMP_PARENT(lua); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 1200 | /* Forces a garbage collecting process. If the Lua program is finished |
| 1201 | * without error, we run the GC on the thread pointer. Its freed all |
| 1202 | * the unused memory. |
| 1203 | * If the thread is finnish with an error or is currently yielded, |
| 1204 | * it seems that the GC applied on the thread doesn't clean anything, |
| 1205 | * so e run the GC on the main thread. |
| 1206 | * NOTE: maybe this action locks all the Lua threads untiml the en of |
| 1207 | * the garbage collection. |
| 1208 | */ |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 1209 | if (lua->gc_count) { |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1210 | if (!SET_SAFE_LJMP_PARENT(lua)) |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 1211 | return; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1212 | lua_gc(hlua_states[lua->state_id], LUA_GCCOLLECT, 0); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1213 | RESET_SAFE_LJMP_PARENT(lua); |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 1214 | } |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 1215 | |
Thierry FOURNIER | a7b536b | 2015-09-21 22:50:24 +0200 | [diff] [blame] | 1216 | lua->T = NULL; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 1217 | |
| 1218 | end: |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 1219 | pool_free(pool_head_hlua, lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1220 | } |
| 1221 | |
| 1222 | /* This function is used to restore the Lua context when a coroutine |
| 1223 | * fails. This function copy the common memory between old coroutine |
| 1224 | * and the new coroutine. The old coroutine is destroyed, and its |
| 1225 | * replaced by the new coroutine. |
| 1226 | * If the flag "keep_msg" is set, the last entry of the old is assumed |
| 1227 | * as string error message and it is copied in the new stack. |
| 1228 | */ |
| 1229 | static int hlua_ctx_renew(struct hlua *lua, int keep_msg) |
| 1230 | { |
| 1231 | lua_State *T; |
| 1232 | int new_ref; |
| 1233 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1234 | /* New Lua coroutine. */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1235 | T = lua_newthread(hlua_states[lua->state_id]); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1236 | if (!T) |
| 1237 | return 0; |
| 1238 | |
| 1239 | /* Copy last error message. */ |
| 1240 | if (keep_msg) |
| 1241 | lua_xmove(lua->T, T, 1); |
| 1242 | |
| 1243 | /* Copy data between the coroutines. */ |
| 1244 | lua_rawgeti(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 1245 | lua_xmove(lua->T, T, 1); |
Ilya Shipitsin | 46a030c | 2020-07-05 16:36:08 +0500 | [diff] [blame] | 1246 | new_ref = luaL_ref(T, LUA_REGISTRYINDEX); /* Value popped. */ |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1247 | |
| 1248 | /* Destroy old data. */ |
| 1249 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 1250 | |
| 1251 | /* The thread is garbage collected by Lua. */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1252 | luaL_unref(hlua_states[lua->state_id], LUA_REGISTRYINDEX, lua->Tref); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1253 | |
| 1254 | /* Fill the struct with the new coroutine values. */ |
| 1255 | lua->Mref = new_ref; |
| 1256 | lua->T = T; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1257 | lua->Tref = luaL_ref(hlua_states[lua->state_id], LUA_REGISTRYINDEX); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1258 | |
| 1259 | /* Set context. */ |
| 1260 | hlua_sethlua(lua); |
| 1261 | |
| 1262 | return 1; |
| 1263 | } |
| 1264 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1265 | void hlua_hook(lua_State *L, lua_Debug *ar) |
| 1266 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 1267 | struct hlua *hlua; |
| 1268 | |
| 1269 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 1270 | hlua = hlua_gethlua(L); |
| 1271 | if (!hlua) |
| 1272 | return; |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1273 | |
| 1274 | /* Lua cannot yield when its returning from a function, |
| 1275 | * so, we can fix the interrupt hook to 1 instruction, |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1276 | * expecting that the function is finished. |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1277 | */ |
| 1278 | if (lua_gethookmask(L) & LUA_MASKRET) { |
| 1279 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, 1); |
| 1280 | return; |
| 1281 | } |
| 1282 | |
| 1283 | /* restore the interrupt condition. */ |
| 1284 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 1285 | |
| 1286 | /* If we interrupt the Lua processing in yieldable state, we yield. |
| 1287 | * If the state is not yieldable, trying yield causes an error. |
| 1288 | */ |
| 1289 | if (lua_isyieldable(L)) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 1290 | MAY_LJMP(hlua_yieldk(L, 0, 0, NULL, TICK_ETERNITY, HLUA_CTRLYIELD)); |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1291 | |
Thierry FOURNIER | a85cfb1 | 2015-03-13 14:50:06 +0100 | [diff] [blame] | 1292 | /* If we cannot yield, update the clock and check the timeout. */ |
| 1293 | tv_update_date(0, 1); |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1294 | hlua->run_time += now_ms - hlua->start_time; |
| 1295 | if (hlua->max_time && hlua->run_time >= hlua->max_time) { |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1296 | lua_pushfstring(L, "execution timeout"); |
| 1297 | WILL_LJMP(lua_error(L)); |
| 1298 | } |
| 1299 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1300 | /* Update the start time. */ |
| 1301 | hlua->start_time = now_ms; |
| 1302 | |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1303 | /* Try to interrupt the process at the end of the current |
| 1304 | * unyieldable function. |
| 1305 | */ |
| 1306 | 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] | 1307 | } |
| 1308 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1309 | /* This function start or resumes the Lua stack execution. If the flag |
| 1310 | * "yield_allowed" if no set and the LUA stack execution returns a yield |
| 1311 | * The function return an error. |
| 1312 | * |
| 1313 | * The function can returns 4 values: |
| 1314 | * - HLUA_E_OK : The execution is terminated without any errors. |
| 1315 | * - HLUA_E_AGAIN : The execution must continue at the next associated |
| 1316 | * task wakeup. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1317 | * - 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] | 1318 | * the top of the stack. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1319 | * - HLUA_E_ERR : An error has occurred without error message. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1320 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1321 | * 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] | 1322 | * LUA code. |
| 1323 | */ |
| 1324 | static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed) |
| 1325 | { |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1326 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
| 1327 | int nres; |
| 1328 | #endif |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1329 | int ret; |
| 1330 | const char *msg; |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1331 | const char *trace; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1332 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1333 | /* Initialise run time counter. */ |
| 1334 | if (!HLUA_IS_RUNNING(lua)) |
| 1335 | lua->run_time = 0; |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1336 | |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1337 | /* Lock the whole Lua execution. This lock must be before the |
| 1338 | * label "resume_execution". |
| 1339 | */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1340 | if (lua->state_id == 0) |
Willy Tarreau | 3eb2e47 | 2021-08-20 15:47:25 +0200 | [diff] [blame] | 1341 | lua_take_global_lock(); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1342 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1343 | resume_execution: |
| 1344 | |
| 1345 | /* This hook interrupts the Lua processing each 'hlua_nb_instruction' |
| 1346 | * instructions. it is used for preventing infinite loops. |
| 1347 | */ |
| 1348 | lua_sethook(lua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 1349 | |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1350 | /* Remove all flags except the running flags. */ |
Thierry FOURNIER | 2f3867f | 2015-09-28 01:02:01 +0200 | [diff] [blame] | 1351 | HLUA_SET_RUN(lua); |
| 1352 | HLUA_CLR_CTRLYIELD(lua); |
| 1353 | HLUA_CLR_WAKERESWR(lua); |
| 1354 | HLUA_CLR_WAKEREQWR(lua); |
Christopher Faulet | a6e792f | 2021-08-04 17:58:21 +0200 | [diff] [blame] | 1355 | HLUA_CLR_NOYIELD(lua); |
| 1356 | if (!yield_allowed) |
| 1357 | HLUA_SET_NOYIELD(lua); |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1358 | |
Christopher Faulet | bc275a9 | 2020-02-26 14:55:16 +0100 | [diff] [blame] | 1359 | /* Update the start time and reset wake_time. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1360 | lua->start_time = now_ms; |
Christopher Faulet | bc275a9 | 2020-02-26 14:55:16 +0100 | [diff] [blame] | 1361 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1362 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1363 | /* Call the function. */ |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1364 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1365 | 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] | 1366 | #else |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1367 | ret = lua_resume(lua->T, hlua_states[lua->state_id], lua->nargs); |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1368 | #endif |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1369 | switch (ret) { |
| 1370 | |
| 1371 | case LUA_OK: |
| 1372 | ret = HLUA_E_OK; |
| 1373 | break; |
| 1374 | |
| 1375 | case LUA_YIELD: |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1376 | /* Check if the execution timeout is expired. It it is the case, we |
| 1377 | * break the Lua execution. |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1378 | */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1379 | tv_update_date(0, 1); |
| 1380 | lua->run_time += now_ms - lua->start_time; |
| 1381 | if (lua->max_time && lua->run_time > lua->max_time) { |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1382 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1383 | ret = HLUA_E_ETMOUT; |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1384 | break; |
| 1385 | } |
| 1386 | /* Process the forced yield. if the general yield is not allowed or |
| 1387 | * if no task were associated this the current Lua execution |
| 1388 | * coroutine, we resume the execution. Else we want to return in the |
| 1389 | * scheduler and we want to be waked up again, to continue the |
| 1390 | * current Lua execution. So we schedule our own task. |
| 1391 | */ |
| 1392 | if (HLUA_IS_CTRLYIELDING(lua)) { |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1393 | if (!yield_allowed || !lua->task) |
| 1394 | goto resume_execution; |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1395 | task_wakeup(lua->task, TASK_WOKEN_MSG); |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1396 | } |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1397 | if (!yield_allowed) { |
| 1398 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1399 | ret = HLUA_E_YIELD; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1400 | break; |
| 1401 | } |
| 1402 | ret = HLUA_E_AGAIN; |
| 1403 | break; |
| 1404 | |
| 1405 | case LUA_ERRRUN: |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1406 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1407 | /* Special exit case. The traditional exit is returned as an error |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1408 | * because the errors ares the only one mean to return immediately |
| 1409 | * from and lua execution. |
| 1410 | */ |
| 1411 | if (lua->flags & HLUA_EXIT) { |
| 1412 | ret = HLUA_E_OK; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 1413 | hlua_ctx_renew(lua, 1); |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1414 | break; |
| 1415 | } |
| 1416 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1417 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1418 | if (!lua_checkstack(lua->T, 1)) { |
| 1419 | ret = HLUA_E_ERR; |
| 1420 | break; |
| 1421 | } |
| 1422 | msg = lua_tostring(lua->T, -1); |
| 1423 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1424 | lua_pop(lua->T, 1); |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 1425 | trace = hlua_traceback(lua->T, ", "); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1426 | if (msg) |
Thierry Fournier | 46278ff | 2020-11-29 11:48:12 +0100 | [diff] [blame] | 1427 | 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] | 1428 | else |
Thierry Fournier | 46278ff | 2020-11-29 11:48:12 +0100 | [diff] [blame] | 1429 | 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] | 1430 | ret = HLUA_E_ERRMSG; |
| 1431 | break; |
| 1432 | |
| 1433 | case LUA_ERRMEM: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1434 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1435 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1436 | ret = HLUA_E_NOMEM; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1437 | break; |
| 1438 | |
| 1439 | case LUA_ERRERR: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1440 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1441 | if (!lua_checkstack(lua->T, 1)) { |
| 1442 | ret = HLUA_E_ERR; |
| 1443 | break; |
| 1444 | } |
| 1445 | msg = lua_tostring(lua->T, -1); |
| 1446 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1447 | lua_pop(lua->T, 1); |
| 1448 | if (msg) |
Thierry Fournier | 46278ff | 2020-11-29 11:48:12 +0100 | [diff] [blame] | 1449 | 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] | 1450 | else |
Thierry Fournier | 46278ff | 2020-11-29 11:48:12 +0100 | [diff] [blame] | 1451 | 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] | 1452 | ret = HLUA_E_ERRMSG; |
| 1453 | break; |
| 1454 | |
| 1455 | default: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1456 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1457 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1458 | ret = HLUA_E_ERR; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1459 | break; |
| 1460 | } |
| 1461 | |
| 1462 | switch (ret) { |
| 1463 | case HLUA_E_AGAIN: |
| 1464 | break; |
| 1465 | |
| 1466 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1467 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1468 | hlua_ctx_renew(lua, 1); |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1469 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1470 | break; |
| 1471 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1472 | case HLUA_E_ETMOUT: |
| 1473 | case HLUA_E_NOMEM: |
| 1474 | case HLUA_E_YIELD: |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1475 | case HLUA_E_ERR: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1476 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1477 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1478 | hlua_ctx_renew(lua, 0); |
| 1479 | break; |
| 1480 | |
| 1481 | case HLUA_E_OK: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1482 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1483 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1484 | break; |
| 1485 | } |
| 1486 | |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1487 | /* This is the main exit point, remove the Lua lock. */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1488 | if (lua->state_id == 0) |
Willy Tarreau | 3eb2e47 | 2021-08-20 15:47:25 +0200 | [diff] [blame] | 1489 | lua_drop_global_lock(); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1490 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1491 | return ret; |
| 1492 | } |
| 1493 | |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1494 | /* This function exit the current code. */ |
| 1495 | __LJMP static int hlua_done(lua_State *L) |
| 1496 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 1497 | struct hlua *hlua; |
| 1498 | |
| 1499 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 1500 | hlua = hlua_gethlua(L); |
| 1501 | if (!hlua) |
| 1502 | return 0; |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1503 | |
| 1504 | hlua->flags |= HLUA_EXIT; |
| 1505 | WILL_LJMP(lua_error(L)); |
| 1506 | |
| 1507 | return 0; |
| 1508 | } |
| 1509 | |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1510 | /* This function is an LUA binding. It provides a function |
| 1511 | * for deleting ACL from a referenced ACL file. |
| 1512 | */ |
| 1513 | __LJMP static int hlua_del_acl(lua_State *L) |
| 1514 | { |
| 1515 | const char *name; |
| 1516 | const char *key; |
| 1517 | struct pat_ref *ref; |
| 1518 | |
| 1519 | MAY_LJMP(check_args(L, 2, "del_acl")); |
| 1520 | |
| 1521 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1522 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1523 | |
| 1524 | ref = pat_ref_lookup(name); |
| 1525 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1526 | WILL_LJMP(luaL_error(L, "'del_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1527 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1528 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1529 | pat_ref_delete(ref, key); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1530 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1531 | return 0; |
| 1532 | } |
| 1533 | |
| 1534 | /* This function is an LUA binding. It provides a function |
| 1535 | * for deleting map entry from a referenced map file. |
| 1536 | */ |
| 1537 | static int hlua_del_map(lua_State *L) |
| 1538 | { |
| 1539 | const char *name; |
| 1540 | const char *key; |
| 1541 | struct pat_ref *ref; |
| 1542 | |
| 1543 | MAY_LJMP(check_args(L, 2, "del_map")); |
| 1544 | |
| 1545 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1546 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1547 | |
| 1548 | ref = pat_ref_lookup(name); |
| 1549 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1550 | WILL_LJMP(luaL_error(L, "'del_map': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1551 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1552 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1553 | pat_ref_delete(ref, key); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1554 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1555 | return 0; |
| 1556 | } |
| 1557 | |
| 1558 | /* This function is an LUA binding. It provides a function |
| 1559 | * for adding ACL pattern from a referenced ACL file. |
| 1560 | */ |
| 1561 | static int hlua_add_acl(lua_State *L) |
| 1562 | { |
| 1563 | const char *name; |
| 1564 | const char *key; |
| 1565 | struct pat_ref *ref; |
| 1566 | |
| 1567 | MAY_LJMP(check_args(L, 2, "add_acl")); |
| 1568 | |
| 1569 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1570 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1571 | |
| 1572 | ref = pat_ref_lookup(name); |
| 1573 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1574 | WILL_LJMP(luaL_error(L, "'add_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1575 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1576 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1577 | if (pat_ref_find_elt(ref, key) == NULL) |
| 1578 | pat_ref_add(ref, key, NULL, NULL); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1579 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1580 | return 0; |
| 1581 | } |
| 1582 | |
| 1583 | /* This function is an LUA binding. It provides a function |
| 1584 | * for setting map pattern and sample from a referenced map |
| 1585 | * file. |
| 1586 | */ |
| 1587 | static int hlua_set_map(lua_State *L) |
| 1588 | { |
| 1589 | const char *name; |
| 1590 | const char *key; |
| 1591 | const char *value; |
| 1592 | struct pat_ref *ref; |
| 1593 | |
| 1594 | MAY_LJMP(check_args(L, 3, "set_map")); |
| 1595 | |
| 1596 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1597 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1598 | value = MAY_LJMP(luaL_checkstring(L, 3)); |
| 1599 | |
| 1600 | ref = pat_ref_lookup(name); |
| 1601 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1602 | WILL_LJMP(luaL_error(L, "'set_map': unknown map file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1603 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1604 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1605 | if (pat_ref_find_elt(ref, key) != NULL) |
| 1606 | pat_ref_set(ref, key, value, NULL); |
| 1607 | else |
| 1608 | pat_ref_add(ref, key, value, NULL); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1609 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1610 | return 0; |
| 1611 | } |
| 1612 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1613 | /* A class is a lot of memory that contain data. This data can be a table, |
| 1614 | * 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] | 1615 | * metatable have an original version registered in the global context with |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1616 | * the name of the object (_G[<name>] = <metable> ). |
| 1617 | * |
| 1618 | * A metable is a table that modify the standard behavior of a standard |
| 1619 | * access to the associated data. The entries of this new metatable are |
| 1620 | * defined as is: |
| 1621 | * |
| 1622 | * http://lua-users.org/wiki/MetatableEvents |
| 1623 | * |
| 1624 | * __index |
| 1625 | * |
| 1626 | * we access an absent field in a table, the result is nil. This is |
| 1627 | * true, but it is not the whole truth. Actually, such access triggers |
| 1628 | * the interpreter to look for an __index metamethod: If there is no |
| 1629 | * such method, as usually happens, then the access results in nil; |
| 1630 | * otherwise, the metamethod will provide the result. |
| 1631 | * |
| 1632 | * Control 'prototype' inheritance. When accessing "myTable[key]" and |
| 1633 | * the key does not appear in the table, but the metatable has an __index |
| 1634 | * property: |
| 1635 | * |
| 1636 | * - if the value is a function, the function is called, passing in the |
| 1637 | * table and the key; the return value of that function is returned as |
| 1638 | * the result. |
| 1639 | * |
| 1640 | * - if the value is another table, the value of the key in that table is |
| 1641 | * asked for and returned (and if it doesn't exist in that table, but that |
| 1642 | * table's metatable has an __index property, then it continues on up) |
| 1643 | * |
| 1644 | * - Use "rawget(myTable,key)" to skip this metamethod. |
| 1645 | * |
| 1646 | * http://www.lua.org/pil/13.4.1.html |
| 1647 | * |
| 1648 | * __newindex |
| 1649 | * |
| 1650 | * Like __index, but control property assignment. |
| 1651 | * |
| 1652 | * __mode - Control weak references. A string value with one or both |
| 1653 | * of the characters 'k' and 'v' which specifies that the the |
| 1654 | * keys and/or values in the table are weak references. |
| 1655 | * |
| 1656 | * __call - Treat a table like a function. When a table is followed by |
| 1657 | * parenthesis such as "myTable( 'foo' )" and the metatable has |
| 1658 | * a __call key pointing to a function, that function is invoked |
| 1659 | * (passing any specified arguments) and the return value is |
| 1660 | * returned. |
| 1661 | * |
| 1662 | * __metatable - Hide the metatable. When "getmetatable( myTable )" is |
| 1663 | * called, if the metatable for myTable has a __metatable |
| 1664 | * key, the value of that key is returned instead of the |
| 1665 | * actual metatable. |
| 1666 | * |
| 1667 | * __tostring - Control string representation. When the builtin |
| 1668 | * "tostring( myTable )" function is called, if the metatable |
| 1669 | * for myTable has a __tostring property set to a function, |
| 1670 | * that function is invoked (passing myTable to it) and the |
| 1671 | * return value is used as the string representation. |
| 1672 | * |
| 1673 | * __len - Control table length. When the table length is requested using |
| 1674 | * the length operator ( '#' ), if the metatable for myTable has |
| 1675 | * a __len key pointing to a function, that function is invoked |
| 1676 | * (passing myTable to it) and the return value used as the value |
| 1677 | * of "#myTable". |
| 1678 | * |
| 1679 | * __gc - Userdata finalizer code. When userdata is set to be garbage |
| 1680 | * collected, if the metatable has a __gc field pointing to a |
| 1681 | * function, that function is first invoked, passing the userdata |
| 1682 | * to it. The __gc metamethod is not called for tables. |
| 1683 | * (See http://lua-users.org/lists/lua-l/2006-11/msg00508.html) |
| 1684 | * |
| 1685 | * Special metamethods for redefining standard operators: |
| 1686 | * http://www.lua.org/pil/13.1.html |
| 1687 | * |
| 1688 | * __add "+" |
| 1689 | * __sub "-" |
| 1690 | * __mul "*" |
| 1691 | * __div "/" |
| 1692 | * __unm "!" |
| 1693 | * __pow "^" |
| 1694 | * __concat ".." |
| 1695 | * |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1696 | * Special methods for redefining standard relations |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1697 | * http://www.lua.org/pil/13.2.html |
| 1698 | * |
| 1699 | * __eq "==" |
| 1700 | * __lt "<" |
| 1701 | * __le "<=" |
| 1702 | */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1703 | |
| 1704 | /* |
| 1705 | * |
| 1706 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1707 | * Class Map |
| 1708 | * |
| 1709 | * |
| 1710 | */ |
| 1711 | |
| 1712 | /* Returns a struct hlua_map if the stack entry "ud" is |
| 1713 | * a class session, otherwise it throws an error. |
| 1714 | */ |
| 1715 | __LJMP static struct map_descriptor *hlua_checkmap(lua_State *L, int ud) |
| 1716 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1717 | return MAY_LJMP(hlua_checkudata(L, ud, class_map_ref)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1718 | } |
| 1719 | |
| 1720 | /* This function is the map constructor. It don't need |
| 1721 | * the class Map object. It creates and return a new Map |
| 1722 | * object. It must be called only during "body" or "init" |
| 1723 | * context because it process some filesystem accesses. |
| 1724 | */ |
| 1725 | __LJMP static int hlua_map_new(struct lua_State *L) |
| 1726 | { |
| 1727 | const char *fn; |
| 1728 | int match = PAT_MATCH_STR; |
| 1729 | struct sample_conv conv; |
| 1730 | const char *file = ""; |
| 1731 | int line = 0; |
| 1732 | lua_Debug ar; |
| 1733 | char *err = NULL; |
| 1734 | struct arg args[2]; |
| 1735 | |
| 1736 | if (lua_gettop(L) < 1 || lua_gettop(L) > 2) |
| 1737 | WILL_LJMP(luaL_error(L, "'new' needs at least 1 argument.")); |
| 1738 | |
| 1739 | fn = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1740 | |
| 1741 | if (lua_gettop(L) >= 2) { |
| 1742 | match = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 1743 | if (match < 0 || match >= PAT_MATCH_NUM) |
| 1744 | WILL_LJMP(luaL_error(L, "'new' needs a valid match method.")); |
| 1745 | } |
| 1746 | |
| 1747 | /* Get Lua filename and line number. */ |
| 1748 | if (lua_getstack(L, 1, &ar)) { /* check function at level */ |
| 1749 | lua_getinfo(L, "Sl", &ar); /* get info about it */ |
| 1750 | if (ar.currentline > 0) { /* is there info? */ |
| 1751 | file = ar.short_src; |
| 1752 | line = ar.currentline; |
| 1753 | } |
| 1754 | } |
| 1755 | |
| 1756 | /* fill fake sample_conv struct. */ |
| 1757 | conv.kw = ""; /* unused. */ |
| 1758 | conv.process = NULL; /* unused. */ |
| 1759 | conv.arg_mask = 0; /* unused. */ |
| 1760 | conv.val_args = NULL; /* unused. */ |
| 1761 | conv.out_type = SMP_T_STR; |
| 1762 | conv.private = (void *)(long)match; |
| 1763 | switch (match) { |
| 1764 | case PAT_MATCH_STR: conv.in_type = SMP_T_STR; break; |
| 1765 | case PAT_MATCH_BEG: conv.in_type = SMP_T_STR; break; |
| 1766 | case PAT_MATCH_SUB: conv.in_type = SMP_T_STR; break; |
| 1767 | case PAT_MATCH_DIR: conv.in_type = SMP_T_STR; break; |
| 1768 | case PAT_MATCH_DOM: conv.in_type = SMP_T_STR; break; |
| 1769 | case PAT_MATCH_END: conv.in_type = SMP_T_STR; break; |
| 1770 | case PAT_MATCH_REG: conv.in_type = SMP_T_STR; break; |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1771 | case PAT_MATCH_INT: conv.in_type = SMP_T_SINT; break; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1772 | case PAT_MATCH_IP: conv.in_type = SMP_T_ADDR; break; |
| 1773 | default: |
| 1774 | WILL_LJMP(luaL_error(L, "'new' doesn't support this match mode.")); |
| 1775 | } |
| 1776 | |
| 1777 | /* fill fake args. */ |
| 1778 | args[0].type = ARGT_STR; |
Christopher Faulet | 73292e9 | 2020-08-06 08:40:09 +0200 | [diff] [blame] | 1779 | args[0].data.str.area = strdup(fn); |
| 1780 | args[0].data.str.data = strlen(fn); |
| 1781 | args[0].data.str.size = args[0].data.str.data+1; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1782 | args[1].type = ARGT_STOP; |
| 1783 | |
| 1784 | /* load the map. */ |
| 1785 | if (!sample_load_map(args, &conv, file, line, &err)) { |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 1786 | /* error case: we can't use luaL_error because we must |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1787 | * free the err variable. |
| 1788 | */ |
| 1789 | luaL_where(L, 1); |
| 1790 | lua_pushfstring(L, "'new': %s.", err); |
| 1791 | lua_concat(L, 2); |
| 1792 | free(err); |
Christopher Faulet | 6ad7df4 | 2020-08-07 11:45:18 +0200 | [diff] [blame] | 1793 | chunk_destroy(&args[0].data.str); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1794 | WILL_LJMP(lua_error(L)); |
| 1795 | } |
| 1796 | |
| 1797 | /* create the lua object. */ |
| 1798 | lua_newtable(L); |
| 1799 | lua_pushlightuserdata(L, args[0].data.map); |
| 1800 | lua_rawseti(L, -2, 0); |
| 1801 | |
| 1802 | /* Pop a class Map metatable and affect it to the userdata. */ |
| 1803 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_map_ref); |
| 1804 | lua_setmetatable(L, -2); |
| 1805 | |
| 1806 | |
| 1807 | return 1; |
| 1808 | } |
| 1809 | |
| 1810 | __LJMP static inline int _hlua_map_lookup(struct lua_State *L, int str) |
| 1811 | { |
| 1812 | struct map_descriptor *desc; |
| 1813 | struct pattern *pat; |
| 1814 | struct sample smp; |
| 1815 | |
| 1816 | MAY_LJMP(check_args(L, 2, "lookup")); |
| 1817 | desc = MAY_LJMP(hlua_checkmap(L, 1)); |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1818 | if (desc->pat.expect_type == SMP_T_SINT) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1819 | smp.data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1820 | smp.data.u.sint = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1821 | } |
| 1822 | else { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1823 | smp.data.type = SMP_T_STR; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1824 | smp.flags = SMP_F_CONST; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1825 | 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] | 1826 | smp.data.u.str.size = smp.data.u.str.data + 1; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1827 | } |
| 1828 | |
| 1829 | pat = pattern_exec_match(&desc->pat, &smp, 1); |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1830 | if (!pat || !pat->data) { |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1831 | if (str) |
| 1832 | lua_pushstring(L, ""); |
| 1833 | else |
| 1834 | lua_pushnil(L); |
| 1835 | return 1; |
| 1836 | } |
| 1837 | |
| 1838 | /* 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] | 1839 | 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] | 1840 | return 1; |
| 1841 | } |
| 1842 | |
| 1843 | __LJMP static int hlua_map_lookup(struct lua_State *L) |
| 1844 | { |
| 1845 | return _hlua_map_lookup(L, 0); |
| 1846 | } |
| 1847 | |
| 1848 | __LJMP static int hlua_map_slookup(struct lua_State *L) |
| 1849 | { |
| 1850 | return _hlua_map_lookup(L, 1); |
| 1851 | } |
| 1852 | |
| 1853 | /* |
| 1854 | * |
| 1855 | * |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1856 | * Class Socket |
| 1857 | * |
| 1858 | * |
| 1859 | */ |
| 1860 | |
| 1861 | __LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud) |
| 1862 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1863 | return MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1864 | } |
| 1865 | |
| 1866 | /* 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] | 1867 | * connection. It is used for notify space available to send or data |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1868 | * received. |
| 1869 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1870 | static void hlua_socket_handler(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1871 | { |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1872 | struct stream_interface *si = appctx->owner; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1873 | |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1874 | if (appctx->ctx.hlua_cosocket.die) { |
| 1875 | si_shutw(si); |
| 1876 | si_shutr(si); |
| 1877 | si_ic(si)->flags |= CF_READ_NULL; |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1878 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1879 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1880 | stream_shutdown(si_strm(si), SF_ERR_KILLED); |
| 1881 | } |
| 1882 | |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 1883 | /* If we can't write, wakeup the pending write signals. */ |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1884 | if (channel_output_closed(si_ic(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1885 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1886 | |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 1887 | /* If we can't read, wakeup the pending read signals. */ |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1888 | if (channel_input_closed(si_oc(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1889 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1890 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 1891 | /* if the connection is not established, inform the stream that we want |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 1892 | * to be notified whenever the connection completes. |
| 1893 | */ |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 1894 | if (si_opposite(si)->state < SI_ST_EST) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 1895 | si_cant_get(si); |
Willy Tarreau | 12c2423 | 2018-12-06 15:29:50 +0100 | [diff] [blame] | 1896 | si_rx_conn_blk(si); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1897 | si_rx_endp_more(si); |
Willy Tarreau | d4da196 | 2015-04-20 01:31:23 +0200 | [diff] [blame] | 1898 | return; |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 1899 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1900 | |
| 1901 | /* This function is called after the connect. */ |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 1902 | appctx->ctx.hlua_cosocket.connected = 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1903 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1904 | /* Wake the tasks which wants to write if the buffer have available space. */ |
Thierry FOURNIER | eba6f64 | 2015-09-26 22:01:07 +0200 | [diff] [blame] | 1905 | if (channel_may_recv(si_ic(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1906 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1907 | |
| 1908 | /* Wake the tasks which wants to read if the buffer contains data. */ |
Thierry FOURNIER | eba6f64 | 2015-09-26 22:01:07 +0200 | [diff] [blame] | 1909 | if (!channel_is_empty(si_oc(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1910 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1911 | |
| 1912 | /* Some data were injected in the buffer, notify the stream |
| 1913 | * interface. |
| 1914 | */ |
| 1915 | if (!channel_is_empty(si_ic(si))) |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 1916 | si_update(si); |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1917 | |
| 1918 | /* If write notifications are registered, we considers we want |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1919 | * to write, so we clear the blocking flag. |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1920 | */ |
| 1921 | if (notification_registered(&appctx->ctx.hlua_cosocket.wake_on_write)) |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1922 | si_rx_endp_more(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1923 | } |
| 1924 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1925 | /* This function is called when the "struct stream" is destroyed. |
| 1926 | * Remove the link from the object to this stream. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1927 | * Wake all the pending signals. |
| 1928 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1929 | static void hlua_socket_release(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1930 | { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1931 | struct xref *peer; |
| 1932 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1933 | /* Remove my link in the original object. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1934 | peer = xref_get_peer_and_lock(&appctx->ctx.hlua_cosocket.xref); |
| 1935 | if (peer) |
| 1936 | xref_disconnect(&appctx->ctx.hlua_cosocket.xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1937 | |
| 1938 | /* Wake all the task waiting for me. */ |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1939 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1940 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1941 | } |
| 1942 | |
| 1943 | /* If the garbage collectio of the object is launch, nobody |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1944 | * uses this object. If the stream does not exists, just quit. |
| 1945 | * Send the shutdown signal to the stream. In some cases, |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1946 | * pending signal can rest in the read and write lists. destroy |
| 1947 | * it. |
| 1948 | */ |
| 1949 | __LJMP static int hlua_socket_gc(lua_State *L) |
| 1950 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1951 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1952 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1953 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1954 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1955 | MAY_LJMP(check_args(L, 1, "__gc")); |
| 1956 | |
| 1957 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1958 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1959 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1960 | return 0; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1961 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1962 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1963 | /* Set the flag which destroy the session. */ |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1964 | appctx->ctx.hlua_cosocket.die = 1; |
| 1965 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1966 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1967 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1968 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1969 | return 0; |
| 1970 | } |
| 1971 | |
| 1972 | /* The close function send shutdown signal and break the |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1973 | * links between the stream and the object. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1974 | */ |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 1975 | __LJMP static int hlua_socket_close_helper(lua_State *L) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1976 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1977 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1978 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1979 | struct xref *peer; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 1980 | struct hlua *hlua; |
| 1981 | |
| 1982 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 1983 | hlua = hlua_gethlua(L); |
| 1984 | if (!hlua) |
| 1985 | return 0; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1986 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1987 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 1988 | |
| 1989 | /* Check if we run on the same thread than the xreator thread. |
| 1990 | * We cannot access to the socket if the thread is different. |
| 1991 | */ |
| 1992 | if (socket->tid != tid) |
| 1993 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 1994 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1995 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1996 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1997 | return 0; |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 1998 | |
| 1999 | hlua->gc_count--; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2000 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2001 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2002 | /* Set the flag which destroy the session. */ |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 2003 | appctx->ctx.hlua_cosocket.die = 1; |
| 2004 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2005 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2006 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2007 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2008 | return 0; |
| 2009 | } |
| 2010 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 2011 | /* The close function calls close_helper. |
| 2012 | */ |
| 2013 | __LJMP static int hlua_socket_close(lua_State *L) |
| 2014 | { |
| 2015 | MAY_LJMP(check_args(L, 1, "close")); |
| 2016 | return hlua_socket_close_helper(L); |
| 2017 | } |
| 2018 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2019 | /* This Lua function assumes that the stack contain three parameters. |
| 2020 | * 1 - USERDATA containing a struct socket |
| 2021 | * 2 - INTEGER with values of the macro defined below |
| 2022 | * If the integer is -1, we must read at most one line. |
| 2023 | * If the integer is -2, we ust read all the data until the |
| 2024 | * end of the stream. |
| 2025 | * If the integer is positive value, we must read a number of |
| 2026 | * bytes corresponding to this value. |
| 2027 | */ |
| 2028 | #define HLSR_READ_LINE (-1) |
| 2029 | #define HLSR_READ_ALL (-2) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2030 | __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] | 2031 | { |
| 2032 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2033 | int wanted = lua_tointeger(L, 2); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2034 | struct hlua *hlua; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2035 | struct appctx *appctx; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2036 | size_t len; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2037 | int nblk; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 2038 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2039 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 2040 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2041 | size_t len2; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 2042 | int skip_at_end = 0; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 2043 | struct channel *oc; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2044 | struct stream_interface *si; |
| 2045 | struct stream *s; |
| 2046 | struct xref *peer; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 2047 | int missing_bytes; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2048 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2049 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 2050 | hlua = hlua_gethlua(L); |
| 2051 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2052 | /* Check if this lua stack is schedulable. */ |
| 2053 | if (!hlua || !hlua->task) |
| 2054 | WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in " |
| 2055 | "'frontend', 'backend' or 'task'")); |
| 2056 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2057 | /* Check if we run on the same thread than the xreator thread. |
| 2058 | * We cannot access to the socket if the thread is different. |
| 2059 | */ |
| 2060 | if (socket->tid != tid) |
| 2061 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2062 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2063 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2064 | peer = xref_get_peer_and_lock(&socket->xref); |
| 2065 | if (!peer) |
| 2066 | goto no_peer; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2067 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2068 | si = appctx->owner; |
| 2069 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2070 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2071 | oc = &s->res; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2072 | if (wanted == HLSR_READ_LINE) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2073 | /* Read line. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2074 | nblk = co_getline_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2075 | if (nblk < 0) /* Connection close. */ |
| 2076 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2077 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2078 | goto connection_empty; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 2079 | |
| 2080 | /* remove final \r\n. */ |
| 2081 | if (nblk == 1) { |
| 2082 | if (blk1[len1-1] == '\n') { |
| 2083 | len1--; |
| 2084 | skip_at_end++; |
| 2085 | if (blk1[len1-1] == '\r') { |
| 2086 | len1--; |
| 2087 | skip_at_end++; |
| 2088 | } |
| 2089 | } |
| 2090 | } |
| 2091 | else { |
| 2092 | if (blk2[len2-1] == '\n') { |
| 2093 | len2--; |
| 2094 | skip_at_end++; |
| 2095 | if (blk2[len2-1] == '\r') { |
| 2096 | len2--; |
| 2097 | skip_at_end++; |
| 2098 | } |
| 2099 | } |
| 2100 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2101 | } |
| 2102 | |
| 2103 | else if (wanted == HLSR_READ_ALL) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2104 | /* Read all the available data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2105 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2106 | if (nblk < 0) /* Connection close. */ |
| 2107 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2108 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2109 | goto connection_empty; |
| 2110 | } |
| 2111 | |
| 2112 | else { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2113 | /* Read a block of data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2114 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2115 | if (nblk < 0) /* Connection close. */ |
| 2116 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2117 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2118 | goto connection_empty; |
| 2119 | |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 2120 | missing_bytes = wanted - socket->b.n; |
| 2121 | if (len1 > missing_bytes) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2122 | nblk = 1; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 2123 | len1 = missing_bytes; |
| 2124 | } if (nblk == 2 && len1 + len2 > missing_bytes) |
| 2125 | len2 = missing_bytes - len1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2126 | } |
| 2127 | |
| 2128 | len = len1; |
| 2129 | |
| 2130 | luaL_addlstring(&socket->b, blk1, len1); |
| 2131 | if (nblk == 2) { |
| 2132 | len += len2; |
| 2133 | luaL_addlstring(&socket->b, blk2, len2); |
| 2134 | } |
| 2135 | |
| 2136 | /* Consume data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2137 | co_skip(oc, len + skip_at_end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2138 | |
| 2139 | /* Don't wait anything. */ |
Thierry FOURNIER | 7e4ee47 | 2018-05-25 15:03:50 +0200 | [diff] [blame] | 2140 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2141 | |
| 2142 | /* If the pattern reclaim to read all the data |
| 2143 | * in the connection, got out. |
| 2144 | */ |
| 2145 | if (wanted == HLSR_READ_ALL) |
| 2146 | goto connection_empty; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 2147 | else if (wanted >= 0 && socket->b.n < wanted) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2148 | goto connection_empty; |
| 2149 | |
| 2150 | /* Return result. */ |
| 2151 | luaL_pushresult(&socket->b); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2152 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2153 | return 1; |
| 2154 | |
| 2155 | connection_closed: |
| 2156 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2157 | xref_unlock(&socket->xref, peer); |
| 2158 | |
| 2159 | no_peer: |
| 2160 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2161 | /* If the buffer containds data. */ |
| 2162 | if (socket->b.n > 0) { |
| 2163 | luaL_pushresult(&socket->b); |
| 2164 | return 1; |
| 2165 | } |
| 2166 | lua_pushnil(L); |
| 2167 | lua_pushstring(L, "connection closed."); |
| 2168 | return 2; |
| 2169 | |
| 2170 | connection_empty: |
| 2171 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2172 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_read, hlua->task)) { |
| 2173 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2174 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2175 | } |
| 2176 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2177 | 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] | 2178 | return 0; |
| 2179 | } |
| 2180 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2181 | /* This Lua function gets two parameters. The first one can be string |
| 2182 | * or a number. If the string is "*l", the user requires one line. If |
| 2183 | * 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] | 2184 | * If the value is a number, the user require a number of bytes equal |
| 2185 | * to the value. The default value is "*l" (a line). |
| 2186 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2187 | * This parameter with a variable type is converted in integer. This |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2188 | * integer takes this values: |
| 2189 | * -1 : read a line |
| 2190 | * -2 : read all the stream |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2191 | * >0 : amount of bytes. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2192 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2193 | * The second parameter is optional. It contains a string that must be |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2194 | * concatenated with the read data. |
| 2195 | */ |
| 2196 | __LJMP static int hlua_socket_receive(struct lua_State *L) |
| 2197 | { |
| 2198 | int wanted = HLSR_READ_LINE; |
| 2199 | const char *pattern; |
Christopher Faulet | c31b200 | 2021-05-03 10:11:13 +0200 | [diff] [blame] | 2200 | int lastarg, type; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2201 | char *error; |
| 2202 | size_t len; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2203 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2204 | |
| 2205 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 2206 | WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments.")); |
| 2207 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2208 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2209 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2210 | /* Check if we run on the same thread than the xreator thread. |
| 2211 | * We cannot access to the socket if the thread is different. |
| 2212 | */ |
| 2213 | if (socket->tid != tid) |
| 2214 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2215 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2216 | /* check for pattern. */ |
| 2217 | if (lua_gettop(L) >= 2) { |
| 2218 | type = lua_type(L, 2); |
| 2219 | if (type == LUA_TSTRING) { |
| 2220 | pattern = lua_tostring(L, 2); |
| 2221 | if (strcmp(pattern, "*a") == 0) |
| 2222 | wanted = HLSR_READ_ALL; |
| 2223 | else if (strcmp(pattern, "*l") == 0) |
| 2224 | wanted = HLSR_READ_LINE; |
| 2225 | else { |
| 2226 | wanted = strtoll(pattern, &error, 10); |
| 2227 | if (*error != '\0') |
| 2228 | WILL_LJMP(luaL_error(L, "Unsupported pattern.")); |
| 2229 | } |
| 2230 | } |
| 2231 | else if (type == LUA_TNUMBER) { |
| 2232 | wanted = lua_tointeger(L, 2); |
| 2233 | if (wanted < 0) |
| 2234 | WILL_LJMP(luaL_error(L, "Unsupported size.")); |
| 2235 | } |
| 2236 | } |
| 2237 | |
| 2238 | /* Set pattern. */ |
| 2239 | lua_pushinteger(L, wanted); |
Tim Duesterhus | c6e377e | 2018-01-04 19:32:13 +0100 | [diff] [blame] | 2240 | |
| 2241 | /* Check if we would replace the top by itself. */ |
| 2242 | if (lua_gettop(L) != 2) |
| 2243 | lua_replace(L, 2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2244 | |
Christopher Faulet | c31b200 | 2021-05-03 10:11:13 +0200 | [diff] [blame] | 2245 | /* Save index of the top of the stack because since buffers are used, it |
| 2246 | * may change |
| 2247 | */ |
| 2248 | lastarg = lua_gettop(L); |
| 2249 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2250 | /* init buffer, and fill it with prefix. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2251 | luaL_buffinit(L, &socket->b); |
| 2252 | |
| 2253 | /* Check prefix. */ |
Christopher Faulet | c31b200 | 2021-05-03 10:11:13 +0200 | [diff] [blame] | 2254 | if (lastarg >= 3) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2255 | if (lua_type(L, 3) != LUA_TSTRING) |
| 2256 | WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix")); |
| 2257 | pattern = lua_tolstring(L, 3, &len); |
| 2258 | luaL_addlstring(&socket->b, pattern, len); |
| 2259 | } |
| 2260 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2261 | return __LJMP(hlua_socket_receive_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2262 | } |
| 2263 | |
| 2264 | /* Write the Lua input string in the output buffer. |
Mark Lakes | 22154b4 | 2018-01-29 14:38:40 -0800 | [diff] [blame] | 2265 | * This function returns a yield if no space is available. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2266 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2267 | 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] | 2268 | { |
| 2269 | struct hlua_socket *socket; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2270 | struct hlua *hlua; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2271 | struct appctx *appctx; |
| 2272 | size_t buf_len; |
| 2273 | const char *buf; |
| 2274 | int len; |
| 2275 | int send_len; |
| 2276 | int sent; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2277 | struct xref *peer; |
| 2278 | struct stream_interface *si; |
| 2279 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2280 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2281 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 2282 | hlua = hlua_gethlua(L); |
| 2283 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2284 | /* Check if this lua stack is schedulable. */ |
| 2285 | if (!hlua || !hlua->task) |
| 2286 | WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in " |
| 2287 | "'frontend', 'backend' or 'task'")); |
| 2288 | |
| 2289 | /* Get object */ |
| 2290 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2291 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2292 | sent = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2293 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2294 | /* Check if we run on the same thread than the xreator thread. |
| 2295 | * We cannot access to the socket if the thread is different. |
| 2296 | */ |
| 2297 | if (socket->tid != tid) |
| 2298 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2299 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2300 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2301 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2302 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2303 | lua_pushinteger(L, -1); |
| 2304 | return 1; |
| 2305 | } |
| 2306 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2307 | si = appctx->owner; |
| 2308 | s = si_strm(si); |
| 2309 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2310 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2311 | if (channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2312 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2313 | lua_pushinteger(L, -1); |
| 2314 | return 1; |
| 2315 | } |
| 2316 | |
| 2317 | /* Update the input buffer data. */ |
| 2318 | buf += sent; |
| 2319 | send_len = buf_len - sent; |
| 2320 | |
| 2321 | /* All the data are sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2322 | if (sent >= buf_len) { |
| 2323 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2324 | return 1; /* Implicitly return the length sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2325 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2326 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2327 | /* Check if the buffer is available because HAProxy doesn't allocate |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 2328 | * the request buffer if its not required. |
| 2329 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2330 | if (s->req.buf.size == 0) { |
Willy Tarreau | 581abd3 | 2018-10-25 10:21:41 +0200 | [diff] [blame] | 2331 | if (!si_alloc_ibuf(si, &appctx->buffer_wait)) |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2332 | goto hlua_socket_write_yield_return; |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 2333 | } |
| 2334 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2335 | /* Check for available space. */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2336 | len = b_room(&s->req.buf); |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2337 | if (len <= 0) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2338 | goto hlua_socket_write_yield_return; |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2339 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2340 | |
| 2341 | /* send data */ |
| 2342 | if (len < send_len) |
| 2343 | send_len = len; |
Thierry FOURNIER | 66b8919 | 2018-05-27 01:14:47 +0200 | [diff] [blame] | 2344 | len = ci_putblk(&s->req, buf, send_len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2345 | |
| 2346 | /* "Not enough space" (-1), "Buffer too little to contain |
| 2347 | * the data" (-2) are not expected because the available length |
| 2348 | * is tested. |
| 2349 | * Other unknown error are also not expected. |
| 2350 | */ |
| 2351 | if (len <= 0) { |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2352 | if (len == -1) |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2353 | s->req.flags |= CF_WAKE_WRITE; |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2354 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 2355 | MAY_LJMP(hlua_socket_close_helper(L)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2356 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2357 | lua_pushinteger(L, -1); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2358 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2359 | return 1; |
| 2360 | } |
| 2361 | |
| 2362 | /* update buffers. */ |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 2363 | appctx_wakeup(appctx); |
Willy Tarreau | de70fa1 | 2015-09-26 11:25:05 +0200 | [diff] [blame] | 2364 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2365 | s->req.rex = TICK_ETERNITY; |
| 2366 | s->res.wex = TICK_ETERNITY; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2367 | |
| 2368 | /* Update length sent. */ |
| 2369 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2370 | lua_pushinteger(L, sent + len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2371 | |
| 2372 | /* All the data buffer is sent ? */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2373 | if (sent + len >= buf_len) { |
| 2374 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2375 | return 1; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2376 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2377 | |
| 2378 | hlua_socket_write_yield_return: |
Thierry FOURNIER | ba42fcd | 2018-05-27 00:59:48 +0200 | [diff] [blame] | 2379 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2380 | xref_unlock(&socket->xref, peer); |
| 2381 | WILL_LJMP(luaL_error(L, "out of memory")); |
| 2382 | } |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2383 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2384 | 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] | 2385 | return 0; |
| 2386 | } |
| 2387 | |
| 2388 | /* This function initiate the send of data. It just check the input |
| 2389 | * parameters and push an integer in the Lua stack that contain the |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2390 | * 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] | 2391 | * "hlua_socket_write_yield" that can yield. |
| 2392 | * |
| 2393 | * The Lua function gets between 3 and 4 parameters. The first one is |
| 2394 | * the associated object. The second is a string buffer. The third is |
| 2395 | * a facultative integer that represents where is the buffer position |
| 2396 | * of the start of the data that can send. The first byte is the |
| 2397 | * position "1". The default value is "1". The fourth argument is a |
| 2398 | * facultative integer that represents where is the buffer position |
| 2399 | * of the end of the data that can send. The default is the last byte. |
| 2400 | */ |
| 2401 | static int hlua_socket_send(struct lua_State *L) |
| 2402 | { |
| 2403 | int i; |
| 2404 | int j; |
| 2405 | const char *buf; |
| 2406 | size_t buf_len; |
| 2407 | |
| 2408 | /* Check number of arguments. */ |
| 2409 | if (lua_gettop(L) < 2 || lua_gettop(L) > 4) |
| 2410 | WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments")); |
| 2411 | |
| 2412 | /* Get the string. */ |
| 2413 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
| 2414 | |
| 2415 | /* Get and check j. */ |
| 2416 | if (lua_gettop(L) == 4) { |
| 2417 | j = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 2418 | if (j < 0) |
| 2419 | j = buf_len + j + 1; |
| 2420 | if (j > buf_len) |
| 2421 | j = buf_len + 1; |
| 2422 | lua_pop(L, 1); |
| 2423 | } |
| 2424 | else |
| 2425 | j = buf_len; |
| 2426 | |
| 2427 | /* Get and check i. */ |
| 2428 | if (lua_gettop(L) == 3) { |
| 2429 | i = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2430 | if (i < 0) |
| 2431 | i = buf_len + i + 1; |
| 2432 | if (i > buf_len) |
| 2433 | i = buf_len + 1; |
| 2434 | lua_pop(L, 1); |
| 2435 | } else |
| 2436 | i = 1; |
| 2437 | |
| 2438 | /* Check bth i and j. */ |
| 2439 | if (i > j) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2440 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2441 | return 1; |
| 2442 | } |
| 2443 | if (i == 0 && j == 0) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2444 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2445 | return 1; |
| 2446 | } |
| 2447 | if (i == 0) |
| 2448 | i = 1; |
| 2449 | if (j == 0) |
| 2450 | j = 1; |
| 2451 | |
| 2452 | /* Pop the string. */ |
| 2453 | lua_pop(L, 1); |
| 2454 | |
| 2455 | /* Update the buffer length. */ |
| 2456 | buf += i - 1; |
| 2457 | buf_len = j - i + 1; |
| 2458 | lua_pushlstring(L, buf, buf_len); |
| 2459 | |
| 2460 | /* This unsigned is used to remember the amount of sent data. */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2461 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2462 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2463 | return MAY_LJMP(hlua_socket_write_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2464 | } |
| 2465 | |
Willy Tarreau | 22b0a68 | 2015-06-17 19:43:49 +0200 | [diff] [blame] | 2466 | #define SOCKET_INFO_MAX_LEN sizeof("[0000:0000:0000:0000:0000:0000:0000:0000]:12345") |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2467 | __LJMP static inline int hlua_socket_info(struct lua_State *L, struct sockaddr_storage *addr) |
| 2468 | { |
| 2469 | static char buffer[SOCKET_INFO_MAX_LEN]; |
| 2470 | int ret; |
| 2471 | int len; |
| 2472 | char *p; |
| 2473 | |
| 2474 | ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1); |
| 2475 | if (ret <= 0) { |
| 2476 | lua_pushnil(L); |
| 2477 | return 1; |
| 2478 | } |
| 2479 | |
| 2480 | if (ret == AF_UNIX) { |
| 2481 | lua_pushstring(L, buffer+1); |
| 2482 | return 1; |
| 2483 | } |
| 2484 | else if (ret == AF_INET6) { |
| 2485 | buffer[0] = '['; |
| 2486 | len = strlen(buffer); |
| 2487 | buffer[len] = ']'; |
| 2488 | len++; |
| 2489 | buffer[len] = ':'; |
| 2490 | len++; |
| 2491 | p = buffer; |
| 2492 | } |
| 2493 | else if (ret == AF_INET) { |
| 2494 | p = buffer + 1; |
| 2495 | len = strlen(p); |
| 2496 | p[len] = ':'; |
| 2497 | len++; |
| 2498 | } |
| 2499 | else { |
| 2500 | lua_pushnil(L); |
| 2501 | return 1; |
| 2502 | } |
| 2503 | |
| 2504 | if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) { |
| 2505 | lua_pushnil(L); |
| 2506 | return 1; |
| 2507 | } |
| 2508 | |
| 2509 | lua_pushstring(L, p); |
| 2510 | return 1; |
| 2511 | } |
| 2512 | |
| 2513 | /* Returns information about the peer of the connection. */ |
| 2514 | __LJMP static int hlua_socket_getpeername(struct lua_State *L) |
| 2515 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2516 | struct hlua_socket *socket; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2517 | struct xref *peer; |
| 2518 | struct appctx *appctx; |
| 2519 | struct stream_interface *si; |
| 2520 | struct stream *s; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2521 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2522 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2523 | MAY_LJMP(check_args(L, 1, "getpeername")); |
| 2524 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2525 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2526 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2527 | /* Check if we run on the same thread than the xreator thread. |
| 2528 | * We cannot access to the socket if the thread is different. |
| 2529 | */ |
| 2530 | if (socket->tid != tid) |
| 2531 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2532 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2533 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2534 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2535 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2536 | lua_pushnil(L); |
| 2537 | return 1; |
| 2538 | } |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2539 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2540 | si = appctx->owner; |
| 2541 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2542 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2543 | if (!s->target_addr) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2544 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2545 | lua_pushnil(L); |
| 2546 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2547 | } |
| 2548 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2549 | ret = MAY_LJMP(hlua_socket_info(L, s->target_addr)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2550 | xref_unlock(&socket->xref, peer); |
| 2551 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2552 | } |
| 2553 | |
| 2554 | /* Returns information about my connection side. */ |
| 2555 | static int hlua_socket_getsockname(struct lua_State *L) |
| 2556 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2557 | struct hlua_socket *socket; |
| 2558 | struct connection *conn; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2559 | struct appctx *appctx; |
| 2560 | struct xref *peer; |
| 2561 | struct stream_interface *si; |
| 2562 | struct stream *s; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2563 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2564 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2565 | MAY_LJMP(check_args(L, 1, "getsockname")); |
| 2566 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2567 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2568 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2569 | /* Check if we run on the same thread than the xreator thread. |
| 2570 | * We cannot access to the socket if the thread is different. |
| 2571 | */ |
| 2572 | if (socket->tid != tid) |
| 2573 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2574 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2575 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2576 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2577 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2578 | lua_pushnil(L); |
| 2579 | return 1; |
| 2580 | } |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2581 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2582 | si = appctx->owner; |
| 2583 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2584 | |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 2585 | conn = cs_conn(objt_cs(s->si[1].end)); |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2586 | if (!conn || !conn_get_src(conn)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2587 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2588 | lua_pushnil(L); |
| 2589 | return 1; |
| 2590 | } |
| 2591 | |
Willy Tarreau | 9da9a6f | 2019-07-17 14:49:44 +0200 | [diff] [blame] | 2592 | ret = hlua_socket_info(L, conn->src); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2593 | xref_unlock(&socket->xref, peer); |
| 2594 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2595 | } |
| 2596 | |
| 2597 | /* This struct define the applet. */ |
Willy Tarreau | 3057645 | 2015-04-13 13:50:30 +0200 | [diff] [blame] | 2598 | static struct applet update_applet = { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2599 | .obj_type = OBJ_TYPE_APPLET, |
| 2600 | .name = "<LUA_TCP>", |
| 2601 | .fct = hlua_socket_handler, |
| 2602 | .release = hlua_socket_release, |
| 2603 | }; |
| 2604 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2605 | __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] | 2606 | { |
| 2607 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2608 | struct hlua *hlua; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2609 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2610 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2611 | struct stream_interface *si; |
| 2612 | struct stream *s; |
| 2613 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2614 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 2615 | hlua = hlua_gethlua(L); |
| 2616 | if (!hlua) |
| 2617 | return 0; |
| 2618 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2619 | /* Check if we run on the same thread than the xreator thread. |
| 2620 | * We cannot access to the socket if the thread is different. |
| 2621 | */ |
| 2622 | if (socket->tid != tid) |
| 2623 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2624 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2625 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2626 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2627 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2628 | lua_pushnil(L); |
| 2629 | lua_pushstring(L, "Can't connect"); |
| 2630 | return 2; |
| 2631 | } |
| 2632 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2633 | si = appctx->owner; |
| 2634 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2635 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2636 | /* Check if we run on the same thread than the xreator thread. |
| 2637 | * We cannot access to the socket if the thread is different. |
| 2638 | */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2639 | if (socket->tid != tid) { |
| 2640 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2641 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2642 | } |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2643 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2644 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2645 | if (!hlua || channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2646 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2647 | lua_pushnil(L); |
| 2648 | lua_pushstring(L, "Can't connect"); |
| 2649 | return 2; |
| 2650 | } |
| 2651 | |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 2652 | appctx = __objt_appctx(s->si[0].end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2653 | |
| 2654 | /* Check for connection established. */ |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2655 | if (appctx->ctx.hlua_cosocket.connected) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2656 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2657 | lua_pushinteger(L, 1); |
| 2658 | return 1; |
| 2659 | } |
| 2660 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2661 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2662 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2663 | WILL_LJMP(luaL_error(L, "out of memory error")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2664 | } |
| 2665 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2666 | 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] | 2667 | return 0; |
| 2668 | } |
| 2669 | |
| 2670 | /* This function fail or initite the connection. */ |
| 2671 | __LJMP static int hlua_socket_connect(struct lua_State *L) |
| 2672 | { |
| 2673 | struct hlua_socket *socket; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2674 | int port = -1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2675 | const char *ip; |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2676 | struct hlua *hlua; |
| 2677 | struct appctx *appctx; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2678 | int low, high; |
| 2679 | struct sockaddr_storage *addr; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2680 | struct xref *peer; |
| 2681 | struct stream_interface *si; |
| 2682 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2683 | |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2684 | if (lua_gettop(L) < 2) |
| 2685 | WILL_LJMP(luaL_error(L, "connect: need at least 2 arguments")); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2686 | |
| 2687 | /* Get args. */ |
| 2688 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2689 | |
| 2690 | /* Check if we run on the same thread than the xreator thread. |
| 2691 | * We cannot access to the socket if the thread is different. |
| 2692 | */ |
| 2693 | if (socket->tid != tid) |
| 2694 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2695 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2696 | ip = MAY_LJMP(luaL_checkstring(L, 2)); |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 2697 | if (lua_gettop(L) >= 3) { |
| 2698 | luaL_Buffer b; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2699 | port = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2700 | |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 2701 | /* Force the ip to end with a colon, to support IPv6 addresses |
| 2702 | * that are not enclosed within square brackets. |
| 2703 | */ |
| 2704 | if (port > 0) { |
| 2705 | luaL_buffinit(L, &b); |
| 2706 | luaL_addstring(&b, ip); |
| 2707 | luaL_addchar(&b, ':'); |
| 2708 | luaL_pushresult(&b); |
| 2709 | ip = lua_tolstring(L, lua_gettop(L), NULL); |
| 2710 | } |
| 2711 | } |
| 2712 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2713 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2714 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2715 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2716 | lua_pushnil(L); |
| 2717 | return 1; |
| 2718 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2719 | |
| 2720 | /* Parse ip address. */ |
Willy Tarreau | 5fc9328 | 2020-09-16 18:25:03 +0200 | [diff] [blame] | 2721 | 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] | 2722 | if (!addr) { |
| 2723 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2724 | WILL_LJMP(luaL_error(L, "connect: cannot parse destination address '%s'", ip)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2725 | } |
Willy Tarreau | 9da9a6f | 2019-07-17 14:49:44 +0200 | [diff] [blame] | 2726 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2727 | /* Set port. */ |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2728 | if (low == 0) { |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2729 | if (addr->ss_family == AF_INET) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2730 | if (port == -1) { |
| 2731 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2732 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2733 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2734 | ((struct sockaddr_in *)addr)->sin_port = htons(port); |
| 2735 | } else if (addr->ss_family == AF_INET6) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2736 | if (port == -1) { |
| 2737 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2738 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2739 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2740 | ((struct sockaddr_in6 *)addr)->sin6_port = htons(port); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2741 | } |
| 2742 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2743 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2744 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2745 | si = appctx->owner; |
| 2746 | s = si_strm(si); |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2747 | |
Willy Tarreau | 9b7587a | 2020-10-15 07:32:10 +0200 | [diff] [blame] | 2748 | if (!sockaddr_alloc(&s->target_addr, addr, sizeof(*addr))) { |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2749 | xref_unlock(&socket->xref, peer); |
| 2750 | WILL_LJMP(luaL_error(L, "connect: internal error")); |
| 2751 | } |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2752 | s->flags |= SF_ADDR_SET; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2753 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2754 | /* Get hlua struct, or NULL if we execute from main lua state */ |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2755 | hlua = hlua_gethlua(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2756 | if (!hlua) |
| 2757 | return 0; |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2758 | |
| 2759 | /* inform the stream that we want to be notified whenever the |
| 2760 | * connection completes. |
| 2761 | */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 2762 | si_cant_get(&s->si[0]); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 2763 | si_rx_endp_more(&s->si[0]); |
Thierry FOURNIER | 8c8fbbe | 2015-09-26 17:02:35 +0200 | [diff] [blame] | 2764 | appctx_wakeup(appctx); |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2765 | |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 2766 | hlua->gc_count++; |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 2767 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2768 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2769 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2770 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2771 | } |
| 2772 | xref_unlock(&socket->xref, peer); |
PiBa-NL | 706d5ee | 2018-05-05 23:51:42 +0200 | [diff] [blame] | 2773 | |
| 2774 | task_wakeup(s->task, TASK_WOKEN_INIT); |
| 2775 | /* Return yield waiting for connection. */ |
| 2776 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2777 | 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] | 2778 | |
| 2779 | return 0; |
| 2780 | } |
| 2781 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2782 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2783 | __LJMP static int hlua_socket_connect_ssl(struct lua_State *L) |
| 2784 | { |
| 2785 | struct hlua_socket *socket; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2786 | struct xref *peer; |
| 2787 | struct appctx *appctx; |
| 2788 | struct stream_interface *si; |
| 2789 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2790 | |
| 2791 | MAY_LJMP(check_args(L, 3, "connect_ssl")); |
| 2792 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2793 | |
| 2794 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2795 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2796 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2797 | lua_pushnil(L); |
| 2798 | return 1; |
| 2799 | } |
| 2800 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2801 | si = appctx->owner; |
| 2802 | s = si_strm(si); |
| 2803 | |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 2804 | s->target = &socket_ssl->obj_type; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2805 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2806 | return MAY_LJMP(hlua_socket_connect(L)); |
| 2807 | } |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2808 | #endif |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2809 | |
| 2810 | __LJMP static int hlua_socket_setoption(struct lua_State *L) |
| 2811 | { |
| 2812 | return 0; |
| 2813 | } |
| 2814 | |
| 2815 | __LJMP static int hlua_socket_settimeout(struct lua_State *L) |
| 2816 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2817 | struct hlua_socket *socket; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2818 | int tmout; |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2819 | double dtmout; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2820 | struct xref *peer; |
| 2821 | struct appctx *appctx; |
| 2822 | struct stream_interface *si; |
| 2823 | struct stream *s; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2824 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2825 | MAY_LJMP(check_args(L, 2, "settimeout")); |
| 2826 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2827 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2828 | |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2829 | /* convert the timeout to millis */ |
| 2830 | dtmout = MAY_LJMP(luaL_checknumber(L, 2)) * 1000; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2831 | |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 2832 | /* Check for negative values */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2833 | if (dtmout < 0) |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 2834 | WILL_LJMP(luaL_error(L, "settimeout: cannot set negatives values")); |
| 2835 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2836 | if (dtmout > INT_MAX) /* overflow check */ |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2837 | 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] | 2838 | |
| 2839 | tmout = MS_TO_TICKS((int)dtmout); |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2840 | if (tmout == 0) |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 2841 | tmout++; /* very small timeouts are adjusted to a minimum of 1ms */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2842 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2843 | /* Check if we run on the same thread than the xreator thread. |
| 2844 | * We cannot access to the socket if the thread is different. |
| 2845 | */ |
| 2846 | if (socket->tid != tid) |
| 2847 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2848 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2849 | /* check for connection break. If some data were read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2850 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2851 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2852 | hlua_pusherror(L, "socket: not yet initialised, you can't set timeouts."); |
| 2853 | WILL_LJMP(lua_error(L)); |
| 2854 | return 0; |
| 2855 | } |
| 2856 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2857 | si = appctx->owner; |
| 2858 | s = si_strm(si); |
| 2859 | |
Cyril Bonté | 7bb6345 | 2018-08-17 23:51:02 +0200 | [diff] [blame] | 2860 | s->sess->fe->timeout.connect = tmout; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2861 | s->req.rto = tmout; |
| 2862 | s->req.wto = tmout; |
| 2863 | s->res.rto = tmout; |
| 2864 | s->res.wto = tmout; |
Cyril Bonté | 7bb6345 | 2018-08-17 23:51:02 +0200 | [diff] [blame] | 2865 | s->req.rex = tick_add_ifset(now_ms, tmout); |
| 2866 | s->req.wex = tick_add_ifset(now_ms, tmout); |
| 2867 | s->res.rex = tick_add_ifset(now_ms, tmout); |
| 2868 | s->res.wex = tick_add_ifset(now_ms, tmout); |
| 2869 | |
| 2870 | s->task->expire = tick_add_ifset(now_ms, tmout); |
| 2871 | task_queue(s->task); |
| 2872 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2873 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2874 | |
Thierry Fournier | e9636f1 | 2018-03-08 09:54:32 +0100 | [diff] [blame] | 2875 | lua_pushinteger(L, 1); |
Tim Duesterhus | 119a5f1 | 2018-01-06 19:16:25 +0100 | [diff] [blame] | 2876 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2877 | } |
| 2878 | |
| 2879 | __LJMP static int hlua_socket_new(lua_State *L) |
| 2880 | { |
| 2881 | struct hlua_socket *socket; |
| 2882 | struct appctx *appctx; |
Willy Tarreau | 15b5e14 | 2015-04-04 14:38:25 +0200 | [diff] [blame] | 2883 | struct session *sess; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2884 | struct stream *strm; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2885 | |
| 2886 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2887 | if (!lua_checkstack(L, 3)) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2888 | hlua_pusherror(L, "socket: full stack"); |
| 2889 | goto out_fail_conf; |
| 2890 | } |
| 2891 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2892 | /* Create the object: obj[0] = userdata. */ |
| 2893 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2894 | socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket))); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2895 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2896 | memset(socket, 0, sizeof(*socket)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2897 | socket->tid = tid; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2898 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 2899 | /* Check if the various memory pools are initialized. */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 2900 | if (!pool_head_stream || !pool_head_buffer) { |
Thierry FOURNIER | 4a6170c | 2015-03-09 17:07:10 +0100 | [diff] [blame] | 2901 | hlua_pusherror(L, "socket: uninitialized pools."); |
| 2902 | goto out_fail_conf; |
| 2903 | } |
| 2904 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2905 | /* Pop a class stream metatable and affect it to the userdata. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2906 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref); |
| 2907 | lua_setmetatable(L, -2); |
| 2908 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2909 | /* Create the applet context */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 2910 | appctx = appctx_new(&update_applet, tid_bit); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2911 | if (!appctx) { |
| 2912 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | feb7640 | 2015-04-03 14:10:06 +0200 | [diff] [blame] | 2913 | goto out_fail_conf; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2914 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2915 | |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2916 | appctx->ctx.hlua_cosocket.connected = 0; |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 2917 | appctx->ctx.hlua_cosocket.die = 0; |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2918 | LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_write); |
| 2919 | LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_read); |
Willy Tarreau | b2bf833 | 2015-04-04 15:58:58 +0200 | [diff] [blame] | 2920 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2921 | /* Now create a session, task and stream for this applet */ |
Amaury Denoyelle | 239fdbf | 2021-03-24 10:22:03 +0100 | [diff] [blame] | 2922 | sess = session_new(socket_proxy, NULL, &appctx->obj_type); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2923 | if (!sess) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2924 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2925 | goto out_fail_sess; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2926 | } |
| 2927 | |
Christopher Faulet | 26256f8 | 2020-09-14 11:40:13 +0200 | [diff] [blame] | 2928 | strm = stream_new(sess, &appctx->obj_type, &BUF_NULL); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2929 | if (!strm) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2930 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2931 | goto out_fail_stream; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2932 | } |
| 2933 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2934 | /* Initialise cross reference between stream and Lua socket object. */ |
| 2935 | xref_create(&socket->xref, &appctx->ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2936 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2937 | /* Configure "right" stream interface. this "si" is used to connect |
| 2938 | * and retrieve data from the server. The connection is initialized |
| 2939 | * with the "struct server". |
| 2940 | */ |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2941 | si_set_state(&strm->si[1], SI_ST_ASS); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2942 | |
| 2943 | /* Force destination server. */ |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2944 | strm->flags |= SF_DIRECT | SF_ASSIGNED | SF_BE_ASSIGNED; |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 2945 | strm->target = &socket_tcp->obj_type; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2946 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2947 | return 1; |
| 2948 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2949 | out_fail_stream: |
Willy Tarreau | 11c3624 | 2015-04-04 15:54:03 +0200 | [diff] [blame] | 2950 | session_free(sess); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2951 | out_fail_sess: |
| 2952 | appctx_free(appctx); |
| 2953 | out_fail_conf: |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2954 | WILL_LJMP(lua_error(L)); |
| 2955 | return 0; |
| 2956 | } |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 2957 | |
| 2958 | /* |
| 2959 | * |
| 2960 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2961 | * Class Channel |
| 2962 | * |
| 2963 | * |
| 2964 | */ |
| 2965 | |
| 2966 | /* Returns the struct hlua_channel join to the class channel in the |
| 2967 | * stack entry "ud" or throws an argument error. |
| 2968 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2969 | __LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2970 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 2971 | return MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2972 | } |
| 2973 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2974 | /* Pushes the channel onto the top of the stack. If the stask does not have a |
| 2975 | * free slots, the function fails and returns 0; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2976 | */ |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 2977 | static int hlua_channel_new(lua_State *L, struct channel *channel) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2978 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2979 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2980 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2981 | return 0; |
| 2982 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2983 | lua_newtable(L); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2984 | lua_pushlightuserdata(L, channel); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2985 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2986 | |
| 2987 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 2988 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref); |
| 2989 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2990 | return 1; |
| 2991 | } |
| 2992 | |
| 2993 | /* Duplicate all the data present in the input channel and put it |
| 2994 | * in a string LUA variables. Returns -1 and push a nil value in |
| 2995 | * the stack if the channel is closed and all the data are consumed, |
| 2996 | * returns 0 if no data are available, otherwise it returns the length |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 2997 | * of the built string. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2998 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2999 | static inline int _hlua_channel_dup(struct channel *chn, lua_State *L) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3000 | { |
| 3001 | char *blk1; |
| 3002 | char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3003 | size_t len1; |
| 3004 | size_t len2; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3005 | int ret; |
| 3006 | luaL_Buffer b; |
| 3007 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3008 | ret = ci_getblk_nc(chn, &blk1, &len1, &blk2, &len2); |
Christopher Faulet | 055310f | 2021-08-05 11:58:37 +0200 | [diff] [blame] | 3009 | if (unlikely(ret <= 0)) { |
| 3010 | if (ret < 0 || HLUA_CANT_YIELD(hlua_gethlua(L))) { |
| 3011 | lua_pushnil(L); |
| 3012 | return -1; |
| 3013 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3014 | return 0; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3015 | } |
| 3016 | |
| 3017 | luaL_buffinit(L, &b); |
| 3018 | luaL_addlstring(&b, blk1, len1); |
| 3019 | if (unlikely(ret == 2)) |
| 3020 | luaL_addlstring(&b, blk2, len2); |
| 3021 | luaL_pushresult(&b); |
| 3022 | |
| 3023 | if (unlikely(ret == 2)) |
| 3024 | return len1 + len2; |
| 3025 | return len1; |
| 3026 | } |
| 3027 | |
| 3028 | /* "_hlua_channel_dup" wrapper. If no data are available, it returns |
| 3029 | * a yield. This function keep the data in the buffer. |
| 3030 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3031 | __LJMP static int hlua_channel_dup_yield(lua_State *L, int status, lua_KContext ctx) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3032 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3033 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3034 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3035 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3036 | |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 3037 | if (IS_HTX_STRM(chn_strm(chn))) { |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3038 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3039 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3040 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3041 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3042 | if (_hlua_channel_dup(chn, L) == 0) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3043 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_dup_yield, TICK_ETERNITY, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3044 | return 1; |
| 3045 | } |
| 3046 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3047 | /* Check arguments for the function "hlua_channel_dup_yield". */ |
| 3048 | __LJMP static int hlua_channel_dup(lua_State *L) |
| 3049 | { |
| 3050 | MAY_LJMP(check_args(L, 1, "dup")); |
| 3051 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3052 | return MAY_LJMP(hlua_channel_dup_yield(L, 0, 0)); |
| 3053 | } |
| 3054 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3055 | /* "_hlua_channel_dup" wrapper. If no data are available, it returns |
| 3056 | * a yield. This function consumes the data in the buffer. It returns |
| 3057 | * a string containing the data or a nil pointer if no data are available |
| 3058 | * and the channel is closed. |
| 3059 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3060 | __LJMP static int hlua_channel_get_yield(lua_State *L, int status, lua_KContext ctx) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3061 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3062 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3063 | int ret; |
| 3064 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3065 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3066 | |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 3067 | if (IS_HTX_STRM(chn_strm(chn))) { |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3068 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3069 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3070 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3071 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3072 | ret = _hlua_channel_dup(chn, L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3073 | if (unlikely(ret == 0)) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3074 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_get_yield, TICK_ETERNITY, 0)); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3075 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3076 | if (unlikely(ret == -1)) |
| 3077 | return 1; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3078 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3079 | b_sub(&chn->buf, ret); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3080 | return 1; |
| 3081 | } |
| 3082 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3083 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3084 | __LJMP static int hlua_channel_get(lua_State *L) |
| 3085 | { |
| 3086 | MAY_LJMP(check_args(L, 1, "get")); |
| 3087 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3088 | return MAY_LJMP(hlua_channel_get_yield(L, 0, 0)); |
| 3089 | } |
| 3090 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3091 | /* This functions consumes and returns one line. If the channel is closed, |
| 3092 | * 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] | 3093 | * 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] | 3094 | * value. |
| 3095 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3096 | __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] | 3097 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3098 | char *blk1; |
| 3099 | char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3100 | size_t len1; |
| 3101 | size_t len2; |
| 3102 | size_t len; |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3103 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3104 | int ret; |
| 3105 | luaL_Buffer b; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3106 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3107 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3108 | |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 3109 | if (IS_HTX_STRM(chn_strm(chn))) { |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3110 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3111 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3112 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3113 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3114 | ret = ci_getline_nc(chn, &blk1, &len1, &blk2, &len2); |
Christopher Faulet | 055310f | 2021-08-05 11:58:37 +0200 | [diff] [blame] | 3115 | if (ret == 0) { |
| 3116 | if (HLUA_CANT_YIELD(hlua_gethlua(L))) { |
| 3117 | _hlua_channel_dup(chn, L); |
| 3118 | return 1; |
| 3119 | } |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3120 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_getline_yield, TICK_ETERNITY, 0)); |
Christopher Faulet | 055310f | 2021-08-05 11:58:37 +0200 | [diff] [blame] | 3121 | } |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3122 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3123 | if (ret == -1) { |
| 3124 | lua_pushnil(L); |
| 3125 | return 1; |
| 3126 | } |
| 3127 | |
| 3128 | luaL_buffinit(L, &b); |
| 3129 | luaL_addlstring(&b, blk1, len1); |
| 3130 | len = len1; |
| 3131 | if (unlikely(ret == 2)) { |
| 3132 | luaL_addlstring(&b, blk2, len2); |
| 3133 | len += len2; |
| 3134 | } |
| 3135 | luaL_pushresult(&b); |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3136 | b_rep_blk(&chn->buf, ci_head(chn), ci_head(chn) + len, NULL, 0); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3137 | return 1; |
| 3138 | } |
| 3139 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3140 | /* Check arguments for the function "hlua_channel_getline_yield". */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3141 | __LJMP static int hlua_channel_getline(lua_State *L) |
| 3142 | { |
| 3143 | MAY_LJMP(check_args(L, 1, "getline")); |
| 3144 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3145 | return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0)); |
| 3146 | } |
| 3147 | |
Christopher Faulet | 3b7bc3e | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3148 | /* This function takes a string as argument, and append it at the input side of |
| 3149 | * channel. If data cannot be copied, because there is not enough space to do so |
| 3150 | * or because the channel is closed, it returns -1. Otherwise, it returns the |
| 3151 | * amount of data written (the string length). This function does not yield. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3152 | */ |
Christopher Faulet | 3b7bc3e | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3153 | __LJMP static int hlua_channel_append(lua_State *L) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3154 | { |
Christopher Faulet | 3b7bc3e | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3155 | struct channel *chn; |
| 3156 | const char *str; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3157 | size_t len; |
Christopher Faulet | 3b7bc3e | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3158 | |
| 3159 | MAY_LJMP(check_args(L, 2, "append")); |
| 3160 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3161 | str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3162 | |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 3163 | if (IS_HTX_STRM(chn_strm(chn))) { |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3164 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3165 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3166 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3167 | |
Christopher Faulet | 3b7bc3e | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3168 | if (len > c_room(chn) || ci_putblk(chn, str, len) < 0) |
| 3169 | lua_pushinteger(L, -1); |
| 3170 | else |
| 3171 | lua_pushinteger(L, len); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3172 | return 1; |
| 3173 | } |
| 3174 | |
Christopher Faulet | 3b7bc3e | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3175 | /* The function replaces input data from the channel by the string passed as |
| 3176 | * argument. Before clearing the buffer, we take care the copy will be |
| 3177 | * possible. It returns the amount of data written (the string length) on |
| 3178 | * success or -1 if the copy is not performed. This function does not yield. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3179 | */ |
| 3180 | __LJMP static int hlua_channel_set(lua_State *L) |
| 3181 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3182 | struct channel *chn; |
Christopher Faulet | 3b7bc3e | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3183 | const char *str; |
| 3184 | size_t len; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3185 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3186 | MAY_LJMP(check_args(L, 2, "set")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3187 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 3b7bc3e | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3188 | str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3189 | |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 3190 | if (IS_HTX_STRM(chn_strm(chn))) { |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3191 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3192 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3193 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3194 | |
Christopher Faulet | 3b7bc3e | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3195 | /* Be sure we can copied the string once input data will be removed. */ |
| 3196 | if (len > ci_data(chn) || channel_input_closed(chn)) |
| 3197 | lua_pushinteger(L, -1); |
| 3198 | else { |
| 3199 | b_set_data(&chn->buf, co_data(chn)); |
| 3200 | if (ci_putblk(chn, str, len) < 0) |
| 3201 | lua_pushinteger(L, -1); |
| 3202 | else |
| 3203 | lua_pushinteger(L, len); |
| 3204 | } |
| 3205 | return -1; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3206 | } |
| 3207 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3208 | /* Append data in the output side of the buffer. This data is immediately |
| 3209 | * sent. The function returns the amount of data written. If the buffer |
| 3210 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3211 | * if the channel is closed. |
| 3212 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3213 | __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] | 3214 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3215 | struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3216 | size_t len; |
| 3217 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3218 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3219 | int max; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 3220 | struct hlua *hlua; |
| 3221 | |
| 3222 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 3223 | hlua = hlua_gethlua(L); |
| 3224 | if (!hlua) { |
| 3225 | lua_pushnil(L); |
| 3226 | return 1; |
| 3227 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3228 | |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 3229 | if (IS_HTX_STRM(chn_strm(chn))) { |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3230 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3231 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3232 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3233 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3234 | if (unlikely(channel_output_closed(chn))) { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3235 | lua_pushinteger(L, -1); |
| 3236 | return 1; |
| 3237 | } |
| 3238 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3239 | /* Check if the buffer is available because HAProxy doesn't allocate |
Thierry FOURNIER | 3e3a608 | 2015-03-05 17:06:12 +0100 | [diff] [blame] | 3240 | * the request buffer if its not required. |
| 3241 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3242 | if (chn->buf.size == 0) { |
Christopher Faulet | 055310f | 2021-08-05 11:58:37 +0200 | [diff] [blame] | 3243 | if (HLUA_CANT_YIELD(hlua_gethlua(L))) |
| 3244 | return 1; |
Willy Tarreau | 4b962a4 | 2018-11-15 11:03:21 +0100 | [diff] [blame] | 3245 | si_rx_buff_blk(chn_prod(chn)); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3246 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0)); |
Thierry FOURNIER | 3e3a608 | 2015-03-05 17:06:12 +0100 | [diff] [blame] | 3247 | } |
| 3248 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3249 | /* The written data will be immediately sent, so we can check |
| 3250 | * the available space without taking in account the reserve. |
| 3251 | * The reserve is guaranteed for the processing of incoming |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3252 | * data, because the buffer will be flushed. |
| 3253 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3254 | max = b_room(&chn->buf); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3255 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3256 | /* If there is no space available, and the output buffer is empty. |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3257 | * in this case, we cannot add more data, so we cannot yield, |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3258 | * we return the amount of copied data. |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3259 | */ |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3260 | if (max == 0 && co_data(chn) == 0) |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3261 | return 1; |
| 3262 | |
| 3263 | /* Adjust the real required length. */ |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3264 | if (max > len - l) |
| 3265 | max = len - l; |
| 3266 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3267 | /* The buffer available size may be not contiguous. This test |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3268 | * detects a non contiguous buffer and realign it. |
| 3269 | */ |
Willy Tarreau | 3f67999 | 2018-06-15 15:06:42 +0200 | [diff] [blame] | 3270 | if (ci_space_for_replace(chn) < max) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3271 | channel_slow_realign(chn, trash.area); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3272 | |
| 3273 | /* Copy input data in the buffer. */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3274 | max = b_rep_blk(&chn->buf, ci_head(chn), ci_head(chn), str + l, max); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3275 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3276 | /* buffer replace considers that the input part is filled. |
| 3277 | * so, I must forward these new data in the output part. |
| 3278 | */ |
Willy Tarreau | bcbd393 | 2018-06-06 07:13:22 +0200 | [diff] [blame] | 3279 | c_adv(chn, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3280 | |
| 3281 | l += max; |
| 3282 | lua_pop(L, 1); |
| 3283 | lua_pushinteger(L, l); |
| 3284 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3285 | if (l < len) { |
Christopher Faulet | 055310f | 2021-08-05 11:58:37 +0200 | [diff] [blame] | 3286 | /* If there is no space available, and the output buffer is empty. |
| 3287 | * in this case, we cannot add more data, so we cannot yield, |
| 3288 | * we return the amount of copied data. |
| 3289 | */ |
| 3290 | max = b_room(&chn->buf); |
| 3291 | if ((max == 0 && co_data(chn) == 0) || HLUA_CANT_YIELD(hlua_gethlua(L))) |
| 3292 | return 1; |
| 3293 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3294 | /* If we are waiting for space in the response buffer, we |
| 3295 | * must set the flag WAKERESWR. This flag required the task |
| 3296 | * wake up if any activity is detected on the response buffer. |
| 3297 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3298 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3299 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3300 | else |
| 3301 | HLUA_SET_WAKEREQWR(hlua); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3302 | 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] | 3303 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3304 | |
| 3305 | return 1; |
| 3306 | } |
| 3307 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3308 | /* Just a wrapper of "_hlua_channel_send". This wrapper permits |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3309 | * yield the LUA process, and resume it without checking the |
| 3310 | * input arguments. |
| 3311 | */ |
| 3312 | __LJMP static int hlua_channel_send(lua_State *L) |
| 3313 | { |
| 3314 | MAY_LJMP(check_args(L, 2, "send")); |
| 3315 | lua_pushinteger(L, 0); |
| 3316 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3317 | return MAY_LJMP(hlua_channel_send_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3318 | } |
| 3319 | |
| 3320 | /* This function forward and amount of butes. The data pass from |
| 3321 | * the input side of the buffer to the output side, and can be |
| 3322 | * forwarded. This function never fails. |
| 3323 | * |
| 3324 | * The Lua function takes an amount of bytes to be forwarded in |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3325 | * input. It returns the number of bytes forwarded. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3326 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3327 | __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] | 3328 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3329 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3330 | int len; |
| 3331 | int l; |
| 3332 | int max; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 3333 | struct hlua *hlua; |
| 3334 | |
| 3335 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 3336 | hlua = hlua_gethlua(L); |
| 3337 | if (!hlua) |
| 3338 | return 1; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3339 | |
| 3340 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3341 | |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 3342 | if (IS_HTX_STRM(chn_strm(chn))) { |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3343 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3344 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3345 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3346 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3347 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3348 | l = MAY_LJMP(luaL_checkinteger(L, -1)); |
| 3349 | |
| 3350 | max = len - l; |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3351 | if (max > ci_data(chn)) |
| 3352 | max = ci_data(chn); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3353 | channel_forward(chn, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3354 | l += max; |
| 3355 | |
| 3356 | lua_pop(L, 1); |
| 3357 | lua_pushinteger(L, l); |
| 3358 | |
| 3359 | /* Check if it miss bytes to forward. */ |
| 3360 | if (l < len) { |
| 3361 | /* The the input channel or the output channel are closed, we |
| 3362 | * must return the amount of data forwarded. |
| 3363 | */ |
Christopher Faulet | 055310f | 2021-08-05 11:58:37 +0200 | [diff] [blame] | 3364 | 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] | 3365 | return 1; |
| 3366 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3367 | /* If we are waiting for space data in the response buffer, we |
| 3368 | * must set the flag WAKERESWR. This flag required the task |
| 3369 | * wake up if any activity is detected on the response buffer. |
| 3370 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3371 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3372 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3373 | else |
| 3374 | HLUA_SET_WAKEREQWR(hlua); |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3375 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3376 | /* Otherwise, we can yield waiting for new data in the inpout side. */ |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3377 | 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] | 3378 | } |
| 3379 | |
| 3380 | return 1; |
| 3381 | } |
| 3382 | |
| 3383 | /* Just check the input and prepare the stack for the previous |
| 3384 | * function "hlua_channel_forward_yield" |
| 3385 | */ |
| 3386 | __LJMP static int hlua_channel_forward(lua_State *L) |
| 3387 | { |
| 3388 | MAY_LJMP(check_args(L, 2, "forward")); |
| 3389 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3390 | MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3391 | |
| 3392 | lua_pushinteger(L, 0); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3393 | return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3394 | } |
| 3395 | |
| 3396 | /* Just returns the number of bytes available in the input |
| 3397 | * side of the buffer. This function never fails. |
| 3398 | */ |
| 3399 | __LJMP static int hlua_channel_get_in_len(lua_State *L) |
| 3400 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3401 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3402 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3403 | MAY_LJMP(check_args(L, 1, "get_in_len")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3404 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a3ceac1 | 2018-12-14 13:39:09 +0100 | [diff] [blame] | 3405 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3406 | struct htx *htx = htxbuf(&chn->buf); |
| 3407 | lua_pushinteger(L, htx->data - co_data(chn)); |
| 3408 | } |
| 3409 | else |
| 3410 | lua_pushinteger(L, ci_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3411 | return 1; |
| 3412 | } |
| 3413 | |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3414 | /* Returns true if the channel is full. */ |
| 3415 | __LJMP static int hlua_channel_is_full(lua_State *L) |
| 3416 | { |
| 3417 | struct channel *chn; |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3418 | |
| 3419 | MAY_LJMP(check_args(L, 1, "is_full")); |
| 3420 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 0ec740e | 2020-02-26 11:59:19 +0100 | [diff] [blame] | 3421 | /* ignore the reserve, we are not on a producer side (ie in an |
| 3422 | * applet). |
| 3423 | */ |
| 3424 | lua_pushboolean(L, channel_full(chn, 0)); |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3425 | return 1; |
| 3426 | } |
| 3427 | |
Christopher Faulet | 2ac9ba2 | 2020-02-25 10:15:50 +0100 | [diff] [blame] | 3428 | /* Returns true if the channel is the response channel. */ |
| 3429 | __LJMP static int hlua_channel_is_resp(lua_State *L) |
| 3430 | { |
| 3431 | struct channel *chn; |
| 3432 | |
| 3433 | MAY_LJMP(check_args(L, 1, "is_resp")); |
| 3434 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3435 | |
| 3436 | lua_pushboolean(L, !!(chn->flags & CF_ISRESP)); |
| 3437 | return 1; |
| 3438 | } |
| 3439 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3440 | /* Just returns the number of bytes available in the output |
| 3441 | * side of the buffer. This function never fails. |
| 3442 | */ |
| 3443 | __LJMP static int hlua_channel_get_out_len(lua_State *L) |
| 3444 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3445 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3446 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3447 | MAY_LJMP(check_args(L, 1, "get_out_len")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3448 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3449 | lua_pushinteger(L, co_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3450 | return 1; |
| 3451 | } |
| 3452 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3453 | /* |
| 3454 | * |
| 3455 | * |
| 3456 | * Class Fetches |
| 3457 | * |
| 3458 | * |
| 3459 | */ |
| 3460 | |
| 3461 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3462 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3463 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3464 | __LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud) |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3465 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3466 | return MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3467 | } |
| 3468 | |
| 3469 | /* This function creates and push in the stack a fetch object according |
| 3470 | * with a current TXN. |
| 3471 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3472 | 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] | 3473 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3474 | struct hlua_smp *hsmp; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3475 | |
| 3476 | /* Check stack size. */ |
| 3477 | if (!lua_checkstack(L, 3)) |
| 3478 | return 0; |
| 3479 | |
| 3480 | /* Create the object: obj[0] = userdata. |
| 3481 | * Note that the base of the Fetches object is the |
| 3482 | * transaction object. |
| 3483 | */ |
| 3484 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3485 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3486 | lua_rawseti(L, -2, 0); |
| 3487 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3488 | hsmp->s = txn->s; |
| 3489 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 3490 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3491 | hsmp->flags = flags; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3492 | |
| 3493 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 3494 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref); |
| 3495 | lua_setmetatable(L, -2); |
| 3496 | |
| 3497 | return 1; |
| 3498 | } |
| 3499 | |
| 3500 | /* This function is an LUA binding. It is called with each sample-fetch. |
| 3501 | * It uses closure argument to store the associated sample-fetch. It |
| 3502 | * returns only one argument or throws an error. An error is thrown |
| 3503 | * only if an error is encountered during the argument parsing. If |
| 3504 | * the "sample-fetch" function fails, nil is returned. |
| 3505 | */ |
| 3506 | __LJMP static int hlua_run_sample_fetch(lua_State *L) |
| 3507 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3508 | struct hlua_smp *hsmp; |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 3509 | struct sample_fetch *f; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 3510 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3511 | int i; |
| 3512 | struct sample smp; |
| 3513 | |
| 3514 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3515 | f = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3516 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3517 | /* Get traditional arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3518 | hsmp = MAY_LJMP(hlua_checkfetches(L, 1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3519 | |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 3520 | /* Check execution authorization. */ |
| 3521 | if (f->use & SMP_USE_HTTP_ANY && |
| 3522 | !(hsmp->flags & HLUA_F_MAY_USE_HTTP)) { |
| 3523 | lua_pushfstring(L, "the sample-fetch '%s' needs an HTTP parser which " |
| 3524 | "is not available in Lua services", f->kw); |
| 3525 | WILL_LJMP(lua_error(L)); |
| 3526 | } |
| 3527 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3528 | /* Get extra arguments. */ |
| 3529 | for (i = 0; i < lua_gettop(L) - 1; i++) { |
| 3530 | if (i >= ARGM_NBARGS) |
| 3531 | break; |
| 3532 | hlua_lua2arg(L, i + 2, &args[i]); |
| 3533 | } |
| 3534 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3535 | args[i].data.str.area = NULL; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3536 | |
| 3537 | /* Check arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3538 | 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] | 3539 | |
| 3540 | /* Run the special args checker. */ |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 3541 | if (f->val_args && !f->val_args(args, NULL)) { |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3542 | lua_pushfstring(L, "error in arguments"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3543 | goto error; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3544 | } |
| 3545 | |
| 3546 | /* Initialise the sample. */ |
| 3547 | memset(&smp, 0, sizeof(smp)); |
| 3548 | |
| 3549 | /* Run the sample fetch process. */ |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 3550 | 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] | 3551 | if (!f->process(args, &smp, f->kw, f->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3552 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3553 | lua_pushstring(L, ""); |
| 3554 | else |
| 3555 | lua_pushnil(L); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3556 | goto end; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3557 | } |
| 3558 | |
| 3559 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3560 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3561 | hlua_smp2lua_str(L, &smp); |
| 3562 | else |
| 3563 | hlua_smp2lua(L, &smp); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3564 | |
| 3565 | end: |
| 3566 | for (i = 0; args[i].type != ARGT_STOP; i++) { |
| 3567 | if (args[i].type == ARGT_STR) |
| 3568 | chunk_destroy(&args[i].data.str); |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 3569 | else if (args[i].type == ARGT_REG) |
| 3570 | regex_free(args[i].data.reg); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3571 | } |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3572 | return 1; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3573 | |
| 3574 | error: |
| 3575 | for (i = 0; args[i].type != ARGT_STOP; i++) { |
| 3576 | if (args[i].type == ARGT_STR) |
| 3577 | chunk_destroy(&args[i].data.str); |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 3578 | else if (args[i].type == ARGT_REG) |
| 3579 | regex_free(args[i].data.reg); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3580 | } |
| 3581 | WILL_LJMP(lua_error(L)); |
| 3582 | return 0; /* Never reached */ |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3583 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3584 | |
| 3585 | /* |
| 3586 | * |
| 3587 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3588 | * Class Converters |
| 3589 | * |
| 3590 | * |
| 3591 | */ |
| 3592 | |
| 3593 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3594 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3595 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3596 | __LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3597 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3598 | return MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3599 | } |
| 3600 | |
| 3601 | /* This function creates and push in the stack a Converters object |
| 3602 | * according with a current TXN. |
| 3603 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3604 | 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] | 3605 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3606 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3607 | |
| 3608 | /* Check stack size. */ |
| 3609 | if (!lua_checkstack(L, 3)) |
| 3610 | return 0; |
| 3611 | |
| 3612 | /* Create the object: obj[0] = userdata. |
| 3613 | * Note that the base of the Converters object is the |
| 3614 | * same than the TXN object. |
| 3615 | */ |
| 3616 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3617 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3618 | lua_rawseti(L, -2, 0); |
| 3619 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3620 | hsmp->s = txn->s; |
| 3621 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 3622 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3623 | hsmp->flags = flags; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3624 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3625 | /* Pop a class stream metatable and affect it to the table. */ |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3626 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref); |
| 3627 | lua_setmetatable(L, -2); |
| 3628 | |
| 3629 | return 1; |
| 3630 | } |
| 3631 | |
| 3632 | /* This function is an LUA binding. It is called with each converter. |
| 3633 | * It uses closure argument to store the associated converter. It |
| 3634 | * returns only one argument or throws an error. An error is thrown |
| 3635 | * only if an error is encountered during the argument parsing. If |
| 3636 | * the converter function function fails, nil is returned. |
| 3637 | */ |
| 3638 | __LJMP static int hlua_run_sample_conv(lua_State *L) |
| 3639 | { |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3640 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3641 | struct sample_conv *conv; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 3642 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3643 | int i; |
| 3644 | struct sample smp; |
| 3645 | |
| 3646 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3647 | conv = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3648 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3649 | /* Get traditional arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3650 | hsmp = MAY_LJMP(hlua_checkconverters(L, 1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3651 | |
| 3652 | /* Get extra arguments. */ |
| 3653 | for (i = 0; i < lua_gettop(L) - 2; i++) { |
| 3654 | if (i >= ARGM_NBARGS) |
| 3655 | break; |
| 3656 | hlua_lua2arg(L, i + 3, &args[i]); |
| 3657 | } |
| 3658 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3659 | args[i].data.str.area = NULL; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3660 | |
| 3661 | /* Check arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3662 | 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] | 3663 | |
| 3664 | /* Run the special args checker. */ |
| 3665 | if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) { |
| 3666 | hlua_pusherror(L, "error in arguments"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3667 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3668 | } |
| 3669 | |
| 3670 | /* Initialise the sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 3671 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3672 | if (!hlua_lua2smp(L, 2, &smp)) { |
| 3673 | hlua_pusherror(L, "error in the input argument"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3674 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3675 | } |
| 3676 | |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 3677 | smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR); |
| 3678 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3679 | /* Apply expected cast. */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3680 | if (!sample_casts[smp.data.type][conv->in_type]) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3681 | hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'", |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3682 | smp_to_type[smp.data.type], smp_to_type[conv->in_type]); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3683 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3684 | } |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3685 | if (sample_casts[smp.data.type][conv->in_type] != c_none && |
| 3686 | !sample_casts[smp.data.type][conv->in_type](&smp)) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3687 | hlua_pusherror(L, "error during the input argument casting"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3688 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3689 | } |
| 3690 | |
| 3691 | /* Run the sample conversion process. */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 3692 | if (!conv->process(args, &smp, conv->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3693 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3694 | lua_pushstring(L, ""); |
| 3695 | else |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 3696 | lua_pushnil(L); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3697 | goto end; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3698 | } |
| 3699 | |
| 3700 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3701 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3702 | hlua_smp2lua_str(L, &smp); |
| 3703 | else |
| 3704 | hlua_smp2lua(L, &smp); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3705 | end: |
| 3706 | for (i = 0; args[i].type != ARGT_STOP; i++) { |
| 3707 | if (args[i].type == ARGT_STR) |
| 3708 | chunk_destroy(&args[i].data.str); |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 3709 | else if (args[i].type == ARGT_REG) |
| 3710 | regex_free(args[i].data.reg); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3711 | } |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 3712 | return 1; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3713 | |
| 3714 | error: |
| 3715 | for (i = 0; args[i].type != ARGT_STOP; i++) { |
| 3716 | if (args[i].type == ARGT_STR) |
| 3717 | chunk_destroy(&args[i].data.str); |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 3718 | else if (args[i].type == ARGT_REG) |
| 3719 | regex_free(args[i].data.reg); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3720 | } |
| 3721 | WILL_LJMP(lua_error(L)); |
| 3722 | return 0; /* Never reached */ |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3723 | } |
| 3724 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3725 | /* |
| 3726 | * |
| 3727 | * |
| 3728 | * Class AppletTCP |
| 3729 | * |
| 3730 | * |
| 3731 | */ |
| 3732 | |
| 3733 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 3734 | * a class stream, otherwise it throws an error. |
| 3735 | */ |
| 3736 | __LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud) |
| 3737 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3738 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3739 | } |
| 3740 | |
| 3741 | /* This function creates and push in the stack an Applet object |
| 3742 | * according with a current TXN. |
| 3743 | */ |
| 3744 | static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx) |
| 3745 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3746 | struct hlua_appctx *luactx; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3747 | struct stream_interface *si = ctx->owner; |
| 3748 | struct stream *s = si_strm(si); |
| 3749 | struct proxy *p = s->be; |
| 3750 | |
| 3751 | /* Check stack size. */ |
| 3752 | if (!lua_checkstack(L, 3)) |
| 3753 | return 0; |
| 3754 | |
| 3755 | /* Create the object: obj[0] = userdata. |
| 3756 | * Note that the base of the Converters object is the |
| 3757 | * same than the TXN object. |
| 3758 | */ |
| 3759 | lua_newtable(L); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3760 | luactx = lua_newuserdata(L, sizeof(*luactx)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3761 | lua_rawseti(L, -2, 0); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3762 | luactx->appctx = ctx; |
| 3763 | luactx->htxn.s = s; |
| 3764 | luactx->htxn.p = p; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3765 | |
| 3766 | /* Create the "f" field that contains a list of fetches. */ |
| 3767 | lua_pushstring(L, "f"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3768 | if (!hlua_fetches_new(L, &luactx->htxn, 0)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3769 | return 0; |
| 3770 | lua_settable(L, -3); |
| 3771 | |
| 3772 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 3773 | lua_pushstring(L, "sf"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3774 | if (!hlua_fetches_new(L, &luactx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3775 | return 0; |
| 3776 | lua_settable(L, -3); |
| 3777 | |
| 3778 | /* Create the "c" field that contains a list of converters. */ |
| 3779 | lua_pushstring(L, "c"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3780 | if (!hlua_converters_new(L, &luactx->htxn, 0)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3781 | return 0; |
| 3782 | lua_settable(L, -3); |
| 3783 | |
| 3784 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 3785 | lua_pushstring(L, "sc"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3786 | if (!hlua_converters_new(L, &luactx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3787 | return 0; |
| 3788 | lua_settable(L, -3); |
| 3789 | |
| 3790 | /* Pop a class stream metatable and affect it to the table. */ |
| 3791 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref); |
| 3792 | lua_setmetatable(L, -2); |
| 3793 | |
| 3794 | return 1; |
| 3795 | } |
| 3796 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3797 | __LJMP static int hlua_applet_tcp_set_var(lua_State *L) |
| 3798 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3799 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3800 | struct stream *s; |
| 3801 | const char *name; |
| 3802 | size_t len; |
| 3803 | struct sample smp; |
| 3804 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 3805 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 3806 | 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] | 3807 | |
| 3808 | /* It is useles to retrieve the stream, but this function |
| 3809 | * runs only in a stream context. |
| 3810 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3811 | luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3812 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3813 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3814 | |
| 3815 | /* Converts the third argument in a sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 3816 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3817 | hlua_lua2smp(L, 3, &smp); |
| 3818 | |
Aurelien DARRAGON | d39fb74 | 2023-05-17 16:06:11 +0200 | [diff] [blame] | 3819 | /* Store the sample in a variable. We don't need to dup the smp, vars API |
| 3820 | * already takes care of duplicating dynamic var data. |
| 3821 | */ |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3822 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 3823 | |
| 3824 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 3825 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 3826 | else |
| 3827 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 3828 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 3829 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3830 | } |
| 3831 | |
| 3832 | __LJMP static int hlua_applet_tcp_unset_var(lua_State *L) |
| 3833 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3834 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3835 | struct stream *s; |
| 3836 | const char *name; |
| 3837 | size_t len; |
| 3838 | struct sample smp; |
| 3839 | |
| 3840 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 3841 | |
| 3842 | /* It is useles to retrieve the stream, but this function |
| 3843 | * runs only in a stream context. |
| 3844 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3845 | luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3846 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3847 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3848 | |
| 3849 | /* Unset the variable. */ |
| 3850 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 3851 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 3852 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3853 | } |
| 3854 | |
| 3855 | __LJMP static int hlua_applet_tcp_get_var(lua_State *L) |
| 3856 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3857 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3858 | struct stream *s; |
| 3859 | const char *name; |
| 3860 | size_t len; |
| 3861 | struct sample smp; |
| 3862 | |
| 3863 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 3864 | |
| 3865 | /* It is useles to retrieve the stream, but this function |
| 3866 | * runs only in a stream context. |
| 3867 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3868 | luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3869 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3870 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3871 | |
| 3872 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 3873 | if (!vars_get_by_name(name, len, &smp)) { |
| 3874 | lua_pushnil(L); |
| 3875 | return 1; |
| 3876 | } |
| 3877 | |
| 3878 | return hlua_smp2lua(L, &smp); |
| 3879 | } |
| 3880 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3881 | __LJMP static int hlua_applet_tcp_set_priv(lua_State *L) |
| 3882 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3883 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3884 | struct stream *s = luactx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 3885 | struct hlua *hlua; |
| 3886 | |
| 3887 | /* 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] | 3888 | if (!s->hlua) |
| 3889 | return 0; |
| 3890 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3891 | |
| 3892 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 3893 | |
| 3894 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 3895 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3896 | |
| 3897 | /* Get and store new value. */ |
| 3898 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 3899 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 3900 | |
| 3901 | return 0; |
| 3902 | } |
| 3903 | |
| 3904 | __LJMP static int hlua_applet_tcp_get_priv(lua_State *L) |
| 3905 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3906 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3907 | struct stream *s = luactx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 3908 | struct hlua *hlua; |
| 3909 | |
| 3910 | /* 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] | 3911 | if (!s->hlua) { |
| 3912 | lua_pushnil(L); |
| 3913 | return 1; |
| 3914 | } |
| 3915 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3916 | |
| 3917 | /* Push configuration index in the stack. */ |
| 3918 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 3919 | |
| 3920 | return 1; |
| 3921 | } |
| 3922 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3923 | /* If expected data not yet available, it returns a yield. This function |
| 3924 | * consumes the data in the buffer. It returns a string containing the |
| 3925 | * data. This string can be empty. |
| 3926 | */ |
| 3927 | __LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx) |
| 3928 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3929 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3930 | struct stream_interface *si = luactx->appctx->owner; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3931 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3932 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3933 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3934 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3935 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3936 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3937 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3938 | ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3939 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3940 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3941 | if (ret == 0) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3942 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3943 | 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] | 3944 | } |
| 3945 | |
| 3946 | /* End of data: commit the total strings and return. */ |
| 3947 | if (ret < 0) { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3948 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3949 | return 1; |
| 3950 | } |
| 3951 | |
| 3952 | /* Ensure that the block 2 length is usable. */ |
| 3953 | if (ret == 1) |
| 3954 | len2 = 0; |
| 3955 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 3956 | /* don't check the max length read and don't check. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3957 | luaL_addlstring(&luactx->b, blk1, len1); |
| 3958 | luaL_addlstring(&luactx->b, blk2, len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3959 | |
| 3960 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3961 | co_skip(si_oc(si), len1 + len2); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3962 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3963 | return 1; |
| 3964 | } |
| 3965 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3966 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3967 | __LJMP static int hlua_applet_tcp_getline(lua_State *L) |
| 3968 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3969 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3970 | |
| 3971 | /* Initialise the string catenation. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3972 | luaL_buffinit(L, &luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3973 | |
| 3974 | return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0)); |
| 3975 | } |
| 3976 | |
| 3977 | /* If expected data not yet available, it returns a yield. This function |
| 3978 | * consumes the data in the buffer. It returns a string containing the |
| 3979 | * data. This string can be empty. |
| 3980 | */ |
| 3981 | __LJMP static int hlua_applet_tcp_recv_yield(lua_State *L, int status, lua_KContext ctx) |
| 3982 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3983 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3984 | struct stream_interface *si = luactx->appctx->owner; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3985 | size_t len = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3986 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3987 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3988 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3989 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3990 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3991 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3992 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3993 | ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3994 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3995 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3996 | if (ret == 0) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3997 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3998 | 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] | 3999 | } |
| 4000 | |
| 4001 | /* End of data: commit the total strings and return. */ |
| 4002 | if (ret < 0) { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4003 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4004 | return 1; |
| 4005 | } |
| 4006 | |
| 4007 | /* Ensure that the block 2 length is usable. */ |
| 4008 | if (ret == 1) |
| 4009 | len2 = 0; |
| 4010 | |
| 4011 | if (len == -1) { |
| 4012 | |
| 4013 | /* If len == -1, catenate all the data avalaile and |
| 4014 | * yield because we want to get all the data until |
| 4015 | * the end of data stream. |
| 4016 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4017 | luaL_addlstring(&luactx->b, blk1, len1); |
| 4018 | luaL_addlstring(&luactx->b, blk2, len2); |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4019 | co_skip(si_oc(si), len1 + len2); |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 4020 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4021 | 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] | 4022 | |
| 4023 | } else { |
| 4024 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4025 | /* Copy the first block caping to the length required. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4026 | if (len1 > len) |
| 4027 | len1 = len; |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4028 | luaL_addlstring(&luactx->b, blk1, len1); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4029 | len -= len1; |
| 4030 | |
| 4031 | /* Copy the second block. */ |
| 4032 | if (len2 > len) |
| 4033 | len2 = len; |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4034 | luaL_addlstring(&luactx->b, blk2, len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4035 | len -= len2; |
| 4036 | |
| 4037 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4038 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4039 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4040 | /* If there is no other data available, yield waiting for new data. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4041 | if (len > 0) { |
| 4042 | lua_pushinteger(L, len); |
| 4043 | lua_replace(L, 2); |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 4044 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4045 | 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] | 4046 | } |
| 4047 | |
| 4048 | /* return the result. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4049 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4050 | return 1; |
| 4051 | } |
| 4052 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4053 | /* we never execute this */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4054 | hlua_pusherror(L, "Lua: internal error"); |
| 4055 | WILL_LJMP(lua_error(L)); |
| 4056 | return 0; |
| 4057 | } |
| 4058 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4059 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4060 | __LJMP static int hlua_applet_tcp_recv(lua_State *L) |
| 4061 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4062 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4063 | int len = -1; |
| 4064 | |
| 4065 | if (lua_gettop(L) > 2) |
| 4066 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 4067 | if (lua_gettop(L) >= 2) { |
| 4068 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4069 | lua_pop(L, 1); |
| 4070 | } |
| 4071 | |
| 4072 | /* Confirm or set the required length */ |
| 4073 | lua_pushinteger(L, len); |
| 4074 | |
| 4075 | /* Initialise the string catenation. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4076 | luaL_buffinit(L, &luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4077 | |
| 4078 | return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0)); |
| 4079 | } |
| 4080 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4081 | /* Append data in the output side of the buffer. This data is immediately |
| 4082 | * sent. The function returns the amount of data written. If the buffer |
| 4083 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4084 | * if the channel is closed. |
| 4085 | */ |
| 4086 | __LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx) |
| 4087 | { |
| 4088 | size_t len; |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4089 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4090 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4091 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4092 | struct stream_interface *si = luactx->appctx->owner; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4093 | struct channel *chn = si_ic(si); |
| 4094 | int max; |
| 4095 | |
| 4096 | /* Get the max amount of data which can write as input in the channel. */ |
| 4097 | max = channel_recv_max(chn); |
| 4098 | if (max > (len - l)) |
| 4099 | max = len - l; |
| 4100 | |
| 4101 | /* Copy data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4102 | ci_putblk(chn, str + l, max); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4103 | |
| 4104 | /* update counters. */ |
| 4105 | l += max; |
| 4106 | lua_pop(L, 1); |
| 4107 | lua_pushinteger(L, l); |
| 4108 | |
| 4109 | /* If some data is not send, declares the situation to the |
| 4110 | * applet, and returns a yield. |
| 4111 | */ |
| 4112 | if (l < len) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 4113 | si_rx_room_blk(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4114 | 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] | 4115 | } |
| 4116 | |
| 4117 | return 1; |
| 4118 | } |
| 4119 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4120 | /* Just a wrapper of "hlua_applet_tcp_send_yield". This wrapper permits |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4121 | * yield the LUA process, and resume it without checking the |
| 4122 | * input arguments. |
| 4123 | */ |
| 4124 | __LJMP static int hlua_applet_tcp_send(lua_State *L) |
| 4125 | { |
| 4126 | MAY_LJMP(check_args(L, 2, "send")); |
| 4127 | lua_pushinteger(L, 0); |
| 4128 | |
| 4129 | return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0)); |
| 4130 | } |
| 4131 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4132 | /* |
| 4133 | * |
| 4134 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4135 | * Class AppletHTTP |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4136 | * |
| 4137 | * |
| 4138 | */ |
| 4139 | |
| 4140 | /* Returns a struct hlua_txn if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4141 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4142 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4143 | __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] | 4144 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4145 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_http_ref)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4146 | } |
| 4147 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4148 | /* This function creates and push in the stack an Applet object |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4149 | * according with a current TXN. |
| 4150 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4151 | static int hlua_applet_http_new(lua_State *L, struct appctx *ctx) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4152 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4153 | struct hlua_appctx *luactx; |
Thierry FOURNIER | 841475e | 2015-12-11 17:10:09 +0100 | [diff] [blame] | 4154 | struct hlua_txn htxn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4155 | struct stream_interface *si = ctx->owner; |
| 4156 | struct stream *s = si_strm(si); |
| 4157 | struct proxy *px = s->be; |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4158 | struct htx *htx; |
| 4159 | struct htx_blk *blk; |
| 4160 | struct htx_sl *sl; |
| 4161 | struct ist path; |
| 4162 | unsigned long long len = 0; |
| 4163 | int32_t pos; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4164 | |
| 4165 | /* Check stack size. */ |
| 4166 | if (!lua_checkstack(L, 3)) |
| 4167 | return 0; |
| 4168 | |
| 4169 | /* Create the object: obj[0] = userdata. |
| 4170 | * Note that the base of the Converters object is the |
| 4171 | * same than the TXN object. |
| 4172 | */ |
| 4173 | lua_newtable(L); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4174 | luactx = lua_newuserdata(L, sizeof(*luactx)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4175 | lua_rawseti(L, -2, 0); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4176 | luactx->appctx = ctx; |
| 4177 | luactx->appctx->ctx.hlua_apphttp.status = 200; /* Default status code returned. */ |
| 4178 | luactx->appctx->ctx.hlua_apphttp.reason = NULL; /* Use default reason based on status */ |
| 4179 | luactx->htxn.s = s; |
| 4180 | luactx->htxn.p = px; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4181 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4182 | /* Create the "f" field that contains a list of fetches. */ |
| 4183 | lua_pushstring(L, "f"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4184 | if (!hlua_fetches_new(L, &luactx->htxn, 0)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4185 | return 0; |
| 4186 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4187 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4188 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 4189 | lua_pushstring(L, "sf"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4190 | if (!hlua_fetches_new(L, &luactx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4191 | return 0; |
| 4192 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4193 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4194 | /* Create the "c" field that contains a list of converters. */ |
| 4195 | lua_pushstring(L, "c"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4196 | if (!hlua_converters_new(L, &luactx->htxn, 0)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4197 | return 0; |
| 4198 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4199 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4200 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 4201 | lua_pushstring(L, "sc"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4202 | if (!hlua_converters_new(L, &luactx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4203 | return 0; |
| 4204 | lua_settable(L, -3); |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4205 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4206 | htx = htxbuf(&s->req.buf); |
| 4207 | blk = htx_get_first_blk(htx); |
Christopher Faulet | ea00973 | 2019-11-18 15:50:25 +0100 | [diff] [blame] | 4208 | BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4209 | sl = htx_get_blk_ptr(htx, blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4210 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4211 | /* Stores the request method. */ |
| 4212 | lua_pushstring(L, "method"); |
| 4213 | lua_pushlstring(L, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); |
| 4214 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4215 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4216 | /* Stores the http version. */ |
| 4217 | lua_pushstring(L, "version"); |
| 4218 | lua_pushlstring(L, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); |
| 4219 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4220 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4221 | /* creates an array of headers. hlua_http_get_headers() crates and push |
| 4222 | * the array on the top of the stack. |
| 4223 | */ |
| 4224 | lua_pushstring(L, "headers"); |
| 4225 | htxn.s = s; |
| 4226 | htxn.p = px; |
| 4227 | htxn.dir = SMP_OPT_DIR_REQ; |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 4228 | if (!hlua_http_get_headers(L, &htxn.s->txn->req)) |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4229 | return 0; |
| 4230 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4231 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4232 | path = http_get_path(htx_sl_req_uri(sl)); |
Tim Duesterhus | ed52637 | 2020-03-05 17:56:33 +0100 | [diff] [blame] | 4233 | if (isttest(path)) { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4234 | char *p, *q, *end; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4235 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4236 | p = path.ptr; |
| 4237 | end = path.ptr + path.len; |
| 4238 | q = p; |
| 4239 | while (q < end && *q != '?') |
| 4240 | q++; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4241 | |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 4242 | /* Stores the request path. */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4243 | lua_pushstring(L, "path"); |
| 4244 | lua_pushlstring(L, p, q - p); |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 4245 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4246 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4247 | /* Stores the query string. */ |
| 4248 | lua_pushstring(L, "qs"); |
| 4249 | if (*q == '?') |
| 4250 | q++; |
| 4251 | lua_pushlstring(L, q, end - q); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4252 | lua_settable(L, -3); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4253 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4254 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4255 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 4256 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 4257 | enum htx_blk_type type = htx_get_blk_type(blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4258 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 4259 | if (type == HTX_BLK_TLR || type == HTX_BLK_EOT) |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4260 | break; |
| 4261 | if (type == HTX_BLK_DATA) |
| 4262 | len += htx_get_blksz(blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4263 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4264 | if (htx->extra != ULLONG_MAX) |
| 4265 | len += htx->extra; |
| 4266 | |
| 4267 | /* Stores the request path. */ |
| 4268 | lua_pushstring(L, "length"); |
| 4269 | lua_pushinteger(L, len); |
| 4270 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4271 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4272 | /* Create an empty array of HTTP request headers. */ |
| 4273 | lua_pushstring(L, "response"); |
| 4274 | lua_newtable(L); |
| 4275 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4276 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4277 | /* Pop a class stream metatable and affect it to the table. */ |
| 4278 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref); |
| 4279 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4280 | |
| 4281 | return 1; |
| 4282 | } |
| 4283 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4284 | __LJMP static int hlua_applet_http_set_var(lua_State *L) |
| 4285 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4286 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4287 | struct stream *s; |
| 4288 | const char *name; |
| 4289 | size_t len; |
| 4290 | struct sample smp; |
| 4291 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 4292 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 4293 | 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] | 4294 | |
| 4295 | /* It is useles to retrieve the stream, but this function |
| 4296 | * runs only in a stream context. |
| 4297 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4298 | luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4299 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4300 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4301 | |
| 4302 | /* Converts the third argument in a sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 4303 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4304 | hlua_lua2smp(L, 3, &smp); |
| 4305 | |
Aurelien DARRAGON | d39fb74 | 2023-05-17 16:06:11 +0200 | [diff] [blame] | 4306 | /* Store the sample in a variable. We don't need to dup the smp, vars API |
| 4307 | * already takes care of duplicating dynamic var data. |
| 4308 | */ |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4309 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 4310 | |
| 4311 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 4312 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 4313 | else |
| 4314 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 4315 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 4316 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4317 | } |
| 4318 | |
| 4319 | __LJMP static int hlua_applet_http_unset_var(lua_State *L) |
| 4320 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4321 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4322 | struct stream *s; |
| 4323 | const char *name; |
| 4324 | size_t len; |
| 4325 | struct sample smp; |
| 4326 | |
| 4327 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 4328 | |
| 4329 | /* It is useles to retrieve the stream, but this function |
| 4330 | * runs only in a stream context. |
| 4331 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4332 | luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4333 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4334 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4335 | |
| 4336 | /* Unset the variable. */ |
| 4337 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 4338 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 4339 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4340 | } |
| 4341 | |
| 4342 | __LJMP static int hlua_applet_http_get_var(lua_State *L) |
| 4343 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4344 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4345 | struct stream *s; |
| 4346 | const char *name; |
| 4347 | size_t len; |
| 4348 | struct sample smp; |
| 4349 | |
| 4350 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 4351 | |
| 4352 | /* It is useles to retrieve the stream, but this function |
| 4353 | * runs only in a stream context. |
| 4354 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4355 | luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4356 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4357 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4358 | |
| 4359 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 4360 | if (!vars_get_by_name(name, len, &smp)) { |
| 4361 | lua_pushnil(L); |
| 4362 | return 1; |
| 4363 | } |
| 4364 | |
| 4365 | return hlua_smp2lua(L, &smp); |
| 4366 | } |
| 4367 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4368 | __LJMP static int hlua_applet_http_set_priv(lua_State *L) |
| 4369 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4370 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4371 | struct stream *s = luactx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4372 | struct hlua *hlua; |
| 4373 | |
| 4374 | /* 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] | 4375 | if (!s->hlua) |
| 4376 | return 0; |
| 4377 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4378 | |
| 4379 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 4380 | |
| 4381 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 4382 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4383 | |
| 4384 | /* Get and store new value. */ |
| 4385 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 4386 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 4387 | |
| 4388 | return 0; |
| 4389 | } |
| 4390 | |
| 4391 | __LJMP static int hlua_applet_http_get_priv(lua_State *L) |
| 4392 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4393 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4394 | struct stream *s = luactx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4395 | struct hlua *hlua; |
| 4396 | |
| 4397 | /* 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] | 4398 | if (!s->hlua) { |
| 4399 | lua_pushnil(L); |
| 4400 | return 1; |
| 4401 | } |
| 4402 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4403 | |
| 4404 | /* Push configuration index in the stack. */ |
| 4405 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 4406 | |
| 4407 | return 1; |
| 4408 | } |
| 4409 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4410 | /* If expected data not yet available, it returns a yield. This function |
| 4411 | * consumes the data in the buffer. It returns a string containing the |
| 4412 | * data. This string can be empty. |
| 4413 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4414 | __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] | 4415 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4416 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4417 | struct stream_interface *si = luactx->appctx->owner; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4418 | struct channel *req = si_oc(si); |
| 4419 | struct htx *htx; |
| 4420 | struct htx_blk *blk; |
| 4421 | size_t count; |
| 4422 | int stop = 0; |
| 4423 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4424 | htx = htx_from_buf(&req->buf); |
| 4425 | count = co_data(req); |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 4426 | blk = htx_get_first_blk(htx); |
Christopher Faulet | cc26b13 | 2018-12-18 21:20:57 +0100 | [diff] [blame] | 4427 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4428 | while (count && !stop && blk) { |
| 4429 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4430 | uint32_t sz = htx_get_blksz(blk); |
| 4431 | struct ist v; |
| 4432 | uint32_t vlen; |
| 4433 | char *nl; |
| 4434 | |
| 4435 | vlen = sz; |
| 4436 | if (vlen > count) { |
| 4437 | if (type != HTX_BLK_DATA) |
| 4438 | break; |
| 4439 | vlen = count; |
| 4440 | } |
| 4441 | |
| 4442 | switch (type) { |
| 4443 | case HTX_BLK_UNUSED: |
| 4444 | break; |
| 4445 | |
| 4446 | case HTX_BLK_DATA: |
| 4447 | v = htx_get_blk_value(htx, blk); |
| 4448 | v.len = vlen; |
| 4449 | nl = istchr(v, '\n'); |
| 4450 | if (nl != NULL) { |
| 4451 | stop = 1; |
| 4452 | vlen = nl - v.ptr + 1; |
| 4453 | } |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4454 | luaL_addlstring(&luactx->b, v.ptr, vlen); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4455 | break; |
| 4456 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4457 | case HTX_BLK_TLR: |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 4458 | case HTX_BLK_EOT: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4459 | stop = 1; |
| 4460 | break; |
| 4461 | |
| 4462 | default: |
| 4463 | break; |
| 4464 | } |
| 4465 | |
| 4466 | co_set_data(req, co_data(req) - vlen); |
| 4467 | count -= vlen; |
| 4468 | if (sz == vlen) |
| 4469 | blk = htx_remove_blk(htx, blk); |
| 4470 | else { |
| 4471 | htx_cut_data_blk(htx, blk, vlen); |
| 4472 | break; |
| 4473 | } |
| 4474 | } |
| 4475 | |
Christopher Faulet | eccb31c | 2021-04-02 14:24:56 +0200 | [diff] [blame] | 4476 | /* The message was fully consumed and no more data are expected |
| 4477 | * (EOM flag set). |
| 4478 | */ |
Christopher Faulet | a5a25b1 | 2022-08-29 15:37:16 +0200 | [diff] [blame] | 4479 | if (htx_is_empty(htx) && (req->flags & CF_EOI)) |
Christopher Faulet | eccb31c | 2021-04-02 14:24:56 +0200 | [diff] [blame] | 4480 | stop = 1; |
| 4481 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4482 | htx_to_buf(htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4483 | if (!stop) { |
| 4484 | si_cant_get(si); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4485 | 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] | 4486 | } |
| 4487 | |
| 4488 | /* return the result. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4489 | luaL_pushresult(&luactx->b); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4490 | return 1; |
| 4491 | } |
| 4492 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4493 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4494 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4495 | __LJMP static int hlua_applet_http_getline(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4496 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4497 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4498 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4499 | /* Initialise the string catenation. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4500 | luaL_buffinit(L, &luactx->b); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4501 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4502 | return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4503 | } |
| 4504 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4505 | /* If expected data not yet available, it returns a yield. This function |
| 4506 | * consumes the data in the buffer. It returns a string containing the |
| 4507 | * data. This string can be empty. |
| 4508 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4509 | __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] | 4510 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4511 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4512 | struct stream_interface *si = luactx->appctx->owner; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4513 | struct channel *req = si_oc(si); |
| 4514 | struct htx *htx; |
| 4515 | struct htx_blk *blk; |
| 4516 | size_t count; |
| 4517 | int len; |
| 4518 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4519 | htx = htx_from_buf(&req->buf); |
| 4520 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4521 | count = co_data(req); |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 4522 | blk = htx_get_head_blk(htx); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4523 | while (count && len && blk) { |
| 4524 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4525 | uint32_t sz = htx_get_blksz(blk); |
| 4526 | struct ist v; |
| 4527 | uint32_t vlen; |
| 4528 | |
| 4529 | vlen = sz; |
| 4530 | if (len > 0 && vlen > len) |
| 4531 | vlen = len; |
| 4532 | if (vlen > count) { |
| 4533 | if (type != HTX_BLK_DATA) |
| 4534 | break; |
| 4535 | vlen = count; |
| 4536 | } |
| 4537 | |
| 4538 | switch (type) { |
| 4539 | case HTX_BLK_UNUSED: |
| 4540 | break; |
| 4541 | |
| 4542 | case HTX_BLK_DATA: |
| 4543 | v = htx_get_blk_value(htx, blk); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4544 | luaL_addlstring(&luactx->b, v.ptr, vlen); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4545 | break; |
| 4546 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4547 | case HTX_BLK_TLR: |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 4548 | case HTX_BLK_EOT: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4549 | len = 0; |
| 4550 | break; |
| 4551 | |
| 4552 | default: |
| 4553 | break; |
| 4554 | } |
| 4555 | |
| 4556 | co_set_data(req, co_data(req) - vlen); |
| 4557 | count -= vlen; |
| 4558 | if (len > 0) |
| 4559 | len -= vlen; |
| 4560 | if (sz == vlen) |
| 4561 | blk = htx_remove_blk(htx, blk); |
| 4562 | else { |
| 4563 | htx_cut_data_blk(htx, blk, vlen); |
| 4564 | break; |
| 4565 | } |
| 4566 | } |
| 4567 | |
Christopher Faulet | eccb31c | 2021-04-02 14:24:56 +0200 | [diff] [blame] | 4568 | /* The message was fully consumed and no more data are expected |
| 4569 | * (EOM flag set). |
| 4570 | */ |
Christopher Faulet | a5a25b1 | 2022-08-29 15:37:16 +0200 | [diff] [blame] | 4571 | if (htx_is_empty(htx) && (req->flags & CF_EOI)) |
Christopher Faulet | eccb31c | 2021-04-02 14:24:56 +0200 | [diff] [blame] | 4572 | len = 0; |
| 4573 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4574 | htx_to_buf(htx, &req->buf); |
| 4575 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4576 | /* If we are no other data available, yield waiting for new data. */ |
| 4577 | if (len) { |
| 4578 | if (len > 0) { |
| 4579 | lua_pushinteger(L, len); |
| 4580 | lua_replace(L, 2); |
| 4581 | } |
| 4582 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4583 | 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] | 4584 | } |
| 4585 | |
| 4586 | /* return the result. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4587 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4588 | return 1; |
| 4589 | } |
| 4590 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4591 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4592 | __LJMP static int hlua_applet_http_recv(lua_State *L) |
| 4593 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4594 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4595 | int len = -1; |
| 4596 | |
| 4597 | /* Check arguments. */ |
| 4598 | if (lua_gettop(L) > 2) |
| 4599 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 4600 | if (lua_gettop(L) >= 2) { |
| 4601 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4602 | lua_pop(L, 1); |
| 4603 | } |
| 4604 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4605 | lua_pushinteger(L, len); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4606 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4607 | /* Initialise the string catenation. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4608 | luaL_buffinit(L, &luactx->b); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4609 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4610 | return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4611 | } |
| 4612 | |
| 4613 | /* Append data in the output side of the buffer. This data is immediately |
| 4614 | * sent. The function returns the amount of data written. If the buffer |
| 4615 | * cannot contain the data, the function yields. The function returns -1 |
| 4616 | * if the channel is closed. |
| 4617 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4618 | __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] | 4619 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4620 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4621 | struct stream_interface *si = luactx->appctx->owner; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4622 | struct channel *res = si_ic(si); |
| 4623 | struct htx *htx = htx_from_buf(&res->buf); |
| 4624 | const char *data; |
| 4625 | size_t len; |
| 4626 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 4627 | int max; |
| 4628 | |
Christopher Faulet | 9060fc0 | 2019-07-03 11:39:30 +0200 | [diff] [blame] | 4629 | max = htx_get_max_blksz(htx, channel_htx_recv_max(res, htx)); |
Christopher Faulet | 4b0e9b2 | 2019-01-09 12:16:58 +0100 | [diff] [blame] | 4630 | if (!max) |
| 4631 | goto snd_yield; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4632 | |
| 4633 | data = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4634 | |
| 4635 | /* Get the max amount of data which can write as input in the channel. */ |
| 4636 | if (max > (len - l)) |
| 4637 | max = len - l; |
| 4638 | |
| 4639 | /* Copy data. */ |
Willy Tarreau | 0a7ef02 | 2019-05-28 10:30:11 +0200 | [diff] [blame] | 4640 | max = htx_add_data(htx, ist2(data + l, max)); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 4641 | channel_add_input(res, max); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4642 | |
| 4643 | /* update counters. */ |
| 4644 | l += max; |
| 4645 | lua_pop(L, 1); |
| 4646 | lua_pushinteger(L, l); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4647 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4648 | /* If some data is not send, declares the situation to the |
| 4649 | * applet, and returns a yield. |
| 4650 | */ |
| 4651 | if (l < len) { |
Christopher Faulet | 4b0e9b2 | 2019-01-09 12:16:58 +0100 | [diff] [blame] | 4652 | snd_yield: |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4653 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4654 | si_rx_room_blk(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4655 | 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] | 4656 | } |
| 4657 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4658 | htx_to_buf(htx, &res->buf); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4659 | return 1; |
| 4660 | } |
| 4661 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4662 | /* Just a wrapper of "hlua_applet_send_yield". This wrapper permits |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4663 | * yield the LUA process, and resume it without checking the |
| 4664 | * input arguments. |
| 4665 | */ |
| 4666 | __LJMP static int hlua_applet_http_send(lua_State *L) |
| 4667 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4668 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4669 | |
| 4670 | /* We want to send some data. Headers must be sent. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4671 | if (!(luactx->appctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4672 | hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data."); |
| 4673 | WILL_LJMP(lua_error(L)); |
| 4674 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4675 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4676 | /* This integer is used for followinf the amount of data sent. */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4677 | lua_pushinteger(L, 0); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4678 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4679 | return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4680 | } |
| 4681 | |
| 4682 | __LJMP static int hlua_applet_http_addheader(lua_State *L) |
| 4683 | { |
| 4684 | const char *name; |
| 4685 | int ret; |
| 4686 | |
| 4687 | MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4688 | name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 4689 | MAY_LJMP(luaL_checkstring(L, 3)); |
| 4690 | |
| 4691 | /* Push in the stack the "response" entry. */ |
| 4692 | ret = lua_getfield(L, 1, "response"); |
| 4693 | if (ret != LUA_TTABLE) { |
| 4694 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] " |
| 4695 | "is expected as an array. %s found", lua_typename(L, ret)); |
| 4696 | WILL_LJMP(lua_error(L)); |
| 4697 | } |
| 4698 | |
| 4699 | /* check if the header is already registered if it is not |
| 4700 | * the case, register it. |
| 4701 | */ |
| 4702 | ret = lua_getfield(L, -1, name); |
| 4703 | if (ret == LUA_TNIL) { |
| 4704 | |
| 4705 | /* Entry not found. */ |
| 4706 | lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */ |
| 4707 | |
| 4708 | /* Insert the new header name in the array in the top of the stack. |
| 4709 | * It left the new array in the top of the stack. |
| 4710 | */ |
| 4711 | lua_newtable(L); |
| 4712 | lua_pushvalue(L, 2); |
| 4713 | lua_pushvalue(L, -2); |
| 4714 | lua_settable(L, -4); |
| 4715 | |
| 4716 | } else if (ret != LUA_TTABLE) { |
| 4717 | |
| 4718 | /* corruption error. */ |
| 4719 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] " |
| 4720 | "is expected as an array. %s found", name, lua_typename(L, ret)); |
| 4721 | WILL_LJMP(lua_error(L)); |
| 4722 | } |
| 4723 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 4724 | /* Now the top of thestack is an array of values. We push |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4725 | * the header value as new entry. |
| 4726 | */ |
| 4727 | lua_pushvalue(L, 3); |
| 4728 | ret = lua_rawlen(L, -2); |
| 4729 | lua_rawseti(L, -2, ret + 1); |
| 4730 | lua_pushboolean(L, 1); |
| 4731 | return 1; |
| 4732 | } |
| 4733 | |
| 4734 | __LJMP static int hlua_applet_http_status(lua_State *L) |
| 4735 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4736 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4737 | int status = MAY_LJMP(luaL_checkinteger(L, 2)); |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4738 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4739 | |
| 4740 | if (status < 100 || status > 599) { |
| 4741 | lua_pushboolean(L, 0); |
| 4742 | return 1; |
| 4743 | } |
| 4744 | |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4745 | luactx->appctx->ctx.hlua_apphttp.status = status; |
| 4746 | luactx->appctx->ctx.hlua_apphttp.reason = reason; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4747 | lua_pushboolean(L, 1); |
| 4748 | return 1; |
| 4749 | } |
| 4750 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4751 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4752 | __LJMP static int hlua_applet_http_send_response(lua_State *L) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4753 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4754 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4755 | struct stream_interface *si = luactx->appctx->owner; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4756 | struct channel *res = si_ic(si); |
| 4757 | struct htx *htx; |
| 4758 | struct htx_sl *sl; |
| 4759 | struct h1m h1m; |
| 4760 | const char *status, *reason; |
| 4761 | const char *name, *value; |
| 4762 | size_t nlen, vlen; |
| 4763 | unsigned int flags; |
| 4764 | |
| 4765 | /* Send the message at once. */ |
| 4766 | htx = htx_from_buf(&res->buf); |
| 4767 | h1m_init_res(&h1m); |
| 4768 | |
| 4769 | /* Use the same http version than the request. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4770 | status = ultoa_r(luactx->appctx->ctx.hlua_apphttp.status, trash.area, trash.size); |
| 4771 | reason = luactx->appctx->ctx.hlua_apphttp.reason; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4772 | if (reason == NULL) |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4773 | reason = http_get_reason(luactx->appctx->ctx.hlua_apphttp.status); |
| 4774 | if (luactx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4775 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11); |
| 4776 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist(status), ist(reason)); |
| 4777 | } |
| 4778 | else { |
| 4779 | flags = HTX_SL_F_IS_RESP; |
| 4780 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), ist(status), ist(reason)); |
| 4781 | } |
| 4782 | if (!sl) { |
| 4783 | hlua_pusherror(L, "Lua applet http '%s': Failed to create response.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4784 | luactx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4785 | WILL_LJMP(lua_error(L)); |
| 4786 | } |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4787 | sl->info.res.status = luactx->appctx->ctx.hlua_apphttp.status; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4788 | |
| 4789 | /* Get the array associated to the field "response" in the object AppletHTTP. */ |
| 4790 | lua_pushvalue(L, 0); |
| 4791 | if (lua_getfield(L, 1, "response") != LUA_TTABLE) { |
| 4792 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4793 | luactx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4794 | WILL_LJMP(lua_error(L)); |
| 4795 | } |
| 4796 | |
| 4797 | /* Browse the list of headers. */ |
| 4798 | lua_pushnil(L); |
| 4799 | while(lua_next(L, -2) != 0) { |
| 4800 | /* We expect a string as -2. */ |
| 4801 | if (lua_type(L, -2) != LUA_TSTRING) { |
| 4802 | 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] | 4803 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4804 | lua_typename(L, lua_type(L, -2))); |
| 4805 | WILL_LJMP(lua_error(L)); |
| 4806 | } |
| 4807 | name = lua_tolstring(L, -2, &nlen); |
| 4808 | |
| 4809 | /* We expect an array as -1. */ |
| 4810 | if (lua_type(L, -1) != LUA_TTABLE) { |
| 4811 | 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] | 4812 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4813 | name, |
| 4814 | lua_typename(L, lua_type(L, -1))); |
| 4815 | WILL_LJMP(lua_error(L)); |
| 4816 | } |
| 4817 | |
| 4818 | /* Browse the table who is on the top of the stack. */ |
| 4819 | lua_pushnil(L); |
| 4820 | while(lua_next(L, -2) != 0) { |
| 4821 | int id; |
| 4822 | |
| 4823 | /* We expect a number as -2. */ |
| 4824 | if (lua_type(L, -2) != LUA_TNUMBER) { |
| 4825 | 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] | 4826 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4827 | name, |
| 4828 | lua_typename(L, lua_type(L, -2))); |
| 4829 | WILL_LJMP(lua_error(L)); |
| 4830 | } |
| 4831 | id = lua_tointeger(L, -2); |
| 4832 | |
| 4833 | /* We expect a string as -2. */ |
| 4834 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 4835 | 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] | 4836 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4837 | name, id, |
| 4838 | lua_typename(L, lua_type(L, -1))); |
| 4839 | WILL_LJMP(lua_error(L)); |
| 4840 | } |
| 4841 | value = lua_tolstring(L, -1, &vlen); |
| 4842 | |
| 4843 | /* Simple Protocol checks. */ |
| 4844 | if (isteqi(ist2(name, nlen), ist("transfer-encoding"))) |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 4845 | h1_parse_xfer_enc_header(&h1m, ist2(value, vlen)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4846 | else if (isteqi(ist2(name, nlen), ist("content-length"))) { |
| 4847 | struct ist v = ist2(value, vlen); |
| 4848 | int ret; |
| 4849 | |
| 4850 | ret = h1_parse_cont_len_header(&h1m, &v); |
| 4851 | if (ret < 0) { |
| 4852 | hlua_pusherror(L, "Lua applet http '%s': Invalid '%s' header.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4853 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4854 | name); |
| 4855 | WILL_LJMP(lua_error(L)); |
| 4856 | } |
| 4857 | else if (ret == 0) |
| 4858 | goto next; /* Skip it */ |
| 4859 | } |
| 4860 | |
| 4861 | /* Add a new header */ |
| 4862 | if (!htx_add_header(htx, ist2(name, nlen), ist2(value, vlen))) { |
| 4863 | 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] | 4864 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4865 | name); |
| 4866 | WILL_LJMP(lua_error(L)); |
| 4867 | } |
| 4868 | next: |
| 4869 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 4870 | lua_pop(L, 1); |
| 4871 | } |
| 4872 | |
| 4873 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 4874 | lua_pop(L, 1); |
| 4875 | } |
| 4876 | |
| 4877 | if (h1m.flags & H1_MF_CHNK) |
| 4878 | h1m.flags &= ~H1_MF_CLEN; |
| 4879 | if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK)) |
| 4880 | h1m.flags |= H1_MF_XFER_LEN; |
| 4881 | |
| 4882 | /* Uset HTX start-line flags */ |
| 4883 | if (h1m.flags & H1_MF_XFER_ENC) |
| 4884 | flags |= HTX_SL_F_XFER_ENC; |
| 4885 | if (h1m.flags & H1_MF_XFER_LEN) { |
| 4886 | flags |= HTX_SL_F_XFER_LEN; |
| 4887 | if (h1m.flags & H1_MF_CHNK) |
| 4888 | flags |= HTX_SL_F_CHNK; |
| 4889 | else if (h1m.flags & H1_MF_CLEN) |
| 4890 | flags |= HTX_SL_F_CLEN; |
| 4891 | if (h1m.body_len == 0) |
| 4892 | flags |= HTX_SL_F_BODYLESS; |
| 4893 | } |
| 4894 | sl->flags |= flags; |
| 4895 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 4896 | /* 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] | 4897 | * and the status code implies the presence of a message body, we must |
| 4898 | * announce a transfer encoding chunked. This is required by haproxy |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4899 | * for the keepalive compliance. If the applet announces a transfer-encoding |
| 4900 | * chunked itself, don't do anything. |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4901 | */ |
| 4902 | if ((flags & (HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN)) == HTX_SL_F_VER_11 && |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4903 | luactx->appctx->ctx.hlua_apphttp.status >= 200 && |
| 4904 | luactx->appctx->ctx.hlua_apphttp.status != 204 && |
| 4905 | luactx->appctx->ctx.hlua_apphttp.status != 304) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4906 | /* Add a new header */ |
| 4907 | sl->flags |= (HTX_SL_F_XFER_ENC|H1_MF_CHNK|H1_MF_XFER_LEN); |
| 4908 | if (!htx_add_header(htx, ist("transfer-encoding"), ist("chunked"))) { |
| 4909 | 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] | 4910 | luactx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4911 | WILL_LJMP(lua_error(L)); |
| 4912 | } |
| 4913 | } |
| 4914 | |
| 4915 | /* Finalize headers. */ |
| 4916 | if (!htx_add_endof(htx, HTX_BLK_EOH)) { |
| 4917 | hlua_pusherror(L, "Lua applet http '%s': Failed create the response.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4918 | luactx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4919 | WILL_LJMP(lua_error(L)); |
| 4920 | } |
| 4921 | |
| 4922 | if (htx_used_space(htx) > b_size(&res->buf) - global.tune.maxrewrite) { |
| 4923 | b_reset(&res->buf); |
| 4924 | hlua_pusherror(L, "Lua: 'start_response': response header block too big"); |
| 4925 | WILL_LJMP(lua_error(L)); |
| 4926 | } |
| 4927 | |
| 4928 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 4929 | channel_add_input(res, htx->data); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4930 | |
| 4931 | /* Headers sent, set the flag. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4932 | luactx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4933 | return 0; |
| 4934 | |
| 4935 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4936 | /* We will build the status line and the headers of the HTTP response. |
| 4937 | * We will try send at once if its not possible, we give back the hand |
| 4938 | * waiting for more room. |
| 4939 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4940 | __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] | 4941 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4942 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4943 | struct stream_interface *si = luactx->appctx->owner; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4944 | struct channel *res = si_ic(si); |
| 4945 | |
| 4946 | if (co_data(res)) { |
| 4947 | si_rx_room_blk(si); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4948 | 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] | 4949 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4950 | return MAY_LJMP(hlua_applet_http_send_response(L)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4951 | } |
| 4952 | |
| 4953 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4954 | __LJMP static int hlua_applet_http_start_response(lua_State *L) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4955 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4956 | return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4957 | } |
| 4958 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4959 | /* |
| 4960 | * |
| 4961 | * |
| 4962 | * Class HTTP |
| 4963 | * |
| 4964 | * |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4965 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4966 | |
| 4967 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 4968 | * a class stream, otherwise it throws an error. |
| 4969 | */ |
| 4970 | __LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4971 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4972 | return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref)); |
| 4973 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4974 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4975 | /* This function creates and push in the stack a HTTP object |
| 4976 | * according with a current TXN. |
| 4977 | */ |
| 4978 | static int hlua_http_new(lua_State *L, struct hlua_txn *txn) |
| 4979 | { |
| 4980 | struct hlua_txn *htxn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4981 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4982 | /* Check stack size. */ |
| 4983 | if (!lua_checkstack(L, 3)) |
| 4984 | return 0; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4985 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4986 | /* Create the object: obj[0] = userdata. |
| 4987 | * Note that the base of the Converters object is the |
| 4988 | * same than the TXN object. |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4989 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4990 | lua_newtable(L); |
| 4991 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
| 4992 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4993 | |
| 4994 | htxn->s = txn->s; |
| 4995 | htxn->p = txn->p; |
Christopher Faulet | 256b69a | 2019-05-23 11:14:21 +0200 | [diff] [blame] | 4996 | htxn->dir = txn->dir; |
| 4997 | htxn->flags = txn->flags; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4998 | |
| 4999 | /* Pop a class stream metatable and affect it to the table. */ |
| 5000 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref); |
| 5001 | lua_setmetatable(L, -2); |
| 5002 | |
| 5003 | return 1; |
| 5004 | } |
| 5005 | |
| 5006 | /* This function creates ans returns an array of HTTP headers. |
| 5007 | * This function does not fails. It is used as wrapper with the |
| 5008 | * 2 following functions. |
| 5009 | */ |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 5010 | __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] | 5011 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5012 | struct htx *htx; |
| 5013 | int32_t pos; |
| 5014 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5015 | /* Create the table. */ |
| 5016 | lua_newtable(L); |
| 5017 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5018 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5019 | htx = htxbuf(&msg->chn->buf); |
| 5020 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 5021 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 5022 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 5023 | struct ist n, v; |
| 5024 | int len; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5025 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5026 | if (type == HTX_BLK_HDR) { |
| 5027 | n = htx_get_blk_name(htx,blk); |
| 5028 | v = htx_get_blk_value(htx, blk); |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5029 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5030 | else if (type == HTX_BLK_EOH) |
| 5031 | break; |
| 5032 | else |
| 5033 | continue; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5034 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5035 | /* Check for existing entry: |
| 5036 | * assume that the table is on the top of the stack, and |
| 5037 | * push the key in the stack, the function lua_gettable() |
| 5038 | * perform the lookup. |
| 5039 | */ |
| 5040 | lua_pushlstring(L, n.ptr, n.len); |
| 5041 | lua_gettable(L, -2); |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5042 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5043 | switch (lua_type(L, -1)) { |
| 5044 | case LUA_TNIL: |
| 5045 | /* Table not found, create it. */ |
| 5046 | lua_pop(L, 1); /* remove the nil value. */ |
| 5047 | lua_pushlstring(L, n.ptr, n.len); /* push the header name as key. */ |
| 5048 | lua_newtable(L); /* create and push empty table. */ |
| 5049 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 5050 | lua_rawseti(L, -2, 0); /* index header value (pop it). */ |
| 5051 | 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] | 5052 | break; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5053 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5054 | case LUA_TTABLE: |
| 5055 | /* Entry found: push the value in the table. */ |
| 5056 | len = lua_rawlen(L, -1); |
| 5057 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 5058 | lua_rawseti(L, -2, len+1); /* index header value (pop it). */ |
| 5059 | lua_pop(L, 1); /* remove the table (it is stored in the main table). */ |
| 5060 | break; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5061 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5062 | default: |
| 5063 | /* Other cases are errors. */ |
| 5064 | hlua_pusherror(L, "internal error during the parsing of headers."); |
| 5065 | WILL_LJMP(lua_error(L)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5066 | } |
| 5067 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5068 | return 1; |
| 5069 | } |
| 5070 | |
| 5071 | __LJMP static int hlua_http_req_get_headers(lua_State *L) |
| 5072 | { |
| 5073 | struct hlua_txn *htxn; |
| 5074 | |
| 5075 | MAY_LJMP(check_args(L, 1, "req_get_headers")); |
| 5076 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5077 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5078 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5079 | WILL_LJMP(lua_error(L)); |
| 5080 | |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 5081 | return hlua_http_get_headers(L, &htxn->s->txn->req); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5082 | } |
| 5083 | |
| 5084 | __LJMP static int hlua_http_res_get_headers(lua_State *L) |
| 5085 | { |
| 5086 | struct hlua_txn *htxn; |
| 5087 | |
| 5088 | MAY_LJMP(check_args(L, 1, "res_get_headers")); |
| 5089 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5090 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5091 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5092 | WILL_LJMP(lua_error(L)); |
| 5093 | |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 5094 | return hlua_http_get_headers(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5095 | } |
| 5096 | |
| 5097 | /* This function replace full header, or just a value in |
| 5098 | * the request or in the response. It is a wrapper fir the |
| 5099 | * 4 following functions. |
| 5100 | */ |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5101 | __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] | 5102 | { |
| 5103 | size_t name_len; |
| 5104 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5105 | const char *reg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 5106 | const char *value = MAY_LJMP(luaL_checkstring(L, 4)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5107 | struct htx *htx; |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 5108 | struct my_regex *re; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5109 | |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 5110 | if (!(re = regex_comp(reg, 1, 1, NULL))) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5111 | WILL_LJMP(luaL_argerror(L, 3, "invalid regex")); |
| 5112 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5113 | htx = htxbuf(&msg->chn->buf); |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5114 | 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] | 5115 | regex_free(re); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5116 | return 0; |
| 5117 | } |
| 5118 | |
| 5119 | __LJMP static int hlua_http_req_rep_hdr(lua_State *L) |
| 5120 | { |
| 5121 | struct hlua_txn *htxn; |
| 5122 | |
| 5123 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 5124 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5125 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5126 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5127 | WILL_LJMP(lua_error(L)); |
| 5128 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5129 | 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] | 5130 | } |
| 5131 | |
| 5132 | __LJMP static int hlua_http_res_rep_hdr(lua_State *L) |
| 5133 | { |
| 5134 | struct hlua_txn *htxn; |
| 5135 | |
| 5136 | MAY_LJMP(check_args(L, 4, "res_rep_hdr")); |
| 5137 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5138 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5139 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5140 | WILL_LJMP(lua_error(L)); |
| 5141 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5142 | 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] | 5143 | } |
| 5144 | |
| 5145 | __LJMP static int hlua_http_req_rep_val(lua_State *L) |
| 5146 | { |
| 5147 | struct hlua_txn *htxn; |
| 5148 | |
| 5149 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 5150 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5151 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5152 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5153 | WILL_LJMP(lua_error(L)); |
| 5154 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5155 | 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] | 5156 | } |
| 5157 | |
| 5158 | __LJMP static int hlua_http_res_rep_val(lua_State *L) |
| 5159 | { |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5160 | struct hlua_txn *htxn; |
| 5161 | |
| 5162 | MAY_LJMP(check_args(L, 4, "res_rep_val")); |
| 5163 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5164 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5165 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5166 | WILL_LJMP(lua_error(L)); |
| 5167 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5168 | 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] | 5169 | } |
| 5170 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5171 | /* This function deletes all the occurrences of an header. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5172 | * It is a wrapper for the 2 following functions. |
| 5173 | */ |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5174 | __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] | 5175 | { |
| 5176 | size_t len; |
| 5177 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5178 | struct htx *htx = htxbuf(&msg->chn->buf); |
| 5179 | struct http_hdr_ctx ctx; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5180 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5181 | ctx.blk = NULL; |
| 5182 | while (http_find_header(htx, ist2(name, len), &ctx, 1)) |
| 5183 | http_remove_header(htx, &ctx); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5184 | return 0; |
| 5185 | } |
| 5186 | |
| 5187 | __LJMP static int hlua_http_req_del_hdr(lua_State *L) |
| 5188 | { |
| 5189 | struct hlua_txn *htxn; |
| 5190 | |
| 5191 | MAY_LJMP(check_args(L, 2, "req_del_hdr")); |
| 5192 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5193 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5194 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5195 | WILL_LJMP(lua_error(L)); |
| 5196 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5197 | return hlua_http_del_hdr(L, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5198 | } |
| 5199 | |
| 5200 | __LJMP static int hlua_http_res_del_hdr(lua_State *L) |
| 5201 | { |
| 5202 | struct hlua_txn *htxn; |
| 5203 | |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5204 | MAY_LJMP(check_args(L, 2, "res_del_hdr")); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5205 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5206 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5207 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5208 | WILL_LJMP(lua_error(L)); |
| 5209 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5210 | return hlua_http_del_hdr(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5211 | } |
| 5212 | |
| 5213 | /* This function adds an header. It is a wrapper used by |
| 5214 | * the 2 following functions. |
| 5215 | */ |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5216 | __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] | 5217 | { |
| 5218 | size_t name_len; |
| 5219 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5220 | size_t value_len; |
| 5221 | const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5222 | struct htx *htx = htxbuf(&msg->chn->buf); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5223 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5224 | lua_pushboolean(L, http_add_header(htx, ist2(name, name_len), |
| 5225 | ist2(value, value_len))); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5226 | return 0; |
| 5227 | } |
| 5228 | |
| 5229 | __LJMP static int hlua_http_req_add_hdr(lua_State *L) |
| 5230 | { |
| 5231 | struct hlua_txn *htxn; |
| 5232 | |
| 5233 | MAY_LJMP(check_args(L, 3, "req_add_hdr")); |
| 5234 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5235 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5236 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5237 | WILL_LJMP(lua_error(L)); |
| 5238 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5239 | return hlua_http_add_hdr(L, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5240 | } |
| 5241 | |
| 5242 | __LJMP static int hlua_http_res_add_hdr(lua_State *L) |
| 5243 | { |
| 5244 | struct hlua_txn *htxn; |
| 5245 | |
| 5246 | MAY_LJMP(check_args(L, 3, "res_add_hdr")); |
| 5247 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5248 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5249 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5250 | WILL_LJMP(lua_error(L)); |
| 5251 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5252 | return hlua_http_add_hdr(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5253 | } |
| 5254 | |
| 5255 | static int hlua_http_req_set_hdr(lua_State *L) |
| 5256 | { |
| 5257 | struct hlua_txn *htxn; |
| 5258 | |
| 5259 | MAY_LJMP(check_args(L, 3, "req_set_hdr")); |
| 5260 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5261 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5262 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5263 | WILL_LJMP(lua_error(L)); |
| 5264 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5265 | hlua_http_del_hdr(L, &htxn->s->txn->req); |
| 5266 | return hlua_http_add_hdr(L, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5267 | } |
| 5268 | |
| 5269 | static int hlua_http_res_set_hdr(lua_State *L) |
| 5270 | { |
| 5271 | struct hlua_txn *htxn; |
| 5272 | |
| 5273 | MAY_LJMP(check_args(L, 3, "res_set_hdr")); |
| 5274 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5275 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5276 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5277 | WILL_LJMP(lua_error(L)); |
| 5278 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5279 | hlua_http_del_hdr(L, &htxn->s->txn->rsp); |
| 5280 | return hlua_http_add_hdr(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5281 | } |
| 5282 | |
| 5283 | /* This function set the method. */ |
| 5284 | static int hlua_http_req_set_meth(lua_State *L) |
| 5285 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5286 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5287 | size_t name_len; |
| 5288 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5289 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5290 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5291 | WILL_LJMP(lua_error(L)); |
| 5292 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5293 | lua_pushboolean(L, http_req_replace_stline(0, name, name_len, htxn->p, htxn->s) != -1); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5294 | return 1; |
| 5295 | } |
| 5296 | |
| 5297 | /* This function set the method. */ |
| 5298 | static int hlua_http_req_set_path(lua_State *L) |
| 5299 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5300 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5301 | size_t name_len; |
| 5302 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 5303 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5304 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5305 | WILL_LJMP(lua_error(L)); |
| 5306 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5307 | lua_pushboolean(L, http_req_replace_stline(1, name, name_len, htxn->p, htxn->s) != -1); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5308 | return 1; |
| 5309 | } |
| 5310 | |
| 5311 | /* This function set the query-string. */ |
| 5312 | static int hlua_http_req_set_query(lua_State *L) |
| 5313 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5314 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5315 | size_t name_len; |
| 5316 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5317 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5318 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5319 | WILL_LJMP(lua_error(L)); |
| 5320 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5321 | /* Check length. */ |
| 5322 | if (name_len > trash.size - 1) { |
| 5323 | lua_pushboolean(L, 0); |
| 5324 | return 1; |
| 5325 | } |
| 5326 | |
| 5327 | /* Add the mark question as prefix. */ |
| 5328 | chunk_reset(&trash); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5329 | trash.area[trash.data++] = '?'; |
| 5330 | memcpy(trash.area + trash.data, name, name_len); |
| 5331 | trash.data += name_len; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5332 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5333 | lua_pushboolean(L, |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5334 | http_req_replace_stline(2, trash.area, trash.data, htxn->p, htxn->s) != -1); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5335 | return 1; |
| 5336 | } |
| 5337 | |
| 5338 | /* This function set the uri. */ |
| 5339 | static int hlua_http_req_set_uri(lua_State *L) |
| 5340 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5341 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5342 | size_t name_len; |
| 5343 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5344 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5345 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5346 | WILL_LJMP(lua_error(L)); |
| 5347 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5348 | lua_pushboolean(L, http_req_replace_stline(3, name, name_len, htxn->p, htxn->s) != -1); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5349 | return 1; |
| 5350 | } |
| 5351 | |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5352 | /* This function set the response code & optionally reason. */ |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5353 | static int hlua_http_res_set_status(lua_State *L) |
| 5354 | { |
| 5355 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5356 | unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2)); |
Christopher Faulet | 96bff76 | 2019-12-17 13:46:18 +0100 | [diff] [blame] | 5357 | const char *str = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
| 5358 | const struct ist reason = ist2(str, (str ? strlen(str) : 0)); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5359 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5360 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5361 | WILL_LJMP(lua_error(L)); |
| 5362 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5363 | http_res_set_status(code, reason, htxn->s); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5364 | return 0; |
| 5365 | } |
| 5366 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5367 | /* |
| 5368 | * |
| 5369 | * |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5370 | * Class TXN |
| 5371 | * |
| 5372 | * |
| 5373 | */ |
| 5374 | |
| 5375 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5376 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5377 | */ |
| 5378 | __LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud) |
| 5379 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 5380 | return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref)); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5381 | } |
| 5382 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5383 | __LJMP static int hlua_set_var(lua_State *L) |
| 5384 | { |
| 5385 | struct hlua_txn *htxn; |
| 5386 | const char *name; |
| 5387 | size_t len; |
| 5388 | struct sample smp; |
| 5389 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 5390 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 5391 | 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] | 5392 | |
| 5393 | /* It is useles to retrieve the stream, but this function |
| 5394 | * runs only in a stream context. |
| 5395 | */ |
| 5396 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5397 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5398 | |
| 5399 | /* Converts the third argument in a sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 5400 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5401 | hlua_lua2smp(L, 3, &smp); |
| 5402 | |
Aurelien DARRAGON | d39fb74 | 2023-05-17 16:06:11 +0200 | [diff] [blame] | 5403 | /* Store the sample in a variable. We don't need to dup the smp, vars API |
| 5404 | * already takes care of duplicating dynamic var data. |
| 5405 | */ |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 5406 | 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] | 5407 | |
| 5408 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 5409 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 5410 | else |
| 5411 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 5412 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 5413 | return 1; |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5414 | } |
| 5415 | |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 5416 | __LJMP static int hlua_unset_var(lua_State *L) |
| 5417 | { |
| 5418 | struct hlua_txn *htxn; |
| 5419 | const char *name; |
| 5420 | size_t len; |
| 5421 | struct sample smp; |
| 5422 | |
| 5423 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 5424 | |
| 5425 | /* It is useles to retrieve the stream, but this function |
| 5426 | * runs only in a stream context. |
| 5427 | */ |
| 5428 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5429 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5430 | |
| 5431 | /* Unset the variable. */ |
| 5432 | 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] | 5433 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 5434 | return 1; |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 5435 | } |
| 5436 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5437 | __LJMP static int hlua_get_var(lua_State *L) |
| 5438 | { |
| 5439 | struct hlua_txn *htxn; |
| 5440 | const char *name; |
| 5441 | size_t len; |
| 5442 | struct sample smp; |
| 5443 | |
| 5444 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 5445 | |
| 5446 | /* It is useles to retrieve the stream, but this function |
| 5447 | * runs only in a stream context. |
| 5448 | */ |
| 5449 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5450 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5451 | |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 5452 | smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR); |
Willy Tarreau | 6204cd9 | 2016-03-10 16:33:04 +0100 | [diff] [blame] | 5453 | if (!vars_get_by_name(name, len, &smp)) { |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5454 | lua_pushnil(L); |
| 5455 | return 1; |
| 5456 | } |
| 5457 | |
| 5458 | return hlua_smp2lua(L, &smp); |
| 5459 | } |
| 5460 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5461 | __LJMP static int hlua_set_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5462 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5463 | struct hlua *hlua; |
| 5464 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5465 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 5466 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5467 | /* It is useles to retrieve the stream, but this function |
| 5468 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5469 | */ |
| 5470 | MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 5471 | |
| 5472 | /* Get hlua struct, or NULL if we execute from main lua state */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5473 | hlua = hlua_gethlua(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 5474 | if (!hlua) |
| 5475 | return 0; |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5476 | |
| 5477 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 5478 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5479 | |
| 5480 | /* Get and store new value. */ |
| 5481 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 5482 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 5483 | |
| 5484 | return 0; |
| 5485 | } |
| 5486 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5487 | __LJMP static int hlua_get_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5488 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5489 | struct hlua *hlua; |
| 5490 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5491 | MAY_LJMP(check_args(L, 1, "get_priv")); |
| 5492 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5493 | /* It is useles to retrieve the stream, but this function |
| 5494 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5495 | */ |
| 5496 | MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 5497 | |
| 5498 | /* Get hlua struct, or NULL if we execute from main lua state */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5499 | hlua = hlua_gethlua(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 5500 | if (!hlua) { |
| 5501 | lua_pushnil(L); |
| 5502 | return 1; |
| 5503 | } |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5504 | |
| 5505 | /* Push configuration index in the stack. */ |
| 5506 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 5507 | |
| 5508 | return 1; |
| 5509 | } |
| 5510 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5511 | /* Create stack entry containing a class TXN. This function |
| 5512 | * return 0 if the stack does not contains free slots, |
| 5513 | * otherwise it returns 1. |
| 5514 | */ |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5515 | 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] | 5516 | { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5517 | struct hlua_txn *htxn; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5518 | |
| 5519 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5520 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5521 | return 0; |
| 5522 | |
| 5523 | /* NOTE: The allocation never fails. The failure |
| 5524 | * throw an error, and the function never returns. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5525 | * if the throw is not available, the process is aborted. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5526 | */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5527 | /* Create the object: obj[0] = userdata. */ |
| 5528 | lua_newtable(L); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5529 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5530 | lua_rawseti(L, -2, 0); |
| 5531 | |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5532 | htxn->s = s; |
| 5533 | htxn->p = p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 5534 | htxn->dir = dir; |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5535 | htxn->flags = flags; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5536 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 5537 | /* Create the "f" field that contains a list of fetches. */ |
| 5538 | lua_pushstring(L, "f"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 5539 | if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5540 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5541 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5542 | |
| 5543 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 5544 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 5545 | 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] | 5546 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5547 | lua_rawset(L, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 5548 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5549 | /* Create the "c" field that contains a list of converters. */ |
| 5550 | lua_pushstring(L, "c"); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5551 | if (!hlua_converters_new(L, htxn, 0)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5552 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5553 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5554 | |
| 5555 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 5556 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 5557 | if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5558 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5559 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5560 | |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5561 | /* Create the "req" field that contains the request channel object. */ |
| 5562 | lua_pushstring(L, "req"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 5563 | if (!hlua_channel_new(L, &s->req)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5564 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5565 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5566 | |
| 5567 | /* Create the "res" field that contains the response channel object. */ |
| 5568 | lua_pushstring(L, "res"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 5569 | if (!hlua_channel_new(L, &s->res)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5570 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5571 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5572 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5573 | /* Creates the HTTP object is the current proxy allows http. */ |
| 5574 | lua_pushstring(L, "http"); |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 5575 | if (IS_HTX_STRM(s)) { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5576 | if (!hlua_http_new(L, htxn)) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5577 | return 0; |
| 5578 | } |
| 5579 | else |
| 5580 | lua_pushnil(L); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5581 | lua_rawset(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5582 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5583 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 5584 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref); |
| 5585 | lua_setmetatable(L, -2); |
| 5586 | |
| 5587 | return 1; |
| 5588 | } |
| 5589 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 5590 | __LJMP static int hlua_txn_deflog(lua_State *L) |
| 5591 | { |
| 5592 | const char *msg; |
| 5593 | struct hlua_txn *htxn; |
| 5594 | |
| 5595 | MAY_LJMP(check_args(L, 2, "deflog")); |
| 5596 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5597 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5598 | |
| 5599 | hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg); |
| 5600 | return 0; |
| 5601 | } |
| 5602 | |
| 5603 | __LJMP static int hlua_txn_log(lua_State *L) |
| 5604 | { |
| 5605 | int level; |
| 5606 | const char *msg; |
| 5607 | struct hlua_txn *htxn; |
| 5608 | |
| 5609 | MAY_LJMP(check_args(L, 3, "log")); |
| 5610 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5611 | level = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5612 | msg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 5613 | |
| 5614 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 5615 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 5616 | |
| 5617 | hlua_sendlog(htxn->s->be, level, msg); |
| 5618 | return 0; |
| 5619 | } |
| 5620 | |
| 5621 | __LJMP static int hlua_txn_log_debug(lua_State *L) |
| 5622 | { |
| 5623 | const char *msg; |
| 5624 | struct hlua_txn *htxn; |
| 5625 | |
| 5626 | MAY_LJMP(check_args(L, 2, "Debug")); |
| 5627 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5628 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5629 | hlua_sendlog(htxn->s->be, LOG_DEBUG, msg); |
| 5630 | return 0; |
| 5631 | } |
| 5632 | |
| 5633 | __LJMP static int hlua_txn_log_info(lua_State *L) |
| 5634 | { |
| 5635 | const char *msg; |
| 5636 | struct hlua_txn *htxn; |
| 5637 | |
| 5638 | MAY_LJMP(check_args(L, 2, "Info")); |
| 5639 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5640 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5641 | hlua_sendlog(htxn->s->be, LOG_INFO, msg); |
| 5642 | return 0; |
| 5643 | } |
| 5644 | |
| 5645 | __LJMP static int hlua_txn_log_warning(lua_State *L) |
| 5646 | { |
| 5647 | const char *msg; |
| 5648 | struct hlua_txn *htxn; |
| 5649 | |
| 5650 | MAY_LJMP(check_args(L, 2, "Warning")); |
| 5651 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5652 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5653 | hlua_sendlog(htxn->s->be, LOG_WARNING, msg); |
| 5654 | return 0; |
| 5655 | } |
| 5656 | |
| 5657 | __LJMP static int hlua_txn_log_alert(lua_State *L) |
| 5658 | { |
| 5659 | const char *msg; |
| 5660 | struct hlua_txn *htxn; |
| 5661 | |
| 5662 | MAY_LJMP(check_args(L, 2, "Alert")); |
| 5663 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5664 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5665 | hlua_sendlog(htxn->s->be, LOG_ALERT, msg); |
| 5666 | return 0; |
| 5667 | } |
| 5668 | |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5669 | __LJMP static int hlua_txn_set_loglevel(lua_State *L) |
| 5670 | { |
| 5671 | struct hlua_txn *htxn; |
| 5672 | int ll; |
| 5673 | |
| 5674 | MAY_LJMP(check_args(L, 2, "set_loglevel")); |
| 5675 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5676 | ll = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5677 | |
| 5678 | if (ll < 0 || ll > 7) |
| 5679 | WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7")); |
| 5680 | |
| 5681 | htxn->s->logs.level = ll; |
| 5682 | return 0; |
| 5683 | } |
| 5684 | |
| 5685 | __LJMP static int hlua_txn_set_tos(lua_State *L) |
| 5686 | { |
| 5687 | struct hlua_txn *htxn; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5688 | int tos; |
| 5689 | |
| 5690 | MAY_LJMP(check_args(L, 2, "set_tos")); |
| 5691 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5692 | tos = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5693 | |
Willy Tarreau | 1a18b54 | 2018-12-11 16:37:42 +0100 | [diff] [blame] | 5694 | conn_set_tos(objt_conn(htxn->s->sess->origin), tos); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5695 | return 0; |
| 5696 | } |
| 5697 | |
| 5698 | __LJMP static int hlua_txn_set_mark(lua_State *L) |
| 5699 | { |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5700 | struct hlua_txn *htxn; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5701 | int mark; |
| 5702 | |
| 5703 | MAY_LJMP(check_args(L, 2, "set_mark")); |
| 5704 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5705 | mark = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5706 | |
Lukas Tribus | 579e3e3 | 2019-08-11 18:03:45 +0200 | [diff] [blame] | 5707 | conn_set_mark(objt_conn(htxn->s->sess->origin), mark); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5708 | return 0; |
| 5709 | } |
| 5710 | |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 5711 | __LJMP static int hlua_txn_set_priority_class(lua_State *L) |
| 5712 | { |
| 5713 | struct hlua_txn *htxn; |
| 5714 | |
| 5715 | MAY_LJMP(check_args(L, 2, "set_priority_class")); |
| 5716 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5717 | htxn->s->priority_class = queue_limit_class(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 5718 | return 0; |
| 5719 | } |
| 5720 | |
| 5721 | __LJMP static int hlua_txn_set_priority_offset(lua_State *L) |
| 5722 | { |
| 5723 | struct hlua_txn *htxn; |
| 5724 | |
| 5725 | MAY_LJMP(check_args(L, 2, "set_priority_offset")); |
| 5726 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5727 | htxn->s->priority_offset = queue_limit_offset(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 5728 | return 0; |
| 5729 | } |
| 5730 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5731 | /* 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] | 5732 | * 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] | 5733 | * message and terminate the transaction. It returns 1 on success and 0 on |
| 5734 | * error. The Reply must be on top of the stack. |
| 5735 | */ |
| 5736 | __LJMP static int hlua_txn_forward_reply(lua_State *L, struct stream *s) |
| 5737 | { |
| 5738 | struct htx *htx; |
| 5739 | struct htx_sl *sl; |
| 5740 | struct h1m h1m; |
| 5741 | const char *status, *reason, *body; |
| 5742 | size_t status_len, reason_len, body_len; |
| 5743 | int ret, code, flags; |
| 5744 | |
| 5745 | code = 200; |
| 5746 | status = "200"; |
| 5747 | status_len = 3; |
| 5748 | ret = lua_getfield(L, -1, "status"); |
| 5749 | if (ret == LUA_TNUMBER) { |
| 5750 | code = lua_tointeger(L, -1); |
| 5751 | status = lua_tolstring(L, -1, &status_len); |
| 5752 | } |
| 5753 | lua_pop(L, 1); |
| 5754 | |
| 5755 | reason = http_get_reason(code); |
| 5756 | reason_len = strlen(reason); |
| 5757 | ret = lua_getfield(L, -1, "reason"); |
| 5758 | if (ret == LUA_TSTRING) |
| 5759 | reason = lua_tolstring(L, -1, &reason_len); |
| 5760 | lua_pop(L, 1); |
| 5761 | |
| 5762 | body = NULL; |
| 5763 | body_len = 0; |
| 5764 | ret = lua_getfield(L, -1, "body"); |
| 5765 | if (ret == LUA_TSTRING) |
| 5766 | body = lua_tolstring(L, -1, &body_len); |
| 5767 | lua_pop(L, 1); |
| 5768 | |
| 5769 | /* Prepare the response before inserting the headers */ |
| 5770 | h1m_init_res(&h1m); |
| 5771 | htx = htx_from_buf(&s->res.buf); |
| 5772 | channel_htx_truncate(&s->res, htx); |
| 5773 | if (s->txn->req.flags & HTTP_MSGF_VER_11) { |
| 5774 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11); |
| 5775 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), |
| 5776 | ist2(status, status_len), ist2(reason, reason_len)); |
| 5777 | } |
| 5778 | else { |
| 5779 | flags = HTX_SL_F_IS_RESP; |
| 5780 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), |
| 5781 | ist2(status, status_len), ist2(reason, reason_len)); |
| 5782 | } |
| 5783 | if (!sl) |
| 5784 | goto fail; |
| 5785 | sl->info.res.status = code; |
| 5786 | |
| 5787 | /* Push in the stack the "headers" entry. */ |
| 5788 | ret = lua_getfield(L, -1, "headers"); |
| 5789 | if (ret != LUA_TTABLE) |
| 5790 | goto skip_headers; |
| 5791 | |
| 5792 | lua_pushnil(L); |
| 5793 | while (lua_next(L, -2) != 0) { |
| 5794 | struct ist name, value; |
| 5795 | const char *n, *v; |
| 5796 | size_t nlen, vlen; |
| 5797 | |
| 5798 | if (!lua_isstring(L, -2) || !lua_istable(L, -1)) { |
| 5799 | /* Skip element if the key is not a string or if the value is not a table */ |
| 5800 | goto next_hdr; |
| 5801 | } |
| 5802 | |
| 5803 | n = lua_tolstring(L, -2, &nlen); |
| 5804 | name = ist2(n, nlen); |
| 5805 | if (isteqi(name, ist("content-length"))) { |
| 5806 | /* Always skip content-length header. It will be added |
| 5807 | * later with the correct len |
| 5808 | */ |
| 5809 | goto next_hdr; |
| 5810 | } |
| 5811 | |
| 5812 | /* Loop on header's values */ |
| 5813 | lua_pushnil(L); |
| 5814 | while (lua_next(L, -2)) { |
| 5815 | if (!lua_isstring(L, -1)) { |
| 5816 | /* Skip the value if it is not a string */ |
| 5817 | goto next_value; |
| 5818 | } |
| 5819 | |
| 5820 | v = lua_tolstring(L, -1, &vlen); |
| 5821 | value = ist2(v, vlen); |
| 5822 | |
| 5823 | if (isteqi(name, ist("transfer-encoding"))) |
| 5824 | h1_parse_xfer_enc_header(&h1m, value); |
| 5825 | if (!htx_add_header(htx, ist2(n, nlen), ist2(v, vlen))) |
| 5826 | goto fail; |
| 5827 | |
| 5828 | next_value: |
| 5829 | lua_pop(L, 1); |
| 5830 | } |
| 5831 | |
| 5832 | next_hdr: |
| 5833 | lua_pop(L, 1); |
| 5834 | } |
| 5835 | skip_headers: |
| 5836 | lua_pop(L, 1); |
| 5837 | |
| 5838 | /* Update h1m flags: CLEN is set if CHNK is not present */ |
| 5839 | if (!(h1m.flags & H1_MF_CHNK)) { |
| 5840 | const char *clen = ultoa(body_len); |
| 5841 | |
| 5842 | h1m.flags |= H1_MF_CLEN; |
| 5843 | if (!htx_add_header(htx, ist("content-length"), ist(clen))) |
| 5844 | goto fail; |
| 5845 | } |
| 5846 | if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK)) |
| 5847 | h1m.flags |= H1_MF_XFER_LEN; |
| 5848 | |
| 5849 | /* Update HTX start-line flags */ |
| 5850 | if (h1m.flags & H1_MF_XFER_ENC) |
| 5851 | flags |= HTX_SL_F_XFER_ENC; |
| 5852 | if (h1m.flags & H1_MF_XFER_LEN) { |
| 5853 | flags |= HTX_SL_F_XFER_LEN; |
| 5854 | if (h1m.flags & H1_MF_CHNK) |
| 5855 | flags |= HTX_SL_F_CHNK; |
| 5856 | else if (h1m.flags & H1_MF_CLEN) |
| 5857 | flags |= HTX_SL_F_CLEN; |
| 5858 | if (h1m.body_len == 0) |
| 5859 | flags |= HTX_SL_F_BODYLESS; |
| 5860 | } |
| 5861 | sl->flags |= flags; |
| 5862 | |
| 5863 | |
| 5864 | if (!htx_add_endof(htx, HTX_BLK_EOH) || |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 5865 | (body_len && !htx_add_data_atonce(htx, ist2(body, body_len)))) |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5866 | goto fail; |
| 5867 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 5868 | htx->flags |= HTX_FL_EOM; |
| 5869 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5870 | /* Now, forward the response and terminate the transaction */ |
| 5871 | s->txn->status = code; |
| 5872 | htx_to_buf(htx, &s->res.buf); |
| 5873 | if (!http_forward_proxy_resp(s, 1)) |
| 5874 | goto fail; |
| 5875 | |
| 5876 | return 1; |
| 5877 | |
| 5878 | fail: |
| 5879 | channel_htx_truncate(&s->res, htx); |
| 5880 | return 0; |
| 5881 | } |
| 5882 | |
| 5883 | /* Terminate a transaction if called from a lua action. For TCP streams, |
| 5884 | * processing is just aborted. Nothing is returned to the client and all |
| 5885 | * arguments are ignored. For HTTP streams, if a reply is passed as argument, it |
| 5886 | * is forwarded to the client before terminating the transaction. On success, |
| 5887 | * the function exits with ACT_RET_DONE code. If an error occurred, it exits |
| 5888 | * with ACT_RET_ERR code. If this function is not called from a lua action, it |
| 5889 | * just exits without any processing. |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5890 | */ |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 5891 | __LJMP static int hlua_txn_done(lua_State *L) |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5892 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 5893 | struct hlua_txn *htxn; |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5894 | struct stream *s; |
| 5895 | int finst; |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5896 | |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 5897 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5898 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5899 | /* If the flags NOTERM is set, we cannot terminate the session, so we |
| 5900 | * just end the execution of the current lua code. */ |
| 5901 | if (htxn->flags & HLUA_TXN_NOTERM) |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5902 | WILL_LJMP(hlua_done(L)); |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5903 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5904 | s = htxn->s; |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5905 | if (!IS_HTX_STRM(htxn->s)) { |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5906 | struct channel *req = &s->req; |
| 5907 | struct channel *res = &s->res; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 5908 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5909 | channel_auto_read(req); |
| 5910 | channel_abort(req); |
| 5911 | channel_auto_close(req); |
| 5912 | channel_erase(req); |
| 5913 | |
| 5914 | res->wex = tick_add_ifset(now_ms, res->wto); |
| 5915 | channel_auto_read(res); |
| 5916 | channel_auto_close(res); |
| 5917 | channel_shutr_now(res); |
| 5918 | |
| 5919 | finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_D); |
| 5920 | goto done; |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 5921 | } |
Thierry FOURNIER | 10ec214 | 2015-08-24 17:23:45 +0200 | [diff] [blame] | 5922 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5923 | if (lua_gettop(L) == 1 || !lua_istable(L, 2)) { |
| 5924 | /* No reply or invalid reply */ |
| 5925 | s->txn->status = 0; |
| 5926 | http_reply_and_close(s, 0, NULL); |
| 5927 | } |
| 5928 | else { |
| 5929 | /* Remove extra args to have the reply on top of the stack */ |
| 5930 | if (lua_gettop(L) > 2) |
| 5931 | lua_pop(L, lua_gettop(L) - 2); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5932 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5933 | if (!hlua_txn_forward_reply(L, s)) { |
| 5934 | if (!(s->flags & SF_ERR_MASK)) |
| 5935 | s->flags |= SF_ERR_PRXCOND; |
| 5936 | lua_pushinteger(L, ACT_RET_ERR); |
| 5937 | WILL_LJMP(hlua_done(L)); |
| 5938 | return 0; /* Never reached */ |
| 5939 | } |
Christopher Faulet | 4d0e263 | 2019-07-16 10:52:40 +0200 | [diff] [blame] | 5940 | } |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 5941 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5942 | finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_H); |
| 5943 | if (htxn->dir == SMP_OPT_DIR_REQ) { |
| 5944 | /* let's log the request time */ |
| 5945 | s->logs.tv_request = now; |
| 5946 | 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] | 5947 | _HA_ATOMIC_INC(&s->sess->fe->fe_counters.intercepted_req); |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5948 | } |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 5949 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5950 | done: |
| 5951 | if (!(s->flags & SF_ERR_MASK)) |
| 5952 | s->flags |= SF_ERR_LOCAL; |
| 5953 | if (!(s->flags & SF_FINST_MASK)) |
| 5954 | s->flags |= finst; |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 5955 | |
Christopher Faulet | 4ad7310 | 2020-03-05 11:07:31 +0100 | [diff] [blame] | 5956 | lua_pushinteger(L, ACT_RET_ABRT); |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 5957 | WILL_LJMP(hlua_done(L)); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 5958 | return 0; |
| 5959 | } |
| 5960 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5961 | /* |
| 5962 | * |
| 5963 | * |
| 5964 | * Class REPLY |
| 5965 | * |
| 5966 | * |
| 5967 | */ |
| 5968 | |
| 5969 | /* Pushes the TXN reply onto the top of the stack. If the stask does not have a |
| 5970 | * free slots, the function fails and returns 0; |
| 5971 | */ |
| 5972 | static int hlua_txn_reply_new(lua_State *L) |
| 5973 | { |
| 5974 | struct hlua_txn *htxn; |
| 5975 | const char *reason, *body = NULL; |
| 5976 | int ret, status; |
| 5977 | |
| 5978 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5979 | if (!IS_HTX_STRM(htxn->s)) { |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5980 | hlua_pusherror(L, "txn object is not an HTTP transaction."); |
| 5981 | WILL_LJMP(lua_error(L)); |
| 5982 | } |
| 5983 | |
| 5984 | /* Default value */ |
| 5985 | status = 200; |
| 5986 | reason = http_get_reason(status); |
| 5987 | |
| 5988 | if (lua_istable(L, 2)) { |
| 5989 | /* load status and reason from the table argument at index 2 */ |
| 5990 | ret = lua_getfield(L, 2, "status"); |
| 5991 | if (ret == LUA_TNIL) |
| 5992 | goto reason; |
| 5993 | else if (ret != LUA_TNUMBER) { |
| 5994 | /* invalid status: ignore the reason */ |
| 5995 | goto body; |
| 5996 | } |
| 5997 | status = lua_tointeger(L, -1); |
| 5998 | |
| 5999 | reason: |
| 6000 | lua_pop(L, 1); /* restore the stack: remove status */ |
| 6001 | ret = lua_getfield(L, 2, "reason"); |
| 6002 | if (ret == LUA_TSTRING) |
| 6003 | reason = lua_tostring(L, -1); |
| 6004 | |
| 6005 | body: |
| 6006 | lua_pop(L, 1); /* restore the stack: remove invalid status or reason */ |
| 6007 | ret = lua_getfield(L, 2, "body"); |
| 6008 | if (ret == LUA_TSTRING) |
| 6009 | body = lua_tostring(L, -1); |
| 6010 | lua_pop(L, 1); /* restore the stack: remove body */ |
| 6011 | } |
| 6012 | |
| 6013 | /* Create the Reply table */ |
| 6014 | lua_newtable(L); |
| 6015 | |
| 6016 | /* Add status element */ |
| 6017 | lua_pushstring(L, "status"); |
| 6018 | lua_pushinteger(L, status); |
| 6019 | lua_settable(L, -3); |
| 6020 | |
| 6021 | /* Add reason element */ |
| 6022 | reason = http_get_reason(status); |
| 6023 | lua_pushstring(L, "reason"); |
| 6024 | lua_pushstring(L, reason); |
| 6025 | lua_settable(L, -3); |
| 6026 | |
| 6027 | /* Add body element, nil if undefined */ |
| 6028 | lua_pushstring(L, "body"); |
| 6029 | if (body) |
| 6030 | lua_pushstring(L, body); |
| 6031 | else |
| 6032 | lua_pushnil(L); |
| 6033 | lua_settable(L, -3); |
| 6034 | |
| 6035 | /* Add headers element */ |
| 6036 | lua_pushstring(L, "headers"); |
| 6037 | lua_newtable(L); |
| 6038 | |
| 6039 | /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */ |
| 6040 | if (lua_istable(L, 2)) { |
| 6041 | /* load headers from the table argument at index 2. If it is a table, copy it. */ |
| 6042 | ret = lua_getfield(L, 2, "headers"); |
| 6043 | if (ret == LUA_TTABLE) { |
| 6044 | /* stack: [ ... <headers:table>, <table> ] */ |
| 6045 | lua_pushnil(L); |
| 6046 | while (lua_next(L, -2) != 0) { |
| 6047 | /* stack: [ ... <headers:table>, <table>, k, v] */ |
| 6048 | if (!lua_isstring(L, -1) && !lua_istable(L, -1)) { |
| 6049 | /* invalid value type, skip it */ |
| 6050 | lua_pop(L, 1); |
| 6051 | continue; |
| 6052 | } |
| 6053 | |
| 6054 | |
| 6055 | /* Duplicate the key and swap it with the value. */ |
| 6056 | lua_pushvalue(L, -2); |
| 6057 | lua_insert(L, -2); |
| 6058 | /* stack: [ ... <headers:table>, <table>, k, k, v ] */ |
| 6059 | |
| 6060 | lua_newtable(L); |
| 6061 | lua_insert(L, -2); |
| 6062 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, v ] */ |
| 6063 | |
| 6064 | if (lua_isstring(L, -1)) { |
| 6065 | /* push the value in the inner table */ |
| 6066 | lua_rawseti(L, -2, 1); |
| 6067 | } |
| 6068 | else { /* table */ |
| 6069 | lua_pushnil(L); |
| 6070 | while (lua_next(L, -2) != 0) { |
| 6071 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2, v2 ] */ |
| 6072 | if (!lua_isstring(L, -1)) { |
| 6073 | /* invalid value type, skip it*/ |
| 6074 | lua_pop(L, 1); |
| 6075 | continue; |
| 6076 | } |
| 6077 | /* push the value in the inner table */ |
| 6078 | lua_rawseti(L, -4, lua_rawlen(L, -4) + 1); |
| 6079 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2 ] */ |
| 6080 | } |
| 6081 | lua_pop(L, 1); |
| 6082 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table> ] */ |
| 6083 | } |
| 6084 | |
| 6085 | /* push (k,v) on the stack in the headers table: |
| 6086 | * stack: [ ... <headers:table>, <table>, k, k, v ] |
| 6087 | */ |
| 6088 | lua_settable(L, -5); |
| 6089 | /* stack: [ ... <headers:table>, <table>, k ] */ |
| 6090 | } |
| 6091 | } |
| 6092 | lua_pop(L, 1); |
| 6093 | } |
| 6094 | /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */ |
| 6095 | lua_settable(L, -3); |
| 6096 | /* stack: [ txn, <Arg:table>, <Reply:table> ] */ |
| 6097 | |
| 6098 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 6099 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_reply_ref); |
| 6100 | lua_setmetatable(L, -2); |
| 6101 | return 1; |
| 6102 | } |
| 6103 | |
| 6104 | /* Set the reply status code, and optionally the reason. If no reason is |
| 6105 | * provided, the default one corresponding to the status code is used. |
| 6106 | */ |
| 6107 | __LJMP static int hlua_txn_reply_set_status(lua_State *L) |
| 6108 | { |
| 6109 | int status = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 6110 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
| 6111 | |
| 6112 | /* First argument (self) must be a table */ |
| 6113 | luaL_checktype(L, 1, LUA_TTABLE); |
| 6114 | |
| 6115 | if (status < 100 || status > 599) { |
| 6116 | lua_pushboolean(L, 0); |
| 6117 | return 1; |
| 6118 | } |
| 6119 | if (!reason) |
| 6120 | reason = http_get_reason(status); |
| 6121 | |
| 6122 | lua_pushinteger(L, status); |
| 6123 | lua_setfield(L, 1, "status"); |
| 6124 | |
| 6125 | lua_pushstring(L, reason); |
| 6126 | lua_setfield(L, 1, "reason"); |
| 6127 | |
| 6128 | lua_pushboolean(L, 1); |
| 6129 | return 1; |
| 6130 | } |
| 6131 | |
| 6132 | /* Add a header into the reply object. Each header name is associated to an |
| 6133 | * array of values in the "headers" table. If the header name is not found, a |
| 6134 | * new entry is created. |
| 6135 | */ |
| 6136 | __LJMP static int hlua_txn_reply_add_header(lua_State *L) |
| 6137 | { |
| 6138 | const char *name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 6139 | const char *value = MAY_LJMP(luaL_checkstring(L, 3)); |
| 6140 | int ret; |
| 6141 | |
| 6142 | /* First argument (self) must be a table */ |
| 6143 | luaL_checktype(L, 1, LUA_TTABLE); |
| 6144 | |
| 6145 | /* Push in the stack the "headers" entry. */ |
| 6146 | ret = lua_getfield(L, 1, "headers"); |
| 6147 | if (ret != LUA_TTABLE) { |
| 6148 | hlua_pusherror(L, "Reply['headers'] is expected to a an array. %s found", lua_typename(L, ret)); |
| 6149 | WILL_LJMP(lua_error(L)); |
| 6150 | } |
| 6151 | |
| 6152 | /* check if the header is already registered. If not, register it. */ |
| 6153 | ret = lua_getfield(L, -1, name); |
| 6154 | if (ret == LUA_TNIL) { |
| 6155 | /* Entry not found. */ |
| 6156 | lua_pop(L, 1); /* remove the nil. The "headers" table is the top of the stack. */ |
| 6157 | |
| 6158 | /* Insert the new header name in the array in the top of the stack. |
| 6159 | * It left the new array in the top of the stack. |
| 6160 | */ |
| 6161 | lua_newtable(L); |
| 6162 | lua_pushstring(L, name); |
| 6163 | lua_pushvalue(L, -2); |
| 6164 | lua_settable(L, -4); |
| 6165 | } |
| 6166 | else if (ret != LUA_TTABLE) { |
| 6167 | hlua_pusherror(L, "Reply['headers']['%s'] is expected to be an array. %s found", name, lua_typename(L, ret)); |
| 6168 | WILL_LJMP(lua_error(L)); |
| 6169 | } |
| 6170 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 6171 | /* Now the top of thestack is an array of values. We push |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 6172 | * the header value as new entry. |
| 6173 | */ |
| 6174 | lua_pushstring(L, value); |
| 6175 | ret = lua_rawlen(L, -2); |
| 6176 | lua_rawseti(L, -2, ret + 1); |
| 6177 | |
| 6178 | lua_pushboolean(L, 1); |
| 6179 | return 1; |
| 6180 | } |
| 6181 | |
| 6182 | /* Remove all occurrences of a given header name. */ |
| 6183 | __LJMP static int hlua_txn_reply_del_header(lua_State *L) |
| 6184 | { |
| 6185 | const char *name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 6186 | int ret; |
| 6187 | |
| 6188 | /* First argument (self) must be a table */ |
| 6189 | luaL_checktype(L, 1, LUA_TTABLE); |
| 6190 | |
| 6191 | /* Push in the stack the "headers" entry. */ |
| 6192 | ret = lua_getfield(L, 1, "headers"); |
| 6193 | if (ret != LUA_TTABLE) { |
| 6194 | hlua_pusherror(L, "Reply['headers'] is expected to be an array. %s found", lua_typename(L, ret)); |
| 6195 | WILL_LJMP(lua_error(L)); |
| 6196 | } |
| 6197 | |
| 6198 | lua_pushstring(L, name); |
| 6199 | lua_pushnil(L); |
| 6200 | lua_settable(L, -3); |
| 6201 | |
| 6202 | lua_pushboolean(L, 1); |
| 6203 | return 1; |
| 6204 | } |
| 6205 | |
| 6206 | /* Set the reply's body. Overwrite any existing entry. */ |
| 6207 | __LJMP static int hlua_txn_reply_set_body(lua_State *L) |
| 6208 | { |
| 6209 | const char *payload = MAY_LJMP(luaL_checkstring(L, 2)); |
| 6210 | |
| 6211 | /* First argument (self) must be a table */ |
| 6212 | luaL_checktype(L, 1, LUA_TTABLE); |
| 6213 | |
| 6214 | lua_pushstring(L, payload); |
| 6215 | lua_setfield(L, 1, "body"); |
| 6216 | |
| 6217 | lua_pushboolean(L, 1); |
| 6218 | return 1; |
| 6219 | } |
| 6220 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 6221 | __LJMP static int hlua_log(lua_State *L) |
| 6222 | { |
| 6223 | int level; |
| 6224 | const char *msg; |
| 6225 | |
| 6226 | MAY_LJMP(check_args(L, 2, "log")); |
| 6227 | level = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 6228 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 6229 | |
| 6230 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 6231 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 6232 | |
| 6233 | hlua_sendlog(NULL, level, msg); |
| 6234 | return 0; |
| 6235 | } |
| 6236 | |
| 6237 | __LJMP static int hlua_log_debug(lua_State *L) |
| 6238 | { |
| 6239 | const char *msg; |
| 6240 | |
| 6241 | MAY_LJMP(check_args(L, 1, "debug")); |
| 6242 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6243 | hlua_sendlog(NULL, LOG_DEBUG, msg); |
| 6244 | return 0; |
| 6245 | } |
| 6246 | |
| 6247 | __LJMP static int hlua_log_info(lua_State *L) |
| 6248 | { |
| 6249 | const char *msg; |
| 6250 | |
| 6251 | MAY_LJMP(check_args(L, 1, "info")); |
| 6252 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6253 | hlua_sendlog(NULL, LOG_INFO, msg); |
| 6254 | return 0; |
| 6255 | } |
| 6256 | |
| 6257 | __LJMP static int hlua_log_warning(lua_State *L) |
| 6258 | { |
| 6259 | const char *msg; |
| 6260 | |
| 6261 | MAY_LJMP(check_args(L, 1, "warning")); |
| 6262 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6263 | hlua_sendlog(NULL, LOG_WARNING, msg); |
| 6264 | return 0; |
| 6265 | } |
| 6266 | |
| 6267 | __LJMP static int hlua_log_alert(lua_State *L) |
| 6268 | { |
| 6269 | const char *msg; |
| 6270 | |
| 6271 | MAY_LJMP(check_args(L, 1, "alert")); |
| 6272 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6273 | hlua_sendlog(NULL, LOG_ALERT, msg); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 6274 | return 0; |
| 6275 | } |
| 6276 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 6277 | __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] | 6278 | { |
| 6279 | int wakeup_ms = lua_tointeger(L, -1); |
Willy Tarreau | 9cc2e4c | 2021-09-30 16:12:31 +0200 | [diff] [blame] | 6280 | if (!tick_is_expired(wakeup_ms, now_ms)) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 6281 | 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] | 6282 | return 0; |
| 6283 | } |
| 6284 | |
| 6285 | __LJMP static int hlua_sleep(lua_State *L) |
| 6286 | { |
| 6287 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6288 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6289 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6290 | MAY_LJMP(check_args(L, 1, "sleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6291 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 6292 | delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6293 | wakeup_ms = tick_add(now_ms, delay); |
| 6294 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6295 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 6296 | 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] | 6297 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6298 | } |
| 6299 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6300 | __LJMP static int hlua_msleep(lua_State *L) |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6301 | { |
| 6302 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6303 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6304 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6305 | MAY_LJMP(check_args(L, 1, "msleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6306 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 6307 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6308 | wakeup_ms = tick_add(now_ms, delay); |
| 6309 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6310 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 6311 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0)); |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6312 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6313 | } |
| 6314 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 6315 | /* This functionis an LUA binding. it permits to give back |
| 6316 | * the hand at the HAProxy scheduler. It is used when the |
| 6317 | * LUA processing consumes a lot of time. |
| 6318 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 6319 | __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] | 6320 | { |
| 6321 | return 0; |
| 6322 | } |
| 6323 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 6324 | __LJMP static int hlua_yield(lua_State *L) |
| 6325 | { |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 6326 | 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] | 6327 | return 0; |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 6328 | } |
| 6329 | |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6330 | /* This function change the nice of the currently executed |
| 6331 | * task. It is used set low or high priority at the current |
| 6332 | * task. |
| 6333 | */ |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 6334 | __LJMP static int hlua_set_nice(lua_State *L) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6335 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6336 | struct hlua *hlua; |
| 6337 | int nice; |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6338 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6339 | MAY_LJMP(check_args(L, 1, "set_nice")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6340 | nice = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6341 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 6342 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 6343 | hlua = hlua_gethlua(L); |
| 6344 | |
| 6345 | /* If the task is not set, I'm in a start mode. */ |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6346 | if (!hlua || !hlua->task) |
| 6347 | return 0; |
| 6348 | |
| 6349 | if (nice < -1024) |
| 6350 | nice = -1024; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6351 | else if (nice > 1024) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6352 | nice = 1024; |
| 6353 | |
| 6354 | hlua->task->nice = nice; |
| 6355 | return 0; |
| 6356 | } |
| 6357 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6358 | /* 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] | 6359 | * HAProxy task subsystem when the task is awaked. The LUA runtime can |
| 6360 | * return an E_AGAIN signal, the emmiter of this signal must set a |
| 6361 | * signal to wake the task. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6362 | * |
| 6363 | * Task wrapper are longjmp safe because the only one Lua code |
| 6364 | * executed is the safe hlua_ctx_resume(); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6365 | */ |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 6366 | 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] | 6367 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 6368 | struct hlua *hlua = context; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6369 | enum hlua_exec status; |
| 6370 | |
Christopher Faulet | 5bc9972 | 2018-04-25 10:34:45 +0200 | [diff] [blame] | 6371 | if (task->thread_mask == MAX_THREADS_MASK) |
| 6372 | task_set_affinity(task, tid_bit); |
| 6373 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6374 | /* If it is the first call to the task, we must initialize the |
| 6375 | * execution timeouts. |
| 6376 | */ |
| 6377 | if (!HLUA_IS_RUNNING(hlua)) |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 6378 | hlua->max_time = hlua_timeout_task; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6379 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6380 | /* Execute the Lua code. */ |
| 6381 | status = hlua_ctx_resume(hlua, 1); |
| 6382 | |
| 6383 | switch (status) { |
| 6384 | /* finished or yield */ |
| 6385 | case HLUA_E_OK: |
| 6386 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 6387 | task_destroy(task); |
Tim Duesterhus | cd235c6 | 2018-04-24 13:56:01 +0200 | [diff] [blame] | 6388 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6389 | break; |
| 6390 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6391 | case HLUA_E_AGAIN: /* co process or timeout wake me later. */ |
Thierry FOURNIER | cb14688 | 2017-12-10 17:10:57 +0100 | [diff] [blame] | 6392 | notification_gc(&hlua->com); |
PiBa-NL | fe971b3 | 2018-05-02 22:27:14 +0200 | [diff] [blame] | 6393 | task->expire = hlua->wake_time; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6394 | break; |
| 6395 | |
| 6396 | /* finished with error. */ |
| 6397 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6398 | SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1)); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6399 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 6400 | task_destroy(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 6401 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6402 | break; |
| 6403 | |
| 6404 | case HLUA_E_ERR: |
| 6405 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6406 | SEND_ERR(NULL, "Lua task: unknown error.\n"); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6407 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 6408 | task_destroy(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 6409 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6410 | break; |
| 6411 | } |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 6412 | return task; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6413 | } |
| 6414 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6415 | /* This function is an LUA binding that register LUA function to be |
| 6416 | * executed after the HAProxy configuration parsing and before the |
| 6417 | * HAProxy scheduler starts. This function expect only one LUA |
| 6418 | * argument that is a function. This function returns nothing, but |
| 6419 | * throws if an error is encountered. |
| 6420 | */ |
| 6421 | __LJMP static int hlua_register_init(lua_State *L) |
| 6422 | { |
| 6423 | struct hlua_init_function *init; |
| 6424 | int ref; |
| 6425 | |
| 6426 | MAY_LJMP(check_args(L, 1, "register_init")); |
| 6427 | |
Aurelien DARRAGON | 8985ab8 | 2023-02-24 09:43:54 +0100 | [diff] [blame] | 6428 | if (hlua_gethlua(L)) { |
| 6429 | /* runtime processing */ |
| 6430 | WILL_LJMP(luaL_error(L, "register_init: not available outside of body context")); |
| 6431 | } |
| 6432 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6433 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 6434 | |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6435 | init = calloc(1, sizeof(*init)); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6436 | if (!init) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6437 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6438 | |
| 6439 | init->function_ref = ref; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 6440 | LIST_APPEND(&hlua_init_functions[hlua_state_id], &init->l); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6441 | return 0; |
| 6442 | } |
| 6443 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6444 | /* This functio is an LUA binding. It permits to register a task |
| 6445 | * executed in parallel of the main HAroxy activity. The task is |
| 6446 | * created and it is set in the HAProxy scheduler. It can be called |
| 6447 | * from the "init" section, "post init" or during the runtime. |
| 6448 | * |
| 6449 | * Lua prototype: |
| 6450 | * |
| 6451 | * <none> core.register_task(<function>) |
| 6452 | */ |
| 6453 | static int hlua_register_task(lua_State *L) |
| 6454 | { |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 6455 | struct hlua *hlua = NULL; |
| 6456 | struct task *task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6457 | int ref; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 6458 | int state_id; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6459 | |
| 6460 | MAY_LJMP(check_args(L, 1, "register_task")); |
| 6461 | |
| 6462 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 6463 | |
Thierry Fournier | 75fc029 | 2020-11-28 13:18:56 +0100 | [diff] [blame] | 6464 | /* Get the reference state. If the reference is NULL, L is the master |
| 6465 | * state, otherwise hlua->T is. |
| 6466 | */ |
| 6467 | hlua = hlua_gethlua(L); |
| 6468 | if (hlua) |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 6469 | /* we are in runtime processing */ |
| 6470 | state_id = hlua->state_id; |
Thierry Fournier | 75fc029 | 2020-11-28 13:18:56 +0100 | [diff] [blame] | 6471 | else |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 6472 | /* we are in initialization mode */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 6473 | state_id = hlua_state_id; |
Thierry Fournier | 75fc029 | 2020-11-28 13:18:56 +0100 | [diff] [blame] | 6474 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6475 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6476 | if (!hlua) |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 6477 | goto alloc_error; |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 6478 | HLUA_INIT(hlua); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6479 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 6480 | /* We are in the common lua state, execute the task anywhere, |
| 6481 | * otherwise, inherit the current thread identifier |
| 6482 | */ |
| 6483 | if (state_id == 0) |
| 6484 | task = task_new(MAX_THREADS_MASK); |
| 6485 | else |
| 6486 | task = task_new(tid_bit); |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 6487 | if (!task) |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 6488 | goto alloc_error; |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 6489 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6490 | task->context = hlua; |
| 6491 | task->process = hlua_process_task; |
| 6492 | |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 6493 | if (!hlua_ctx_init(hlua, state_id, task, 1)) |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 6494 | goto alloc_error; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6495 | |
| 6496 | /* Restore the function in the stack. */ |
| 6497 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref); |
Aurelien DARRAGON | b6aade3 | 2023-03-13 14:09:21 +0100 | [diff] [blame] | 6498 | /* function ref not needed anymore since it was pushed to the substack */ |
| 6499 | hlua_unref(L, ref); |
| 6500 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6501 | hlua->nargs = 0; |
| 6502 | |
| 6503 | /* Schedule task. */ |
Willy Tarreau | 790810f | 2021-09-30 16:17:37 +0200 | [diff] [blame] | 6504 | task_wakeup(task, TASK_WOKEN_INIT); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6505 | |
| 6506 | return 0; |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 6507 | |
| 6508 | alloc_error: |
| 6509 | task_destroy(task); |
| 6510 | hlua_ctx_destroy(hlua); |
| 6511 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 6512 | return 0; /* Never reached */ |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6513 | } |
| 6514 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6515 | /* Wrapper called by HAProxy to execute an LUA converter. This wrapper |
| 6516 | * doesn't allow "yield" functions because the HAProxy engine cannot |
| 6517 | * resume converters. |
| 6518 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6519 | 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] | 6520 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 6521 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6522 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6523 | const char *error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6524 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6525 | if (!stream) |
| 6526 | return 0; |
| 6527 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6528 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6529 | * Lua context can be not initialized. This behavior |
| 6530 | * permits to save performances because a systematic |
| 6531 | * Lua initialization cause 5% performances loss. |
| 6532 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6533 | if (!stream->hlua) { |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 6534 | struct hlua *hlua; |
| 6535 | |
| 6536 | hlua = pool_alloc(pool_head_hlua); |
| 6537 | if (!hlua) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6538 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 6539 | return 0; |
| 6540 | } |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 6541 | HLUA_INIT(hlua); |
| 6542 | stream->hlua = hlua; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 6543 | if (!hlua_ctx_init(stream->hlua, fcn_ref_to_stack_id(fcn), stream->task, 0)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6544 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 6545 | return 0; |
| 6546 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6547 | } |
| 6548 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6549 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6550 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6551 | |
| 6552 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 6553 | if (!SET_SAFE_LJMP(stream->hlua)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6554 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 6555 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6556 | else |
| 6557 | error = "critical error"; |
| 6558 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6559 | return 0; |
| 6560 | } |
| 6561 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6562 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6563 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6564 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 6565 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6566 | return 0; |
| 6567 | } |
| 6568 | |
| 6569 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 6570 | lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref[stream->hlua->state_id]); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6571 | |
| 6572 | /* convert input sample and pust-it in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6573 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6574 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 6575 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6576 | return 0; |
| 6577 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6578 | hlua_smp2lua(stream->hlua->T, smp); |
| 6579 | stream->hlua->nargs = 1; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6580 | |
| 6581 | /* push keywords in the stack. */ |
| 6582 | if (arg_p) { |
| 6583 | for (; arg_p->type != ARGT_STOP; arg_p++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6584 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6585 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 6586 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6587 | return 0; |
| 6588 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6589 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 6590 | stream->hlua->nargs++; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6591 | } |
| 6592 | } |
| 6593 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6594 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6595 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6596 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6597 | /* At this point the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 6598 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6599 | } |
| 6600 | |
| 6601 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6602 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6603 | /* finished. */ |
| 6604 | case HLUA_E_OK: |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 6605 | /* If the stack is empty, the function fails. */ |
| 6606 | if (lua_gettop(stream->hlua->T) <= 0) |
| 6607 | return 0; |
| 6608 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6609 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6610 | hlua_lua2smp(stream->hlua->T, -1, smp); |
Aurelien DARRAGON | d39fb74 | 2023-05-17 16:06:11 +0200 | [diff] [blame] | 6611 | /* dup the smp before popping the related lua value and |
| 6612 | * returning it to haproxy |
| 6613 | */ |
| 6614 | smp_dup(smp); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6615 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6616 | return 1; |
| 6617 | |
| 6618 | /* yield. */ |
| 6619 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6620 | 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] | 6621 | return 0; |
| 6622 | |
| 6623 | /* finished with error. */ |
| 6624 | case HLUA_E_ERRMSG: |
| 6625 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6626 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6627 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 6628 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6629 | return 0; |
| 6630 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6631 | case HLUA_E_ETMOUT: |
| 6632 | SEND_ERR(stream->be, "Lua converter '%s': execution timeout.\n", fcn->name); |
| 6633 | return 0; |
| 6634 | |
| 6635 | case HLUA_E_NOMEM: |
| 6636 | SEND_ERR(stream->be, "Lua converter '%s': out of memory error.\n", fcn->name); |
| 6637 | return 0; |
| 6638 | |
| 6639 | case HLUA_E_YIELD: |
| 6640 | SEND_ERR(stream->be, "Lua converter '%s': yield functions like core.tcp() or core.sleep() are not allowed.\n", fcn->name); |
| 6641 | return 0; |
| 6642 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6643 | case HLUA_E_ERR: |
| 6644 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6645 | SEND_ERR(stream->be, "Lua converter '%s' returns an unknown error.\n", fcn->name); |
Tim Duesterhus | 588b314 | 2020-05-29 14:35:51 +0200 | [diff] [blame] | 6646 | /* fall through */ |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6647 | |
| 6648 | default: |
| 6649 | return 0; |
| 6650 | } |
| 6651 | } |
| 6652 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6653 | /* Wrapper called by HAProxy to execute a sample-fetch. this wrapper |
| 6654 | * doesn't allow "yield" functions because the HAProxy engine cannot |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6655 | * resume sample-fetches. This function will be called by the sample |
| 6656 | * fetch engine to call lua-based fetch operations. |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6657 | */ |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6658 | static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp, |
| 6659 | const char *kw, void *private) |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6660 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 6661 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6662 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6663 | const char *error; |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6664 | unsigned int hflags = HLUA_TXN_NOTERM; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6665 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6666 | if (!stream) |
| 6667 | return 0; |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 6668 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6669 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6670 | * Lua context can be not initialized. This behavior |
| 6671 | * permits to save performances because a systematic |
| 6672 | * Lua initialization cause 5% performances loss. |
| 6673 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6674 | if (!stream->hlua) { |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 6675 | struct hlua *hlua; |
| 6676 | |
| 6677 | hlua = pool_alloc(pool_head_hlua); |
| 6678 | if (!hlua) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6679 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 6680 | return 0; |
| 6681 | } |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 6682 | hlua->T = NULL; |
| 6683 | stream->hlua = hlua; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 6684 | if (!hlua_ctx_init(stream->hlua, fcn_ref_to_stack_id(fcn), stream->task, 0)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6685 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 6686 | return 0; |
| 6687 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6688 | } |
| 6689 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6690 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6691 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6692 | |
| 6693 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 6694 | if (!SET_SAFE_LJMP(stream->hlua)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6695 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 6696 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6697 | else |
| 6698 | error = "critical error"; |
| 6699 | 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] | 6700 | return 0; |
| 6701 | } |
| 6702 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6703 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6704 | if (!lua_checkstack(stream->hlua->T, 2)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6705 | 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] | 6706 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6707 | return 0; |
| 6708 | } |
| 6709 | |
| 6710 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 6711 | lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref[stream->hlua->state_id]); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6712 | |
| 6713 | /* push arguments in the stack. */ |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6714 | 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] | 6715 | 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] | 6716 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6717 | return 0; |
| 6718 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6719 | stream->hlua->nargs = 1; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6720 | |
| 6721 | /* push keywords in the stack. */ |
| 6722 | for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) { |
| 6723 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6724 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6725 | 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] | 6726 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6727 | return 0; |
| 6728 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6729 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 6730 | stream->hlua->nargs++; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6731 | } |
| 6732 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6733 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6734 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6735 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6736 | /* At this point the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 6737 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6738 | } |
| 6739 | |
| 6740 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6741 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6742 | /* finished. */ |
| 6743 | case HLUA_E_OK: |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 6744 | /* If the stack is empty, the function fails. */ |
| 6745 | if (lua_gettop(stream->hlua->T) <= 0) |
| 6746 | return 0; |
| 6747 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6748 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6749 | hlua_lua2smp(stream->hlua->T, -1, smp); |
Aurelien DARRAGON | d39fb74 | 2023-05-17 16:06:11 +0200 | [diff] [blame] | 6750 | /* dup the smp before popping the related lua value and |
| 6751 | * returning it to haproxy |
| 6752 | */ |
| 6753 | smp_dup(smp); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6754 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6755 | |
| 6756 | /* Set the end of execution flag. */ |
| 6757 | smp->flags &= ~SMP_F_MAY_CHANGE; |
| 6758 | return 1; |
| 6759 | |
| 6760 | /* yield. */ |
| 6761 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6762 | 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] | 6763 | return 0; |
| 6764 | |
| 6765 | /* finished with error. */ |
| 6766 | case HLUA_E_ERRMSG: |
| 6767 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6768 | SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6769 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 6770 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6771 | return 0; |
| 6772 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6773 | case HLUA_E_ETMOUT: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6774 | SEND_ERR(smp->px, "Lua sample-fetch '%s': execution timeout.\n", fcn->name); |
| 6775 | return 0; |
| 6776 | |
| 6777 | case HLUA_E_NOMEM: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6778 | SEND_ERR(smp->px, "Lua sample-fetch '%s': out of memory error.\n", fcn->name); |
| 6779 | return 0; |
| 6780 | |
| 6781 | case HLUA_E_YIELD: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6782 | SEND_ERR(smp->px, "Lua sample-fetch '%s': yield not allowed.\n", fcn->name); |
| 6783 | return 0; |
| 6784 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6785 | case HLUA_E_ERR: |
| 6786 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6787 | SEND_ERR(smp->px, "Lua sample-fetch '%s' returns an unknown error.\n", fcn->name); |
Tim Duesterhus | 588b314 | 2020-05-29 14:35:51 +0200 | [diff] [blame] | 6788 | /* fall through */ |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6789 | |
| 6790 | default: |
| 6791 | return 0; |
| 6792 | } |
| 6793 | } |
| 6794 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6795 | /* This function is an LUA binding used for registering |
| 6796 | * "sample-conv" functions. It expects a converter name used |
| 6797 | * in the haproxy configuration file, and an LUA function. |
| 6798 | */ |
| 6799 | __LJMP static int hlua_register_converters(lua_State *L) |
| 6800 | { |
| 6801 | struct sample_conv_kw_list *sck; |
| 6802 | const char *name; |
| 6803 | int ref; |
| 6804 | int len; |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 6805 | struct hlua_function *fcn = NULL; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 6806 | struct sample_conv *sc; |
| 6807 | struct buffer *trash; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6808 | |
| 6809 | MAY_LJMP(check_args(L, 2, "register_converters")); |
| 6810 | |
Aurelien DARRAGON | 8985ab8 | 2023-02-24 09:43:54 +0100 | [diff] [blame] | 6811 | if (hlua_gethlua(L)) { |
| 6812 | /* runtime processing */ |
| 6813 | WILL_LJMP(luaL_error(L, "register_converters: not available outside of body context")); |
| 6814 | } |
| 6815 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6816 | /* First argument : converter name. */ |
| 6817 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6818 | |
| 6819 | /* Second argument : lua function. */ |
| 6820 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 6821 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 6822 | /* Check if the converter is already registered */ |
| 6823 | trash = get_trash_chunk(); |
| 6824 | chunk_printf(trash, "lua.%s", name); |
| 6825 | sc = find_sample_conv(trash->area, trash->data); |
| 6826 | if (sc != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 6827 | fcn = sc->private; |
| 6828 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 6829 | ha_warning("Trying to register converter 'lua.%s' more than once. " |
| 6830 | "This will become a hard error in version 2.5.\n", name); |
| 6831 | } |
| 6832 | fcn->function_ref[hlua_state_id] = ref; |
| 6833 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 6834 | } |
| 6835 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6836 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6837 | sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6838 | if (!sck) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 6839 | goto alloc_error; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 6840 | fcn = new_hlua_function(); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6841 | if (!fcn) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 6842 | goto alloc_error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6843 | |
| 6844 | /* Fill fcn. */ |
| 6845 | fcn->name = strdup(name); |
| 6846 | if (!fcn->name) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 6847 | goto alloc_error; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 6848 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6849 | |
| 6850 | /* List head */ |
| 6851 | sck->list.n = sck->list.p = NULL; |
| 6852 | |
| 6853 | /* converter keyword. */ |
| 6854 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6855 | sck->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6856 | if (!sck->kw[0].kw) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 6857 | goto alloc_error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6858 | |
| 6859 | snprintf((char *)sck->kw[0].kw, len, "lua.%s", name); |
| 6860 | sck->kw[0].process = hlua_sample_conv_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 6861 | 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] | 6862 | sck->kw[0].val_args = NULL; |
| 6863 | sck->kw[0].in_type = SMP_T_STR; |
| 6864 | sck->kw[0].out_type = SMP_T_STR; |
| 6865 | sck->kw[0].private = fcn; |
| 6866 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6867 | /* Register this new converter */ |
| 6868 | sample_register_convs(sck); |
| 6869 | |
| 6870 | return 0; |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 6871 | |
| 6872 | alloc_error: |
| 6873 | release_hlua_function(fcn); |
| 6874 | ha_free(&sck); |
| 6875 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 6876 | return 0; /* Never reached */ |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6877 | } |
| 6878 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6879 | /* This function is an LUA binding used for registering |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6880 | * "sample-fetch" functions. It expects a converter name used |
| 6881 | * in the haproxy configuration file, and an LUA function. |
| 6882 | */ |
| 6883 | __LJMP static int hlua_register_fetches(lua_State *L) |
| 6884 | { |
| 6885 | const char *name; |
| 6886 | int ref; |
| 6887 | int len; |
| 6888 | struct sample_fetch_kw_list *sfk; |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 6889 | struct hlua_function *fcn = NULL; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 6890 | struct sample_fetch *sf; |
| 6891 | struct buffer *trash; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6892 | |
| 6893 | MAY_LJMP(check_args(L, 2, "register_fetches")); |
| 6894 | |
Aurelien DARRAGON | 8985ab8 | 2023-02-24 09:43:54 +0100 | [diff] [blame] | 6895 | if (hlua_gethlua(L)) { |
| 6896 | /* runtime processing */ |
| 6897 | WILL_LJMP(luaL_error(L, "register_fetches: not available outside of body context")); |
| 6898 | } |
| 6899 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6900 | /* First argument : sample-fetch name. */ |
| 6901 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6902 | |
| 6903 | /* Second argument : lua function. */ |
| 6904 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 6905 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 6906 | /* Check if the sample-fetch is already registered */ |
| 6907 | trash = get_trash_chunk(); |
| 6908 | chunk_printf(trash, "lua.%s", name); |
| 6909 | sf = find_sample_fetch(trash->area, trash->data); |
| 6910 | if (sf != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 6911 | fcn = sf->private; |
| 6912 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 6913 | ha_warning("Trying to register sample-fetch 'lua.%s' more than once. " |
| 6914 | "This will become a hard error in version 2.5.\n", name); |
| 6915 | } |
| 6916 | fcn->function_ref[hlua_state_id] = ref; |
| 6917 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 6918 | } |
| 6919 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6920 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6921 | sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6922 | if (!sfk) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 6923 | goto alloc_error; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 6924 | fcn = new_hlua_function(); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6925 | if (!fcn) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 6926 | goto alloc_error; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6927 | |
| 6928 | /* Fill fcn. */ |
| 6929 | fcn->name = strdup(name); |
| 6930 | if (!fcn->name) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 6931 | goto alloc_error; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 6932 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6933 | |
| 6934 | /* List head */ |
| 6935 | sfk->list.n = sfk->list.p = NULL; |
| 6936 | |
| 6937 | /* sample-fetch keyword. */ |
| 6938 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6939 | sfk->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6940 | if (!sfk->kw[0].kw) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 6941 | goto alloc_error; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6942 | |
| 6943 | snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name); |
| 6944 | sfk->kw[0].process = hlua_sample_fetch_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 6945 | 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] | 6946 | sfk->kw[0].val_args = NULL; |
| 6947 | sfk->kw[0].out_type = SMP_T_STR; |
| 6948 | sfk->kw[0].use = SMP_USE_HTTP_ANY; |
| 6949 | sfk->kw[0].val = 0; |
| 6950 | sfk->kw[0].private = fcn; |
| 6951 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6952 | /* Register this new fetch. */ |
| 6953 | sample_register_fetches(sfk); |
| 6954 | |
| 6955 | return 0; |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 6956 | |
| 6957 | alloc_error: |
| 6958 | release_hlua_function(fcn); |
| 6959 | ha_free(&sfk); |
| 6960 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 6961 | return 0; /* Never reached */ |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6962 | } |
| 6963 | |
Christopher Faulet | 501465d | 2020-02-26 14:54:16 +0100 | [diff] [blame] | 6964 | /* This function is a lua binding to set the wake_time. |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 6965 | */ |
Christopher Faulet | 501465d | 2020-02-26 14:54:16 +0100 | [diff] [blame] | 6966 | __LJMP static int hlua_set_wake_time(lua_State *L) |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 6967 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 6968 | struct hlua *hlua; |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 6969 | unsigned int delay; |
| 6970 | unsigned int wakeup_ms; |
| 6971 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 6972 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 6973 | hlua = hlua_gethlua(L); |
| 6974 | if (!hlua) { |
| 6975 | return 0; |
| 6976 | } |
| 6977 | |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 6978 | MAY_LJMP(check_args(L, 1, "wake_time")); |
| 6979 | |
| 6980 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 6981 | wakeup_ms = tick_add(now_ms, delay); |
| 6982 | hlua->wake_time = wakeup_ms; |
| 6983 | return 0; |
| 6984 | } |
| 6985 | |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6986 | /* This function is a wrapper to execute each LUA function declared as an action |
| 6987 | * wrapper during the initialisation period. This function may return any |
| 6988 | * ACT_RET_* value. On error ACT_RET_CONT is returned and the action is |
| 6989 | * ignored. If the lua action yields, ACT_RET_YIELD is returned. On success, the |
| 6990 | * return value is the first element on the stack. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6991 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6992 | 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] | 6993 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6994 | { |
| 6995 | char **arg; |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6996 | unsigned int hflags = 0; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6997 | int dir, act_ret = ACT_RET_CONT; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6998 | const char *error; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6999 | |
| 7000 | switch (rule->from) { |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 7001 | case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break; |
| 7002 | case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break; |
| 7003 | case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break; |
| 7004 | case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7005 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 7006 | SEND_ERR(px, "Lua: internal error while execute action.\n"); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7007 | goto end; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7008 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7009 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7010 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 7011 | * Lua context can be not initialized. This behavior |
| 7012 | * permits to save performances because a systematic |
| 7013 | * Lua initialization cause 5% performances loss. |
| 7014 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 7015 | if (!s->hlua) { |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 7016 | struct hlua *hlua; |
| 7017 | |
| 7018 | hlua = pool_alloc(pool_head_hlua); |
| 7019 | if (!hlua) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 7020 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7021 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7022 | goto end; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 7023 | } |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 7024 | HLUA_INIT(hlua); |
| 7025 | s->hlua = hlua; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 7026 | if (!hlua_ctx_init(s->hlua, fcn_ref_to_stack_id(rule->arg.hlua_rule->fcn), s->task, 0)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 7027 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7028 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7029 | goto end; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 7030 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 7031 | } |
| 7032 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7033 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 7034 | if (!HLUA_IS_RUNNING(s->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7035 | |
| 7036 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7037 | if (!SET_SAFE_LJMP(s->hlua)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 7038 | if (lua_type(s->hlua->T, -1) == LUA_TSTRING) |
| 7039 | error = lua_tostring(s->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7040 | else |
| 7041 | error = "critical error"; |
| 7042 | SEND_ERR(px, "Lua function '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7043 | rule->arg.hlua_rule->fcn->name, error); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7044 | goto end; |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7045 | } |
| 7046 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7047 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 7048 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 7049 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7050 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7051 | RESET_SAFE_LJMP(s->hlua); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7052 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7053 | } |
| 7054 | |
| 7055 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 7056 | lua_rawgeti(s->hlua->T, LUA_REGISTRYINDEX, rule->arg.hlua_rule->fcn->function_ref[s->hlua->state_id]); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7057 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7058 | /* Create and and push object stream in the stack. */ |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 7059 | if (!hlua_txn_new(s->hlua->T, s, px, dir, hflags)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 7060 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7061 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7062 | RESET_SAFE_LJMP(s->hlua); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7063 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7064 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 7065 | s->hlua->nargs = 1; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7066 | |
| 7067 | /* push keywords in the stack. */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7068 | for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 7069 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 7070 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7071 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7072 | RESET_SAFE_LJMP(s->hlua); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7073 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7074 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 7075 | lua_pushstring(s->hlua->T, *arg); |
| 7076 | s->hlua->nargs++; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7077 | } |
| 7078 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7079 | /* Now the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7080 | RESET_SAFE_LJMP(s->hlua); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7081 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 7082 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 7083 | s->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7084 | } |
| 7085 | |
| 7086 | /* Execute the function. */ |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 7087 | switch (hlua_ctx_resume(s->hlua, !(flags & ACT_OPT_FINAL))) { |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7088 | /* finished. */ |
| 7089 | case HLUA_E_OK: |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7090 | /* Catch the return value */ |
| 7091 | if (lua_gettop(s->hlua->T) > 0) |
| 7092 | act_ret = lua_tointeger(s->hlua->T, -1); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7093 | |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 7094 | /* Set timeout in the required channel. */ |
Christopher Faulet | 498c483 | 2020-07-28 11:59:58 +0200 | [diff] [blame] | 7095 | if (act_ret == ACT_RET_YIELD) { |
| 7096 | if (flags & ACT_OPT_FINAL) |
| 7097 | goto err_yield; |
| 7098 | |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 7099 | if (dir == SMP_OPT_DIR_REQ) |
| 7100 | s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp), |
| 7101 | s->hlua->wake_time); |
| 7102 | else |
| 7103 | s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp), |
| 7104 | s->hlua->wake_time); |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 7105 | } |
| 7106 | goto end; |
| 7107 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7108 | /* yield. */ |
| 7109 | case HLUA_E_AGAIN: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 7110 | /* Set timeout in the required channel. */ |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 7111 | if (dir == SMP_OPT_DIR_REQ) |
| 7112 | s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp), |
| 7113 | s->hlua->wake_time); |
| 7114 | else |
| 7115 | s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp), |
| 7116 | s->hlua->wake_time); |
| 7117 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7118 | /* Some actions can be wake up when a "write" event |
| 7119 | * is detected on a response channel. This is useful |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7120 | * only for actions targeted on the requests. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7121 | */ |
Christopher Faulet | 51fa358 | 2019-07-26 14:54:52 +0200 | [diff] [blame] | 7122 | if (HLUA_IS_WAKERESWR(s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 7123 | s->res.flags |= CF_WAKE_WRITE; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 7124 | if (HLUA_IS_WAKEREQWR(s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 7125 | s->req.flags |= CF_WAKE_WRITE; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7126 | act_ret = ACT_RET_YIELD; |
| 7127 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7128 | |
| 7129 | /* finished with error. */ |
| 7130 | case HLUA_E_ERRMSG: |
| 7131 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 7132 | SEND_ERR(px, "Lua function '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7133 | rule->arg.hlua_rule->fcn->name, lua_tostring(s->hlua->T, -1)); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 7134 | lua_pop(s->hlua->T, 1); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7135 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7136 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7137 | case HLUA_E_ETMOUT: |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7138 | 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] | 7139 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7140 | |
| 7141 | case HLUA_E_NOMEM: |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7142 | 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] | 7143 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7144 | |
| 7145 | case HLUA_E_YIELD: |
Christopher Faulet | 498c483 | 2020-07-28 11:59:58 +0200 | [diff] [blame] | 7146 | err_yield: |
| 7147 | act_ret = ACT_RET_CONT; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7148 | 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] | 7149 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7150 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7151 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7152 | case HLUA_E_ERR: |
| 7153 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 7154 | SEND_ERR(px, "Lua function '%s' return an unknown error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7155 | rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7156 | |
| 7157 | default: |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7158 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7159 | } |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7160 | |
| 7161 | end: |
Christopher Faulet | 2361fd9 | 2020-07-30 10:40:58 +0200 | [diff] [blame] | 7162 | if (act_ret != ACT_RET_YIELD && s->hlua) |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 7163 | s->hlua->wake_time = TICK_ETERNITY; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7164 | return act_ret; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7165 | } |
| 7166 | |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 7167 | 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] | 7168 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 7169 | struct appctx *ctx = context; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7170 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7171 | appctx_wakeup(ctx); |
Willy Tarreau | d958741 | 2017-08-23 16:07:33 +0200 | [diff] [blame] | 7172 | t->expire = TICK_ETERNITY; |
Willy Tarreau | d1aa41f | 2017-07-21 16:41:56 +0200 | [diff] [blame] | 7173 | return t; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7174 | } |
| 7175 | |
| 7176 | static int hlua_applet_tcp_init(struct appctx *ctx, struct proxy *px, struct stream *strm) |
| 7177 | { |
| 7178 | struct stream_interface *si = ctx->owner; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7179 | struct hlua *hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7180 | struct task *task; |
| 7181 | char **arg; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7182 | const char *error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7183 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7184 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7185 | if (!hlua) { |
| 7186 | SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7187 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7188 | return 0; |
| 7189 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7190 | HLUA_INIT(hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7191 | ctx->ctx.hlua_apptcp.hlua = hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7192 | ctx->ctx.hlua_apptcp.flags = 0; |
| 7193 | |
| 7194 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 7195 | task = task_new(tid_bit); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7196 | if (!task) { |
| 7197 | SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7198 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7199 | return 0; |
| 7200 | } |
| 7201 | task->nice = 0; |
| 7202 | task->context = ctx; |
| 7203 | task->process = hlua_applet_wakeup; |
| 7204 | ctx->ctx.hlua_apptcp.task = task; |
| 7205 | |
| 7206 | /* In the execution wrappers linked with a stream, the |
| 7207 | * Lua context can be not initialized. This behavior |
| 7208 | * permits to save performances because a systematic |
| 7209 | * Lua initialization cause 5% performances loss. |
| 7210 | */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 7211 | if (!hlua_ctx_init(hlua, fcn_ref_to_stack_id(ctx->rule->arg.hlua_rule->fcn), task, 0)) { |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7212 | SEND_ERR(px, "Lua applet tcp '%s': can't initialize Lua context.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7213 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7214 | return 0; |
| 7215 | } |
| 7216 | |
| 7217 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 7218 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7219 | |
| 7220 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7221 | if (!SET_SAFE_LJMP(hlua)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7222 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 7223 | error = lua_tostring(hlua->T, -1); |
| 7224 | else |
| 7225 | error = "critical error"; |
| 7226 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7227 | ctx->rule->arg.hlua_rule->fcn->name, error); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7228 | return 0; |
| 7229 | } |
| 7230 | |
| 7231 | /* Check stack available size. */ |
| 7232 | if (!lua_checkstack(hlua->T, 1)) { |
| 7233 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7234 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7235 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7236 | return 0; |
| 7237 | } |
| 7238 | |
| 7239 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 7240 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn->function_ref[hlua->state_id]); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7241 | |
| 7242 | /* Create and and push object stream in the stack. */ |
| 7243 | if (!hlua_applet_tcp_new(hlua->T, ctx)) { |
| 7244 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7245 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7246 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7247 | return 0; |
| 7248 | } |
| 7249 | hlua->nargs = 1; |
| 7250 | |
| 7251 | /* push keywords in the stack. */ |
| 7252 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 7253 | if (!lua_checkstack(hlua->T, 1)) { |
| 7254 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7255 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7256 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7257 | return 0; |
| 7258 | } |
| 7259 | lua_pushstring(hlua->T, *arg); |
| 7260 | hlua->nargs++; |
| 7261 | } |
| 7262 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7263 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7264 | |
| 7265 | /* Wakeup the applet ASAP. */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 7266 | si_cant_get(si); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 7267 | si_rx_endp_more(si); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7268 | |
| 7269 | return 1; |
| 7270 | } |
| 7271 | |
Willy Tarreau | 60409db | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 7272 | void hlua_applet_tcp_fct(struct appctx *ctx) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7273 | { |
| 7274 | struct stream_interface *si = ctx->owner; |
| 7275 | struct stream *strm = si_strm(si); |
| 7276 | struct channel *res = si_ic(si); |
| 7277 | struct act_rule *rule = ctx->rule; |
| 7278 | struct proxy *px = strm->be; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7279 | struct hlua *hlua = ctx->ctx.hlua_apptcp.hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7280 | |
| 7281 | /* The applet execution is already done. */ |
Olivier Houchard | 594c8c5 | 2018-08-28 14:41:31 +0200 | [diff] [blame] | 7282 | if (ctx->ctx.hlua_apptcp.flags & APPLET_DONE) { |
| 7283 | /* eat the whole request */ |
| 7284 | co_skip(si_oc(si), co_data(si_oc(si))); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7285 | return; |
Olivier Houchard | 594c8c5 | 2018-08-28 14:41:31 +0200 | [diff] [blame] | 7286 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7287 | |
| 7288 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 7289 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 7290 | return; |
| 7291 | |
| 7292 | /* Execute the function. */ |
| 7293 | switch (hlua_ctx_resume(hlua, 1)) { |
| 7294 | /* finished. */ |
| 7295 | case HLUA_E_OK: |
| 7296 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 7297 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7298 | /* eat the whole request */ |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 7299 | co_skip(si_oc(si), co_data(si_oc(si))); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7300 | res->flags |= CF_READ_NULL; |
| 7301 | si_shutr(si); |
| 7302 | return; |
| 7303 | |
| 7304 | /* yield. */ |
| 7305 | case HLUA_E_AGAIN: |
Thierry Fournier | 0164f20 | 2016-02-20 17:47:43 +0100 | [diff] [blame] | 7306 | if (hlua->wake_time != TICK_ETERNITY) |
| 7307 | task_schedule(ctx->ctx.hlua_apptcp.task, hlua->wake_time); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7308 | return; |
| 7309 | |
| 7310 | /* finished with error. */ |
| 7311 | case HLUA_E_ERRMSG: |
| 7312 | /* Display log. */ |
| 7313 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7314 | rule->arg.hlua_rule->fcn->name, lua_tostring(hlua->T, -1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7315 | lua_pop(hlua->T, 1); |
| 7316 | goto error; |
| 7317 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7318 | case HLUA_E_ETMOUT: |
| 7319 | SEND_ERR(px, "Lua applet tcp '%s': execution timeout.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7320 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7321 | goto error; |
| 7322 | |
| 7323 | case HLUA_E_NOMEM: |
| 7324 | SEND_ERR(px, "Lua applet tcp '%s': out of memory error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7325 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7326 | goto error; |
| 7327 | |
| 7328 | case HLUA_E_YIELD: /* unexpected */ |
| 7329 | SEND_ERR(px, "Lua applet tcp '%s': yield not allowed.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7330 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7331 | goto error; |
| 7332 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7333 | case HLUA_E_ERR: |
| 7334 | /* Display log. */ |
| 7335 | SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7336 | rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7337 | goto error; |
| 7338 | |
| 7339 | default: |
| 7340 | goto error; |
| 7341 | } |
| 7342 | |
| 7343 | error: |
| 7344 | |
| 7345 | /* For all other cases, just close the stream. */ |
| 7346 | si_shutw(si); |
| 7347 | si_shutr(si); |
| 7348 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 7349 | } |
| 7350 | |
| 7351 | static void hlua_applet_tcp_release(struct appctx *ctx) |
| 7352 | { |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 7353 | task_destroy(ctx->ctx.hlua_apptcp.task); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7354 | ctx->ctx.hlua_apptcp.task = NULL; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7355 | hlua_ctx_destroy(ctx->ctx.hlua_apptcp.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7356 | ctx->ctx.hlua_apptcp.hlua = NULL; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7357 | } |
| 7358 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7359 | /* The function returns 1 if the initialisation is complete, 0 if |
| 7360 | * an errors occurs and -1 if more data are required for initializing |
| 7361 | * the applet. |
| 7362 | */ |
| 7363 | static int hlua_applet_http_init(struct appctx *ctx, struct proxy *px, struct stream *strm) |
| 7364 | { |
| 7365 | struct stream_interface *si = ctx->owner; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7366 | struct http_txn *txn; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7367 | struct hlua *hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7368 | char **arg; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7369 | struct task *task; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7370 | const char *error; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7371 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7372 | txn = strm->txn; |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7373 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7374 | if (!hlua) { |
| 7375 | SEND_ERR(px, "Lua applet http '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7376 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7377 | return 0; |
| 7378 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7379 | HLUA_INIT(hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7380 | ctx->ctx.hlua_apphttp.hlua = hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7381 | ctx->ctx.hlua_apphttp.left_bytes = -1; |
| 7382 | ctx->ctx.hlua_apphttp.flags = 0; |
| 7383 | |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 7384 | if (txn->req.flags & HTTP_MSGF_VER_11) |
| 7385 | ctx->ctx.hlua_apphttp.flags |= APPLET_HTTP11; |
| 7386 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7387 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 7388 | task = task_new(tid_bit); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7389 | if (!task) { |
| 7390 | SEND_ERR(px, "Lua applet http '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7391 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7392 | return 0; |
| 7393 | } |
| 7394 | task->nice = 0; |
| 7395 | task->context = ctx; |
| 7396 | task->process = hlua_applet_wakeup; |
| 7397 | ctx->ctx.hlua_apphttp.task = task; |
| 7398 | |
| 7399 | /* In the execution wrappers linked with a stream, the |
| 7400 | * Lua context can be not initialized. This behavior |
| 7401 | * permits to save performances because a systematic |
| 7402 | * Lua initialization cause 5% performances loss. |
| 7403 | */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 7404 | if (!hlua_ctx_init(hlua, fcn_ref_to_stack_id(ctx->rule->arg.hlua_rule->fcn), task, 0)) { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7405 | SEND_ERR(px, "Lua applet http '%s': can't initialize Lua context.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7406 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7407 | return 0; |
| 7408 | } |
| 7409 | |
| 7410 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 7411 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7412 | |
| 7413 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7414 | if (!SET_SAFE_LJMP(hlua)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7415 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 7416 | error = lua_tostring(hlua->T, -1); |
| 7417 | else |
| 7418 | error = "critical error"; |
| 7419 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7420 | ctx->rule->arg.hlua_rule->fcn->name, error); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7421 | return 0; |
| 7422 | } |
| 7423 | |
| 7424 | /* Check stack available size. */ |
| 7425 | if (!lua_checkstack(hlua->T, 1)) { |
| 7426 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7427 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7428 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7429 | return 0; |
| 7430 | } |
| 7431 | |
| 7432 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 7433 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn->function_ref[hlua->state_id]); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7434 | |
| 7435 | /* Create and and push object stream in the stack. */ |
| 7436 | if (!hlua_applet_http_new(hlua->T, ctx)) { |
| 7437 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7438 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7439 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7440 | return 0; |
| 7441 | } |
| 7442 | hlua->nargs = 1; |
| 7443 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7444 | /* push keywords in the stack. */ |
| 7445 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 7446 | if (!lua_checkstack(hlua->T, 1)) { |
| 7447 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7448 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7449 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7450 | return 0; |
| 7451 | } |
| 7452 | lua_pushstring(hlua->T, *arg); |
| 7453 | hlua->nargs++; |
| 7454 | } |
| 7455 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7456 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7457 | |
| 7458 | /* Wakeup the applet when data is ready for read. */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 7459 | si_cant_get(si); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7460 | |
| 7461 | return 1; |
| 7462 | } |
| 7463 | |
Willy Tarreau | 60409db | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 7464 | void hlua_applet_http_fct(struct appctx *ctx) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7465 | { |
| 7466 | struct stream_interface *si = ctx->owner; |
| 7467 | struct stream *strm = si_strm(si); |
| 7468 | struct channel *req = si_oc(si); |
| 7469 | struct channel *res = si_ic(si); |
| 7470 | struct act_rule *rule = ctx->rule; |
| 7471 | struct proxy *px = strm->be; |
| 7472 | struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua; |
| 7473 | struct htx *req_htx, *res_htx; |
| 7474 | |
| 7475 | res_htx = htx_from_buf(&res->buf); |
| 7476 | |
| 7477 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 7478 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 7479 | goto out; |
| 7480 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 7481 | /* Check if the input buffer is available. */ |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7482 | if (!b_size(&res->buf)) { |
| 7483 | si_rx_room_blk(si); |
| 7484 | goto out; |
| 7485 | } |
| 7486 | /* check that the output is not closed */ |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7487 | if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR)) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7488 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 7489 | |
| 7490 | /* Set the currently running flag. */ |
| 7491 | if (!HLUA_IS_RUNNING(hlua) && |
| 7492 | !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
Christopher Faulet | bd878d2 | 2021-04-28 10:50:21 +0200 | [diff] [blame] | 7493 | if (!co_data(req)) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7494 | si_cant_get(si); |
| 7495 | goto out; |
| 7496 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7497 | } |
| 7498 | |
| 7499 | /* Executes The applet if it is not done. */ |
| 7500 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
| 7501 | |
| 7502 | /* Execute the function. */ |
| 7503 | switch (hlua_ctx_resume(hlua, 1)) { |
| 7504 | /* finished. */ |
| 7505 | case HLUA_E_OK: |
| 7506 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 7507 | break; |
| 7508 | |
| 7509 | /* yield. */ |
| 7510 | case HLUA_E_AGAIN: |
| 7511 | if (hlua->wake_time != TICK_ETERNITY) |
| 7512 | task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time); |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7513 | goto out; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7514 | |
| 7515 | /* finished with error. */ |
| 7516 | case HLUA_E_ERRMSG: |
| 7517 | /* Display log. */ |
| 7518 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7519 | rule->arg.hlua_rule->fcn->name, lua_tostring(hlua->T, -1)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7520 | lua_pop(hlua->T, 1); |
| 7521 | goto error; |
| 7522 | |
| 7523 | case HLUA_E_ETMOUT: |
| 7524 | SEND_ERR(px, "Lua applet http '%s': execution timeout.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7525 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7526 | goto error; |
| 7527 | |
| 7528 | case HLUA_E_NOMEM: |
| 7529 | SEND_ERR(px, "Lua applet http '%s': out of memory error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7530 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7531 | goto error; |
| 7532 | |
| 7533 | case HLUA_E_YIELD: /* unexpected */ |
| 7534 | SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7535 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7536 | goto error; |
| 7537 | |
| 7538 | case HLUA_E_ERR: |
| 7539 | /* Display log. */ |
| 7540 | SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7541 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7542 | goto error; |
| 7543 | |
| 7544 | default: |
| 7545 | goto error; |
| 7546 | } |
| 7547 | } |
| 7548 | |
| 7549 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7550 | if (ctx->ctx.hlua_apphttp.flags & APPLET_RSP_SENT) |
| 7551 | goto done; |
| 7552 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7553 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) |
| 7554 | goto error; |
| 7555 | |
Christopher Faulet | 868b3b1 | 2022-04-07 10:07:18 +0200 | [diff] [blame] | 7556 | /* no more data are expected. If the response buffer is empty |
| 7557 | * for a chunked message, be sure to add something (EOT block in |
| 7558 | * this case) to have something to send. It is important to be |
| 7559 | * sure the EOM flags will be handled by the endpoint. |
| 7560 | */ |
| 7561 | if (htx_is_empty(res_htx) && (strm->txn->rsp.flags & (HTTP_MSGF_XFER_LEN|HTTP_MSGF_CNT_LEN)) == HTTP_MSGF_XFER_LEN) { |
| 7562 | if (!htx_add_endof(res_htx, HTX_BLK_EOT)) { |
| 7563 | si_rx_room_blk(si); |
| 7564 | goto out; |
| 7565 | } |
| 7566 | channel_add_input(res, 1); |
| 7567 | } |
| 7568 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 7569 | res_htx->flags |= HTX_FL_EOM; |
Christopher Faulet | 79c0fc7 | 2022-03-07 15:50:54 +0100 | [diff] [blame] | 7570 | res->flags |= CF_EOI; |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7571 | strm->txn->status = ctx->ctx.hlua_apphttp.status; |
| 7572 | ctx->ctx.hlua_apphttp.flags |= APPLET_RSP_SENT; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7573 | } |
| 7574 | |
| 7575 | done: |
| 7576 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7577 | if (!(res->flags & CF_SHUTR)) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7578 | res->flags |= CF_READ_NULL; |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7579 | si_shutr(si); |
| 7580 | } |
| 7581 | |
| 7582 | /* eat the whole request */ |
| 7583 | if (co_data(req)) { |
| 7584 | req_htx = htx_from_buf(&req->buf); |
| 7585 | co_htx_skip(req, req_htx, co_data(req)); |
| 7586 | htx_to_buf(req_htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7587 | } |
| 7588 | } |
| 7589 | |
| 7590 | out: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7591 | htx_to_buf(res_htx, &res->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7592 | return; |
| 7593 | |
| 7594 | error: |
| 7595 | |
| 7596 | /* If we are in HTTP mode, and we are not send any |
| 7597 | * data, return a 500 server error in best effort: |
| 7598 | * if there is no room available in the buffer, |
| 7599 | * just close the connection. |
| 7600 | */ |
| 7601 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) { |
Christopher Faulet | f734638 | 2019-07-17 22:02:08 +0200 | [diff] [blame] | 7602 | struct buffer *err = &http_err_chunks[HTTP_ERR_500]; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7603 | |
| 7604 | channel_erase(res); |
| 7605 | res->buf.data = b_data(err); |
| 7606 | memcpy(res->buf.area, b_head(err), b_data(err)); |
| 7607 | res_htx = htx_from_buf(&res->buf); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 7608 | channel_add_input(res, res_htx->data); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7609 | } |
| 7610 | if (!(strm->flags & SF_ERR_MASK)) |
| 7611 | strm->flags |= SF_ERR_RESOURCE; |
| 7612 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 7613 | goto done; |
| 7614 | } |
| 7615 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7616 | static void hlua_applet_http_release(struct appctx *ctx) |
| 7617 | { |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 7618 | task_destroy(ctx->ctx.hlua_apphttp.task); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7619 | ctx->ctx.hlua_apphttp.task = NULL; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7620 | hlua_ctx_destroy(ctx->ctx.hlua_apphttp.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7621 | ctx->ctx.hlua_apphttp.hlua = NULL; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7622 | } |
| 7623 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7624 | /* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 7625 | * success case, else return ACT_RET_PRS_ERR. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7626 | * |
| 7627 | * This function can fail with an abort() due to an Lua critical error. |
| 7628 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 7629 | * tolerated. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7630 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7631 | static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px, |
| 7632 | struct act_rule *rule, char **err) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7633 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7634 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7635 | int i; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7636 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7637 | /* Memory for the rule. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7638 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7639 | if (!rule->arg.hlua_rule) { |
| 7640 | memprintf(err, "out of memory error"); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 7641 | goto error; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7642 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7643 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7644 | /* Memory for arguments. */ |
Tim Duesterhus | e52b6e5 | 2020-09-12 20:26:43 +0200 | [diff] [blame] | 7645 | rule->arg.hlua_rule->args = calloc(fcn->nargs + 1, |
| 7646 | sizeof(*rule->arg.hlua_rule->args)); |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7647 | if (!rule->arg.hlua_rule->args) { |
| 7648 | memprintf(err, "out of memory error"); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 7649 | goto error; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7650 | } |
| 7651 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7652 | /* Reference the Lua function and store the reference. */ |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7653 | rule->arg.hlua_rule->fcn = fcn; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7654 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7655 | /* Expect some arguments */ |
| 7656 | for (i = 0; i < fcn->nargs; i++) { |
Thierry FOURNIER | 1725c2e | 2019-01-06 19:38:49 +0100 | [diff] [blame] | 7657 | if (*args[*cur_arg] == '\0') { |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7658 | memprintf(err, "expect %d arguments", fcn->nargs); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 7659 | goto error; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7660 | } |
Thierry FOURNIER | 1725c2e | 2019-01-06 19:38:49 +0100 | [diff] [blame] | 7661 | rule->arg.hlua_rule->args[i] = strdup(args[*cur_arg]); |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7662 | if (!rule->arg.hlua_rule->args[i]) { |
| 7663 | memprintf(err, "out of memory error"); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 7664 | goto error; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7665 | } |
| 7666 | (*cur_arg)++; |
| 7667 | } |
| 7668 | rule->arg.hlua_rule->args[i] = NULL; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7669 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 7670 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7671 | rule->action_ptr = hlua_action; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 7672 | return ACT_RET_PRS_OK; |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 7673 | |
| 7674 | error: |
| 7675 | if (rule->arg.hlua_rule) { |
| 7676 | if (rule->arg.hlua_rule->args) { |
| 7677 | for (i = 0; i < fcn->nargs; i++) |
| 7678 | ha_free(&rule->arg.hlua_rule->args[i]); |
| 7679 | ha_free(&rule->arg.hlua_rule->args); |
| 7680 | } |
| 7681 | ha_free(&rule->arg.hlua_rule); |
| 7682 | } |
| 7683 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7684 | } |
| 7685 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7686 | static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px, |
| 7687 | struct act_rule *rule, char **err) |
| 7688 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7689 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7690 | |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 7691 | /* HTTP applets are forbidden in tcp-request rules. |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 7692 | * HTTP applet request requires everything initialized by |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 7693 | * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER). |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 7694 | * The applet will be immediately initialized, but its before |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 7695 | * the call of this analyzer. |
| 7696 | */ |
| 7697 | if (rule->from != ACT_F_HTTP_REQ) { |
| 7698 | memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets"); |
| 7699 | return ACT_RET_PRS_ERR; |
| 7700 | } |
| 7701 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7702 | /* Memory for the rule. */ |
| 7703 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 7704 | if (!rule->arg.hlua_rule) { |
| 7705 | memprintf(err, "out of memory error"); |
| 7706 | return ACT_RET_PRS_ERR; |
| 7707 | } |
| 7708 | |
| 7709 | /* Reference the Lua function and store the reference. */ |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7710 | rule->arg.hlua_rule->fcn = fcn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7711 | |
| 7712 | /* TODO: later accept arguments. */ |
| 7713 | rule->arg.hlua_rule->args = NULL; |
| 7714 | |
| 7715 | /* Add applet pointer in the rule. */ |
| 7716 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 7717 | rule->applet.name = fcn->name; |
| 7718 | rule->applet.init = hlua_applet_http_init; |
| 7719 | rule->applet.fct = hlua_applet_http_fct; |
| 7720 | rule->applet.release = hlua_applet_http_release; |
| 7721 | rule->applet.timeout = hlua_timeout_applet; |
| 7722 | |
| 7723 | return ACT_RET_PRS_OK; |
| 7724 | } |
| 7725 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7726 | /* This function is an LUA binding used for registering |
| 7727 | * "sample-conv" functions. It expects a converter name used |
| 7728 | * in the haproxy configuration file, and an LUA function. |
| 7729 | */ |
| 7730 | __LJMP static int hlua_register_action(lua_State *L) |
| 7731 | { |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 7732 | struct action_kw_list *akl = NULL; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7733 | const char *name; |
| 7734 | int ref; |
| 7735 | int len; |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 7736 | struct hlua_function *fcn = NULL; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7737 | int nargs; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 7738 | struct buffer *trash; |
| 7739 | struct action_kw *akw; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7740 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7741 | /* Initialise the number of expected arguments at 0. */ |
| 7742 | nargs = 0; |
| 7743 | |
| 7744 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 7745 | 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] | 7746 | |
Aurelien DARRAGON | 8985ab8 | 2023-02-24 09:43:54 +0100 | [diff] [blame] | 7747 | if (hlua_gethlua(L)) { |
| 7748 | /* runtime processing */ |
| 7749 | WILL_LJMP(luaL_error(L, "register_action: not available outside of body context")); |
| 7750 | } |
| 7751 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7752 | /* First argument : converter name. */ |
| 7753 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 7754 | |
| 7755 | /* Second argument : environment. */ |
| 7756 | if (lua_type(L, 2) != LUA_TTABLE) |
| 7757 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 7758 | |
| 7759 | /* Third argument : lua function. */ |
| 7760 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 7761 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7762 | /* 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] | 7763 | if (lua_gettop(L) >= 4) |
| 7764 | nargs = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 7765 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7766 | /* browse the second argument as an array. */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7767 | lua_pushnil(L); |
| 7768 | while (lua_next(L, 2) != 0) { |
| 7769 | if (lua_type(L, -1) != LUA_TSTRING) |
| 7770 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 7771 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 7772 | /* Check if action exists */ |
| 7773 | trash = get_trash_chunk(); |
| 7774 | chunk_printf(trash, "lua.%s", name); |
| 7775 | if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) { |
| 7776 | akw = tcp_req_cont_action(trash->area); |
| 7777 | } else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) { |
| 7778 | akw = tcp_res_cont_action(trash->area); |
| 7779 | } else if (strcmp(lua_tostring(L, -1), "http-req") == 0) { |
| 7780 | akw = action_http_req_custom(trash->area); |
| 7781 | } else if (strcmp(lua_tostring(L, -1), "http-res") == 0) { |
| 7782 | akw = action_http_res_custom(trash->area); |
| 7783 | } else { |
| 7784 | akw = NULL; |
| 7785 | } |
| 7786 | if (akw != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 7787 | fcn = akw->private; |
| 7788 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 7789 | ha_warning("Trying to register action 'lua.%s' more than once. " |
| 7790 | "This will become a hard error in version 2.5.\n", name); |
| 7791 | } |
| 7792 | fcn->function_ref[hlua_state_id] = ref; |
| 7793 | |
| 7794 | /* pop the environment string. */ |
| 7795 | lua_pop(L, 1); |
| 7796 | continue; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 7797 | } |
| 7798 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7799 | /* Check required environment. Only accepted "http" or "tcp". */ |
| 7800 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7801 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7802 | if (!akl) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 7803 | goto alloc_error;; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 7804 | fcn = new_hlua_function(); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7805 | if (!fcn) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 7806 | goto alloc_error; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7807 | |
| 7808 | /* Fill fcn. */ |
| 7809 | fcn->name = strdup(name); |
| 7810 | if (!fcn->name) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 7811 | goto alloc_error; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 7812 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7813 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 7814 | /* Set the expected number of arguments. */ |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7815 | fcn->nargs = nargs; |
| 7816 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7817 | /* List head */ |
| 7818 | akl->list.n = akl->list.p = NULL; |
| 7819 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7820 | /* action keyword. */ |
| 7821 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7822 | akl->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7823 | if (!akl->kw[0].kw) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 7824 | goto alloc_error; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7825 | |
| 7826 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 7827 | |
Amaury Denoyelle | e4a617c | 2021-05-06 15:33:09 +0200 | [diff] [blame] | 7828 | akl->kw[0].flags = 0; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7829 | akl->kw[0].private = fcn; |
| 7830 | akl->kw[0].parse = action_register_lua; |
| 7831 | |
| 7832 | /* select the action registering point. */ |
| 7833 | if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) |
| 7834 | tcp_req_cont_keywords_register(akl); |
| 7835 | else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) |
| 7836 | tcp_res_cont_keywords_register(akl); |
| 7837 | else if (strcmp(lua_tostring(L, -1), "http-req") == 0) |
| 7838 | http_req_keywords_register(akl); |
| 7839 | else if (strcmp(lua_tostring(L, -1), "http-res") == 0) |
| 7840 | http_res_keywords_register(akl); |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 7841 | else { |
| 7842 | release_hlua_function(fcn); |
| 7843 | if (akl) |
| 7844 | ha_free((char **)&(akl->kw[0].kw)); |
| 7845 | ha_free(&akl); |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7846 | WILL_LJMP(luaL_error(L, "Lua action environment '%s' is unknown. " |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7847 | "'tcp-req', 'tcp-res', 'http-req' or 'http-res' " |
| 7848 | "are expected.", lua_tostring(L, -1))); |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 7849 | } |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7850 | |
| 7851 | /* pop the environment string. */ |
| 7852 | lua_pop(L, 1); |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 7853 | |
| 7854 | /* reset for next loop */ |
| 7855 | akl = NULL; |
| 7856 | fcn = NULL; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7857 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7858 | return ACT_RET_PRS_OK; |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 7859 | |
| 7860 | alloc_error: |
| 7861 | release_hlua_function(fcn); |
| 7862 | ha_free(&akl); |
| 7863 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 7864 | return 0; /* Never reached */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7865 | } |
| 7866 | |
| 7867 | static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px, |
| 7868 | struct act_rule *rule, char **err) |
| 7869 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7870 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7871 | |
Christopher Faulet | 280f85b | 2019-07-15 15:02:04 +0200 | [diff] [blame] | 7872 | if (px->mode == PR_MODE_HTTP) { |
| 7873 | memprintf(err, "Lua TCP services cannot be used on HTTP proxies"); |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 7874 | return ACT_RET_PRS_ERR; |
| 7875 | } |
| 7876 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7877 | /* Memory for the rule. */ |
| 7878 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 7879 | if (!rule->arg.hlua_rule) { |
| 7880 | memprintf(err, "out of memory error"); |
| 7881 | return ACT_RET_PRS_ERR; |
| 7882 | } |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7883 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7884 | /* Reference the Lua function and store the reference. */ |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7885 | rule->arg.hlua_rule->fcn = fcn; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7886 | |
| 7887 | /* TODO: later accept arguments. */ |
| 7888 | rule->arg.hlua_rule->args = NULL; |
| 7889 | |
| 7890 | /* Add applet pointer in the rule. */ |
| 7891 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 7892 | rule->applet.name = fcn->name; |
| 7893 | rule->applet.init = hlua_applet_tcp_init; |
| 7894 | rule->applet.fct = hlua_applet_tcp_fct; |
| 7895 | rule->applet.release = hlua_applet_tcp_release; |
| 7896 | rule->applet.timeout = hlua_timeout_applet; |
| 7897 | |
| 7898 | return 0; |
| 7899 | } |
| 7900 | |
| 7901 | /* This function is an LUA binding used for registering |
| 7902 | * "sample-conv" functions. It expects a converter name used |
| 7903 | * in the haproxy configuration file, and an LUA function. |
| 7904 | */ |
| 7905 | __LJMP static int hlua_register_service(lua_State *L) |
| 7906 | { |
| 7907 | struct action_kw_list *akl; |
| 7908 | const char *name; |
| 7909 | const char *env; |
| 7910 | int ref; |
| 7911 | int len; |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 7912 | struct hlua_function *fcn = NULL; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 7913 | struct buffer *trash; |
| 7914 | struct action_kw *akw; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7915 | |
| 7916 | MAY_LJMP(check_args(L, 3, "register_service")); |
| 7917 | |
Aurelien DARRAGON | 8985ab8 | 2023-02-24 09:43:54 +0100 | [diff] [blame] | 7918 | if (hlua_gethlua(L)) { |
| 7919 | /* runtime processing */ |
| 7920 | WILL_LJMP(luaL_error(L, "register_service: not available outside of body context")); |
| 7921 | } |
| 7922 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7923 | /* First argument : converter name. */ |
| 7924 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 7925 | |
| 7926 | /* Second argument : environment. */ |
| 7927 | env = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7928 | |
| 7929 | /* Third argument : lua function. */ |
| 7930 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 7931 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 7932 | /* Check for service already registered */ |
| 7933 | trash = get_trash_chunk(); |
| 7934 | chunk_printf(trash, "lua.%s", name); |
| 7935 | akw = service_find(trash->area); |
| 7936 | if (akw != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 7937 | fcn = akw->private; |
| 7938 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 7939 | ha_warning("Trying to register service 'lua.%s' more than once. " |
| 7940 | "This will become a hard error in version 2.5.\n", name); |
| 7941 | } |
| 7942 | fcn->function_ref[hlua_state_id] = ref; |
| 7943 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 7944 | } |
| 7945 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7946 | /* Allocate and fill the sample fetch keyword struct. */ |
| 7947 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
| 7948 | if (!akl) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 7949 | goto alloc_error; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 7950 | fcn = new_hlua_function(); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7951 | if (!fcn) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 7952 | goto alloc_error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7953 | |
| 7954 | /* Fill fcn. */ |
| 7955 | len = strlen("<lua.>") + strlen(name) + 1; |
| 7956 | fcn->name = calloc(1, len); |
| 7957 | if (!fcn->name) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 7958 | goto alloc_error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7959 | snprintf((char *)fcn->name, len, "<lua.%s>", name); |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 7960 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7961 | |
| 7962 | /* List head */ |
| 7963 | akl->list.n = akl->list.p = NULL; |
| 7964 | |
| 7965 | /* converter keyword. */ |
| 7966 | len = strlen("lua.") + strlen(name) + 1; |
| 7967 | akl->kw[0].kw = calloc(1, len); |
| 7968 | if (!akl->kw[0].kw) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 7969 | goto alloc_error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7970 | |
| 7971 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 7972 | |
Thierry FOURNIER / OZON.IO | 02564fd | 2016-11-12 11:07:05 +0100 | [diff] [blame] | 7973 | /* Check required environment. Only accepted "http" or "tcp". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7974 | if (strcmp(env, "tcp") == 0) |
| 7975 | akl->kw[0].parse = action_register_service_tcp; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7976 | else if (strcmp(env, "http") == 0) |
| 7977 | akl->kw[0].parse = action_register_service_http; |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 7978 | else { |
| 7979 | release_hlua_function(fcn); |
| 7980 | if (akl) |
| 7981 | ha_free((char **)&(akl->kw[0].kw)); |
| 7982 | ha_free(&akl); |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7983 | WILL_LJMP(luaL_error(L, "Lua service environment '%s' is unknown. " |
Eric Salama | fe7456f | 2017-12-21 14:30:07 +0100 | [diff] [blame] | 7984 | "'tcp' or 'http' are expected.", env)); |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 7985 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7986 | |
Amaury Denoyelle | e4a617c | 2021-05-06 15:33:09 +0200 | [diff] [blame] | 7987 | akl->kw[0].flags = 0; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7988 | akl->kw[0].private = fcn; |
| 7989 | |
| 7990 | /* End of array. */ |
| 7991 | memset(&akl->kw[1], 0, sizeof(*akl->kw)); |
| 7992 | |
| 7993 | /* Register this new converter */ |
| 7994 | service_keywords_register(akl); |
| 7995 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7996 | return 0; |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 7997 | |
| 7998 | alloc_error: |
| 7999 | release_hlua_function(fcn); |
| 8000 | ha_free(&akl); |
| 8001 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 8002 | return 0; /* Never reached */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 8003 | } |
| 8004 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8005 | /* This function initialises Lua cli handler. It copies the |
| 8006 | * arguments in the Lua stack and create channel IO objects. |
| 8007 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 8008 | 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] | 8009 | { |
| 8010 | struct hlua *hlua; |
| 8011 | struct hlua_function *fcn; |
| 8012 | int i; |
| 8013 | const char *error; |
| 8014 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8015 | fcn = private; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 8016 | appctx->ctx.hlua_cli.fcn = private; |
| 8017 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 8018 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 8019 | if (!hlua) { |
| 8020 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 8021 | return 1; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 8022 | } |
| 8023 | HLUA_INIT(hlua); |
| 8024 | appctx->ctx.hlua_cli.hlua = hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8025 | |
| 8026 | /* Create task used by signal to wakeup applets. |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8027 | * 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] | 8028 | * applet_http. It is absolutely compatible. |
| 8029 | */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 8030 | appctx->ctx.hlua_cli.task = task_new(tid_bit); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8031 | if (!appctx->ctx.hlua_cli.task) { |
Thierry FOURNIER | ffbf569 | 2016-12-16 11:14:06 +0100 | [diff] [blame] | 8032 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 8033 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8034 | } |
| 8035 | appctx->ctx.hlua_cli.task->nice = 0; |
| 8036 | appctx->ctx.hlua_cli.task->context = appctx; |
| 8037 | appctx->ctx.hlua_cli.task->process = hlua_applet_wakeup; |
| 8038 | |
| 8039 | /* Initialises the Lua context */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8040 | if (!hlua_ctx_init(hlua, fcn_ref_to_stack_id(fcn), appctx->ctx.hlua_cli.task, 0)) { |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8041 | 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] | 8042 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8043 | } |
| 8044 | |
| 8045 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8046 | if (!SET_SAFE_LJMP(hlua)) { |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8047 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 8048 | error = lua_tostring(hlua->T, -1); |
| 8049 | else |
| 8050 | error = "critical error"; |
| 8051 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error); |
| 8052 | goto error; |
| 8053 | } |
| 8054 | |
| 8055 | /* Check stack available size. */ |
| 8056 | if (!lua_checkstack(hlua->T, 2)) { |
| 8057 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 8058 | goto error; |
| 8059 | } |
| 8060 | |
| 8061 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8062 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, fcn->function_ref[hlua->state_id]); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8063 | |
| 8064 | /* Once the arguments parsed, the CLI is like an AppletTCP, |
| 8065 | * so push AppletTCP in the stack. |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8066 | */ |
| 8067 | if (!hlua_applet_tcp_new(hlua->T, appctx)) { |
| 8068 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 8069 | goto error; |
| 8070 | } |
| 8071 | hlua->nargs = 1; |
| 8072 | |
| 8073 | /* push keywords in the stack. */ |
| 8074 | for (i = 0; *args[i]; i++) { |
| 8075 | /* Check stack available size. */ |
| 8076 | if (!lua_checkstack(hlua->T, 1)) { |
| 8077 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 8078 | goto error; |
| 8079 | } |
| 8080 | lua_pushstring(hlua->T, args[i]); |
| 8081 | hlua->nargs++; |
| 8082 | } |
| 8083 | |
| 8084 | /* We must initialize the execution timeouts. */ |
| 8085 | hlua->max_time = hlua_timeout_session; |
| 8086 | |
| 8087 | /* At this point the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8088 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8089 | |
| 8090 | /* It's ok */ |
| 8091 | return 0; |
| 8092 | |
| 8093 | /* It's not ok. */ |
| 8094 | error: |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8095 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8096 | hlua_ctx_destroy(hlua); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 8097 | appctx->ctx.hlua_cli.hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8098 | return 1; |
| 8099 | } |
| 8100 | |
| 8101 | static int hlua_cli_io_handler_fct(struct appctx *appctx) |
| 8102 | { |
| 8103 | struct hlua *hlua; |
| 8104 | struct stream_interface *si; |
| 8105 | struct hlua_function *fcn; |
| 8106 | |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 8107 | hlua = appctx->ctx.hlua_cli.hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8108 | si = appctx->owner; |
Willy Tarreau | 8ae4f75 | 2016-12-14 15:41:45 +0100 | [diff] [blame] | 8109 | fcn = appctx->ctx.hlua_cli.fcn; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8110 | |
| 8111 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 8112 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 8113 | return 1; |
| 8114 | |
| 8115 | /* Execute the function. */ |
| 8116 | switch (hlua_ctx_resume(hlua, 1)) { |
| 8117 | |
| 8118 | /* finished. */ |
| 8119 | case HLUA_E_OK: |
| 8120 | return 1; |
| 8121 | |
| 8122 | /* yield. */ |
| 8123 | case HLUA_E_AGAIN: |
| 8124 | /* We want write. */ |
| 8125 | if (HLUA_IS_WAKERESWR(hlua)) |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 8126 | si_rx_room_blk(si); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8127 | /* Set the timeout. */ |
| 8128 | if (hlua->wake_time != TICK_ETERNITY) |
| 8129 | task_schedule(hlua->task, hlua->wake_time); |
| 8130 | return 0; |
| 8131 | |
| 8132 | /* finished with error. */ |
| 8133 | case HLUA_E_ERRMSG: |
| 8134 | /* Display log. */ |
| 8135 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", |
| 8136 | fcn->name, lua_tostring(hlua->T, -1)); |
| 8137 | lua_pop(hlua->T, 1); |
| 8138 | return 1; |
| 8139 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 8140 | case HLUA_E_ETMOUT: |
| 8141 | SEND_ERR(NULL, "Lua converter '%s': execution timeout.\n", |
| 8142 | fcn->name); |
| 8143 | return 1; |
| 8144 | |
| 8145 | case HLUA_E_NOMEM: |
| 8146 | SEND_ERR(NULL, "Lua converter '%s': out of memory error.\n", |
| 8147 | fcn->name); |
| 8148 | return 1; |
| 8149 | |
| 8150 | case HLUA_E_YIELD: /* unexpected */ |
| 8151 | SEND_ERR(NULL, "Lua converter '%s': yield not allowed.\n", |
| 8152 | fcn->name); |
| 8153 | return 1; |
| 8154 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8155 | case HLUA_E_ERR: |
| 8156 | /* Display log. */ |
| 8157 | SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n", |
| 8158 | fcn->name); |
| 8159 | return 1; |
| 8160 | |
| 8161 | default: |
| 8162 | return 1; |
| 8163 | } |
| 8164 | |
| 8165 | return 1; |
| 8166 | } |
| 8167 | |
| 8168 | static void hlua_cli_io_release_fct(struct appctx *appctx) |
| 8169 | { |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 8170 | hlua_ctx_destroy(appctx->ctx.hlua_cli.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 8171 | appctx->ctx.hlua_cli.hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8172 | } |
| 8173 | |
| 8174 | /* This function is an LUA binding used for registering |
| 8175 | * new keywords in the cli. It expects a list of keywords |
| 8176 | * which are the "path". It is limited to 5 keywords. A |
| 8177 | * description of the command, a function to be executed |
| 8178 | * for the parsing and a function for io handlers. |
| 8179 | */ |
| 8180 | __LJMP static int hlua_register_cli(lua_State *L) |
| 8181 | { |
| 8182 | struct cli_kw_list *cli_kws; |
| 8183 | const char *message; |
| 8184 | int ref_io; |
| 8185 | int len; |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 8186 | struct hlua_function *fcn = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8187 | int index; |
| 8188 | int i; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8189 | struct buffer *trash; |
| 8190 | const char *kw[5]; |
| 8191 | struct cli_kw *cli_kw; |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 8192 | const char *errmsg; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8193 | |
| 8194 | MAY_LJMP(check_args(L, 3, "register_cli")); |
| 8195 | |
Aurelien DARRAGON | 8985ab8 | 2023-02-24 09:43:54 +0100 | [diff] [blame] | 8196 | if (hlua_gethlua(L)) { |
| 8197 | /* runtime processing */ |
| 8198 | WILL_LJMP(luaL_error(L, "register_cli: not available outside of body context")); |
| 8199 | } |
| 8200 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8201 | /* First argument : an array of maximum 5 keywords. */ |
| 8202 | if (!lua_istable(L, 1)) |
| 8203 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table")); |
| 8204 | |
| 8205 | /* Second argument : string with contextual message. */ |
| 8206 | message = MAY_LJMP(luaL_checkstring(L, 2)); |
| 8207 | |
| 8208 | /* Third and fourth argument : lua function. */ |
| 8209 | ref_io = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 8210 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8211 | /* Check for CLI service already registered */ |
| 8212 | trash = get_trash_chunk(); |
| 8213 | index = 0; |
| 8214 | lua_pushnil(L); |
| 8215 | memset(kw, 0, sizeof(kw)); |
| 8216 | while (lua_next(L, 1) != 0) { |
| 8217 | if (index >= CLI_PREFIX_KW_NB) |
| 8218 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries")); |
| 8219 | if (lua_type(L, -1) != LUA_TSTRING) |
| 8220 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings")); |
| 8221 | kw[index] = lua_tostring(L, -1); |
| 8222 | if (index == 0) |
| 8223 | chunk_printf(trash, "%s", kw[index]); |
| 8224 | else |
| 8225 | chunk_appendf(trash, " %s", kw[index]); |
| 8226 | index++; |
| 8227 | lua_pop(L, 1); |
| 8228 | } |
| 8229 | cli_kw = cli_find_kw_exact((char **)kw); |
| 8230 | if (cli_kw != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8231 | fcn = cli_kw->private; |
| 8232 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 8233 | ha_warning("Trying to register CLI keyword 'lua.%s' more than once. " |
| 8234 | "This will become a hard error in version 2.5.\n", trash->area); |
| 8235 | } |
| 8236 | fcn->function_ref[hlua_state_id] = ref_io; |
| 8237 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8238 | } |
| 8239 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8240 | /* Allocate and fill the sample fetch keyword struct. */ |
| 8241 | cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 8242 | if (!cli_kws) { |
| 8243 | errmsg = "Lua out of memory error."; |
| 8244 | goto error; |
| 8245 | } |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 8246 | fcn = new_hlua_function(); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 8247 | if (!fcn) { |
| 8248 | errmsg = "Lua out of memory error."; |
| 8249 | goto error; |
| 8250 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8251 | |
| 8252 | /* Fill path. */ |
| 8253 | index = 0; |
| 8254 | lua_pushnil(L); |
| 8255 | while(lua_next(L, 1) != 0) { |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 8256 | if (index >= 5) { |
| 8257 | errmsg = "1st argument must be a table with a maximum of 5 entries"; |
| 8258 | goto error; |
| 8259 | } |
| 8260 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 8261 | errmsg = "1st argument must be a table filled with strings"; |
| 8262 | goto error; |
| 8263 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8264 | cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1)); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 8265 | if (!cli_kws->kw[0].str_kw[index]) { |
| 8266 | errmsg = "Lua out of memory error."; |
| 8267 | goto error; |
| 8268 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8269 | index++; |
| 8270 | lua_pop(L, 1); |
| 8271 | } |
| 8272 | |
| 8273 | /* Copy help message. */ |
| 8274 | cli_kws->kw[0].usage = strdup(message); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 8275 | if (!cli_kws->kw[0].usage) { |
| 8276 | errmsg = "Lua out of memory error."; |
| 8277 | goto error; |
| 8278 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8279 | |
| 8280 | /* Fill fcn io handler. */ |
| 8281 | len = strlen("<lua.cli>") + 1; |
| 8282 | for (i = 0; i < index; i++) |
| 8283 | len += strlen(cli_kws->kw[0].str_kw[i]) + 1; |
| 8284 | fcn->name = calloc(1, len); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 8285 | if (!fcn->name) { |
| 8286 | errmsg = "Lua out of memory error."; |
| 8287 | goto error; |
| 8288 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8289 | strncat((char *)fcn->name, "<lua.cli", len); |
| 8290 | for (i = 0; i < index; i++) { |
| 8291 | strncat((char *)fcn->name, ".", len); |
| 8292 | strncat((char *)fcn->name, cli_kws->kw[0].str_kw[i], len); |
| 8293 | } |
| 8294 | strncat((char *)fcn->name, ">", len); |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8295 | fcn->function_ref[hlua_state_id] = ref_io; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8296 | |
| 8297 | /* Fill last entries. */ |
| 8298 | cli_kws->kw[0].private = fcn; |
| 8299 | cli_kws->kw[0].parse = hlua_cli_parse_fct; |
| 8300 | cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct; |
| 8301 | cli_kws->kw[0].io_release = hlua_cli_io_release_fct; |
| 8302 | |
| 8303 | /* Register this new converter */ |
| 8304 | cli_register_kw(cli_kws); |
| 8305 | |
| 8306 | return 0; |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 8307 | |
| 8308 | error: |
| 8309 | release_hlua_function(fcn); |
| 8310 | if (cli_kws) { |
| 8311 | for (i = 0; i < index; i++) |
| 8312 | ha_free((char **)&(cli_kws->kw[0].str_kw[i])); |
| 8313 | ha_free((char **)&(cli_kws->kw[0].usage)); |
| 8314 | } |
| 8315 | ha_free(&cli_kws); |
| 8316 | WILL_LJMP(luaL_error(L, errmsg)); |
| 8317 | return 0; /* Never reached */ |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8318 | } |
| 8319 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8320 | 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] | 8321 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8322 | char **err, unsigned int *timeout) |
| 8323 | { |
| 8324 | const char *error; |
| 8325 | |
| 8326 | error = parse_time_err(args[1], timeout, TIME_UNIT_MS); |
Willy Tarreau | 9faebe3 | 2019-06-07 19:00:37 +0200 | [diff] [blame] | 8327 | if (error == PARSE_TIME_OVER) { |
| 8328 | memprintf(err, "timer overflow in argument <%s> to <%s> (maximum value is 2147483647 ms or ~24.8 days)", |
| 8329 | args[1], args[0]); |
| 8330 | return -1; |
| 8331 | } |
| 8332 | else if (error == PARSE_TIME_UNDER) { |
| 8333 | memprintf(err, "timer underflow in argument <%s> to <%s> (minimum non-null value is 1 ms)", |
| 8334 | args[1], args[0]); |
| 8335 | return -1; |
| 8336 | } |
| 8337 | else if (error) { |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8338 | memprintf(err, "%s: invalid timeout", args[0]); |
| 8339 | return -1; |
| 8340 | } |
| 8341 | return 0; |
| 8342 | } |
| 8343 | |
| 8344 | 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] | 8345 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8346 | char **err) |
| 8347 | { |
| 8348 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 8349 | file, line, err, &hlua_timeout_session); |
| 8350 | } |
| 8351 | |
| 8352 | 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] | 8353 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8354 | char **err) |
| 8355 | { |
| 8356 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 8357 | file, line, err, &hlua_timeout_task); |
| 8358 | } |
| 8359 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8360 | 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] | 8361 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8362 | char **err) |
| 8363 | { |
| 8364 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 8365 | file, line, err, &hlua_timeout_applet); |
| 8366 | } |
| 8367 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 8368 | 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] | 8369 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 8370 | char **err) |
| 8371 | { |
| 8372 | char *error; |
| 8373 | |
| 8374 | hlua_nb_instruction = strtoll(args[1], &error, 10); |
| 8375 | if (*error != '\0') { |
| 8376 | memprintf(err, "%s: invalid number", args[0]); |
| 8377 | return -1; |
| 8378 | } |
| 8379 | return 0; |
| 8380 | } |
| 8381 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8382 | 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] | 8383 | const struct proxy *defpx, const char *file, int line, |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8384 | char **err) |
| 8385 | { |
| 8386 | char *error; |
| 8387 | |
| 8388 | if (*(args[1]) == 0) { |
| 8389 | memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]); |
| 8390 | return -1; |
| 8391 | } |
| 8392 | hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L; |
| 8393 | if (*error != '\0') { |
| 8394 | memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error); |
| 8395 | return -1; |
| 8396 | } |
| 8397 | return 0; |
| 8398 | } |
| 8399 | |
| 8400 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8401 | /* This function is called by the main configuration key "lua-load". It loads and |
| 8402 | * execute an lua file during the parsing of the HAProxy configuration file. It is |
| 8403 | * the main lua entry point. |
| 8404 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 8405 | * This function runs with the HAProxy keywords API. It returns -1 if an error |
| 8406 | * occurs, otherwise it returns 0. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8407 | * |
| 8408 | * In some error case, LUA set an error message in top of the stack. This function |
| 8409 | * 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] | 8410 | * |
| 8411 | * This function can fail with an abort() due to an Lua critical error. |
| 8412 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 8413 | * tolerated. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8414 | */ |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8415 | static int hlua_load_state(char *filename, lua_State *L, char **err) |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8416 | { |
| 8417 | int error; |
| 8418 | |
| 8419 | /* Just load and compile the file. */ |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8420 | error = luaL_loadfile(L, filename); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8421 | if (error) { |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8422 | memprintf(err, "error in Lua file '%s': %s", filename, lua_tostring(L, -1)); |
| 8423 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8424 | return -1; |
| 8425 | } |
| 8426 | |
| 8427 | /* If no syntax error where detected, execute the code. */ |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8428 | error = lua_pcall(L, 0, LUA_MULTRET, 0); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8429 | switch (error) { |
| 8430 | case LUA_OK: |
| 8431 | break; |
| 8432 | case LUA_ERRRUN: |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8433 | memprintf(err, "Lua runtime error: %s\n", lua_tostring(L, -1)); |
| 8434 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8435 | return -1; |
| 8436 | case LUA_ERRMEM: |
Thierry Fournier | de6145f | 2020-11-29 00:55:53 +0100 | [diff] [blame] | 8437 | memprintf(err, "Lua out of memory error\n"); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8438 | return -1; |
| 8439 | case LUA_ERRERR: |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8440 | memprintf(err, "Lua message handler error: %s\n", lua_tostring(L, -1)); |
| 8441 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8442 | return -1; |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 8443 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM <= 503 |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8444 | case LUA_ERRGCMM: |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8445 | memprintf(err, "Lua garbage collector error: %s\n", lua_tostring(L, -1)); |
| 8446 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8447 | return -1; |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 8448 | #endif |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8449 | default: |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8450 | memprintf(err, "Lua unknown error: %s\n", lua_tostring(L, -1)); |
| 8451 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8452 | return -1; |
| 8453 | } |
| 8454 | |
| 8455 | return 0; |
| 8456 | } |
| 8457 | |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8458 | static int hlua_load(char **args, int section_type, struct proxy *curpx, |
Willy Tarreau | 0182516 | 2021-03-09 09:53:46 +0100 | [diff] [blame] | 8459 | const struct proxy *defpx, const char *file, int line, |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8460 | char **err) |
| 8461 | { |
| 8462 | if (*(args[1]) == 0) { |
| 8463 | memprintf(err, "'%s' expects a file name as parameter.\n", args[0]); |
| 8464 | return -1; |
| 8465 | } |
| 8466 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8467 | /* loading for global state */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8468 | hlua_state_id = 0; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8469 | ha_set_tid(0); |
Thierry Fournier | afc63e2 | 2020-11-28 17:06:51 +0100 | [diff] [blame] | 8470 | return hlua_load_state(args[1], hlua_states[0], err); |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8471 | } |
| 8472 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8473 | 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] | 8474 | const struct proxy *defpx, const char *file, int line, |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8475 | char **err) |
| 8476 | { |
| 8477 | int len; |
| 8478 | |
| 8479 | if (*(args[1]) == 0) { |
| 8480 | memprintf(err, "'%s' expects a file as parameter.\n", args[0]); |
| 8481 | return -1; |
| 8482 | } |
| 8483 | |
| 8484 | if (per_thread_load == NULL) { |
| 8485 | /* allocate the first entry large enough to store the final NULL */ |
| 8486 | per_thread_load = calloc(1, sizeof(*per_thread_load)); |
| 8487 | if (per_thread_load == NULL) { |
| 8488 | memprintf(err, "out of memory error"); |
| 8489 | return -1; |
| 8490 | } |
| 8491 | } |
| 8492 | |
| 8493 | /* count used entries */ |
| 8494 | for (len = 0; per_thread_load[len] != NULL; len++) |
| 8495 | ; |
| 8496 | |
| 8497 | per_thread_load = realloc(per_thread_load, (len + 2) * sizeof(*per_thread_load)); |
| 8498 | if (per_thread_load == NULL) { |
| 8499 | memprintf(err, "out of memory error"); |
| 8500 | return -1; |
| 8501 | } |
| 8502 | |
| 8503 | per_thread_load[len] = strdup(args[1]); |
| 8504 | per_thread_load[len + 1] = NULL; |
| 8505 | |
| 8506 | if (per_thread_load[len] == NULL) { |
| 8507 | memprintf(err, "out of memory error"); |
| 8508 | return -1; |
| 8509 | } |
| 8510 | |
| 8511 | /* loading for thread 1 only */ |
| 8512 | hlua_state_id = 1; |
| 8513 | ha_set_tid(0); |
| 8514 | return hlua_load_state(args[1], hlua_states[1], err); |
| 8515 | } |
| 8516 | |
Tim Duesterhus | c9fc9f2 | 2020-01-12 13:55:39 +0100 | [diff] [blame] | 8517 | /* Prepend the given <path> followed by a semicolon to the `package.<type>` variable |
| 8518 | * in the given <ctx>. |
| 8519 | */ |
Thierry Fournier | 3fb9e51 | 2020-11-28 10:13:12 +0100 | [diff] [blame] | 8520 | static int hlua_prepend_path(lua_State *L, char *type, char *path) |
Tim Duesterhus | c9fc9f2 | 2020-01-12 13:55:39 +0100 | [diff] [blame] | 8521 | { |
Thierry Fournier | 3fb9e51 | 2020-11-28 10:13:12 +0100 | [diff] [blame] | 8522 | lua_getglobal(L, "package"); /* push package variable */ |
| 8523 | lua_pushstring(L, path); /* push given path */ |
| 8524 | lua_pushstring(L, ";"); /* push semicolon */ |
| 8525 | lua_getfield(L, -3, type); /* push old path */ |
| 8526 | lua_concat(L, 3); /* concatenate to new path */ |
| 8527 | lua_setfield(L, -2, type); /* store new path */ |
| 8528 | lua_pop(L, 1); /* pop package variable */ |
Tim Duesterhus | c9fc9f2 | 2020-01-12 13:55:39 +0100 | [diff] [blame] | 8529 | |
| 8530 | return 0; |
| 8531 | } |
| 8532 | |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8533 | 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] | 8534 | const struct proxy *defpx, const char *file, int line, |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8535 | char **err) |
| 8536 | { |
| 8537 | char *path; |
| 8538 | char *type = "path"; |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 8539 | struct prepend_path *p = NULL; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8540 | |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8541 | if (too_many_args(2, args, err, NULL)) { |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 8542 | goto err; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8543 | } |
| 8544 | |
| 8545 | if (!(*args[1])) { |
| 8546 | memprintf(err, "'%s' expects to receive a <path> as argument", args[0]); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 8547 | goto err; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8548 | } |
| 8549 | path = args[1]; |
| 8550 | |
| 8551 | if (*args[2]) { |
| 8552 | if (strcmp(args[2], "path") != 0 && strcmp(args[2], "cpath") != 0) { |
| 8553 | 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] | 8554 | goto err; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8555 | } |
| 8556 | type = args[2]; |
| 8557 | } |
| 8558 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8559 | p = calloc(1, sizeof(*p)); |
| 8560 | if (p == NULL) { |
Tim Duesterhus | f89d43a | 2021-01-03 20:04:37 +0100 | [diff] [blame] | 8561 | memprintf(err, "memory allocation failed"); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 8562 | goto err; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8563 | } |
| 8564 | p->path = strdup(path); |
| 8565 | if (p->path == NULL) { |
Tim Duesterhus | f89d43a | 2021-01-03 20:04:37 +0100 | [diff] [blame] | 8566 | memprintf(err, "memory allocation failed"); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 8567 | goto err2; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8568 | } |
| 8569 | p->type = strdup(type); |
| 8570 | if (p->type == NULL) { |
Tim Duesterhus | f89d43a | 2021-01-03 20:04:37 +0100 | [diff] [blame] | 8571 | memprintf(err, "memory allocation failed"); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 8572 | goto err2; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8573 | } |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 8574 | LIST_APPEND(&prepend_path_list, &p->l); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8575 | |
| 8576 | hlua_prepend_path(hlua_states[0], type, path); |
| 8577 | hlua_prepend_path(hlua_states[1], type, path); |
| 8578 | return 0; |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 8579 | |
| 8580 | err2: |
| 8581 | free(p->type); |
| 8582 | free(p->path); |
| 8583 | err: |
| 8584 | free(p); |
| 8585 | return -1; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8586 | } |
| 8587 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8588 | /* configuration keywords declaration */ |
| 8589 | static struct cfg_kw_list cfg_kws = {{ },{ |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8590 | { CFG_GLOBAL, "lua-prepend-path", hlua_config_prepend_path }, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8591 | { CFG_GLOBAL, "lua-load", hlua_load }, |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8592 | { CFG_GLOBAL, "lua-load-per-thread", hlua_load_per_thread }, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8593 | { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout }, |
| 8594 | { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout }, |
Thierry FOURNIER | 56da101 | 2015-10-01 08:42:31 +0200 | [diff] [blame] | 8595 | { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout }, |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 8596 | { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield }, |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8597 | { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem }, |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8598 | { 0, NULL, NULL }, |
| 8599 | }}; |
| 8600 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 8601 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |
| 8602 | |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 8603 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8604 | /* This function can fail with an abort() due to an Lua critical error. |
| 8605 | * We are in the initialisation process of HAProxy, this abort() is |
| 8606 | * tolerated. |
| 8607 | */ |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8608 | int hlua_post_init_state(lua_State *L) |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8609 | { |
| 8610 | struct hlua_init_function *init; |
| 8611 | const char *msg; |
| 8612 | enum hlua_exec ret; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8613 | const char *error; |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8614 | const char *kind; |
| 8615 | const char *trace; |
| 8616 | int return_status = 1; |
| 8617 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
| 8618 | int nres; |
| 8619 | #endif |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8620 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 8621 | /* disable memory limit checks if limit is not set */ |
| 8622 | if (!hlua_global_allocator.limit) |
| 8623 | hlua_global_allocator.limit = ~hlua_global_allocator.limit; |
| 8624 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 8625 | /* Call post initialisation function in safe environment. */ |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 8626 | if (setjmp(safe_ljmp_env) != 0) { |
| 8627 | lua_atpanic(L, hlua_panic_safe); |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8628 | if (lua_type(L, -1) == LUA_TSTRING) |
| 8629 | error = lua_tostring(L, -1); |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 8630 | else |
| 8631 | error = "critical error"; |
| 8632 | fprintf(stderr, "Lua post-init: %s.\n", error); |
| 8633 | exit(1); |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 8634 | } else { |
| 8635 | lua_atpanic(L, hlua_panic_ljmp); |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 8636 | } |
Frédéric Lécaille | 54f2bcf | 2018-08-29 13:46:24 +0200 | [diff] [blame] | 8637 | |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8638 | hlua_fcn_post_init(L); |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 8639 | |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8640 | list_for_each_entry(init, &hlua_init_functions[hlua_state_id], l) { |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8641 | lua_rawgeti(L, LUA_REGISTRYINDEX, init->function_ref); |
Aurelien DARRAGON | 93591b4 | 2023-03-20 16:29:55 +0100 | [diff] [blame^] | 8642 | /* function ref should be released right away since it was pushed |
| 8643 | * on the stack and will not be used anymore |
| 8644 | */ |
| 8645 | hlua_unref(L, init->function_ref); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8646 | |
| 8647 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8648 | ret = lua_resume(L, L, 0, &nres); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8649 | #else |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8650 | ret = lua_resume(L, L, 0); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8651 | #endif |
| 8652 | kind = NULL; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8653 | switch (ret) { |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8654 | |
| 8655 | case LUA_OK: |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8656 | lua_pop(L, -1); |
Thierry Fournier | 13d08b7 | 2020-11-28 11:02:58 +0100 | [diff] [blame] | 8657 | break; |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8658 | |
| 8659 | case LUA_ERRERR: |
| 8660 | kind = "message handler error"; |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 8661 | /* Fall through */ |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8662 | case LUA_ERRRUN: |
| 8663 | if (!kind) |
| 8664 | kind = "runtime error"; |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8665 | msg = lua_tostring(L, -1); |
| 8666 | lua_settop(L, 0); /* Empty the stack. */ |
| 8667 | lua_pop(L, 1); |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 8668 | trace = hlua_traceback(L, ", "); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8669 | if (msg) |
| 8670 | ha_alert("Lua init: %s: '%s' from %s\n", kind, msg, trace); |
| 8671 | else |
| 8672 | ha_alert("Lua init: unknown %s from %s\n", kind, trace); |
| 8673 | return_status = 0; |
| 8674 | break; |
| 8675 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8676 | default: |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8677 | /* Unknown error */ |
| 8678 | kind = "Unknown error"; |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 8679 | /* Fall through */ |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8680 | case LUA_YIELD: |
| 8681 | /* yield is not configured at this step, this state doesn't happen */ |
| 8682 | if (!kind) |
| 8683 | kind = "yield not allowed"; |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 8684 | /* Fall through */ |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8685 | case LUA_ERRMEM: |
| 8686 | if (!kind) |
| 8687 | kind = "out of memory error"; |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8688 | lua_settop(L, 0); |
| 8689 | lua_pop(L, 1); |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 8690 | trace = hlua_traceback(L, ", "); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8691 | ha_alert("Lua init: %s: %s\n", kind, trace); |
| 8692 | return_status = 0; |
| 8693 | break; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8694 | } |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8695 | if (!return_status) |
| 8696 | break; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8697 | } |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 8698 | |
| 8699 | lua_atpanic(L, hlua_panic_safe); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8700 | return return_status; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8701 | } |
| 8702 | |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8703 | int hlua_post_init() |
| 8704 | { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8705 | int ret; |
| 8706 | int i; |
| 8707 | int errors; |
| 8708 | char *err = NULL; |
| 8709 | struct hlua_function *fcn; |
| 8710 | |
Willy Tarreau | 42afc59 | 2021-08-30 09:35:18 +0200 | [diff] [blame] | 8711 | #if defined(USE_OPENSSL) |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8712 | /* Initialize SSL server. */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 8713 | if (socket_ssl->xprt->prepare_srv) { |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8714 | int saved_used_backed = global.ssl_used_backend; |
| 8715 | // don't affect maxconn automatic computation |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 8716 | socket_ssl->xprt->prepare_srv(socket_ssl); |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8717 | global.ssl_used_backend = saved_used_backed; |
| 8718 | } |
| 8719 | #endif |
| 8720 | |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8721 | /* Perform post init of common thread */ |
| 8722 | hlua_state_id = 0; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8723 | ha_set_tid(0); |
| 8724 | ret = hlua_post_init_state(hlua_states[hlua_state_id]); |
| 8725 | if (ret == 0) |
| 8726 | return 0; |
| 8727 | |
| 8728 | /* init remaining lua states and load files */ |
| 8729 | for (hlua_state_id = 2; hlua_state_id < global.nbthread + 1; hlua_state_id++) { |
| 8730 | |
| 8731 | /* set thread context */ |
| 8732 | ha_set_tid(hlua_state_id - 1); |
| 8733 | |
| 8734 | /* Init lua state */ |
| 8735 | hlua_states[hlua_state_id] = hlua_init_state(hlua_state_id); |
| 8736 | |
| 8737 | /* Load lua files */ |
| 8738 | for (i = 0; per_thread_load && per_thread_load[i]; i++) { |
| 8739 | ret = hlua_load_state(per_thread_load[i], hlua_states[hlua_state_id], &err); |
| 8740 | if (ret != 0) { |
| 8741 | ha_alert("Lua init: %s\n", err); |
| 8742 | return 0; |
| 8743 | } |
| 8744 | } |
| 8745 | } |
| 8746 | |
| 8747 | /* Reset thread context */ |
| 8748 | ha_set_tid(0); |
| 8749 | |
| 8750 | /* Execute post init for all states */ |
| 8751 | for (hlua_state_id = 1; hlua_state_id < global.nbthread + 1; hlua_state_id++) { |
| 8752 | |
| 8753 | /* set thread context */ |
| 8754 | ha_set_tid(hlua_state_id - 1); |
| 8755 | |
| 8756 | /* run post init */ |
| 8757 | ret = hlua_post_init_state(hlua_states[hlua_state_id]); |
| 8758 | if (ret == 0) |
| 8759 | return 0; |
| 8760 | } |
| 8761 | |
| 8762 | /* Reset thread context */ |
| 8763 | ha_set_tid(0); |
| 8764 | |
| 8765 | /* control functions registering. Each function must have: |
| 8766 | * - only the function_ref[0] set positive and all other to -1 |
| 8767 | * - only the function_ref[0] set to -1 and all other positive |
| 8768 | * This ensure a same reference is not used both in shared |
| 8769 | * lua state and thread dedicated lua state. Note: is the case |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 8770 | * reach, the shared state is priority, but the bug will be |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8771 | * complicated to found for the end user. |
| 8772 | */ |
| 8773 | errors = 0; |
| 8774 | list_for_each_entry(fcn, &referenced_functions, l) { |
| 8775 | ret = 0; |
| 8776 | for (i = 1; i < global.nbthread + 1; i++) { |
| 8777 | if (fcn->function_ref[i] == -1) |
| 8778 | ret--; |
| 8779 | else |
| 8780 | ret++; |
| 8781 | } |
| 8782 | if (abs(ret) != global.nbthread) { |
| 8783 | ha_alert("Lua function '%s' is not referenced in all thread. " |
| 8784 | "Expect function in all thread or in none thread.\n", fcn->name); |
| 8785 | errors++; |
| 8786 | continue; |
| 8787 | } |
| 8788 | |
| 8789 | if ((fcn->function_ref[0] == -1) == (ret < 0)) { |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 8790 | ha_alert("Lua function '%s' is referenced both ins shared Lua context (through lua-load) " |
| 8791 | "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] | 8792 | "exclusive.\n", fcn->name); |
| 8793 | errors++; |
| 8794 | } |
| 8795 | } |
| 8796 | |
| 8797 | if (errors > 0) |
| 8798 | return 0; |
| 8799 | |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 8800 | /* 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] | 8801 | * -1 in order to have probably a segfault if someone use it |
| 8802 | */ |
| 8803 | hlua_state_id = -1; |
| 8804 | |
| 8805 | return 1; |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8806 | } |
| 8807 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8808 | /* The memory allocator used by the Lua stack. <ud> is a pointer to the |
| 8809 | * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize> |
| 8810 | * 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] | 8811 | * allocation. <nsize> is the requested new size. A new allocation is |
| 8812 | * indicated by <ptr> being NULL. A free is indicated by <nsize> being |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 8813 | * zero. This one verifies that the limits are respected but is optimized |
| 8814 | * for the fast case where limits are not used, hence stats are not updated. |
Willy Tarreau | e00ad05 | 2021-10-22 16:00:02 +0200 | [diff] [blame] | 8815 | * |
| 8816 | * Warning: while this API ressembles glibc's realloc() a lot, glibc surpasses |
| 8817 | * POSIX by making realloc(ptr,0) an effective free(), but others do not do |
| 8818 | * that and will simply allocate zero as if it were the result of malloc(0), |
| 8819 | * so mapping this onto realloc() will lead to memory leaks on non-glibc |
| 8820 | * systems. |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8821 | */ |
| 8822 | static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize) |
| 8823 | { |
| 8824 | struct hlua_mem_allocator *zone = ud; |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 8825 | size_t limit, old, new; |
| 8826 | |
Tim Duesterhus | 2258652 | 2021-01-08 10:35:33 +0100 | [diff] [blame] | 8827 | if (unlikely(!ptr && !nsize)) |
| 8828 | return NULL; |
| 8829 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 8830 | /* a limit of ~0 means unlimited and boot complete, so there's no need |
| 8831 | * for accounting anymore. |
| 8832 | */ |
Willy Tarreau | e00ad05 | 2021-10-22 16:00:02 +0200 | [diff] [blame] | 8833 | if (likely(~zone->limit == 0)) { |
| 8834 | if (!nsize) |
| 8835 | ha_free(&ptr); |
| 8836 | else |
| 8837 | ptr = realloc(ptr, nsize); |
| 8838 | return ptr; |
| 8839 | } |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8840 | |
Willy Tarreau | d36c7fa | 2020-12-02 12:26:29 +0100 | [diff] [blame] | 8841 | if (!ptr) |
| 8842 | osize = 0; |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8843 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 8844 | /* enforce strict limits across all threads */ |
| 8845 | limit = zone->limit; |
| 8846 | old = _HA_ATOMIC_LOAD(&zone->allocated); |
| 8847 | do { |
| 8848 | new = old + nsize - osize; |
| 8849 | if (unlikely(nsize && limit && new > limit)) |
| 8850 | return NULL; |
| 8851 | } while (!_HA_ATOMIC_CAS(&zone->allocated, &old, new)); |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8852 | |
Willy Tarreau | e00ad05 | 2021-10-22 16:00:02 +0200 | [diff] [blame] | 8853 | if (!nsize) |
| 8854 | ha_free(&ptr); |
| 8855 | else |
| 8856 | ptr = realloc(ptr, nsize); |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 8857 | |
| 8858 | if (unlikely(!ptr && nsize)) // failed |
| 8859 | _HA_ATOMIC_SUB(&zone->allocated, nsize - osize); |
| 8860 | |
| 8861 | __ha_barrier_atomic_store(); |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8862 | return ptr; |
| 8863 | } |
| 8864 | |
Thierry Fournier | ecb83c2 | 2020-11-28 15:49:44 +0100 | [diff] [blame] | 8865 | /* 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] | 8866 | * We are in the initialisation process of HAProxy, this abort() is |
| 8867 | * tolerated. |
| 8868 | */ |
Thierry Fournier | ecb83c2 | 2020-11-28 15:49:44 +0100 | [diff] [blame] | 8869 | lua_State *hlua_init_state(int thread_num) |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 8870 | { |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8871 | int i; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8872 | int idx; |
| 8873 | struct sample_fetch *sf; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8874 | struct sample_conv *sc; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8875 | char *p; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8876 | const char *error_msg; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 8877 | void **context; |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8878 | lua_State *L; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8879 | struct prepend_path *pp; |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8880 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 8881 | /* Init main lua stack. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8882 | L = lua_newstate(hlua_alloc, &hlua_global_allocator); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 8883 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 8884 | /* Initialise Lua context to NULL */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8885 | context = lua_getextraspace(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 8886 | *context = NULL; |
| 8887 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 8888 | /* From this point, until the end of the initialisation function, |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8889 | * the Lua function can fail with an abort. We are in the initialisation |
| 8890 | * process of HAProxy, this abort() is tolerated. |
| 8891 | */ |
| 8892 | |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 8893 | /* Call post initialisation function in safe environment. */ |
| 8894 | if (setjmp(safe_ljmp_env) != 0) { |
| 8895 | lua_atpanic(L, hlua_panic_safe); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8896 | if (lua_type(L, -1) == LUA_TSTRING) |
| 8897 | error_msg = lua_tostring(L, -1); |
Thierry Fournier | 2f05cc6 | 2020-11-28 16:08:02 +0100 | [diff] [blame] | 8898 | else |
| 8899 | error_msg = "critical error"; |
| 8900 | fprintf(stderr, "Lua init: %s.\n", error_msg); |
| 8901 | exit(1); |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 8902 | } else { |
| 8903 | lua_atpanic(L, hlua_panic_ljmp); |
Thierry Fournier | 2f05cc6 | 2020-11-28 16:08:02 +0100 | [diff] [blame] | 8904 | } |
| 8905 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 8906 | /* Initialise lua. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8907 | luaL_openlibs(L); |
Tim Duesterhus | 541fe1e | 2020-01-12 13:55:41 +0100 | [diff] [blame] | 8908 | #define HLUA_PREPEND_PATH_TOSTRING1(x) #x |
| 8909 | #define HLUA_PREPEND_PATH_TOSTRING(x) HLUA_PREPEND_PATH_TOSTRING1(x) |
| 8910 | #ifdef HLUA_PREPEND_PATH |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8911 | hlua_prepend_path(L, "path", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_PATH)); |
Tim Duesterhus | 541fe1e | 2020-01-12 13:55:41 +0100 | [diff] [blame] | 8912 | #endif |
| 8913 | #ifdef HLUA_PREPEND_CPATH |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8914 | hlua_prepend_path(L, "cpath", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_CPATH)); |
Tim Duesterhus | 541fe1e | 2020-01-12 13:55:41 +0100 | [diff] [blame] | 8915 | #endif |
| 8916 | #undef HLUA_PREPEND_PATH_TOSTRING |
| 8917 | #undef HLUA_PREPEND_PATH_TOSTRING1 |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8918 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8919 | /* Apply configured prepend path */ |
| 8920 | list_for_each_entry(pp, &prepend_path_list, l) |
| 8921 | hlua_prepend_path(L, pp->type, pp->path); |
| 8922 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8923 | /* |
| 8924 | * |
| 8925 | * Create "core" object. |
| 8926 | * |
| 8927 | */ |
| 8928 | |
Thierry FOURNIER | a2d8c65 | 2015-03-11 17:29:39 +0100 | [diff] [blame] | 8929 | /* This table entry is the object "core" base. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8930 | lua_newtable(L); |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8931 | |
Thierry Fournier | ecb83c2 | 2020-11-28 15:49:44 +0100 | [diff] [blame] | 8932 | /* set the thread id */ |
| 8933 | hlua_class_const_int(L, "thread", thread_num); |
| 8934 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8935 | /* Push the loglevel constants. */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8936 | for (i = 0; i < NB_LOG_LEVELS; i++) |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8937 | hlua_class_const_int(L, log_levels[i], i); |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8938 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8939 | /* Register special functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8940 | hlua_class_function(L, "register_init", hlua_register_init); |
| 8941 | hlua_class_function(L, "register_task", hlua_register_task); |
| 8942 | hlua_class_function(L, "register_fetches", hlua_register_fetches); |
| 8943 | hlua_class_function(L, "register_converters", hlua_register_converters); |
| 8944 | hlua_class_function(L, "register_action", hlua_register_action); |
| 8945 | hlua_class_function(L, "register_service", hlua_register_service); |
| 8946 | hlua_class_function(L, "register_cli", hlua_register_cli); |
| 8947 | hlua_class_function(L, "yield", hlua_yield); |
| 8948 | hlua_class_function(L, "set_nice", hlua_set_nice); |
| 8949 | hlua_class_function(L, "sleep", hlua_sleep); |
| 8950 | hlua_class_function(L, "msleep", hlua_msleep); |
| 8951 | hlua_class_function(L, "add_acl", hlua_add_acl); |
| 8952 | hlua_class_function(L, "del_acl", hlua_del_acl); |
| 8953 | hlua_class_function(L, "set_map", hlua_set_map); |
| 8954 | hlua_class_function(L, "del_map", hlua_del_map); |
| 8955 | hlua_class_function(L, "tcp", hlua_socket_new); |
| 8956 | hlua_class_function(L, "log", hlua_log); |
| 8957 | hlua_class_function(L, "Debug", hlua_log_debug); |
| 8958 | hlua_class_function(L, "Info", hlua_log_info); |
| 8959 | hlua_class_function(L, "Warning", hlua_log_warning); |
| 8960 | hlua_class_function(L, "Alert", hlua_log_alert); |
| 8961 | hlua_class_function(L, "done", hlua_done); |
| 8962 | hlua_fcn_reg_core_fcn(L); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8963 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8964 | lua_setglobal(L, "core"); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 8965 | |
| 8966 | /* |
| 8967 | * |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 8968 | * Create "act" object. |
| 8969 | * |
| 8970 | */ |
| 8971 | |
| 8972 | /* This table entry is the object "act" base. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8973 | lua_newtable(L); |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 8974 | |
| 8975 | /* push action return constants */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8976 | hlua_class_const_int(L, "CONTINUE", ACT_RET_CONT); |
| 8977 | hlua_class_const_int(L, "STOP", ACT_RET_STOP); |
| 8978 | hlua_class_const_int(L, "YIELD", ACT_RET_YIELD); |
| 8979 | hlua_class_const_int(L, "ERROR", ACT_RET_ERR); |
| 8980 | hlua_class_const_int(L, "DONE", ACT_RET_DONE); |
| 8981 | hlua_class_const_int(L, "DENY", ACT_RET_DENY); |
| 8982 | hlua_class_const_int(L, "ABORT", ACT_RET_ABRT); |
| 8983 | hlua_class_const_int(L, "INVALID", ACT_RET_INV); |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 8984 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8985 | hlua_class_function(L, "wake_time", hlua_set_wake_time); |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 8986 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8987 | lua_setglobal(L, "act"); |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 8988 | |
| 8989 | /* |
| 8990 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8991 | * Register class Map |
| 8992 | * |
| 8993 | */ |
| 8994 | |
| 8995 | /* This table entry is the object "Map" base. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8996 | lua_newtable(L); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8997 | |
| 8998 | /* register pattern types. */ |
| 8999 | for (i=0; i<PAT_MATCH_NUM; i++) |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9000 | hlua_class_const_int(L, pat_match_names[i], i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 9001 | for (i=0; i<PAT_MATCH_NUM; i++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 9002 | snprintf(trash.area, trash.size, "_%s", pat_match_names[i]); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9003 | hlua_class_const_int(L, trash.area, i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 9004 | } |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 9005 | |
| 9006 | /* register constructor. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9007 | hlua_class_function(L, "new", hlua_map_new); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 9008 | |
| 9009 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9010 | lua_newtable(L); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 9011 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 9012 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9013 | lua_pushstring(L, "__index"); |
| 9014 | lua_newtable(L); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 9015 | |
| 9016 | /* Register . */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9017 | hlua_class_function(L, "lookup", hlua_map_lookup); |
| 9018 | hlua_class_function(L, "slookup", hlua_map_slookup); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 9019 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9020 | lua_rawset(L, -3); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 9021 | |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 9022 | /* Register previous table in the registry with reference and named entry. |
| 9023 | * The function hlua_register_metatable() pops the stack, so we |
| 9024 | * previously create a copy of the table. |
| 9025 | */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9026 | lua_pushvalue(L, -1); /* Copy the -1 entry and push it on the stack. */ |
| 9027 | class_map_ref = hlua_register_metatable(L, CLASS_MAP); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 9028 | |
| 9029 | /* Assign the metatable to the mai Map object. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9030 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 9031 | |
| 9032 | /* Set a name to the table. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9033 | lua_setglobal(L, "Map"); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 9034 | |
| 9035 | /* |
| 9036 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 9037 | * Register class Channel |
| 9038 | * |
| 9039 | */ |
| 9040 | |
| 9041 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9042 | lua_newtable(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 9043 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 9044 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9045 | lua_pushstring(L, "__index"); |
| 9046 | lua_newtable(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 9047 | |
| 9048 | /* Register . */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9049 | hlua_class_function(L, "get", hlua_channel_get); |
| 9050 | hlua_class_function(L, "dup", hlua_channel_dup); |
| 9051 | hlua_class_function(L, "getline", hlua_channel_getline); |
| 9052 | hlua_class_function(L, "set", hlua_channel_set); |
| 9053 | hlua_class_function(L, "append", hlua_channel_append); |
| 9054 | hlua_class_function(L, "send", hlua_channel_send); |
| 9055 | hlua_class_function(L, "forward", hlua_channel_forward); |
| 9056 | hlua_class_function(L, "get_in_len", hlua_channel_get_in_len); |
| 9057 | hlua_class_function(L, "get_out_len", hlua_channel_get_out_len); |
| 9058 | hlua_class_function(L, "is_full", hlua_channel_is_full); |
| 9059 | hlua_class_function(L, "is_resp", hlua_channel_is_resp); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 9060 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9061 | lua_rawset(L, -3); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 9062 | |
| 9063 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9064 | class_channel_ref = hlua_register_metatable(L, CLASS_CHANNEL); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 9065 | |
| 9066 | /* |
| 9067 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 9068 | * Register class Fetches |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 9069 | * |
| 9070 | */ |
| 9071 | |
| 9072 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9073 | lua_newtable(L); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 9074 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 9075 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9076 | lua_pushstring(L, "__index"); |
| 9077 | lua_newtable(L); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 9078 | |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 9079 | /* Browse existing fetches and create the associated |
| 9080 | * object method. |
| 9081 | */ |
| 9082 | sf = NULL; |
| 9083 | while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) { |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 9084 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 9085 | * by an underscore. |
| 9086 | */ |
Willy Tarreau | fa92f36 | 2021-08-26 16:48:53 +0200 | [diff] [blame] | 9087 | strlcpy2(trash.area, sf->kw, trash.size); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 9088 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 9089 | if (*p == '.' || *p == '-' || *p == '+') |
| 9090 | *p = '_'; |
| 9091 | |
| 9092 | /* Register the function. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9093 | lua_pushstring(L, trash.area); |
| 9094 | lua_pushlightuserdata(L, sf); |
| 9095 | lua_pushcclosure(L, hlua_run_sample_fetch, 1); |
| 9096 | lua_rawset(L, -3); |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 9097 | } |
| 9098 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9099 | lua_rawset(L, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 9100 | |
| 9101 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9102 | class_fetches_ref = hlua_register_metatable(L, CLASS_FETCHES); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 9103 | |
| 9104 | /* |
| 9105 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 9106 | * Register class Converters |
| 9107 | * |
| 9108 | */ |
| 9109 | |
| 9110 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9111 | lua_newtable(L); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 9112 | |
| 9113 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9114 | lua_pushstring(L, "__index"); |
| 9115 | lua_newtable(L); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 9116 | |
| 9117 | /* Browse existing converters and create the associated |
| 9118 | * object method. |
| 9119 | */ |
| 9120 | sc = NULL; |
| 9121 | while ((sc = sample_conv_getnext(sc, &idx)) != NULL) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 9122 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 9123 | * by an underscore. |
| 9124 | */ |
Willy Tarreau | fa92f36 | 2021-08-26 16:48:53 +0200 | [diff] [blame] | 9125 | strlcpy2(trash.area, sc->kw, trash.size); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 9126 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 9127 | if (*p == '.' || *p == '-' || *p == '+') |
| 9128 | *p = '_'; |
| 9129 | |
| 9130 | /* Register the function. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9131 | lua_pushstring(L, trash.area); |
| 9132 | lua_pushlightuserdata(L, sc); |
| 9133 | lua_pushcclosure(L, hlua_run_sample_conv, 1); |
| 9134 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 9135 | } |
| 9136 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9137 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 9138 | |
| 9139 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9140 | class_converters_ref = hlua_register_metatable(L, CLASS_CONVERTERS); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 9141 | |
| 9142 | /* |
| 9143 | * |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 9144 | * Register class HTTP |
| 9145 | * |
| 9146 | */ |
| 9147 | |
| 9148 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9149 | lua_newtable(L); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 9150 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 9151 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9152 | lua_pushstring(L, "__index"); |
| 9153 | lua_newtable(L); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 9154 | |
| 9155 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9156 | hlua_class_function(L, "req_get_headers",hlua_http_req_get_headers); |
| 9157 | hlua_class_function(L, "req_del_header", hlua_http_req_del_hdr); |
| 9158 | hlua_class_function(L, "req_rep_header", hlua_http_req_rep_hdr); |
| 9159 | hlua_class_function(L, "req_rep_value", hlua_http_req_rep_val); |
| 9160 | hlua_class_function(L, "req_add_header", hlua_http_req_add_hdr); |
| 9161 | hlua_class_function(L, "req_set_header", hlua_http_req_set_hdr); |
| 9162 | hlua_class_function(L, "req_set_method", hlua_http_req_set_meth); |
| 9163 | hlua_class_function(L, "req_set_path", hlua_http_req_set_path); |
| 9164 | hlua_class_function(L, "req_set_query", hlua_http_req_set_query); |
| 9165 | hlua_class_function(L, "req_set_uri", hlua_http_req_set_uri); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 9166 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9167 | hlua_class_function(L, "res_get_headers",hlua_http_res_get_headers); |
| 9168 | hlua_class_function(L, "res_del_header", hlua_http_res_del_hdr); |
| 9169 | hlua_class_function(L, "res_rep_header", hlua_http_res_rep_hdr); |
| 9170 | hlua_class_function(L, "res_rep_value", hlua_http_res_rep_val); |
| 9171 | hlua_class_function(L, "res_add_header", hlua_http_res_add_hdr); |
| 9172 | hlua_class_function(L, "res_set_header", hlua_http_res_set_hdr); |
| 9173 | hlua_class_function(L, "res_set_status", hlua_http_res_set_status); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 9174 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9175 | lua_rawset(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 9176 | |
| 9177 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9178 | class_http_ref = hlua_register_metatable(L, CLASS_HTTP); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 9179 | |
| 9180 | /* |
| 9181 | * |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9182 | * Register class AppletTCP |
| 9183 | * |
| 9184 | */ |
| 9185 | |
| 9186 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9187 | lua_newtable(L); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9188 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 9189 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9190 | lua_pushstring(L, "__index"); |
| 9191 | lua_newtable(L); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9192 | |
| 9193 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9194 | hlua_class_function(L, "getline", hlua_applet_tcp_getline); |
| 9195 | hlua_class_function(L, "receive", hlua_applet_tcp_recv); |
| 9196 | hlua_class_function(L, "send", hlua_applet_tcp_send); |
| 9197 | hlua_class_function(L, "set_priv", hlua_applet_tcp_set_priv); |
| 9198 | hlua_class_function(L, "get_priv", hlua_applet_tcp_get_priv); |
| 9199 | hlua_class_function(L, "set_var", hlua_applet_tcp_set_var); |
| 9200 | hlua_class_function(L, "unset_var", hlua_applet_tcp_unset_var); |
| 9201 | hlua_class_function(L, "get_var", hlua_applet_tcp_get_var); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9202 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9203 | lua_settable(L, -3); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9204 | |
| 9205 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9206 | class_applet_tcp_ref = hlua_register_metatable(L, CLASS_APPLET_TCP); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9207 | |
| 9208 | /* |
| 9209 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9210 | * Register class AppletHTTP |
| 9211 | * |
| 9212 | */ |
| 9213 | |
| 9214 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9215 | lua_newtable(L); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9216 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 9217 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9218 | lua_pushstring(L, "__index"); |
| 9219 | lua_newtable(L); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9220 | |
| 9221 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9222 | hlua_class_function(L, "set_priv", hlua_applet_http_set_priv); |
| 9223 | hlua_class_function(L, "get_priv", hlua_applet_http_get_priv); |
| 9224 | hlua_class_function(L, "set_var", hlua_applet_http_set_var); |
| 9225 | hlua_class_function(L, "unset_var", hlua_applet_http_unset_var); |
| 9226 | hlua_class_function(L, "get_var", hlua_applet_http_get_var); |
| 9227 | hlua_class_function(L, "getline", hlua_applet_http_getline); |
| 9228 | hlua_class_function(L, "receive", hlua_applet_http_recv); |
| 9229 | hlua_class_function(L, "send", hlua_applet_http_send); |
| 9230 | hlua_class_function(L, "add_header", hlua_applet_http_addheader); |
| 9231 | hlua_class_function(L, "set_status", hlua_applet_http_status); |
| 9232 | hlua_class_function(L, "start_response", hlua_applet_http_start_response); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9233 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9234 | lua_settable(L, -3); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9235 | |
| 9236 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9237 | class_applet_http_ref = hlua_register_metatable(L, CLASS_APPLET_HTTP); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9238 | |
| 9239 | /* |
| 9240 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 9241 | * Register class TXN |
| 9242 | * |
| 9243 | */ |
| 9244 | |
| 9245 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9246 | lua_newtable(L); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 9247 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 9248 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9249 | lua_pushstring(L, "__index"); |
| 9250 | lua_newtable(L); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 9251 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 9252 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9253 | hlua_class_function(L, "set_priv", hlua_set_priv); |
| 9254 | hlua_class_function(L, "get_priv", hlua_get_priv); |
| 9255 | hlua_class_function(L, "set_var", hlua_set_var); |
| 9256 | hlua_class_function(L, "unset_var", hlua_unset_var); |
| 9257 | hlua_class_function(L, "get_var", hlua_get_var); |
| 9258 | hlua_class_function(L, "done", hlua_txn_done); |
| 9259 | hlua_class_function(L, "reply", hlua_txn_reply_new); |
| 9260 | hlua_class_function(L, "set_loglevel", hlua_txn_set_loglevel); |
| 9261 | hlua_class_function(L, "set_tos", hlua_txn_set_tos); |
| 9262 | hlua_class_function(L, "set_mark", hlua_txn_set_mark); |
| 9263 | hlua_class_function(L, "set_priority_class", hlua_txn_set_priority_class); |
| 9264 | hlua_class_function(L, "set_priority_offset", hlua_txn_set_priority_offset); |
| 9265 | hlua_class_function(L, "deflog", hlua_txn_deflog); |
| 9266 | hlua_class_function(L, "log", hlua_txn_log); |
| 9267 | hlua_class_function(L, "Debug", hlua_txn_log_debug); |
| 9268 | hlua_class_function(L, "Info", hlua_txn_log_info); |
| 9269 | hlua_class_function(L, "Warning", hlua_txn_log_warning); |
| 9270 | hlua_class_function(L, "Alert", hlua_txn_log_alert); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 9271 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9272 | lua_rawset(L, -3); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 9273 | |
| 9274 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9275 | class_txn_ref = hlua_register_metatable(L, CLASS_TXN); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9276 | |
| 9277 | /* |
| 9278 | * |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 9279 | * Register class reply |
| 9280 | * |
| 9281 | */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9282 | lua_newtable(L); |
| 9283 | lua_pushstring(L, "__index"); |
| 9284 | lua_newtable(L); |
| 9285 | hlua_class_function(L, "set_status", hlua_txn_reply_set_status); |
| 9286 | hlua_class_function(L, "add_header", hlua_txn_reply_add_header); |
| 9287 | hlua_class_function(L, "del_header", hlua_txn_reply_del_header); |
| 9288 | hlua_class_function(L, "set_body", hlua_txn_reply_set_body); |
| 9289 | lua_settable(L, -3); /* Sets the __index entry. */ |
| 9290 | class_txn_reply_ref = luaL_ref(L, LUA_REGISTRYINDEX); |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 9291 | |
| 9292 | |
| 9293 | /* |
| 9294 | * |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9295 | * Register class Socket |
| 9296 | * |
| 9297 | */ |
| 9298 | |
| 9299 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9300 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9301 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 9302 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9303 | lua_pushstring(L, "__index"); |
| 9304 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9305 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 9306 | #ifdef USE_OPENSSL |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9307 | hlua_class_function(L, "connect_ssl", hlua_socket_connect_ssl); |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 9308 | #endif |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9309 | hlua_class_function(L, "connect", hlua_socket_connect); |
| 9310 | hlua_class_function(L, "send", hlua_socket_send); |
| 9311 | hlua_class_function(L, "receive", hlua_socket_receive); |
| 9312 | hlua_class_function(L, "close", hlua_socket_close); |
| 9313 | hlua_class_function(L, "getpeername", hlua_socket_getpeername); |
| 9314 | hlua_class_function(L, "getsockname", hlua_socket_getsockname); |
| 9315 | hlua_class_function(L, "setoption", hlua_socket_setoption); |
| 9316 | hlua_class_function(L, "settimeout", hlua_socket_settimeout); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9317 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9318 | 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] | 9319 | |
| 9320 | /* Register the garbage collector entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9321 | lua_pushstring(L, "__gc"); |
| 9322 | lua_pushcclosure(L, hlua_socket_gc, 0); |
| 9323 | 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] | 9324 | |
| 9325 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9326 | class_socket_ref = hlua_register_metatable(L, CLASS_SOCKET); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9327 | |
Thierry Fournier | aafc777 | 2020-12-04 11:47:47 +0100 | [diff] [blame] | 9328 | lua_atpanic(L, hlua_panic_safe); |
| 9329 | |
| 9330 | return L; |
| 9331 | } |
| 9332 | |
| 9333 | void hlua_init(void) { |
| 9334 | int i; |
Amaury Denoyelle | 239fdbf | 2021-03-24 10:22:03 +0100 | [diff] [blame] | 9335 | char *errmsg; |
Thierry Fournier | aafc777 | 2020-12-04 11:47:47 +0100 | [diff] [blame] | 9336 | #ifdef USE_OPENSSL |
| 9337 | struct srv_kw *kw; |
| 9338 | int tmp_error; |
| 9339 | char *error; |
| 9340 | char *args[] = { /* SSL client configuration. */ |
| 9341 | "ssl", |
| 9342 | "verify", |
| 9343 | "none", |
| 9344 | NULL |
| 9345 | }; |
| 9346 | #endif |
| 9347 | |
| 9348 | /* Init post init function list head */ |
| 9349 | for (i = 0; i < MAX_THREADS + 1; i++) |
| 9350 | LIST_INIT(&hlua_init_functions[i]); |
| 9351 | |
| 9352 | /* Init state for common/shared lua parts */ |
| 9353 | hlua_state_id = 0; |
| 9354 | ha_set_tid(0); |
| 9355 | hlua_states[0] = hlua_init_state(0); |
| 9356 | |
| 9357 | /* Init state 1 for thread 0. We have at least one thread. */ |
| 9358 | hlua_state_id = 1; |
| 9359 | ha_set_tid(0); |
| 9360 | hlua_states[1] = hlua_init_state(1); |
| 9361 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9362 | /* Proxy and server configuration initialisation. */ |
Amaury Denoyelle | 239fdbf | 2021-03-24 10:22:03 +0100 | [diff] [blame] | 9363 | socket_proxy = alloc_new_proxy("LUA-SOCKET", PR_CAP_FE|PR_CAP_BE|PR_CAP_LUA, &errmsg); |
| 9364 | if (!socket_proxy) { |
| 9365 | fprintf(stderr, "Lua init: %s\n", errmsg); |
| 9366 | exit(1); |
| 9367 | } |
| 9368 | proxy_preset_defaults(socket_proxy); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9369 | |
| 9370 | /* Init TCP server: unchanged parameters */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 9371 | socket_tcp = new_server(socket_proxy); |
| 9372 | if (!socket_tcp) { |
| 9373 | fprintf(stderr, "Lua init: failed to allocate tcp server socket\n"); |
| 9374 | exit(1); |
| 9375 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9376 | |
| 9377 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9378 | /* Init TCP server: unchanged parameters */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 9379 | socket_ssl = new_server(socket_proxy); |
| 9380 | if (!socket_ssl) { |
| 9381 | fprintf(stderr, "Lua init: failed to allocate ssl server socket\n"); |
| 9382 | exit(1); |
| 9383 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9384 | |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 9385 | socket_ssl->use_ssl = 1; |
| 9386 | socket_ssl->xprt = xprt_get(XPRT_SSL); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9387 | |
Bertrand Jacquin | 80839ff | 2021-01-21 19:14:46 +0000 | [diff] [blame] | 9388 | for (i = 0; args[i] != NULL; i++) { |
| 9389 | 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] | 9390 | /* |
| 9391 | * |
| 9392 | * If the keyword is not known, we can search in the registered |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 9393 | * server keywords. This is useful to configure special SSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9394 | * features like client certificates and ssl_verify. |
| 9395 | * |
| 9396 | */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 9397 | tmp_error = kw->parse(args, &i, socket_proxy, socket_ssl, &error); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9398 | if (tmp_error != 0) { |
| 9399 | fprintf(stderr, "INTERNAL ERROR: %s\n", error); |
| 9400 | abort(); /* This must be never arrives because the command line |
| 9401 | not editable by the user. */ |
| 9402 | } |
Bertrand Jacquin | 80839ff | 2021-01-21 19:14:46 +0000 | [diff] [blame] | 9403 | i += kw->skip; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9404 | } |
| 9405 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9406 | #endif |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 9407 | |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 9408 | } |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 9409 | |
Tim Duesterhus | d0c0ca2 | 2020-07-04 11:53:26 +0200 | [diff] [blame] | 9410 | static void hlua_deinit() |
| 9411 | { |
Willy Tarreau | 186f376 | 2020-12-04 11:48:12 +0100 | [diff] [blame] | 9412 | int thr; |
| 9413 | |
| 9414 | for (thr = 0; thr < MAX_THREADS+1; thr++) { |
| 9415 | if (hlua_states[thr]) |
| 9416 | lua_close(hlua_states[thr]); |
| 9417 | } |
Willy Tarreau | 430bf4a | 2021-03-04 09:45:32 +0100 | [diff] [blame] | 9418 | |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 9419 | free_server(socket_tcp); |
Willy Tarreau | 430bf4a | 2021-03-04 09:45:32 +0100 | [diff] [blame] | 9420 | |
Willy Tarreau | 0f143af | 2021-03-05 10:41:48 +0100 | [diff] [blame] | 9421 | #ifdef USE_OPENSSL |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 9422 | free_server(socket_ssl); |
Willy Tarreau | 0f143af | 2021-03-05 10:41:48 +0100 | [diff] [blame] | 9423 | #endif |
Amaury Denoyelle | 239fdbf | 2021-03-24 10:22:03 +0100 | [diff] [blame] | 9424 | |
| 9425 | free_proxy(socket_proxy); |
Tim Duesterhus | d0c0ca2 | 2020-07-04 11:53:26 +0200 | [diff] [blame] | 9426 | } |
| 9427 | |
| 9428 | REGISTER_POST_DEINIT(hlua_deinit); |
| 9429 | |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 9430 | static void hlua_register_build_options(void) |
| 9431 | { |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 9432 | char *ptr = NULL; |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 9433 | |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 9434 | memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE); |
| 9435 | hap_register_build_opts(ptr, 1); |
| 9436 | } |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 9437 | |
| 9438 | INITCALL0(STG_REGISTER, hlua_register_build_options); |