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 |
| 354 | * error if the rgument is not a "function". |
| 355 | */ |
| 356 | __LJMP unsigned int hlua_checkfunction(lua_State *L, int argno) |
| 357 | { |
| 358 | if (!lua_isfunction(L, argno)) { |
Thierry FOURNIER | fd1e955 | 2018-02-23 18:41:18 +0100 | [diff] [blame] | 359 | 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] | 360 | WILL_LJMP(luaL_argerror(L, argno, msg)); |
| 361 | } |
| 362 | lua_pushvalue(L, argno); |
| 363 | return luaL_ref(L, LUA_REGISTRYINDEX); |
| 364 | } |
| 365 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 366 | /* Return the string that is of the top of the stack. */ |
| 367 | const char *hlua_get_top_error_string(lua_State *L) |
| 368 | { |
| 369 | if (lua_gettop(L) < 1) |
| 370 | return "unknown error"; |
| 371 | if (lua_type(L, -1) != LUA_TSTRING) |
| 372 | return "unknown error"; |
| 373 | return lua_tostring(L, -1); |
| 374 | } |
| 375 | |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 376 | __LJMP const char *hlua_traceback(lua_State *L, const char* sep) |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 377 | { |
| 378 | lua_Debug ar; |
| 379 | int level = 0; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 380 | struct buffer *msg = get_trash_chunk(); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 381 | |
| 382 | while (lua_getstack(L, level++, &ar)) { |
| 383 | |
| 384 | /* Add separator */ |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 385 | if (b_data(msg)) |
| 386 | chunk_appendf(msg, "%s", sep); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 387 | |
| 388 | /* Fill fields: |
| 389 | * 'S': fills in the fields source, short_src, linedefined, lastlinedefined, and what; |
| 390 | * 'l': fills in the field currentline; |
| 391 | * 'n': fills in the field name and namewhat; |
| 392 | * 't': fills in the field istailcall; |
| 393 | */ |
| 394 | lua_getinfo(L, "Slnt", &ar); |
| 395 | |
| 396 | /* Append code localisation */ |
| 397 | if (ar.currentline > 0) |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 398 | chunk_appendf(msg, "%s:%d: ", ar.short_src, ar.currentline); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 399 | else |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 400 | chunk_appendf(msg, "%s: ", ar.short_src); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 401 | |
| 402 | /* |
| 403 | * Get function name |
| 404 | * |
| 405 | * if namewhat is no empty, name is defined. |
| 406 | * what contains "Lua" for Lua function, "C" for C function, |
| 407 | * or "main" for main code. |
| 408 | */ |
| 409 | 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] | 410 | chunk_appendf(msg, "in %s '%s'", ar.namewhat, ar.name); /* use it */ |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 411 | |
| 412 | 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] | 413 | chunk_appendf(msg, "in main chunk"); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 414 | |
| 415 | else if (*ar.what != 'C') /* for Lua functions, use <file:line> */ |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 416 | chunk_appendf(msg, "in function line %d", ar.linedefined); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 417 | |
| 418 | else /* nothing left... */ |
| 419 | chunk_appendf(msg, "?"); |
| 420 | |
| 421 | |
| 422 | /* Display tailed call */ |
| 423 | if (ar.istailcall) |
| 424 | chunk_appendf(msg, " ..."); |
| 425 | } |
| 426 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 427 | return msg->area; |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 431 | /* This function check the number of arguments available in the |
| 432 | * stack. If the number of arguments available is not the same |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 433 | * then <nb> an error is thrown. |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 434 | */ |
| 435 | __LJMP static inline void check_args(lua_State *L, int nb, char *fcn) |
| 436 | { |
| 437 | if (lua_gettop(L) == nb) |
| 438 | return; |
| 439 | WILL_LJMP(luaL_error(L, "'%s' needs %d arguments", fcn, nb)); |
| 440 | } |
| 441 | |
Mark Lakes | 22154b4 | 2018-01-29 14:38:40 -0800 | [diff] [blame] | 442 | /* This function pushes an error string prefixed by the file name |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 443 | * and the line number where the error is encountered. |
| 444 | */ |
| 445 | static int hlua_pusherror(lua_State *L, const char *fmt, ...) |
| 446 | { |
| 447 | va_list argp; |
| 448 | va_start(argp, fmt); |
| 449 | luaL_where(L, 1); |
| 450 | lua_pushvfstring(L, fmt, argp); |
| 451 | va_end(argp); |
| 452 | lua_concat(L, 2); |
| 453 | return 1; |
| 454 | } |
| 455 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 456 | /* This functions is used with sample fetch and converters. It |
| 457 | * converts the HAProxy configuration argument in a lua stack |
| 458 | * values. |
| 459 | * |
| 460 | * It takes an array of "arg", and each entry of the array is |
| 461 | * converted and pushed in the LUA stack. |
| 462 | */ |
| 463 | static int hlua_arg2lua(lua_State *L, const struct arg *arg) |
| 464 | { |
| 465 | switch (arg->type) { |
| 466 | case ARGT_SINT: |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 467 | case ARGT_TIME: |
| 468 | case ARGT_SIZE: |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 469 | lua_pushinteger(L, arg->data.sint); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 470 | break; |
| 471 | |
| 472 | case ARGT_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 473 | lua_pushlstring(L, arg->data.str.area, arg->data.str.data); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 474 | break; |
| 475 | |
| 476 | case ARGT_IPV4: |
| 477 | case ARGT_IPV6: |
| 478 | case ARGT_MSK4: |
| 479 | case ARGT_MSK6: |
| 480 | case ARGT_FE: |
| 481 | case ARGT_BE: |
| 482 | case ARGT_TAB: |
| 483 | case ARGT_SRV: |
| 484 | case ARGT_USR: |
| 485 | case ARGT_MAP: |
| 486 | default: |
| 487 | lua_pushnil(L); |
| 488 | break; |
| 489 | } |
| 490 | return 1; |
| 491 | } |
| 492 | |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 493 | /* 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] | 494 | * 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] | 495 | * with sample fetch wrappers. The input arguments are given to the |
| 496 | * lua wrapper and converted as arg list by the function. |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 497 | */ |
| 498 | static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg) |
| 499 | { |
| 500 | switch (lua_type(L, ud)) { |
| 501 | |
| 502 | case LUA_TNUMBER: |
| 503 | case LUA_TBOOLEAN: |
| 504 | arg->type = ARGT_SINT; |
| 505 | arg->data.sint = lua_tointeger(L, ud); |
| 506 | break; |
| 507 | |
| 508 | case LUA_TSTRING: |
| 509 | arg->type = ARGT_STR; |
Tim Duesterhus | 2e89dec | 2019-09-29 23:03:08 +0200 | [diff] [blame] | 510 | 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] | 511 | /* 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] | 512 | 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] | 513 | arg->data.str.head = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 514 | break; |
| 515 | |
| 516 | case LUA_TUSERDATA: |
| 517 | case LUA_TNIL: |
| 518 | case LUA_TTABLE: |
| 519 | case LUA_TFUNCTION: |
| 520 | case LUA_TTHREAD: |
| 521 | case LUA_TLIGHTUSERDATA: |
| 522 | arg->type = ARGT_SINT; |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 523 | arg->data.sint = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 524 | break; |
| 525 | } |
| 526 | return 1; |
| 527 | } |
| 528 | |
| 529 | /* the following functions are used to convert a struct sample |
| 530 | * in Lua type. This useful to convert the return of the |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 531 | * fetches or converters. |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 532 | */ |
Willy Tarreau | 5eadada | 2015-03-10 17:28:54 +0100 | [diff] [blame] | 533 | static int hlua_smp2lua(lua_State *L, struct sample *smp) |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 534 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 535 | switch (smp->data.type) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 536 | case SMP_T_SINT: |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 537 | case SMP_T_BOOL: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 538 | lua_pushinteger(L, smp->data.u.sint); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 539 | break; |
| 540 | |
| 541 | case SMP_T_BIN: |
| 542 | case SMP_T_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 543 | 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] | 544 | break; |
| 545 | |
| 546 | case SMP_T_METH: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 547 | switch (smp->data.u.meth.meth) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 548 | case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break; |
| 549 | case HTTP_METH_GET: lua_pushstring(L, "GET"); break; |
| 550 | case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break; |
| 551 | case HTTP_METH_POST: lua_pushstring(L, "POST"); break; |
| 552 | case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break; |
| 553 | case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break; |
| 554 | case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break; |
| 555 | case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break; |
| 556 | case HTTP_METH_OTHER: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 557 | 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] | 558 | break; |
| 559 | default: |
| 560 | lua_pushnil(L); |
| 561 | break; |
| 562 | } |
| 563 | break; |
| 564 | |
| 565 | case SMP_T_IPV4: |
| 566 | case SMP_T_IPV6: |
| 567 | 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] | 568 | if (sample_casts[smp->data.type][SMP_T_STR] && |
| 569 | sample_casts[smp->data.type][SMP_T_STR](smp)) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 570 | 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] | 571 | else |
| 572 | lua_pushnil(L); |
| 573 | break; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 574 | default: |
| 575 | lua_pushnil(L); |
| 576 | break; |
| 577 | } |
| 578 | return 1; |
| 579 | } |
| 580 | |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 581 | /* the following functions are used to convert a struct sample |
| 582 | * in Lua strings. This is useful to convert the return of the |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 583 | * fetches or converters. |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 584 | */ |
| 585 | static int hlua_smp2lua_str(lua_State *L, struct sample *smp) |
| 586 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 587 | switch (smp->data.type) { |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 588 | |
| 589 | case SMP_T_BIN: |
| 590 | case SMP_T_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 591 | 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] | 592 | break; |
| 593 | |
| 594 | case SMP_T_METH: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 595 | switch (smp->data.u.meth.meth) { |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 596 | case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break; |
| 597 | case HTTP_METH_GET: lua_pushstring(L, "GET"); break; |
| 598 | case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break; |
| 599 | case HTTP_METH_POST: lua_pushstring(L, "POST"); break; |
| 600 | case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break; |
| 601 | case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break; |
| 602 | case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break; |
| 603 | case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break; |
| 604 | case HTTP_METH_OTHER: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 605 | 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] | 606 | break; |
| 607 | default: |
| 608 | lua_pushstring(L, ""); |
| 609 | break; |
| 610 | } |
| 611 | break; |
| 612 | |
| 613 | case SMP_T_SINT: |
| 614 | case SMP_T_BOOL: |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 615 | case SMP_T_IPV4: |
| 616 | case SMP_T_IPV6: |
| 617 | 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] | 618 | if (sample_casts[smp->data.type][SMP_T_STR] && |
| 619 | sample_casts[smp->data.type][SMP_T_STR](smp)) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 620 | 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] | 621 | else |
| 622 | lua_pushstring(L, ""); |
| 623 | break; |
| 624 | default: |
| 625 | lua_pushstring(L, ""); |
| 626 | break; |
| 627 | } |
| 628 | return 1; |
| 629 | } |
| 630 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 631 | /* the following functions are used to convert an Lua type in a |
| 632 | * struct sample. This is useful to provide data from a converter |
| 633 | * to the LUA code. |
| 634 | */ |
| 635 | static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp) |
| 636 | { |
| 637 | switch (lua_type(L, ud)) { |
| 638 | |
| 639 | case LUA_TNUMBER: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 640 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 641 | smp->data.u.sint = lua_tointeger(L, ud); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 642 | break; |
| 643 | |
| 644 | |
| 645 | case LUA_TBOOLEAN: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 646 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 647 | smp->data.u.sint = lua_toboolean(L, ud); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 648 | break; |
| 649 | |
| 650 | case LUA_TSTRING: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 651 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 652 | smp->flags |= SMP_F_CONST; |
Tim Duesterhus | 2e89dec | 2019-09-29 23:03:08 +0200 | [diff] [blame] | 653 | 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] | 654 | /* 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] | 655 | 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] | 656 | smp->data.u.str.head = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 657 | break; |
| 658 | |
| 659 | case LUA_TUSERDATA: |
| 660 | case LUA_TNIL: |
| 661 | case LUA_TTABLE: |
| 662 | case LUA_TFUNCTION: |
| 663 | case LUA_TTHREAD: |
| 664 | case LUA_TLIGHTUSERDATA: |
Thierry FOURNIER | 93405e1 | 2015-08-26 14:19:03 +0200 | [diff] [blame] | 665 | case LUA_TNONE: |
| 666 | default: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 667 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 668 | smp->data.u.sint = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 669 | break; |
| 670 | } |
| 671 | return 1; |
| 672 | } |
| 673 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 674 | /* This function check the "argp" built by another conversion function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 675 | * 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] | 676 | * 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] | 677 | * |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 678 | * This function assumes that the argp argument contains ARGM_NBARGS + 1 |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 679 | * entries. |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 680 | */ |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 681 | __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] | 682 | uint64_t mask, struct proxy *p) |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 683 | { |
| 684 | int min_arg; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 685 | int i, idx; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 686 | struct proxy *px; |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 687 | struct userlist *ul; |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 688 | struct my_regex *reg; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 689 | const char *msg = NULL; |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 690 | char *sname, *pname, *err = NULL; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 691 | |
| 692 | idx = 0; |
| 693 | min_arg = ARGM(mask); |
| 694 | mask >>= ARGM_BITS; |
| 695 | |
| 696 | while (1) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 697 | struct buffer tmp = BUF_NULL; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 698 | |
| 699 | /* Check oversize. */ |
| 700 | if (idx >= ARGM_NBARGS && argp[idx].type != ARGT_STOP) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 701 | msg = "Malformed argument mask"; |
| 702 | goto error; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 703 | } |
| 704 | |
| 705 | /* Check for mandatory arguments. */ |
| 706 | if (argp[idx].type == ARGT_STOP) { |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 707 | if (idx < min_arg) { |
| 708 | |
| 709 | /* If miss other argument than the first one, we return an error. */ |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 710 | if (idx > 0) { |
| 711 | msg = "Mandatory argument expected"; |
| 712 | goto error; |
| 713 | } |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 714 | |
| 715 | /* If first argument have a certain type, some default values |
| 716 | * may be used. See the function smp_resolve_args(). |
| 717 | */ |
| 718 | switch (mask & ARGT_MASK) { |
| 719 | |
| 720 | case ARGT_FE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 721 | if (!(p->cap & PR_CAP_FE)) { |
| 722 | msg = "Mandatory argument expected"; |
| 723 | goto error; |
| 724 | } |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 725 | argp[idx].data.prx = p; |
| 726 | argp[idx].type = ARGT_FE; |
| 727 | argp[idx+1].type = ARGT_STOP; |
| 728 | break; |
| 729 | |
| 730 | case ARGT_BE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 731 | if (!(p->cap & PR_CAP_BE)) { |
| 732 | msg = "Mandatory argument expected"; |
| 733 | goto error; |
| 734 | } |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 735 | argp[idx].data.prx = p; |
| 736 | argp[idx].type = ARGT_BE; |
| 737 | argp[idx+1].type = ARGT_STOP; |
| 738 | break; |
| 739 | |
| 740 | case ARGT_TAB: |
| 741 | argp[idx].data.prx = p; |
| 742 | argp[idx].type = ARGT_TAB; |
| 743 | argp[idx+1].type = ARGT_STOP; |
| 744 | break; |
| 745 | |
| 746 | default: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 747 | msg = "Mandatory argument expected"; |
| 748 | goto error; |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 749 | break; |
| 750 | } |
| 751 | } |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 752 | break; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 753 | } |
| 754 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 755 | /* Check for exceed the number of required argument. */ |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 756 | if ((mask & ARGT_MASK) == ARGT_STOP && |
| 757 | argp[idx].type != ARGT_STOP) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 758 | msg = "Last argument expected"; |
| 759 | goto error; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 760 | } |
| 761 | |
| 762 | if ((mask & ARGT_MASK) == ARGT_STOP && |
| 763 | argp[idx].type == ARGT_STOP) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 764 | break; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 765 | } |
| 766 | |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 767 | /* Convert some argument types. All string in argp[] are for not |
| 768 | * duplicated yet. |
| 769 | */ |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 770 | switch (mask & ARGT_MASK) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 771 | case ARGT_SINT: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 772 | if (argp[idx].type != ARGT_SINT) { |
| 773 | msg = "integer expected"; |
| 774 | goto error; |
| 775 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 776 | argp[idx].type = ARGT_SINT; |
| 777 | break; |
| 778 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 779 | case ARGT_TIME: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 780 | if (argp[idx].type != ARGT_SINT) { |
| 781 | msg = "integer expected"; |
| 782 | goto error; |
| 783 | } |
Thierry FOURNIER | 29176f3 | 2015-07-07 00:41:29 +0200 | [diff] [blame] | 784 | argp[idx].type = ARGT_TIME; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 785 | break; |
| 786 | |
| 787 | case ARGT_SIZE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 788 | if (argp[idx].type != ARGT_SINT) { |
| 789 | msg = "integer expected"; |
| 790 | goto error; |
| 791 | } |
Thierry FOURNIER | 29176f3 | 2015-07-07 00:41:29 +0200 | [diff] [blame] | 792 | argp[idx].type = ARGT_SIZE; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 793 | break; |
| 794 | |
| 795 | case ARGT_FE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 796 | if (argp[idx].type != ARGT_STR) { |
| 797 | msg = "string expected"; |
| 798 | goto error; |
| 799 | } |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 800 | 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] | 801 | if (!argp[idx].data.prx) { |
| 802 | msg = "frontend doesn't exist"; |
| 803 | goto error; |
| 804 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 805 | argp[idx].type = ARGT_FE; |
| 806 | break; |
| 807 | |
| 808 | case ARGT_BE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 809 | if (argp[idx].type != ARGT_STR) { |
| 810 | msg = "string expected"; |
| 811 | goto error; |
| 812 | } |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 813 | 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] | 814 | if (!argp[idx].data.prx) { |
| 815 | msg = "backend doesn't exist"; |
| 816 | goto error; |
| 817 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 818 | argp[idx].type = ARGT_BE; |
| 819 | break; |
| 820 | |
| 821 | case ARGT_TAB: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 822 | if (argp[idx].type != ARGT_STR) { |
| 823 | msg = "string expected"; |
| 824 | goto error; |
| 825 | } |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 826 | 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] | 827 | if (!argp[idx].data.t) { |
| 828 | msg = "table doesn't exist"; |
| 829 | goto error; |
| 830 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 831 | argp[idx].type = ARGT_TAB; |
| 832 | break; |
| 833 | |
| 834 | case ARGT_SRV: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 835 | if (argp[idx].type != ARGT_STR) { |
| 836 | msg = "string expected"; |
| 837 | goto error; |
| 838 | } |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 839 | sname = strrchr(argp[idx].data.str.area, '/'); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 840 | if (sname) { |
| 841 | *sname++ = '\0'; |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 842 | pname = argp[idx].data.str.area; |
Willy Tarreau | 9e0bb10 | 2015-05-26 11:24:42 +0200 | [diff] [blame] | 843 | px = proxy_be_by_name(pname); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 844 | if (!px) { |
| 845 | msg = "backend doesn't exist"; |
| 846 | goto error; |
| 847 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 848 | } |
| 849 | else { |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 850 | sname = argp[idx].data.str.area; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 851 | px = p; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 852 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 853 | argp[idx].data.srv = findserver(px, sname); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 854 | if (!argp[idx].data.srv) { |
| 855 | msg = "server doesn't exist"; |
| 856 | goto error; |
| 857 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 858 | argp[idx].type = ARGT_SRV; |
| 859 | break; |
| 860 | |
| 861 | case ARGT_IPV4: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 862 | if (argp[idx].type != ARGT_STR) { |
| 863 | msg = "string expected"; |
| 864 | goto error; |
| 865 | } |
| 866 | if (inet_pton(AF_INET, argp[idx].data.str.area, &argp[idx].data.ipv4)) { |
| 867 | msg = "invalid IPv4 address"; |
| 868 | goto error; |
| 869 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 870 | argp[idx].type = ARGT_IPV4; |
| 871 | break; |
| 872 | |
| 873 | case ARGT_MSK4: |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 874 | if (argp[idx].type == ARGT_SINT) |
| 875 | len2mask4(argp[idx].data.sint, &argp[idx].data.ipv4); |
| 876 | else if (argp[idx].type == ARGT_STR) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 877 | if (!str2mask(argp[idx].data.str.area, &argp[idx].data.ipv4)) { |
| 878 | msg = "invalid IPv4 mask"; |
| 879 | goto error; |
| 880 | } |
| 881 | } |
| 882 | else { |
| 883 | msg = "integer or string expected"; |
| 884 | goto error; |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 885 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 886 | argp[idx].type = ARGT_MSK4; |
| 887 | break; |
| 888 | |
| 889 | case ARGT_IPV6: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 890 | if (argp[idx].type != ARGT_STR) { |
| 891 | msg = "string expected"; |
| 892 | goto error; |
| 893 | } |
| 894 | if (inet_pton(AF_INET6, argp[idx].data.str.area, &argp[idx].data.ipv6)) { |
| 895 | msg = "invalid IPv6 address"; |
| 896 | goto error; |
| 897 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 898 | argp[idx].type = ARGT_IPV6; |
| 899 | break; |
| 900 | |
| 901 | case ARGT_MSK6: |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 902 | if (argp[idx].type == ARGT_SINT) |
| 903 | len2mask6(argp[idx].data.sint, &argp[idx].data.ipv6); |
| 904 | else if (argp[idx].type == ARGT_STR) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 905 | if (!str2mask6(argp[idx].data.str.area, &argp[idx].data.ipv6)) { |
| 906 | msg = "invalid IPv6 mask"; |
| 907 | goto error; |
| 908 | } |
| 909 | } |
| 910 | else { |
| 911 | msg = "integer or string expected"; |
| 912 | goto error; |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 913 | } |
Tim Duesterhus | b814da6 | 2018-01-25 16:24:50 +0100 | [diff] [blame] | 914 | argp[idx].type = ARGT_MSK6; |
| 915 | break; |
| 916 | |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 917 | case ARGT_REG: |
| 918 | if (argp[idx].type != ARGT_STR) { |
| 919 | msg = "string expected"; |
| 920 | goto error; |
| 921 | } |
| 922 | reg = regex_comp(argp[idx].data.str.area, !(argp[idx].type_flags & ARGF_REG_ICASE), 1, &err); |
| 923 | if (!reg) { |
| 924 | msg = lua_pushfstring(L, "error compiling regex '%s' : '%s'", |
| 925 | argp[idx].data.str.area, err); |
| 926 | free(err); |
| 927 | goto error; |
| 928 | } |
| 929 | argp[idx].type = ARGT_REG; |
| 930 | argp[idx].data.reg = reg; |
| 931 | break; |
| 932 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 933 | case ARGT_USR: |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 934 | if (argp[idx].type != ARGT_STR) { |
| 935 | msg = "string expected"; |
| 936 | goto error; |
| 937 | } |
| 938 | if (p->uri_auth && p->uri_auth->userlist && |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 939 | strcmp(p->uri_auth->userlist->name, argp[idx].data.str.area) == 0) |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 940 | ul = p->uri_auth->userlist; |
| 941 | else |
| 942 | ul = auth_find_userlist(argp[idx].data.str.area); |
| 943 | |
| 944 | if (!ul) { |
| 945 | msg = lua_pushfstring(L, "unable to find userlist '%s'", argp[idx].data.str.area); |
| 946 | goto error; |
| 947 | } |
| 948 | argp[idx].type = ARGT_USR; |
| 949 | argp[idx].data.usr = ul; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 950 | break; |
| 951 | |
| 952 | case ARGT_STR: |
| 953 | if (!chunk_dup(&tmp, &argp[idx].data.str)) { |
| 954 | msg = "unable to duplicate string arg"; |
| 955 | goto error; |
| 956 | } |
| 957 | argp[idx].data.str = tmp; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 958 | break; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 959 | |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 960 | case ARGT_MAP: |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 961 | msg = "type not yet supported"; |
| 962 | goto error; |
| 963 | break; |
| 964 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 965 | } |
| 966 | |
| 967 | /* Check for type of argument. */ |
| 968 | if ((mask & ARGT_MASK) != argp[idx].type) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 969 | msg = lua_pushfstring(L, "'%s' expected, got '%s'", |
| 970 | arg_type_names[(mask & ARGT_MASK)], |
| 971 | arg_type_names[argp[idx].type & ARGT_MASK]); |
| 972 | goto error; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 973 | } |
| 974 | |
| 975 | /* Next argument. */ |
| 976 | mask >>= ARGT_BITS; |
| 977 | idx++; |
| 978 | } |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 979 | return 0; |
| 980 | |
| 981 | error: |
| 982 | for (i = 0; i < idx; i++) { |
| 983 | if (argp[i].type == ARGT_STR) |
| 984 | chunk_destroy(&argp[i].data.str); |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 985 | else if (argp[i].type == ARGT_REG) |
| 986 | regex_free(argp[i].data.reg); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 987 | } |
| 988 | WILL_LJMP(luaL_argerror(L, first + idx, msg)); |
| 989 | return 0; /* Never reached */ |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 990 | } |
| 991 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 992 | /* |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 993 | * The following functions are used to make correspondence between the the |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 994 | * executed lua pointer and the "struct hlua *" that contain the context. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 995 | * |
| 996 | * - hlua_gethlua : return the hlua context associated with an lua_State. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 997 | * - hlua_sethlua : create the association between hlua context and lua_state. |
| 998 | */ |
| 999 | static inline struct hlua *hlua_gethlua(lua_State *L) |
| 1000 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 1001 | struct hlua **hlua = lua_getextraspace(L); |
| 1002 | return *hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1003 | } |
| 1004 | static inline void hlua_sethlua(struct hlua *hlua) |
| 1005 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 1006 | struct hlua **hlua_store = lua_getextraspace(hlua->T); |
| 1007 | *hlua_store = hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1008 | } |
| 1009 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1010 | /* This function is used to send logs. It try to send on screen (stderr) |
| 1011 | * and on the default syslog server. |
| 1012 | */ |
| 1013 | static inline void hlua_sendlog(struct proxy *px, int level, const char *msg) |
| 1014 | { |
| 1015 | struct tm tm; |
| 1016 | char *p; |
| 1017 | |
| 1018 | /* Cleanup the log message. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1019 | p = trash.area; |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1020 | for (; *msg != '\0'; msg++, p++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1021 | if (p >= trash.area + trash.size - 1) { |
Thierry FOURNIER | ccf0063 | 2015-09-16 12:47:03 +0200 | [diff] [blame] | 1022 | /* Break the message if exceed the buffer size. */ |
| 1023 | *(p-4) = ' '; |
| 1024 | *(p-3) = '.'; |
| 1025 | *(p-2) = '.'; |
| 1026 | *(p-1) = '.'; |
| 1027 | break; |
| 1028 | } |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 1029 | if (isprint((unsigned char)*msg)) |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1030 | *p = *msg; |
| 1031 | else |
| 1032 | *p = '.'; |
| 1033 | } |
| 1034 | *p = '\0'; |
| 1035 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1036 | send_log(px, level, "%s\n", trash.area); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1037 | if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) { |
Christopher Faulet | f98d821 | 2020-10-02 18:13:52 +0200 | [diff] [blame] | 1038 | if (level == LOG_DEBUG && !(global.mode & MODE_DEBUG)) |
| 1039 | return; |
| 1040 | |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 1041 | get_localtime(date.tv_sec, &tm); |
| 1042 | fprintf(stderr, "[%s] %03d/%02d%02d%02d (%d) : %s\n", |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1043 | 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] | 1044 | (int)getpid(), trash.area); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1045 | fflush(stderr); |
| 1046 | } |
| 1047 | } |
| 1048 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1049 | /* This function just ensure that the yield will be always |
| 1050 | * returned with a timeout and permit to set some flags |
| 1051 | */ |
| 1052 | __LJMP void hlua_yieldk(lua_State *L, int nresults, int ctx, |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1053 | lua_KFunction k, int timeout, unsigned int flags) |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1054 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 1055 | struct hlua *hlua; |
| 1056 | |
| 1057 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 1058 | hlua = hlua_gethlua(L); |
| 1059 | if (!hlua) { |
| 1060 | return; |
| 1061 | } |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1062 | |
| 1063 | /* Set the wake timeout. If timeout is required, we set |
| 1064 | * the expiration time. |
| 1065 | */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1066 | hlua->wake_time = timeout; |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1067 | |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 1068 | hlua->flags |= flags; |
| 1069 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1070 | /* Process the yield. */ |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 1071 | MAY_LJMP(lua_yieldk(L, nresults, ctx, k)); |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1072 | } |
| 1073 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1074 | /* This function initialises the Lua environment stored in the stream. |
| 1075 | * 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] | 1076 | * 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] | 1077 | * |
| 1078 | * This function is particular. it initialises a new Lua thread. If the |
| 1079 | * initialisation fails (example: out of memory error), the lua function |
| 1080 | * throws an error (longjmp). |
| 1081 | * |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1082 | * 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] | 1083 | * environment, so we must not initialise it. While the support of |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1084 | * threads appear, the safe environment set a lock to ensure only one |
| 1085 | * Lua execution at a time. If we initialize safe environment in another |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1086 | * safe environment, we have a dead lock. |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1087 | * |
| 1088 | * set "already_safe" true if the context is initialized form safe |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1089 | * Lua function. |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1090 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1091 | * This function manipulates two Lua stacks: the main and the thread. Only |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 1092 | * the main stack can fail. The thread is not manipulated. This function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1093 | * MUST NOT manipulate the created thread stack state, because it is not |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1094 | * protected against errors thrown by the thread stack. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1095 | */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1096 | 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] | 1097 | { |
| 1098 | lua->Mref = LUA_REFNIL; |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1099 | lua->flags = 0; |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 1100 | lua->gc_count = 0; |
Christopher Faulet | bc275a9 | 2020-02-26 14:55:16 +0100 | [diff] [blame] | 1101 | lua->wake_time = TICK_ETERNITY; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1102 | lua->state_id = state_id; |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 1103 | LIST_INIT(&lua->com); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1104 | if (!already_safe) { |
| 1105 | if (!SET_SAFE_LJMP_PARENT(lua)) { |
| 1106 | lua->Tref = LUA_REFNIL; |
| 1107 | return 0; |
| 1108 | } |
| 1109 | } |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1110 | lua->T = lua_newthread(hlua_states[state_id]); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1111 | if (!lua->T) { |
| 1112 | lua->Tref = LUA_REFNIL; |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1113 | if (!already_safe) |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1114 | RESET_SAFE_LJMP_PARENT(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1115 | return 0; |
| 1116 | } |
| 1117 | hlua_sethlua(lua); |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1118 | lua->Tref = luaL_ref(hlua_states[state_id], LUA_REGISTRYINDEX); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1119 | lua->task = task; |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1120 | if (!already_safe) |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1121 | RESET_SAFE_LJMP_PARENT(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1122 | return 1; |
| 1123 | } |
| 1124 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1125 | /* Used to destroy the Lua coroutine when the attached stream or task |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1126 | * is destroyed. The destroy also the memory context. The struct "lua" |
| 1127 | * is not freed. |
| 1128 | */ |
| 1129 | void hlua_ctx_destroy(struct hlua *lua) |
| 1130 | { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 1131 | if (!lua) |
Thierry FOURNIER | a718b29 | 2015-03-04 16:48:34 +0100 | [diff] [blame] | 1132 | return; |
| 1133 | |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 1134 | if (!lua->T) |
| 1135 | goto end; |
| 1136 | |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 1137 | /* Purge all the pending signals. */ |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1138 | notification_purge(&lua->com); |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 1139 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1140 | if (!SET_SAFE_LJMP(lua)) |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 1141 | return; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1142 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1143 | RESET_SAFE_LJMP(lua); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 1144 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1145 | if (!SET_SAFE_LJMP_PARENT(lua)) |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 1146 | return; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1147 | luaL_unref(hlua_states[lua->state_id], LUA_REGISTRYINDEX, lua->Tref); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1148 | RESET_SAFE_LJMP_PARENT(lua); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 1149 | /* Forces a garbage collecting process. If the Lua program is finished |
| 1150 | * without error, we run the GC on the thread pointer. Its freed all |
| 1151 | * the unused memory. |
| 1152 | * If the thread is finnish with an error or is currently yielded, |
| 1153 | * it seems that the GC applied on the thread doesn't clean anything, |
| 1154 | * so e run the GC on the main thread. |
| 1155 | * NOTE: maybe this action locks all the Lua threads untiml the en of |
| 1156 | * the garbage collection. |
| 1157 | */ |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 1158 | if (lua->gc_count) { |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1159 | if (!SET_SAFE_LJMP_PARENT(lua)) |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 1160 | return; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1161 | lua_gc(hlua_states[lua->state_id], LUA_GCCOLLECT, 0); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1162 | RESET_SAFE_LJMP_PARENT(lua); |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 1163 | } |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 1164 | |
Thierry FOURNIER | a7b536b | 2015-09-21 22:50:24 +0200 | [diff] [blame] | 1165 | lua->T = NULL; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 1166 | |
| 1167 | end: |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 1168 | pool_free(pool_head_hlua, lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1169 | } |
| 1170 | |
| 1171 | /* This function is used to restore the Lua context when a coroutine |
| 1172 | * fails. This function copy the common memory between old coroutine |
| 1173 | * and the new coroutine. The old coroutine is destroyed, and its |
| 1174 | * replaced by the new coroutine. |
| 1175 | * If the flag "keep_msg" is set, the last entry of the old is assumed |
| 1176 | * as string error message and it is copied in the new stack. |
| 1177 | */ |
| 1178 | static int hlua_ctx_renew(struct hlua *lua, int keep_msg) |
| 1179 | { |
| 1180 | lua_State *T; |
| 1181 | int new_ref; |
| 1182 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1183 | /* New Lua coroutine. */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1184 | T = lua_newthread(hlua_states[lua->state_id]); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1185 | if (!T) |
| 1186 | return 0; |
| 1187 | |
| 1188 | /* Copy last error message. */ |
| 1189 | if (keep_msg) |
| 1190 | lua_xmove(lua->T, T, 1); |
| 1191 | |
| 1192 | /* Copy data between the coroutines. */ |
| 1193 | lua_rawgeti(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 1194 | lua_xmove(lua->T, T, 1); |
Ilya Shipitsin | 46a030c | 2020-07-05 16:36:08 +0500 | [diff] [blame] | 1195 | new_ref = luaL_ref(T, LUA_REGISTRYINDEX); /* Value popped. */ |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1196 | |
| 1197 | /* Destroy old data. */ |
| 1198 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 1199 | |
| 1200 | /* The thread is garbage collected by Lua. */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1201 | luaL_unref(hlua_states[lua->state_id], LUA_REGISTRYINDEX, lua->Tref); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1202 | |
| 1203 | /* Fill the struct with the new coroutine values. */ |
| 1204 | lua->Mref = new_ref; |
| 1205 | lua->T = T; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1206 | lua->Tref = luaL_ref(hlua_states[lua->state_id], LUA_REGISTRYINDEX); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1207 | |
| 1208 | /* Set context. */ |
| 1209 | hlua_sethlua(lua); |
| 1210 | |
| 1211 | return 1; |
| 1212 | } |
| 1213 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1214 | void hlua_hook(lua_State *L, lua_Debug *ar) |
| 1215 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 1216 | struct hlua *hlua; |
| 1217 | |
| 1218 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 1219 | hlua = hlua_gethlua(L); |
| 1220 | if (!hlua) |
| 1221 | return; |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1222 | |
| 1223 | /* Lua cannot yield when its returning from a function, |
| 1224 | * so, we can fix the interrupt hook to 1 instruction, |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1225 | * expecting that the function is finished. |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1226 | */ |
| 1227 | if (lua_gethookmask(L) & LUA_MASKRET) { |
| 1228 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, 1); |
| 1229 | return; |
| 1230 | } |
| 1231 | |
| 1232 | /* restore the interrupt condition. */ |
| 1233 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 1234 | |
| 1235 | /* If we interrupt the Lua processing in yieldable state, we yield. |
| 1236 | * If the state is not yieldable, trying yield causes an error. |
| 1237 | */ |
| 1238 | if (lua_isyieldable(L)) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 1239 | MAY_LJMP(hlua_yieldk(L, 0, 0, NULL, TICK_ETERNITY, HLUA_CTRLYIELD)); |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1240 | |
Thierry FOURNIER | a85cfb1 | 2015-03-13 14:50:06 +0100 | [diff] [blame] | 1241 | /* If we cannot yield, update the clock and check the timeout. */ |
| 1242 | tv_update_date(0, 1); |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1243 | hlua->run_time += now_ms - hlua->start_time; |
| 1244 | if (hlua->max_time && hlua->run_time >= hlua->max_time) { |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1245 | lua_pushfstring(L, "execution timeout"); |
| 1246 | WILL_LJMP(lua_error(L)); |
| 1247 | } |
| 1248 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1249 | /* Update the start time. */ |
| 1250 | hlua->start_time = now_ms; |
| 1251 | |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1252 | /* Try to interrupt the process at the end of the current |
| 1253 | * unyieldable function. |
| 1254 | */ |
| 1255 | 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] | 1256 | } |
| 1257 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1258 | /* This function start or resumes the Lua stack execution. If the flag |
| 1259 | * "yield_allowed" if no set and the LUA stack execution returns a yield |
| 1260 | * The function return an error. |
| 1261 | * |
| 1262 | * The function can returns 4 values: |
| 1263 | * - HLUA_E_OK : The execution is terminated without any errors. |
| 1264 | * - HLUA_E_AGAIN : The execution must continue at the next associated |
| 1265 | * task wakeup. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1266 | * - 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] | 1267 | * the top of the stack. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1268 | * - HLUA_E_ERR : An error has occurred without error message. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1269 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1270 | * 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] | 1271 | * LUA code. |
| 1272 | */ |
| 1273 | static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed) |
| 1274 | { |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1275 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
| 1276 | int nres; |
| 1277 | #endif |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1278 | int ret; |
| 1279 | const char *msg; |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1280 | const char *trace; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1281 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1282 | /* Initialise run time counter. */ |
| 1283 | if (!HLUA_IS_RUNNING(lua)) |
| 1284 | lua->run_time = 0; |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1285 | |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1286 | /* Lock the whole Lua execution. This lock must be before the |
| 1287 | * label "resume_execution". |
| 1288 | */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1289 | if (lua->state_id == 0) |
Willy Tarreau | 3eb2e47 | 2021-08-20 15:47:25 +0200 | [diff] [blame] | 1290 | lua_take_global_lock(); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1291 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1292 | resume_execution: |
| 1293 | |
| 1294 | /* This hook interrupts the Lua processing each 'hlua_nb_instruction' |
| 1295 | * instructions. it is used for preventing infinite loops. |
| 1296 | */ |
| 1297 | lua_sethook(lua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 1298 | |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1299 | /* Remove all flags except the running flags. */ |
Thierry FOURNIER | 2f3867f | 2015-09-28 01:02:01 +0200 | [diff] [blame] | 1300 | HLUA_SET_RUN(lua); |
| 1301 | HLUA_CLR_CTRLYIELD(lua); |
| 1302 | HLUA_CLR_WAKERESWR(lua); |
| 1303 | HLUA_CLR_WAKEREQWR(lua); |
Christopher Faulet | a6e792f | 2021-08-04 17:58:21 +0200 | [diff] [blame^] | 1304 | HLUA_CLR_NOYIELD(lua); |
| 1305 | if (!yield_allowed) |
| 1306 | HLUA_SET_NOYIELD(lua); |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1307 | |
Christopher Faulet | bc275a9 | 2020-02-26 14:55:16 +0100 | [diff] [blame] | 1308 | /* Update the start time and reset wake_time. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1309 | lua->start_time = now_ms; |
Christopher Faulet | bc275a9 | 2020-02-26 14:55:16 +0100 | [diff] [blame] | 1310 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1311 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1312 | /* Call the function. */ |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1313 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1314 | 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] | 1315 | #else |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1316 | ret = lua_resume(lua->T, hlua_states[lua->state_id], lua->nargs); |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1317 | #endif |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1318 | switch (ret) { |
| 1319 | |
| 1320 | case LUA_OK: |
| 1321 | ret = HLUA_E_OK; |
| 1322 | break; |
| 1323 | |
| 1324 | case LUA_YIELD: |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1325 | /* Check if the execution timeout is expired. It it is the case, we |
| 1326 | * break the Lua execution. |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1327 | */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1328 | tv_update_date(0, 1); |
| 1329 | lua->run_time += now_ms - lua->start_time; |
| 1330 | if (lua->max_time && lua->run_time > lua->max_time) { |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1331 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1332 | ret = HLUA_E_ETMOUT; |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1333 | break; |
| 1334 | } |
| 1335 | /* Process the forced yield. if the general yield is not allowed or |
| 1336 | * if no task were associated this the current Lua execution |
| 1337 | * coroutine, we resume the execution. Else we want to return in the |
| 1338 | * scheduler and we want to be waked up again, to continue the |
| 1339 | * current Lua execution. So we schedule our own task. |
| 1340 | */ |
| 1341 | if (HLUA_IS_CTRLYIELDING(lua)) { |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1342 | if (!yield_allowed || !lua->task) |
| 1343 | goto resume_execution; |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1344 | task_wakeup(lua->task, TASK_WOKEN_MSG); |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1345 | } |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1346 | if (!yield_allowed) { |
| 1347 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1348 | ret = HLUA_E_YIELD; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1349 | break; |
| 1350 | } |
| 1351 | ret = HLUA_E_AGAIN; |
| 1352 | break; |
| 1353 | |
| 1354 | case LUA_ERRRUN: |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1355 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1356 | /* Special exit case. The traditional exit is returned as an error |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1357 | * because the errors ares the only one mean to return immediately |
| 1358 | * from and lua execution. |
| 1359 | */ |
| 1360 | if (lua->flags & HLUA_EXIT) { |
| 1361 | ret = HLUA_E_OK; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 1362 | hlua_ctx_renew(lua, 1); |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1363 | break; |
| 1364 | } |
| 1365 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1366 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1367 | if (!lua_checkstack(lua->T, 1)) { |
| 1368 | ret = HLUA_E_ERR; |
| 1369 | break; |
| 1370 | } |
| 1371 | msg = lua_tostring(lua->T, -1); |
| 1372 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1373 | lua_pop(lua->T, 1); |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 1374 | trace = hlua_traceback(lua->T, ", "); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1375 | if (msg) |
Thierry Fournier | 46278ff | 2020-11-29 11:48:12 +0100 | [diff] [blame] | 1376 | 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] | 1377 | else |
Thierry Fournier | 46278ff | 2020-11-29 11:48:12 +0100 | [diff] [blame] | 1378 | 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] | 1379 | ret = HLUA_E_ERRMSG; |
| 1380 | break; |
| 1381 | |
| 1382 | case LUA_ERRMEM: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1383 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1384 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1385 | ret = HLUA_E_NOMEM; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1386 | break; |
| 1387 | |
| 1388 | case LUA_ERRERR: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1389 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1390 | if (!lua_checkstack(lua->T, 1)) { |
| 1391 | ret = HLUA_E_ERR; |
| 1392 | break; |
| 1393 | } |
| 1394 | msg = lua_tostring(lua->T, -1); |
| 1395 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1396 | lua_pop(lua->T, 1); |
| 1397 | if (msg) |
Thierry Fournier | 46278ff | 2020-11-29 11:48:12 +0100 | [diff] [blame] | 1398 | 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] | 1399 | else |
Thierry Fournier | 46278ff | 2020-11-29 11:48:12 +0100 | [diff] [blame] | 1400 | 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] | 1401 | ret = HLUA_E_ERRMSG; |
| 1402 | break; |
| 1403 | |
| 1404 | default: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1405 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1406 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1407 | ret = HLUA_E_ERR; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1408 | break; |
| 1409 | } |
| 1410 | |
| 1411 | switch (ret) { |
| 1412 | case HLUA_E_AGAIN: |
| 1413 | break; |
| 1414 | |
| 1415 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1416 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1417 | hlua_ctx_renew(lua, 1); |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1418 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1419 | break; |
| 1420 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1421 | case HLUA_E_ETMOUT: |
| 1422 | case HLUA_E_NOMEM: |
| 1423 | case HLUA_E_YIELD: |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1424 | case HLUA_E_ERR: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1425 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1426 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1427 | hlua_ctx_renew(lua, 0); |
| 1428 | break; |
| 1429 | |
| 1430 | case HLUA_E_OK: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1431 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1432 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1433 | break; |
| 1434 | } |
| 1435 | |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1436 | /* This is the main exit point, remove the Lua lock. */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1437 | if (lua->state_id == 0) |
Willy Tarreau | 3eb2e47 | 2021-08-20 15:47:25 +0200 | [diff] [blame] | 1438 | lua_drop_global_lock(); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1439 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1440 | return ret; |
| 1441 | } |
| 1442 | |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1443 | /* This function exit the current code. */ |
| 1444 | __LJMP static int hlua_done(lua_State *L) |
| 1445 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 1446 | struct hlua *hlua; |
| 1447 | |
| 1448 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 1449 | hlua = hlua_gethlua(L); |
| 1450 | if (!hlua) |
| 1451 | return 0; |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1452 | |
| 1453 | hlua->flags |= HLUA_EXIT; |
| 1454 | WILL_LJMP(lua_error(L)); |
| 1455 | |
| 1456 | return 0; |
| 1457 | } |
| 1458 | |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1459 | /* This function is an LUA binding. It provides a function |
| 1460 | * for deleting ACL from a referenced ACL file. |
| 1461 | */ |
| 1462 | __LJMP static int hlua_del_acl(lua_State *L) |
| 1463 | { |
| 1464 | const char *name; |
| 1465 | const char *key; |
| 1466 | struct pat_ref *ref; |
| 1467 | |
| 1468 | MAY_LJMP(check_args(L, 2, "del_acl")); |
| 1469 | |
| 1470 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1471 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1472 | |
| 1473 | ref = pat_ref_lookup(name); |
| 1474 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1475 | WILL_LJMP(luaL_error(L, "'del_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1476 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1477 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1478 | pat_ref_delete(ref, key); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1479 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1480 | return 0; |
| 1481 | } |
| 1482 | |
| 1483 | /* This function is an LUA binding. It provides a function |
| 1484 | * for deleting map entry from a referenced map file. |
| 1485 | */ |
| 1486 | static int hlua_del_map(lua_State *L) |
| 1487 | { |
| 1488 | const char *name; |
| 1489 | const char *key; |
| 1490 | struct pat_ref *ref; |
| 1491 | |
| 1492 | MAY_LJMP(check_args(L, 2, "del_map")); |
| 1493 | |
| 1494 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1495 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1496 | |
| 1497 | ref = pat_ref_lookup(name); |
| 1498 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1499 | WILL_LJMP(luaL_error(L, "'del_map': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1500 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1501 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1502 | pat_ref_delete(ref, key); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1503 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1504 | return 0; |
| 1505 | } |
| 1506 | |
| 1507 | /* This function is an LUA binding. It provides a function |
| 1508 | * for adding ACL pattern from a referenced ACL file. |
| 1509 | */ |
| 1510 | static int hlua_add_acl(lua_State *L) |
| 1511 | { |
| 1512 | const char *name; |
| 1513 | const char *key; |
| 1514 | struct pat_ref *ref; |
| 1515 | |
| 1516 | MAY_LJMP(check_args(L, 2, "add_acl")); |
| 1517 | |
| 1518 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1519 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1520 | |
| 1521 | ref = pat_ref_lookup(name); |
| 1522 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1523 | WILL_LJMP(luaL_error(L, "'add_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1524 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1525 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1526 | if (pat_ref_find_elt(ref, key) == NULL) |
| 1527 | pat_ref_add(ref, key, NULL, NULL); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1528 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1529 | return 0; |
| 1530 | } |
| 1531 | |
| 1532 | /* This function is an LUA binding. It provides a function |
| 1533 | * for setting map pattern and sample from a referenced map |
| 1534 | * file. |
| 1535 | */ |
| 1536 | static int hlua_set_map(lua_State *L) |
| 1537 | { |
| 1538 | const char *name; |
| 1539 | const char *key; |
| 1540 | const char *value; |
| 1541 | struct pat_ref *ref; |
| 1542 | |
| 1543 | MAY_LJMP(check_args(L, 3, "set_map")); |
| 1544 | |
| 1545 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1546 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1547 | value = MAY_LJMP(luaL_checkstring(L, 3)); |
| 1548 | |
| 1549 | ref = pat_ref_lookup(name); |
| 1550 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1551 | WILL_LJMP(luaL_error(L, "'set_map': unknown map file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1552 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1553 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1554 | if (pat_ref_find_elt(ref, key) != NULL) |
| 1555 | pat_ref_set(ref, key, value, NULL); |
| 1556 | else |
| 1557 | pat_ref_add(ref, key, value, NULL); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1558 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1559 | return 0; |
| 1560 | } |
| 1561 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1562 | /* A class is a lot of memory that contain data. This data can be a table, |
| 1563 | * 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] | 1564 | * metatable have an original version registered in the global context with |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1565 | * the name of the object (_G[<name>] = <metable> ). |
| 1566 | * |
| 1567 | * A metable is a table that modify the standard behavior of a standard |
| 1568 | * access to the associated data. The entries of this new metatable are |
| 1569 | * defined as is: |
| 1570 | * |
| 1571 | * http://lua-users.org/wiki/MetatableEvents |
| 1572 | * |
| 1573 | * __index |
| 1574 | * |
| 1575 | * we access an absent field in a table, the result is nil. This is |
| 1576 | * true, but it is not the whole truth. Actually, such access triggers |
| 1577 | * the interpreter to look for an __index metamethod: If there is no |
| 1578 | * such method, as usually happens, then the access results in nil; |
| 1579 | * otherwise, the metamethod will provide the result. |
| 1580 | * |
| 1581 | * Control 'prototype' inheritance. When accessing "myTable[key]" and |
| 1582 | * the key does not appear in the table, but the metatable has an __index |
| 1583 | * property: |
| 1584 | * |
| 1585 | * - if the value is a function, the function is called, passing in the |
| 1586 | * table and the key; the return value of that function is returned as |
| 1587 | * the result. |
| 1588 | * |
| 1589 | * - if the value is another table, the value of the key in that table is |
| 1590 | * asked for and returned (and if it doesn't exist in that table, but that |
| 1591 | * table's metatable has an __index property, then it continues on up) |
| 1592 | * |
| 1593 | * - Use "rawget(myTable,key)" to skip this metamethod. |
| 1594 | * |
| 1595 | * http://www.lua.org/pil/13.4.1.html |
| 1596 | * |
| 1597 | * __newindex |
| 1598 | * |
| 1599 | * Like __index, but control property assignment. |
| 1600 | * |
| 1601 | * __mode - Control weak references. A string value with one or both |
| 1602 | * of the characters 'k' and 'v' which specifies that the the |
| 1603 | * keys and/or values in the table are weak references. |
| 1604 | * |
| 1605 | * __call - Treat a table like a function. When a table is followed by |
| 1606 | * parenthesis such as "myTable( 'foo' )" and the metatable has |
| 1607 | * a __call key pointing to a function, that function is invoked |
| 1608 | * (passing any specified arguments) and the return value is |
| 1609 | * returned. |
| 1610 | * |
| 1611 | * __metatable - Hide the metatable. When "getmetatable( myTable )" is |
| 1612 | * called, if the metatable for myTable has a __metatable |
| 1613 | * key, the value of that key is returned instead of the |
| 1614 | * actual metatable. |
| 1615 | * |
| 1616 | * __tostring - Control string representation. When the builtin |
| 1617 | * "tostring( myTable )" function is called, if the metatable |
| 1618 | * for myTable has a __tostring property set to a function, |
| 1619 | * that function is invoked (passing myTable to it) and the |
| 1620 | * return value is used as the string representation. |
| 1621 | * |
| 1622 | * __len - Control table length. When the table length is requested using |
| 1623 | * the length operator ( '#' ), if the metatable for myTable has |
| 1624 | * a __len key pointing to a function, that function is invoked |
| 1625 | * (passing myTable to it) and the return value used as the value |
| 1626 | * of "#myTable". |
| 1627 | * |
| 1628 | * __gc - Userdata finalizer code. When userdata is set to be garbage |
| 1629 | * collected, if the metatable has a __gc field pointing to a |
| 1630 | * function, that function is first invoked, passing the userdata |
| 1631 | * to it. The __gc metamethod is not called for tables. |
| 1632 | * (See http://lua-users.org/lists/lua-l/2006-11/msg00508.html) |
| 1633 | * |
| 1634 | * Special metamethods for redefining standard operators: |
| 1635 | * http://www.lua.org/pil/13.1.html |
| 1636 | * |
| 1637 | * __add "+" |
| 1638 | * __sub "-" |
| 1639 | * __mul "*" |
| 1640 | * __div "/" |
| 1641 | * __unm "!" |
| 1642 | * __pow "^" |
| 1643 | * __concat ".." |
| 1644 | * |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1645 | * Special methods for redefining standard relations |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1646 | * http://www.lua.org/pil/13.2.html |
| 1647 | * |
| 1648 | * __eq "==" |
| 1649 | * __lt "<" |
| 1650 | * __le "<=" |
| 1651 | */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1652 | |
| 1653 | /* |
| 1654 | * |
| 1655 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1656 | * Class Map |
| 1657 | * |
| 1658 | * |
| 1659 | */ |
| 1660 | |
| 1661 | /* Returns a struct hlua_map if the stack entry "ud" is |
| 1662 | * a class session, otherwise it throws an error. |
| 1663 | */ |
| 1664 | __LJMP static struct map_descriptor *hlua_checkmap(lua_State *L, int ud) |
| 1665 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1666 | return MAY_LJMP(hlua_checkudata(L, ud, class_map_ref)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1667 | } |
| 1668 | |
| 1669 | /* This function is the map constructor. It don't need |
| 1670 | * the class Map object. It creates and return a new Map |
| 1671 | * object. It must be called only during "body" or "init" |
| 1672 | * context because it process some filesystem accesses. |
| 1673 | */ |
| 1674 | __LJMP static int hlua_map_new(struct lua_State *L) |
| 1675 | { |
| 1676 | const char *fn; |
| 1677 | int match = PAT_MATCH_STR; |
| 1678 | struct sample_conv conv; |
| 1679 | const char *file = ""; |
| 1680 | int line = 0; |
| 1681 | lua_Debug ar; |
| 1682 | char *err = NULL; |
| 1683 | struct arg args[2]; |
| 1684 | |
| 1685 | if (lua_gettop(L) < 1 || lua_gettop(L) > 2) |
| 1686 | WILL_LJMP(luaL_error(L, "'new' needs at least 1 argument.")); |
| 1687 | |
| 1688 | fn = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1689 | |
| 1690 | if (lua_gettop(L) >= 2) { |
| 1691 | match = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 1692 | if (match < 0 || match >= PAT_MATCH_NUM) |
| 1693 | WILL_LJMP(luaL_error(L, "'new' needs a valid match method.")); |
| 1694 | } |
| 1695 | |
| 1696 | /* Get Lua filename and line number. */ |
| 1697 | if (lua_getstack(L, 1, &ar)) { /* check function at level */ |
| 1698 | lua_getinfo(L, "Sl", &ar); /* get info about it */ |
| 1699 | if (ar.currentline > 0) { /* is there info? */ |
| 1700 | file = ar.short_src; |
| 1701 | line = ar.currentline; |
| 1702 | } |
| 1703 | } |
| 1704 | |
| 1705 | /* fill fake sample_conv struct. */ |
| 1706 | conv.kw = ""; /* unused. */ |
| 1707 | conv.process = NULL; /* unused. */ |
| 1708 | conv.arg_mask = 0; /* unused. */ |
| 1709 | conv.val_args = NULL; /* unused. */ |
| 1710 | conv.out_type = SMP_T_STR; |
| 1711 | conv.private = (void *)(long)match; |
| 1712 | switch (match) { |
| 1713 | case PAT_MATCH_STR: conv.in_type = SMP_T_STR; break; |
| 1714 | case PAT_MATCH_BEG: conv.in_type = SMP_T_STR; break; |
| 1715 | case PAT_MATCH_SUB: conv.in_type = SMP_T_STR; break; |
| 1716 | case PAT_MATCH_DIR: conv.in_type = SMP_T_STR; break; |
| 1717 | case PAT_MATCH_DOM: conv.in_type = SMP_T_STR; break; |
| 1718 | case PAT_MATCH_END: conv.in_type = SMP_T_STR; break; |
| 1719 | case PAT_MATCH_REG: conv.in_type = SMP_T_STR; break; |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1720 | case PAT_MATCH_INT: conv.in_type = SMP_T_SINT; break; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1721 | case PAT_MATCH_IP: conv.in_type = SMP_T_ADDR; break; |
| 1722 | default: |
| 1723 | WILL_LJMP(luaL_error(L, "'new' doesn't support this match mode.")); |
| 1724 | } |
| 1725 | |
| 1726 | /* fill fake args. */ |
| 1727 | args[0].type = ARGT_STR; |
Christopher Faulet | 73292e9 | 2020-08-06 08:40:09 +0200 | [diff] [blame] | 1728 | args[0].data.str.area = strdup(fn); |
| 1729 | args[0].data.str.data = strlen(fn); |
| 1730 | args[0].data.str.size = args[0].data.str.data+1; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1731 | args[1].type = ARGT_STOP; |
| 1732 | |
| 1733 | /* load the map. */ |
| 1734 | if (!sample_load_map(args, &conv, file, line, &err)) { |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 1735 | /* error case: we can't use luaL_error because we must |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1736 | * free the err variable. |
| 1737 | */ |
| 1738 | luaL_where(L, 1); |
| 1739 | lua_pushfstring(L, "'new': %s.", err); |
| 1740 | lua_concat(L, 2); |
| 1741 | free(err); |
Christopher Faulet | 6ad7df4 | 2020-08-07 11:45:18 +0200 | [diff] [blame] | 1742 | chunk_destroy(&args[0].data.str); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1743 | WILL_LJMP(lua_error(L)); |
| 1744 | } |
| 1745 | |
| 1746 | /* create the lua object. */ |
| 1747 | lua_newtable(L); |
| 1748 | lua_pushlightuserdata(L, args[0].data.map); |
| 1749 | lua_rawseti(L, -2, 0); |
| 1750 | |
| 1751 | /* Pop a class Map metatable and affect it to the userdata. */ |
| 1752 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_map_ref); |
| 1753 | lua_setmetatable(L, -2); |
| 1754 | |
| 1755 | |
| 1756 | return 1; |
| 1757 | } |
| 1758 | |
| 1759 | __LJMP static inline int _hlua_map_lookup(struct lua_State *L, int str) |
| 1760 | { |
| 1761 | struct map_descriptor *desc; |
| 1762 | struct pattern *pat; |
| 1763 | struct sample smp; |
| 1764 | |
| 1765 | MAY_LJMP(check_args(L, 2, "lookup")); |
| 1766 | desc = MAY_LJMP(hlua_checkmap(L, 1)); |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1767 | if (desc->pat.expect_type == SMP_T_SINT) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1768 | smp.data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1769 | smp.data.u.sint = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1770 | } |
| 1771 | else { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1772 | smp.data.type = SMP_T_STR; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1773 | smp.flags = SMP_F_CONST; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1774 | 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] | 1775 | smp.data.u.str.size = smp.data.u.str.data + 1; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1776 | } |
| 1777 | |
| 1778 | pat = pattern_exec_match(&desc->pat, &smp, 1); |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1779 | if (!pat || !pat->data) { |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1780 | if (str) |
| 1781 | lua_pushstring(L, ""); |
| 1782 | else |
| 1783 | lua_pushnil(L); |
| 1784 | return 1; |
| 1785 | } |
| 1786 | |
| 1787 | /* 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] | 1788 | 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] | 1789 | return 1; |
| 1790 | } |
| 1791 | |
| 1792 | __LJMP static int hlua_map_lookup(struct lua_State *L) |
| 1793 | { |
| 1794 | return _hlua_map_lookup(L, 0); |
| 1795 | } |
| 1796 | |
| 1797 | __LJMP static int hlua_map_slookup(struct lua_State *L) |
| 1798 | { |
| 1799 | return _hlua_map_lookup(L, 1); |
| 1800 | } |
| 1801 | |
| 1802 | /* |
| 1803 | * |
| 1804 | * |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1805 | * Class Socket |
| 1806 | * |
| 1807 | * |
| 1808 | */ |
| 1809 | |
| 1810 | __LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud) |
| 1811 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1812 | return MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1813 | } |
| 1814 | |
| 1815 | /* 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] | 1816 | * connection. It is used for notify space available to send or data |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1817 | * received. |
| 1818 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1819 | static void hlua_socket_handler(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1820 | { |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1821 | struct stream_interface *si = appctx->owner; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1822 | |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1823 | if (appctx->ctx.hlua_cosocket.die) { |
| 1824 | si_shutw(si); |
| 1825 | si_shutr(si); |
| 1826 | si_ic(si)->flags |= CF_READ_NULL; |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1827 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1828 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1829 | stream_shutdown(si_strm(si), SF_ERR_KILLED); |
| 1830 | } |
| 1831 | |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 1832 | /* If we can't write, wakeup the pending write signals. */ |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1833 | if (channel_output_closed(si_ic(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1834 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1835 | |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 1836 | /* If we can't read, wakeup the pending read signals. */ |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1837 | if (channel_input_closed(si_oc(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1838 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1839 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 1840 | /* if the connection is not established, inform the stream that we want |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 1841 | * to be notified whenever the connection completes. |
| 1842 | */ |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 1843 | if (si_opposite(si)->state < SI_ST_EST) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 1844 | si_cant_get(si); |
Willy Tarreau | 12c2423 | 2018-12-06 15:29:50 +0100 | [diff] [blame] | 1845 | si_rx_conn_blk(si); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1846 | si_rx_endp_more(si); |
Willy Tarreau | d4da196 | 2015-04-20 01:31:23 +0200 | [diff] [blame] | 1847 | return; |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 1848 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1849 | |
| 1850 | /* This function is called after the connect. */ |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 1851 | appctx->ctx.hlua_cosocket.connected = 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1852 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1853 | /* 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] | 1854 | if (channel_may_recv(si_ic(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1855 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1856 | |
| 1857 | /* Wake the tasks which wants to read if the buffer contains data. */ |
Thierry FOURNIER | eba6f64 | 2015-09-26 22:01:07 +0200 | [diff] [blame] | 1858 | if (!channel_is_empty(si_oc(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1859 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1860 | |
| 1861 | /* Some data were injected in the buffer, notify the stream |
| 1862 | * interface. |
| 1863 | */ |
| 1864 | if (!channel_is_empty(si_ic(si))) |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 1865 | si_update(si); |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1866 | |
| 1867 | /* If write notifications are registered, we considers we want |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1868 | * to write, so we clear the blocking flag. |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1869 | */ |
| 1870 | if (notification_registered(&appctx->ctx.hlua_cosocket.wake_on_write)) |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1871 | si_rx_endp_more(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1872 | } |
| 1873 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1874 | /* This function is called when the "struct stream" is destroyed. |
| 1875 | * Remove the link from the object to this stream. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1876 | * Wake all the pending signals. |
| 1877 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1878 | static void hlua_socket_release(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1879 | { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1880 | struct xref *peer; |
| 1881 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1882 | /* Remove my link in the original object. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1883 | peer = xref_get_peer_and_lock(&appctx->ctx.hlua_cosocket.xref); |
| 1884 | if (peer) |
| 1885 | xref_disconnect(&appctx->ctx.hlua_cosocket.xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1886 | |
| 1887 | /* Wake all the task waiting for me. */ |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1888 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1889 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1890 | } |
| 1891 | |
| 1892 | /* If the garbage collectio of the object is launch, nobody |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1893 | * uses this object. If the stream does not exists, just quit. |
| 1894 | * Send the shutdown signal to the stream. In some cases, |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1895 | * pending signal can rest in the read and write lists. destroy |
| 1896 | * it. |
| 1897 | */ |
| 1898 | __LJMP static int hlua_socket_gc(lua_State *L) |
| 1899 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1900 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1901 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1902 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1903 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1904 | MAY_LJMP(check_args(L, 1, "__gc")); |
| 1905 | |
| 1906 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1907 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1908 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1909 | return 0; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1910 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1911 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1912 | /* Set the flag which destroy the session. */ |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1913 | appctx->ctx.hlua_cosocket.die = 1; |
| 1914 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1915 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1916 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1917 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1918 | return 0; |
| 1919 | } |
| 1920 | |
| 1921 | /* The close function send shutdown signal and break the |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1922 | * links between the stream and the object. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1923 | */ |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 1924 | __LJMP static int hlua_socket_close_helper(lua_State *L) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1925 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1926 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1927 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1928 | struct xref *peer; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 1929 | struct hlua *hlua; |
| 1930 | |
| 1931 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 1932 | hlua = hlua_gethlua(L); |
| 1933 | if (!hlua) |
| 1934 | return 0; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1935 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1936 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 1937 | |
| 1938 | /* Check if we run on the same thread than the xreator thread. |
| 1939 | * We cannot access to the socket if the thread is different. |
| 1940 | */ |
| 1941 | if (socket->tid != tid) |
| 1942 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 1943 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1944 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1945 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1946 | return 0; |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 1947 | |
| 1948 | hlua->gc_count--; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1949 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1950 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1951 | /* Set the flag which destroy the session. */ |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1952 | appctx->ctx.hlua_cosocket.die = 1; |
| 1953 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1954 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1955 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1956 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1957 | return 0; |
| 1958 | } |
| 1959 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 1960 | /* The close function calls close_helper. |
| 1961 | */ |
| 1962 | __LJMP static int hlua_socket_close(lua_State *L) |
| 1963 | { |
| 1964 | MAY_LJMP(check_args(L, 1, "close")); |
| 1965 | return hlua_socket_close_helper(L); |
| 1966 | } |
| 1967 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1968 | /* This Lua function assumes that the stack contain three parameters. |
| 1969 | * 1 - USERDATA containing a struct socket |
| 1970 | * 2 - INTEGER with values of the macro defined below |
| 1971 | * If the integer is -1, we must read at most one line. |
| 1972 | * If the integer is -2, we ust read all the data until the |
| 1973 | * end of the stream. |
| 1974 | * If the integer is positive value, we must read a number of |
| 1975 | * bytes corresponding to this value. |
| 1976 | */ |
| 1977 | #define HLSR_READ_LINE (-1) |
| 1978 | #define HLSR_READ_ALL (-2) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1979 | __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] | 1980 | { |
| 1981 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 1982 | int wanted = lua_tointeger(L, 2); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 1983 | struct hlua *hlua; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1984 | struct appctx *appctx; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 1985 | size_t len; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1986 | int nblk; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 1987 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 1988 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 1989 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 1990 | size_t len2; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 1991 | int skip_at_end = 0; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 1992 | struct channel *oc; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1993 | struct stream_interface *si; |
| 1994 | struct stream *s; |
| 1995 | struct xref *peer; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1996 | int missing_bytes; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1997 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 1998 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 1999 | hlua = hlua_gethlua(L); |
| 2000 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2001 | /* Check if this lua stack is schedulable. */ |
| 2002 | if (!hlua || !hlua->task) |
| 2003 | WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in " |
| 2004 | "'frontend', 'backend' or 'task'")); |
| 2005 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2006 | /* Check if we run on the same thread than the xreator thread. |
| 2007 | * We cannot access to the socket if the thread is different. |
| 2008 | */ |
| 2009 | if (socket->tid != tid) |
| 2010 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2011 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2012 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2013 | peer = xref_get_peer_and_lock(&socket->xref); |
| 2014 | if (!peer) |
| 2015 | goto no_peer; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2016 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2017 | si = appctx->owner; |
| 2018 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2019 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2020 | oc = &s->res; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2021 | if (wanted == HLSR_READ_LINE) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2022 | /* Read line. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2023 | nblk = co_getline_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2024 | if (nblk < 0) /* Connection close. */ |
| 2025 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2026 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2027 | goto connection_empty; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 2028 | |
| 2029 | /* remove final \r\n. */ |
| 2030 | if (nblk == 1) { |
| 2031 | if (blk1[len1-1] == '\n') { |
| 2032 | len1--; |
| 2033 | skip_at_end++; |
| 2034 | if (blk1[len1-1] == '\r') { |
| 2035 | len1--; |
| 2036 | skip_at_end++; |
| 2037 | } |
| 2038 | } |
| 2039 | } |
| 2040 | else { |
| 2041 | if (blk2[len2-1] == '\n') { |
| 2042 | len2--; |
| 2043 | skip_at_end++; |
| 2044 | if (blk2[len2-1] == '\r') { |
| 2045 | len2--; |
| 2046 | skip_at_end++; |
| 2047 | } |
| 2048 | } |
| 2049 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2050 | } |
| 2051 | |
| 2052 | else if (wanted == HLSR_READ_ALL) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2053 | /* Read all the available data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2054 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2055 | if (nblk < 0) /* Connection close. */ |
| 2056 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2057 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2058 | goto connection_empty; |
| 2059 | } |
| 2060 | |
| 2061 | else { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2062 | /* Read a block of data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2063 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2064 | if (nblk < 0) /* Connection close. */ |
| 2065 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2066 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2067 | goto connection_empty; |
| 2068 | |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 2069 | missing_bytes = wanted - socket->b.n; |
| 2070 | if (len1 > missing_bytes) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2071 | nblk = 1; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 2072 | len1 = missing_bytes; |
| 2073 | } if (nblk == 2 && len1 + len2 > missing_bytes) |
| 2074 | len2 = missing_bytes - len1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2075 | } |
| 2076 | |
| 2077 | len = len1; |
| 2078 | |
| 2079 | luaL_addlstring(&socket->b, blk1, len1); |
| 2080 | if (nblk == 2) { |
| 2081 | len += len2; |
| 2082 | luaL_addlstring(&socket->b, blk2, len2); |
| 2083 | } |
| 2084 | |
| 2085 | /* Consume data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2086 | co_skip(oc, len + skip_at_end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2087 | |
| 2088 | /* Don't wait anything. */ |
Thierry FOURNIER | 7e4ee47 | 2018-05-25 15:03:50 +0200 | [diff] [blame] | 2089 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2090 | |
| 2091 | /* If the pattern reclaim to read all the data |
| 2092 | * in the connection, got out. |
| 2093 | */ |
| 2094 | if (wanted == HLSR_READ_ALL) |
| 2095 | goto connection_empty; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 2096 | else if (wanted >= 0 && socket->b.n < wanted) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2097 | goto connection_empty; |
| 2098 | |
| 2099 | /* Return result. */ |
| 2100 | luaL_pushresult(&socket->b); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2101 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2102 | return 1; |
| 2103 | |
| 2104 | connection_closed: |
| 2105 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2106 | xref_unlock(&socket->xref, peer); |
| 2107 | |
| 2108 | no_peer: |
| 2109 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2110 | /* If the buffer containds data. */ |
| 2111 | if (socket->b.n > 0) { |
| 2112 | luaL_pushresult(&socket->b); |
| 2113 | return 1; |
| 2114 | } |
| 2115 | lua_pushnil(L); |
| 2116 | lua_pushstring(L, "connection closed."); |
| 2117 | return 2; |
| 2118 | |
| 2119 | connection_empty: |
| 2120 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2121 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_read, hlua->task)) { |
| 2122 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2123 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2124 | } |
| 2125 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2126 | 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] | 2127 | return 0; |
| 2128 | } |
| 2129 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2130 | /* This Lua function gets two parameters. The first one can be string |
| 2131 | * or a number. If the string is "*l", the user requires one line. If |
| 2132 | * 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] | 2133 | * If the value is a number, the user require a number of bytes equal |
| 2134 | * to the value. The default value is "*l" (a line). |
| 2135 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2136 | * This parameter with a variable type is converted in integer. This |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2137 | * integer takes this values: |
| 2138 | * -1 : read a line |
| 2139 | * -2 : read all the stream |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2140 | * >0 : amount of bytes. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2141 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2142 | * The second parameter is optional. It contains a string that must be |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2143 | * concatenated with the read data. |
| 2144 | */ |
| 2145 | __LJMP static int hlua_socket_receive(struct lua_State *L) |
| 2146 | { |
| 2147 | int wanted = HLSR_READ_LINE; |
| 2148 | const char *pattern; |
Christopher Faulet | c31b200 | 2021-05-03 10:11:13 +0200 | [diff] [blame] | 2149 | int lastarg, type; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2150 | char *error; |
| 2151 | size_t len; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2152 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2153 | |
| 2154 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 2155 | WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments.")); |
| 2156 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2157 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2158 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2159 | /* Check if we run on the same thread than the xreator thread. |
| 2160 | * We cannot access to the socket if the thread is different. |
| 2161 | */ |
| 2162 | if (socket->tid != tid) |
| 2163 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2164 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2165 | /* check for pattern. */ |
| 2166 | if (lua_gettop(L) >= 2) { |
| 2167 | type = lua_type(L, 2); |
| 2168 | if (type == LUA_TSTRING) { |
| 2169 | pattern = lua_tostring(L, 2); |
| 2170 | if (strcmp(pattern, "*a") == 0) |
| 2171 | wanted = HLSR_READ_ALL; |
| 2172 | else if (strcmp(pattern, "*l") == 0) |
| 2173 | wanted = HLSR_READ_LINE; |
| 2174 | else { |
| 2175 | wanted = strtoll(pattern, &error, 10); |
| 2176 | if (*error != '\0') |
| 2177 | WILL_LJMP(luaL_error(L, "Unsupported pattern.")); |
| 2178 | } |
| 2179 | } |
| 2180 | else if (type == LUA_TNUMBER) { |
| 2181 | wanted = lua_tointeger(L, 2); |
| 2182 | if (wanted < 0) |
| 2183 | WILL_LJMP(luaL_error(L, "Unsupported size.")); |
| 2184 | } |
| 2185 | } |
| 2186 | |
| 2187 | /* Set pattern. */ |
| 2188 | lua_pushinteger(L, wanted); |
Tim Duesterhus | c6e377e | 2018-01-04 19:32:13 +0100 | [diff] [blame] | 2189 | |
| 2190 | /* Check if we would replace the top by itself. */ |
| 2191 | if (lua_gettop(L) != 2) |
| 2192 | lua_replace(L, 2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2193 | |
Christopher Faulet | c31b200 | 2021-05-03 10:11:13 +0200 | [diff] [blame] | 2194 | /* Save index of the top of the stack because since buffers are used, it |
| 2195 | * may change |
| 2196 | */ |
| 2197 | lastarg = lua_gettop(L); |
| 2198 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2199 | /* init buffer, and fill it with prefix. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2200 | luaL_buffinit(L, &socket->b); |
| 2201 | |
| 2202 | /* Check prefix. */ |
Christopher Faulet | c31b200 | 2021-05-03 10:11:13 +0200 | [diff] [blame] | 2203 | if (lastarg >= 3) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2204 | if (lua_type(L, 3) != LUA_TSTRING) |
| 2205 | WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix")); |
| 2206 | pattern = lua_tolstring(L, 3, &len); |
| 2207 | luaL_addlstring(&socket->b, pattern, len); |
| 2208 | } |
| 2209 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2210 | return __LJMP(hlua_socket_receive_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2211 | } |
| 2212 | |
| 2213 | /* Write the Lua input string in the output buffer. |
Mark Lakes | 22154b4 | 2018-01-29 14:38:40 -0800 | [diff] [blame] | 2214 | * This function returns a yield if no space is available. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2215 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2216 | 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] | 2217 | { |
| 2218 | struct hlua_socket *socket; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2219 | struct hlua *hlua; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2220 | struct appctx *appctx; |
| 2221 | size_t buf_len; |
| 2222 | const char *buf; |
| 2223 | int len; |
| 2224 | int send_len; |
| 2225 | int sent; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2226 | struct xref *peer; |
| 2227 | struct stream_interface *si; |
| 2228 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2229 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2230 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 2231 | hlua = hlua_gethlua(L); |
| 2232 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2233 | /* Check if this lua stack is schedulable. */ |
| 2234 | if (!hlua || !hlua->task) |
| 2235 | WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in " |
| 2236 | "'frontend', 'backend' or 'task'")); |
| 2237 | |
| 2238 | /* Get object */ |
| 2239 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2240 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2241 | sent = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2242 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2243 | /* Check if we run on the same thread than the xreator thread. |
| 2244 | * We cannot access to the socket if the thread is different. |
| 2245 | */ |
| 2246 | if (socket->tid != tid) |
| 2247 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2248 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2249 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2250 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2251 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2252 | lua_pushinteger(L, -1); |
| 2253 | return 1; |
| 2254 | } |
| 2255 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2256 | si = appctx->owner; |
| 2257 | s = si_strm(si); |
| 2258 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2259 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2260 | if (channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2261 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2262 | lua_pushinteger(L, -1); |
| 2263 | return 1; |
| 2264 | } |
| 2265 | |
| 2266 | /* Update the input buffer data. */ |
| 2267 | buf += sent; |
| 2268 | send_len = buf_len - sent; |
| 2269 | |
| 2270 | /* All the data are sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2271 | if (sent >= buf_len) { |
| 2272 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2273 | return 1; /* Implicitly return the length sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2274 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2275 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2276 | /* Check if the buffer is available because HAProxy doesn't allocate |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 2277 | * the request buffer if its not required. |
| 2278 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2279 | if (s->req.buf.size == 0) { |
Willy Tarreau | 581abd3 | 2018-10-25 10:21:41 +0200 | [diff] [blame] | 2280 | if (!si_alloc_ibuf(si, &appctx->buffer_wait)) |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2281 | goto hlua_socket_write_yield_return; |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 2282 | } |
| 2283 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2284 | /* Check for available space. */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2285 | len = b_room(&s->req.buf); |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2286 | if (len <= 0) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2287 | goto hlua_socket_write_yield_return; |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2288 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2289 | |
| 2290 | /* send data */ |
| 2291 | if (len < send_len) |
| 2292 | send_len = len; |
Thierry FOURNIER | 66b8919 | 2018-05-27 01:14:47 +0200 | [diff] [blame] | 2293 | len = ci_putblk(&s->req, buf, send_len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2294 | |
| 2295 | /* "Not enough space" (-1), "Buffer too little to contain |
| 2296 | * the data" (-2) are not expected because the available length |
| 2297 | * is tested. |
| 2298 | * Other unknown error are also not expected. |
| 2299 | */ |
| 2300 | if (len <= 0) { |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2301 | if (len == -1) |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2302 | s->req.flags |= CF_WAKE_WRITE; |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2303 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 2304 | MAY_LJMP(hlua_socket_close_helper(L)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2305 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2306 | lua_pushinteger(L, -1); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2307 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2308 | return 1; |
| 2309 | } |
| 2310 | |
| 2311 | /* update buffers. */ |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 2312 | appctx_wakeup(appctx); |
Willy Tarreau | de70fa1 | 2015-09-26 11:25:05 +0200 | [diff] [blame] | 2313 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2314 | s->req.rex = TICK_ETERNITY; |
| 2315 | s->res.wex = TICK_ETERNITY; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2316 | |
| 2317 | /* Update length sent. */ |
| 2318 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2319 | lua_pushinteger(L, sent + len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2320 | |
| 2321 | /* All the data buffer is sent ? */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2322 | if (sent + len >= buf_len) { |
| 2323 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2324 | return 1; |
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 | |
| 2327 | hlua_socket_write_yield_return: |
Thierry FOURNIER | ba42fcd | 2018-05-27 00:59:48 +0200 | [diff] [blame] | 2328 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2329 | xref_unlock(&socket->xref, peer); |
| 2330 | WILL_LJMP(luaL_error(L, "out of memory")); |
| 2331 | } |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2332 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2333 | 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] | 2334 | return 0; |
| 2335 | } |
| 2336 | |
| 2337 | /* This function initiate the send of data. It just check the input |
| 2338 | * parameters and push an integer in the Lua stack that contain the |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2339 | * 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] | 2340 | * "hlua_socket_write_yield" that can yield. |
| 2341 | * |
| 2342 | * The Lua function gets between 3 and 4 parameters. The first one is |
| 2343 | * the associated object. The second is a string buffer. The third is |
| 2344 | * a facultative integer that represents where is the buffer position |
| 2345 | * of the start of the data that can send. The first byte is the |
| 2346 | * position "1". The default value is "1". The fourth argument is a |
| 2347 | * facultative integer that represents where is the buffer position |
| 2348 | * of the end of the data that can send. The default is the last byte. |
| 2349 | */ |
| 2350 | static int hlua_socket_send(struct lua_State *L) |
| 2351 | { |
| 2352 | int i; |
| 2353 | int j; |
| 2354 | const char *buf; |
| 2355 | size_t buf_len; |
| 2356 | |
| 2357 | /* Check number of arguments. */ |
| 2358 | if (lua_gettop(L) < 2 || lua_gettop(L) > 4) |
| 2359 | WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments")); |
| 2360 | |
| 2361 | /* Get the string. */ |
| 2362 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
| 2363 | |
| 2364 | /* Get and check j. */ |
| 2365 | if (lua_gettop(L) == 4) { |
| 2366 | j = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 2367 | if (j < 0) |
| 2368 | j = buf_len + j + 1; |
| 2369 | if (j > buf_len) |
| 2370 | j = buf_len + 1; |
| 2371 | lua_pop(L, 1); |
| 2372 | } |
| 2373 | else |
| 2374 | j = buf_len; |
| 2375 | |
| 2376 | /* Get and check i. */ |
| 2377 | if (lua_gettop(L) == 3) { |
| 2378 | i = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2379 | if (i < 0) |
| 2380 | i = buf_len + i + 1; |
| 2381 | if (i > buf_len) |
| 2382 | i = buf_len + 1; |
| 2383 | lua_pop(L, 1); |
| 2384 | } else |
| 2385 | i = 1; |
| 2386 | |
| 2387 | /* Check bth i and j. */ |
| 2388 | if (i > j) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2389 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2390 | return 1; |
| 2391 | } |
| 2392 | if (i == 0 && j == 0) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2393 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2394 | return 1; |
| 2395 | } |
| 2396 | if (i == 0) |
| 2397 | i = 1; |
| 2398 | if (j == 0) |
| 2399 | j = 1; |
| 2400 | |
| 2401 | /* Pop the string. */ |
| 2402 | lua_pop(L, 1); |
| 2403 | |
| 2404 | /* Update the buffer length. */ |
| 2405 | buf += i - 1; |
| 2406 | buf_len = j - i + 1; |
| 2407 | lua_pushlstring(L, buf, buf_len); |
| 2408 | |
| 2409 | /* This unsigned is used to remember the amount of sent data. */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2410 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2411 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2412 | return MAY_LJMP(hlua_socket_write_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2413 | } |
| 2414 | |
Willy Tarreau | 22b0a68 | 2015-06-17 19:43:49 +0200 | [diff] [blame] | 2415 | #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] | 2416 | __LJMP static inline int hlua_socket_info(struct lua_State *L, struct sockaddr_storage *addr) |
| 2417 | { |
| 2418 | static char buffer[SOCKET_INFO_MAX_LEN]; |
| 2419 | int ret; |
| 2420 | int len; |
| 2421 | char *p; |
| 2422 | |
| 2423 | ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1); |
| 2424 | if (ret <= 0) { |
| 2425 | lua_pushnil(L); |
| 2426 | return 1; |
| 2427 | } |
| 2428 | |
| 2429 | if (ret == AF_UNIX) { |
| 2430 | lua_pushstring(L, buffer+1); |
| 2431 | return 1; |
| 2432 | } |
| 2433 | else if (ret == AF_INET6) { |
| 2434 | buffer[0] = '['; |
| 2435 | len = strlen(buffer); |
| 2436 | buffer[len] = ']'; |
| 2437 | len++; |
| 2438 | buffer[len] = ':'; |
| 2439 | len++; |
| 2440 | p = buffer; |
| 2441 | } |
| 2442 | else if (ret == AF_INET) { |
| 2443 | p = buffer + 1; |
| 2444 | len = strlen(p); |
| 2445 | p[len] = ':'; |
| 2446 | len++; |
| 2447 | } |
| 2448 | else { |
| 2449 | lua_pushnil(L); |
| 2450 | return 1; |
| 2451 | } |
| 2452 | |
| 2453 | if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) { |
| 2454 | lua_pushnil(L); |
| 2455 | return 1; |
| 2456 | } |
| 2457 | |
| 2458 | lua_pushstring(L, p); |
| 2459 | return 1; |
| 2460 | } |
| 2461 | |
| 2462 | /* Returns information about the peer of the connection. */ |
| 2463 | __LJMP static int hlua_socket_getpeername(struct lua_State *L) |
| 2464 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2465 | struct hlua_socket *socket; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2466 | struct xref *peer; |
| 2467 | struct appctx *appctx; |
| 2468 | struct stream_interface *si; |
| 2469 | struct stream *s; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2470 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2471 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2472 | MAY_LJMP(check_args(L, 1, "getpeername")); |
| 2473 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2474 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2475 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2476 | /* Check if we run on the same thread than the xreator thread. |
| 2477 | * We cannot access to the socket if the thread is different. |
| 2478 | */ |
| 2479 | if (socket->tid != tid) |
| 2480 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2481 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2482 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2483 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2484 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2485 | lua_pushnil(L); |
| 2486 | return 1; |
| 2487 | } |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2488 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2489 | si = appctx->owner; |
| 2490 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2491 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2492 | if (!s->target_addr) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2493 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2494 | lua_pushnil(L); |
| 2495 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2496 | } |
| 2497 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2498 | ret = MAY_LJMP(hlua_socket_info(L, s->target_addr)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2499 | xref_unlock(&socket->xref, peer); |
| 2500 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2501 | } |
| 2502 | |
| 2503 | /* Returns information about my connection side. */ |
| 2504 | static int hlua_socket_getsockname(struct lua_State *L) |
| 2505 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2506 | struct hlua_socket *socket; |
| 2507 | struct connection *conn; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2508 | struct appctx *appctx; |
| 2509 | struct xref *peer; |
| 2510 | struct stream_interface *si; |
| 2511 | struct stream *s; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2512 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2513 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2514 | MAY_LJMP(check_args(L, 1, "getsockname")); |
| 2515 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2516 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2517 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2518 | /* Check if we run on the same thread than the xreator thread. |
| 2519 | * We cannot access to the socket if the thread is different. |
| 2520 | */ |
| 2521 | if (socket->tid != tid) |
| 2522 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2523 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2524 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2525 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2526 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2527 | lua_pushnil(L); |
| 2528 | return 1; |
| 2529 | } |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2530 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2531 | si = appctx->owner; |
| 2532 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2533 | |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 2534 | conn = cs_conn(objt_cs(s->si[1].end)); |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2535 | if (!conn || !conn_get_src(conn)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2536 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2537 | lua_pushnil(L); |
| 2538 | return 1; |
| 2539 | } |
| 2540 | |
Willy Tarreau | 9da9a6f | 2019-07-17 14:49:44 +0200 | [diff] [blame] | 2541 | ret = hlua_socket_info(L, conn->src); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2542 | xref_unlock(&socket->xref, peer); |
| 2543 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2544 | } |
| 2545 | |
| 2546 | /* This struct define the applet. */ |
Willy Tarreau | 3057645 | 2015-04-13 13:50:30 +0200 | [diff] [blame] | 2547 | static struct applet update_applet = { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2548 | .obj_type = OBJ_TYPE_APPLET, |
| 2549 | .name = "<LUA_TCP>", |
| 2550 | .fct = hlua_socket_handler, |
| 2551 | .release = hlua_socket_release, |
| 2552 | }; |
| 2553 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2554 | __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] | 2555 | { |
| 2556 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2557 | struct hlua *hlua; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2558 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2559 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2560 | struct stream_interface *si; |
| 2561 | struct stream *s; |
| 2562 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2563 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 2564 | hlua = hlua_gethlua(L); |
| 2565 | if (!hlua) |
| 2566 | return 0; |
| 2567 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2568 | /* Check if we run on the same thread than the xreator thread. |
| 2569 | * We cannot access to the socket if the thread is different. |
| 2570 | */ |
| 2571 | if (socket->tid != tid) |
| 2572 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2573 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2574 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2575 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2576 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2577 | lua_pushnil(L); |
| 2578 | lua_pushstring(L, "Can't connect"); |
| 2579 | return 2; |
| 2580 | } |
| 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 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2585 | /* Check if we run on the same thread than the xreator thread. |
| 2586 | * We cannot access to the socket if the thread is different. |
| 2587 | */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2588 | if (socket->tid != tid) { |
| 2589 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2590 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2591 | } |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2592 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2593 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2594 | if (!hlua || channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2595 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2596 | lua_pushnil(L); |
| 2597 | lua_pushstring(L, "Can't connect"); |
| 2598 | return 2; |
| 2599 | } |
| 2600 | |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 2601 | appctx = __objt_appctx(s->si[0].end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2602 | |
| 2603 | /* Check for connection established. */ |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2604 | if (appctx->ctx.hlua_cosocket.connected) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2605 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2606 | lua_pushinteger(L, 1); |
| 2607 | return 1; |
| 2608 | } |
| 2609 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2610 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2611 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2612 | WILL_LJMP(luaL_error(L, "out of memory error")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2613 | } |
| 2614 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2615 | 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] | 2616 | return 0; |
| 2617 | } |
| 2618 | |
| 2619 | /* This function fail or initite the connection. */ |
| 2620 | __LJMP static int hlua_socket_connect(struct lua_State *L) |
| 2621 | { |
| 2622 | struct hlua_socket *socket; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2623 | int port = -1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2624 | const char *ip; |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2625 | struct hlua *hlua; |
| 2626 | struct appctx *appctx; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2627 | int low, high; |
| 2628 | struct sockaddr_storage *addr; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2629 | struct xref *peer; |
| 2630 | struct stream_interface *si; |
| 2631 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2632 | |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2633 | if (lua_gettop(L) < 2) |
| 2634 | WILL_LJMP(luaL_error(L, "connect: need at least 2 arguments")); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2635 | |
| 2636 | /* Get args. */ |
| 2637 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2638 | |
| 2639 | /* Check if we run on the same thread than the xreator thread. |
| 2640 | * We cannot access to the socket if the thread is different. |
| 2641 | */ |
| 2642 | if (socket->tid != tid) |
| 2643 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2644 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2645 | ip = MAY_LJMP(luaL_checkstring(L, 2)); |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 2646 | if (lua_gettop(L) >= 3) { |
| 2647 | luaL_Buffer b; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2648 | port = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2649 | |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 2650 | /* Force the ip to end with a colon, to support IPv6 addresses |
| 2651 | * that are not enclosed within square brackets. |
| 2652 | */ |
| 2653 | if (port > 0) { |
| 2654 | luaL_buffinit(L, &b); |
| 2655 | luaL_addstring(&b, ip); |
| 2656 | luaL_addchar(&b, ':'); |
| 2657 | luaL_pushresult(&b); |
| 2658 | ip = lua_tolstring(L, lua_gettop(L), NULL); |
| 2659 | } |
| 2660 | } |
| 2661 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2662 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2663 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2664 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2665 | lua_pushnil(L); |
| 2666 | return 1; |
| 2667 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2668 | |
| 2669 | /* Parse ip address. */ |
Willy Tarreau | 5fc9328 | 2020-09-16 18:25:03 +0200 | [diff] [blame] | 2670 | 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] | 2671 | if (!addr) { |
| 2672 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2673 | WILL_LJMP(luaL_error(L, "connect: cannot parse destination address '%s'", ip)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2674 | } |
Willy Tarreau | 9da9a6f | 2019-07-17 14:49:44 +0200 | [diff] [blame] | 2675 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2676 | /* Set port. */ |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2677 | if (low == 0) { |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2678 | if (addr->ss_family == AF_INET) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2679 | if (port == -1) { |
| 2680 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2681 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2682 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2683 | ((struct sockaddr_in *)addr)->sin_port = htons(port); |
| 2684 | } else if (addr->ss_family == AF_INET6) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2685 | if (port == -1) { |
| 2686 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2687 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2688 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2689 | ((struct sockaddr_in6 *)addr)->sin6_port = htons(port); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2690 | } |
| 2691 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2692 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2693 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2694 | si = appctx->owner; |
| 2695 | s = si_strm(si); |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2696 | |
Willy Tarreau | 9b7587a | 2020-10-15 07:32:10 +0200 | [diff] [blame] | 2697 | if (!sockaddr_alloc(&s->target_addr, addr, sizeof(*addr))) { |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2698 | xref_unlock(&socket->xref, peer); |
| 2699 | WILL_LJMP(luaL_error(L, "connect: internal error")); |
| 2700 | } |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2701 | s->flags |= SF_ADDR_SET; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2702 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2703 | /* Get hlua struct, or NULL if we execute from main lua state */ |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2704 | hlua = hlua_gethlua(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2705 | if (!hlua) |
| 2706 | return 0; |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2707 | |
| 2708 | /* inform the stream that we want to be notified whenever the |
| 2709 | * connection completes. |
| 2710 | */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 2711 | si_cant_get(&s->si[0]); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 2712 | si_rx_endp_more(&s->si[0]); |
Thierry FOURNIER | 8c8fbbe | 2015-09-26 17:02:35 +0200 | [diff] [blame] | 2713 | appctx_wakeup(appctx); |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2714 | |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 2715 | hlua->gc_count++; |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 2716 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2717 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2718 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2719 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2720 | } |
| 2721 | xref_unlock(&socket->xref, peer); |
PiBa-NL | 706d5ee | 2018-05-05 23:51:42 +0200 | [diff] [blame] | 2722 | |
| 2723 | task_wakeup(s->task, TASK_WOKEN_INIT); |
| 2724 | /* Return yield waiting for connection. */ |
| 2725 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2726 | 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] | 2727 | |
| 2728 | return 0; |
| 2729 | } |
| 2730 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2731 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2732 | __LJMP static int hlua_socket_connect_ssl(struct lua_State *L) |
| 2733 | { |
| 2734 | struct hlua_socket *socket; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2735 | struct xref *peer; |
| 2736 | struct appctx *appctx; |
| 2737 | struct stream_interface *si; |
| 2738 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2739 | |
| 2740 | MAY_LJMP(check_args(L, 3, "connect_ssl")); |
| 2741 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2742 | |
| 2743 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2744 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2745 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2746 | lua_pushnil(L); |
| 2747 | return 1; |
| 2748 | } |
| 2749 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2750 | si = appctx->owner; |
| 2751 | s = si_strm(si); |
| 2752 | |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 2753 | s->target = &socket_ssl->obj_type; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2754 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2755 | return MAY_LJMP(hlua_socket_connect(L)); |
| 2756 | } |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2757 | #endif |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2758 | |
| 2759 | __LJMP static int hlua_socket_setoption(struct lua_State *L) |
| 2760 | { |
| 2761 | return 0; |
| 2762 | } |
| 2763 | |
| 2764 | __LJMP static int hlua_socket_settimeout(struct lua_State *L) |
| 2765 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2766 | struct hlua_socket *socket; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2767 | int tmout; |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2768 | double dtmout; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2769 | struct xref *peer; |
| 2770 | struct appctx *appctx; |
| 2771 | struct stream_interface *si; |
| 2772 | struct stream *s; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2773 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2774 | MAY_LJMP(check_args(L, 2, "settimeout")); |
| 2775 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2776 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2777 | |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2778 | /* convert the timeout to millis */ |
| 2779 | dtmout = MAY_LJMP(luaL_checknumber(L, 2)) * 1000; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2780 | |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 2781 | /* Check for negative values */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2782 | if (dtmout < 0) |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 2783 | WILL_LJMP(luaL_error(L, "settimeout: cannot set negatives values")); |
| 2784 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2785 | if (dtmout > INT_MAX) /* overflow check */ |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2786 | 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] | 2787 | |
| 2788 | tmout = MS_TO_TICKS((int)dtmout); |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2789 | if (tmout == 0) |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 2790 | tmout++; /* very small timeouts are adjusted to a minimum of 1ms */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2791 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2792 | /* Check if we run on the same thread than the xreator thread. |
| 2793 | * We cannot access to the socket if the thread is different. |
| 2794 | */ |
| 2795 | if (socket->tid != tid) |
| 2796 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2797 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2798 | /* check for connection break. If some data were read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2799 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2800 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2801 | hlua_pusherror(L, "socket: not yet initialised, you can't set timeouts."); |
| 2802 | WILL_LJMP(lua_error(L)); |
| 2803 | return 0; |
| 2804 | } |
| 2805 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2806 | si = appctx->owner; |
| 2807 | s = si_strm(si); |
| 2808 | |
Cyril Bonté | 7bb6345 | 2018-08-17 23:51:02 +0200 | [diff] [blame] | 2809 | s->sess->fe->timeout.connect = tmout; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2810 | s->req.rto = tmout; |
| 2811 | s->req.wto = tmout; |
| 2812 | s->res.rto = tmout; |
| 2813 | s->res.wto = tmout; |
Cyril Bonté | 7bb6345 | 2018-08-17 23:51:02 +0200 | [diff] [blame] | 2814 | s->req.rex = tick_add_ifset(now_ms, tmout); |
| 2815 | s->req.wex = tick_add_ifset(now_ms, tmout); |
| 2816 | s->res.rex = tick_add_ifset(now_ms, tmout); |
| 2817 | s->res.wex = tick_add_ifset(now_ms, tmout); |
| 2818 | |
| 2819 | s->task->expire = tick_add_ifset(now_ms, tmout); |
| 2820 | task_queue(s->task); |
| 2821 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2822 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2823 | |
Thierry Fournier | e9636f1 | 2018-03-08 09:54:32 +0100 | [diff] [blame] | 2824 | lua_pushinteger(L, 1); |
Tim Duesterhus | 119a5f1 | 2018-01-06 19:16:25 +0100 | [diff] [blame] | 2825 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2826 | } |
| 2827 | |
| 2828 | __LJMP static int hlua_socket_new(lua_State *L) |
| 2829 | { |
| 2830 | struct hlua_socket *socket; |
| 2831 | struct appctx *appctx; |
Willy Tarreau | 15b5e14 | 2015-04-04 14:38:25 +0200 | [diff] [blame] | 2832 | struct session *sess; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2833 | struct stream *strm; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2834 | |
| 2835 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2836 | if (!lua_checkstack(L, 3)) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2837 | hlua_pusherror(L, "socket: full stack"); |
| 2838 | goto out_fail_conf; |
| 2839 | } |
| 2840 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2841 | /* Create the object: obj[0] = userdata. */ |
| 2842 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2843 | socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket))); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2844 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2845 | memset(socket, 0, sizeof(*socket)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2846 | socket->tid = tid; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2847 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 2848 | /* Check if the various memory pools are initialized. */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 2849 | if (!pool_head_stream || !pool_head_buffer) { |
Thierry FOURNIER | 4a6170c | 2015-03-09 17:07:10 +0100 | [diff] [blame] | 2850 | hlua_pusherror(L, "socket: uninitialized pools."); |
| 2851 | goto out_fail_conf; |
| 2852 | } |
| 2853 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2854 | /* Pop a class stream metatable and affect it to the userdata. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2855 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref); |
| 2856 | lua_setmetatable(L, -2); |
| 2857 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2858 | /* Create the applet context */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 2859 | appctx = appctx_new(&update_applet, tid_bit); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2860 | if (!appctx) { |
| 2861 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | feb7640 | 2015-04-03 14:10:06 +0200 | [diff] [blame] | 2862 | goto out_fail_conf; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2863 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2864 | |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2865 | appctx->ctx.hlua_cosocket.connected = 0; |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 2866 | appctx->ctx.hlua_cosocket.die = 0; |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2867 | LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_write); |
| 2868 | LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_read); |
Willy Tarreau | b2bf833 | 2015-04-04 15:58:58 +0200 | [diff] [blame] | 2869 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2870 | /* Now create a session, task and stream for this applet */ |
Amaury Denoyelle | 239fdbf | 2021-03-24 10:22:03 +0100 | [diff] [blame] | 2871 | sess = session_new(socket_proxy, NULL, &appctx->obj_type); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2872 | if (!sess) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2873 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2874 | goto out_fail_sess; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2875 | } |
| 2876 | |
Christopher Faulet | 26256f8 | 2020-09-14 11:40:13 +0200 | [diff] [blame] | 2877 | strm = stream_new(sess, &appctx->obj_type, &BUF_NULL); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2878 | if (!strm) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2879 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2880 | goto out_fail_stream; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2881 | } |
| 2882 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2883 | /* Initialise cross reference between stream and Lua socket object. */ |
| 2884 | xref_create(&socket->xref, &appctx->ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2885 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2886 | /* Configure "right" stream interface. this "si" is used to connect |
| 2887 | * and retrieve data from the server. The connection is initialized |
| 2888 | * with the "struct server". |
| 2889 | */ |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2890 | si_set_state(&strm->si[1], SI_ST_ASS); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2891 | |
| 2892 | /* Force destination server. */ |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2893 | strm->flags |= SF_DIRECT | SF_ASSIGNED | SF_BE_ASSIGNED; |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 2894 | strm->target = &socket_tcp->obj_type; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2895 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2896 | return 1; |
| 2897 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2898 | out_fail_stream: |
Willy Tarreau | 11c3624 | 2015-04-04 15:54:03 +0200 | [diff] [blame] | 2899 | session_free(sess); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2900 | out_fail_sess: |
| 2901 | appctx_free(appctx); |
| 2902 | out_fail_conf: |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2903 | WILL_LJMP(lua_error(L)); |
| 2904 | return 0; |
| 2905 | } |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 2906 | |
| 2907 | /* |
| 2908 | * |
| 2909 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2910 | * Class Channel |
| 2911 | * |
| 2912 | * |
| 2913 | */ |
| 2914 | |
| 2915 | /* Returns the struct hlua_channel join to the class channel in the |
| 2916 | * stack entry "ud" or throws an argument error. |
| 2917 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2918 | __LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2919 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 2920 | return MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2921 | } |
| 2922 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2923 | /* Pushes the channel onto the top of the stack. If the stask does not have a |
| 2924 | * free slots, the function fails and returns 0; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2925 | */ |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 2926 | static int hlua_channel_new(lua_State *L, struct channel *channel) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2927 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2928 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2929 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2930 | return 0; |
| 2931 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2932 | lua_newtable(L); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2933 | lua_pushlightuserdata(L, channel); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2934 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2935 | |
| 2936 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 2937 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref); |
| 2938 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2939 | return 1; |
| 2940 | } |
| 2941 | |
| 2942 | /* Duplicate all the data present in the input channel and put it |
| 2943 | * in a string LUA variables. Returns -1 and push a nil value in |
| 2944 | * the stack if the channel is closed and all the data are consumed, |
| 2945 | * returns 0 if no data are available, otherwise it returns the length |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 2946 | * of the built string. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2947 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2948 | static inline int _hlua_channel_dup(struct channel *chn, lua_State *L) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2949 | { |
| 2950 | char *blk1; |
| 2951 | char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2952 | size_t len1; |
| 2953 | size_t len2; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2954 | int ret; |
| 2955 | luaL_Buffer b; |
| 2956 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2957 | ret = ci_getblk_nc(chn, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2958 | if (unlikely(ret == 0)) |
| 2959 | return 0; |
| 2960 | |
| 2961 | if (unlikely(ret < 0)) { |
| 2962 | lua_pushnil(L); |
| 2963 | return -1; |
| 2964 | } |
| 2965 | |
| 2966 | luaL_buffinit(L, &b); |
| 2967 | luaL_addlstring(&b, blk1, len1); |
| 2968 | if (unlikely(ret == 2)) |
| 2969 | luaL_addlstring(&b, blk2, len2); |
| 2970 | luaL_pushresult(&b); |
| 2971 | |
| 2972 | if (unlikely(ret == 2)) |
| 2973 | return len1 + len2; |
| 2974 | return len1; |
| 2975 | } |
| 2976 | |
| 2977 | /* "_hlua_channel_dup" wrapper. If no data are available, it returns |
| 2978 | * a yield. This function keep the data in the buffer. |
| 2979 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2980 | __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] | 2981 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2982 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2983 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2984 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2985 | |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 2986 | if (IS_HTX_STRM(chn_strm(chn))) { |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 2987 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2988 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 2989 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2990 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2991 | if (_hlua_channel_dup(chn, L) == 0) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2992 | 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] | 2993 | return 1; |
| 2994 | } |
| 2995 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2996 | /* Check arguments for the function "hlua_channel_dup_yield". */ |
| 2997 | __LJMP static int hlua_channel_dup(lua_State *L) |
| 2998 | { |
| 2999 | MAY_LJMP(check_args(L, 1, "dup")); |
| 3000 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3001 | return MAY_LJMP(hlua_channel_dup_yield(L, 0, 0)); |
| 3002 | } |
| 3003 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3004 | /* "_hlua_channel_dup" wrapper. If no data are available, it returns |
| 3005 | * a yield. This function consumes the data in the buffer. It returns |
| 3006 | * a string containing the data or a nil pointer if no data are available |
| 3007 | * and the channel is closed. |
| 3008 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3009 | __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] | 3010 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3011 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3012 | int ret; |
| 3013 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3014 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3015 | |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 3016 | if (IS_HTX_STRM(chn_strm(chn))) { |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3017 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3018 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3019 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3020 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3021 | ret = _hlua_channel_dup(chn, L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3022 | if (unlikely(ret == 0)) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3023 | 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] | 3024 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3025 | if (unlikely(ret == -1)) |
| 3026 | return 1; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3027 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3028 | b_sub(&chn->buf, ret); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3029 | return 1; |
| 3030 | } |
| 3031 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3032 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3033 | __LJMP static int hlua_channel_get(lua_State *L) |
| 3034 | { |
| 3035 | MAY_LJMP(check_args(L, 1, "get")); |
| 3036 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3037 | return MAY_LJMP(hlua_channel_get_yield(L, 0, 0)); |
| 3038 | } |
| 3039 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3040 | /* This functions consumes and returns one line. If the channel is closed, |
| 3041 | * 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] | 3042 | * 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] | 3043 | * value. |
| 3044 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3045 | __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] | 3046 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3047 | char *blk1; |
| 3048 | char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3049 | size_t len1; |
| 3050 | size_t len2; |
| 3051 | size_t len; |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3052 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3053 | int ret; |
| 3054 | luaL_Buffer b; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3055 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3056 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3057 | |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 3058 | if (IS_HTX_STRM(chn_strm(chn))) { |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3059 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3060 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3061 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3062 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3063 | ret = ci_getline_nc(chn, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3064 | if (ret == 0) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3065 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_getline_yield, TICK_ETERNITY, 0)); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3066 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3067 | if (ret == -1) { |
| 3068 | lua_pushnil(L); |
| 3069 | return 1; |
| 3070 | } |
| 3071 | |
| 3072 | luaL_buffinit(L, &b); |
| 3073 | luaL_addlstring(&b, blk1, len1); |
| 3074 | len = len1; |
| 3075 | if (unlikely(ret == 2)) { |
| 3076 | luaL_addlstring(&b, blk2, len2); |
| 3077 | len += len2; |
| 3078 | } |
| 3079 | luaL_pushresult(&b); |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3080 | 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] | 3081 | return 1; |
| 3082 | } |
| 3083 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3084 | /* Check arguments for the function "hlua_channel_getline_yield". */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3085 | __LJMP static int hlua_channel_getline(lua_State *L) |
| 3086 | { |
| 3087 | MAY_LJMP(check_args(L, 1, "getline")); |
| 3088 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3089 | return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0)); |
| 3090 | } |
| 3091 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3092 | /* This function takes a string as input, and append it at the |
| 3093 | * input side of channel. If the data is too big, but a space |
| 3094 | * is probably available after sending some data, the function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3095 | * yields. If the data is bigger than the buffer, or if the |
| 3096 | * channel is closed, it returns -1. Otherwise, it returns the |
| 3097 | * amount of data written. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3098 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3099 | __LJMP static int hlua_channel_append_yield(lua_State *L, int status, lua_KContext ctx) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3100 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3101 | struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3102 | size_t len; |
| 3103 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3104 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3105 | int ret; |
| 3106 | int max; |
| 3107 | |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 3108 | if (IS_HTX_STRM(chn_strm(chn))) { |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3109 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3110 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3111 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3112 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3113 | /* Check if the buffer is available because HAProxy doesn't allocate |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 3114 | * the request buffer if its not required. |
| 3115 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3116 | if (chn->buf.size == 0) { |
Willy Tarreau | 4b962a4 | 2018-11-15 11:03:21 +0100 | [diff] [blame] | 3117 | si_rx_buff_blk(chn_prod(chn)); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3118 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0)); |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 3119 | } |
| 3120 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3121 | max = channel_recv_limit(chn) - b_data(&chn->buf); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3122 | if (max > len - l) |
| 3123 | max = len - l; |
| 3124 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3125 | ret = ci_putblk(chn, str + l, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3126 | if (ret == -2 || ret == -3) { |
| 3127 | lua_pushinteger(L, -1); |
| 3128 | return 1; |
| 3129 | } |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 3130 | if (ret == -1) { |
| 3131 | chn->flags |= CF_WAKE_WRITE; |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3132 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0)); |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 3133 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3134 | l += ret; |
| 3135 | lua_pop(L, 1); |
| 3136 | lua_pushinteger(L, l); |
| 3137 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3138 | max = channel_recv_limit(chn) - b_data(&chn->buf); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3139 | if (max == 0 && co_data(chn) == 0) { |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3140 | /* There are no space available, and the output buffer is empty. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3141 | * in this case, we cannot add more data, so we cannot yield, |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3142 | * we return the amount of copied data. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3143 | */ |
| 3144 | return 1; |
| 3145 | } |
| 3146 | if (l < len) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3147 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3148 | return 1; |
| 3149 | } |
| 3150 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3151 | /* Just a wrapper of "hlua_channel_append_yield". It returns the length |
| 3152 | * of the written string, or -1 if the channel is closed or if the |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3153 | * buffer size is too little for the data. |
| 3154 | */ |
| 3155 | __LJMP static int hlua_channel_append(lua_State *L) |
| 3156 | { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3157 | size_t len; |
| 3158 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3159 | MAY_LJMP(check_args(L, 2, "append")); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3160 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3161 | MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3162 | MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3163 | lua_pushinteger(L, 0); |
| 3164 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3165 | return MAY_LJMP(hlua_channel_append_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3166 | } |
| 3167 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3168 | /* Just a wrapper of "hlua_channel_append_yield". This wrapper starts |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3169 | * his process by cleaning the buffer. The result is a replacement |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3170 | * of the current data. It returns the length of the written string, |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3171 | * or -1 if the channel is closed or if the buffer size is too |
| 3172 | * little for the data. |
| 3173 | */ |
| 3174 | __LJMP static int hlua_channel_set(lua_State *L) |
| 3175 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3176 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3177 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3178 | MAY_LJMP(check_args(L, 2, "set")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3179 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3180 | lua_pushinteger(L, 0); |
| 3181 | |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 3182 | if (IS_HTX_STRM(chn_strm(chn))) { |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3183 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3184 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3185 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3186 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3187 | b_set_data(&chn->buf, co_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3188 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3189 | return MAY_LJMP(hlua_channel_append_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3190 | } |
| 3191 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3192 | /* Append data in the output side of the buffer. This data is immediately |
| 3193 | * sent. The function returns the amount of data written. If the buffer |
| 3194 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3195 | * if the channel is closed. |
| 3196 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3197 | __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] | 3198 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3199 | struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3200 | size_t len; |
| 3201 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3202 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3203 | int max; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 3204 | struct hlua *hlua; |
| 3205 | |
| 3206 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 3207 | hlua = hlua_gethlua(L); |
| 3208 | if (!hlua) { |
| 3209 | lua_pushnil(L); |
| 3210 | return 1; |
| 3211 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3212 | |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 3213 | if (IS_HTX_STRM(chn_strm(chn))) { |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3214 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3215 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3216 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3217 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3218 | if (unlikely(channel_output_closed(chn))) { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3219 | lua_pushinteger(L, -1); |
| 3220 | return 1; |
| 3221 | } |
| 3222 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3223 | /* Check if the buffer is available because HAProxy doesn't allocate |
Thierry FOURNIER | 3e3a608 | 2015-03-05 17:06:12 +0100 | [diff] [blame] | 3224 | * the request buffer if its not required. |
| 3225 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3226 | if (chn->buf.size == 0) { |
Willy Tarreau | 4b962a4 | 2018-11-15 11:03:21 +0100 | [diff] [blame] | 3227 | si_rx_buff_blk(chn_prod(chn)); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3228 | 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] | 3229 | } |
| 3230 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3231 | /* The written data will be immediately sent, so we can check |
| 3232 | * the available space without taking in account the reserve. |
| 3233 | * The reserve is guaranteed for the processing of incoming |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3234 | * data, because the buffer will be flushed. |
| 3235 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3236 | max = b_room(&chn->buf); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3237 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3238 | /* If there is no space available, and the output buffer is empty. |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3239 | * in this case, we cannot add more data, so we cannot yield, |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3240 | * we return the amount of copied data. |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3241 | */ |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3242 | if (max == 0 && co_data(chn) == 0) |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3243 | return 1; |
| 3244 | |
| 3245 | /* Adjust the real required length. */ |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3246 | if (max > len - l) |
| 3247 | max = len - l; |
| 3248 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3249 | /* The buffer available size may be not contiguous. This test |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3250 | * detects a non contiguous buffer and realign it. |
| 3251 | */ |
Willy Tarreau | 3f67999 | 2018-06-15 15:06:42 +0200 | [diff] [blame] | 3252 | if (ci_space_for_replace(chn) < max) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3253 | channel_slow_realign(chn, trash.area); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3254 | |
| 3255 | /* Copy input data in the buffer. */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3256 | 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] | 3257 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3258 | /* buffer replace considers that the input part is filled. |
| 3259 | * so, I must forward these new data in the output part. |
| 3260 | */ |
Willy Tarreau | bcbd393 | 2018-06-06 07:13:22 +0200 | [diff] [blame] | 3261 | c_adv(chn, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3262 | |
| 3263 | l += max; |
| 3264 | lua_pop(L, 1); |
| 3265 | lua_pushinteger(L, l); |
| 3266 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3267 | /* If there is no space available, and the output buffer is empty. |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3268 | * in this case, we cannot add more data, so we cannot yield, |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3269 | * we return the amount of copied data. |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3270 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3271 | max = b_room(&chn->buf); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3272 | if (max == 0 && co_data(chn) == 0) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3273 | return 1; |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3274 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3275 | if (l < len) { |
| 3276 | /* If we are waiting for space in the response buffer, we |
| 3277 | * must set the flag WAKERESWR. This flag required the task |
| 3278 | * wake up if any activity is detected on the response buffer. |
| 3279 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3280 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3281 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3282 | else |
| 3283 | HLUA_SET_WAKEREQWR(hlua); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3284 | 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] | 3285 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3286 | |
| 3287 | return 1; |
| 3288 | } |
| 3289 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3290 | /* Just a wrapper of "_hlua_channel_send". This wrapper permits |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3291 | * yield the LUA process, and resume it without checking the |
| 3292 | * input arguments. |
| 3293 | */ |
| 3294 | __LJMP static int hlua_channel_send(lua_State *L) |
| 3295 | { |
| 3296 | MAY_LJMP(check_args(L, 2, "send")); |
| 3297 | lua_pushinteger(L, 0); |
| 3298 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3299 | return MAY_LJMP(hlua_channel_send_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3300 | } |
| 3301 | |
| 3302 | /* This function forward and amount of butes. The data pass from |
| 3303 | * the input side of the buffer to the output side, and can be |
| 3304 | * forwarded. This function never fails. |
| 3305 | * |
| 3306 | * The Lua function takes an amount of bytes to be forwarded in |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3307 | * input. It returns the number of bytes forwarded. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3308 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3309 | __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] | 3310 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3311 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3312 | int len; |
| 3313 | int l; |
| 3314 | int max; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 3315 | struct hlua *hlua; |
| 3316 | |
| 3317 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 3318 | hlua = hlua_gethlua(L); |
| 3319 | if (!hlua) |
| 3320 | return 1; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3321 | |
| 3322 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3323 | |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 3324 | if (IS_HTX_STRM(chn_strm(chn))) { |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3325 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3326 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3327 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3328 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3329 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3330 | l = MAY_LJMP(luaL_checkinteger(L, -1)); |
| 3331 | |
| 3332 | max = len - l; |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3333 | if (max > ci_data(chn)) |
| 3334 | max = ci_data(chn); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3335 | channel_forward(chn, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3336 | l += max; |
| 3337 | |
| 3338 | lua_pop(L, 1); |
| 3339 | lua_pushinteger(L, l); |
| 3340 | |
| 3341 | /* Check if it miss bytes to forward. */ |
| 3342 | if (l < len) { |
| 3343 | /* The the input channel or the output channel are closed, we |
| 3344 | * must return the amount of data forwarded. |
| 3345 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3346 | if (channel_input_closed(chn) || channel_output_closed(chn)) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3347 | return 1; |
| 3348 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3349 | /* If we are waiting for space data in the response buffer, we |
| 3350 | * must set the flag WAKERESWR. This flag required the task |
| 3351 | * wake up if any activity is detected on the response buffer. |
| 3352 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3353 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3354 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3355 | else |
| 3356 | HLUA_SET_WAKEREQWR(hlua); |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3357 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3358 | /* Otherwise, we can yield waiting for new data in the inpout side. */ |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3359 | 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] | 3360 | } |
| 3361 | |
| 3362 | return 1; |
| 3363 | } |
| 3364 | |
| 3365 | /* Just check the input and prepare the stack for the previous |
| 3366 | * function "hlua_channel_forward_yield" |
| 3367 | */ |
| 3368 | __LJMP static int hlua_channel_forward(lua_State *L) |
| 3369 | { |
| 3370 | MAY_LJMP(check_args(L, 2, "forward")); |
| 3371 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3372 | MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3373 | |
| 3374 | lua_pushinteger(L, 0); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3375 | return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3376 | } |
| 3377 | |
| 3378 | /* Just returns the number of bytes available in the input |
| 3379 | * side of the buffer. This function never fails. |
| 3380 | */ |
| 3381 | __LJMP static int hlua_channel_get_in_len(lua_State *L) |
| 3382 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3383 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3384 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3385 | MAY_LJMP(check_args(L, 1, "get_in_len")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3386 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a3ceac1 | 2018-12-14 13:39:09 +0100 | [diff] [blame] | 3387 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3388 | struct htx *htx = htxbuf(&chn->buf); |
| 3389 | lua_pushinteger(L, htx->data - co_data(chn)); |
| 3390 | } |
| 3391 | else |
| 3392 | lua_pushinteger(L, ci_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3393 | return 1; |
| 3394 | } |
| 3395 | |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3396 | /* Returns true if the channel is full. */ |
| 3397 | __LJMP static int hlua_channel_is_full(lua_State *L) |
| 3398 | { |
| 3399 | struct channel *chn; |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3400 | |
| 3401 | MAY_LJMP(check_args(L, 1, "is_full")); |
| 3402 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 0ec740e | 2020-02-26 11:59:19 +0100 | [diff] [blame] | 3403 | /* ignore the reserve, we are not on a producer side (ie in an |
| 3404 | * applet). |
| 3405 | */ |
| 3406 | lua_pushboolean(L, channel_full(chn, 0)); |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3407 | return 1; |
| 3408 | } |
| 3409 | |
Christopher Faulet | 2ac9ba2 | 2020-02-25 10:15:50 +0100 | [diff] [blame] | 3410 | /* Returns true if the channel is the response channel. */ |
| 3411 | __LJMP static int hlua_channel_is_resp(lua_State *L) |
| 3412 | { |
| 3413 | struct channel *chn; |
| 3414 | |
| 3415 | MAY_LJMP(check_args(L, 1, "is_resp")); |
| 3416 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3417 | |
| 3418 | lua_pushboolean(L, !!(chn->flags & CF_ISRESP)); |
| 3419 | return 1; |
| 3420 | } |
| 3421 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3422 | /* Just returns the number of bytes available in the output |
| 3423 | * side of the buffer. This function never fails. |
| 3424 | */ |
| 3425 | __LJMP static int hlua_channel_get_out_len(lua_State *L) |
| 3426 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3427 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3428 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3429 | MAY_LJMP(check_args(L, 1, "get_out_len")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3430 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3431 | lua_pushinteger(L, co_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3432 | return 1; |
| 3433 | } |
| 3434 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3435 | /* |
| 3436 | * |
| 3437 | * |
| 3438 | * Class Fetches |
| 3439 | * |
| 3440 | * |
| 3441 | */ |
| 3442 | |
| 3443 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3444 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3445 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3446 | __LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud) |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3447 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3448 | return MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3449 | } |
| 3450 | |
| 3451 | /* This function creates and push in the stack a fetch object according |
| 3452 | * with a current TXN. |
| 3453 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3454 | 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] | 3455 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3456 | struct hlua_smp *hsmp; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3457 | |
| 3458 | /* Check stack size. */ |
| 3459 | if (!lua_checkstack(L, 3)) |
| 3460 | return 0; |
| 3461 | |
| 3462 | /* Create the object: obj[0] = userdata. |
| 3463 | * Note that the base of the Fetches object is the |
| 3464 | * transaction object. |
| 3465 | */ |
| 3466 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3467 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3468 | lua_rawseti(L, -2, 0); |
| 3469 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3470 | hsmp->s = txn->s; |
| 3471 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 3472 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3473 | hsmp->flags = flags; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3474 | |
| 3475 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 3476 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref); |
| 3477 | lua_setmetatable(L, -2); |
| 3478 | |
| 3479 | return 1; |
| 3480 | } |
| 3481 | |
| 3482 | /* This function is an LUA binding. It is called with each sample-fetch. |
| 3483 | * It uses closure argument to store the associated sample-fetch. It |
| 3484 | * returns only one argument or throws an error. An error is thrown |
| 3485 | * only if an error is encountered during the argument parsing. If |
| 3486 | * the "sample-fetch" function fails, nil is returned. |
| 3487 | */ |
| 3488 | __LJMP static int hlua_run_sample_fetch(lua_State *L) |
| 3489 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3490 | struct hlua_smp *hsmp; |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 3491 | struct sample_fetch *f; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 3492 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3493 | int i; |
| 3494 | struct sample smp; |
| 3495 | |
| 3496 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3497 | f = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3498 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3499 | /* Get traditional arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3500 | hsmp = MAY_LJMP(hlua_checkfetches(L, 1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3501 | |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 3502 | /* Check execution authorization. */ |
| 3503 | if (f->use & SMP_USE_HTTP_ANY && |
| 3504 | !(hsmp->flags & HLUA_F_MAY_USE_HTTP)) { |
| 3505 | lua_pushfstring(L, "the sample-fetch '%s' needs an HTTP parser which " |
| 3506 | "is not available in Lua services", f->kw); |
| 3507 | WILL_LJMP(lua_error(L)); |
| 3508 | } |
| 3509 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3510 | /* Get extra arguments. */ |
| 3511 | for (i = 0; i < lua_gettop(L) - 1; i++) { |
| 3512 | if (i >= ARGM_NBARGS) |
| 3513 | break; |
| 3514 | hlua_lua2arg(L, i + 2, &args[i]); |
| 3515 | } |
| 3516 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3517 | args[i].data.str.area = NULL; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3518 | |
| 3519 | /* Check arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3520 | 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] | 3521 | |
| 3522 | /* Run the special args checker. */ |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 3523 | if (f->val_args && !f->val_args(args, NULL)) { |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3524 | lua_pushfstring(L, "error in arguments"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3525 | goto error; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3526 | } |
| 3527 | |
| 3528 | /* Initialise the sample. */ |
| 3529 | memset(&smp, 0, sizeof(smp)); |
| 3530 | |
| 3531 | /* Run the sample fetch process. */ |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 3532 | 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] | 3533 | if (!f->process(args, &smp, f->kw, f->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3534 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3535 | lua_pushstring(L, ""); |
| 3536 | else |
| 3537 | lua_pushnil(L); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3538 | goto end; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3539 | } |
| 3540 | |
| 3541 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3542 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3543 | hlua_smp2lua_str(L, &smp); |
| 3544 | else |
| 3545 | hlua_smp2lua(L, &smp); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3546 | |
| 3547 | end: |
| 3548 | for (i = 0; args[i].type != ARGT_STOP; i++) { |
| 3549 | if (args[i].type == ARGT_STR) |
| 3550 | chunk_destroy(&args[i].data.str); |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 3551 | else if (args[i].type == ARGT_REG) |
| 3552 | regex_free(args[i].data.reg); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3553 | } |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3554 | return 1; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3555 | |
| 3556 | error: |
| 3557 | for (i = 0; args[i].type != ARGT_STOP; i++) { |
| 3558 | if (args[i].type == ARGT_STR) |
| 3559 | chunk_destroy(&args[i].data.str); |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 3560 | else if (args[i].type == ARGT_REG) |
| 3561 | regex_free(args[i].data.reg); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3562 | } |
| 3563 | WILL_LJMP(lua_error(L)); |
| 3564 | return 0; /* Never reached */ |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3565 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3566 | |
| 3567 | /* |
| 3568 | * |
| 3569 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3570 | * Class Converters |
| 3571 | * |
| 3572 | * |
| 3573 | */ |
| 3574 | |
| 3575 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3576 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3577 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3578 | __LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3579 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3580 | return MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3581 | } |
| 3582 | |
| 3583 | /* This function creates and push in the stack a Converters object |
| 3584 | * according with a current TXN. |
| 3585 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3586 | 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] | 3587 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3588 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3589 | |
| 3590 | /* Check stack size. */ |
| 3591 | if (!lua_checkstack(L, 3)) |
| 3592 | return 0; |
| 3593 | |
| 3594 | /* Create the object: obj[0] = userdata. |
| 3595 | * Note that the base of the Converters object is the |
| 3596 | * same than the TXN object. |
| 3597 | */ |
| 3598 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3599 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3600 | lua_rawseti(L, -2, 0); |
| 3601 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3602 | hsmp->s = txn->s; |
| 3603 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 3604 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3605 | hsmp->flags = flags; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3606 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3607 | /* Pop a class stream metatable and affect it to the table. */ |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3608 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref); |
| 3609 | lua_setmetatable(L, -2); |
| 3610 | |
| 3611 | return 1; |
| 3612 | } |
| 3613 | |
| 3614 | /* This function is an LUA binding. It is called with each converter. |
| 3615 | * It uses closure argument to store the associated converter. It |
| 3616 | * returns only one argument or throws an error. An error is thrown |
| 3617 | * only if an error is encountered during the argument parsing. If |
| 3618 | * the converter function function fails, nil is returned. |
| 3619 | */ |
| 3620 | __LJMP static int hlua_run_sample_conv(lua_State *L) |
| 3621 | { |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3622 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3623 | struct sample_conv *conv; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 3624 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3625 | int i; |
| 3626 | struct sample smp; |
| 3627 | |
| 3628 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3629 | conv = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3630 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3631 | /* Get traditional arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3632 | hsmp = MAY_LJMP(hlua_checkconverters(L, 1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3633 | |
| 3634 | /* Get extra arguments. */ |
| 3635 | for (i = 0; i < lua_gettop(L) - 2; i++) { |
| 3636 | if (i >= ARGM_NBARGS) |
| 3637 | break; |
| 3638 | hlua_lua2arg(L, i + 3, &args[i]); |
| 3639 | } |
| 3640 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3641 | args[i].data.str.area = NULL; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3642 | |
| 3643 | /* Check arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3644 | 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] | 3645 | |
| 3646 | /* Run the special args checker. */ |
| 3647 | if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) { |
| 3648 | hlua_pusherror(L, "error in arguments"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3649 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3650 | } |
| 3651 | |
| 3652 | /* Initialise the sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 3653 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3654 | if (!hlua_lua2smp(L, 2, &smp)) { |
| 3655 | hlua_pusherror(L, "error in the input argument"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3656 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3657 | } |
| 3658 | |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 3659 | smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR); |
| 3660 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3661 | /* Apply expected cast. */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3662 | if (!sample_casts[smp.data.type][conv->in_type]) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3663 | hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'", |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3664 | smp_to_type[smp.data.type], smp_to_type[conv->in_type]); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3665 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3666 | } |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3667 | if (sample_casts[smp.data.type][conv->in_type] != c_none && |
| 3668 | !sample_casts[smp.data.type][conv->in_type](&smp)) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3669 | hlua_pusherror(L, "error during the input argument casting"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3670 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3671 | } |
| 3672 | |
| 3673 | /* Run the sample conversion process. */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 3674 | if (!conv->process(args, &smp, conv->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3675 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3676 | lua_pushstring(L, ""); |
| 3677 | else |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 3678 | lua_pushnil(L); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3679 | goto end; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3680 | } |
| 3681 | |
| 3682 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3683 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3684 | hlua_smp2lua_str(L, &smp); |
| 3685 | else |
| 3686 | hlua_smp2lua(L, &smp); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3687 | end: |
| 3688 | for (i = 0; args[i].type != ARGT_STOP; i++) { |
| 3689 | if (args[i].type == ARGT_STR) |
| 3690 | chunk_destroy(&args[i].data.str); |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 3691 | else if (args[i].type == ARGT_REG) |
| 3692 | regex_free(args[i].data.reg); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3693 | } |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 3694 | return 1; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3695 | |
| 3696 | error: |
| 3697 | for (i = 0; args[i].type != ARGT_STOP; i++) { |
| 3698 | if (args[i].type == ARGT_STR) |
| 3699 | chunk_destroy(&args[i].data.str); |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 3700 | else if (args[i].type == ARGT_REG) |
| 3701 | regex_free(args[i].data.reg); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3702 | } |
| 3703 | WILL_LJMP(lua_error(L)); |
| 3704 | return 0; /* Never reached */ |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3705 | } |
| 3706 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3707 | /* |
| 3708 | * |
| 3709 | * |
| 3710 | * Class AppletTCP |
| 3711 | * |
| 3712 | * |
| 3713 | */ |
| 3714 | |
| 3715 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 3716 | * a class stream, otherwise it throws an error. |
| 3717 | */ |
| 3718 | __LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud) |
| 3719 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3720 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3721 | } |
| 3722 | |
| 3723 | /* This function creates and push in the stack an Applet object |
| 3724 | * according with a current TXN. |
| 3725 | */ |
| 3726 | static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx) |
| 3727 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3728 | struct hlua_appctx *luactx; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3729 | struct stream_interface *si = ctx->owner; |
| 3730 | struct stream *s = si_strm(si); |
| 3731 | struct proxy *p = s->be; |
| 3732 | |
| 3733 | /* Check stack size. */ |
| 3734 | if (!lua_checkstack(L, 3)) |
| 3735 | return 0; |
| 3736 | |
| 3737 | /* Create the object: obj[0] = userdata. |
| 3738 | * Note that the base of the Converters object is the |
| 3739 | * same than the TXN object. |
| 3740 | */ |
| 3741 | lua_newtable(L); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3742 | luactx = lua_newuserdata(L, sizeof(*luactx)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3743 | lua_rawseti(L, -2, 0); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3744 | luactx->appctx = ctx; |
| 3745 | luactx->htxn.s = s; |
| 3746 | luactx->htxn.p = p; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3747 | |
| 3748 | /* Create the "f" field that contains a list of fetches. */ |
| 3749 | lua_pushstring(L, "f"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3750 | if (!hlua_fetches_new(L, &luactx->htxn, 0)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3751 | return 0; |
| 3752 | lua_settable(L, -3); |
| 3753 | |
| 3754 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 3755 | lua_pushstring(L, "sf"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3756 | if (!hlua_fetches_new(L, &luactx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3757 | return 0; |
| 3758 | lua_settable(L, -3); |
| 3759 | |
| 3760 | /* Create the "c" field that contains a list of converters. */ |
| 3761 | lua_pushstring(L, "c"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3762 | if (!hlua_converters_new(L, &luactx->htxn, 0)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3763 | return 0; |
| 3764 | lua_settable(L, -3); |
| 3765 | |
| 3766 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 3767 | lua_pushstring(L, "sc"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3768 | if (!hlua_converters_new(L, &luactx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3769 | return 0; |
| 3770 | lua_settable(L, -3); |
| 3771 | |
| 3772 | /* Pop a class stream metatable and affect it to the table. */ |
| 3773 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref); |
| 3774 | lua_setmetatable(L, -2); |
| 3775 | |
| 3776 | return 1; |
| 3777 | } |
| 3778 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3779 | __LJMP static int hlua_applet_tcp_set_var(lua_State *L) |
| 3780 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3781 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3782 | struct stream *s; |
| 3783 | const char *name; |
| 3784 | size_t len; |
| 3785 | struct sample smp; |
| 3786 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 3787 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 3788 | 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] | 3789 | |
| 3790 | /* It is useles to retrieve the stream, but this function |
| 3791 | * runs only in a stream context. |
| 3792 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3793 | luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3794 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3795 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3796 | |
| 3797 | /* Converts the third argument in a sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 3798 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3799 | hlua_lua2smp(L, 3, &smp); |
| 3800 | |
| 3801 | /* Store the sample in a variable. */ |
| 3802 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 3803 | |
| 3804 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 3805 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 3806 | else |
| 3807 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 3808 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 3809 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3810 | } |
| 3811 | |
| 3812 | __LJMP static int hlua_applet_tcp_unset_var(lua_State *L) |
| 3813 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3814 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3815 | struct stream *s; |
| 3816 | const char *name; |
| 3817 | size_t len; |
| 3818 | struct sample smp; |
| 3819 | |
| 3820 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 3821 | |
| 3822 | /* It is useles to retrieve the stream, but this function |
| 3823 | * runs only in a stream context. |
| 3824 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3825 | luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3826 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3827 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3828 | |
| 3829 | /* Unset the variable. */ |
| 3830 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 3831 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 3832 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3833 | } |
| 3834 | |
| 3835 | __LJMP static int hlua_applet_tcp_get_var(lua_State *L) |
| 3836 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3837 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3838 | struct stream *s; |
| 3839 | const char *name; |
| 3840 | size_t len; |
| 3841 | struct sample smp; |
| 3842 | |
| 3843 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 3844 | |
| 3845 | /* It is useles to retrieve the stream, but this function |
| 3846 | * runs only in a stream context. |
| 3847 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3848 | luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3849 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3850 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3851 | |
| 3852 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 3853 | if (!vars_get_by_name(name, len, &smp)) { |
| 3854 | lua_pushnil(L); |
| 3855 | return 1; |
| 3856 | } |
| 3857 | |
| 3858 | return hlua_smp2lua(L, &smp); |
| 3859 | } |
| 3860 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3861 | __LJMP static int hlua_applet_tcp_set_priv(lua_State *L) |
| 3862 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3863 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3864 | struct stream *s = luactx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 3865 | struct hlua *hlua; |
| 3866 | |
| 3867 | /* 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] | 3868 | if (!s->hlua) |
| 3869 | return 0; |
| 3870 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3871 | |
| 3872 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 3873 | |
| 3874 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 3875 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3876 | |
| 3877 | /* Get and store new value. */ |
| 3878 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 3879 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 3880 | |
| 3881 | return 0; |
| 3882 | } |
| 3883 | |
| 3884 | __LJMP static int hlua_applet_tcp_get_priv(lua_State *L) |
| 3885 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3886 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3887 | struct stream *s = luactx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 3888 | struct hlua *hlua; |
| 3889 | |
| 3890 | /* 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] | 3891 | if (!s->hlua) { |
| 3892 | lua_pushnil(L); |
| 3893 | return 1; |
| 3894 | } |
| 3895 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3896 | |
| 3897 | /* Push configuration index in the stack. */ |
| 3898 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 3899 | |
| 3900 | return 1; |
| 3901 | } |
| 3902 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3903 | /* If expected data not yet available, it returns a yield. This function |
| 3904 | * consumes the data in the buffer. It returns a string containing the |
| 3905 | * data. This string can be empty. |
| 3906 | */ |
| 3907 | __LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx) |
| 3908 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3909 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3910 | struct stream_interface *si = luactx->appctx->owner; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3911 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3912 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3913 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3914 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3915 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3916 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3917 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3918 | ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3919 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3920 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3921 | if (ret == 0) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3922 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3923 | 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] | 3924 | } |
| 3925 | |
| 3926 | /* End of data: commit the total strings and return. */ |
| 3927 | if (ret < 0) { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3928 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3929 | return 1; |
| 3930 | } |
| 3931 | |
| 3932 | /* Ensure that the block 2 length is usable. */ |
| 3933 | if (ret == 1) |
| 3934 | len2 = 0; |
| 3935 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 3936 | /* don't check the max length read and don't check. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3937 | luaL_addlstring(&luactx->b, blk1, len1); |
| 3938 | luaL_addlstring(&luactx->b, blk2, len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3939 | |
| 3940 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3941 | co_skip(si_oc(si), len1 + len2); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3942 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3943 | return 1; |
| 3944 | } |
| 3945 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3946 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3947 | __LJMP static int hlua_applet_tcp_getline(lua_State *L) |
| 3948 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3949 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3950 | |
| 3951 | /* Initialise the string catenation. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3952 | luaL_buffinit(L, &luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3953 | |
| 3954 | return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0)); |
| 3955 | } |
| 3956 | |
| 3957 | /* If expected data not yet available, it returns a yield. This function |
| 3958 | * consumes the data in the buffer. It returns a string containing the |
| 3959 | * data. This string can be empty. |
| 3960 | */ |
| 3961 | __LJMP static int hlua_applet_tcp_recv_yield(lua_State *L, int status, lua_KContext ctx) |
| 3962 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3963 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3964 | struct stream_interface *si = luactx->appctx->owner; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3965 | size_t len = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3966 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3967 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3968 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3969 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3970 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3971 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3972 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3973 | ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3974 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3975 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3976 | if (ret == 0) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3977 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3978 | 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] | 3979 | } |
| 3980 | |
| 3981 | /* End of data: commit the total strings and return. */ |
| 3982 | if (ret < 0) { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3983 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3984 | return 1; |
| 3985 | } |
| 3986 | |
| 3987 | /* Ensure that the block 2 length is usable. */ |
| 3988 | if (ret == 1) |
| 3989 | len2 = 0; |
| 3990 | |
| 3991 | if (len == -1) { |
| 3992 | |
| 3993 | /* If len == -1, catenate all the data avalaile and |
| 3994 | * yield because we want to get all the data until |
| 3995 | * the end of data stream. |
| 3996 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3997 | luaL_addlstring(&luactx->b, blk1, len1); |
| 3998 | luaL_addlstring(&luactx->b, blk2, len2); |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3999 | co_skip(si_oc(si), len1 + len2); |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 4000 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4001 | 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] | 4002 | |
| 4003 | } else { |
| 4004 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4005 | /* Copy the first block caping to the length required. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4006 | if (len1 > len) |
| 4007 | len1 = len; |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4008 | luaL_addlstring(&luactx->b, blk1, len1); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4009 | len -= len1; |
| 4010 | |
| 4011 | /* Copy the second block. */ |
| 4012 | if (len2 > len) |
| 4013 | len2 = len; |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4014 | luaL_addlstring(&luactx->b, blk2, len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4015 | len -= len2; |
| 4016 | |
| 4017 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4018 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4019 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4020 | /* If there is no other data available, yield waiting for new data. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4021 | if (len > 0) { |
| 4022 | lua_pushinteger(L, len); |
| 4023 | lua_replace(L, 2); |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 4024 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4025 | 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] | 4026 | } |
| 4027 | |
| 4028 | /* return the result. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4029 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4030 | return 1; |
| 4031 | } |
| 4032 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4033 | /* we never execute this */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4034 | hlua_pusherror(L, "Lua: internal error"); |
| 4035 | WILL_LJMP(lua_error(L)); |
| 4036 | return 0; |
| 4037 | } |
| 4038 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4039 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4040 | __LJMP static int hlua_applet_tcp_recv(lua_State *L) |
| 4041 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4042 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4043 | int len = -1; |
| 4044 | |
| 4045 | if (lua_gettop(L) > 2) |
| 4046 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 4047 | if (lua_gettop(L) >= 2) { |
| 4048 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4049 | lua_pop(L, 1); |
| 4050 | } |
| 4051 | |
| 4052 | /* Confirm or set the required length */ |
| 4053 | lua_pushinteger(L, len); |
| 4054 | |
| 4055 | /* Initialise the string catenation. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4056 | luaL_buffinit(L, &luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4057 | |
| 4058 | return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0)); |
| 4059 | } |
| 4060 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4061 | /* Append data in the output side of the buffer. This data is immediately |
| 4062 | * sent. The function returns the amount of data written. If the buffer |
| 4063 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4064 | * if the channel is closed. |
| 4065 | */ |
| 4066 | __LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx) |
| 4067 | { |
| 4068 | size_t len; |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4069 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4070 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4071 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4072 | struct stream_interface *si = luactx->appctx->owner; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4073 | struct channel *chn = si_ic(si); |
| 4074 | int max; |
| 4075 | |
| 4076 | /* Get the max amount of data which can write as input in the channel. */ |
| 4077 | max = channel_recv_max(chn); |
| 4078 | if (max > (len - l)) |
| 4079 | max = len - l; |
| 4080 | |
| 4081 | /* Copy data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4082 | ci_putblk(chn, str + l, max); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4083 | |
| 4084 | /* update counters. */ |
| 4085 | l += max; |
| 4086 | lua_pop(L, 1); |
| 4087 | lua_pushinteger(L, l); |
| 4088 | |
| 4089 | /* If some data is not send, declares the situation to the |
| 4090 | * applet, and returns a yield. |
| 4091 | */ |
| 4092 | if (l < len) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 4093 | si_rx_room_blk(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4094 | 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] | 4095 | } |
| 4096 | |
| 4097 | return 1; |
| 4098 | } |
| 4099 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4100 | /* Just a wrapper of "hlua_applet_tcp_send_yield". This wrapper permits |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4101 | * yield the LUA process, and resume it without checking the |
| 4102 | * input arguments. |
| 4103 | */ |
| 4104 | __LJMP static int hlua_applet_tcp_send(lua_State *L) |
| 4105 | { |
| 4106 | MAY_LJMP(check_args(L, 2, "send")); |
| 4107 | lua_pushinteger(L, 0); |
| 4108 | |
| 4109 | return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0)); |
| 4110 | } |
| 4111 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4112 | /* |
| 4113 | * |
| 4114 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4115 | * Class AppletHTTP |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4116 | * |
| 4117 | * |
| 4118 | */ |
| 4119 | |
| 4120 | /* Returns a struct hlua_txn if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4121 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4122 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4123 | __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] | 4124 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4125 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_http_ref)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4126 | } |
| 4127 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4128 | /* This function creates and push in the stack an Applet object |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4129 | * according with a current TXN. |
| 4130 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4131 | static int hlua_applet_http_new(lua_State *L, struct appctx *ctx) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4132 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4133 | struct hlua_appctx *luactx; |
Thierry FOURNIER | 841475e | 2015-12-11 17:10:09 +0100 | [diff] [blame] | 4134 | struct hlua_txn htxn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4135 | struct stream_interface *si = ctx->owner; |
| 4136 | struct stream *s = si_strm(si); |
| 4137 | struct proxy *px = s->be; |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4138 | struct htx *htx; |
| 4139 | struct htx_blk *blk; |
| 4140 | struct htx_sl *sl; |
| 4141 | struct ist path; |
| 4142 | unsigned long long len = 0; |
| 4143 | int32_t pos; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4144 | |
| 4145 | /* Check stack size. */ |
| 4146 | if (!lua_checkstack(L, 3)) |
| 4147 | return 0; |
| 4148 | |
| 4149 | /* Create the object: obj[0] = userdata. |
| 4150 | * Note that the base of the Converters object is the |
| 4151 | * same than the TXN object. |
| 4152 | */ |
| 4153 | lua_newtable(L); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4154 | luactx = lua_newuserdata(L, sizeof(*luactx)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4155 | lua_rawseti(L, -2, 0); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4156 | luactx->appctx = ctx; |
| 4157 | luactx->appctx->ctx.hlua_apphttp.status = 200; /* Default status code returned. */ |
| 4158 | luactx->appctx->ctx.hlua_apphttp.reason = NULL; /* Use default reason based on status */ |
| 4159 | luactx->htxn.s = s; |
| 4160 | luactx->htxn.p = px; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4161 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4162 | /* Create the "f" field that contains a list of fetches. */ |
| 4163 | lua_pushstring(L, "f"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4164 | if (!hlua_fetches_new(L, &luactx->htxn, 0)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4165 | return 0; |
| 4166 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4167 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4168 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 4169 | lua_pushstring(L, "sf"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4170 | if (!hlua_fetches_new(L, &luactx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4171 | return 0; |
| 4172 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4173 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4174 | /* Create the "c" field that contains a list of converters. */ |
| 4175 | lua_pushstring(L, "c"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4176 | if (!hlua_converters_new(L, &luactx->htxn, 0)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4177 | return 0; |
| 4178 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4179 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4180 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 4181 | lua_pushstring(L, "sc"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4182 | if (!hlua_converters_new(L, &luactx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4183 | return 0; |
| 4184 | lua_settable(L, -3); |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4185 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4186 | htx = htxbuf(&s->req.buf); |
| 4187 | blk = htx_get_first_blk(htx); |
Christopher Faulet | ea00973 | 2019-11-18 15:50:25 +0100 | [diff] [blame] | 4188 | BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4189 | sl = htx_get_blk_ptr(htx, blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4190 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4191 | /* Stores the request method. */ |
| 4192 | lua_pushstring(L, "method"); |
| 4193 | lua_pushlstring(L, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); |
| 4194 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4195 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4196 | /* Stores the http version. */ |
| 4197 | lua_pushstring(L, "version"); |
| 4198 | lua_pushlstring(L, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); |
| 4199 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4200 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4201 | /* creates an array of headers. hlua_http_get_headers() crates and push |
| 4202 | * the array on the top of the stack. |
| 4203 | */ |
| 4204 | lua_pushstring(L, "headers"); |
| 4205 | htxn.s = s; |
| 4206 | htxn.p = px; |
| 4207 | htxn.dir = SMP_OPT_DIR_REQ; |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 4208 | if (!hlua_http_get_headers(L, &htxn.s->txn->req)) |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4209 | return 0; |
| 4210 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4211 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4212 | path = http_get_path(htx_sl_req_uri(sl)); |
Tim Duesterhus | ed52637 | 2020-03-05 17:56:33 +0100 | [diff] [blame] | 4213 | if (isttest(path)) { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4214 | char *p, *q, *end; |
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 | p = path.ptr; |
| 4217 | end = path.ptr + path.len; |
| 4218 | q = p; |
| 4219 | while (q < end && *q != '?') |
| 4220 | q++; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4221 | |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 4222 | /* Stores the request path. */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4223 | lua_pushstring(L, "path"); |
| 4224 | lua_pushlstring(L, p, q - p); |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 4225 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4226 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4227 | /* Stores the query string. */ |
| 4228 | lua_pushstring(L, "qs"); |
| 4229 | if (*q == '?') |
| 4230 | q++; |
| 4231 | lua_pushlstring(L, q, end - q); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4232 | lua_settable(L, -3); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4233 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4234 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4235 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 4236 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 4237 | enum htx_blk_type type = htx_get_blk_type(blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4238 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 4239 | if (type == HTX_BLK_TLR || type == HTX_BLK_EOT) |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4240 | break; |
| 4241 | if (type == HTX_BLK_DATA) |
| 4242 | len += htx_get_blksz(blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4243 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4244 | if (htx->extra != ULLONG_MAX) |
| 4245 | len += htx->extra; |
| 4246 | |
| 4247 | /* Stores the request path. */ |
| 4248 | lua_pushstring(L, "length"); |
| 4249 | lua_pushinteger(L, len); |
| 4250 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4251 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4252 | /* Create an empty array of HTTP request headers. */ |
| 4253 | lua_pushstring(L, "response"); |
| 4254 | lua_newtable(L); |
| 4255 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4256 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4257 | /* Pop a class stream metatable and affect it to the table. */ |
| 4258 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref); |
| 4259 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4260 | |
| 4261 | return 1; |
| 4262 | } |
| 4263 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4264 | __LJMP static int hlua_applet_http_set_var(lua_State *L) |
| 4265 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4266 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4267 | struct stream *s; |
| 4268 | const char *name; |
| 4269 | size_t len; |
| 4270 | struct sample smp; |
| 4271 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 4272 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 4273 | 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] | 4274 | |
| 4275 | /* It is useles to retrieve the stream, but this function |
| 4276 | * runs only in a stream context. |
| 4277 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4278 | luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4279 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4280 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4281 | |
| 4282 | /* Converts the third argument in a sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 4283 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4284 | hlua_lua2smp(L, 3, &smp); |
| 4285 | |
| 4286 | /* Store the sample in a variable. */ |
| 4287 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 4288 | |
| 4289 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 4290 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 4291 | else |
| 4292 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 4293 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 4294 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4295 | } |
| 4296 | |
| 4297 | __LJMP static int hlua_applet_http_unset_var(lua_State *L) |
| 4298 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4299 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4300 | struct stream *s; |
| 4301 | const char *name; |
| 4302 | size_t len; |
| 4303 | struct sample smp; |
| 4304 | |
| 4305 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 4306 | |
| 4307 | /* It is useles to retrieve the stream, but this function |
| 4308 | * runs only in a stream context. |
| 4309 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4310 | luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4311 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4312 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4313 | |
| 4314 | /* Unset the variable. */ |
| 4315 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 4316 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 4317 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4318 | } |
| 4319 | |
| 4320 | __LJMP static int hlua_applet_http_get_var(lua_State *L) |
| 4321 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4322 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4323 | struct stream *s; |
| 4324 | const char *name; |
| 4325 | size_t len; |
| 4326 | struct sample smp; |
| 4327 | |
| 4328 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 4329 | |
| 4330 | /* It is useles to retrieve the stream, but this function |
| 4331 | * runs only in a stream context. |
| 4332 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4333 | luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4334 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4335 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4336 | |
| 4337 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 4338 | if (!vars_get_by_name(name, len, &smp)) { |
| 4339 | lua_pushnil(L); |
| 4340 | return 1; |
| 4341 | } |
| 4342 | |
| 4343 | return hlua_smp2lua(L, &smp); |
| 4344 | } |
| 4345 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4346 | __LJMP static int hlua_applet_http_set_priv(lua_State *L) |
| 4347 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4348 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4349 | struct stream *s = luactx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4350 | struct hlua *hlua; |
| 4351 | |
| 4352 | /* 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] | 4353 | if (!s->hlua) |
| 4354 | return 0; |
| 4355 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4356 | |
| 4357 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 4358 | |
| 4359 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 4360 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4361 | |
| 4362 | /* Get and store new value. */ |
| 4363 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 4364 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 4365 | |
| 4366 | return 0; |
| 4367 | } |
| 4368 | |
| 4369 | __LJMP static int hlua_applet_http_get_priv(lua_State *L) |
| 4370 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4371 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4372 | struct stream *s = luactx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4373 | struct hlua *hlua; |
| 4374 | |
| 4375 | /* 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] | 4376 | if (!s->hlua) { |
| 4377 | lua_pushnil(L); |
| 4378 | return 1; |
| 4379 | } |
| 4380 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4381 | |
| 4382 | /* Push configuration index in the stack. */ |
| 4383 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 4384 | |
| 4385 | return 1; |
| 4386 | } |
| 4387 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4388 | /* If expected data not yet available, it returns a yield. This function |
| 4389 | * consumes the data in the buffer. It returns a string containing the |
| 4390 | * data. This string can be empty. |
| 4391 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4392 | __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] | 4393 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4394 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4395 | struct stream_interface *si = luactx->appctx->owner; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4396 | struct channel *req = si_oc(si); |
| 4397 | struct htx *htx; |
| 4398 | struct htx_blk *blk; |
| 4399 | size_t count; |
| 4400 | int stop = 0; |
| 4401 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4402 | htx = htx_from_buf(&req->buf); |
| 4403 | count = co_data(req); |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 4404 | blk = htx_get_first_blk(htx); |
Christopher Faulet | cc26b13 | 2018-12-18 21:20:57 +0100 | [diff] [blame] | 4405 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4406 | while (count && !stop && blk) { |
| 4407 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4408 | uint32_t sz = htx_get_blksz(blk); |
| 4409 | struct ist v; |
| 4410 | uint32_t vlen; |
| 4411 | char *nl; |
| 4412 | |
| 4413 | vlen = sz; |
| 4414 | if (vlen > count) { |
| 4415 | if (type != HTX_BLK_DATA) |
| 4416 | break; |
| 4417 | vlen = count; |
| 4418 | } |
| 4419 | |
| 4420 | switch (type) { |
| 4421 | case HTX_BLK_UNUSED: |
| 4422 | break; |
| 4423 | |
| 4424 | case HTX_BLK_DATA: |
| 4425 | v = htx_get_blk_value(htx, blk); |
| 4426 | v.len = vlen; |
| 4427 | nl = istchr(v, '\n'); |
| 4428 | if (nl != NULL) { |
| 4429 | stop = 1; |
| 4430 | vlen = nl - v.ptr + 1; |
| 4431 | } |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4432 | luaL_addlstring(&luactx->b, v.ptr, vlen); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4433 | break; |
| 4434 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4435 | case HTX_BLK_TLR: |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 4436 | case HTX_BLK_EOT: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4437 | stop = 1; |
| 4438 | break; |
| 4439 | |
| 4440 | default: |
| 4441 | break; |
| 4442 | } |
| 4443 | |
| 4444 | co_set_data(req, co_data(req) - vlen); |
| 4445 | count -= vlen; |
| 4446 | if (sz == vlen) |
| 4447 | blk = htx_remove_blk(htx, blk); |
| 4448 | else { |
| 4449 | htx_cut_data_blk(htx, blk, vlen); |
| 4450 | break; |
| 4451 | } |
| 4452 | } |
| 4453 | |
Christopher Faulet | eccb31c | 2021-04-02 14:24:56 +0200 | [diff] [blame] | 4454 | /* The message was fully consumed and no more data are expected |
| 4455 | * (EOM flag set). |
| 4456 | */ |
| 4457 | if (htx_is_empty(htx) && (htx->flags & HTX_FL_EOM)) |
| 4458 | stop = 1; |
| 4459 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4460 | htx_to_buf(htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4461 | if (!stop) { |
| 4462 | si_cant_get(si); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4463 | 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] | 4464 | } |
| 4465 | |
| 4466 | /* return the result. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4467 | luaL_pushresult(&luactx->b); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4468 | return 1; |
| 4469 | } |
| 4470 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4471 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4472 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4473 | __LJMP static int hlua_applet_http_getline(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4474 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4475 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4476 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4477 | /* Initialise the string catenation. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4478 | luaL_buffinit(L, &luactx->b); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4479 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4480 | return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4481 | } |
| 4482 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4483 | /* If expected data not yet available, it returns a yield. This function |
| 4484 | * consumes the data in the buffer. It returns a string containing the |
| 4485 | * data. This string can be empty. |
| 4486 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4487 | __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] | 4488 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4489 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4490 | struct stream_interface *si = luactx->appctx->owner; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4491 | struct channel *req = si_oc(si); |
| 4492 | struct htx *htx; |
| 4493 | struct htx_blk *blk; |
| 4494 | size_t count; |
| 4495 | int len; |
| 4496 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4497 | htx = htx_from_buf(&req->buf); |
| 4498 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4499 | count = co_data(req); |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 4500 | blk = htx_get_head_blk(htx); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4501 | while (count && len && blk) { |
| 4502 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4503 | uint32_t sz = htx_get_blksz(blk); |
| 4504 | struct ist v; |
| 4505 | uint32_t vlen; |
| 4506 | |
| 4507 | vlen = sz; |
| 4508 | if (len > 0 && vlen > len) |
| 4509 | vlen = len; |
| 4510 | if (vlen > count) { |
| 4511 | if (type != HTX_BLK_DATA) |
| 4512 | break; |
| 4513 | vlen = count; |
| 4514 | } |
| 4515 | |
| 4516 | switch (type) { |
| 4517 | case HTX_BLK_UNUSED: |
| 4518 | break; |
| 4519 | |
| 4520 | case HTX_BLK_DATA: |
| 4521 | v = htx_get_blk_value(htx, blk); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4522 | luaL_addlstring(&luactx->b, v.ptr, vlen); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4523 | break; |
| 4524 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4525 | case HTX_BLK_TLR: |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 4526 | case HTX_BLK_EOT: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4527 | len = 0; |
| 4528 | break; |
| 4529 | |
| 4530 | default: |
| 4531 | break; |
| 4532 | } |
| 4533 | |
| 4534 | co_set_data(req, co_data(req) - vlen); |
| 4535 | count -= vlen; |
| 4536 | if (len > 0) |
| 4537 | len -= vlen; |
| 4538 | if (sz == vlen) |
| 4539 | blk = htx_remove_blk(htx, blk); |
| 4540 | else { |
| 4541 | htx_cut_data_blk(htx, blk, vlen); |
| 4542 | break; |
| 4543 | } |
| 4544 | } |
| 4545 | |
Christopher Faulet | eccb31c | 2021-04-02 14:24:56 +0200 | [diff] [blame] | 4546 | /* The message was fully consumed and no more data are expected |
| 4547 | * (EOM flag set). |
| 4548 | */ |
| 4549 | if (htx_is_empty(htx) && (htx->flags & HTX_FL_EOM)) |
| 4550 | len = 0; |
| 4551 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4552 | htx_to_buf(htx, &req->buf); |
| 4553 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4554 | /* If we are no other data available, yield waiting for new data. */ |
| 4555 | if (len) { |
| 4556 | if (len > 0) { |
| 4557 | lua_pushinteger(L, len); |
| 4558 | lua_replace(L, 2); |
| 4559 | } |
| 4560 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4561 | 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] | 4562 | } |
| 4563 | |
| 4564 | /* return the result. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4565 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4566 | return 1; |
| 4567 | } |
| 4568 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4569 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4570 | __LJMP static int hlua_applet_http_recv(lua_State *L) |
| 4571 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4572 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4573 | int len = -1; |
| 4574 | |
| 4575 | /* Check arguments. */ |
| 4576 | if (lua_gettop(L) > 2) |
| 4577 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 4578 | if (lua_gettop(L) >= 2) { |
| 4579 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4580 | lua_pop(L, 1); |
| 4581 | } |
| 4582 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4583 | lua_pushinteger(L, len); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4584 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4585 | /* Initialise the string catenation. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4586 | luaL_buffinit(L, &luactx->b); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4587 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4588 | return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4589 | } |
| 4590 | |
| 4591 | /* Append data in the output side of the buffer. This data is immediately |
| 4592 | * sent. The function returns the amount of data written. If the buffer |
| 4593 | * cannot contain the data, the function yields. The function returns -1 |
| 4594 | * if the channel is closed. |
| 4595 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4596 | __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] | 4597 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4598 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4599 | struct stream_interface *si = luactx->appctx->owner; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4600 | struct channel *res = si_ic(si); |
| 4601 | struct htx *htx = htx_from_buf(&res->buf); |
| 4602 | const char *data; |
| 4603 | size_t len; |
| 4604 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 4605 | int max; |
| 4606 | |
Christopher Faulet | 9060fc0 | 2019-07-03 11:39:30 +0200 | [diff] [blame] | 4607 | max = htx_get_max_blksz(htx, channel_htx_recv_max(res, htx)); |
Christopher Faulet | 4b0e9b2 | 2019-01-09 12:16:58 +0100 | [diff] [blame] | 4608 | if (!max) |
| 4609 | goto snd_yield; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4610 | |
| 4611 | data = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4612 | |
| 4613 | /* Get the max amount of data which can write as input in the channel. */ |
| 4614 | if (max > (len - l)) |
| 4615 | max = len - l; |
| 4616 | |
| 4617 | /* Copy data. */ |
Willy Tarreau | 0a7ef02 | 2019-05-28 10:30:11 +0200 | [diff] [blame] | 4618 | max = htx_add_data(htx, ist2(data + l, max)); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 4619 | channel_add_input(res, max); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4620 | |
| 4621 | /* update counters. */ |
| 4622 | l += max; |
| 4623 | lua_pop(L, 1); |
| 4624 | lua_pushinteger(L, l); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4625 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4626 | /* If some data is not send, declares the situation to the |
| 4627 | * applet, and returns a yield. |
| 4628 | */ |
| 4629 | if (l < len) { |
Christopher Faulet | 4b0e9b2 | 2019-01-09 12:16:58 +0100 | [diff] [blame] | 4630 | snd_yield: |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4631 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4632 | si_rx_room_blk(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4633 | 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] | 4634 | } |
| 4635 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4636 | htx_to_buf(htx, &res->buf); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4637 | return 1; |
| 4638 | } |
| 4639 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4640 | /* Just a wrapper of "hlua_applet_send_yield". This wrapper permits |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4641 | * yield the LUA process, and resume it without checking the |
| 4642 | * input arguments. |
| 4643 | */ |
| 4644 | __LJMP static int hlua_applet_http_send(lua_State *L) |
| 4645 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4646 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4647 | |
| 4648 | /* We want to send some data. Headers must be sent. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4649 | if (!(luactx->appctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4650 | hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data."); |
| 4651 | WILL_LJMP(lua_error(L)); |
| 4652 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4653 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4654 | /* This integer is used for followinf the amount of data sent. */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4655 | lua_pushinteger(L, 0); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4656 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4657 | return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4658 | } |
| 4659 | |
| 4660 | __LJMP static int hlua_applet_http_addheader(lua_State *L) |
| 4661 | { |
| 4662 | const char *name; |
| 4663 | int ret; |
| 4664 | |
| 4665 | MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4666 | name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 4667 | MAY_LJMP(luaL_checkstring(L, 3)); |
| 4668 | |
| 4669 | /* Push in the stack the "response" entry. */ |
| 4670 | ret = lua_getfield(L, 1, "response"); |
| 4671 | if (ret != LUA_TTABLE) { |
| 4672 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] " |
| 4673 | "is expected as an array. %s found", lua_typename(L, ret)); |
| 4674 | WILL_LJMP(lua_error(L)); |
| 4675 | } |
| 4676 | |
| 4677 | /* check if the header is already registered if it is not |
| 4678 | * the case, register it. |
| 4679 | */ |
| 4680 | ret = lua_getfield(L, -1, name); |
| 4681 | if (ret == LUA_TNIL) { |
| 4682 | |
| 4683 | /* Entry not found. */ |
| 4684 | lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */ |
| 4685 | |
| 4686 | /* Insert the new header name in the array in the top of the stack. |
| 4687 | * It left the new array in the top of the stack. |
| 4688 | */ |
| 4689 | lua_newtable(L); |
| 4690 | lua_pushvalue(L, 2); |
| 4691 | lua_pushvalue(L, -2); |
| 4692 | lua_settable(L, -4); |
| 4693 | |
| 4694 | } else if (ret != LUA_TTABLE) { |
| 4695 | |
| 4696 | /* corruption error. */ |
| 4697 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] " |
| 4698 | "is expected as an array. %s found", name, lua_typename(L, ret)); |
| 4699 | WILL_LJMP(lua_error(L)); |
| 4700 | } |
| 4701 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 4702 | /* Now the top of thestack is an array of values. We push |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4703 | * the header value as new entry. |
| 4704 | */ |
| 4705 | lua_pushvalue(L, 3); |
| 4706 | ret = lua_rawlen(L, -2); |
| 4707 | lua_rawseti(L, -2, ret + 1); |
| 4708 | lua_pushboolean(L, 1); |
| 4709 | return 1; |
| 4710 | } |
| 4711 | |
| 4712 | __LJMP static int hlua_applet_http_status(lua_State *L) |
| 4713 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4714 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4715 | int status = MAY_LJMP(luaL_checkinteger(L, 2)); |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4716 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4717 | |
| 4718 | if (status < 100 || status > 599) { |
| 4719 | lua_pushboolean(L, 0); |
| 4720 | return 1; |
| 4721 | } |
| 4722 | |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4723 | luactx->appctx->ctx.hlua_apphttp.status = status; |
| 4724 | luactx->appctx->ctx.hlua_apphttp.reason = reason; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4725 | lua_pushboolean(L, 1); |
| 4726 | return 1; |
| 4727 | } |
| 4728 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4729 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4730 | __LJMP static int hlua_applet_http_send_response(lua_State *L) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4731 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4732 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4733 | struct stream_interface *si = luactx->appctx->owner; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4734 | struct channel *res = si_ic(si); |
| 4735 | struct htx *htx; |
| 4736 | struct htx_sl *sl; |
| 4737 | struct h1m h1m; |
| 4738 | const char *status, *reason; |
| 4739 | const char *name, *value; |
| 4740 | size_t nlen, vlen; |
| 4741 | unsigned int flags; |
| 4742 | |
| 4743 | /* Send the message at once. */ |
| 4744 | htx = htx_from_buf(&res->buf); |
| 4745 | h1m_init_res(&h1m); |
| 4746 | |
| 4747 | /* Use the same http version than the request. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4748 | status = ultoa_r(luactx->appctx->ctx.hlua_apphttp.status, trash.area, trash.size); |
| 4749 | reason = luactx->appctx->ctx.hlua_apphttp.reason; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4750 | if (reason == NULL) |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4751 | reason = http_get_reason(luactx->appctx->ctx.hlua_apphttp.status); |
| 4752 | if (luactx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4753 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11); |
| 4754 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist(status), ist(reason)); |
| 4755 | } |
| 4756 | else { |
| 4757 | flags = HTX_SL_F_IS_RESP; |
| 4758 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), ist(status), ist(reason)); |
| 4759 | } |
| 4760 | if (!sl) { |
| 4761 | hlua_pusherror(L, "Lua applet http '%s': Failed to create response.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4762 | luactx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4763 | WILL_LJMP(lua_error(L)); |
| 4764 | } |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4765 | sl->info.res.status = luactx->appctx->ctx.hlua_apphttp.status; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4766 | |
| 4767 | /* Get the array associated to the field "response" in the object AppletHTTP. */ |
| 4768 | lua_pushvalue(L, 0); |
| 4769 | if (lua_getfield(L, 1, "response") != LUA_TTABLE) { |
| 4770 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4771 | luactx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4772 | WILL_LJMP(lua_error(L)); |
| 4773 | } |
| 4774 | |
| 4775 | /* Browse the list of headers. */ |
| 4776 | lua_pushnil(L); |
| 4777 | while(lua_next(L, -2) != 0) { |
| 4778 | /* We expect a string as -2. */ |
| 4779 | if (lua_type(L, -2) != LUA_TSTRING) { |
| 4780 | 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] | 4781 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4782 | lua_typename(L, lua_type(L, -2))); |
| 4783 | WILL_LJMP(lua_error(L)); |
| 4784 | } |
| 4785 | name = lua_tolstring(L, -2, &nlen); |
| 4786 | |
| 4787 | /* We expect an array as -1. */ |
| 4788 | if (lua_type(L, -1) != LUA_TTABLE) { |
| 4789 | 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] | 4790 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4791 | name, |
| 4792 | lua_typename(L, lua_type(L, -1))); |
| 4793 | WILL_LJMP(lua_error(L)); |
| 4794 | } |
| 4795 | |
| 4796 | /* Browse the table who is on the top of the stack. */ |
| 4797 | lua_pushnil(L); |
| 4798 | while(lua_next(L, -2) != 0) { |
| 4799 | int id; |
| 4800 | |
| 4801 | /* We expect a number as -2. */ |
| 4802 | if (lua_type(L, -2) != LUA_TNUMBER) { |
| 4803 | 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] | 4804 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4805 | name, |
| 4806 | lua_typename(L, lua_type(L, -2))); |
| 4807 | WILL_LJMP(lua_error(L)); |
| 4808 | } |
| 4809 | id = lua_tointeger(L, -2); |
| 4810 | |
| 4811 | /* We expect a string as -2. */ |
| 4812 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 4813 | 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] | 4814 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4815 | name, id, |
| 4816 | lua_typename(L, lua_type(L, -1))); |
| 4817 | WILL_LJMP(lua_error(L)); |
| 4818 | } |
| 4819 | value = lua_tolstring(L, -1, &vlen); |
| 4820 | |
| 4821 | /* Simple Protocol checks. */ |
| 4822 | if (isteqi(ist2(name, nlen), ist("transfer-encoding"))) |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 4823 | h1_parse_xfer_enc_header(&h1m, ist2(value, vlen)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4824 | else if (isteqi(ist2(name, nlen), ist("content-length"))) { |
| 4825 | struct ist v = ist2(value, vlen); |
| 4826 | int ret; |
| 4827 | |
| 4828 | ret = h1_parse_cont_len_header(&h1m, &v); |
| 4829 | if (ret < 0) { |
| 4830 | hlua_pusherror(L, "Lua applet http '%s': Invalid '%s' header.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4831 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4832 | name); |
| 4833 | WILL_LJMP(lua_error(L)); |
| 4834 | } |
| 4835 | else if (ret == 0) |
| 4836 | goto next; /* Skip it */ |
| 4837 | } |
| 4838 | |
| 4839 | /* Add a new header */ |
| 4840 | if (!htx_add_header(htx, ist2(name, nlen), ist2(value, vlen))) { |
| 4841 | 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] | 4842 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4843 | name); |
| 4844 | WILL_LJMP(lua_error(L)); |
| 4845 | } |
| 4846 | next: |
| 4847 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 4848 | lua_pop(L, 1); |
| 4849 | } |
| 4850 | |
| 4851 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 4852 | lua_pop(L, 1); |
| 4853 | } |
| 4854 | |
| 4855 | if (h1m.flags & H1_MF_CHNK) |
| 4856 | h1m.flags &= ~H1_MF_CLEN; |
| 4857 | if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK)) |
| 4858 | h1m.flags |= H1_MF_XFER_LEN; |
| 4859 | |
| 4860 | /* Uset HTX start-line flags */ |
| 4861 | if (h1m.flags & H1_MF_XFER_ENC) |
| 4862 | flags |= HTX_SL_F_XFER_ENC; |
| 4863 | if (h1m.flags & H1_MF_XFER_LEN) { |
| 4864 | flags |= HTX_SL_F_XFER_LEN; |
| 4865 | if (h1m.flags & H1_MF_CHNK) |
| 4866 | flags |= HTX_SL_F_CHNK; |
| 4867 | else if (h1m.flags & H1_MF_CLEN) |
| 4868 | flags |= HTX_SL_F_CLEN; |
| 4869 | if (h1m.body_len == 0) |
| 4870 | flags |= HTX_SL_F_BODYLESS; |
| 4871 | } |
| 4872 | sl->flags |= flags; |
| 4873 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 4874 | /* 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] | 4875 | * and the status code implies the presence of a message body, we must |
| 4876 | * announce a transfer encoding chunked. This is required by haproxy |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4877 | * for the keepalive compliance. If the applet announces a transfer-encoding |
| 4878 | * chunked itself, don't do anything. |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4879 | */ |
| 4880 | 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] | 4881 | luactx->appctx->ctx.hlua_apphttp.status >= 200 && |
| 4882 | luactx->appctx->ctx.hlua_apphttp.status != 204 && |
| 4883 | luactx->appctx->ctx.hlua_apphttp.status != 304) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4884 | /* Add a new header */ |
| 4885 | sl->flags |= (HTX_SL_F_XFER_ENC|H1_MF_CHNK|H1_MF_XFER_LEN); |
| 4886 | if (!htx_add_header(htx, ist("transfer-encoding"), ist("chunked"))) { |
| 4887 | 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] | 4888 | luactx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4889 | WILL_LJMP(lua_error(L)); |
| 4890 | } |
| 4891 | } |
| 4892 | |
| 4893 | /* Finalize headers. */ |
| 4894 | if (!htx_add_endof(htx, HTX_BLK_EOH)) { |
| 4895 | hlua_pusherror(L, "Lua applet http '%s': Failed create the response.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4896 | luactx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4897 | WILL_LJMP(lua_error(L)); |
| 4898 | } |
| 4899 | |
| 4900 | if (htx_used_space(htx) > b_size(&res->buf) - global.tune.maxrewrite) { |
| 4901 | b_reset(&res->buf); |
| 4902 | hlua_pusherror(L, "Lua: 'start_response': response header block too big"); |
| 4903 | WILL_LJMP(lua_error(L)); |
| 4904 | } |
| 4905 | |
| 4906 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 4907 | channel_add_input(res, htx->data); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4908 | |
| 4909 | /* Headers sent, set the flag. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4910 | luactx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4911 | return 0; |
| 4912 | |
| 4913 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4914 | /* We will build the status line and the headers of the HTTP response. |
| 4915 | * We will try send at once if its not possible, we give back the hand |
| 4916 | * waiting for more room. |
| 4917 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4918 | __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] | 4919 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4920 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4921 | struct stream_interface *si = luactx->appctx->owner; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4922 | struct channel *res = si_ic(si); |
| 4923 | |
| 4924 | if (co_data(res)) { |
| 4925 | si_rx_room_blk(si); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4926 | 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] | 4927 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4928 | return MAY_LJMP(hlua_applet_http_send_response(L)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4929 | } |
| 4930 | |
| 4931 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4932 | __LJMP static int hlua_applet_http_start_response(lua_State *L) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4933 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4934 | return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4935 | } |
| 4936 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4937 | /* |
| 4938 | * |
| 4939 | * |
| 4940 | * Class HTTP |
| 4941 | * |
| 4942 | * |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4943 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4944 | |
| 4945 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 4946 | * a class stream, otherwise it throws an error. |
| 4947 | */ |
| 4948 | __LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4949 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4950 | return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref)); |
| 4951 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4952 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4953 | /* This function creates and push in the stack a HTTP object |
| 4954 | * according with a current TXN. |
| 4955 | */ |
| 4956 | static int hlua_http_new(lua_State *L, struct hlua_txn *txn) |
| 4957 | { |
| 4958 | struct hlua_txn *htxn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4959 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4960 | /* Check stack size. */ |
| 4961 | if (!lua_checkstack(L, 3)) |
| 4962 | return 0; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4963 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4964 | /* Create the object: obj[0] = userdata. |
| 4965 | * Note that the base of the Converters object is the |
| 4966 | * same than the TXN object. |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4967 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4968 | lua_newtable(L); |
| 4969 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
| 4970 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4971 | |
| 4972 | htxn->s = txn->s; |
| 4973 | htxn->p = txn->p; |
Christopher Faulet | 256b69a | 2019-05-23 11:14:21 +0200 | [diff] [blame] | 4974 | htxn->dir = txn->dir; |
| 4975 | htxn->flags = txn->flags; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4976 | |
| 4977 | /* Pop a class stream metatable and affect it to the table. */ |
| 4978 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref); |
| 4979 | lua_setmetatable(L, -2); |
| 4980 | |
| 4981 | return 1; |
| 4982 | } |
| 4983 | |
| 4984 | /* This function creates ans returns an array of HTTP headers. |
| 4985 | * This function does not fails. It is used as wrapper with the |
| 4986 | * 2 following functions. |
| 4987 | */ |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 4988 | __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] | 4989 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4990 | struct htx *htx; |
| 4991 | int32_t pos; |
| 4992 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4993 | /* Create the table. */ |
| 4994 | lua_newtable(L); |
| 4995 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4996 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4997 | htx = htxbuf(&msg->chn->buf); |
| 4998 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 4999 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 5000 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 5001 | struct ist n, v; |
| 5002 | int len; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5003 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5004 | if (type == HTX_BLK_HDR) { |
| 5005 | n = htx_get_blk_name(htx,blk); |
| 5006 | v = htx_get_blk_value(htx, blk); |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5007 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5008 | else if (type == HTX_BLK_EOH) |
| 5009 | break; |
| 5010 | else |
| 5011 | continue; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5012 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5013 | /* Check for existing entry: |
| 5014 | * assume that the table is on the top of the stack, and |
| 5015 | * push the key in the stack, the function lua_gettable() |
| 5016 | * perform the lookup. |
| 5017 | */ |
| 5018 | lua_pushlstring(L, n.ptr, n.len); |
| 5019 | lua_gettable(L, -2); |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5020 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5021 | switch (lua_type(L, -1)) { |
| 5022 | case LUA_TNIL: |
| 5023 | /* Table not found, create it. */ |
| 5024 | lua_pop(L, 1); /* remove the nil value. */ |
| 5025 | lua_pushlstring(L, n.ptr, n.len); /* push the header name as key. */ |
| 5026 | lua_newtable(L); /* create and push empty table. */ |
| 5027 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 5028 | lua_rawseti(L, -2, 0); /* index header value (pop it). */ |
| 5029 | 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] | 5030 | break; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5031 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5032 | case LUA_TTABLE: |
| 5033 | /* Entry found: push the value in the table. */ |
| 5034 | len = lua_rawlen(L, -1); |
| 5035 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 5036 | lua_rawseti(L, -2, len+1); /* index header value (pop it). */ |
| 5037 | lua_pop(L, 1); /* remove the table (it is stored in the main table). */ |
| 5038 | break; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5039 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5040 | default: |
| 5041 | /* Other cases are errors. */ |
| 5042 | hlua_pusherror(L, "internal error during the parsing of headers."); |
| 5043 | WILL_LJMP(lua_error(L)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5044 | } |
| 5045 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5046 | return 1; |
| 5047 | } |
| 5048 | |
| 5049 | __LJMP static int hlua_http_req_get_headers(lua_State *L) |
| 5050 | { |
| 5051 | struct hlua_txn *htxn; |
| 5052 | |
| 5053 | MAY_LJMP(check_args(L, 1, "req_get_headers")); |
| 5054 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5055 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5056 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5057 | WILL_LJMP(lua_error(L)); |
| 5058 | |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 5059 | return hlua_http_get_headers(L, &htxn->s->txn->req); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5060 | } |
| 5061 | |
| 5062 | __LJMP static int hlua_http_res_get_headers(lua_State *L) |
| 5063 | { |
| 5064 | struct hlua_txn *htxn; |
| 5065 | |
| 5066 | MAY_LJMP(check_args(L, 1, "res_get_headers")); |
| 5067 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5068 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5069 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5070 | WILL_LJMP(lua_error(L)); |
| 5071 | |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 5072 | return hlua_http_get_headers(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5073 | } |
| 5074 | |
| 5075 | /* This function replace full header, or just a value in |
| 5076 | * the request or in the response. It is a wrapper fir the |
| 5077 | * 4 following functions. |
| 5078 | */ |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5079 | __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] | 5080 | { |
| 5081 | size_t name_len; |
| 5082 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5083 | const char *reg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 5084 | const char *value = MAY_LJMP(luaL_checkstring(L, 4)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5085 | struct htx *htx; |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 5086 | struct my_regex *re; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5087 | |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 5088 | if (!(re = regex_comp(reg, 1, 1, NULL))) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5089 | WILL_LJMP(luaL_argerror(L, 3, "invalid regex")); |
| 5090 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5091 | htx = htxbuf(&msg->chn->buf); |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5092 | 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] | 5093 | regex_free(re); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5094 | return 0; |
| 5095 | } |
| 5096 | |
| 5097 | __LJMP static int hlua_http_req_rep_hdr(lua_State *L) |
| 5098 | { |
| 5099 | struct hlua_txn *htxn; |
| 5100 | |
| 5101 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 5102 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5103 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5104 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5105 | WILL_LJMP(lua_error(L)); |
| 5106 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5107 | 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] | 5108 | } |
| 5109 | |
| 5110 | __LJMP static int hlua_http_res_rep_hdr(lua_State *L) |
| 5111 | { |
| 5112 | struct hlua_txn *htxn; |
| 5113 | |
| 5114 | MAY_LJMP(check_args(L, 4, "res_rep_hdr")); |
| 5115 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5116 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5117 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5118 | WILL_LJMP(lua_error(L)); |
| 5119 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5120 | 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] | 5121 | } |
| 5122 | |
| 5123 | __LJMP static int hlua_http_req_rep_val(lua_State *L) |
| 5124 | { |
| 5125 | struct hlua_txn *htxn; |
| 5126 | |
| 5127 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 5128 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5129 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5130 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5131 | WILL_LJMP(lua_error(L)); |
| 5132 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5133 | 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] | 5134 | } |
| 5135 | |
| 5136 | __LJMP static int hlua_http_res_rep_val(lua_State *L) |
| 5137 | { |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5138 | struct hlua_txn *htxn; |
| 5139 | |
| 5140 | MAY_LJMP(check_args(L, 4, "res_rep_val")); |
| 5141 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5142 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5143 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5144 | WILL_LJMP(lua_error(L)); |
| 5145 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5146 | 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] | 5147 | } |
| 5148 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5149 | /* This function deletes all the occurrences of an header. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5150 | * It is a wrapper for the 2 following functions. |
| 5151 | */ |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5152 | __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] | 5153 | { |
| 5154 | size_t len; |
| 5155 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5156 | struct htx *htx = htxbuf(&msg->chn->buf); |
| 5157 | struct http_hdr_ctx ctx; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5158 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5159 | ctx.blk = NULL; |
| 5160 | while (http_find_header(htx, ist2(name, len), &ctx, 1)) |
| 5161 | http_remove_header(htx, &ctx); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5162 | return 0; |
| 5163 | } |
| 5164 | |
| 5165 | __LJMP static int hlua_http_req_del_hdr(lua_State *L) |
| 5166 | { |
| 5167 | struct hlua_txn *htxn; |
| 5168 | |
| 5169 | MAY_LJMP(check_args(L, 2, "req_del_hdr")); |
| 5170 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5171 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5172 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5173 | WILL_LJMP(lua_error(L)); |
| 5174 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5175 | return hlua_http_del_hdr(L, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5176 | } |
| 5177 | |
| 5178 | __LJMP static int hlua_http_res_del_hdr(lua_State *L) |
| 5179 | { |
| 5180 | struct hlua_txn *htxn; |
| 5181 | |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5182 | MAY_LJMP(check_args(L, 2, "res_del_hdr")); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5183 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5184 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5185 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5186 | WILL_LJMP(lua_error(L)); |
| 5187 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5188 | return hlua_http_del_hdr(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5189 | } |
| 5190 | |
| 5191 | /* This function adds an header. It is a wrapper used by |
| 5192 | * the 2 following functions. |
| 5193 | */ |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5194 | __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] | 5195 | { |
| 5196 | size_t name_len; |
| 5197 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5198 | size_t value_len; |
| 5199 | const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5200 | struct htx *htx = htxbuf(&msg->chn->buf); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5201 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5202 | lua_pushboolean(L, http_add_header(htx, ist2(name, name_len), |
| 5203 | ist2(value, value_len))); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5204 | return 0; |
| 5205 | } |
| 5206 | |
| 5207 | __LJMP static int hlua_http_req_add_hdr(lua_State *L) |
| 5208 | { |
| 5209 | struct hlua_txn *htxn; |
| 5210 | |
| 5211 | MAY_LJMP(check_args(L, 3, "req_add_hdr")); |
| 5212 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5213 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5214 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5215 | WILL_LJMP(lua_error(L)); |
| 5216 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5217 | return hlua_http_add_hdr(L, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5218 | } |
| 5219 | |
| 5220 | __LJMP static int hlua_http_res_add_hdr(lua_State *L) |
| 5221 | { |
| 5222 | struct hlua_txn *htxn; |
| 5223 | |
| 5224 | MAY_LJMP(check_args(L, 3, "res_add_hdr")); |
| 5225 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5226 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5227 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5228 | WILL_LJMP(lua_error(L)); |
| 5229 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5230 | return hlua_http_add_hdr(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5231 | } |
| 5232 | |
| 5233 | static int hlua_http_req_set_hdr(lua_State *L) |
| 5234 | { |
| 5235 | struct hlua_txn *htxn; |
| 5236 | |
| 5237 | MAY_LJMP(check_args(L, 3, "req_set_hdr")); |
| 5238 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5239 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5240 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5241 | WILL_LJMP(lua_error(L)); |
| 5242 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5243 | hlua_http_del_hdr(L, &htxn->s->txn->req); |
| 5244 | return hlua_http_add_hdr(L, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5245 | } |
| 5246 | |
| 5247 | static int hlua_http_res_set_hdr(lua_State *L) |
| 5248 | { |
| 5249 | struct hlua_txn *htxn; |
| 5250 | |
| 5251 | MAY_LJMP(check_args(L, 3, "res_set_hdr")); |
| 5252 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5253 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5254 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5255 | WILL_LJMP(lua_error(L)); |
| 5256 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5257 | hlua_http_del_hdr(L, &htxn->s->txn->rsp); |
| 5258 | return hlua_http_add_hdr(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5259 | } |
| 5260 | |
| 5261 | /* This function set the method. */ |
| 5262 | static int hlua_http_req_set_meth(lua_State *L) |
| 5263 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5264 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5265 | size_t name_len; |
| 5266 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5267 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5268 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5269 | WILL_LJMP(lua_error(L)); |
| 5270 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5271 | 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] | 5272 | return 1; |
| 5273 | } |
| 5274 | |
| 5275 | /* This function set the method. */ |
| 5276 | static int hlua_http_req_set_path(lua_State *L) |
| 5277 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5278 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5279 | size_t name_len; |
| 5280 | 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] | 5281 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5282 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5283 | WILL_LJMP(lua_error(L)); |
| 5284 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5285 | 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] | 5286 | return 1; |
| 5287 | } |
| 5288 | |
| 5289 | /* This function set the query-string. */ |
| 5290 | static int hlua_http_req_set_query(lua_State *L) |
| 5291 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5292 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5293 | size_t name_len; |
| 5294 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5295 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5296 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5297 | WILL_LJMP(lua_error(L)); |
| 5298 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5299 | /* Check length. */ |
| 5300 | if (name_len > trash.size - 1) { |
| 5301 | lua_pushboolean(L, 0); |
| 5302 | return 1; |
| 5303 | } |
| 5304 | |
| 5305 | /* Add the mark question as prefix. */ |
| 5306 | chunk_reset(&trash); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5307 | trash.area[trash.data++] = '?'; |
| 5308 | memcpy(trash.area + trash.data, name, name_len); |
| 5309 | trash.data += name_len; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5310 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5311 | lua_pushboolean(L, |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5312 | 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] | 5313 | return 1; |
| 5314 | } |
| 5315 | |
| 5316 | /* This function set the uri. */ |
| 5317 | static int hlua_http_req_set_uri(lua_State *L) |
| 5318 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5319 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5320 | size_t name_len; |
| 5321 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5322 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5323 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5324 | WILL_LJMP(lua_error(L)); |
| 5325 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5326 | lua_pushboolean(L, http_req_replace_stline(3, name, name_len, htxn->p, htxn->s) != -1); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5327 | return 1; |
| 5328 | } |
| 5329 | |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5330 | /* This function set the response code & optionally reason. */ |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5331 | static int hlua_http_res_set_status(lua_State *L) |
| 5332 | { |
| 5333 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5334 | unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2)); |
Christopher Faulet | 96bff76 | 2019-12-17 13:46:18 +0100 | [diff] [blame] | 5335 | const char *str = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
| 5336 | const struct ist reason = ist2(str, (str ? strlen(str) : 0)); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5337 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5338 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5339 | WILL_LJMP(lua_error(L)); |
| 5340 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5341 | http_res_set_status(code, reason, htxn->s); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5342 | return 0; |
| 5343 | } |
| 5344 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5345 | /* |
| 5346 | * |
| 5347 | * |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5348 | * Class TXN |
| 5349 | * |
| 5350 | * |
| 5351 | */ |
| 5352 | |
| 5353 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5354 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5355 | */ |
| 5356 | __LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud) |
| 5357 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 5358 | return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref)); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5359 | } |
| 5360 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5361 | __LJMP static int hlua_set_var(lua_State *L) |
| 5362 | { |
| 5363 | struct hlua_txn *htxn; |
| 5364 | const char *name; |
| 5365 | size_t len; |
| 5366 | struct sample smp; |
| 5367 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 5368 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 5369 | 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] | 5370 | |
| 5371 | /* It is useles to retrieve the stream, but this function |
| 5372 | * runs only in a stream context. |
| 5373 | */ |
| 5374 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5375 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5376 | |
| 5377 | /* Converts the third argument in a sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 5378 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5379 | hlua_lua2smp(L, 3, &smp); |
| 5380 | |
| 5381 | /* Store the sample in a variable. */ |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 5382 | 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] | 5383 | |
| 5384 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 5385 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 5386 | else |
| 5387 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 5388 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 5389 | return 1; |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5390 | } |
| 5391 | |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 5392 | __LJMP static int hlua_unset_var(lua_State *L) |
| 5393 | { |
| 5394 | struct hlua_txn *htxn; |
| 5395 | const char *name; |
| 5396 | size_t len; |
| 5397 | struct sample smp; |
| 5398 | |
| 5399 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 5400 | |
| 5401 | /* It is useles to retrieve the stream, but this function |
| 5402 | * runs only in a stream context. |
| 5403 | */ |
| 5404 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5405 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5406 | |
| 5407 | /* Unset the variable. */ |
| 5408 | 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] | 5409 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 5410 | return 1; |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 5411 | } |
| 5412 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5413 | __LJMP static int hlua_get_var(lua_State *L) |
| 5414 | { |
| 5415 | struct hlua_txn *htxn; |
| 5416 | const char *name; |
| 5417 | size_t len; |
| 5418 | struct sample smp; |
| 5419 | |
| 5420 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 5421 | |
| 5422 | /* It is useles to retrieve the stream, but this function |
| 5423 | * runs only in a stream context. |
| 5424 | */ |
| 5425 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5426 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5427 | |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 5428 | 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] | 5429 | if (!vars_get_by_name(name, len, &smp)) { |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5430 | lua_pushnil(L); |
| 5431 | return 1; |
| 5432 | } |
| 5433 | |
| 5434 | return hlua_smp2lua(L, &smp); |
| 5435 | } |
| 5436 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5437 | __LJMP static int hlua_set_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5438 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5439 | struct hlua *hlua; |
| 5440 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5441 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 5442 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5443 | /* It is useles to retrieve the stream, but this function |
| 5444 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5445 | */ |
| 5446 | MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 5447 | |
| 5448 | /* Get hlua struct, or NULL if we execute from main lua state */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5449 | hlua = hlua_gethlua(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 5450 | if (!hlua) |
| 5451 | return 0; |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5452 | |
| 5453 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 5454 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5455 | |
| 5456 | /* Get and store new value. */ |
| 5457 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 5458 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 5459 | |
| 5460 | return 0; |
| 5461 | } |
| 5462 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5463 | __LJMP static int hlua_get_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5464 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5465 | struct hlua *hlua; |
| 5466 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5467 | MAY_LJMP(check_args(L, 1, "get_priv")); |
| 5468 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5469 | /* It is useles to retrieve the stream, but this function |
| 5470 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5471 | */ |
| 5472 | MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 5473 | |
| 5474 | /* Get hlua struct, or NULL if we execute from main lua state */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5475 | hlua = hlua_gethlua(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 5476 | if (!hlua) { |
| 5477 | lua_pushnil(L); |
| 5478 | return 1; |
| 5479 | } |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5480 | |
| 5481 | /* Push configuration index in the stack. */ |
| 5482 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 5483 | |
| 5484 | return 1; |
| 5485 | } |
| 5486 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5487 | /* Create stack entry containing a class TXN. This function |
| 5488 | * return 0 if the stack does not contains free slots, |
| 5489 | * otherwise it returns 1. |
| 5490 | */ |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5491 | 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] | 5492 | { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5493 | struct hlua_txn *htxn; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5494 | |
| 5495 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5496 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5497 | return 0; |
| 5498 | |
| 5499 | /* NOTE: The allocation never fails. The failure |
| 5500 | * throw an error, and the function never returns. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5501 | * if the throw is not available, the process is aborted. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5502 | */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5503 | /* Create the object: obj[0] = userdata. */ |
| 5504 | lua_newtable(L); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5505 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5506 | lua_rawseti(L, -2, 0); |
| 5507 | |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5508 | htxn->s = s; |
| 5509 | htxn->p = p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 5510 | htxn->dir = dir; |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5511 | htxn->flags = flags; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5512 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 5513 | /* Create the "f" field that contains a list of fetches. */ |
| 5514 | lua_pushstring(L, "f"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 5515 | if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5516 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5517 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5518 | |
| 5519 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 5520 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 5521 | 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] | 5522 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5523 | lua_rawset(L, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 5524 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5525 | /* Create the "c" field that contains a list of converters. */ |
| 5526 | lua_pushstring(L, "c"); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5527 | if (!hlua_converters_new(L, htxn, 0)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5528 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5529 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5530 | |
| 5531 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 5532 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 5533 | if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5534 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5535 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5536 | |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5537 | /* Create the "req" field that contains the request channel object. */ |
| 5538 | lua_pushstring(L, "req"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 5539 | if (!hlua_channel_new(L, &s->req)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +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 | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5542 | |
| 5543 | /* Create the "res" field that contains the response channel object. */ |
| 5544 | lua_pushstring(L, "res"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 5545 | if (!hlua_channel_new(L, &s->res)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +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 | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5548 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5549 | /* Creates the HTTP object is the current proxy allows http. */ |
| 5550 | lua_pushstring(L, "http"); |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 5551 | if (IS_HTX_STRM(s)) { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5552 | if (!hlua_http_new(L, htxn)) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5553 | return 0; |
| 5554 | } |
| 5555 | else |
| 5556 | lua_pushnil(L); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5557 | lua_rawset(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5558 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5559 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 5560 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref); |
| 5561 | lua_setmetatable(L, -2); |
| 5562 | |
| 5563 | return 1; |
| 5564 | } |
| 5565 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 5566 | __LJMP static int hlua_txn_deflog(lua_State *L) |
| 5567 | { |
| 5568 | const char *msg; |
| 5569 | struct hlua_txn *htxn; |
| 5570 | |
| 5571 | MAY_LJMP(check_args(L, 2, "deflog")); |
| 5572 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5573 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5574 | |
| 5575 | hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg); |
| 5576 | return 0; |
| 5577 | } |
| 5578 | |
| 5579 | __LJMP static int hlua_txn_log(lua_State *L) |
| 5580 | { |
| 5581 | int level; |
| 5582 | const char *msg; |
| 5583 | struct hlua_txn *htxn; |
| 5584 | |
| 5585 | MAY_LJMP(check_args(L, 3, "log")); |
| 5586 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5587 | level = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5588 | msg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 5589 | |
| 5590 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 5591 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 5592 | |
| 5593 | hlua_sendlog(htxn->s->be, level, msg); |
| 5594 | return 0; |
| 5595 | } |
| 5596 | |
| 5597 | __LJMP static int hlua_txn_log_debug(lua_State *L) |
| 5598 | { |
| 5599 | const char *msg; |
| 5600 | struct hlua_txn *htxn; |
| 5601 | |
| 5602 | MAY_LJMP(check_args(L, 2, "Debug")); |
| 5603 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5604 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5605 | hlua_sendlog(htxn->s->be, LOG_DEBUG, msg); |
| 5606 | return 0; |
| 5607 | } |
| 5608 | |
| 5609 | __LJMP static int hlua_txn_log_info(lua_State *L) |
| 5610 | { |
| 5611 | const char *msg; |
| 5612 | struct hlua_txn *htxn; |
| 5613 | |
| 5614 | MAY_LJMP(check_args(L, 2, "Info")); |
| 5615 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5616 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5617 | hlua_sendlog(htxn->s->be, LOG_INFO, msg); |
| 5618 | return 0; |
| 5619 | } |
| 5620 | |
| 5621 | __LJMP static int hlua_txn_log_warning(lua_State *L) |
| 5622 | { |
| 5623 | const char *msg; |
| 5624 | struct hlua_txn *htxn; |
| 5625 | |
| 5626 | MAY_LJMP(check_args(L, 2, "Warning")); |
| 5627 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5628 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5629 | hlua_sendlog(htxn->s->be, LOG_WARNING, msg); |
| 5630 | return 0; |
| 5631 | } |
| 5632 | |
| 5633 | __LJMP static int hlua_txn_log_alert(lua_State *L) |
| 5634 | { |
| 5635 | const char *msg; |
| 5636 | struct hlua_txn *htxn; |
| 5637 | |
| 5638 | MAY_LJMP(check_args(L, 2, "Alert")); |
| 5639 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5640 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5641 | hlua_sendlog(htxn->s->be, LOG_ALERT, msg); |
| 5642 | return 0; |
| 5643 | } |
| 5644 | |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5645 | __LJMP static int hlua_txn_set_loglevel(lua_State *L) |
| 5646 | { |
| 5647 | struct hlua_txn *htxn; |
| 5648 | int ll; |
| 5649 | |
| 5650 | MAY_LJMP(check_args(L, 2, "set_loglevel")); |
| 5651 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5652 | ll = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5653 | |
| 5654 | if (ll < 0 || ll > 7) |
| 5655 | WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7")); |
| 5656 | |
| 5657 | htxn->s->logs.level = ll; |
| 5658 | return 0; |
| 5659 | } |
| 5660 | |
| 5661 | __LJMP static int hlua_txn_set_tos(lua_State *L) |
| 5662 | { |
| 5663 | struct hlua_txn *htxn; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5664 | int tos; |
| 5665 | |
| 5666 | MAY_LJMP(check_args(L, 2, "set_tos")); |
| 5667 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5668 | tos = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5669 | |
Willy Tarreau | 1a18b54 | 2018-12-11 16:37:42 +0100 | [diff] [blame] | 5670 | conn_set_tos(objt_conn(htxn->s->sess->origin), tos); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5671 | return 0; |
| 5672 | } |
| 5673 | |
| 5674 | __LJMP static int hlua_txn_set_mark(lua_State *L) |
| 5675 | { |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5676 | struct hlua_txn *htxn; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5677 | int mark; |
| 5678 | |
| 5679 | MAY_LJMP(check_args(L, 2, "set_mark")); |
| 5680 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5681 | mark = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5682 | |
Lukas Tribus | 579e3e3 | 2019-08-11 18:03:45 +0200 | [diff] [blame] | 5683 | conn_set_mark(objt_conn(htxn->s->sess->origin), mark); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5684 | return 0; |
| 5685 | } |
| 5686 | |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 5687 | __LJMP static int hlua_txn_set_priority_class(lua_State *L) |
| 5688 | { |
| 5689 | struct hlua_txn *htxn; |
| 5690 | |
| 5691 | MAY_LJMP(check_args(L, 2, "set_priority_class")); |
| 5692 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5693 | htxn->s->priority_class = queue_limit_class(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 5694 | return 0; |
| 5695 | } |
| 5696 | |
| 5697 | __LJMP static int hlua_txn_set_priority_offset(lua_State *L) |
| 5698 | { |
| 5699 | struct hlua_txn *htxn; |
| 5700 | |
| 5701 | MAY_LJMP(check_args(L, 2, "set_priority_offset")); |
| 5702 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5703 | htxn->s->priority_offset = queue_limit_offset(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 5704 | return 0; |
| 5705 | } |
| 5706 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5707 | /* 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] | 5708 | * 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] | 5709 | * message and terminate the transaction. It returns 1 on success and 0 on |
| 5710 | * error. The Reply must be on top of the stack. |
| 5711 | */ |
| 5712 | __LJMP static int hlua_txn_forward_reply(lua_State *L, struct stream *s) |
| 5713 | { |
| 5714 | struct htx *htx; |
| 5715 | struct htx_sl *sl; |
| 5716 | struct h1m h1m; |
| 5717 | const char *status, *reason, *body; |
| 5718 | size_t status_len, reason_len, body_len; |
| 5719 | int ret, code, flags; |
| 5720 | |
| 5721 | code = 200; |
| 5722 | status = "200"; |
| 5723 | status_len = 3; |
| 5724 | ret = lua_getfield(L, -1, "status"); |
| 5725 | if (ret == LUA_TNUMBER) { |
| 5726 | code = lua_tointeger(L, -1); |
| 5727 | status = lua_tolstring(L, -1, &status_len); |
| 5728 | } |
| 5729 | lua_pop(L, 1); |
| 5730 | |
| 5731 | reason = http_get_reason(code); |
| 5732 | reason_len = strlen(reason); |
| 5733 | ret = lua_getfield(L, -1, "reason"); |
| 5734 | if (ret == LUA_TSTRING) |
| 5735 | reason = lua_tolstring(L, -1, &reason_len); |
| 5736 | lua_pop(L, 1); |
| 5737 | |
| 5738 | body = NULL; |
| 5739 | body_len = 0; |
| 5740 | ret = lua_getfield(L, -1, "body"); |
| 5741 | if (ret == LUA_TSTRING) |
| 5742 | body = lua_tolstring(L, -1, &body_len); |
| 5743 | lua_pop(L, 1); |
| 5744 | |
| 5745 | /* Prepare the response before inserting the headers */ |
| 5746 | h1m_init_res(&h1m); |
| 5747 | htx = htx_from_buf(&s->res.buf); |
| 5748 | channel_htx_truncate(&s->res, htx); |
| 5749 | if (s->txn->req.flags & HTTP_MSGF_VER_11) { |
| 5750 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11); |
| 5751 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), |
| 5752 | ist2(status, status_len), ist2(reason, reason_len)); |
| 5753 | } |
| 5754 | else { |
| 5755 | flags = HTX_SL_F_IS_RESP; |
| 5756 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), |
| 5757 | ist2(status, status_len), ist2(reason, reason_len)); |
| 5758 | } |
| 5759 | if (!sl) |
| 5760 | goto fail; |
| 5761 | sl->info.res.status = code; |
| 5762 | |
| 5763 | /* Push in the stack the "headers" entry. */ |
| 5764 | ret = lua_getfield(L, -1, "headers"); |
| 5765 | if (ret != LUA_TTABLE) |
| 5766 | goto skip_headers; |
| 5767 | |
| 5768 | lua_pushnil(L); |
| 5769 | while (lua_next(L, -2) != 0) { |
| 5770 | struct ist name, value; |
| 5771 | const char *n, *v; |
| 5772 | size_t nlen, vlen; |
| 5773 | |
| 5774 | if (!lua_isstring(L, -2) || !lua_istable(L, -1)) { |
| 5775 | /* Skip element if the key is not a string or if the value is not a table */ |
| 5776 | goto next_hdr; |
| 5777 | } |
| 5778 | |
| 5779 | n = lua_tolstring(L, -2, &nlen); |
| 5780 | name = ist2(n, nlen); |
| 5781 | if (isteqi(name, ist("content-length"))) { |
| 5782 | /* Always skip content-length header. It will be added |
| 5783 | * later with the correct len |
| 5784 | */ |
| 5785 | goto next_hdr; |
| 5786 | } |
| 5787 | |
| 5788 | /* Loop on header's values */ |
| 5789 | lua_pushnil(L); |
| 5790 | while (lua_next(L, -2)) { |
| 5791 | if (!lua_isstring(L, -1)) { |
| 5792 | /* Skip the value if it is not a string */ |
| 5793 | goto next_value; |
| 5794 | } |
| 5795 | |
| 5796 | v = lua_tolstring(L, -1, &vlen); |
| 5797 | value = ist2(v, vlen); |
| 5798 | |
| 5799 | if (isteqi(name, ist("transfer-encoding"))) |
| 5800 | h1_parse_xfer_enc_header(&h1m, value); |
| 5801 | if (!htx_add_header(htx, ist2(n, nlen), ist2(v, vlen))) |
| 5802 | goto fail; |
| 5803 | |
| 5804 | next_value: |
| 5805 | lua_pop(L, 1); |
| 5806 | } |
| 5807 | |
| 5808 | next_hdr: |
| 5809 | lua_pop(L, 1); |
| 5810 | } |
| 5811 | skip_headers: |
| 5812 | lua_pop(L, 1); |
| 5813 | |
| 5814 | /* Update h1m flags: CLEN is set if CHNK is not present */ |
| 5815 | if (!(h1m.flags & H1_MF_CHNK)) { |
| 5816 | const char *clen = ultoa(body_len); |
| 5817 | |
| 5818 | h1m.flags |= H1_MF_CLEN; |
| 5819 | if (!htx_add_header(htx, ist("content-length"), ist(clen))) |
| 5820 | goto fail; |
| 5821 | } |
| 5822 | if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK)) |
| 5823 | h1m.flags |= H1_MF_XFER_LEN; |
| 5824 | |
| 5825 | /* Update HTX start-line flags */ |
| 5826 | if (h1m.flags & H1_MF_XFER_ENC) |
| 5827 | flags |= HTX_SL_F_XFER_ENC; |
| 5828 | if (h1m.flags & H1_MF_XFER_LEN) { |
| 5829 | flags |= HTX_SL_F_XFER_LEN; |
| 5830 | if (h1m.flags & H1_MF_CHNK) |
| 5831 | flags |= HTX_SL_F_CHNK; |
| 5832 | else if (h1m.flags & H1_MF_CLEN) |
| 5833 | flags |= HTX_SL_F_CLEN; |
| 5834 | if (h1m.body_len == 0) |
| 5835 | flags |= HTX_SL_F_BODYLESS; |
| 5836 | } |
| 5837 | sl->flags |= flags; |
| 5838 | |
| 5839 | |
| 5840 | if (!htx_add_endof(htx, HTX_BLK_EOH) || |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 5841 | (body_len && !htx_add_data_atonce(htx, ist2(body, body_len)))) |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5842 | goto fail; |
| 5843 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 5844 | htx->flags |= HTX_FL_EOM; |
| 5845 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5846 | /* Now, forward the response and terminate the transaction */ |
| 5847 | s->txn->status = code; |
| 5848 | htx_to_buf(htx, &s->res.buf); |
| 5849 | if (!http_forward_proxy_resp(s, 1)) |
| 5850 | goto fail; |
| 5851 | |
| 5852 | return 1; |
| 5853 | |
| 5854 | fail: |
| 5855 | channel_htx_truncate(&s->res, htx); |
| 5856 | return 0; |
| 5857 | } |
| 5858 | |
| 5859 | /* Terminate a transaction if called from a lua action. For TCP streams, |
| 5860 | * processing is just aborted. Nothing is returned to the client and all |
| 5861 | * arguments are ignored. For HTTP streams, if a reply is passed as argument, it |
| 5862 | * is forwarded to the client before terminating the transaction. On success, |
| 5863 | * the function exits with ACT_RET_DONE code. If an error occurred, it exits |
| 5864 | * with ACT_RET_ERR code. If this function is not called from a lua action, it |
| 5865 | * just exits without any processing. |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5866 | */ |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 5867 | __LJMP static int hlua_txn_done(lua_State *L) |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5868 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 5869 | struct hlua_txn *htxn; |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5870 | struct stream *s; |
| 5871 | int finst; |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5872 | |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 5873 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5874 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5875 | /* If the flags NOTERM is set, we cannot terminate the session, so we |
| 5876 | * just end the execution of the current lua code. */ |
| 5877 | if (htxn->flags & HLUA_TXN_NOTERM) |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5878 | WILL_LJMP(hlua_done(L)); |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5879 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5880 | s = htxn->s; |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5881 | if (!IS_HTX_STRM(htxn->s)) { |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5882 | struct channel *req = &s->req; |
| 5883 | struct channel *res = &s->res; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 5884 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5885 | channel_auto_read(req); |
| 5886 | channel_abort(req); |
| 5887 | channel_auto_close(req); |
| 5888 | channel_erase(req); |
| 5889 | |
| 5890 | res->wex = tick_add_ifset(now_ms, res->wto); |
| 5891 | channel_auto_read(res); |
| 5892 | channel_auto_close(res); |
| 5893 | channel_shutr_now(res); |
| 5894 | |
| 5895 | finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_D); |
| 5896 | goto done; |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 5897 | } |
Thierry FOURNIER | 10ec214 | 2015-08-24 17:23:45 +0200 | [diff] [blame] | 5898 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5899 | if (lua_gettop(L) == 1 || !lua_istable(L, 2)) { |
| 5900 | /* No reply or invalid reply */ |
| 5901 | s->txn->status = 0; |
| 5902 | http_reply_and_close(s, 0, NULL); |
| 5903 | } |
| 5904 | else { |
| 5905 | /* Remove extra args to have the reply on top of the stack */ |
| 5906 | if (lua_gettop(L) > 2) |
| 5907 | lua_pop(L, lua_gettop(L) - 2); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5908 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5909 | if (!hlua_txn_forward_reply(L, s)) { |
| 5910 | if (!(s->flags & SF_ERR_MASK)) |
| 5911 | s->flags |= SF_ERR_PRXCOND; |
| 5912 | lua_pushinteger(L, ACT_RET_ERR); |
| 5913 | WILL_LJMP(hlua_done(L)); |
| 5914 | return 0; /* Never reached */ |
| 5915 | } |
Christopher Faulet | 4d0e263 | 2019-07-16 10:52:40 +0200 | [diff] [blame] | 5916 | } |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 5917 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5918 | finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_H); |
| 5919 | if (htxn->dir == SMP_OPT_DIR_REQ) { |
| 5920 | /* let's log the request time */ |
| 5921 | s->logs.tv_request = now; |
| 5922 | 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] | 5923 | _HA_ATOMIC_INC(&s->sess->fe->fe_counters.intercepted_req); |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5924 | } |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 5925 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5926 | done: |
| 5927 | if (!(s->flags & SF_ERR_MASK)) |
| 5928 | s->flags |= SF_ERR_LOCAL; |
| 5929 | if (!(s->flags & SF_FINST_MASK)) |
| 5930 | s->flags |= finst; |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 5931 | |
Christopher Faulet | 4ad7310 | 2020-03-05 11:07:31 +0100 | [diff] [blame] | 5932 | lua_pushinteger(L, ACT_RET_ABRT); |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 5933 | WILL_LJMP(hlua_done(L)); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 5934 | return 0; |
| 5935 | } |
| 5936 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5937 | /* |
| 5938 | * |
| 5939 | * |
| 5940 | * Class REPLY |
| 5941 | * |
| 5942 | * |
| 5943 | */ |
| 5944 | |
| 5945 | /* Pushes the TXN reply onto the top of the stack. If the stask does not have a |
| 5946 | * free slots, the function fails and returns 0; |
| 5947 | */ |
| 5948 | static int hlua_txn_reply_new(lua_State *L) |
| 5949 | { |
| 5950 | struct hlua_txn *htxn; |
| 5951 | const char *reason, *body = NULL; |
| 5952 | int ret, status; |
| 5953 | |
| 5954 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5955 | if (!IS_HTX_STRM(htxn->s)) { |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5956 | hlua_pusherror(L, "txn object is not an HTTP transaction."); |
| 5957 | WILL_LJMP(lua_error(L)); |
| 5958 | } |
| 5959 | |
| 5960 | /* Default value */ |
| 5961 | status = 200; |
| 5962 | reason = http_get_reason(status); |
| 5963 | |
| 5964 | if (lua_istable(L, 2)) { |
| 5965 | /* load status and reason from the table argument at index 2 */ |
| 5966 | ret = lua_getfield(L, 2, "status"); |
| 5967 | if (ret == LUA_TNIL) |
| 5968 | goto reason; |
| 5969 | else if (ret != LUA_TNUMBER) { |
| 5970 | /* invalid status: ignore the reason */ |
| 5971 | goto body; |
| 5972 | } |
| 5973 | status = lua_tointeger(L, -1); |
| 5974 | |
| 5975 | reason: |
| 5976 | lua_pop(L, 1); /* restore the stack: remove status */ |
| 5977 | ret = lua_getfield(L, 2, "reason"); |
| 5978 | if (ret == LUA_TSTRING) |
| 5979 | reason = lua_tostring(L, -1); |
| 5980 | |
| 5981 | body: |
| 5982 | lua_pop(L, 1); /* restore the stack: remove invalid status or reason */ |
| 5983 | ret = lua_getfield(L, 2, "body"); |
| 5984 | if (ret == LUA_TSTRING) |
| 5985 | body = lua_tostring(L, -1); |
| 5986 | lua_pop(L, 1); /* restore the stack: remove body */ |
| 5987 | } |
| 5988 | |
| 5989 | /* Create the Reply table */ |
| 5990 | lua_newtable(L); |
| 5991 | |
| 5992 | /* Add status element */ |
| 5993 | lua_pushstring(L, "status"); |
| 5994 | lua_pushinteger(L, status); |
| 5995 | lua_settable(L, -3); |
| 5996 | |
| 5997 | /* Add reason element */ |
| 5998 | reason = http_get_reason(status); |
| 5999 | lua_pushstring(L, "reason"); |
| 6000 | lua_pushstring(L, reason); |
| 6001 | lua_settable(L, -3); |
| 6002 | |
| 6003 | /* Add body element, nil if undefined */ |
| 6004 | lua_pushstring(L, "body"); |
| 6005 | if (body) |
| 6006 | lua_pushstring(L, body); |
| 6007 | else |
| 6008 | lua_pushnil(L); |
| 6009 | lua_settable(L, -3); |
| 6010 | |
| 6011 | /* Add headers element */ |
| 6012 | lua_pushstring(L, "headers"); |
| 6013 | lua_newtable(L); |
| 6014 | |
| 6015 | /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */ |
| 6016 | if (lua_istable(L, 2)) { |
| 6017 | /* load headers from the table argument at index 2. If it is a table, copy it. */ |
| 6018 | ret = lua_getfield(L, 2, "headers"); |
| 6019 | if (ret == LUA_TTABLE) { |
| 6020 | /* stack: [ ... <headers:table>, <table> ] */ |
| 6021 | lua_pushnil(L); |
| 6022 | while (lua_next(L, -2) != 0) { |
| 6023 | /* stack: [ ... <headers:table>, <table>, k, v] */ |
| 6024 | if (!lua_isstring(L, -1) && !lua_istable(L, -1)) { |
| 6025 | /* invalid value type, skip it */ |
| 6026 | lua_pop(L, 1); |
| 6027 | continue; |
| 6028 | } |
| 6029 | |
| 6030 | |
| 6031 | /* Duplicate the key and swap it with the value. */ |
| 6032 | lua_pushvalue(L, -2); |
| 6033 | lua_insert(L, -2); |
| 6034 | /* stack: [ ... <headers:table>, <table>, k, k, v ] */ |
| 6035 | |
| 6036 | lua_newtable(L); |
| 6037 | lua_insert(L, -2); |
| 6038 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, v ] */ |
| 6039 | |
| 6040 | if (lua_isstring(L, -1)) { |
| 6041 | /* push the value in the inner table */ |
| 6042 | lua_rawseti(L, -2, 1); |
| 6043 | } |
| 6044 | else { /* table */ |
| 6045 | lua_pushnil(L); |
| 6046 | while (lua_next(L, -2) != 0) { |
| 6047 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2, v2 ] */ |
| 6048 | if (!lua_isstring(L, -1)) { |
| 6049 | /* invalid value type, skip it*/ |
| 6050 | lua_pop(L, 1); |
| 6051 | continue; |
| 6052 | } |
| 6053 | /* push the value in the inner table */ |
| 6054 | lua_rawseti(L, -4, lua_rawlen(L, -4) + 1); |
| 6055 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2 ] */ |
| 6056 | } |
| 6057 | lua_pop(L, 1); |
| 6058 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table> ] */ |
| 6059 | } |
| 6060 | |
| 6061 | /* push (k,v) on the stack in the headers table: |
| 6062 | * stack: [ ... <headers:table>, <table>, k, k, v ] |
| 6063 | */ |
| 6064 | lua_settable(L, -5); |
| 6065 | /* stack: [ ... <headers:table>, <table>, k ] */ |
| 6066 | } |
| 6067 | } |
| 6068 | lua_pop(L, 1); |
| 6069 | } |
| 6070 | /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */ |
| 6071 | lua_settable(L, -3); |
| 6072 | /* stack: [ txn, <Arg:table>, <Reply:table> ] */ |
| 6073 | |
| 6074 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 6075 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_reply_ref); |
| 6076 | lua_setmetatable(L, -2); |
| 6077 | return 1; |
| 6078 | } |
| 6079 | |
| 6080 | /* Set the reply status code, and optionally the reason. If no reason is |
| 6081 | * provided, the default one corresponding to the status code is used. |
| 6082 | */ |
| 6083 | __LJMP static int hlua_txn_reply_set_status(lua_State *L) |
| 6084 | { |
| 6085 | int status = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 6086 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
| 6087 | |
| 6088 | /* First argument (self) must be a table */ |
| 6089 | luaL_checktype(L, 1, LUA_TTABLE); |
| 6090 | |
| 6091 | if (status < 100 || status > 599) { |
| 6092 | lua_pushboolean(L, 0); |
| 6093 | return 1; |
| 6094 | } |
| 6095 | if (!reason) |
| 6096 | reason = http_get_reason(status); |
| 6097 | |
| 6098 | lua_pushinteger(L, status); |
| 6099 | lua_setfield(L, 1, "status"); |
| 6100 | |
| 6101 | lua_pushstring(L, reason); |
| 6102 | lua_setfield(L, 1, "reason"); |
| 6103 | |
| 6104 | lua_pushboolean(L, 1); |
| 6105 | return 1; |
| 6106 | } |
| 6107 | |
| 6108 | /* Add a header into the reply object. Each header name is associated to an |
| 6109 | * array of values in the "headers" table. If the header name is not found, a |
| 6110 | * new entry is created. |
| 6111 | */ |
| 6112 | __LJMP static int hlua_txn_reply_add_header(lua_State *L) |
| 6113 | { |
| 6114 | const char *name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 6115 | const char *value = MAY_LJMP(luaL_checkstring(L, 3)); |
| 6116 | int ret; |
| 6117 | |
| 6118 | /* First argument (self) must be a table */ |
| 6119 | luaL_checktype(L, 1, LUA_TTABLE); |
| 6120 | |
| 6121 | /* Push in the stack the "headers" entry. */ |
| 6122 | ret = lua_getfield(L, 1, "headers"); |
| 6123 | if (ret != LUA_TTABLE) { |
| 6124 | hlua_pusherror(L, "Reply['headers'] is expected to a an array. %s found", lua_typename(L, ret)); |
| 6125 | WILL_LJMP(lua_error(L)); |
| 6126 | } |
| 6127 | |
| 6128 | /* check if the header is already registered. If not, register it. */ |
| 6129 | ret = lua_getfield(L, -1, name); |
| 6130 | if (ret == LUA_TNIL) { |
| 6131 | /* Entry not found. */ |
| 6132 | lua_pop(L, 1); /* remove the nil. The "headers" table is the top of the stack. */ |
| 6133 | |
| 6134 | /* Insert the new header name in the array in the top of the stack. |
| 6135 | * It left the new array in the top of the stack. |
| 6136 | */ |
| 6137 | lua_newtable(L); |
| 6138 | lua_pushstring(L, name); |
| 6139 | lua_pushvalue(L, -2); |
| 6140 | lua_settable(L, -4); |
| 6141 | } |
| 6142 | else if (ret != LUA_TTABLE) { |
| 6143 | hlua_pusherror(L, "Reply['headers']['%s'] is expected to be an array. %s found", name, lua_typename(L, ret)); |
| 6144 | WILL_LJMP(lua_error(L)); |
| 6145 | } |
| 6146 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 6147 | /* Now the top of thestack is an array of values. We push |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 6148 | * the header value as new entry. |
| 6149 | */ |
| 6150 | lua_pushstring(L, value); |
| 6151 | ret = lua_rawlen(L, -2); |
| 6152 | lua_rawseti(L, -2, ret + 1); |
| 6153 | |
| 6154 | lua_pushboolean(L, 1); |
| 6155 | return 1; |
| 6156 | } |
| 6157 | |
| 6158 | /* Remove all occurrences of a given header name. */ |
| 6159 | __LJMP static int hlua_txn_reply_del_header(lua_State *L) |
| 6160 | { |
| 6161 | const char *name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 6162 | int ret; |
| 6163 | |
| 6164 | /* First argument (self) must be a table */ |
| 6165 | luaL_checktype(L, 1, LUA_TTABLE); |
| 6166 | |
| 6167 | /* Push in the stack the "headers" entry. */ |
| 6168 | ret = lua_getfield(L, 1, "headers"); |
| 6169 | if (ret != LUA_TTABLE) { |
| 6170 | hlua_pusherror(L, "Reply['headers'] is expected to be an array. %s found", lua_typename(L, ret)); |
| 6171 | WILL_LJMP(lua_error(L)); |
| 6172 | } |
| 6173 | |
| 6174 | lua_pushstring(L, name); |
| 6175 | lua_pushnil(L); |
| 6176 | lua_settable(L, -3); |
| 6177 | |
| 6178 | lua_pushboolean(L, 1); |
| 6179 | return 1; |
| 6180 | } |
| 6181 | |
| 6182 | /* Set the reply's body. Overwrite any existing entry. */ |
| 6183 | __LJMP static int hlua_txn_reply_set_body(lua_State *L) |
| 6184 | { |
| 6185 | const char *payload = MAY_LJMP(luaL_checkstring(L, 2)); |
| 6186 | |
| 6187 | /* First argument (self) must be a table */ |
| 6188 | luaL_checktype(L, 1, LUA_TTABLE); |
| 6189 | |
| 6190 | lua_pushstring(L, payload); |
| 6191 | lua_setfield(L, 1, "body"); |
| 6192 | |
| 6193 | lua_pushboolean(L, 1); |
| 6194 | return 1; |
| 6195 | } |
| 6196 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 6197 | __LJMP static int hlua_log(lua_State *L) |
| 6198 | { |
| 6199 | int level; |
| 6200 | const char *msg; |
| 6201 | |
| 6202 | MAY_LJMP(check_args(L, 2, "log")); |
| 6203 | level = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 6204 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 6205 | |
| 6206 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 6207 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 6208 | |
| 6209 | hlua_sendlog(NULL, level, msg); |
| 6210 | return 0; |
| 6211 | } |
| 6212 | |
| 6213 | __LJMP static int hlua_log_debug(lua_State *L) |
| 6214 | { |
| 6215 | const char *msg; |
| 6216 | |
| 6217 | MAY_LJMP(check_args(L, 1, "debug")); |
| 6218 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6219 | hlua_sendlog(NULL, LOG_DEBUG, msg); |
| 6220 | return 0; |
| 6221 | } |
| 6222 | |
| 6223 | __LJMP static int hlua_log_info(lua_State *L) |
| 6224 | { |
| 6225 | const char *msg; |
| 6226 | |
| 6227 | MAY_LJMP(check_args(L, 1, "info")); |
| 6228 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6229 | hlua_sendlog(NULL, LOG_INFO, msg); |
| 6230 | return 0; |
| 6231 | } |
| 6232 | |
| 6233 | __LJMP static int hlua_log_warning(lua_State *L) |
| 6234 | { |
| 6235 | const char *msg; |
| 6236 | |
| 6237 | MAY_LJMP(check_args(L, 1, "warning")); |
| 6238 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6239 | hlua_sendlog(NULL, LOG_WARNING, msg); |
| 6240 | return 0; |
| 6241 | } |
| 6242 | |
| 6243 | __LJMP static int hlua_log_alert(lua_State *L) |
| 6244 | { |
| 6245 | const char *msg; |
| 6246 | |
| 6247 | MAY_LJMP(check_args(L, 1, "alert")); |
| 6248 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6249 | hlua_sendlog(NULL, LOG_ALERT, msg); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 6250 | return 0; |
| 6251 | } |
| 6252 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 6253 | __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] | 6254 | { |
| 6255 | int wakeup_ms = lua_tointeger(L, -1); |
| 6256 | if (now_ms < wakeup_ms) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 6257 | 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] | 6258 | return 0; |
| 6259 | } |
| 6260 | |
| 6261 | __LJMP static int hlua_sleep(lua_State *L) |
| 6262 | { |
| 6263 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6264 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6265 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6266 | MAY_LJMP(check_args(L, 1, "sleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6267 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 6268 | delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6269 | wakeup_ms = tick_add(now_ms, delay); |
| 6270 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6271 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 6272 | 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] | 6273 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6274 | } |
| 6275 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6276 | __LJMP static int hlua_msleep(lua_State *L) |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6277 | { |
| 6278 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6279 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6280 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6281 | MAY_LJMP(check_args(L, 1, "msleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6282 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 6283 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6284 | wakeup_ms = tick_add(now_ms, delay); |
| 6285 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6286 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 6287 | 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] | 6288 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6289 | } |
| 6290 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 6291 | /* This functionis an LUA binding. it permits to give back |
| 6292 | * the hand at the HAProxy scheduler. It is used when the |
| 6293 | * LUA processing consumes a lot of time. |
| 6294 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 6295 | __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] | 6296 | { |
| 6297 | return 0; |
| 6298 | } |
| 6299 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 6300 | __LJMP static int hlua_yield(lua_State *L) |
| 6301 | { |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 6302 | 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] | 6303 | return 0; |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 6304 | } |
| 6305 | |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6306 | /* This function change the nice of the currently executed |
| 6307 | * task. It is used set low or high priority at the current |
| 6308 | * task. |
| 6309 | */ |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 6310 | __LJMP static int hlua_set_nice(lua_State *L) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6311 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6312 | struct hlua *hlua; |
| 6313 | int nice; |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6314 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6315 | MAY_LJMP(check_args(L, 1, "set_nice")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6316 | nice = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6317 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 6318 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 6319 | hlua = hlua_gethlua(L); |
| 6320 | |
| 6321 | /* If the task is not set, I'm in a start mode. */ |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6322 | if (!hlua || !hlua->task) |
| 6323 | return 0; |
| 6324 | |
| 6325 | if (nice < -1024) |
| 6326 | nice = -1024; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6327 | else if (nice > 1024) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6328 | nice = 1024; |
| 6329 | |
| 6330 | hlua->task->nice = nice; |
| 6331 | return 0; |
| 6332 | } |
| 6333 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6334 | /* 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] | 6335 | * HAProxy task subsystem when the task is awaked. The LUA runtime can |
| 6336 | * return an E_AGAIN signal, the emmiter of this signal must set a |
| 6337 | * signal to wake the task. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6338 | * |
| 6339 | * Task wrapper are longjmp safe because the only one Lua code |
| 6340 | * executed is the safe hlua_ctx_resume(); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6341 | */ |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 6342 | 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] | 6343 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 6344 | struct hlua *hlua = context; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6345 | enum hlua_exec status; |
| 6346 | |
Christopher Faulet | 5bc9972 | 2018-04-25 10:34:45 +0200 | [diff] [blame] | 6347 | if (task->thread_mask == MAX_THREADS_MASK) |
| 6348 | task_set_affinity(task, tid_bit); |
| 6349 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6350 | /* If it is the first call to the task, we must initialize the |
| 6351 | * execution timeouts. |
| 6352 | */ |
| 6353 | if (!HLUA_IS_RUNNING(hlua)) |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 6354 | hlua->max_time = hlua_timeout_task; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6355 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6356 | /* Execute the Lua code. */ |
| 6357 | status = hlua_ctx_resume(hlua, 1); |
| 6358 | |
| 6359 | switch (status) { |
| 6360 | /* finished or yield */ |
| 6361 | case HLUA_E_OK: |
| 6362 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 6363 | task_destroy(task); |
Tim Duesterhus | cd235c6 | 2018-04-24 13:56:01 +0200 | [diff] [blame] | 6364 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6365 | break; |
| 6366 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6367 | case HLUA_E_AGAIN: /* co process or timeout wake me later. */ |
Thierry FOURNIER | cb14688 | 2017-12-10 17:10:57 +0100 | [diff] [blame] | 6368 | notification_gc(&hlua->com); |
PiBa-NL | fe971b3 | 2018-05-02 22:27:14 +0200 | [diff] [blame] | 6369 | task->expire = hlua->wake_time; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6370 | break; |
| 6371 | |
| 6372 | /* finished with error. */ |
| 6373 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6374 | SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1)); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6375 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 6376 | task_destroy(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 6377 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6378 | break; |
| 6379 | |
| 6380 | case HLUA_E_ERR: |
| 6381 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6382 | SEND_ERR(NULL, "Lua task: unknown error.\n"); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6383 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 6384 | task_destroy(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 6385 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6386 | break; |
| 6387 | } |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 6388 | return task; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6389 | } |
| 6390 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6391 | /* This function is an LUA binding that register LUA function to be |
| 6392 | * executed after the HAProxy configuration parsing and before the |
| 6393 | * HAProxy scheduler starts. This function expect only one LUA |
| 6394 | * argument that is a function. This function returns nothing, but |
| 6395 | * throws if an error is encountered. |
| 6396 | */ |
| 6397 | __LJMP static int hlua_register_init(lua_State *L) |
| 6398 | { |
| 6399 | struct hlua_init_function *init; |
| 6400 | int ref; |
| 6401 | |
| 6402 | MAY_LJMP(check_args(L, 1, "register_init")); |
| 6403 | |
| 6404 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 6405 | |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6406 | init = calloc(1, sizeof(*init)); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6407 | if (!init) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6408 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6409 | |
| 6410 | init->function_ref = ref; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 6411 | LIST_APPEND(&hlua_init_functions[hlua_state_id], &init->l); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6412 | return 0; |
| 6413 | } |
| 6414 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6415 | /* This functio is an LUA binding. It permits to register a task |
| 6416 | * executed in parallel of the main HAroxy activity. The task is |
| 6417 | * created and it is set in the HAProxy scheduler. It can be called |
| 6418 | * from the "init" section, "post init" or during the runtime. |
| 6419 | * |
| 6420 | * Lua prototype: |
| 6421 | * |
| 6422 | * <none> core.register_task(<function>) |
| 6423 | */ |
| 6424 | static int hlua_register_task(lua_State *L) |
| 6425 | { |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 6426 | struct hlua *hlua = NULL; |
| 6427 | struct task *task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6428 | int ref; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 6429 | int state_id; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6430 | |
| 6431 | MAY_LJMP(check_args(L, 1, "register_task")); |
| 6432 | |
| 6433 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 6434 | |
Thierry Fournier | 75fc029 | 2020-11-28 13:18:56 +0100 | [diff] [blame] | 6435 | /* Get the reference state. If the reference is NULL, L is the master |
| 6436 | * state, otherwise hlua->T is. |
| 6437 | */ |
| 6438 | hlua = hlua_gethlua(L); |
| 6439 | if (hlua) |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 6440 | /* we are in runtime processing */ |
| 6441 | state_id = hlua->state_id; |
Thierry Fournier | 75fc029 | 2020-11-28 13:18:56 +0100 | [diff] [blame] | 6442 | else |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 6443 | /* we are in initialization mode */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 6444 | state_id = hlua_state_id; |
Thierry Fournier | 75fc029 | 2020-11-28 13:18:56 +0100 | [diff] [blame] | 6445 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6446 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6447 | if (!hlua) |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 6448 | goto alloc_error; |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 6449 | HLUA_INIT(hlua); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6450 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 6451 | /* We are in the common lua state, execute the task anywhere, |
| 6452 | * otherwise, inherit the current thread identifier |
| 6453 | */ |
| 6454 | if (state_id == 0) |
| 6455 | task = task_new(MAX_THREADS_MASK); |
| 6456 | else |
| 6457 | task = task_new(tid_bit); |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 6458 | if (!task) |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 6459 | goto alloc_error; |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 6460 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6461 | task->context = hlua; |
| 6462 | task->process = hlua_process_task; |
| 6463 | |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 6464 | if (!hlua_ctx_init(hlua, state_id, task, 1)) |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 6465 | goto alloc_error; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6466 | |
| 6467 | /* Restore the function in the stack. */ |
| 6468 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref); |
| 6469 | hlua->nargs = 0; |
| 6470 | |
| 6471 | /* Schedule task. */ |
| 6472 | task_schedule(task, now_ms); |
| 6473 | |
| 6474 | return 0; |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 6475 | |
| 6476 | alloc_error: |
| 6477 | task_destroy(task); |
| 6478 | hlua_ctx_destroy(hlua); |
| 6479 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 6480 | return 0; /* Never reached */ |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6481 | } |
| 6482 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6483 | /* Wrapper called by HAProxy to execute an LUA converter. This wrapper |
| 6484 | * doesn't allow "yield" functions because the HAProxy engine cannot |
| 6485 | * resume converters. |
| 6486 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6487 | 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] | 6488 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 6489 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6490 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6491 | const char *error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6492 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6493 | if (!stream) |
| 6494 | return 0; |
| 6495 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6496 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6497 | * Lua context can be not initialized. This behavior |
| 6498 | * permits to save performances because a systematic |
| 6499 | * Lua initialization cause 5% performances loss. |
| 6500 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6501 | if (!stream->hlua) { |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 6502 | struct hlua *hlua; |
| 6503 | |
| 6504 | hlua = pool_alloc(pool_head_hlua); |
| 6505 | if (!hlua) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6506 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 6507 | return 0; |
| 6508 | } |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 6509 | HLUA_INIT(hlua); |
| 6510 | stream->hlua = hlua; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 6511 | 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] | 6512 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 6513 | return 0; |
| 6514 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6515 | } |
| 6516 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6517 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6518 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6519 | |
| 6520 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 6521 | if (!SET_SAFE_LJMP(stream->hlua)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6522 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 6523 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6524 | else |
| 6525 | error = "critical error"; |
| 6526 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6527 | return 0; |
| 6528 | } |
| 6529 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6530 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6531 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6532 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 6533 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6534 | return 0; |
| 6535 | } |
| 6536 | |
| 6537 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 6538 | 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] | 6539 | |
| 6540 | /* convert input sample and pust-it in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6541 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6542 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 6543 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6544 | return 0; |
| 6545 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6546 | hlua_smp2lua(stream->hlua->T, smp); |
| 6547 | stream->hlua->nargs = 1; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6548 | |
| 6549 | /* push keywords in the stack. */ |
| 6550 | if (arg_p) { |
| 6551 | for (; arg_p->type != ARGT_STOP; arg_p++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6552 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6553 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 6554 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6555 | return 0; |
| 6556 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6557 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 6558 | stream->hlua->nargs++; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6559 | } |
| 6560 | } |
| 6561 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6562 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6563 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6564 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6565 | /* At this point the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 6566 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6567 | } |
| 6568 | |
| 6569 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6570 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6571 | /* finished. */ |
| 6572 | case HLUA_E_OK: |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 6573 | /* If the stack is empty, the function fails. */ |
| 6574 | if (lua_gettop(stream->hlua->T) <= 0) |
| 6575 | return 0; |
| 6576 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6577 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6578 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 6579 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6580 | return 1; |
| 6581 | |
| 6582 | /* yield. */ |
| 6583 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6584 | 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] | 6585 | return 0; |
| 6586 | |
| 6587 | /* finished with error. */ |
| 6588 | case HLUA_E_ERRMSG: |
| 6589 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6590 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6591 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 6592 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6593 | return 0; |
| 6594 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6595 | case HLUA_E_ETMOUT: |
| 6596 | SEND_ERR(stream->be, "Lua converter '%s': execution timeout.\n", fcn->name); |
| 6597 | return 0; |
| 6598 | |
| 6599 | case HLUA_E_NOMEM: |
| 6600 | SEND_ERR(stream->be, "Lua converter '%s': out of memory error.\n", fcn->name); |
| 6601 | return 0; |
| 6602 | |
| 6603 | case HLUA_E_YIELD: |
| 6604 | SEND_ERR(stream->be, "Lua converter '%s': yield functions like core.tcp() or core.sleep() are not allowed.\n", fcn->name); |
| 6605 | return 0; |
| 6606 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6607 | case HLUA_E_ERR: |
| 6608 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6609 | 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] | 6610 | /* fall through */ |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6611 | |
| 6612 | default: |
| 6613 | return 0; |
| 6614 | } |
| 6615 | } |
| 6616 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6617 | /* Wrapper called by HAProxy to execute a sample-fetch. this wrapper |
| 6618 | * doesn't allow "yield" functions because the HAProxy engine cannot |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6619 | * resume sample-fetches. This function will be called by the sample |
| 6620 | * fetch engine to call lua-based fetch operations. |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6621 | */ |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6622 | static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp, |
| 6623 | const char *kw, void *private) |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6624 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 6625 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6626 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6627 | const char *error; |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6628 | unsigned int hflags = HLUA_TXN_NOTERM; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6629 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6630 | if (!stream) |
| 6631 | return 0; |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 6632 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6633 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6634 | * Lua context can be not initialized. This behavior |
| 6635 | * permits to save performances because a systematic |
| 6636 | * Lua initialization cause 5% performances loss. |
| 6637 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6638 | if (!stream->hlua) { |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 6639 | struct hlua *hlua; |
| 6640 | |
| 6641 | hlua = pool_alloc(pool_head_hlua); |
| 6642 | if (!hlua) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6643 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 6644 | return 0; |
| 6645 | } |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 6646 | hlua->T = NULL; |
| 6647 | stream->hlua = hlua; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 6648 | 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] | 6649 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 6650 | return 0; |
| 6651 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6652 | } |
| 6653 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6654 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6655 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6656 | |
| 6657 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 6658 | if (!SET_SAFE_LJMP(stream->hlua)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6659 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 6660 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6661 | else |
| 6662 | error = "critical error"; |
| 6663 | 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] | 6664 | return 0; |
| 6665 | } |
| 6666 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6667 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6668 | if (!lua_checkstack(stream->hlua->T, 2)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6669 | 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] | 6670 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6671 | return 0; |
| 6672 | } |
| 6673 | |
| 6674 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 6675 | 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] | 6676 | |
| 6677 | /* push arguments in the stack. */ |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6678 | 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] | 6679 | 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] | 6680 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6681 | return 0; |
| 6682 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6683 | stream->hlua->nargs = 1; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6684 | |
| 6685 | /* push keywords in the stack. */ |
| 6686 | for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) { |
| 6687 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6688 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6689 | 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] | 6690 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6691 | return 0; |
| 6692 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6693 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 6694 | stream->hlua->nargs++; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6695 | } |
| 6696 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6697 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6698 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6699 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6700 | /* At this point the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 6701 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6702 | } |
| 6703 | |
| 6704 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6705 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6706 | /* finished. */ |
| 6707 | case HLUA_E_OK: |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 6708 | /* If the stack is empty, the function fails. */ |
| 6709 | if (lua_gettop(stream->hlua->T) <= 0) |
| 6710 | return 0; |
| 6711 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6712 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6713 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 6714 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6715 | |
| 6716 | /* Set the end of execution flag. */ |
| 6717 | smp->flags &= ~SMP_F_MAY_CHANGE; |
| 6718 | return 1; |
| 6719 | |
| 6720 | /* yield. */ |
| 6721 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6722 | 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] | 6723 | return 0; |
| 6724 | |
| 6725 | /* finished with error. */ |
| 6726 | case HLUA_E_ERRMSG: |
| 6727 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6728 | SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6729 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 6730 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6731 | return 0; |
| 6732 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6733 | case HLUA_E_ETMOUT: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6734 | SEND_ERR(smp->px, "Lua sample-fetch '%s': execution timeout.\n", fcn->name); |
| 6735 | return 0; |
| 6736 | |
| 6737 | case HLUA_E_NOMEM: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6738 | SEND_ERR(smp->px, "Lua sample-fetch '%s': out of memory error.\n", fcn->name); |
| 6739 | return 0; |
| 6740 | |
| 6741 | case HLUA_E_YIELD: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6742 | SEND_ERR(smp->px, "Lua sample-fetch '%s': yield not allowed.\n", fcn->name); |
| 6743 | return 0; |
| 6744 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6745 | case HLUA_E_ERR: |
| 6746 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6747 | 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] | 6748 | /* fall through */ |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6749 | |
| 6750 | default: |
| 6751 | return 0; |
| 6752 | } |
| 6753 | } |
| 6754 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6755 | /* This function is an LUA binding used for registering |
| 6756 | * "sample-conv" functions. It expects a converter name used |
| 6757 | * in the haproxy configuration file, and an LUA function. |
| 6758 | */ |
| 6759 | __LJMP static int hlua_register_converters(lua_State *L) |
| 6760 | { |
| 6761 | struct sample_conv_kw_list *sck; |
| 6762 | const char *name; |
| 6763 | int ref; |
| 6764 | int len; |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 6765 | struct hlua_function *fcn = NULL; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 6766 | struct sample_conv *sc; |
| 6767 | struct buffer *trash; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6768 | |
| 6769 | MAY_LJMP(check_args(L, 2, "register_converters")); |
| 6770 | |
| 6771 | /* First argument : converter name. */ |
| 6772 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6773 | |
| 6774 | /* Second argument : lua function. */ |
| 6775 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 6776 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 6777 | /* Check if the converter is already registered */ |
| 6778 | trash = get_trash_chunk(); |
| 6779 | chunk_printf(trash, "lua.%s", name); |
| 6780 | sc = find_sample_conv(trash->area, trash->data); |
| 6781 | if (sc != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 6782 | fcn = sc->private; |
| 6783 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 6784 | ha_warning("Trying to register converter 'lua.%s' more than once. " |
| 6785 | "This will become a hard error in version 2.5.\n", name); |
| 6786 | } |
| 6787 | fcn->function_ref[hlua_state_id] = ref; |
| 6788 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 6789 | } |
| 6790 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6791 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6792 | sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6793 | if (!sck) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 6794 | goto alloc_error; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 6795 | fcn = new_hlua_function(); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6796 | if (!fcn) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 6797 | goto alloc_error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6798 | |
| 6799 | /* Fill fcn. */ |
| 6800 | fcn->name = strdup(name); |
| 6801 | if (!fcn->name) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 6802 | goto alloc_error; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 6803 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6804 | |
| 6805 | /* List head */ |
| 6806 | sck->list.n = sck->list.p = NULL; |
| 6807 | |
| 6808 | /* converter keyword. */ |
| 6809 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6810 | sck->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6811 | if (!sck->kw[0].kw) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 6812 | goto alloc_error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6813 | |
| 6814 | snprintf((char *)sck->kw[0].kw, len, "lua.%s", name); |
| 6815 | sck->kw[0].process = hlua_sample_conv_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 6816 | 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] | 6817 | sck->kw[0].val_args = NULL; |
| 6818 | sck->kw[0].in_type = SMP_T_STR; |
| 6819 | sck->kw[0].out_type = SMP_T_STR; |
| 6820 | sck->kw[0].private = fcn; |
| 6821 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6822 | /* Register this new converter */ |
| 6823 | sample_register_convs(sck); |
| 6824 | |
| 6825 | return 0; |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 6826 | |
| 6827 | alloc_error: |
| 6828 | release_hlua_function(fcn); |
| 6829 | ha_free(&sck); |
| 6830 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 6831 | return 0; /* Never reached */ |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6832 | } |
| 6833 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6834 | /* This function is an LUA binding used for registering |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6835 | * "sample-fetch" functions. It expects a converter name used |
| 6836 | * in the haproxy configuration file, and an LUA function. |
| 6837 | */ |
| 6838 | __LJMP static int hlua_register_fetches(lua_State *L) |
| 6839 | { |
| 6840 | const char *name; |
| 6841 | int ref; |
| 6842 | int len; |
| 6843 | struct sample_fetch_kw_list *sfk; |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 6844 | struct hlua_function *fcn = NULL; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 6845 | struct sample_fetch *sf; |
| 6846 | struct buffer *trash; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6847 | |
| 6848 | MAY_LJMP(check_args(L, 2, "register_fetches")); |
| 6849 | |
| 6850 | /* First argument : sample-fetch name. */ |
| 6851 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6852 | |
| 6853 | /* Second argument : lua function. */ |
| 6854 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 6855 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 6856 | /* Check if the sample-fetch is already registered */ |
| 6857 | trash = get_trash_chunk(); |
| 6858 | chunk_printf(trash, "lua.%s", name); |
| 6859 | sf = find_sample_fetch(trash->area, trash->data); |
| 6860 | if (sf != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 6861 | fcn = sf->private; |
| 6862 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 6863 | ha_warning("Trying to register sample-fetch 'lua.%s' more than once. " |
| 6864 | "This will become a hard error in version 2.5.\n", name); |
| 6865 | } |
| 6866 | fcn->function_ref[hlua_state_id] = ref; |
| 6867 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 6868 | } |
| 6869 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6870 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6871 | sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6872 | if (!sfk) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 6873 | goto alloc_error; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 6874 | fcn = new_hlua_function(); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6875 | if (!fcn) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 6876 | goto alloc_error; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6877 | |
| 6878 | /* Fill fcn. */ |
| 6879 | fcn->name = strdup(name); |
| 6880 | if (!fcn->name) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 6881 | goto alloc_error; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 6882 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6883 | |
| 6884 | /* List head */ |
| 6885 | sfk->list.n = sfk->list.p = NULL; |
| 6886 | |
| 6887 | /* sample-fetch keyword. */ |
| 6888 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6889 | sfk->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6890 | if (!sfk->kw[0].kw) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 6891 | goto alloc_error; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6892 | |
| 6893 | snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name); |
| 6894 | sfk->kw[0].process = hlua_sample_fetch_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 6895 | 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] | 6896 | sfk->kw[0].val_args = NULL; |
| 6897 | sfk->kw[0].out_type = SMP_T_STR; |
| 6898 | sfk->kw[0].use = SMP_USE_HTTP_ANY; |
| 6899 | sfk->kw[0].val = 0; |
| 6900 | sfk->kw[0].private = fcn; |
| 6901 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6902 | /* Register this new fetch. */ |
| 6903 | sample_register_fetches(sfk); |
| 6904 | |
| 6905 | return 0; |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 6906 | |
| 6907 | alloc_error: |
| 6908 | release_hlua_function(fcn); |
| 6909 | ha_free(&sfk); |
| 6910 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 6911 | return 0; /* Never reached */ |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6912 | } |
| 6913 | |
Christopher Faulet | 501465d | 2020-02-26 14:54:16 +0100 | [diff] [blame] | 6914 | /* This function is a lua binding to set the wake_time. |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 6915 | */ |
Christopher Faulet | 501465d | 2020-02-26 14:54:16 +0100 | [diff] [blame] | 6916 | __LJMP static int hlua_set_wake_time(lua_State *L) |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 6917 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 6918 | struct hlua *hlua; |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 6919 | unsigned int delay; |
| 6920 | unsigned int wakeup_ms; |
| 6921 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 6922 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 6923 | hlua = hlua_gethlua(L); |
| 6924 | if (!hlua) { |
| 6925 | return 0; |
| 6926 | } |
| 6927 | |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 6928 | MAY_LJMP(check_args(L, 1, "wake_time")); |
| 6929 | |
| 6930 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 6931 | wakeup_ms = tick_add(now_ms, delay); |
| 6932 | hlua->wake_time = wakeup_ms; |
| 6933 | return 0; |
| 6934 | } |
| 6935 | |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6936 | /* This function is a wrapper to execute each LUA function declared as an action |
| 6937 | * wrapper during the initialisation period. This function may return any |
| 6938 | * ACT_RET_* value. On error ACT_RET_CONT is returned and the action is |
| 6939 | * ignored. If the lua action yields, ACT_RET_YIELD is returned. On success, the |
| 6940 | * return value is the first element on the stack. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6941 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6942 | 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] | 6943 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6944 | { |
| 6945 | char **arg; |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6946 | unsigned int hflags = 0; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6947 | int dir, act_ret = ACT_RET_CONT; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6948 | const char *error; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6949 | |
| 6950 | switch (rule->from) { |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 6951 | case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break; |
| 6952 | case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break; |
| 6953 | case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break; |
| 6954 | case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6955 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6956 | SEND_ERR(px, "Lua: internal error while execute action.\n"); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6957 | goto end; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6958 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6959 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6960 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6961 | * Lua context can be not initialized. This behavior |
| 6962 | * permits to save performances because a systematic |
| 6963 | * Lua initialization cause 5% performances loss. |
| 6964 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6965 | if (!s->hlua) { |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 6966 | struct hlua *hlua; |
| 6967 | |
| 6968 | hlua = pool_alloc(pool_head_hlua); |
| 6969 | if (!hlua) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6970 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 6971 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6972 | goto end; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6973 | } |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 6974 | HLUA_INIT(hlua); |
| 6975 | s->hlua = hlua; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 6976 | 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] | 6977 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 6978 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6979 | goto end; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6980 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6981 | } |
| 6982 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6983 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6984 | if (!HLUA_IS_RUNNING(s->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6985 | |
| 6986 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 6987 | if (!SET_SAFE_LJMP(s->hlua)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6988 | if (lua_type(s->hlua->T, -1) == LUA_TSTRING) |
| 6989 | error = lua_tostring(s->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6990 | else |
| 6991 | error = "critical error"; |
| 6992 | SEND_ERR(px, "Lua function '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 6993 | rule->arg.hlua_rule->fcn->name, error); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6994 | goto end; |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6995 | } |
| 6996 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6997 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6998 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6999 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7000 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7001 | RESET_SAFE_LJMP(s->hlua); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7002 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7003 | } |
| 7004 | |
| 7005 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 7006 | 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] | 7007 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7008 | /* Create and and push object stream in the stack. */ |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 7009 | if (!hlua_txn_new(s->hlua->T, s, px, dir, hflags)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 7010 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7011 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7012 | RESET_SAFE_LJMP(s->hlua); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7013 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7014 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 7015 | s->hlua->nargs = 1; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7016 | |
| 7017 | /* push keywords in the stack. */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7018 | for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 7019 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 7020 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7021 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7022 | RESET_SAFE_LJMP(s->hlua); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7023 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7024 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 7025 | lua_pushstring(s->hlua->T, *arg); |
| 7026 | s->hlua->nargs++; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7027 | } |
| 7028 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7029 | /* Now the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7030 | RESET_SAFE_LJMP(s->hlua); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7031 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 7032 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 7033 | s->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7034 | } |
| 7035 | |
| 7036 | /* Execute the function. */ |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 7037 | switch (hlua_ctx_resume(s->hlua, !(flags & ACT_OPT_FINAL))) { |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7038 | /* finished. */ |
| 7039 | case HLUA_E_OK: |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7040 | /* Catch the return value */ |
| 7041 | if (lua_gettop(s->hlua->T) > 0) |
| 7042 | act_ret = lua_tointeger(s->hlua->T, -1); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7043 | |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 7044 | /* Set timeout in the required channel. */ |
Christopher Faulet | 498c483 | 2020-07-28 11:59:58 +0200 | [diff] [blame] | 7045 | if (act_ret == ACT_RET_YIELD) { |
| 7046 | if (flags & ACT_OPT_FINAL) |
| 7047 | goto err_yield; |
| 7048 | |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 7049 | if (dir == SMP_OPT_DIR_REQ) |
| 7050 | s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp), |
| 7051 | s->hlua->wake_time); |
| 7052 | else |
| 7053 | s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp), |
| 7054 | s->hlua->wake_time); |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 7055 | } |
| 7056 | goto end; |
| 7057 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7058 | /* yield. */ |
| 7059 | case HLUA_E_AGAIN: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 7060 | /* Set timeout in the required channel. */ |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 7061 | if (dir == SMP_OPT_DIR_REQ) |
| 7062 | s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp), |
| 7063 | s->hlua->wake_time); |
| 7064 | else |
| 7065 | s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp), |
| 7066 | s->hlua->wake_time); |
| 7067 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7068 | /* Some actions can be wake up when a "write" event |
| 7069 | * is detected on a response channel. This is useful |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7070 | * only for actions targeted on the requests. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7071 | */ |
Christopher Faulet | 51fa358 | 2019-07-26 14:54:52 +0200 | [diff] [blame] | 7072 | if (HLUA_IS_WAKERESWR(s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 7073 | s->res.flags |= CF_WAKE_WRITE; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 7074 | if (HLUA_IS_WAKEREQWR(s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 7075 | s->req.flags |= CF_WAKE_WRITE; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7076 | act_ret = ACT_RET_YIELD; |
| 7077 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7078 | |
| 7079 | /* finished with error. */ |
| 7080 | case HLUA_E_ERRMSG: |
| 7081 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 7082 | SEND_ERR(px, "Lua function '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7083 | rule->arg.hlua_rule->fcn->name, lua_tostring(s->hlua->T, -1)); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 7084 | lua_pop(s->hlua->T, 1); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7085 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7086 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7087 | case HLUA_E_ETMOUT: |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7088 | 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] | 7089 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7090 | |
| 7091 | case HLUA_E_NOMEM: |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7092 | 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] | 7093 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7094 | |
| 7095 | case HLUA_E_YIELD: |
Christopher Faulet | 498c483 | 2020-07-28 11:59:58 +0200 | [diff] [blame] | 7096 | err_yield: |
| 7097 | act_ret = ACT_RET_CONT; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7098 | 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] | 7099 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7100 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7101 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7102 | case HLUA_E_ERR: |
| 7103 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 7104 | SEND_ERR(px, "Lua function '%s' return an unknown error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7105 | rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7106 | |
| 7107 | default: |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7108 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7109 | } |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7110 | |
| 7111 | end: |
Christopher Faulet | 2361fd9 | 2020-07-30 10:40:58 +0200 | [diff] [blame] | 7112 | if (act_ret != ACT_RET_YIELD && s->hlua) |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 7113 | s->hlua->wake_time = TICK_ETERNITY; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7114 | return act_ret; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7115 | } |
| 7116 | |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 7117 | 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] | 7118 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 7119 | struct appctx *ctx = context; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7120 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7121 | appctx_wakeup(ctx); |
Willy Tarreau | d958741 | 2017-08-23 16:07:33 +0200 | [diff] [blame] | 7122 | t->expire = TICK_ETERNITY; |
Willy Tarreau | d1aa41f | 2017-07-21 16:41:56 +0200 | [diff] [blame] | 7123 | return t; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7124 | } |
| 7125 | |
| 7126 | static int hlua_applet_tcp_init(struct appctx *ctx, struct proxy *px, struct stream *strm) |
| 7127 | { |
| 7128 | struct stream_interface *si = ctx->owner; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7129 | struct hlua *hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7130 | struct task *task; |
| 7131 | char **arg; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7132 | const char *error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7133 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7134 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7135 | if (!hlua) { |
| 7136 | SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7137 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7138 | return 0; |
| 7139 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7140 | HLUA_INIT(hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7141 | ctx->ctx.hlua_apptcp.hlua = hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7142 | ctx->ctx.hlua_apptcp.flags = 0; |
| 7143 | |
| 7144 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 7145 | task = task_new(tid_bit); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7146 | if (!task) { |
| 7147 | SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7148 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7149 | return 0; |
| 7150 | } |
| 7151 | task->nice = 0; |
| 7152 | task->context = ctx; |
| 7153 | task->process = hlua_applet_wakeup; |
| 7154 | ctx->ctx.hlua_apptcp.task = task; |
| 7155 | |
| 7156 | /* In the execution wrappers linked with a stream, the |
| 7157 | * Lua context can be not initialized. This behavior |
| 7158 | * permits to save performances because a systematic |
| 7159 | * Lua initialization cause 5% performances loss. |
| 7160 | */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 7161 | 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] | 7162 | 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] | 7163 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7164 | return 0; |
| 7165 | } |
| 7166 | |
| 7167 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 7168 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7169 | |
| 7170 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7171 | if (!SET_SAFE_LJMP(hlua)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7172 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 7173 | error = lua_tostring(hlua->T, -1); |
| 7174 | else |
| 7175 | error = "critical error"; |
| 7176 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7177 | ctx->rule->arg.hlua_rule->fcn->name, error); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7178 | return 0; |
| 7179 | } |
| 7180 | |
| 7181 | /* Check stack available size. */ |
| 7182 | if (!lua_checkstack(hlua->T, 1)) { |
| 7183 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7184 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7185 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7186 | return 0; |
| 7187 | } |
| 7188 | |
| 7189 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 7190 | 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] | 7191 | |
| 7192 | /* Create and and push object stream in the stack. */ |
| 7193 | if (!hlua_applet_tcp_new(hlua->T, ctx)) { |
| 7194 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7195 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7196 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7197 | return 0; |
| 7198 | } |
| 7199 | hlua->nargs = 1; |
| 7200 | |
| 7201 | /* push keywords in the stack. */ |
| 7202 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 7203 | if (!lua_checkstack(hlua->T, 1)) { |
| 7204 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7205 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7206 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7207 | return 0; |
| 7208 | } |
| 7209 | lua_pushstring(hlua->T, *arg); |
| 7210 | hlua->nargs++; |
| 7211 | } |
| 7212 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7213 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7214 | |
| 7215 | /* Wakeup the applet ASAP. */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 7216 | si_cant_get(si); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 7217 | si_rx_endp_more(si); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7218 | |
| 7219 | return 1; |
| 7220 | } |
| 7221 | |
Willy Tarreau | 60409db | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 7222 | void hlua_applet_tcp_fct(struct appctx *ctx) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7223 | { |
| 7224 | struct stream_interface *si = ctx->owner; |
| 7225 | struct stream *strm = si_strm(si); |
| 7226 | struct channel *res = si_ic(si); |
| 7227 | struct act_rule *rule = ctx->rule; |
| 7228 | struct proxy *px = strm->be; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7229 | struct hlua *hlua = ctx->ctx.hlua_apptcp.hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7230 | |
| 7231 | /* The applet execution is already done. */ |
Olivier Houchard | 594c8c5 | 2018-08-28 14:41:31 +0200 | [diff] [blame] | 7232 | if (ctx->ctx.hlua_apptcp.flags & APPLET_DONE) { |
| 7233 | /* eat the whole request */ |
| 7234 | co_skip(si_oc(si), co_data(si_oc(si))); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7235 | return; |
Olivier Houchard | 594c8c5 | 2018-08-28 14:41:31 +0200 | [diff] [blame] | 7236 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7237 | |
| 7238 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 7239 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 7240 | return; |
| 7241 | |
| 7242 | /* Execute the function. */ |
| 7243 | switch (hlua_ctx_resume(hlua, 1)) { |
| 7244 | /* finished. */ |
| 7245 | case HLUA_E_OK: |
| 7246 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 7247 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7248 | /* eat the whole request */ |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 7249 | co_skip(si_oc(si), co_data(si_oc(si))); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7250 | res->flags |= CF_READ_NULL; |
| 7251 | si_shutr(si); |
| 7252 | return; |
| 7253 | |
| 7254 | /* yield. */ |
| 7255 | case HLUA_E_AGAIN: |
Thierry Fournier | 0164f20 | 2016-02-20 17:47:43 +0100 | [diff] [blame] | 7256 | if (hlua->wake_time != TICK_ETERNITY) |
| 7257 | task_schedule(ctx->ctx.hlua_apptcp.task, hlua->wake_time); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7258 | return; |
| 7259 | |
| 7260 | /* finished with error. */ |
| 7261 | case HLUA_E_ERRMSG: |
| 7262 | /* Display log. */ |
| 7263 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7264 | rule->arg.hlua_rule->fcn->name, lua_tostring(hlua->T, -1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7265 | lua_pop(hlua->T, 1); |
| 7266 | goto error; |
| 7267 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7268 | case HLUA_E_ETMOUT: |
| 7269 | SEND_ERR(px, "Lua applet tcp '%s': execution timeout.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7270 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7271 | goto error; |
| 7272 | |
| 7273 | case HLUA_E_NOMEM: |
| 7274 | SEND_ERR(px, "Lua applet tcp '%s': out of memory error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7275 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7276 | goto error; |
| 7277 | |
| 7278 | case HLUA_E_YIELD: /* unexpected */ |
| 7279 | SEND_ERR(px, "Lua applet tcp '%s': yield not allowed.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7280 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7281 | goto error; |
| 7282 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7283 | case HLUA_E_ERR: |
| 7284 | /* Display log. */ |
| 7285 | SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7286 | rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7287 | goto error; |
| 7288 | |
| 7289 | default: |
| 7290 | goto error; |
| 7291 | } |
| 7292 | |
| 7293 | error: |
| 7294 | |
| 7295 | /* For all other cases, just close the stream. */ |
| 7296 | si_shutw(si); |
| 7297 | si_shutr(si); |
| 7298 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 7299 | } |
| 7300 | |
| 7301 | static void hlua_applet_tcp_release(struct appctx *ctx) |
| 7302 | { |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 7303 | task_destroy(ctx->ctx.hlua_apptcp.task); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7304 | ctx->ctx.hlua_apptcp.task = NULL; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7305 | hlua_ctx_destroy(ctx->ctx.hlua_apptcp.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7306 | ctx->ctx.hlua_apptcp.hlua = NULL; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7307 | } |
| 7308 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7309 | /* The function returns 1 if the initialisation is complete, 0 if |
| 7310 | * an errors occurs and -1 if more data are required for initializing |
| 7311 | * the applet. |
| 7312 | */ |
| 7313 | static int hlua_applet_http_init(struct appctx *ctx, struct proxy *px, struct stream *strm) |
| 7314 | { |
| 7315 | struct stream_interface *si = ctx->owner; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7316 | struct http_txn *txn; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7317 | struct hlua *hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7318 | char **arg; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7319 | struct task *task; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7320 | const char *error; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7321 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7322 | txn = strm->txn; |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7323 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7324 | if (!hlua) { |
| 7325 | SEND_ERR(px, "Lua applet http '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7326 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7327 | return 0; |
| 7328 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7329 | HLUA_INIT(hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7330 | ctx->ctx.hlua_apphttp.hlua = hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7331 | ctx->ctx.hlua_apphttp.left_bytes = -1; |
| 7332 | ctx->ctx.hlua_apphttp.flags = 0; |
| 7333 | |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 7334 | if (txn->req.flags & HTTP_MSGF_VER_11) |
| 7335 | ctx->ctx.hlua_apphttp.flags |= APPLET_HTTP11; |
| 7336 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7337 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 7338 | task = task_new(tid_bit); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7339 | if (!task) { |
| 7340 | SEND_ERR(px, "Lua applet http '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7341 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7342 | return 0; |
| 7343 | } |
| 7344 | task->nice = 0; |
| 7345 | task->context = ctx; |
| 7346 | task->process = hlua_applet_wakeup; |
| 7347 | ctx->ctx.hlua_apphttp.task = task; |
| 7348 | |
| 7349 | /* In the execution wrappers linked with a stream, the |
| 7350 | * Lua context can be not initialized. This behavior |
| 7351 | * permits to save performances because a systematic |
| 7352 | * Lua initialization cause 5% performances loss. |
| 7353 | */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 7354 | 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] | 7355 | 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] | 7356 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7357 | return 0; |
| 7358 | } |
| 7359 | |
| 7360 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 7361 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7362 | |
| 7363 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7364 | if (!SET_SAFE_LJMP(hlua)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7365 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 7366 | error = lua_tostring(hlua->T, -1); |
| 7367 | else |
| 7368 | error = "critical error"; |
| 7369 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7370 | ctx->rule->arg.hlua_rule->fcn->name, error); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7371 | return 0; |
| 7372 | } |
| 7373 | |
| 7374 | /* Check stack available size. */ |
| 7375 | if (!lua_checkstack(hlua->T, 1)) { |
| 7376 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7377 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7378 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7379 | return 0; |
| 7380 | } |
| 7381 | |
| 7382 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 7383 | 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] | 7384 | |
| 7385 | /* Create and and push object stream in the stack. */ |
| 7386 | if (!hlua_applet_http_new(hlua->T, ctx)) { |
| 7387 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7388 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7389 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7390 | return 0; |
| 7391 | } |
| 7392 | hlua->nargs = 1; |
| 7393 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7394 | /* push keywords in the stack. */ |
| 7395 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 7396 | if (!lua_checkstack(hlua->T, 1)) { |
| 7397 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7398 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7399 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7400 | return 0; |
| 7401 | } |
| 7402 | lua_pushstring(hlua->T, *arg); |
| 7403 | hlua->nargs++; |
| 7404 | } |
| 7405 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7406 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7407 | |
| 7408 | /* Wakeup the applet when data is ready for read. */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 7409 | si_cant_get(si); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7410 | |
| 7411 | return 1; |
| 7412 | } |
| 7413 | |
Willy Tarreau | 60409db | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 7414 | void hlua_applet_http_fct(struct appctx *ctx) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7415 | { |
| 7416 | struct stream_interface *si = ctx->owner; |
| 7417 | struct stream *strm = si_strm(si); |
| 7418 | struct channel *req = si_oc(si); |
| 7419 | struct channel *res = si_ic(si); |
| 7420 | struct act_rule *rule = ctx->rule; |
| 7421 | struct proxy *px = strm->be; |
| 7422 | struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua; |
| 7423 | struct htx *req_htx, *res_htx; |
| 7424 | |
| 7425 | res_htx = htx_from_buf(&res->buf); |
| 7426 | |
| 7427 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 7428 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 7429 | goto out; |
| 7430 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 7431 | /* Check if the input buffer is available. */ |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7432 | if (!b_size(&res->buf)) { |
| 7433 | si_rx_room_blk(si); |
| 7434 | goto out; |
| 7435 | } |
| 7436 | /* check that the output is not closed */ |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7437 | if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR)) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7438 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 7439 | |
| 7440 | /* Set the currently running flag. */ |
| 7441 | if (!HLUA_IS_RUNNING(hlua) && |
| 7442 | !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
Christopher Faulet | bd878d2 | 2021-04-28 10:50:21 +0200 | [diff] [blame] | 7443 | if (!co_data(req)) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7444 | si_cant_get(si); |
| 7445 | goto out; |
| 7446 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7447 | } |
| 7448 | |
| 7449 | /* Executes The applet if it is not done. */ |
| 7450 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
| 7451 | |
| 7452 | /* Execute the function. */ |
| 7453 | switch (hlua_ctx_resume(hlua, 1)) { |
| 7454 | /* finished. */ |
| 7455 | case HLUA_E_OK: |
| 7456 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 7457 | break; |
| 7458 | |
| 7459 | /* yield. */ |
| 7460 | case HLUA_E_AGAIN: |
| 7461 | if (hlua->wake_time != TICK_ETERNITY) |
| 7462 | task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time); |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7463 | goto out; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7464 | |
| 7465 | /* finished with error. */ |
| 7466 | case HLUA_E_ERRMSG: |
| 7467 | /* Display log. */ |
| 7468 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7469 | rule->arg.hlua_rule->fcn->name, lua_tostring(hlua->T, -1)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7470 | lua_pop(hlua->T, 1); |
| 7471 | goto error; |
| 7472 | |
| 7473 | case HLUA_E_ETMOUT: |
| 7474 | SEND_ERR(px, "Lua applet http '%s': execution timeout.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7475 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7476 | goto error; |
| 7477 | |
| 7478 | case HLUA_E_NOMEM: |
| 7479 | SEND_ERR(px, "Lua applet http '%s': out of memory error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7480 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7481 | goto error; |
| 7482 | |
| 7483 | case HLUA_E_YIELD: /* unexpected */ |
| 7484 | SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7485 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7486 | goto error; |
| 7487 | |
| 7488 | case HLUA_E_ERR: |
| 7489 | /* Display log. */ |
| 7490 | SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7491 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7492 | goto error; |
| 7493 | |
| 7494 | default: |
| 7495 | goto error; |
| 7496 | } |
| 7497 | } |
| 7498 | |
| 7499 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7500 | if (ctx->ctx.hlua_apphttp.flags & APPLET_RSP_SENT) |
| 7501 | goto done; |
| 7502 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7503 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) |
| 7504 | goto error; |
| 7505 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 7506 | /* no more data are expected. Don't add TLR because mux-h1 will take care of it */ |
| 7507 | res_htx->flags |= HTX_FL_EOM; |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7508 | strm->txn->status = ctx->ctx.hlua_apphttp.status; |
| 7509 | ctx->ctx.hlua_apphttp.flags |= APPLET_RSP_SENT; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7510 | } |
| 7511 | |
| 7512 | done: |
| 7513 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7514 | if (!(res->flags & CF_SHUTR)) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7515 | res->flags |= CF_READ_NULL; |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7516 | si_shutr(si); |
| 7517 | } |
| 7518 | |
| 7519 | /* eat the whole request */ |
| 7520 | if (co_data(req)) { |
| 7521 | req_htx = htx_from_buf(&req->buf); |
| 7522 | co_htx_skip(req, req_htx, co_data(req)); |
| 7523 | htx_to_buf(req_htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7524 | } |
| 7525 | } |
| 7526 | |
| 7527 | out: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7528 | htx_to_buf(res_htx, &res->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7529 | return; |
| 7530 | |
| 7531 | error: |
| 7532 | |
| 7533 | /* If we are in HTTP mode, and we are not send any |
| 7534 | * data, return a 500 server error in best effort: |
| 7535 | * if there is no room available in the buffer, |
| 7536 | * just close the connection. |
| 7537 | */ |
| 7538 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) { |
Christopher Faulet | f734638 | 2019-07-17 22:02:08 +0200 | [diff] [blame] | 7539 | struct buffer *err = &http_err_chunks[HTTP_ERR_500]; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7540 | |
| 7541 | channel_erase(res); |
| 7542 | res->buf.data = b_data(err); |
| 7543 | memcpy(res->buf.area, b_head(err), b_data(err)); |
| 7544 | res_htx = htx_from_buf(&res->buf); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 7545 | channel_add_input(res, res_htx->data); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7546 | } |
| 7547 | if (!(strm->flags & SF_ERR_MASK)) |
| 7548 | strm->flags |= SF_ERR_RESOURCE; |
| 7549 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 7550 | goto done; |
| 7551 | } |
| 7552 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7553 | static void hlua_applet_http_release(struct appctx *ctx) |
| 7554 | { |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 7555 | task_destroy(ctx->ctx.hlua_apphttp.task); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7556 | ctx->ctx.hlua_apphttp.task = NULL; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7557 | hlua_ctx_destroy(ctx->ctx.hlua_apphttp.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7558 | ctx->ctx.hlua_apphttp.hlua = NULL; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7559 | } |
| 7560 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7561 | /* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 7562 | * success case, else return ACT_RET_PRS_ERR. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7563 | * |
| 7564 | * This function can fail with an abort() due to an Lua critical error. |
| 7565 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 7566 | * tolerated. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7567 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7568 | static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px, |
| 7569 | struct act_rule *rule, char **err) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7570 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7571 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7572 | int i; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7573 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7574 | /* Memory for the rule. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7575 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7576 | if (!rule->arg.hlua_rule) { |
| 7577 | memprintf(err, "out of memory error"); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 7578 | goto error; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7579 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7580 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7581 | /* Memory for arguments. */ |
Tim Duesterhus | e52b6e5 | 2020-09-12 20:26:43 +0200 | [diff] [blame] | 7582 | rule->arg.hlua_rule->args = calloc(fcn->nargs + 1, |
| 7583 | sizeof(*rule->arg.hlua_rule->args)); |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7584 | if (!rule->arg.hlua_rule->args) { |
| 7585 | memprintf(err, "out of memory error"); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 7586 | goto error; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7587 | } |
| 7588 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7589 | /* Reference the Lua function and store the reference. */ |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7590 | rule->arg.hlua_rule->fcn = fcn; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7591 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7592 | /* Expect some arguments */ |
| 7593 | for (i = 0; i < fcn->nargs; i++) { |
Thierry FOURNIER | 1725c2e | 2019-01-06 19:38:49 +0100 | [diff] [blame] | 7594 | if (*args[*cur_arg] == '\0') { |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7595 | memprintf(err, "expect %d arguments", fcn->nargs); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 7596 | goto error; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7597 | } |
Thierry FOURNIER | 1725c2e | 2019-01-06 19:38:49 +0100 | [diff] [blame] | 7598 | rule->arg.hlua_rule->args[i] = strdup(args[*cur_arg]); |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7599 | if (!rule->arg.hlua_rule->args[i]) { |
| 7600 | memprintf(err, "out of memory error"); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 7601 | goto error; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7602 | } |
| 7603 | (*cur_arg)++; |
| 7604 | } |
| 7605 | rule->arg.hlua_rule->args[i] = NULL; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7606 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 7607 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7608 | rule->action_ptr = hlua_action; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 7609 | return ACT_RET_PRS_OK; |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 7610 | |
| 7611 | error: |
| 7612 | if (rule->arg.hlua_rule) { |
| 7613 | if (rule->arg.hlua_rule->args) { |
| 7614 | for (i = 0; i < fcn->nargs; i++) |
| 7615 | ha_free(&rule->arg.hlua_rule->args[i]); |
| 7616 | ha_free(&rule->arg.hlua_rule->args); |
| 7617 | } |
| 7618 | ha_free(&rule->arg.hlua_rule); |
| 7619 | } |
| 7620 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7621 | } |
| 7622 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7623 | static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px, |
| 7624 | struct act_rule *rule, char **err) |
| 7625 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7626 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7627 | |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 7628 | /* HTTP applets are forbidden in tcp-request rules. |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 7629 | * HTTP applet request requires everything initialized by |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 7630 | * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER). |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 7631 | * The applet will be immediately initialized, but its before |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 7632 | * the call of this analyzer. |
| 7633 | */ |
| 7634 | if (rule->from != ACT_F_HTTP_REQ) { |
| 7635 | memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets"); |
| 7636 | return ACT_RET_PRS_ERR; |
| 7637 | } |
| 7638 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7639 | /* Memory for the rule. */ |
| 7640 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 7641 | if (!rule->arg.hlua_rule) { |
| 7642 | memprintf(err, "out of memory error"); |
| 7643 | return ACT_RET_PRS_ERR; |
| 7644 | } |
| 7645 | |
| 7646 | /* Reference the Lua function and store the reference. */ |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7647 | rule->arg.hlua_rule->fcn = fcn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7648 | |
| 7649 | /* TODO: later accept arguments. */ |
| 7650 | rule->arg.hlua_rule->args = NULL; |
| 7651 | |
| 7652 | /* Add applet pointer in the rule. */ |
| 7653 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 7654 | rule->applet.name = fcn->name; |
| 7655 | rule->applet.init = hlua_applet_http_init; |
| 7656 | rule->applet.fct = hlua_applet_http_fct; |
| 7657 | rule->applet.release = hlua_applet_http_release; |
| 7658 | rule->applet.timeout = hlua_timeout_applet; |
| 7659 | |
| 7660 | return ACT_RET_PRS_OK; |
| 7661 | } |
| 7662 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7663 | /* This function is an LUA binding used for registering |
| 7664 | * "sample-conv" functions. It expects a converter name used |
| 7665 | * in the haproxy configuration file, and an LUA function. |
| 7666 | */ |
| 7667 | __LJMP static int hlua_register_action(lua_State *L) |
| 7668 | { |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 7669 | struct action_kw_list *akl = NULL; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7670 | const char *name; |
| 7671 | int ref; |
| 7672 | int len; |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 7673 | struct hlua_function *fcn = NULL; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7674 | int nargs; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 7675 | struct buffer *trash; |
| 7676 | struct action_kw *akw; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7677 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7678 | /* Initialise the number of expected arguments at 0. */ |
| 7679 | nargs = 0; |
| 7680 | |
| 7681 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 7682 | 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] | 7683 | |
| 7684 | /* First argument : converter name. */ |
| 7685 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 7686 | |
| 7687 | /* Second argument : environment. */ |
| 7688 | if (lua_type(L, 2) != LUA_TTABLE) |
| 7689 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 7690 | |
| 7691 | /* Third argument : lua function. */ |
| 7692 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 7693 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7694 | /* 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] | 7695 | if (lua_gettop(L) >= 4) |
| 7696 | nargs = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 7697 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7698 | /* browse the second argument as an array. */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7699 | lua_pushnil(L); |
| 7700 | while (lua_next(L, 2) != 0) { |
| 7701 | if (lua_type(L, -1) != LUA_TSTRING) |
| 7702 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 7703 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 7704 | /* Check if action exists */ |
| 7705 | trash = get_trash_chunk(); |
| 7706 | chunk_printf(trash, "lua.%s", name); |
| 7707 | if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) { |
| 7708 | akw = tcp_req_cont_action(trash->area); |
| 7709 | } else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) { |
| 7710 | akw = tcp_res_cont_action(trash->area); |
| 7711 | } else if (strcmp(lua_tostring(L, -1), "http-req") == 0) { |
| 7712 | akw = action_http_req_custom(trash->area); |
| 7713 | } else if (strcmp(lua_tostring(L, -1), "http-res") == 0) { |
| 7714 | akw = action_http_res_custom(trash->area); |
| 7715 | } else { |
| 7716 | akw = NULL; |
| 7717 | } |
| 7718 | if (akw != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 7719 | fcn = akw->private; |
| 7720 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 7721 | ha_warning("Trying to register action 'lua.%s' more than once. " |
| 7722 | "This will become a hard error in version 2.5.\n", name); |
| 7723 | } |
| 7724 | fcn->function_ref[hlua_state_id] = ref; |
| 7725 | |
| 7726 | /* pop the environment string. */ |
| 7727 | lua_pop(L, 1); |
| 7728 | continue; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 7729 | } |
| 7730 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7731 | /* Check required environment. Only accepted "http" or "tcp". */ |
| 7732 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7733 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7734 | if (!akl) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 7735 | goto alloc_error;; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 7736 | fcn = new_hlua_function(); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7737 | if (!fcn) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 7738 | goto alloc_error; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7739 | |
| 7740 | /* Fill fcn. */ |
| 7741 | fcn->name = strdup(name); |
| 7742 | if (!fcn->name) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 7743 | goto alloc_error; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 7744 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7745 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 7746 | /* Set the expected number of arguments. */ |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7747 | fcn->nargs = nargs; |
| 7748 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7749 | /* List head */ |
| 7750 | akl->list.n = akl->list.p = NULL; |
| 7751 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7752 | /* action keyword. */ |
| 7753 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7754 | akl->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7755 | if (!akl->kw[0].kw) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 7756 | goto alloc_error; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7757 | |
| 7758 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 7759 | |
Amaury Denoyelle | e4a617c | 2021-05-06 15:33:09 +0200 | [diff] [blame] | 7760 | akl->kw[0].flags = 0; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7761 | akl->kw[0].private = fcn; |
| 7762 | akl->kw[0].parse = action_register_lua; |
| 7763 | |
| 7764 | /* select the action registering point. */ |
| 7765 | if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) |
| 7766 | tcp_req_cont_keywords_register(akl); |
| 7767 | else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) |
| 7768 | tcp_res_cont_keywords_register(akl); |
| 7769 | else if (strcmp(lua_tostring(L, -1), "http-req") == 0) |
| 7770 | http_req_keywords_register(akl); |
| 7771 | else if (strcmp(lua_tostring(L, -1), "http-res") == 0) |
| 7772 | http_res_keywords_register(akl); |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 7773 | else { |
| 7774 | release_hlua_function(fcn); |
| 7775 | if (akl) |
| 7776 | ha_free((char **)&(akl->kw[0].kw)); |
| 7777 | ha_free(&akl); |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7778 | WILL_LJMP(luaL_error(L, "Lua action environment '%s' is unknown. " |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7779 | "'tcp-req', 'tcp-res', 'http-req' or 'http-res' " |
| 7780 | "are expected.", lua_tostring(L, -1))); |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 7781 | } |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7782 | |
| 7783 | /* pop the environment string. */ |
| 7784 | lua_pop(L, 1); |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 7785 | |
| 7786 | /* reset for next loop */ |
| 7787 | akl = NULL; |
| 7788 | fcn = NULL; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7789 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7790 | return ACT_RET_PRS_OK; |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 7791 | |
| 7792 | alloc_error: |
| 7793 | release_hlua_function(fcn); |
| 7794 | ha_free(&akl); |
| 7795 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 7796 | return 0; /* Never reached */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7797 | } |
| 7798 | |
| 7799 | static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px, |
| 7800 | struct act_rule *rule, char **err) |
| 7801 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7802 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7803 | |
Christopher Faulet | 280f85b | 2019-07-15 15:02:04 +0200 | [diff] [blame] | 7804 | if (px->mode == PR_MODE_HTTP) { |
| 7805 | memprintf(err, "Lua TCP services cannot be used on HTTP proxies"); |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 7806 | return ACT_RET_PRS_ERR; |
| 7807 | } |
| 7808 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7809 | /* Memory for the rule. */ |
| 7810 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 7811 | if (!rule->arg.hlua_rule) { |
| 7812 | memprintf(err, "out of memory error"); |
| 7813 | return ACT_RET_PRS_ERR; |
| 7814 | } |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7815 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7816 | /* Reference the Lua function and store the reference. */ |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7817 | rule->arg.hlua_rule->fcn = fcn; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7818 | |
| 7819 | /* TODO: later accept arguments. */ |
| 7820 | rule->arg.hlua_rule->args = NULL; |
| 7821 | |
| 7822 | /* Add applet pointer in the rule. */ |
| 7823 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 7824 | rule->applet.name = fcn->name; |
| 7825 | rule->applet.init = hlua_applet_tcp_init; |
| 7826 | rule->applet.fct = hlua_applet_tcp_fct; |
| 7827 | rule->applet.release = hlua_applet_tcp_release; |
| 7828 | rule->applet.timeout = hlua_timeout_applet; |
| 7829 | |
| 7830 | return 0; |
| 7831 | } |
| 7832 | |
| 7833 | /* This function is an LUA binding used for registering |
| 7834 | * "sample-conv" functions. It expects a converter name used |
| 7835 | * in the haproxy configuration file, and an LUA function. |
| 7836 | */ |
| 7837 | __LJMP static int hlua_register_service(lua_State *L) |
| 7838 | { |
| 7839 | struct action_kw_list *akl; |
| 7840 | const char *name; |
| 7841 | const char *env; |
| 7842 | int ref; |
| 7843 | int len; |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 7844 | struct hlua_function *fcn = NULL; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 7845 | struct buffer *trash; |
| 7846 | struct action_kw *akw; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7847 | |
| 7848 | MAY_LJMP(check_args(L, 3, "register_service")); |
| 7849 | |
| 7850 | /* First argument : converter name. */ |
| 7851 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 7852 | |
| 7853 | /* Second argument : environment. */ |
| 7854 | env = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7855 | |
| 7856 | /* Third argument : lua function. */ |
| 7857 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 7858 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 7859 | /* Check for service already registered */ |
| 7860 | trash = get_trash_chunk(); |
| 7861 | chunk_printf(trash, "lua.%s", name); |
| 7862 | akw = service_find(trash->area); |
| 7863 | if (akw != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 7864 | fcn = akw->private; |
| 7865 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 7866 | ha_warning("Trying to register service 'lua.%s' more than once. " |
| 7867 | "This will become a hard error in version 2.5.\n", name); |
| 7868 | } |
| 7869 | fcn->function_ref[hlua_state_id] = ref; |
| 7870 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 7871 | } |
| 7872 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7873 | /* Allocate and fill the sample fetch keyword struct. */ |
| 7874 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
| 7875 | if (!akl) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 7876 | goto alloc_error; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 7877 | fcn = new_hlua_function(); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7878 | if (!fcn) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 7879 | goto alloc_error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7880 | |
| 7881 | /* Fill fcn. */ |
| 7882 | len = strlen("<lua.>") + strlen(name) + 1; |
| 7883 | fcn->name = calloc(1, len); |
| 7884 | if (!fcn->name) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 7885 | goto alloc_error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7886 | snprintf((char *)fcn->name, len, "<lua.%s>", name); |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 7887 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7888 | |
| 7889 | /* List head */ |
| 7890 | akl->list.n = akl->list.p = NULL; |
| 7891 | |
| 7892 | /* converter keyword. */ |
| 7893 | len = strlen("lua.") + strlen(name) + 1; |
| 7894 | akl->kw[0].kw = calloc(1, len); |
| 7895 | if (!akl->kw[0].kw) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 7896 | goto alloc_error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7897 | |
| 7898 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 7899 | |
Thierry FOURNIER / OZON.IO | 02564fd | 2016-11-12 11:07:05 +0100 | [diff] [blame] | 7900 | /* Check required environment. Only accepted "http" or "tcp". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7901 | if (strcmp(env, "tcp") == 0) |
| 7902 | akl->kw[0].parse = action_register_service_tcp; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7903 | else if (strcmp(env, "http") == 0) |
| 7904 | akl->kw[0].parse = action_register_service_http; |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 7905 | else { |
| 7906 | release_hlua_function(fcn); |
| 7907 | if (akl) |
| 7908 | ha_free((char **)&(akl->kw[0].kw)); |
| 7909 | ha_free(&akl); |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7910 | WILL_LJMP(luaL_error(L, "Lua service environment '%s' is unknown. " |
Eric Salama | fe7456f | 2017-12-21 14:30:07 +0100 | [diff] [blame] | 7911 | "'tcp' or 'http' are expected.", env)); |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 7912 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7913 | |
Amaury Denoyelle | e4a617c | 2021-05-06 15:33:09 +0200 | [diff] [blame] | 7914 | akl->kw[0].flags = 0; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7915 | akl->kw[0].private = fcn; |
| 7916 | |
| 7917 | /* End of array. */ |
| 7918 | memset(&akl->kw[1], 0, sizeof(*akl->kw)); |
| 7919 | |
| 7920 | /* Register this new converter */ |
| 7921 | service_keywords_register(akl); |
| 7922 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7923 | return 0; |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 7924 | |
| 7925 | alloc_error: |
| 7926 | release_hlua_function(fcn); |
| 7927 | ha_free(&akl); |
| 7928 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 7929 | return 0; /* Never reached */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7930 | } |
| 7931 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7932 | /* This function initialises Lua cli handler. It copies the |
| 7933 | * arguments in the Lua stack and create channel IO objects. |
| 7934 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 7935 | 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] | 7936 | { |
| 7937 | struct hlua *hlua; |
| 7938 | struct hlua_function *fcn; |
| 7939 | int i; |
| 7940 | const char *error; |
| 7941 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7942 | fcn = private; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7943 | appctx->ctx.hlua_cli.fcn = private; |
| 7944 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7945 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7946 | if (!hlua) { |
| 7947 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 7948 | return 1; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7949 | } |
| 7950 | HLUA_INIT(hlua); |
| 7951 | appctx->ctx.hlua_cli.hlua = hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7952 | |
| 7953 | /* Create task used by signal to wakeup applets. |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 7954 | * 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] | 7955 | * applet_http. It is absolutely compatible. |
| 7956 | */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 7957 | appctx->ctx.hlua_cli.task = task_new(tid_bit); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7958 | if (!appctx->ctx.hlua_cli.task) { |
Thierry FOURNIER | ffbf569 | 2016-12-16 11:14:06 +0100 | [diff] [blame] | 7959 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 7960 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7961 | } |
| 7962 | appctx->ctx.hlua_cli.task->nice = 0; |
| 7963 | appctx->ctx.hlua_cli.task->context = appctx; |
| 7964 | appctx->ctx.hlua_cli.task->process = hlua_applet_wakeup; |
| 7965 | |
| 7966 | /* Initialises the Lua context */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 7967 | 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] | 7968 | 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] | 7969 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7970 | } |
| 7971 | |
| 7972 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7973 | if (!SET_SAFE_LJMP(hlua)) { |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7974 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 7975 | error = lua_tostring(hlua->T, -1); |
| 7976 | else |
| 7977 | error = "critical error"; |
| 7978 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error); |
| 7979 | goto error; |
| 7980 | } |
| 7981 | |
| 7982 | /* Check stack available size. */ |
| 7983 | if (!lua_checkstack(hlua->T, 2)) { |
| 7984 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 7985 | goto error; |
| 7986 | } |
| 7987 | |
| 7988 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 7989 | 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] | 7990 | |
| 7991 | /* Once the arguments parsed, the CLI is like an AppletTCP, |
| 7992 | * so push AppletTCP in the stack. |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7993 | */ |
| 7994 | if (!hlua_applet_tcp_new(hlua->T, appctx)) { |
| 7995 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 7996 | goto error; |
| 7997 | } |
| 7998 | hlua->nargs = 1; |
| 7999 | |
| 8000 | /* push keywords in the stack. */ |
| 8001 | for (i = 0; *args[i]; i++) { |
| 8002 | /* Check stack available size. */ |
| 8003 | if (!lua_checkstack(hlua->T, 1)) { |
| 8004 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 8005 | goto error; |
| 8006 | } |
| 8007 | lua_pushstring(hlua->T, args[i]); |
| 8008 | hlua->nargs++; |
| 8009 | } |
| 8010 | |
| 8011 | /* We must initialize the execution timeouts. */ |
| 8012 | hlua->max_time = hlua_timeout_session; |
| 8013 | |
| 8014 | /* At this point the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8015 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8016 | |
| 8017 | /* It's ok */ |
| 8018 | return 0; |
| 8019 | |
| 8020 | /* It's not ok. */ |
| 8021 | error: |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8022 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8023 | hlua_ctx_destroy(hlua); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 8024 | appctx->ctx.hlua_cli.hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8025 | return 1; |
| 8026 | } |
| 8027 | |
| 8028 | static int hlua_cli_io_handler_fct(struct appctx *appctx) |
| 8029 | { |
| 8030 | struct hlua *hlua; |
| 8031 | struct stream_interface *si; |
| 8032 | struct hlua_function *fcn; |
| 8033 | |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 8034 | hlua = appctx->ctx.hlua_cli.hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8035 | si = appctx->owner; |
Willy Tarreau | 8ae4f75 | 2016-12-14 15:41:45 +0100 | [diff] [blame] | 8036 | fcn = appctx->ctx.hlua_cli.fcn; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8037 | |
| 8038 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 8039 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 8040 | return 1; |
| 8041 | |
| 8042 | /* Execute the function. */ |
| 8043 | switch (hlua_ctx_resume(hlua, 1)) { |
| 8044 | |
| 8045 | /* finished. */ |
| 8046 | case HLUA_E_OK: |
| 8047 | return 1; |
| 8048 | |
| 8049 | /* yield. */ |
| 8050 | case HLUA_E_AGAIN: |
| 8051 | /* We want write. */ |
| 8052 | if (HLUA_IS_WAKERESWR(hlua)) |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 8053 | si_rx_room_blk(si); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8054 | /* Set the timeout. */ |
| 8055 | if (hlua->wake_time != TICK_ETERNITY) |
| 8056 | task_schedule(hlua->task, hlua->wake_time); |
| 8057 | return 0; |
| 8058 | |
| 8059 | /* finished with error. */ |
| 8060 | case HLUA_E_ERRMSG: |
| 8061 | /* Display log. */ |
| 8062 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", |
| 8063 | fcn->name, lua_tostring(hlua->T, -1)); |
| 8064 | lua_pop(hlua->T, 1); |
| 8065 | return 1; |
| 8066 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 8067 | case HLUA_E_ETMOUT: |
| 8068 | SEND_ERR(NULL, "Lua converter '%s': execution timeout.\n", |
| 8069 | fcn->name); |
| 8070 | return 1; |
| 8071 | |
| 8072 | case HLUA_E_NOMEM: |
| 8073 | SEND_ERR(NULL, "Lua converter '%s': out of memory error.\n", |
| 8074 | fcn->name); |
| 8075 | return 1; |
| 8076 | |
| 8077 | case HLUA_E_YIELD: /* unexpected */ |
| 8078 | SEND_ERR(NULL, "Lua converter '%s': yield not allowed.\n", |
| 8079 | fcn->name); |
| 8080 | return 1; |
| 8081 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8082 | case HLUA_E_ERR: |
| 8083 | /* Display log. */ |
| 8084 | SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n", |
| 8085 | fcn->name); |
| 8086 | return 1; |
| 8087 | |
| 8088 | default: |
| 8089 | return 1; |
| 8090 | } |
| 8091 | |
| 8092 | return 1; |
| 8093 | } |
| 8094 | |
| 8095 | static void hlua_cli_io_release_fct(struct appctx *appctx) |
| 8096 | { |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 8097 | hlua_ctx_destroy(appctx->ctx.hlua_cli.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 8098 | appctx->ctx.hlua_cli.hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8099 | } |
| 8100 | |
| 8101 | /* This function is an LUA binding used for registering |
| 8102 | * new keywords in the cli. It expects a list of keywords |
| 8103 | * which are the "path". It is limited to 5 keywords. A |
| 8104 | * description of the command, a function to be executed |
| 8105 | * for the parsing and a function for io handlers. |
| 8106 | */ |
| 8107 | __LJMP static int hlua_register_cli(lua_State *L) |
| 8108 | { |
| 8109 | struct cli_kw_list *cli_kws; |
| 8110 | const char *message; |
| 8111 | int ref_io; |
| 8112 | int len; |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 8113 | struct hlua_function *fcn = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8114 | int index; |
| 8115 | int i; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8116 | struct buffer *trash; |
| 8117 | const char *kw[5]; |
| 8118 | struct cli_kw *cli_kw; |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 8119 | const char *errmsg; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8120 | |
| 8121 | MAY_LJMP(check_args(L, 3, "register_cli")); |
| 8122 | |
| 8123 | /* First argument : an array of maximum 5 keywords. */ |
| 8124 | if (!lua_istable(L, 1)) |
| 8125 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table")); |
| 8126 | |
| 8127 | /* Second argument : string with contextual message. */ |
| 8128 | message = MAY_LJMP(luaL_checkstring(L, 2)); |
| 8129 | |
| 8130 | /* Third and fourth argument : lua function. */ |
| 8131 | ref_io = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 8132 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8133 | /* Check for CLI service already registered */ |
| 8134 | trash = get_trash_chunk(); |
| 8135 | index = 0; |
| 8136 | lua_pushnil(L); |
| 8137 | memset(kw, 0, sizeof(kw)); |
| 8138 | while (lua_next(L, 1) != 0) { |
| 8139 | if (index >= CLI_PREFIX_KW_NB) |
| 8140 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries")); |
| 8141 | if (lua_type(L, -1) != LUA_TSTRING) |
| 8142 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings")); |
| 8143 | kw[index] = lua_tostring(L, -1); |
| 8144 | if (index == 0) |
| 8145 | chunk_printf(trash, "%s", kw[index]); |
| 8146 | else |
| 8147 | chunk_appendf(trash, " %s", kw[index]); |
| 8148 | index++; |
| 8149 | lua_pop(L, 1); |
| 8150 | } |
| 8151 | cli_kw = cli_find_kw_exact((char **)kw); |
| 8152 | if (cli_kw != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8153 | fcn = cli_kw->private; |
| 8154 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 8155 | ha_warning("Trying to register CLI keyword 'lua.%s' more than once. " |
| 8156 | "This will become a hard error in version 2.5.\n", trash->area); |
| 8157 | } |
| 8158 | fcn->function_ref[hlua_state_id] = ref_io; |
| 8159 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8160 | } |
| 8161 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8162 | /* Allocate and fill the sample fetch keyword struct. */ |
| 8163 | cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 8164 | if (!cli_kws) { |
| 8165 | errmsg = "Lua out of memory error."; |
| 8166 | goto error; |
| 8167 | } |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 8168 | fcn = new_hlua_function(); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 8169 | if (!fcn) { |
| 8170 | errmsg = "Lua out of memory error."; |
| 8171 | goto error; |
| 8172 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8173 | |
| 8174 | /* Fill path. */ |
| 8175 | index = 0; |
| 8176 | lua_pushnil(L); |
| 8177 | while(lua_next(L, 1) != 0) { |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 8178 | if (index >= 5) { |
| 8179 | errmsg = "1st argument must be a table with a maximum of 5 entries"; |
| 8180 | goto error; |
| 8181 | } |
| 8182 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 8183 | errmsg = "1st argument must be a table filled with strings"; |
| 8184 | goto error; |
| 8185 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8186 | cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1)); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 8187 | if (!cli_kws->kw[0].str_kw[index]) { |
| 8188 | errmsg = "Lua out of memory error."; |
| 8189 | goto error; |
| 8190 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8191 | index++; |
| 8192 | lua_pop(L, 1); |
| 8193 | } |
| 8194 | |
| 8195 | /* Copy help message. */ |
| 8196 | cli_kws->kw[0].usage = strdup(message); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 8197 | if (!cli_kws->kw[0].usage) { |
| 8198 | errmsg = "Lua out of memory error."; |
| 8199 | goto error; |
| 8200 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8201 | |
| 8202 | /* Fill fcn io handler. */ |
| 8203 | len = strlen("<lua.cli>") + 1; |
| 8204 | for (i = 0; i < index; i++) |
| 8205 | len += strlen(cli_kws->kw[0].str_kw[i]) + 1; |
| 8206 | fcn->name = calloc(1, len); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 8207 | if (!fcn->name) { |
| 8208 | errmsg = "Lua out of memory error."; |
| 8209 | goto error; |
| 8210 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8211 | strncat((char *)fcn->name, "<lua.cli", len); |
| 8212 | for (i = 0; i < index; i++) { |
| 8213 | strncat((char *)fcn->name, ".", len); |
| 8214 | strncat((char *)fcn->name, cli_kws->kw[0].str_kw[i], len); |
| 8215 | } |
| 8216 | strncat((char *)fcn->name, ">", len); |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8217 | fcn->function_ref[hlua_state_id] = ref_io; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8218 | |
| 8219 | /* Fill last entries. */ |
| 8220 | cli_kws->kw[0].private = fcn; |
| 8221 | cli_kws->kw[0].parse = hlua_cli_parse_fct; |
| 8222 | cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct; |
| 8223 | cli_kws->kw[0].io_release = hlua_cli_io_release_fct; |
| 8224 | |
| 8225 | /* Register this new converter */ |
| 8226 | cli_register_kw(cli_kws); |
| 8227 | |
| 8228 | return 0; |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 8229 | |
| 8230 | error: |
| 8231 | release_hlua_function(fcn); |
| 8232 | if (cli_kws) { |
| 8233 | for (i = 0; i < index; i++) |
| 8234 | ha_free((char **)&(cli_kws->kw[0].str_kw[i])); |
| 8235 | ha_free((char **)&(cli_kws->kw[0].usage)); |
| 8236 | } |
| 8237 | ha_free(&cli_kws); |
| 8238 | WILL_LJMP(luaL_error(L, errmsg)); |
| 8239 | return 0; /* Never reached */ |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8240 | } |
| 8241 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8242 | 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] | 8243 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8244 | char **err, unsigned int *timeout) |
| 8245 | { |
| 8246 | const char *error; |
| 8247 | |
| 8248 | error = parse_time_err(args[1], timeout, TIME_UNIT_MS); |
Willy Tarreau | 9faebe3 | 2019-06-07 19:00:37 +0200 | [diff] [blame] | 8249 | if (error == PARSE_TIME_OVER) { |
| 8250 | memprintf(err, "timer overflow in argument <%s> to <%s> (maximum value is 2147483647 ms or ~24.8 days)", |
| 8251 | args[1], args[0]); |
| 8252 | return -1; |
| 8253 | } |
| 8254 | else if (error == PARSE_TIME_UNDER) { |
| 8255 | memprintf(err, "timer underflow in argument <%s> to <%s> (minimum non-null value is 1 ms)", |
| 8256 | args[1], args[0]); |
| 8257 | return -1; |
| 8258 | } |
| 8259 | else if (error) { |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8260 | memprintf(err, "%s: invalid timeout", args[0]); |
| 8261 | return -1; |
| 8262 | } |
| 8263 | return 0; |
| 8264 | } |
| 8265 | |
| 8266 | 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] | 8267 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8268 | char **err) |
| 8269 | { |
| 8270 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 8271 | file, line, err, &hlua_timeout_session); |
| 8272 | } |
| 8273 | |
| 8274 | 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] | 8275 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8276 | char **err) |
| 8277 | { |
| 8278 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 8279 | file, line, err, &hlua_timeout_task); |
| 8280 | } |
| 8281 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8282 | 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] | 8283 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8284 | char **err) |
| 8285 | { |
| 8286 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 8287 | file, line, err, &hlua_timeout_applet); |
| 8288 | } |
| 8289 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 8290 | 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] | 8291 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 8292 | char **err) |
| 8293 | { |
| 8294 | char *error; |
| 8295 | |
| 8296 | hlua_nb_instruction = strtoll(args[1], &error, 10); |
| 8297 | if (*error != '\0') { |
| 8298 | memprintf(err, "%s: invalid number", args[0]); |
| 8299 | return -1; |
| 8300 | } |
| 8301 | return 0; |
| 8302 | } |
| 8303 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8304 | 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] | 8305 | const struct proxy *defpx, const char *file, int line, |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8306 | char **err) |
| 8307 | { |
| 8308 | char *error; |
| 8309 | |
| 8310 | if (*(args[1]) == 0) { |
| 8311 | memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]); |
| 8312 | return -1; |
| 8313 | } |
| 8314 | hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L; |
| 8315 | if (*error != '\0') { |
| 8316 | memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error); |
| 8317 | return -1; |
| 8318 | } |
| 8319 | return 0; |
| 8320 | } |
| 8321 | |
| 8322 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8323 | /* This function is called by the main configuration key "lua-load". It loads and |
| 8324 | * execute an lua file during the parsing of the HAProxy configuration file. It is |
| 8325 | * the main lua entry point. |
| 8326 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 8327 | * This function runs with the HAProxy keywords API. It returns -1 if an error |
| 8328 | * occurs, otherwise it returns 0. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8329 | * |
| 8330 | * In some error case, LUA set an error message in top of the stack. This function |
| 8331 | * 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] | 8332 | * |
| 8333 | * This function can fail with an abort() due to an Lua critical error. |
| 8334 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 8335 | * tolerated. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8336 | */ |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8337 | static int hlua_load_state(char *filename, lua_State *L, char **err) |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8338 | { |
| 8339 | int error; |
| 8340 | |
| 8341 | /* Just load and compile the file. */ |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8342 | error = luaL_loadfile(L, filename); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8343 | if (error) { |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8344 | memprintf(err, "error in Lua file '%s': %s", filename, lua_tostring(L, -1)); |
| 8345 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8346 | return -1; |
| 8347 | } |
| 8348 | |
| 8349 | /* If no syntax error where detected, execute the code. */ |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8350 | error = lua_pcall(L, 0, LUA_MULTRET, 0); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8351 | switch (error) { |
| 8352 | case LUA_OK: |
| 8353 | break; |
| 8354 | case LUA_ERRRUN: |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8355 | memprintf(err, "Lua runtime error: %s\n", lua_tostring(L, -1)); |
| 8356 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8357 | return -1; |
| 8358 | case LUA_ERRMEM: |
Thierry Fournier | de6145f | 2020-11-29 00:55:53 +0100 | [diff] [blame] | 8359 | memprintf(err, "Lua out of memory error\n"); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8360 | return -1; |
| 8361 | case LUA_ERRERR: |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8362 | memprintf(err, "Lua message handler error: %s\n", lua_tostring(L, -1)); |
| 8363 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8364 | return -1; |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 8365 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM <= 503 |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8366 | case LUA_ERRGCMM: |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8367 | memprintf(err, "Lua garbage collector error: %s\n", lua_tostring(L, -1)); |
| 8368 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8369 | return -1; |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 8370 | #endif |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8371 | default: |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8372 | memprintf(err, "Lua unknown error: %s\n", lua_tostring(L, -1)); |
| 8373 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8374 | return -1; |
| 8375 | } |
| 8376 | |
| 8377 | return 0; |
| 8378 | } |
| 8379 | |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8380 | static int hlua_load(char **args, int section_type, struct proxy *curpx, |
Willy Tarreau | 0182516 | 2021-03-09 09:53:46 +0100 | [diff] [blame] | 8381 | const struct proxy *defpx, const char *file, int line, |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8382 | char **err) |
| 8383 | { |
| 8384 | if (*(args[1]) == 0) { |
| 8385 | memprintf(err, "'%s' expects a file name as parameter.\n", args[0]); |
| 8386 | return -1; |
| 8387 | } |
| 8388 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8389 | /* loading for global state */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8390 | hlua_state_id = 0; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8391 | ha_set_tid(0); |
Thierry Fournier | afc63e2 | 2020-11-28 17:06:51 +0100 | [diff] [blame] | 8392 | return hlua_load_state(args[1], hlua_states[0], err); |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8393 | } |
| 8394 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8395 | 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] | 8396 | const struct proxy *defpx, const char *file, int line, |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8397 | char **err) |
| 8398 | { |
| 8399 | int len; |
| 8400 | |
| 8401 | if (*(args[1]) == 0) { |
| 8402 | memprintf(err, "'%s' expects a file as parameter.\n", args[0]); |
| 8403 | return -1; |
| 8404 | } |
| 8405 | |
| 8406 | if (per_thread_load == NULL) { |
| 8407 | /* allocate the first entry large enough to store the final NULL */ |
| 8408 | per_thread_load = calloc(1, sizeof(*per_thread_load)); |
| 8409 | if (per_thread_load == NULL) { |
| 8410 | memprintf(err, "out of memory error"); |
| 8411 | return -1; |
| 8412 | } |
| 8413 | } |
| 8414 | |
| 8415 | /* count used entries */ |
| 8416 | for (len = 0; per_thread_load[len] != NULL; len++) |
| 8417 | ; |
| 8418 | |
| 8419 | per_thread_load = realloc(per_thread_load, (len + 2) * sizeof(*per_thread_load)); |
| 8420 | if (per_thread_load == NULL) { |
| 8421 | memprintf(err, "out of memory error"); |
| 8422 | return -1; |
| 8423 | } |
| 8424 | |
| 8425 | per_thread_load[len] = strdup(args[1]); |
| 8426 | per_thread_load[len + 1] = NULL; |
| 8427 | |
| 8428 | if (per_thread_load[len] == NULL) { |
| 8429 | memprintf(err, "out of memory error"); |
| 8430 | return -1; |
| 8431 | } |
| 8432 | |
| 8433 | /* loading for thread 1 only */ |
| 8434 | hlua_state_id = 1; |
| 8435 | ha_set_tid(0); |
| 8436 | return hlua_load_state(args[1], hlua_states[1], err); |
| 8437 | } |
| 8438 | |
Tim Duesterhus | c9fc9f2 | 2020-01-12 13:55:39 +0100 | [diff] [blame] | 8439 | /* Prepend the given <path> followed by a semicolon to the `package.<type>` variable |
| 8440 | * in the given <ctx>. |
| 8441 | */ |
Thierry Fournier | 3fb9e51 | 2020-11-28 10:13:12 +0100 | [diff] [blame] | 8442 | static int hlua_prepend_path(lua_State *L, char *type, char *path) |
Tim Duesterhus | c9fc9f2 | 2020-01-12 13:55:39 +0100 | [diff] [blame] | 8443 | { |
Thierry Fournier | 3fb9e51 | 2020-11-28 10:13:12 +0100 | [diff] [blame] | 8444 | lua_getglobal(L, "package"); /* push package variable */ |
| 8445 | lua_pushstring(L, path); /* push given path */ |
| 8446 | lua_pushstring(L, ";"); /* push semicolon */ |
| 8447 | lua_getfield(L, -3, type); /* push old path */ |
| 8448 | lua_concat(L, 3); /* concatenate to new path */ |
| 8449 | lua_setfield(L, -2, type); /* store new path */ |
| 8450 | lua_pop(L, 1); /* pop package variable */ |
Tim Duesterhus | c9fc9f2 | 2020-01-12 13:55:39 +0100 | [diff] [blame] | 8451 | |
| 8452 | return 0; |
| 8453 | } |
| 8454 | |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8455 | 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] | 8456 | const struct proxy *defpx, const char *file, int line, |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8457 | char **err) |
| 8458 | { |
| 8459 | char *path; |
| 8460 | char *type = "path"; |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 8461 | struct prepend_path *p = NULL; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8462 | |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8463 | if (too_many_args(2, args, err, NULL)) { |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 8464 | goto err; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8465 | } |
| 8466 | |
| 8467 | if (!(*args[1])) { |
| 8468 | memprintf(err, "'%s' expects to receive a <path> as argument", args[0]); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 8469 | goto err; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8470 | } |
| 8471 | path = args[1]; |
| 8472 | |
| 8473 | if (*args[2]) { |
| 8474 | if (strcmp(args[2], "path") != 0 && strcmp(args[2], "cpath") != 0) { |
| 8475 | 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] | 8476 | goto err; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8477 | } |
| 8478 | type = args[2]; |
| 8479 | } |
| 8480 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8481 | p = calloc(1, sizeof(*p)); |
| 8482 | if (p == NULL) { |
Tim Duesterhus | f89d43a | 2021-01-03 20:04:37 +0100 | [diff] [blame] | 8483 | memprintf(err, "memory allocation failed"); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 8484 | goto err; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8485 | } |
| 8486 | p->path = strdup(path); |
| 8487 | if (p->path == NULL) { |
Tim Duesterhus | f89d43a | 2021-01-03 20:04:37 +0100 | [diff] [blame] | 8488 | memprintf(err, "memory allocation failed"); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 8489 | goto err2; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8490 | } |
| 8491 | p->type = strdup(type); |
| 8492 | if (p->type == NULL) { |
Tim Duesterhus | f89d43a | 2021-01-03 20:04:37 +0100 | [diff] [blame] | 8493 | memprintf(err, "memory allocation failed"); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 8494 | goto err2; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8495 | } |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 8496 | LIST_APPEND(&prepend_path_list, &p->l); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8497 | |
| 8498 | hlua_prepend_path(hlua_states[0], type, path); |
| 8499 | hlua_prepend_path(hlua_states[1], type, path); |
| 8500 | return 0; |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 8501 | |
| 8502 | err2: |
| 8503 | free(p->type); |
| 8504 | free(p->path); |
| 8505 | err: |
| 8506 | free(p); |
| 8507 | return -1; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8508 | } |
| 8509 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8510 | /* configuration keywords declaration */ |
| 8511 | static struct cfg_kw_list cfg_kws = {{ },{ |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8512 | { CFG_GLOBAL, "lua-prepend-path", hlua_config_prepend_path }, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8513 | { CFG_GLOBAL, "lua-load", hlua_load }, |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8514 | { CFG_GLOBAL, "lua-load-per-thread", hlua_load_per_thread }, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8515 | { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout }, |
| 8516 | { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout }, |
Thierry FOURNIER | 56da101 | 2015-10-01 08:42:31 +0200 | [diff] [blame] | 8517 | { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout }, |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 8518 | { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield }, |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8519 | { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem }, |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8520 | { 0, NULL, NULL }, |
| 8521 | }}; |
| 8522 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 8523 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |
| 8524 | |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 8525 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8526 | /* This function can fail with an abort() due to an Lua critical error. |
| 8527 | * We are in the initialisation process of HAProxy, this abort() is |
| 8528 | * tolerated. |
| 8529 | */ |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8530 | int hlua_post_init_state(lua_State *L) |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8531 | { |
| 8532 | struct hlua_init_function *init; |
| 8533 | const char *msg; |
| 8534 | enum hlua_exec ret; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8535 | const char *error; |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8536 | const char *kind; |
| 8537 | const char *trace; |
| 8538 | int return_status = 1; |
| 8539 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
| 8540 | int nres; |
| 8541 | #endif |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8542 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 8543 | /* disable memory limit checks if limit is not set */ |
| 8544 | if (!hlua_global_allocator.limit) |
| 8545 | hlua_global_allocator.limit = ~hlua_global_allocator.limit; |
| 8546 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 8547 | /* Call post initialisation function in safe environment. */ |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 8548 | if (setjmp(safe_ljmp_env) != 0) { |
| 8549 | lua_atpanic(L, hlua_panic_safe); |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8550 | if (lua_type(L, -1) == LUA_TSTRING) |
| 8551 | error = lua_tostring(L, -1); |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 8552 | else |
| 8553 | error = "critical error"; |
| 8554 | fprintf(stderr, "Lua post-init: %s.\n", error); |
| 8555 | exit(1); |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 8556 | } else { |
| 8557 | lua_atpanic(L, hlua_panic_ljmp); |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 8558 | } |
Frédéric Lécaille | 54f2bcf | 2018-08-29 13:46:24 +0200 | [diff] [blame] | 8559 | |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8560 | hlua_fcn_post_init(L); |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 8561 | |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8562 | list_for_each_entry(init, &hlua_init_functions[hlua_state_id], l) { |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8563 | lua_rawgeti(L, LUA_REGISTRYINDEX, init->function_ref); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8564 | |
| 8565 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8566 | ret = lua_resume(L, L, 0, &nres); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8567 | #else |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8568 | ret = lua_resume(L, L, 0); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8569 | #endif |
| 8570 | kind = NULL; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8571 | switch (ret) { |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8572 | |
| 8573 | case LUA_OK: |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8574 | lua_pop(L, -1); |
Thierry Fournier | 13d08b7 | 2020-11-28 11:02:58 +0100 | [diff] [blame] | 8575 | break; |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8576 | |
| 8577 | case LUA_ERRERR: |
| 8578 | kind = "message handler error"; |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 8579 | /* Fall through */ |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8580 | case LUA_ERRRUN: |
| 8581 | if (!kind) |
| 8582 | kind = "runtime error"; |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8583 | msg = lua_tostring(L, -1); |
| 8584 | lua_settop(L, 0); /* Empty the stack. */ |
| 8585 | lua_pop(L, 1); |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 8586 | trace = hlua_traceback(L, ", "); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8587 | if (msg) |
| 8588 | ha_alert("Lua init: %s: '%s' from %s\n", kind, msg, trace); |
| 8589 | else |
| 8590 | ha_alert("Lua init: unknown %s from %s\n", kind, trace); |
| 8591 | return_status = 0; |
| 8592 | break; |
| 8593 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8594 | default: |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8595 | /* Unknown error */ |
| 8596 | kind = "Unknown error"; |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 8597 | /* Fall through */ |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8598 | case LUA_YIELD: |
| 8599 | /* yield is not configured at this step, this state doesn't happen */ |
| 8600 | if (!kind) |
| 8601 | kind = "yield not allowed"; |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 8602 | /* Fall through */ |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8603 | case LUA_ERRMEM: |
| 8604 | if (!kind) |
| 8605 | kind = "out of memory error"; |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8606 | lua_settop(L, 0); |
| 8607 | lua_pop(L, 1); |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 8608 | trace = hlua_traceback(L, ", "); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8609 | ha_alert("Lua init: %s: %s\n", kind, trace); |
| 8610 | return_status = 0; |
| 8611 | break; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8612 | } |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8613 | if (!return_status) |
| 8614 | break; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8615 | } |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 8616 | |
| 8617 | lua_atpanic(L, hlua_panic_safe); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8618 | return return_status; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8619 | } |
| 8620 | |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8621 | int hlua_post_init() |
| 8622 | { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8623 | int ret; |
| 8624 | int i; |
| 8625 | int errors; |
| 8626 | char *err = NULL; |
| 8627 | struct hlua_function *fcn; |
| 8628 | |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8629 | #if USE_OPENSSL |
| 8630 | /* Initialize SSL server. */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 8631 | if (socket_ssl->xprt->prepare_srv) { |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8632 | int saved_used_backed = global.ssl_used_backend; |
| 8633 | // don't affect maxconn automatic computation |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 8634 | socket_ssl->xprt->prepare_srv(socket_ssl); |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8635 | global.ssl_used_backend = saved_used_backed; |
| 8636 | } |
| 8637 | #endif |
| 8638 | |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8639 | /* Perform post init of common thread */ |
| 8640 | hlua_state_id = 0; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8641 | ha_set_tid(0); |
| 8642 | ret = hlua_post_init_state(hlua_states[hlua_state_id]); |
| 8643 | if (ret == 0) |
| 8644 | return 0; |
| 8645 | |
| 8646 | /* init remaining lua states and load files */ |
| 8647 | for (hlua_state_id = 2; hlua_state_id < global.nbthread + 1; hlua_state_id++) { |
| 8648 | |
| 8649 | /* set thread context */ |
| 8650 | ha_set_tid(hlua_state_id - 1); |
| 8651 | |
| 8652 | /* Init lua state */ |
| 8653 | hlua_states[hlua_state_id] = hlua_init_state(hlua_state_id); |
| 8654 | |
| 8655 | /* Load lua files */ |
| 8656 | for (i = 0; per_thread_load && per_thread_load[i]; i++) { |
| 8657 | ret = hlua_load_state(per_thread_load[i], hlua_states[hlua_state_id], &err); |
| 8658 | if (ret != 0) { |
| 8659 | ha_alert("Lua init: %s\n", err); |
| 8660 | return 0; |
| 8661 | } |
| 8662 | } |
| 8663 | } |
| 8664 | |
| 8665 | /* Reset thread context */ |
| 8666 | ha_set_tid(0); |
| 8667 | |
| 8668 | /* Execute post init for all states */ |
| 8669 | for (hlua_state_id = 1; hlua_state_id < global.nbthread + 1; hlua_state_id++) { |
| 8670 | |
| 8671 | /* set thread context */ |
| 8672 | ha_set_tid(hlua_state_id - 1); |
| 8673 | |
| 8674 | /* run post init */ |
| 8675 | ret = hlua_post_init_state(hlua_states[hlua_state_id]); |
| 8676 | if (ret == 0) |
| 8677 | return 0; |
| 8678 | } |
| 8679 | |
| 8680 | /* Reset thread context */ |
| 8681 | ha_set_tid(0); |
| 8682 | |
| 8683 | /* control functions registering. Each function must have: |
| 8684 | * - only the function_ref[0] set positive and all other to -1 |
| 8685 | * - only the function_ref[0] set to -1 and all other positive |
| 8686 | * This ensure a same reference is not used both in shared |
| 8687 | * lua state and thread dedicated lua state. Note: is the case |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 8688 | * reach, the shared state is priority, but the bug will be |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8689 | * complicated to found for the end user. |
| 8690 | */ |
| 8691 | errors = 0; |
| 8692 | list_for_each_entry(fcn, &referenced_functions, l) { |
| 8693 | ret = 0; |
| 8694 | for (i = 1; i < global.nbthread + 1; i++) { |
| 8695 | if (fcn->function_ref[i] == -1) |
| 8696 | ret--; |
| 8697 | else |
| 8698 | ret++; |
| 8699 | } |
| 8700 | if (abs(ret) != global.nbthread) { |
| 8701 | ha_alert("Lua function '%s' is not referenced in all thread. " |
| 8702 | "Expect function in all thread or in none thread.\n", fcn->name); |
| 8703 | errors++; |
| 8704 | continue; |
| 8705 | } |
| 8706 | |
| 8707 | if ((fcn->function_ref[0] == -1) == (ret < 0)) { |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 8708 | ha_alert("Lua function '%s' is referenced both ins shared Lua context (through lua-load) " |
| 8709 | "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] | 8710 | "exclusive.\n", fcn->name); |
| 8711 | errors++; |
| 8712 | } |
| 8713 | } |
| 8714 | |
| 8715 | if (errors > 0) |
| 8716 | return 0; |
| 8717 | |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 8718 | /* 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] | 8719 | * -1 in order to have probably a segfault if someone use it |
| 8720 | */ |
| 8721 | hlua_state_id = -1; |
| 8722 | |
| 8723 | return 1; |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8724 | } |
| 8725 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8726 | /* The memory allocator used by the Lua stack. <ud> is a pointer to the |
| 8727 | * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize> |
| 8728 | * 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] | 8729 | * allocation. <nsize> is the requested new size. A new allocation is |
| 8730 | * indicated by <ptr> being NULL. A free is indicated by <nsize> being |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 8731 | * zero. This one verifies that the limits are respected but is optimized |
| 8732 | * for the fast case where limits are not used, hence stats are not updated. |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8733 | */ |
| 8734 | static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize) |
| 8735 | { |
| 8736 | struct hlua_mem_allocator *zone = ud; |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 8737 | size_t limit, old, new; |
| 8738 | |
Tim Duesterhus | 2258652 | 2021-01-08 10:35:33 +0100 | [diff] [blame] | 8739 | if (unlikely(!ptr && !nsize)) |
| 8740 | return NULL; |
| 8741 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 8742 | /* a limit of ~0 means unlimited and boot complete, so there's no need |
| 8743 | * for accounting anymore. |
| 8744 | */ |
Christopher Faulet | cc2c4f8 | 2021-03-24 14:52:24 +0100 | [diff] [blame] | 8745 | if (likely(~zone->limit == 0)) |
| 8746 | return realloc(ptr, nsize); |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8747 | |
Willy Tarreau | d36c7fa | 2020-12-02 12:26:29 +0100 | [diff] [blame] | 8748 | if (!ptr) |
| 8749 | osize = 0; |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8750 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 8751 | /* enforce strict limits across all threads */ |
| 8752 | limit = zone->limit; |
| 8753 | old = _HA_ATOMIC_LOAD(&zone->allocated); |
| 8754 | do { |
| 8755 | new = old + nsize - osize; |
| 8756 | if (unlikely(nsize && limit && new > limit)) |
| 8757 | return NULL; |
| 8758 | } while (!_HA_ATOMIC_CAS(&zone->allocated, &old, new)); |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8759 | |
| 8760 | ptr = realloc(ptr, nsize); |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 8761 | |
| 8762 | if (unlikely(!ptr && nsize)) // failed |
| 8763 | _HA_ATOMIC_SUB(&zone->allocated, nsize - osize); |
| 8764 | |
| 8765 | __ha_barrier_atomic_store(); |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8766 | return ptr; |
| 8767 | } |
| 8768 | |
Thierry Fournier | ecb83c2 | 2020-11-28 15:49:44 +0100 | [diff] [blame] | 8769 | /* 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] | 8770 | * We are in the initialisation process of HAProxy, this abort() is |
| 8771 | * tolerated. |
| 8772 | */ |
Thierry Fournier | ecb83c2 | 2020-11-28 15:49:44 +0100 | [diff] [blame] | 8773 | lua_State *hlua_init_state(int thread_num) |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 8774 | { |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8775 | int i; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8776 | int idx; |
| 8777 | struct sample_fetch *sf; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8778 | struct sample_conv *sc; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8779 | char *p; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8780 | const char *error_msg; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 8781 | void **context; |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8782 | lua_State *L; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8783 | struct prepend_path *pp; |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8784 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 8785 | /* Init main lua stack. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8786 | L = lua_newstate(hlua_alloc, &hlua_global_allocator); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 8787 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 8788 | /* Initialise Lua context to NULL */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8789 | context = lua_getextraspace(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 8790 | *context = NULL; |
| 8791 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 8792 | /* From this point, until the end of the initialisation function, |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8793 | * the Lua function can fail with an abort. We are in the initialisation |
| 8794 | * process of HAProxy, this abort() is tolerated. |
| 8795 | */ |
| 8796 | |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 8797 | /* Call post initialisation function in safe environment. */ |
| 8798 | if (setjmp(safe_ljmp_env) != 0) { |
| 8799 | lua_atpanic(L, hlua_panic_safe); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8800 | if (lua_type(L, -1) == LUA_TSTRING) |
| 8801 | error_msg = lua_tostring(L, -1); |
Thierry Fournier | 2f05cc6 | 2020-11-28 16:08:02 +0100 | [diff] [blame] | 8802 | else |
| 8803 | error_msg = "critical error"; |
| 8804 | fprintf(stderr, "Lua init: %s.\n", error_msg); |
| 8805 | exit(1); |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 8806 | } else { |
| 8807 | lua_atpanic(L, hlua_panic_ljmp); |
Thierry Fournier | 2f05cc6 | 2020-11-28 16:08:02 +0100 | [diff] [blame] | 8808 | } |
| 8809 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 8810 | /* Initialise lua. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8811 | luaL_openlibs(L); |
Tim Duesterhus | 541fe1e | 2020-01-12 13:55:41 +0100 | [diff] [blame] | 8812 | #define HLUA_PREPEND_PATH_TOSTRING1(x) #x |
| 8813 | #define HLUA_PREPEND_PATH_TOSTRING(x) HLUA_PREPEND_PATH_TOSTRING1(x) |
| 8814 | #ifdef HLUA_PREPEND_PATH |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8815 | hlua_prepend_path(L, "path", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_PATH)); |
Tim Duesterhus | 541fe1e | 2020-01-12 13:55:41 +0100 | [diff] [blame] | 8816 | #endif |
| 8817 | #ifdef HLUA_PREPEND_CPATH |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8818 | hlua_prepend_path(L, "cpath", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_CPATH)); |
Tim Duesterhus | 541fe1e | 2020-01-12 13:55:41 +0100 | [diff] [blame] | 8819 | #endif |
| 8820 | #undef HLUA_PREPEND_PATH_TOSTRING |
| 8821 | #undef HLUA_PREPEND_PATH_TOSTRING1 |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8822 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8823 | /* Apply configured prepend path */ |
| 8824 | list_for_each_entry(pp, &prepend_path_list, l) |
| 8825 | hlua_prepend_path(L, pp->type, pp->path); |
| 8826 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8827 | /* |
| 8828 | * |
| 8829 | * Create "core" object. |
| 8830 | * |
| 8831 | */ |
| 8832 | |
Thierry FOURNIER | a2d8c65 | 2015-03-11 17:29:39 +0100 | [diff] [blame] | 8833 | /* This table entry is the object "core" base. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8834 | lua_newtable(L); |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8835 | |
Thierry Fournier | ecb83c2 | 2020-11-28 15:49:44 +0100 | [diff] [blame] | 8836 | /* set the thread id */ |
| 8837 | hlua_class_const_int(L, "thread", thread_num); |
| 8838 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8839 | /* Push the loglevel constants. */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8840 | for (i = 0; i < NB_LOG_LEVELS; i++) |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8841 | hlua_class_const_int(L, log_levels[i], i); |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8842 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8843 | /* Register special functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8844 | hlua_class_function(L, "register_init", hlua_register_init); |
| 8845 | hlua_class_function(L, "register_task", hlua_register_task); |
| 8846 | hlua_class_function(L, "register_fetches", hlua_register_fetches); |
| 8847 | hlua_class_function(L, "register_converters", hlua_register_converters); |
| 8848 | hlua_class_function(L, "register_action", hlua_register_action); |
| 8849 | hlua_class_function(L, "register_service", hlua_register_service); |
| 8850 | hlua_class_function(L, "register_cli", hlua_register_cli); |
| 8851 | hlua_class_function(L, "yield", hlua_yield); |
| 8852 | hlua_class_function(L, "set_nice", hlua_set_nice); |
| 8853 | hlua_class_function(L, "sleep", hlua_sleep); |
| 8854 | hlua_class_function(L, "msleep", hlua_msleep); |
| 8855 | hlua_class_function(L, "add_acl", hlua_add_acl); |
| 8856 | hlua_class_function(L, "del_acl", hlua_del_acl); |
| 8857 | hlua_class_function(L, "set_map", hlua_set_map); |
| 8858 | hlua_class_function(L, "del_map", hlua_del_map); |
| 8859 | hlua_class_function(L, "tcp", hlua_socket_new); |
| 8860 | hlua_class_function(L, "log", hlua_log); |
| 8861 | hlua_class_function(L, "Debug", hlua_log_debug); |
| 8862 | hlua_class_function(L, "Info", hlua_log_info); |
| 8863 | hlua_class_function(L, "Warning", hlua_log_warning); |
| 8864 | hlua_class_function(L, "Alert", hlua_log_alert); |
| 8865 | hlua_class_function(L, "done", hlua_done); |
| 8866 | hlua_fcn_reg_core_fcn(L); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8867 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8868 | lua_setglobal(L, "core"); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 8869 | |
| 8870 | /* |
| 8871 | * |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 8872 | * Create "act" object. |
| 8873 | * |
| 8874 | */ |
| 8875 | |
| 8876 | /* This table entry is the object "act" base. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8877 | lua_newtable(L); |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 8878 | |
| 8879 | /* push action return constants */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8880 | hlua_class_const_int(L, "CONTINUE", ACT_RET_CONT); |
| 8881 | hlua_class_const_int(L, "STOP", ACT_RET_STOP); |
| 8882 | hlua_class_const_int(L, "YIELD", ACT_RET_YIELD); |
| 8883 | hlua_class_const_int(L, "ERROR", ACT_RET_ERR); |
| 8884 | hlua_class_const_int(L, "DONE", ACT_RET_DONE); |
| 8885 | hlua_class_const_int(L, "DENY", ACT_RET_DENY); |
| 8886 | hlua_class_const_int(L, "ABORT", ACT_RET_ABRT); |
| 8887 | hlua_class_const_int(L, "INVALID", ACT_RET_INV); |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 8888 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8889 | hlua_class_function(L, "wake_time", hlua_set_wake_time); |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 8890 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8891 | lua_setglobal(L, "act"); |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 8892 | |
| 8893 | /* |
| 8894 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8895 | * Register class Map |
| 8896 | * |
| 8897 | */ |
| 8898 | |
| 8899 | /* This table entry is the object "Map" base. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8900 | lua_newtable(L); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8901 | |
| 8902 | /* register pattern types. */ |
| 8903 | for (i=0; i<PAT_MATCH_NUM; i++) |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8904 | hlua_class_const_int(L, pat_match_names[i], i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 8905 | for (i=0; i<PAT_MATCH_NUM; i++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8906 | snprintf(trash.area, trash.size, "_%s", pat_match_names[i]); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8907 | hlua_class_const_int(L, trash.area, i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 8908 | } |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8909 | |
| 8910 | /* register constructor. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8911 | hlua_class_function(L, "new", hlua_map_new); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8912 | |
| 8913 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8914 | lua_newtable(L); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8915 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8916 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8917 | lua_pushstring(L, "__index"); |
| 8918 | lua_newtable(L); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8919 | |
| 8920 | /* Register . */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8921 | hlua_class_function(L, "lookup", hlua_map_lookup); |
| 8922 | hlua_class_function(L, "slookup", hlua_map_slookup); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8923 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8924 | lua_rawset(L, -3); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8925 | |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8926 | /* Register previous table in the registry with reference and named entry. |
| 8927 | * The function hlua_register_metatable() pops the stack, so we |
| 8928 | * previously create a copy of the table. |
| 8929 | */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8930 | lua_pushvalue(L, -1); /* Copy the -1 entry and push it on the stack. */ |
| 8931 | class_map_ref = hlua_register_metatable(L, CLASS_MAP); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8932 | |
| 8933 | /* Assign the metatable to the mai Map object. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8934 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8935 | |
| 8936 | /* Set a name to the table. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8937 | lua_setglobal(L, "Map"); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8938 | |
| 8939 | /* |
| 8940 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8941 | * Register class Channel |
| 8942 | * |
| 8943 | */ |
| 8944 | |
| 8945 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8946 | lua_newtable(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8947 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8948 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8949 | lua_pushstring(L, "__index"); |
| 8950 | lua_newtable(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8951 | |
| 8952 | /* Register . */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8953 | hlua_class_function(L, "get", hlua_channel_get); |
| 8954 | hlua_class_function(L, "dup", hlua_channel_dup); |
| 8955 | hlua_class_function(L, "getline", hlua_channel_getline); |
| 8956 | hlua_class_function(L, "set", hlua_channel_set); |
| 8957 | hlua_class_function(L, "append", hlua_channel_append); |
| 8958 | hlua_class_function(L, "send", hlua_channel_send); |
| 8959 | hlua_class_function(L, "forward", hlua_channel_forward); |
| 8960 | hlua_class_function(L, "get_in_len", hlua_channel_get_in_len); |
| 8961 | hlua_class_function(L, "get_out_len", hlua_channel_get_out_len); |
| 8962 | hlua_class_function(L, "is_full", hlua_channel_is_full); |
| 8963 | hlua_class_function(L, "is_resp", hlua_channel_is_resp); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8964 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8965 | lua_rawset(L, -3); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8966 | |
| 8967 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8968 | class_channel_ref = hlua_register_metatable(L, CLASS_CHANNEL); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8969 | |
| 8970 | /* |
| 8971 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8972 | * Register class Fetches |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 8973 | * |
| 8974 | */ |
| 8975 | |
| 8976 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8977 | lua_newtable(L); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 8978 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8979 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8980 | lua_pushstring(L, "__index"); |
| 8981 | lua_newtable(L); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 8982 | |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8983 | /* Browse existing fetches and create the associated |
| 8984 | * object method. |
| 8985 | */ |
| 8986 | sf = NULL; |
| 8987 | while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) { |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8988 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 8989 | * by an underscore. |
| 8990 | */ |
Willy Tarreau | fa92f36 | 2021-08-26 16:48:53 +0200 | [diff] [blame] | 8991 | strlcpy2(trash.area, sf->kw, trash.size); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8992 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8993 | if (*p == '.' || *p == '-' || *p == '+') |
| 8994 | *p = '_'; |
| 8995 | |
| 8996 | /* Register the function. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8997 | lua_pushstring(L, trash.area); |
| 8998 | lua_pushlightuserdata(L, sf); |
| 8999 | lua_pushcclosure(L, hlua_run_sample_fetch, 1); |
| 9000 | lua_rawset(L, -3); |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 9001 | } |
| 9002 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9003 | lua_rawset(L, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 9004 | |
| 9005 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9006 | class_fetches_ref = hlua_register_metatable(L, CLASS_FETCHES); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 9007 | |
| 9008 | /* |
| 9009 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 9010 | * Register class Converters |
| 9011 | * |
| 9012 | */ |
| 9013 | |
| 9014 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9015 | lua_newtable(L); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 9016 | |
| 9017 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9018 | lua_pushstring(L, "__index"); |
| 9019 | lua_newtable(L); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 9020 | |
| 9021 | /* Browse existing converters and create the associated |
| 9022 | * object method. |
| 9023 | */ |
| 9024 | sc = NULL; |
| 9025 | while ((sc = sample_conv_getnext(sc, &idx)) != NULL) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 9026 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 9027 | * by an underscore. |
| 9028 | */ |
Willy Tarreau | fa92f36 | 2021-08-26 16:48:53 +0200 | [diff] [blame] | 9029 | strlcpy2(trash.area, sc->kw, trash.size); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 9030 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 9031 | if (*p == '.' || *p == '-' || *p == '+') |
| 9032 | *p = '_'; |
| 9033 | |
| 9034 | /* Register the function. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9035 | lua_pushstring(L, trash.area); |
| 9036 | lua_pushlightuserdata(L, sc); |
| 9037 | lua_pushcclosure(L, hlua_run_sample_conv, 1); |
| 9038 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 9039 | } |
| 9040 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9041 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 9042 | |
| 9043 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9044 | class_converters_ref = hlua_register_metatable(L, CLASS_CONVERTERS); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 9045 | |
| 9046 | /* |
| 9047 | * |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 9048 | * Register class HTTP |
| 9049 | * |
| 9050 | */ |
| 9051 | |
| 9052 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9053 | lua_newtable(L); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 9054 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 9055 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9056 | lua_pushstring(L, "__index"); |
| 9057 | lua_newtable(L); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 9058 | |
| 9059 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9060 | hlua_class_function(L, "req_get_headers",hlua_http_req_get_headers); |
| 9061 | hlua_class_function(L, "req_del_header", hlua_http_req_del_hdr); |
| 9062 | hlua_class_function(L, "req_rep_header", hlua_http_req_rep_hdr); |
| 9063 | hlua_class_function(L, "req_rep_value", hlua_http_req_rep_val); |
| 9064 | hlua_class_function(L, "req_add_header", hlua_http_req_add_hdr); |
| 9065 | hlua_class_function(L, "req_set_header", hlua_http_req_set_hdr); |
| 9066 | hlua_class_function(L, "req_set_method", hlua_http_req_set_meth); |
| 9067 | hlua_class_function(L, "req_set_path", hlua_http_req_set_path); |
| 9068 | hlua_class_function(L, "req_set_query", hlua_http_req_set_query); |
| 9069 | hlua_class_function(L, "req_set_uri", hlua_http_req_set_uri); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 9070 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9071 | hlua_class_function(L, "res_get_headers",hlua_http_res_get_headers); |
| 9072 | hlua_class_function(L, "res_del_header", hlua_http_res_del_hdr); |
| 9073 | hlua_class_function(L, "res_rep_header", hlua_http_res_rep_hdr); |
| 9074 | hlua_class_function(L, "res_rep_value", hlua_http_res_rep_val); |
| 9075 | hlua_class_function(L, "res_add_header", hlua_http_res_add_hdr); |
| 9076 | hlua_class_function(L, "res_set_header", hlua_http_res_set_hdr); |
| 9077 | hlua_class_function(L, "res_set_status", hlua_http_res_set_status); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 9078 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9079 | lua_rawset(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 9080 | |
| 9081 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9082 | class_http_ref = hlua_register_metatable(L, CLASS_HTTP); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 9083 | |
| 9084 | /* |
| 9085 | * |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9086 | * Register class AppletTCP |
| 9087 | * |
| 9088 | */ |
| 9089 | |
| 9090 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9091 | lua_newtable(L); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9092 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 9093 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9094 | lua_pushstring(L, "__index"); |
| 9095 | lua_newtable(L); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9096 | |
| 9097 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9098 | hlua_class_function(L, "getline", hlua_applet_tcp_getline); |
| 9099 | hlua_class_function(L, "receive", hlua_applet_tcp_recv); |
| 9100 | hlua_class_function(L, "send", hlua_applet_tcp_send); |
| 9101 | hlua_class_function(L, "set_priv", hlua_applet_tcp_set_priv); |
| 9102 | hlua_class_function(L, "get_priv", hlua_applet_tcp_get_priv); |
| 9103 | hlua_class_function(L, "set_var", hlua_applet_tcp_set_var); |
| 9104 | hlua_class_function(L, "unset_var", hlua_applet_tcp_unset_var); |
| 9105 | hlua_class_function(L, "get_var", hlua_applet_tcp_get_var); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9106 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9107 | lua_settable(L, -3); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9108 | |
| 9109 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9110 | class_applet_tcp_ref = hlua_register_metatable(L, CLASS_APPLET_TCP); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9111 | |
| 9112 | /* |
| 9113 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9114 | * Register class AppletHTTP |
| 9115 | * |
| 9116 | */ |
| 9117 | |
| 9118 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9119 | lua_newtable(L); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9120 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 9121 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9122 | lua_pushstring(L, "__index"); |
| 9123 | lua_newtable(L); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9124 | |
| 9125 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9126 | hlua_class_function(L, "set_priv", hlua_applet_http_set_priv); |
| 9127 | hlua_class_function(L, "get_priv", hlua_applet_http_get_priv); |
| 9128 | hlua_class_function(L, "set_var", hlua_applet_http_set_var); |
| 9129 | hlua_class_function(L, "unset_var", hlua_applet_http_unset_var); |
| 9130 | hlua_class_function(L, "get_var", hlua_applet_http_get_var); |
| 9131 | hlua_class_function(L, "getline", hlua_applet_http_getline); |
| 9132 | hlua_class_function(L, "receive", hlua_applet_http_recv); |
| 9133 | hlua_class_function(L, "send", hlua_applet_http_send); |
| 9134 | hlua_class_function(L, "add_header", hlua_applet_http_addheader); |
| 9135 | hlua_class_function(L, "set_status", hlua_applet_http_status); |
| 9136 | hlua_class_function(L, "start_response", hlua_applet_http_start_response); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9137 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9138 | lua_settable(L, -3); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9139 | |
| 9140 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9141 | class_applet_http_ref = hlua_register_metatable(L, CLASS_APPLET_HTTP); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9142 | |
| 9143 | /* |
| 9144 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 9145 | * Register class TXN |
| 9146 | * |
| 9147 | */ |
| 9148 | |
| 9149 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9150 | lua_newtable(L); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 9151 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 9152 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9153 | lua_pushstring(L, "__index"); |
| 9154 | lua_newtable(L); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 9155 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 9156 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9157 | hlua_class_function(L, "set_priv", hlua_set_priv); |
| 9158 | hlua_class_function(L, "get_priv", hlua_get_priv); |
| 9159 | hlua_class_function(L, "set_var", hlua_set_var); |
| 9160 | hlua_class_function(L, "unset_var", hlua_unset_var); |
| 9161 | hlua_class_function(L, "get_var", hlua_get_var); |
| 9162 | hlua_class_function(L, "done", hlua_txn_done); |
| 9163 | hlua_class_function(L, "reply", hlua_txn_reply_new); |
| 9164 | hlua_class_function(L, "set_loglevel", hlua_txn_set_loglevel); |
| 9165 | hlua_class_function(L, "set_tos", hlua_txn_set_tos); |
| 9166 | hlua_class_function(L, "set_mark", hlua_txn_set_mark); |
| 9167 | hlua_class_function(L, "set_priority_class", hlua_txn_set_priority_class); |
| 9168 | hlua_class_function(L, "set_priority_offset", hlua_txn_set_priority_offset); |
| 9169 | hlua_class_function(L, "deflog", hlua_txn_deflog); |
| 9170 | hlua_class_function(L, "log", hlua_txn_log); |
| 9171 | hlua_class_function(L, "Debug", hlua_txn_log_debug); |
| 9172 | hlua_class_function(L, "Info", hlua_txn_log_info); |
| 9173 | hlua_class_function(L, "Warning", hlua_txn_log_warning); |
| 9174 | hlua_class_function(L, "Alert", hlua_txn_log_alert); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 9175 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9176 | lua_rawset(L, -3); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 9177 | |
| 9178 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9179 | class_txn_ref = hlua_register_metatable(L, CLASS_TXN); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9180 | |
| 9181 | /* |
| 9182 | * |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 9183 | * Register class reply |
| 9184 | * |
| 9185 | */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9186 | lua_newtable(L); |
| 9187 | lua_pushstring(L, "__index"); |
| 9188 | lua_newtable(L); |
| 9189 | hlua_class_function(L, "set_status", hlua_txn_reply_set_status); |
| 9190 | hlua_class_function(L, "add_header", hlua_txn_reply_add_header); |
| 9191 | hlua_class_function(L, "del_header", hlua_txn_reply_del_header); |
| 9192 | hlua_class_function(L, "set_body", hlua_txn_reply_set_body); |
| 9193 | lua_settable(L, -3); /* Sets the __index entry. */ |
| 9194 | class_txn_reply_ref = luaL_ref(L, LUA_REGISTRYINDEX); |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 9195 | |
| 9196 | |
| 9197 | /* |
| 9198 | * |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9199 | * Register class Socket |
| 9200 | * |
| 9201 | */ |
| 9202 | |
| 9203 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9204 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9205 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 9206 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9207 | lua_pushstring(L, "__index"); |
| 9208 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9209 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 9210 | #ifdef USE_OPENSSL |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9211 | hlua_class_function(L, "connect_ssl", hlua_socket_connect_ssl); |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 9212 | #endif |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9213 | hlua_class_function(L, "connect", hlua_socket_connect); |
| 9214 | hlua_class_function(L, "send", hlua_socket_send); |
| 9215 | hlua_class_function(L, "receive", hlua_socket_receive); |
| 9216 | hlua_class_function(L, "close", hlua_socket_close); |
| 9217 | hlua_class_function(L, "getpeername", hlua_socket_getpeername); |
| 9218 | hlua_class_function(L, "getsockname", hlua_socket_getsockname); |
| 9219 | hlua_class_function(L, "setoption", hlua_socket_setoption); |
| 9220 | hlua_class_function(L, "settimeout", hlua_socket_settimeout); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9221 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9222 | 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] | 9223 | |
| 9224 | /* Register the garbage collector entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9225 | lua_pushstring(L, "__gc"); |
| 9226 | lua_pushcclosure(L, hlua_socket_gc, 0); |
| 9227 | 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] | 9228 | |
| 9229 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9230 | class_socket_ref = hlua_register_metatable(L, CLASS_SOCKET); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9231 | |
Thierry Fournier | aafc777 | 2020-12-04 11:47:47 +0100 | [diff] [blame] | 9232 | lua_atpanic(L, hlua_panic_safe); |
| 9233 | |
| 9234 | return L; |
| 9235 | } |
| 9236 | |
| 9237 | void hlua_init(void) { |
| 9238 | int i; |
Amaury Denoyelle | 239fdbf | 2021-03-24 10:22:03 +0100 | [diff] [blame] | 9239 | char *errmsg; |
Thierry Fournier | aafc777 | 2020-12-04 11:47:47 +0100 | [diff] [blame] | 9240 | #ifdef USE_OPENSSL |
| 9241 | struct srv_kw *kw; |
| 9242 | int tmp_error; |
| 9243 | char *error; |
| 9244 | char *args[] = { /* SSL client configuration. */ |
| 9245 | "ssl", |
| 9246 | "verify", |
| 9247 | "none", |
| 9248 | NULL |
| 9249 | }; |
| 9250 | #endif |
| 9251 | |
| 9252 | /* Init post init function list head */ |
| 9253 | for (i = 0; i < MAX_THREADS + 1; i++) |
| 9254 | LIST_INIT(&hlua_init_functions[i]); |
| 9255 | |
| 9256 | /* Init state for common/shared lua parts */ |
| 9257 | hlua_state_id = 0; |
| 9258 | ha_set_tid(0); |
| 9259 | hlua_states[0] = hlua_init_state(0); |
| 9260 | |
| 9261 | /* Init state 1 for thread 0. We have at least one thread. */ |
| 9262 | hlua_state_id = 1; |
| 9263 | ha_set_tid(0); |
| 9264 | hlua_states[1] = hlua_init_state(1); |
| 9265 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9266 | /* Proxy and server configuration initialisation. */ |
Amaury Denoyelle | 239fdbf | 2021-03-24 10:22:03 +0100 | [diff] [blame] | 9267 | socket_proxy = alloc_new_proxy("LUA-SOCKET", PR_CAP_FE|PR_CAP_BE|PR_CAP_LUA, &errmsg); |
| 9268 | if (!socket_proxy) { |
| 9269 | fprintf(stderr, "Lua init: %s\n", errmsg); |
| 9270 | exit(1); |
| 9271 | } |
| 9272 | proxy_preset_defaults(socket_proxy); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9273 | |
| 9274 | /* Init TCP server: unchanged parameters */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 9275 | socket_tcp = new_server(socket_proxy); |
| 9276 | if (!socket_tcp) { |
| 9277 | fprintf(stderr, "Lua init: failed to allocate tcp server socket\n"); |
| 9278 | exit(1); |
| 9279 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9280 | |
| 9281 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9282 | /* Init TCP server: unchanged parameters */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 9283 | socket_ssl = new_server(socket_proxy); |
| 9284 | if (!socket_ssl) { |
| 9285 | fprintf(stderr, "Lua init: failed to allocate ssl server socket\n"); |
| 9286 | exit(1); |
| 9287 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9288 | |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 9289 | socket_ssl->use_ssl = 1; |
| 9290 | socket_ssl->xprt = xprt_get(XPRT_SSL); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9291 | |
Bertrand Jacquin | 80839ff | 2021-01-21 19:14:46 +0000 | [diff] [blame] | 9292 | for (i = 0; args[i] != NULL; i++) { |
| 9293 | 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] | 9294 | /* |
| 9295 | * |
| 9296 | * If the keyword is not known, we can search in the registered |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 9297 | * server keywords. This is useful to configure special SSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9298 | * features like client certificates and ssl_verify. |
| 9299 | * |
| 9300 | */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 9301 | tmp_error = kw->parse(args, &i, socket_proxy, socket_ssl, &error); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9302 | if (tmp_error != 0) { |
| 9303 | fprintf(stderr, "INTERNAL ERROR: %s\n", error); |
| 9304 | abort(); /* This must be never arrives because the command line |
| 9305 | not editable by the user. */ |
| 9306 | } |
Bertrand Jacquin | 80839ff | 2021-01-21 19:14:46 +0000 | [diff] [blame] | 9307 | i += kw->skip; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9308 | } |
| 9309 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9310 | #endif |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 9311 | |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 9312 | } |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 9313 | |
Tim Duesterhus | d0c0ca2 | 2020-07-04 11:53:26 +0200 | [diff] [blame] | 9314 | static void hlua_deinit() |
| 9315 | { |
Willy Tarreau | 186f376 | 2020-12-04 11:48:12 +0100 | [diff] [blame] | 9316 | int thr; |
| 9317 | |
| 9318 | for (thr = 0; thr < MAX_THREADS+1; thr++) { |
| 9319 | if (hlua_states[thr]) |
| 9320 | lua_close(hlua_states[thr]); |
| 9321 | } |
Willy Tarreau | 430bf4a | 2021-03-04 09:45:32 +0100 | [diff] [blame] | 9322 | |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 9323 | free_server(socket_tcp); |
Willy Tarreau | 430bf4a | 2021-03-04 09:45:32 +0100 | [diff] [blame] | 9324 | |
Willy Tarreau | 0f143af | 2021-03-05 10:41:48 +0100 | [diff] [blame] | 9325 | #ifdef USE_OPENSSL |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 9326 | free_server(socket_ssl); |
Willy Tarreau | 0f143af | 2021-03-05 10:41:48 +0100 | [diff] [blame] | 9327 | #endif |
Amaury Denoyelle | 239fdbf | 2021-03-24 10:22:03 +0100 | [diff] [blame] | 9328 | |
| 9329 | free_proxy(socket_proxy); |
Tim Duesterhus | d0c0ca2 | 2020-07-04 11:53:26 +0200 | [diff] [blame] | 9330 | } |
| 9331 | |
| 9332 | REGISTER_POST_DEINIT(hlua_deinit); |
| 9333 | |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 9334 | static void hlua_register_build_options(void) |
| 9335 | { |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 9336 | char *ptr = NULL; |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 9337 | |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 9338 | memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE); |
| 9339 | hap_register_build_opts(ptr, 1); |
| 9340 | } |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 9341 | |
| 9342 | INITCALL0(STG_REGISTER, hlua_register_build_options); |