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: |
Olivier Houchard | a265827 | 2022-09-13 00:35:53 +0200 | [diff] [blame] | 741 | if (!p->table) { |
| 742 | msg = "Mandatory argument expected"; |
| 743 | goto error; |
| 744 | } |
| 745 | argp[idx].data.t = p->table; |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 746 | argp[idx].type = ARGT_TAB; |
| 747 | argp[idx+1].type = ARGT_STOP; |
| 748 | break; |
| 749 | |
| 750 | default: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 751 | msg = "Mandatory argument expected"; |
| 752 | goto error; |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 753 | break; |
| 754 | } |
| 755 | } |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 756 | break; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 757 | } |
| 758 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 759 | /* Check for exceed the number of required argument. */ |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 760 | if ((mask & ARGT_MASK) == ARGT_STOP && |
| 761 | argp[idx].type != ARGT_STOP) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 762 | msg = "Last argument expected"; |
| 763 | goto error; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 764 | } |
| 765 | |
| 766 | if ((mask & ARGT_MASK) == ARGT_STOP && |
| 767 | argp[idx].type == ARGT_STOP) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 768 | break; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 769 | } |
| 770 | |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 771 | /* Convert some argument types. All string in argp[] are for not |
| 772 | * duplicated yet. |
| 773 | */ |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 774 | switch (mask & ARGT_MASK) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 775 | case ARGT_SINT: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 776 | if (argp[idx].type != ARGT_SINT) { |
| 777 | msg = "integer expected"; |
| 778 | goto error; |
| 779 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 780 | argp[idx].type = ARGT_SINT; |
| 781 | break; |
| 782 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 783 | case ARGT_TIME: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 784 | if (argp[idx].type != ARGT_SINT) { |
| 785 | msg = "integer expected"; |
| 786 | goto error; |
| 787 | } |
Thierry FOURNIER | 29176f3 | 2015-07-07 00:41:29 +0200 | [diff] [blame] | 788 | argp[idx].type = ARGT_TIME; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 789 | break; |
| 790 | |
| 791 | case ARGT_SIZE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 792 | if (argp[idx].type != ARGT_SINT) { |
| 793 | msg = "integer expected"; |
| 794 | goto error; |
| 795 | } |
Thierry FOURNIER | 29176f3 | 2015-07-07 00:41:29 +0200 | [diff] [blame] | 796 | argp[idx].type = ARGT_SIZE; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 797 | break; |
| 798 | |
| 799 | case ARGT_FE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 800 | if (argp[idx].type != ARGT_STR) { |
| 801 | msg = "string expected"; |
| 802 | goto error; |
| 803 | } |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 804 | 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] | 805 | if (!argp[idx].data.prx) { |
| 806 | msg = "frontend doesn't exist"; |
| 807 | goto error; |
| 808 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 809 | argp[idx].type = ARGT_FE; |
| 810 | break; |
| 811 | |
| 812 | case ARGT_BE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 813 | if (argp[idx].type != ARGT_STR) { |
| 814 | msg = "string expected"; |
| 815 | goto error; |
| 816 | } |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 817 | 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] | 818 | if (!argp[idx].data.prx) { |
| 819 | msg = "backend doesn't exist"; |
| 820 | goto error; |
| 821 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 822 | argp[idx].type = ARGT_BE; |
| 823 | break; |
| 824 | |
| 825 | case ARGT_TAB: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 826 | if (argp[idx].type != ARGT_STR) { |
| 827 | msg = "string expected"; |
| 828 | goto error; |
| 829 | } |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 830 | 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] | 831 | if (!argp[idx].data.t) { |
| 832 | msg = "table doesn't exist"; |
| 833 | goto error; |
| 834 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 835 | argp[idx].type = ARGT_TAB; |
| 836 | break; |
| 837 | |
| 838 | case ARGT_SRV: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 839 | if (argp[idx].type != ARGT_STR) { |
| 840 | msg = "string expected"; |
| 841 | goto error; |
| 842 | } |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 843 | sname = strrchr(argp[idx].data.str.area, '/'); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 844 | if (sname) { |
| 845 | *sname++ = '\0'; |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 846 | pname = argp[idx].data.str.area; |
Willy Tarreau | 9e0bb10 | 2015-05-26 11:24:42 +0200 | [diff] [blame] | 847 | px = proxy_be_by_name(pname); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 848 | if (!px) { |
| 849 | msg = "backend doesn't exist"; |
| 850 | goto error; |
| 851 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 852 | } |
| 853 | else { |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 854 | sname = argp[idx].data.str.area; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 855 | px = p; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 856 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 857 | argp[idx].data.srv = findserver(px, sname); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 858 | if (!argp[idx].data.srv) { |
| 859 | msg = "server doesn't exist"; |
| 860 | goto error; |
| 861 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 862 | argp[idx].type = ARGT_SRV; |
| 863 | break; |
| 864 | |
| 865 | case ARGT_IPV4: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 866 | if (argp[idx].type != ARGT_STR) { |
| 867 | msg = "string expected"; |
| 868 | goto error; |
| 869 | } |
| 870 | if (inet_pton(AF_INET, argp[idx].data.str.area, &argp[idx].data.ipv4)) { |
| 871 | msg = "invalid IPv4 address"; |
| 872 | goto error; |
| 873 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 874 | argp[idx].type = ARGT_IPV4; |
| 875 | break; |
| 876 | |
| 877 | case ARGT_MSK4: |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 878 | if (argp[idx].type == ARGT_SINT) |
| 879 | len2mask4(argp[idx].data.sint, &argp[idx].data.ipv4); |
| 880 | else if (argp[idx].type == ARGT_STR) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 881 | if (!str2mask(argp[idx].data.str.area, &argp[idx].data.ipv4)) { |
| 882 | msg = "invalid IPv4 mask"; |
| 883 | goto error; |
| 884 | } |
| 885 | } |
| 886 | else { |
| 887 | msg = "integer or string expected"; |
| 888 | goto error; |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 889 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 890 | argp[idx].type = ARGT_MSK4; |
| 891 | break; |
| 892 | |
| 893 | case ARGT_IPV6: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 894 | if (argp[idx].type != ARGT_STR) { |
| 895 | msg = "string expected"; |
| 896 | goto error; |
| 897 | } |
| 898 | if (inet_pton(AF_INET6, argp[idx].data.str.area, &argp[idx].data.ipv6)) { |
| 899 | msg = "invalid IPv6 address"; |
| 900 | goto error; |
| 901 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 902 | argp[idx].type = ARGT_IPV6; |
| 903 | break; |
| 904 | |
| 905 | case ARGT_MSK6: |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 906 | if (argp[idx].type == ARGT_SINT) |
| 907 | len2mask6(argp[idx].data.sint, &argp[idx].data.ipv6); |
| 908 | else if (argp[idx].type == ARGT_STR) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 909 | if (!str2mask6(argp[idx].data.str.area, &argp[idx].data.ipv6)) { |
| 910 | msg = "invalid IPv6 mask"; |
| 911 | goto error; |
| 912 | } |
| 913 | } |
| 914 | else { |
| 915 | msg = "integer or string expected"; |
| 916 | goto error; |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 917 | } |
Tim Duesterhus | b814da6 | 2018-01-25 16:24:50 +0100 | [diff] [blame] | 918 | argp[idx].type = ARGT_MSK6; |
| 919 | break; |
| 920 | |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 921 | case ARGT_REG: |
| 922 | if (argp[idx].type != ARGT_STR) { |
| 923 | msg = "string expected"; |
| 924 | goto error; |
| 925 | } |
| 926 | reg = regex_comp(argp[idx].data.str.area, !(argp[idx].type_flags & ARGF_REG_ICASE), 1, &err); |
| 927 | if (!reg) { |
| 928 | msg = lua_pushfstring(L, "error compiling regex '%s' : '%s'", |
| 929 | argp[idx].data.str.area, err); |
| 930 | free(err); |
| 931 | goto error; |
| 932 | } |
| 933 | argp[idx].type = ARGT_REG; |
| 934 | argp[idx].data.reg = reg; |
| 935 | break; |
| 936 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 937 | case ARGT_USR: |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 938 | if (argp[idx].type != ARGT_STR) { |
| 939 | msg = "string expected"; |
| 940 | goto error; |
| 941 | } |
| 942 | if (p->uri_auth && p->uri_auth->userlist && |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 943 | strcmp(p->uri_auth->userlist->name, argp[idx].data.str.area) == 0) |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 944 | ul = p->uri_auth->userlist; |
| 945 | else |
| 946 | ul = auth_find_userlist(argp[idx].data.str.area); |
| 947 | |
| 948 | if (!ul) { |
| 949 | msg = lua_pushfstring(L, "unable to find userlist '%s'", argp[idx].data.str.area); |
| 950 | goto error; |
| 951 | } |
| 952 | argp[idx].type = ARGT_USR; |
| 953 | argp[idx].data.usr = ul; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 954 | break; |
| 955 | |
| 956 | case ARGT_STR: |
| 957 | if (!chunk_dup(&tmp, &argp[idx].data.str)) { |
| 958 | msg = "unable to duplicate string arg"; |
| 959 | goto error; |
| 960 | } |
| 961 | argp[idx].data.str = tmp; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 962 | break; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 963 | |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 964 | case ARGT_MAP: |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 965 | msg = "type not yet supported"; |
| 966 | goto error; |
| 967 | break; |
| 968 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 969 | } |
| 970 | |
| 971 | /* Check for type of argument. */ |
| 972 | if ((mask & ARGT_MASK) != argp[idx].type) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 973 | msg = lua_pushfstring(L, "'%s' expected, got '%s'", |
| 974 | arg_type_names[(mask & ARGT_MASK)], |
| 975 | arg_type_names[argp[idx].type & ARGT_MASK]); |
| 976 | goto error; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 977 | } |
| 978 | |
| 979 | /* Next argument. */ |
| 980 | mask >>= ARGT_BITS; |
| 981 | idx++; |
| 982 | } |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 983 | return 0; |
| 984 | |
| 985 | error: |
Olivier Houchard | da25dac | 2022-09-13 00:31:17 +0200 | [diff] [blame] | 986 | argp[idx].type = ARGT_STOP; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 987 | for (i = 0; i < idx; i++) { |
| 988 | if (argp[i].type == ARGT_STR) |
| 989 | chunk_destroy(&argp[i].data.str); |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 990 | else if (argp[i].type == ARGT_REG) |
| 991 | regex_free(argp[i].data.reg); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 992 | } |
| 993 | WILL_LJMP(luaL_argerror(L, first + idx, msg)); |
| 994 | return 0; /* Never reached */ |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 995 | } |
| 996 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 997 | /* |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 998 | * The following functions are used to make correspondence between the the |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 999 | * executed lua pointer and the "struct hlua *" that contain the context. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1000 | * |
| 1001 | * - hlua_gethlua : return the hlua context associated with an lua_State. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1002 | * - hlua_sethlua : create the association between hlua context and lua_state. |
| 1003 | */ |
| 1004 | static inline struct hlua *hlua_gethlua(lua_State *L) |
| 1005 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 1006 | struct hlua **hlua = lua_getextraspace(L); |
| 1007 | return *hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1008 | } |
| 1009 | static inline void hlua_sethlua(struct hlua *hlua) |
| 1010 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 1011 | struct hlua **hlua_store = lua_getextraspace(hlua->T); |
| 1012 | *hlua_store = hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1013 | } |
| 1014 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1015 | /* This function is used to send logs. It try to send on screen (stderr) |
| 1016 | * and on the default syslog server. |
| 1017 | */ |
| 1018 | static inline void hlua_sendlog(struct proxy *px, int level, const char *msg) |
| 1019 | { |
| 1020 | struct tm tm; |
| 1021 | char *p; |
| 1022 | |
| 1023 | /* Cleanup the log message. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1024 | p = trash.area; |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1025 | for (; *msg != '\0'; msg++, p++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1026 | if (p >= trash.area + trash.size - 1) { |
Thierry FOURNIER | ccf0063 | 2015-09-16 12:47:03 +0200 | [diff] [blame] | 1027 | /* Break the message if exceed the buffer size. */ |
| 1028 | *(p-4) = ' '; |
| 1029 | *(p-3) = '.'; |
| 1030 | *(p-2) = '.'; |
| 1031 | *(p-1) = '.'; |
| 1032 | break; |
| 1033 | } |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 1034 | if (isprint((unsigned char)*msg)) |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1035 | *p = *msg; |
| 1036 | else |
| 1037 | *p = '.'; |
| 1038 | } |
| 1039 | *p = '\0'; |
| 1040 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1041 | send_log(px, level, "%s\n", trash.area); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1042 | if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) { |
Christopher Faulet | f98d821 | 2020-10-02 18:13:52 +0200 | [diff] [blame] | 1043 | if (level == LOG_DEBUG && !(global.mode & MODE_DEBUG)) |
| 1044 | return; |
| 1045 | |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 1046 | get_localtime(date.tv_sec, &tm); |
| 1047 | fprintf(stderr, "[%s] %03d/%02d%02d%02d (%d) : %s\n", |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1048 | 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] | 1049 | (int)getpid(), trash.area); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1050 | fflush(stderr); |
| 1051 | } |
| 1052 | } |
| 1053 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1054 | /* This function just ensure that the yield will be always |
| 1055 | * returned with a timeout and permit to set some flags |
| 1056 | */ |
| 1057 | __LJMP void hlua_yieldk(lua_State *L, int nresults, int ctx, |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1058 | lua_KFunction k, int timeout, unsigned int flags) |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1059 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 1060 | struct hlua *hlua; |
| 1061 | |
| 1062 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 1063 | hlua = hlua_gethlua(L); |
| 1064 | if (!hlua) { |
| 1065 | return; |
| 1066 | } |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1067 | |
| 1068 | /* Set the wake timeout. If timeout is required, we set |
| 1069 | * the expiration time. |
| 1070 | */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1071 | hlua->wake_time = timeout; |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1072 | |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 1073 | hlua->flags |= flags; |
| 1074 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1075 | /* Process the yield. */ |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 1076 | MAY_LJMP(lua_yieldk(L, nresults, ctx, k)); |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1077 | } |
| 1078 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1079 | /* This function initialises the Lua environment stored in the stream. |
| 1080 | * 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] | 1081 | * 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] | 1082 | * |
| 1083 | * This function is particular. it initialises a new Lua thread. If the |
| 1084 | * initialisation fails (example: out of memory error), the lua function |
| 1085 | * throws an error (longjmp). |
| 1086 | * |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1087 | * 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] | 1088 | * environment, so we must not initialise it. While the support of |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1089 | * threads appear, the safe environment set a lock to ensure only one |
| 1090 | * Lua execution at a time. If we initialize safe environment in another |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1091 | * safe environment, we have a dead lock. |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1092 | * |
| 1093 | * set "already_safe" true if the context is initialized form safe |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1094 | * Lua function. |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1095 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1096 | * This function manipulates two Lua stacks: the main and the thread. Only |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 1097 | * the main stack can fail. The thread is not manipulated. This function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1098 | * MUST NOT manipulate the created thread stack state, because it is not |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1099 | * protected against errors thrown by the thread stack. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1100 | */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1101 | 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] | 1102 | { |
| 1103 | lua->Mref = LUA_REFNIL; |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1104 | lua->flags = 0; |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 1105 | lua->gc_count = 0; |
Christopher Faulet | bc275a9 | 2020-02-26 14:55:16 +0100 | [diff] [blame] | 1106 | lua->wake_time = TICK_ETERNITY; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1107 | lua->state_id = state_id; |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 1108 | LIST_INIT(&lua->com); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1109 | if (!already_safe) { |
| 1110 | if (!SET_SAFE_LJMP_PARENT(lua)) { |
| 1111 | lua->Tref = LUA_REFNIL; |
| 1112 | return 0; |
| 1113 | } |
| 1114 | } |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1115 | lua->T = lua_newthread(hlua_states[state_id]); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1116 | if (!lua->T) { |
| 1117 | lua->Tref = LUA_REFNIL; |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1118 | if (!already_safe) |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1119 | RESET_SAFE_LJMP_PARENT(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1120 | return 0; |
| 1121 | } |
| 1122 | hlua_sethlua(lua); |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1123 | lua->Tref = luaL_ref(hlua_states[state_id], LUA_REGISTRYINDEX); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1124 | lua->task = task; |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1125 | if (!already_safe) |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1126 | RESET_SAFE_LJMP_PARENT(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1127 | return 1; |
| 1128 | } |
| 1129 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1130 | /* Used to destroy the Lua coroutine when the attached stream or task |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1131 | * is destroyed. The destroy also the memory context. The struct "lua" |
| 1132 | * is not freed. |
| 1133 | */ |
| 1134 | void hlua_ctx_destroy(struct hlua *lua) |
| 1135 | { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 1136 | if (!lua) |
Thierry FOURNIER | a718b29 | 2015-03-04 16:48:34 +0100 | [diff] [blame] | 1137 | return; |
| 1138 | |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 1139 | if (!lua->T) |
| 1140 | goto end; |
| 1141 | |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 1142 | /* Purge all the pending signals. */ |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1143 | notification_purge(&lua->com); |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 1144 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1145 | if (!SET_SAFE_LJMP(lua)) |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 1146 | return; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1147 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1148 | RESET_SAFE_LJMP(lua); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 1149 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1150 | if (!SET_SAFE_LJMP_PARENT(lua)) |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 1151 | return; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1152 | luaL_unref(hlua_states[lua->state_id], LUA_REGISTRYINDEX, lua->Tref); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1153 | RESET_SAFE_LJMP_PARENT(lua); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 1154 | /* Forces a garbage collecting process. If the Lua program is finished |
| 1155 | * without error, we run the GC on the thread pointer. Its freed all |
| 1156 | * the unused memory. |
| 1157 | * If the thread is finnish with an error or is currently yielded, |
| 1158 | * it seems that the GC applied on the thread doesn't clean anything, |
| 1159 | * so e run the GC on the main thread. |
| 1160 | * NOTE: maybe this action locks all the Lua threads untiml the en of |
| 1161 | * the garbage collection. |
| 1162 | */ |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 1163 | if (lua->gc_count) { |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1164 | if (!SET_SAFE_LJMP_PARENT(lua)) |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 1165 | return; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1166 | lua_gc(hlua_states[lua->state_id], LUA_GCCOLLECT, 0); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1167 | RESET_SAFE_LJMP_PARENT(lua); |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 1168 | } |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 1169 | |
Thierry FOURNIER | a7b536b | 2015-09-21 22:50:24 +0200 | [diff] [blame] | 1170 | lua->T = NULL; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 1171 | |
| 1172 | end: |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 1173 | pool_free(pool_head_hlua, lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1174 | } |
| 1175 | |
| 1176 | /* This function is used to restore the Lua context when a coroutine |
| 1177 | * fails. This function copy the common memory between old coroutine |
| 1178 | * and the new coroutine. The old coroutine is destroyed, and its |
| 1179 | * replaced by the new coroutine. |
| 1180 | * If the flag "keep_msg" is set, the last entry of the old is assumed |
| 1181 | * as string error message and it is copied in the new stack. |
| 1182 | */ |
| 1183 | static int hlua_ctx_renew(struct hlua *lua, int keep_msg) |
| 1184 | { |
| 1185 | lua_State *T; |
| 1186 | int new_ref; |
| 1187 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1188 | /* New Lua coroutine. */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1189 | T = lua_newthread(hlua_states[lua->state_id]); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1190 | if (!T) |
| 1191 | return 0; |
| 1192 | |
| 1193 | /* Copy last error message. */ |
| 1194 | if (keep_msg) |
| 1195 | lua_xmove(lua->T, T, 1); |
| 1196 | |
| 1197 | /* Copy data between the coroutines. */ |
| 1198 | lua_rawgeti(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 1199 | lua_xmove(lua->T, T, 1); |
Ilya Shipitsin | 46a030c | 2020-07-05 16:36:08 +0500 | [diff] [blame] | 1200 | new_ref = luaL_ref(T, LUA_REGISTRYINDEX); /* Value popped. */ |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1201 | |
| 1202 | /* Destroy old data. */ |
| 1203 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 1204 | |
| 1205 | /* The thread is garbage collected by Lua. */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1206 | luaL_unref(hlua_states[lua->state_id], LUA_REGISTRYINDEX, lua->Tref); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1207 | |
| 1208 | /* Fill the struct with the new coroutine values. */ |
| 1209 | lua->Mref = new_ref; |
| 1210 | lua->T = T; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1211 | lua->Tref = luaL_ref(hlua_states[lua->state_id], LUA_REGISTRYINDEX); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1212 | |
| 1213 | /* Set context. */ |
| 1214 | hlua_sethlua(lua); |
| 1215 | |
| 1216 | return 1; |
| 1217 | } |
| 1218 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1219 | void hlua_hook(lua_State *L, lua_Debug *ar) |
| 1220 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 1221 | struct hlua *hlua; |
| 1222 | |
| 1223 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 1224 | hlua = hlua_gethlua(L); |
| 1225 | if (!hlua) |
| 1226 | return; |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1227 | |
| 1228 | /* Lua cannot yield when its returning from a function, |
| 1229 | * so, we can fix the interrupt hook to 1 instruction, |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1230 | * expecting that the function is finished. |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1231 | */ |
| 1232 | if (lua_gethookmask(L) & LUA_MASKRET) { |
| 1233 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, 1); |
| 1234 | return; |
| 1235 | } |
| 1236 | |
| 1237 | /* restore the interrupt condition. */ |
| 1238 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 1239 | |
| 1240 | /* If we interrupt the Lua processing in yieldable state, we yield. |
| 1241 | * If the state is not yieldable, trying yield causes an error. |
| 1242 | */ |
| 1243 | if (lua_isyieldable(L)) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 1244 | MAY_LJMP(hlua_yieldk(L, 0, 0, NULL, TICK_ETERNITY, HLUA_CTRLYIELD)); |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1245 | |
Thierry FOURNIER | a85cfb1 | 2015-03-13 14:50:06 +0100 | [diff] [blame] | 1246 | /* If we cannot yield, update the clock and check the timeout. */ |
| 1247 | tv_update_date(0, 1); |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1248 | hlua->run_time += now_ms - hlua->start_time; |
| 1249 | if (hlua->max_time && hlua->run_time >= hlua->max_time) { |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1250 | lua_pushfstring(L, "execution timeout"); |
| 1251 | WILL_LJMP(lua_error(L)); |
| 1252 | } |
| 1253 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1254 | /* Update the start time. */ |
| 1255 | hlua->start_time = now_ms; |
| 1256 | |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1257 | /* Try to interrupt the process at the end of the current |
| 1258 | * unyieldable function. |
| 1259 | */ |
| 1260 | 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] | 1261 | } |
| 1262 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1263 | /* This function start or resumes the Lua stack execution. If the flag |
| 1264 | * "yield_allowed" if no set and the LUA stack execution returns a yield |
| 1265 | * The function return an error. |
| 1266 | * |
| 1267 | * The function can returns 4 values: |
| 1268 | * - HLUA_E_OK : The execution is terminated without any errors. |
| 1269 | * - HLUA_E_AGAIN : The execution must continue at the next associated |
| 1270 | * task wakeup. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1271 | * - 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] | 1272 | * the top of the stack. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1273 | * - HLUA_E_ERR : An error has occurred without error message. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1274 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1275 | * 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] | 1276 | * LUA code. |
| 1277 | */ |
| 1278 | static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed) |
| 1279 | { |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1280 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
| 1281 | int nres; |
| 1282 | #endif |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1283 | int ret; |
| 1284 | const char *msg; |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1285 | const char *trace; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1286 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1287 | /* Initialise run time counter. */ |
| 1288 | if (!HLUA_IS_RUNNING(lua)) |
| 1289 | lua->run_time = 0; |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1290 | |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1291 | /* Lock the whole Lua execution. This lock must be before the |
| 1292 | * label "resume_execution". |
| 1293 | */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1294 | if (lua->state_id == 0) |
Willy Tarreau | 3eb2e47 | 2021-08-20 15:47:25 +0200 | [diff] [blame] | 1295 | lua_take_global_lock(); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1296 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1297 | resume_execution: |
| 1298 | |
| 1299 | /* This hook interrupts the Lua processing each 'hlua_nb_instruction' |
| 1300 | * instructions. it is used for preventing infinite loops. |
| 1301 | */ |
| 1302 | lua_sethook(lua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 1303 | |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1304 | /* Remove all flags except the running flags. */ |
Thierry FOURNIER | 2f3867f | 2015-09-28 01:02:01 +0200 | [diff] [blame] | 1305 | HLUA_SET_RUN(lua); |
| 1306 | HLUA_CLR_CTRLYIELD(lua); |
| 1307 | HLUA_CLR_WAKERESWR(lua); |
| 1308 | HLUA_CLR_WAKEREQWR(lua); |
Christopher Faulet | a6e792f | 2021-08-04 17:58:21 +0200 | [diff] [blame] | 1309 | HLUA_CLR_NOYIELD(lua); |
| 1310 | if (!yield_allowed) |
| 1311 | HLUA_SET_NOYIELD(lua); |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1312 | |
Christopher Faulet | bc275a9 | 2020-02-26 14:55:16 +0100 | [diff] [blame] | 1313 | /* Update the start time and reset wake_time. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1314 | lua->start_time = now_ms; |
Christopher Faulet | bc275a9 | 2020-02-26 14:55:16 +0100 | [diff] [blame] | 1315 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1316 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1317 | /* Call the function. */ |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1318 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1319 | 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] | 1320 | #else |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1321 | ret = lua_resume(lua->T, hlua_states[lua->state_id], lua->nargs); |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1322 | #endif |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1323 | switch (ret) { |
| 1324 | |
| 1325 | case LUA_OK: |
| 1326 | ret = HLUA_E_OK; |
| 1327 | break; |
| 1328 | |
| 1329 | case LUA_YIELD: |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1330 | /* Check if the execution timeout is expired. It it is the case, we |
| 1331 | * break the Lua execution. |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1332 | */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1333 | tv_update_date(0, 1); |
| 1334 | lua->run_time += now_ms - lua->start_time; |
| 1335 | if (lua->max_time && lua->run_time > lua->max_time) { |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1336 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1337 | ret = HLUA_E_ETMOUT; |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1338 | break; |
| 1339 | } |
| 1340 | /* Process the forced yield. if the general yield is not allowed or |
| 1341 | * if no task were associated this the current Lua execution |
| 1342 | * coroutine, we resume the execution. Else we want to return in the |
| 1343 | * scheduler and we want to be waked up again, to continue the |
| 1344 | * current Lua execution. So we schedule our own task. |
| 1345 | */ |
| 1346 | if (HLUA_IS_CTRLYIELDING(lua)) { |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1347 | if (!yield_allowed || !lua->task) |
| 1348 | goto resume_execution; |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1349 | task_wakeup(lua->task, TASK_WOKEN_MSG); |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1350 | } |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1351 | if (!yield_allowed) { |
| 1352 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1353 | ret = HLUA_E_YIELD; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1354 | break; |
| 1355 | } |
| 1356 | ret = HLUA_E_AGAIN; |
| 1357 | break; |
| 1358 | |
| 1359 | case LUA_ERRRUN: |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1360 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1361 | /* Special exit case. The traditional exit is returned as an error |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1362 | * because the errors ares the only one mean to return immediately |
| 1363 | * from and lua execution. |
| 1364 | */ |
| 1365 | if (lua->flags & HLUA_EXIT) { |
| 1366 | ret = HLUA_E_OK; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 1367 | hlua_ctx_renew(lua, 1); |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1368 | break; |
| 1369 | } |
| 1370 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1371 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1372 | if (!lua_checkstack(lua->T, 1)) { |
| 1373 | ret = HLUA_E_ERR; |
| 1374 | break; |
| 1375 | } |
| 1376 | msg = lua_tostring(lua->T, -1); |
| 1377 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1378 | lua_pop(lua->T, 1); |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 1379 | trace = hlua_traceback(lua->T, ", "); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1380 | if (msg) |
Thierry Fournier | 46278ff | 2020-11-29 11:48:12 +0100 | [diff] [blame] | 1381 | 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] | 1382 | else |
Thierry Fournier | 46278ff | 2020-11-29 11:48:12 +0100 | [diff] [blame] | 1383 | 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] | 1384 | ret = HLUA_E_ERRMSG; |
| 1385 | break; |
| 1386 | |
| 1387 | case LUA_ERRMEM: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1388 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1389 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1390 | ret = HLUA_E_NOMEM; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1391 | break; |
| 1392 | |
| 1393 | case LUA_ERRERR: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1394 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1395 | if (!lua_checkstack(lua->T, 1)) { |
| 1396 | ret = HLUA_E_ERR; |
| 1397 | break; |
| 1398 | } |
| 1399 | msg = lua_tostring(lua->T, -1); |
| 1400 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1401 | lua_pop(lua->T, 1); |
| 1402 | if (msg) |
Thierry Fournier | 46278ff | 2020-11-29 11:48:12 +0100 | [diff] [blame] | 1403 | 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] | 1404 | else |
Thierry Fournier | 46278ff | 2020-11-29 11:48:12 +0100 | [diff] [blame] | 1405 | 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] | 1406 | ret = HLUA_E_ERRMSG; |
| 1407 | break; |
| 1408 | |
| 1409 | default: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1410 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1411 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1412 | ret = HLUA_E_ERR; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1413 | break; |
| 1414 | } |
| 1415 | |
| 1416 | switch (ret) { |
| 1417 | case HLUA_E_AGAIN: |
| 1418 | break; |
| 1419 | |
| 1420 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1421 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1422 | hlua_ctx_renew(lua, 1); |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1423 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1424 | break; |
| 1425 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1426 | case HLUA_E_ETMOUT: |
| 1427 | case HLUA_E_NOMEM: |
| 1428 | case HLUA_E_YIELD: |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1429 | case HLUA_E_ERR: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1430 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1431 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1432 | hlua_ctx_renew(lua, 0); |
| 1433 | break; |
| 1434 | |
| 1435 | case HLUA_E_OK: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1436 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1437 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1438 | break; |
| 1439 | } |
| 1440 | |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1441 | /* This is the main exit point, remove the Lua lock. */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1442 | if (lua->state_id == 0) |
Willy Tarreau | 3eb2e47 | 2021-08-20 15:47:25 +0200 | [diff] [blame] | 1443 | lua_drop_global_lock(); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1444 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1445 | return ret; |
| 1446 | } |
| 1447 | |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1448 | /* This function exit the current code. */ |
| 1449 | __LJMP static int hlua_done(lua_State *L) |
| 1450 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 1451 | struct hlua *hlua; |
| 1452 | |
| 1453 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 1454 | hlua = hlua_gethlua(L); |
| 1455 | if (!hlua) |
| 1456 | return 0; |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1457 | |
| 1458 | hlua->flags |= HLUA_EXIT; |
| 1459 | WILL_LJMP(lua_error(L)); |
| 1460 | |
| 1461 | return 0; |
| 1462 | } |
| 1463 | |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1464 | /* This function is an LUA binding. It provides a function |
| 1465 | * for deleting ACL from a referenced ACL file. |
| 1466 | */ |
| 1467 | __LJMP static int hlua_del_acl(lua_State *L) |
| 1468 | { |
| 1469 | const char *name; |
| 1470 | const char *key; |
| 1471 | struct pat_ref *ref; |
| 1472 | |
| 1473 | MAY_LJMP(check_args(L, 2, "del_acl")); |
| 1474 | |
| 1475 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1476 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1477 | |
| 1478 | ref = pat_ref_lookup(name); |
| 1479 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1480 | WILL_LJMP(luaL_error(L, "'del_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1481 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1482 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1483 | pat_ref_delete(ref, key); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1484 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1485 | return 0; |
| 1486 | } |
| 1487 | |
| 1488 | /* This function is an LUA binding. It provides a function |
| 1489 | * for deleting map entry from a referenced map file. |
| 1490 | */ |
| 1491 | static int hlua_del_map(lua_State *L) |
| 1492 | { |
| 1493 | const char *name; |
| 1494 | const char *key; |
| 1495 | struct pat_ref *ref; |
| 1496 | |
| 1497 | MAY_LJMP(check_args(L, 2, "del_map")); |
| 1498 | |
| 1499 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1500 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1501 | |
| 1502 | ref = pat_ref_lookup(name); |
| 1503 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1504 | WILL_LJMP(luaL_error(L, "'del_map': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1505 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1506 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1507 | pat_ref_delete(ref, key); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1508 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1509 | return 0; |
| 1510 | } |
| 1511 | |
| 1512 | /* This function is an LUA binding. It provides a function |
| 1513 | * for adding ACL pattern from a referenced ACL file. |
| 1514 | */ |
| 1515 | static int hlua_add_acl(lua_State *L) |
| 1516 | { |
| 1517 | const char *name; |
| 1518 | const char *key; |
| 1519 | struct pat_ref *ref; |
| 1520 | |
| 1521 | MAY_LJMP(check_args(L, 2, "add_acl")); |
| 1522 | |
| 1523 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1524 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1525 | |
| 1526 | ref = pat_ref_lookup(name); |
| 1527 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1528 | WILL_LJMP(luaL_error(L, "'add_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1529 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1530 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1531 | if (pat_ref_find_elt(ref, key) == NULL) |
| 1532 | pat_ref_add(ref, key, NULL, NULL); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1533 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1534 | return 0; |
| 1535 | } |
| 1536 | |
| 1537 | /* This function is an LUA binding. It provides a function |
| 1538 | * for setting map pattern and sample from a referenced map |
| 1539 | * file. |
| 1540 | */ |
| 1541 | static int hlua_set_map(lua_State *L) |
| 1542 | { |
| 1543 | const char *name; |
| 1544 | const char *key; |
| 1545 | const char *value; |
| 1546 | struct pat_ref *ref; |
| 1547 | |
| 1548 | MAY_LJMP(check_args(L, 3, "set_map")); |
| 1549 | |
| 1550 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1551 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1552 | value = MAY_LJMP(luaL_checkstring(L, 3)); |
| 1553 | |
| 1554 | ref = pat_ref_lookup(name); |
| 1555 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1556 | WILL_LJMP(luaL_error(L, "'set_map': unknown map file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1557 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1558 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1559 | if (pat_ref_find_elt(ref, key) != NULL) |
| 1560 | pat_ref_set(ref, key, value, NULL); |
| 1561 | else |
| 1562 | pat_ref_add(ref, key, value, NULL); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1563 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1564 | return 0; |
| 1565 | } |
| 1566 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1567 | /* A class is a lot of memory that contain data. This data can be a table, |
| 1568 | * 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] | 1569 | * metatable have an original version registered in the global context with |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1570 | * the name of the object (_G[<name>] = <metable> ). |
| 1571 | * |
| 1572 | * A metable is a table that modify the standard behavior of a standard |
| 1573 | * access to the associated data. The entries of this new metatable are |
| 1574 | * defined as is: |
| 1575 | * |
| 1576 | * http://lua-users.org/wiki/MetatableEvents |
| 1577 | * |
| 1578 | * __index |
| 1579 | * |
| 1580 | * we access an absent field in a table, the result is nil. This is |
| 1581 | * true, but it is not the whole truth. Actually, such access triggers |
| 1582 | * the interpreter to look for an __index metamethod: If there is no |
| 1583 | * such method, as usually happens, then the access results in nil; |
| 1584 | * otherwise, the metamethod will provide the result. |
| 1585 | * |
| 1586 | * Control 'prototype' inheritance. When accessing "myTable[key]" and |
| 1587 | * the key does not appear in the table, but the metatable has an __index |
| 1588 | * property: |
| 1589 | * |
| 1590 | * - if the value is a function, the function is called, passing in the |
| 1591 | * table and the key; the return value of that function is returned as |
| 1592 | * the result. |
| 1593 | * |
| 1594 | * - if the value is another table, the value of the key in that table is |
| 1595 | * asked for and returned (and if it doesn't exist in that table, but that |
| 1596 | * table's metatable has an __index property, then it continues on up) |
| 1597 | * |
| 1598 | * - Use "rawget(myTable,key)" to skip this metamethod. |
| 1599 | * |
| 1600 | * http://www.lua.org/pil/13.4.1.html |
| 1601 | * |
| 1602 | * __newindex |
| 1603 | * |
| 1604 | * Like __index, but control property assignment. |
| 1605 | * |
| 1606 | * __mode - Control weak references. A string value with one or both |
| 1607 | * of the characters 'k' and 'v' which specifies that the the |
| 1608 | * keys and/or values in the table are weak references. |
| 1609 | * |
| 1610 | * __call - Treat a table like a function. When a table is followed by |
| 1611 | * parenthesis such as "myTable( 'foo' )" and the metatable has |
| 1612 | * a __call key pointing to a function, that function is invoked |
| 1613 | * (passing any specified arguments) and the return value is |
| 1614 | * returned. |
| 1615 | * |
| 1616 | * __metatable - Hide the metatable. When "getmetatable( myTable )" is |
| 1617 | * called, if the metatable for myTable has a __metatable |
| 1618 | * key, the value of that key is returned instead of the |
| 1619 | * actual metatable. |
| 1620 | * |
| 1621 | * __tostring - Control string representation. When the builtin |
| 1622 | * "tostring( myTable )" function is called, if the metatable |
| 1623 | * for myTable has a __tostring property set to a function, |
| 1624 | * that function is invoked (passing myTable to it) and the |
| 1625 | * return value is used as the string representation. |
| 1626 | * |
| 1627 | * __len - Control table length. When the table length is requested using |
| 1628 | * the length operator ( '#' ), if the metatable for myTable has |
| 1629 | * a __len key pointing to a function, that function is invoked |
| 1630 | * (passing myTable to it) and the return value used as the value |
| 1631 | * of "#myTable". |
| 1632 | * |
| 1633 | * __gc - Userdata finalizer code. When userdata is set to be garbage |
| 1634 | * collected, if the metatable has a __gc field pointing to a |
| 1635 | * function, that function is first invoked, passing the userdata |
| 1636 | * to it. The __gc metamethod is not called for tables. |
| 1637 | * (See http://lua-users.org/lists/lua-l/2006-11/msg00508.html) |
| 1638 | * |
| 1639 | * Special metamethods for redefining standard operators: |
| 1640 | * http://www.lua.org/pil/13.1.html |
| 1641 | * |
| 1642 | * __add "+" |
| 1643 | * __sub "-" |
| 1644 | * __mul "*" |
| 1645 | * __div "/" |
| 1646 | * __unm "!" |
| 1647 | * __pow "^" |
| 1648 | * __concat ".." |
| 1649 | * |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1650 | * Special methods for redefining standard relations |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1651 | * http://www.lua.org/pil/13.2.html |
| 1652 | * |
| 1653 | * __eq "==" |
| 1654 | * __lt "<" |
| 1655 | * __le "<=" |
| 1656 | */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1657 | |
| 1658 | /* |
| 1659 | * |
| 1660 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1661 | * Class Map |
| 1662 | * |
| 1663 | * |
| 1664 | */ |
| 1665 | |
| 1666 | /* Returns a struct hlua_map if the stack entry "ud" is |
| 1667 | * a class session, otherwise it throws an error. |
| 1668 | */ |
| 1669 | __LJMP static struct map_descriptor *hlua_checkmap(lua_State *L, int ud) |
| 1670 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1671 | return MAY_LJMP(hlua_checkudata(L, ud, class_map_ref)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1672 | } |
| 1673 | |
| 1674 | /* This function is the map constructor. It don't need |
| 1675 | * the class Map object. It creates and return a new Map |
| 1676 | * object. It must be called only during "body" or "init" |
| 1677 | * context because it process some filesystem accesses. |
| 1678 | */ |
| 1679 | __LJMP static int hlua_map_new(struct lua_State *L) |
| 1680 | { |
| 1681 | const char *fn; |
| 1682 | int match = PAT_MATCH_STR; |
| 1683 | struct sample_conv conv; |
| 1684 | const char *file = ""; |
| 1685 | int line = 0; |
| 1686 | lua_Debug ar; |
| 1687 | char *err = NULL; |
| 1688 | struct arg args[2]; |
| 1689 | |
| 1690 | if (lua_gettop(L) < 1 || lua_gettop(L) > 2) |
| 1691 | WILL_LJMP(luaL_error(L, "'new' needs at least 1 argument.")); |
| 1692 | |
| 1693 | fn = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1694 | |
| 1695 | if (lua_gettop(L) >= 2) { |
| 1696 | match = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 1697 | if (match < 0 || match >= PAT_MATCH_NUM) |
| 1698 | WILL_LJMP(luaL_error(L, "'new' needs a valid match method.")); |
| 1699 | } |
| 1700 | |
| 1701 | /* Get Lua filename and line number. */ |
| 1702 | if (lua_getstack(L, 1, &ar)) { /* check function at level */ |
| 1703 | lua_getinfo(L, "Sl", &ar); /* get info about it */ |
| 1704 | if (ar.currentline > 0) { /* is there info? */ |
| 1705 | file = ar.short_src; |
| 1706 | line = ar.currentline; |
| 1707 | } |
| 1708 | } |
| 1709 | |
| 1710 | /* fill fake sample_conv struct. */ |
| 1711 | conv.kw = ""; /* unused. */ |
| 1712 | conv.process = NULL; /* unused. */ |
| 1713 | conv.arg_mask = 0; /* unused. */ |
| 1714 | conv.val_args = NULL; /* unused. */ |
| 1715 | conv.out_type = SMP_T_STR; |
| 1716 | conv.private = (void *)(long)match; |
| 1717 | switch (match) { |
| 1718 | case PAT_MATCH_STR: conv.in_type = SMP_T_STR; break; |
| 1719 | case PAT_MATCH_BEG: conv.in_type = SMP_T_STR; break; |
| 1720 | case PAT_MATCH_SUB: conv.in_type = SMP_T_STR; break; |
| 1721 | case PAT_MATCH_DIR: conv.in_type = SMP_T_STR; break; |
| 1722 | case PAT_MATCH_DOM: conv.in_type = SMP_T_STR; break; |
| 1723 | case PAT_MATCH_END: conv.in_type = SMP_T_STR; break; |
| 1724 | case PAT_MATCH_REG: conv.in_type = SMP_T_STR; break; |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1725 | case PAT_MATCH_INT: conv.in_type = SMP_T_SINT; break; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1726 | case PAT_MATCH_IP: conv.in_type = SMP_T_ADDR; break; |
| 1727 | default: |
| 1728 | WILL_LJMP(luaL_error(L, "'new' doesn't support this match mode.")); |
| 1729 | } |
| 1730 | |
| 1731 | /* fill fake args. */ |
| 1732 | args[0].type = ARGT_STR; |
Christopher Faulet | 73292e9 | 2020-08-06 08:40:09 +0200 | [diff] [blame] | 1733 | args[0].data.str.area = strdup(fn); |
| 1734 | args[0].data.str.data = strlen(fn); |
| 1735 | args[0].data.str.size = args[0].data.str.data+1; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1736 | args[1].type = ARGT_STOP; |
| 1737 | |
| 1738 | /* load the map. */ |
| 1739 | if (!sample_load_map(args, &conv, file, line, &err)) { |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 1740 | /* error case: we can't use luaL_error because we must |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1741 | * free the err variable. |
| 1742 | */ |
| 1743 | luaL_where(L, 1); |
| 1744 | lua_pushfstring(L, "'new': %s.", err); |
| 1745 | lua_concat(L, 2); |
| 1746 | free(err); |
Christopher Faulet | 6ad7df4 | 2020-08-07 11:45:18 +0200 | [diff] [blame] | 1747 | chunk_destroy(&args[0].data.str); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1748 | WILL_LJMP(lua_error(L)); |
| 1749 | } |
| 1750 | |
| 1751 | /* create the lua object. */ |
| 1752 | lua_newtable(L); |
| 1753 | lua_pushlightuserdata(L, args[0].data.map); |
| 1754 | lua_rawseti(L, -2, 0); |
| 1755 | |
| 1756 | /* Pop a class Map metatable and affect it to the userdata. */ |
| 1757 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_map_ref); |
| 1758 | lua_setmetatable(L, -2); |
| 1759 | |
| 1760 | |
| 1761 | return 1; |
| 1762 | } |
| 1763 | |
| 1764 | __LJMP static inline int _hlua_map_lookup(struct lua_State *L, int str) |
| 1765 | { |
| 1766 | struct map_descriptor *desc; |
| 1767 | struct pattern *pat; |
| 1768 | struct sample smp; |
| 1769 | |
| 1770 | MAY_LJMP(check_args(L, 2, "lookup")); |
| 1771 | desc = MAY_LJMP(hlua_checkmap(L, 1)); |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1772 | if (desc->pat.expect_type == SMP_T_SINT) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1773 | smp.data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1774 | smp.data.u.sint = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1775 | } |
| 1776 | else { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1777 | smp.data.type = SMP_T_STR; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1778 | smp.flags = SMP_F_CONST; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1779 | 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] | 1780 | smp.data.u.str.size = smp.data.u.str.data + 1; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1781 | } |
| 1782 | |
| 1783 | pat = pattern_exec_match(&desc->pat, &smp, 1); |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1784 | if (!pat || !pat->data) { |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1785 | if (str) |
| 1786 | lua_pushstring(L, ""); |
| 1787 | else |
| 1788 | lua_pushnil(L); |
| 1789 | return 1; |
| 1790 | } |
| 1791 | |
| 1792 | /* 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] | 1793 | 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] | 1794 | return 1; |
| 1795 | } |
| 1796 | |
| 1797 | __LJMP static int hlua_map_lookup(struct lua_State *L) |
| 1798 | { |
| 1799 | return _hlua_map_lookup(L, 0); |
| 1800 | } |
| 1801 | |
| 1802 | __LJMP static int hlua_map_slookup(struct lua_State *L) |
| 1803 | { |
| 1804 | return _hlua_map_lookup(L, 1); |
| 1805 | } |
| 1806 | |
| 1807 | /* |
| 1808 | * |
| 1809 | * |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1810 | * Class Socket |
| 1811 | * |
| 1812 | * |
| 1813 | */ |
| 1814 | |
| 1815 | __LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud) |
| 1816 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1817 | return MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1818 | } |
| 1819 | |
| 1820 | /* 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] | 1821 | * connection. It is used for notify space available to send or data |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1822 | * received. |
| 1823 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1824 | static void hlua_socket_handler(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1825 | { |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1826 | struct stream_interface *si = appctx->owner; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1827 | |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1828 | if (appctx->ctx.hlua_cosocket.die) { |
| 1829 | si_shutw(si); |
| 1830 | si_shutr(si); |
| 1831 | si_ic(si)->flags |= CF_READ_NULL; |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1832 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1833 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1834 | stream_shutdown(si_strm(si), SF_ERR_KILLED); |
| 1835 | } |
| 1836 | |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 1837 | /* If we can't write, wakeup the pending write signals. */ |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1838 | if (channel_output_closed(si_ic(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1839 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1840 | |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 1841 | /* If we can't read, wakeup the pending read signals. */ |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1842 | if (channel_input_closed(si_oc(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1843 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1844 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 1845 | /* if the connection is not established, inform the stream that we want |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 1846 | * to be notified whenever the connection completes. |
| 1847 | */ |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 1848 | if (si_opposite(si)->state < SI_ST_EST) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 1849 | si_cant_get(si); |
Willy Tarreau | 12c2423 | 2018-12-06 15:29:50 +0100 | [diff] [blame] | 1850 | si_rx_conn_blk(si); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1851 | si_rx_endp_more(si); |
Willy Tarreau | d4da196 | 2015-04-20 01:31:23 +0200 | [diff] [blame] | 1852 | return; |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 1853 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1854 | |
| 1855 | /* This function is called after the connect. */ |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 1856 | appctx->ctx.hlua_cosocket.connected = 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1857 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1858 | /* 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] | 1859 | if (channel_may_recv(si_ic(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1860 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1861 | |
| 1862 | /* Wake the tasks which wants to read if the buffer contains data. */ |
Thierry FOURNIER | eba6f64 | 2015-09-26 22:01:07 +0200 | [diff] [blame] | 1863 | if (!channel_is_empty(si_oc(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1864 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1865 | |
| 1866 | /* Some data were injected in the buffer, notify the stream |
| 1867 | * interface. |
| 1868 | */ |
| 1869 | if (!channel_is_empty(si_ic(si))) |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 1870 | si_update(si); |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1871 | |
| 1872 | /* If write notifications are registered, we considers we want |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1873 | * to write, so we clear the blocking flag. |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1874 | */ |
| 1875 | if (notification_registered(&appctx->ctx.hlua_cosocket.wake_on_write)) |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1876 | si_rx_endp_more(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1877 | } |
| 1878 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1879 | /* This function is called when the "struct stream" is destroyed. |
| 1880 | * Remove the link from the object to this stream. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1881 | * Wake all the pending signals. |
| 1882 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1883 | static void hlua_socket_release(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1884 | { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1885 | struct xref *peer; |
| 1886 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1887 | /* Remove my link in the original object. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1888 | peer = xref_get_peer_and_lock(&appctx->ctx.hlua_cosocket.xref); |
| 1889 | if (peer) |
| 1890 | xref_disconnect(&appctx->ctx.hlua_cosocket.xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1891 | |
| 1892 | /* Wake all the task waiting for me. */ |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1893 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1894 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1895 | } |
| 1896 | |
| 1897 | /* If the garbage collectio of the object is launch, nobody |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1898 | * uses this object. If the stream does not exists, just quit. |
| 1899 | * Send the shutdown signal to the stream. In some cases, |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1900 | * pending signal can rest in the read and write lists. destroy |
| 1901 | * it. |
| 1902 | */ |
| 1903 | __LJMP static int hlua_socket_gc(lua_State *L) |
| 1904 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1905 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1906 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1907 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1908 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1909 | MAY_LJMP(check_args(L, 1, "__gc")); |
| 1910 | |
| 1911 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1912 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1913 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1914 | return 0; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1915 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1916 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1917 | /* Set the flag which destroy the session. */ |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1918 | appctx->ctx.hlua_cosocket.die = 1; |
| 1919 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1920 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1921 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1922 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1923 | return 0; |
| 1924 | } |
| 1925 | |
| 1926 | /* The close function send shutdown signal and break the |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1927 | * links between the stream and the object. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1928 | */ |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 1929 | __LJMP static int hlua_socket_close_helper(lua_State *L) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1930 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1931 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1932 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1933 | struct xref *peer; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 1934 | struct hlua *hlua; |
| 1935 | |
| 1936 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 1937 | hlua = hlua_gethlua(L); |
| 1938 | if (!hlua) |
| 1939 | return 0; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1940 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1941 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 1942 | |
| 1943 | /* Check if we run on the same thread than the xreator thread. |
| 1944 | * We cannot access to the socket if the thread is different. |
| 1945 | */ |
| 1946 | if (socket->tid != tid) |
| 1947 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 1948 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1949 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1950 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1951 | return 0; |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 1952 | |
| 1953 | hlua->gc_count--; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1954 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1955 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1956 | /* Set the flag which destroy the session. */ |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1957 | appctx->ctx.hlua_cosocket.die = 1; |
| 1958 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1959 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1960 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1961 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1962 | return 0; |
| 1963 | } |
| 1964 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 1965 | /* The close function calls close_helper. |
| 1966 | */ |
| 1967 | __LJMP static int hlua_socket_close(lua_State *L) |
| 1968 | { |
| 1969 | MAY_LJMP(check_args(L, 1, "close")); |
| 1970 | return hlua_socket_close_helper(L); |
| 1971 | } |
| 1972 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1973 | /* This Lua function assumes that the stack contain three parameters. |
| 1974 | * 1 - USERDATA containing a struct socket |
| 1975 | * 2 - INTEGER with values of the macro defined below |
| 1976 | * If the integer is -1, we must read at most one line. |
| 1977 | * If the integer is -2, we ust read all the data until the |
| 1978 | * end of the stream. |
| 1979 | * If the integer is positive value, we must read a number of |
| 1980 | * bytes corresponding to this value. |
| 1981 | */ |
| 1982 | #define HLSR_READ_LINE (-1) |
| 1983 | #define HLSR_READ_ALL (-2) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1984 | __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] | 1985 | { |
| 1986 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 1987 | int wanted = lua_tointeger(L, 2); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 1988 | struct hlua *hlua; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1989 | struct appctx *appctx; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 1990 | size_t len; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1991 | int nblk; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 1992 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 1993 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 1994 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 1995 | size_t len2; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 1996 | int skip_at_end = 0; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 1997 | struct channel *oc; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1998 | struct stream_interface *si; |
| 1999 | struct stream *s; |
| 2000 | struct xref *peer; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 2001 | int missing_bytes; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2002 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2003 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 2004 | hlua = hlua_gethlua(L); |
| 2005 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2006 | /* Check if this lua stack is schedulable. */ |
| 2007 | if (!hlua || !hlua->task) |
| 2008 | WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in " |
| 2009 | "'frontend', 'backend' or 'task'")); |
| 2010 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2011 | /* Check if we run on the same thread than the xreator thread. |
| 2012 | * We cannot access to the socket if the thread is different. |
| 2013 | */ |
| 2014 | if (socket->tid != tid) |
| 2015 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2016 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2017 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2018 | peer = xref_get_peer_and_lock(&socket->xref); |
| 2019 | if (!peer) |
| 2020 | goto no_peer; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2021 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2022 | si = appctx->owner; |
| 2023 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2024 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2025 | oc = &s->res; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2026 | if (wanted == HLSR_READ_LINE) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2027 | /* Read line. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2028 | nblk = co_getline_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2029 | if (nblk < 0) /* Connection close. */ |
| 2030 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2031 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2032 | goto connection_empty; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 2033 | |
| 2034 | /* remove final \r\n. */ |
| 2035 | if (nblk == 1) { |
| 2036 | if (blk1[len1-1] == '\n') { |
| 2037 | len1--; |
| 2038 | skip_at_end++; |
| 2039 | if (blk1[len1-1] == '\r') { |
| 2040 | len1--; |
| 2041 | skip_at_end++; |
| 2042 | } |
| 2043 | } |
| 2044 | } |
| 2045 | else { |
| 2046 | if (blk2[len2-1] == '\n') { |
| 2047 | len2--; |
| 2048 | skip_at_end++; |
| 2049 | if (blk2[len2-1] == '\r') { |
| 2050 | len2--; |
| 2051 | skip_at_end++; |
| 2052 | } |
| 2053 | } |
| 2054 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2055 | } |
| 2056 | |
| 2057 | else if (wanted == HLSR_READ_ALL) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2058 | /* Read all the available data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2059 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2060 | if (nblk < 0) /* Connection close. */ |
| 2061 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2062 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2063 | goto connection_empty; |
| 2064 | } |
| 2065 | |
| 2066 | else { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2067 | /* Read a block of data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2068 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2069 | if (nblk < 0) /* Connection close. */ |
| 2070 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2071 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2072 | goto connection_empty; |
| 2073 | |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 2074 | missing_bytes = wanted - socket->b.n; |
| 2075 | if (len1 > missing_bytes) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2076 | nblk = 1; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 2077 | len1 = missing_bytes; |
| 2078 | } if (nblk == 2 && len1 + len2 > missing_bytes) |
| 2079 | len2 = missing_bytes - len1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2080 | } |
| 2081 | |
| 2082 | len = len1; |
| 2083 | |
| 2084 | luaL_addlstring(&socket->b, blk1, len1); |
| 2085 | if (nblk == 2) { |
| 2086 | len += len2; |
| 2087 | luaL_addlstring(&socket->b, blk2, len2); |
| 2088 | } |
| 2089 | |
| 2090 | /* Consume data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2091 | co_skip(oc, len + skip_at_end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2092 | |
| 2093 | /* Don't wait anything. */ |
Thierry FOURNIER | 7e4ee47 | 2018-05-25 15:03:50 +0200 | [diff] [blame] | 2094 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2095 | |
| 2096 | /* If the pattern reclaim to read all the data |
| 2097 | * in the connection, got out. |
| 2098 | */ |
| 2099 | if (wanted == HLSR_READ_ALL) |
| 2100 | goto connection_empty; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 2101 | else if (wanted >= 0 && socket->b.n < wanted) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2102 | goto connection_empty; |
| 2103 | |
| 2104 | /* Return result. */ |
| 2105 | luaL_pushresult(&socket->b); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2106 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2107 | return 1; |
| 2108 | |
| 2109 | connection_closed: |
| 2110 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2111 | xref_unlock(&socket->xref, peer); |
| 2112 | |
| 2113 | no_peer: |
| 2114 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2115 | /* If the buffer containds data. */ |
| 2116 | if (socket->b.n > 0) { |
| 2117 | luaL_pushresult(&socket->b); |
| 2118 | return 1; |
| 2119 | } |
| 2120 | lua_pushnil(L); |
| 2121 | lua_pushstring(L, "connection closed."); |
| 2122 | return 2; |
| 2123 | |
| 2124 | connection_empty: |
| 2125 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2126 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_read, hlua->task)) { |
| 2127 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2128 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2129 | } |
| 2130 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2131 | 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] | 2132 | return 0; |
| 2133 | } |
| 2134 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2135 | /* This Lua function gets two parameters. The first one can be string |
| 2136 | * or a number. If the string is "*l", the user requires one line. If |
| 2137 | * 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] | 2138 | * If the value is a number, the user require a number of bytes equal |
| 2139 | * to the value. The default value is "*l" (a line). |
| 2140 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2141 | * This parameter with a variable type is converted in integer. This |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2142 | * integer takes this values: |
| 2143 | * -1 : read a line |
| 2144 | * -2 : read all the stream |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2145 | * >0 : amount of bytes. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2146 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2147 | * The second parameter is optional. It contains a string that must be |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2148 | * concatenated with the read data. |
| 2149 | */ |
| 2150 | __LJMP static int hlua_socket_receive(struct lua_State *L) |
| 2151 | { |
| 2152 | int wanted = HLSR_READ_LINE; |
| 2153 | const char *pattern; |
Christopher Faulet | c31b200 | 2021-05-03 10:11:13 +0200 | [diff] [blame] | 2154 | int lastarg, type; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2155 | char *error; |
| 2156 | size_t len; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2157 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2158 | |
| 2159 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 2160 | WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments.")); |
| 2161 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2162 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2163 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2164 | /* Check if we run on the same thread than the xreator thread. |
| 2165 | * We cannot access to the socket if the thread is different. |
| 2166 | */ |
| 2167 | if (socket->tid != tid) |
| 2168 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2169 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2170 | /* check for pattern. */ |
| 2171 | if (lua_gettop(L) >= 2) { |
| 2172 | type = lua_type(L, 2); |
| 2173 | if (type == LUA_TSTRING) { |
| 2174 | pattern = lua_tostring(L, 2); |
| 2175 | if (strcmp(pattern, "*a") == 0) |
| 2176 | wanted = HLSR_READ_ALL; |
| 2177 | else if (strcmp(pattern, "*l") == 0) |
| 2178 | wanted = HLSR_READ_LINE; |
| 2179 | else { |
| 2180 | wanted = strtoll(pattern, &error, 10); |
| 2181 | if (*error != '\0') |
| 2182 | WILL_LJMP(luaL_error(L, "Unsupported pattern.")); |
| 2183 | } |
| 2184 | } |
| 2185 | else if (type == LUA_TNUMBER) { |
| 2186 | wanted = lua_tointeger(L, 2); |
| 2187 | if (wanted < 0) |
| 2188 | WILL_LJMP(luaL_error(L, "Unsupported size.")); |
| 2189 | } |
| 2190 | } |
| 2191 | |
| 2192 | /* Set pattern. */ |
| 2193 | lua_pushinteger(L, wanted); |
Tim Duesterhus | c6e377e | 2018-01-04 19:32:13 +0100 | [diff] [blame] | 2194 | |
| 2195 | /* Check if we would replace the top by itself. */ |
| 2196 | if (lua_gettop(L) != 2) |
| 2197 | lua_replace(L, 2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2198 | |
Christopher Faulet | c31b200 | 2021-05-03 10:11:13 +0200 | [diff] [blame] | 2199 | /* Save index of the top of the stack because since buffers are used, it |
| 2200 | * may change |
| 2201 | */ |
| 2202 | lastarg = lua_gettop(L); |
| 2203 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2204 | /* init buffer, and fill it with prefix. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2205 | luaL_buffinit(L, &socket->b); |
| 2206 | |
| 2207 | /* Check prefix. */ |
Christopher Faulet | c31b200 | 2021-05-03 10:11:13 +0200 | [diff] [blame] | 2208 | if (lastarg >= 3) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2209 | if (lua_type(L, 3) != LUA_TSTRING) |
| 2210 | WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix")); |
| 2211 | pattern = lua_tolstring(L, 3, &len); |
| 2212 | luaL_addlstring(&socket->b, pattern, len); |
| 2213 | } |
| 2214 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2215 | return __LJMP(hlua_socket_receive_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2216 | } |
| 2217 | |
| 2218 | /* Write the Lua input string in the output buffer. |
Mark Lakes | 22154b4 | 2018-01-29 14:38:40 -0800 | [diff] [blame] | 2219 | * This function returns a yield if no space is available. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2220 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2221 | 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] | 2222 | { |
| 2223 | struct hlua_socket *socket; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2224 | struct hlua *hlua; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2225 | struct appctx *appctx; |
| 2226 | size_t buf_len; |
| 2227 | const char *buf; |
| 2228 | int len; |
| 2229 | int send_len; |
| 2230 | int sent; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2231 | struct xref *peer; |
| 2232 | struct stream_interface *si; |
| 2233 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2234 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2235 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 2236 | hlua = hlua_gethlua(L); |
| 2237 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2238 | /* Check if this lua stack is schedulable. */ |
| 2239 | if (!hlua || !hlua->task) |
| 2240 | WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in " |
| 2241 | "'frontend', 'backend' or 'task'")); |
| 2242 | |
| 2243 | /* Get object */ |
| 2244 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2245 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2246 | sent = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2247 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2248 | /* Check if we run on the same thread than the xreator thread. |
| 2249 | * We cannot access to the socket if the thread is different. |
| 2250 | */ |
| 2251 | if (socket->tid != tid) |
| 2252 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2253 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2254 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2255 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2256 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2257 | lua_pushinteger(L, -1); |
| 2258 | return 1; |
| 2259 | } |
| 2260 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2261 | si = appctx->owner; |
| 2262 | s = si_strm(si); |
| 2263 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2264 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2265 | if (channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2266 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2267 | lua_pushinteger(L, -1); |
| 2268 | return 1; |
| 2269 | } |
| 2270 | |
| 2271 | /* Update the input buffer data. */ |
| 2272 | buf += sent; |
| 2273 | send_len = buf_len - sent; |
| 2274 | |
| 2275 | /* All the data are sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2276 | if (sent >= buf_len) { |
| 2277 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2278 | return 1; /* Implicitly return the length sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2279 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2280 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2281 | /* Check if the buffer is available because HAProxy doesn't allocate |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 2282 | * the request buffer if its not required. |
| 2283 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2284 | if (s->req.buf.size == 0) { |
Willy Tarreau | 581abd3 | 2018-10-25 10:21:41 +0200 | [diff] [blame] | 2285 | if (!si_alloc_ibuf(si, &appctx->buffer_wait)) |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2286 | goto hlua_socket_write_yield_return; |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 2287 | } |
| 2288 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2289 | /* Check for available space. */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2290 | len = b_room(&s->req.buf); |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2291 | if (len <= 0) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2292 | goto hlua_socket_write_yield_return; |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2293 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2294 | |
| 2295 | /* send data */ |
| 2296 | if (len < send_len) |
| 2297 | send_len = len; |
Thierry FOURNIER | 66b8919 | 2018-05-27 01:14:47 +0200 | [diff] [blame] | 2298 | len = ci_putblk(&s->req, buf, send_len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2299 | |
| 2300 | /* "Not enough space" (-1), "Buffer too little to contain |
| 2301 | * the data" (-2) are not expected because the available length |
| 2302 | * is tested. |
| 2303 | * Other unknown error are also not expected. |
| 2304 | */ |
| 2305 | if (len <= 0) { |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2306 | if (len == -1) |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2307 | s->req.flags |= CF_WAKE_WRITE; |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2308 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 2309 | MAY_LJMP(hlua_socket_close_helper(L)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2310 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2311 | lua_pushinteger(L, -1); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2312 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2313 | return 1; |
| 2314 | } |
| 2315 | |
| 2316 | /* update buffers. */ |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 2317 | appctx_wakeup(appctx); |
Willy Tarreau | de70fa1 | 2015-09-26 11:25:05 +0200 | [diff] [blame] | 2318 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2319 | s->req.rex = TICK_ETERNITY; |
| 2320 | s->res.wex = TICK_ETERNITY; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2321 | |
| 2322 | /* Update length sent. */ |
| 2323 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2324 | lua_pushinteger(L, sent + len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2325 | |
| 2326 | /* All the data buffer is sent ? */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2327 | if (sent + len >= buf_len) { |
| 2328 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2329 | return 1; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2330 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2331 | |
| 2332 | hlua_socket_write_yield_return: |
Thierry FOURNIER | ba42fcd | 2018-05-27 00:59:48 +0200 | [diff] [blame] | 2333 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2334 | xref_unlock(&socket->xref, peer); |
| 2335 | WILL_LJMP(luaL_error(L, "out of memory")); |
| 2336 | } |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2337 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2338 | 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] | 2339 | return 0; |
| 2340 | } |
| 2341 | |
| 2342 | /* This function initiate the send of data. It just check the input |
| 2343 | * parameters and push an integer in the Lua stack that contain the |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2344 | * 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] | 2345 | * "hlua_socket_write_yield" that can yield. |
| 2346 | * |
| 2347 | * The Lua function gets between 3 and 4 parameters. The first one is |
| 2348 | * the associated object. The second is a string buffer. The third is |
| 2349 | * a facultative integer that represents where is the buffer position |
| 2350 | * of the start of the data that can send. The first byte is the |
| 2351 | * position "1". The default value is "1". The fourth argument is a |
| 2352 | * facultative integer that represents where is the buffer position |
| 2353 | * of the end of the data that can send. The default is the last byte. |
| 2354 | */ |
| 2355 | static int hlua_socket_send(struct lua_State *L) |
| 2356 | { |
| 2357 | int i; |
| 2358 | int j; |
| 2359 | const char *buf; |
| 2360 | size_t buf_len; |
| 2361 | |
| 2362 | /* Check number of arguments. */ |
| 2363 | if (lua_gettop(L) < 2 || lua_gettop(L) > 4) |
| 2364 | WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments")); |
| 2365 | |
| 2366 | /* Get the string. */ |
| 2367 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
| 2368 | |
| 2369 | /* Get and check j. */ |
| 2370 | if (lua_gettop(L) == 4) { |
| 2371 | j = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 2372 | if (j < 0) |
| 2373 | j = buf_len + j + 1; |
| 2374 | if (j > buf_len) |
| 2375 | j = buf_len + 1; |
| 2376 | lua_pop(L, 1); |
| 2377 | } |
| 2378 | else |
| 2379 | j = buf_len; |
| 2380 | |
| 2381 | /* Get and check i. */ |
| 2382 | if (lua_gettop(L) == 3) { |
| 2383 | i = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2384 | if (i < 0) |
| 2385 | i = buf_len + i + 1; |
| 2386 | if (i > buf_len) |
| 2387 | i = buf_len + 1; |
| 2388 | lua_pop(L, 1); |
| 2389 | } else |
| 2390 | i = 1; |
| 2391 | |
| 2392 | /* Check bth i and j. */ |
| 2393 | if (i > j) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2394 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2395 | return 1; |
| 2396 | } |
| 2397 | if (i == 0 && j == 0) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2398 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2399 | return 1; |
| 2400 | } |
| 2401 | if (i == 0) |
| 2402 | i = 1; |
| 2403 | if (j == 0) |
| 2404 | j = 1; |
| 2405 | |
| 2406 | /* Pop the string. */ |
| 2407 | lua_pop(L, 1); |
| 2408 | |
| 2409 | /* Update the buffer length. */ |
| 2410 | buf += i - 1; |
| 2411 | buf_len = j - i + 1; |
| 2412 | lua_pushlstring(L, buf, buf_len); |
| 2413 | |
| 2414 | /* This unsigned is used to remember the amount of sent data. */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2415 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2416 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2417 | return MAY_LJMP(hlua_socket_write_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2418 | } |
| 2419 | |
Willy Tarreau | 22b0a68 | 2015-06-17 19:43:49 +0200 | [diff] [blame] | 2420 | #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] | 2421 | __LJMP static inline int hlua_socket_info(struct lua_State *L, struct sockaddr_storage *addr) |
| 2422 | { |
| 2423 | static char buffer[SOCKET_INFO_MAX_LEN]; |
| 2424 | int ret; |
| 2425 | int len; |
| 2426 | char *p; |
| 2427 | |
| 2428 | ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1); |
| 2429 | if (ret <= 0) { |
| 2430 | lua_pushnil(L); |
| 2431 | return 1; |
| 2432 | } |
| 2433 | |
| 2434 | if (ret == AF_UNIX) { |
| 2435 | lua_pushstring(L, buffer+1); |
| 2436 | return 1; |
| 2437 | } |
| 2438 | else if (ret == AF_INET6) { |
| 2439 | buffer[0] = '['; |
| 2440 | len = strlen(buffer); |
| 2441 | buffer[len] = ']'; |
| 2442 | len++; |
| 2443 | buffer[len] = ':'; |
| 2444 | len++; |
| 2445 | p = buffer; |
| 2446 | } |
| 2447 | else if (ret == AF_INET) { |
| 2448 | p = buffer + 1; |
| 2449 | len = strlen(p); |
| 2450 | p[len] = ':'; |
| 2451 | len++; |
| 2452 | } |
| 2453 | else { |
| 2454 | lua_pushnil(L); |
| 2455 | return 1; |
| 2456 | } |
| 2457 | |
| 2458 | if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) { |
| 2459 | lua_pushnil(L); |
| 2460 | return 1; |
| 2461 | } |
| 2462 | |
| 2463 | lua_pushstring(L, p); |
| 2464 | return 1; |
| 2465 | } |
| 2466 | |
| 2467 | /* Returns information about the peer of the connection. */ |
| 2468 | __LJMP static int hlua_socket_getpeername(struct lua_State *L) |
| 2469 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2470 | struct hlua_socket *socket; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2471 | struct xref *peer; |
| 2472 | struct appctx *appctx; |
| 2473 | struct stream_interface *si; |
| 2474 | struct stream *s; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2475 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2476 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2477 | MAY_LJMP(check_args(L, 1, "getpeername")); |
| 2478 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2479 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2480 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2481 | /* Check if we run on the same thread than the xreator thread. |
| 2482 | * We cannot access to the socket if the thread is different. |
| 2483 | */ |
| 2484 | if (socket->tid != tid) |
| 2485 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2486 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2487 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2488 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2489 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2490 | lua_pushnil(L); |
| 2491 | return 1; |
| 2492 | } |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2493 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2494 | si = appctx->owner; |
| 2495 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2496 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2497 | if (!s->target_addr) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2498 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2499 | lua_pushnil(L); |
| 2500 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2501 | } |
| 2502 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2503 | ret = MAY_LJMP(hlua_socket_info(L, s->target_addr)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2504 | xref_unlock(&socket->xref, peer); |
| 2505 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2506 | } |
| 2507 | |
| 2508 | /* Returns information about my connection side. */ |
| 2509 | static int hlua_socket_getsockname(struct lua_State *L) |
| 2510 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2511 | struct hlua_socket *socket; |
| 2512 | struct connection *conn; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2513 | struct appctx *appctx; |
| 2514 | struct xref *peer; |
| 2515 | struct stream_interface *si; |
| 2516 | struct stream *s; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2517 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2518 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2519 | MAY_LJMP(check_args(L, 1, "getsockname")); |
| 2520 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2521 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2522 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2523 | /* Check if we run on the same thread than the xreator thread. |
| 2524 | * We cannot access to the socket if the thread is different. |
| 2525 | */ |
| 2526 | if (socket->tid != tid) |
| 2527 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2528 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2529 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2530 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2531 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2532 | lua_pushnil(L); |
| 2533 | return 1; |
| 2534 | } |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2535 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2536 | si = appctx->owner; |
| 2537 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2538 | |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 2539 | conn = cs_conn(objt_cs(s->si[1].end)); |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2540 | if (!conn || !conn_get_src(conn)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2541 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2542 | lua_pushnil(L); |
| 2543 | return 1; |
| 2544 | } |
| 2545 | |
Willy Tarreau | 9da9a6f | 2019-07-17 14:49:44 +0200 | [diff] [blame] | 2546 | ret = hlua_socket_info(L, conn->src); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2547 | xref_unlock(&socket->xref, peer); |
| 2548 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2549 | } |
| 2550 | |
| 2551 | /* This struct define the applet. */ |
Willy Tarreau | 3057645 | 2015-04-13 13:50:30 +0200 | [diff] [blame] | 2552 | static struct applet update_applet = { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2553 | .obj_type = OBJ_TYPE_APPLET, |
| 2554 | .name = "<LUA_TCP>", |
| 2555 | .fct = hlua_socket_handler, |
| 2556 | .release = hlua_socket_release, |
| 2557 | }; |
| 2558 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2559 | __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] | 2560 | { |
| 2561 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2562 | struct hlua *hlua; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2563 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2564 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2565 | struct stream_interface *si; |
| 2566 | struct stream *s; |
| 2567 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2568 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 2569 | hlua = hlua_gethlua(L); |
| 2570 | if (!hlua) |
| 2571 | return 0; |
| 2572 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2573 | /* Check if we run on the same thread than the xreator thread. |
| 2574 | * We cannot access to the socket if the thread is different. |
| 2575 | */ |
| 2576 | if (socket->tid != tid) |
| 2577 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2578 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2579 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2580 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2581 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2582 | lua_pushnil(L); |
| 2583 | lua_pushstring(L, "Can't connect"); |
| 2584 | return 2; |
| 2585 | } |
| 2586 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2587 | si = appctx->owner; |
| 2588 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2589 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2590 | /* Check if we run on the same thread than the xreator thread. |
| 2591 | * We cannot access to the socket if the thread is different. |
| 2592 | */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2593 | if (socket->tid != tid) { |
| 2594 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2595 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2596 | } |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2597 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2598 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2599 | if (!hlua || channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2600 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2601 | lua_pushnil(L); |
| 2602 | lua_pushstring(L, "Can't connect"); |
| 2603 | return 2; |
| 2604 | } |
| 2605 | |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 2606 | appctx = __objt_appctx(s->si[0].end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2607 | |
| 2608 | /* Check for connection established. */ |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2609 | if (appctx->ctx.hlua_cosocket.connected) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2610 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2611 | lua_pushinteger(L, 1); |
| 2612 | return 1; |
| 2613 | } |
| 2614 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2615 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2616 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2617 | WILL_LJMP(luaL_error(L, "out of memory error")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2618 | } |
| 2619 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2620 | 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] | 2621 | return 0; |
| 2622 | } |
| 2623 | |
| 2624 | /* This function fail or initite the connection. */ |
| 2625 | __LJMP static int hlua_socket_connect(struct lua_State *L) |
| 2626 | { |
| 2627 | struct hlua_socket *socket; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2628 | int port = -1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2629 | const char *ip; |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2630 | struct hlua *hlua; |
| 2631 | struct appctx *appctx; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2632 | int low, high; |
| 2633 | struct sockaddr_storage *addr; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2634 | struct xref *peer; |
| 2635 | struct stream_interface *si; |
| 2636 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2637 | |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2638 | if (lua_gettop(L) < 2) |
| 2639 | WILL_LJMP(luaL_error(L, "connect: need at least 2 arguments")); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2640 | |
| 2641 | /* Get args. */ |
| 2642 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2643 | |
| 2644 | /* Check if we run on the same thread than the xreator thread. |
| 2645 | * We cannot access to the socket if the thread is different. |
| 2646 | */ |
| 2647 | if (socket->tid != tid) |
| 2648 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2649 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2650 | ip = MAY_LJMP(luaL_checkstring(L, 2)); |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 2651 | if (lua_gettop(L) >= 3) { |
| 2652 | luaL_Buffer b; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2653 | port = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2654 | |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 2655 | /* Force the ip to end with a colon, to support IPv6 addresses |
| 2656 | * that are not enclosed within square brackets. |
| 2657 | */ |
| 2658 | if (port > 0) { |
| 2659 | luaL_buffinit(L, &b); |
| 2660 | luaL_addstring(&b, ip); |
| 2661 | luaL_addchar(&b, ':'); |
| 2662 | luaL_pushresult(&b); |
| 2663 | ip = lua_tolstring(L, lua_gettop(L), NULL); |
| 2664 | } |
| 2665 | } |
| 2666 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2667 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2668 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2669 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2670 | lua_pushnil(L); |
| 2671 | return 1; |
| 2672 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2673 | |
| 2674 | /* Parse ip address. */ |
Willy Tarreau | 5fc9328 | 2020-09-16 18:25:03 +0200 | [diff] [blame] | 2675 | 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] | 2676 | if (!addr) { |
| 2677 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2678 | WILL_LJMP(luaL_error(L, "connect: cannot parse destination address '%s'", ip)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2679 | } |
Willy Tarreau | 9da9a6f | 2019-07-17 14:49:44 +0200 | [diff] [blame] | 2680 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2681 | /* Set port. */ |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2682 | if (low == 0) { |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2683 | if (addr->ss_family == AF_INET) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2684 | if (port == -1) { |
| 2685 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2686 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2687 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2688 | ((struct sockaddr_in *)addr)->sin_port = htons(port); |
| 2689 | } else if (addr->ss_family == AF_INET6) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2690 | if (port == -1) { |
| 2691 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2692 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2693 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2694 | ((struct sockaddr_in6 *)addr)->sin6_port = htons(port); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2695 | } |
| 2696 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2697 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2698 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2699 | si = appctx->owner; |
| 2700 | s = si_strm(si); |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2701 | |
Willy Tarreau | 9b7587a | 2020-10-15 07:32:10 +0200 | [diff] [blame] | 2702 | if (!sockaddr_alloc(&s->target_addr, addr, sizeof(*addr))) { |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2703 | xref_unlock(&socket->xref, peer); |
| 2704 | WILL_LJMP(luaL_error(L, "connect: internal error")); |
| 2705 | } |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2706 | s->flags |= SF_ADDR_SET; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2707 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2708 | /* Get hlua struct, or NULL if we execute from main lua state */ |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2709 | hlua = hlua_gethlua(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2710 | if (!hlua) |
| 2711 | return 0; |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2712 | |
| 2713 | /* inform the stream that we want to be notified whenever the |
| 2714 | * connection completes. |
| 2715 | */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 2716 | si_cant_get(&s->si[0]); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 2717 | si_rx_endp_more(&s->si[0]); |
Thierry FOURNIER | 8c8fbbe | 2015-09-26 17:02:35 +0200 | [diff] [blame] | 2718 | appctx_wakeup(appctx); |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2719 | |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 2720 | hlua->gc_count++; |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 2721 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2722 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2723 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2724 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2725 | } |
| 2726 | xref_unlock(&socket->xref, peer); |
PiBa-NL | 706d5ee | 2018-05-05 23:51:42 +0200 | [diff] [blame] | 2727 | |
| 2728 | task_wakeup(s->task, TASK_WOKEN_INIT); |
| 2729 | /* Return yield waiting for connection. */ |
| 2730 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2731 | 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] | 2732 | |
| 2733 | return 0; |
| 2734 | } |
| 2735 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2736 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2737 | __LJMP static int hlua_socket_connect_ssl(struct lua_State *L) |
| 2738 | { |
| 2739 | struct hlua_socket *socket; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2740 | struct xref *peer; |
| 2741 | struct appctx *appctx; |
| 2742 | struct stream_interface *si; |
| 2743 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2744 | |
| 2745 | MAY_LJMP(check_args(L, 3, "connect_ssl")); |
| 2746 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2747 | |
| 2748 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2749 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2750 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2751 | lua_pushnil(L); |
| 2752 | return 1; |
| 2753 | } |
| 2754 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2755 | si = appctx->owner; |
| 2756 | s = si_strm(si); |
| 2757 | |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 2758 | s->target = &socket_ssl->obj_type; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2759 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2760 | return MAY_LJMP(hlua_socket_connect(L)); |
| 2761 | } |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2762 | #endif |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2763 | |
| 2764 | __LJMP static int hlua_socket_setoption(struct lua_State *L) |
| 2765 | { |
| 2766 | return 0; |
| 2767 | } |
| 2768 | |
| 2769 | __LJMP static int hlua_socket_settimeout(struct lua_State *L) |
| 2770 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2771 | struct hlua_socket *socket; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2772 | int tmout; |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2773 | double dtmout; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2774 | struct xref *peer; |
| 2775 | struct appctx *appctx; |
| 2776 | struct stream_interface *si; |
| 2777 | struct stream *s; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2778 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2779 | MAY_LJMP(check_args(L, 2, "settimeout")); |
| 2780 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2781 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2782 | |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2783 | /* convert the timeout to millis */ |
| 2784 | dtmout = MAY_LJMP(luaL_checknumber(L, 2)) * 1000; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2785 | |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 2786 | /* Check for negative values */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2787 | if (dtmout < 0) |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 2788 | WILL_LJMP(luaL_error(L, "settimeout: cannot set negatives values")); |
| 2789 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2790 | if (dtmout > INT_MAX) /* overflow check */ |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2791 | 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] | 2792 | |
| 2793 | tmout = MS_TO_TICKS((int)dtmout); |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2794 | if (tmout == 0) |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 2795 | tmout++; /* very small timeouts are adjusted to a minimum of 1ms */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2796 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2797 | /* Check if we run on the same thread than the xreator thread. |
| 2798 | * We cannot access to the socket if the thread is different. |
| 2799 | */ |
| 2800 | if (socket->tid != tid) |
| 2801 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2802 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2803 | /* check for connection break. If some data were read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2804 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2805 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2806 | hlua_pusherror(L, "socket: not yet initialised, you can't set timeouts."); |
| 2807 | WILL_LJMP(lua_error(L)); |
| 2808 | return 0; |
| 2809 | } |
| 2810 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2811 | si = appctx->owner; |
| 2812 | s = si_strm(si); |
| 2813 | |
Cyril Bonté | 7bb6345 | 2018-08-17 23:51:02 +0200 | [diff] [blame] | 2814 | s->sess->fe->timeout.connect = tmout; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2815 | s->req.rto = tmout; |
| 2816 | s->req.wto = tmout; |
| 2817 | s->res.rto = tmout; |
| 2818 | s->res.wto = tmout; |
Cyril Bonté | 7bb6345 | 2018-08-17 23:51:02 +0200 | [diff] [blame] | 2819 | s->req.rex = tick_add_ifset(now_ms, tmout); |
| 2820 | s->req.wex = tick_add_ifset(now_ms, tmout); |
| 2821 | s->res.rex = tick_add_ifset(now_ms, tmout); |
| 2822 | s->res.wex = tick_add_ifset(now_ms, tmout); |
| 2823 | |
| 2824 | s->task->expire = tick_add_ifset(now_ms, tmout); |
| 2825 | task_queue(s->task); |
| 2826 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2827 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2828 | |
Thierry Fournier | e9636f1 | 2018-03-08 09:54:32 +0100 | [diff] [blame] | 2829 | lua_pushinteger(L, 1); |
Tim Duesterhus | 119a5f1 | 2018-01-06 19:16:25 +0100 | [diff] [blame] | 2830 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2831 | } |
| 2832 | |
| 2833 | __LJMP static int hlua_socket_new(lua_State *L) |
| 2834 | { |
| 2835 | struct hlua_socket *socket; |
| 2836 | struct appctx *appctx; |
Willy Tarreau | 15b5e14 | 2015-04-04 14:38:25 +0200 | [diff] [blame] | 2837 | struct session *sess; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2838 | struct stream *strm; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2839 | |
| 2840 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2841 | if (!lua_checkstack(L, 3)) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2842 | hlua_pusherror(L, "socket: full stack"); |
| 2843 | goto out_fail_conf; |
| 2844 | } |
| 2845 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2846 | /* Create the object: obj[0] = userdata. */ |
| 2847 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2848 | socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket))); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2849 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2850 | memset(socket, 0, sizeof(*socket)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2851 | socket->tid = tid; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2852 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 2853 | /* Check if the various memory pools are initialized. */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 2854 | if (!pool_head_stream || !pool_head_buffer) { |
Thierry FOURNIER | 4a6170c | 2015-03-09 17:07:10 +0100 | [diff] [blame] | 2855 | hlua_pusherror(L, "socket: uninitialized pools."); |
| 2856 | goto out_fail_conf; |
| 2857 | } |
| 2858 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2859 | /* Pop a class stream metatable and affect it to the userdata. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2860 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref); |
| 2861 | lua_setmetatable(L, -2); |
| 2862 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2863 | /* Create the applet context */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 2864 | appctx = appctx_new(&update_applet, tid_bit); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2865 | if (!appctx) { |
| 2866 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | feb7640 | 2015-04-03 14:10:06 +0200 | [diff] [blame] | 2867 | goto out_fail_conf; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2868 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2869 | |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2870 | appctx->ctx.hlua_cosocket.connected = 0; |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 2871 | appctx->ctx.hlua_cosocket.die = 0; |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2872 | LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_write); |
| 2873 | LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_read); |
Willy Tarreau | b2bf833 | 2015-04-04 15:58:58 +0200 | [diff] [blame] | 2874 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2875 | /* Now create a session, task and stream for this applet */ |
Amaury Denoyelle | 239fdbf | 2021-03-24 10:22:03 +0100 | [diff] [blame] | 2876 | sess = session_new(socket_proxy, NULL, &appctx->obj_type); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2877 | if (!sess) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2878 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2879 | goto out_fail_sess; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2880 | } |
| 2881 | |
Christopher Faulet | 26256f8 | 2020-09-14 11:40:13 +0200 | [diff] [blame] | 2882 | strm = stream_new(sess, &appctx->obj_type, &BUF_NULL); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2883 | if (!strm) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2884 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2885 | goto out_fail_stream; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2886 | } |
| 2887 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2888 | /* Initialise cross reference between stream and Lua socket object. */ |
| 2889 | xref_create(&socket->xref, &appctx->ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2890 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2891 | /* Configure "right" stream interface. this "si" is used to connect |
| 2892 | * and retrieve data from the server. The connection is initialized |
| 2893 | * with the "struct server". |
| 2894 | */ |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2895 | si_set_state(&strm->si[1], SI_ST_ASS); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2896 | |
| 2897 | /* Force destination server. */ |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2898 | strm->flags |= SF_DIRECT | SF_ASSIGNED | SF_BE_ASSIGNED; |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 2899 | strm->target = &socket_tcp->obj_type; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2900 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2901 | return 1; |
| 2902 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2903 | out_fail_stream: |
Willy Tarreau | 11c3624 | 2015-04-04 15:54:03 +0200 | [diff] [blame] | 2904 | session_free(sess); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2905 | out_fail_sess: |
| 2906 | appctx_free(appctx); |
| 2907 | out_fail_conf: |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2908 | WILL_LJMP(lua_error(L)); |
| 2909 | return 0; |
| 2910 | } |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 2911 | |
| 2912 | /* |
| 2913 | * |
| 2914 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2915 | * Class Channel |
| 2916 | * |
| 2917 | * |
| 2918 | */ |
| 2919 | |
| 2920 | /* Returns the struct hlua_channel join to the class channel in the |
| 2921 | * stack entry "ud" or throws an argument error. |
| 2922 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2923 | __LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2924 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 2925 | return MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2926 | } |
| 2927 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2928 | /* Pushes the channel onto the top of the stack. If the stask does not have a |
| 2929 | * free slots, the function fails and returns 0; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2930 | */ |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 2931 | static int hlua_channel_new(lua_State *L, struct channel *channel) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2932 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2933 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2934 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2935 | return 0; |
| 2936 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2937 | lua_newtable(L); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2938 | lua_pushlightuserdata(L, channel); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2939 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2940 | |
| 2941 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 2942 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref); |
| 2943 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2944 | return 1; |
| 2945 | } |
| 2946 | |
| 2947 | /* Duplicate all the data present in the input channel and put it |
| 2948 | * in a string LUA variables. Returns -1 and push a nil value in |
| 2949 | * the stack if the channel is closed and all the data are consumed, |
| 2950 | * returns 0 if no data are available, otherwise it returns the length |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 2951 | * of the built string. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2952 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2953 | static inline int _hlua_channel_dup(struct channel *chn, lua_State *L) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2954 | { |
| 2955 | char *blk1; |
| 2956 | char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2957 | size_t len1; |
| 2958 | size_t len2; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2959 | int ret; |
| 2960 | luaL_Buffer b; |
| 2961 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2962 | ret = ci_getblk_nc(chn, &blk1, &len1, &blk2, &len2); |
Christopher Faulet | 055310f | 2021-08-05 11:58:37 +0200 | [diff] [blame] | 2963 | if (unlikely(ret <= 0)) { |
| 2964 | if (ret < 0 || HLUA_CANT_YIELD(hlua_gethlua(L))) { |
| 2965 | lua_pushnil(L); |
| 2966 | return -1; |
| 2967 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2968 | return 0; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2969 | } |
| 2970 | |
| 2971 | luaL_buffinit(L, &b); |
| 2972 | luaL_addlstring(&b, blk1, len1); |
| 2973 | if (unlikely(ret == 2)) |
| 2974 | luaL_addlstring(&b, blk2, len2); |
| 2975 | luaL_pushresult(&b); |
| 2976 | |
| 2977 | if (unlikely(ret == 2)) |
| 2978 | return len1 + len2; |
| 2979 | return len1; |
| 2980 | } |
| 2981 | |
| 2982 | /* "_hlua_channel_dup" wrapper. If no data are available, it returns |
| 2983 | * a yield. This function keep the data in the buffer. |
| 2984 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2985 | __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] | 2986 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2987 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2988 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2989 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2990 | |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 2991 | if (IS_HTX_STRM(chn_strm(chn))) { |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 2992 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2993 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 2994 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2995 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2996 | if (_hlua_channel_dup(chn, L) == 0) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2997 | 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] | 2998 | return 1; |
| 2999 | } |
| 3000 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3001 | /* Check arguments for the function "hlua_channel_dup_yield". */ |
| 3002 | __LJMP static int hlua_channel_dup(lua_State *L) |
| 3003 | { |
| 3004 | MAY_LJMP(check_args(L, 1, "dup")); |
| 3005 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3006 | return MAY_LJMP(hlua_channel_dup_yield(L, 0, 0)); |
| 3007 | } |
| 3008 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3009 | /* "_hlua_channel_dup" wrapper. If no data are available, it returns |
| 3010 | * a yield. This function consumes the data in the buffer. It returns |
| 3011 | * a string containing the data or a nil pointer if no data are available |
| 3012 | * and the channel is closed. |
| 3013 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3014 | __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] | 3015 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3016 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3017 | int ret; |
| 3018 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3019 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3020 | |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 3021 | if (IS_HTX_STRM(chn_strm(chn))) { |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3022 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3023 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3024 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3025 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3026 | ret = _hlua_channel_dup(chn, L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3027 | if (unlikely(ret == 0)) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3028 | 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] | 3029 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3030 | if (unlikely(ret == -1)) |
| 3031 | return 1; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3032 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3033 | b_sub(&chn->buf, ret); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3034 | return 1; |
| 3035 | } |
| 3036 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3037 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3038 | __LJMP static int hlua_channel_get(lua_State *L) |
| 3039 | { |
| 3040 | MAY_LJMP(check_args(L, 1, "get")); |
| 3041 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3042 | return MAY_LJMP(hlua_channel_get_yield(L, 0, 0)); |
| 3043 | } |
| 3044 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3045 | /* This functions consumes and returns one line. If the channel is closed, |
| 3046 | * 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] | 3047 | * 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] | 3048 | * value. |
| 3049 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3050 | __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] | 3051 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3052 | char *blk1; |
| 3053 | char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3054 | size_t len1; |
| 3055 | size_t len2; |
| 3056 | size_t len; |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3057 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3058 | int ret; |
| 3059 | luaL_Buffer b; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3060 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3061 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3062 | |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 3063 | if (IS_HTX_STRM(chn_strm(chn))) { |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3064 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3065 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3066 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3067 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3068 | ret = ci_getline_nc(chn, &blk1, &len1, &blk2, &len2); |
Christopher Faulet | 055310f | 2021-08-05 11:58:37 +0200 | [diff] [blame] | 3069 | if (ret == 0) { |
| 3070 | if (HLUA_CANT_YIELD(hlua_gethlua(L))) { |
| 3071 | _hlua_channel_dup(chn, L); |
| 3072 | return 1; |
| 3073 | } |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3074 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_getline_yield, TICK_ETERNITY, 0)); |
Christopher Faulet | 055310f | 2021-08-05 11:58:37 +0200 | [diff] [blame] | 3075 | } |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3076 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3077 | if (ret == -1) { |
| 3078 | lua_pushnil(L); |
| 3079 | return 1; |
| 3080 | } |
| 3081 | |
| 3082 | luaL_buffinit(L, &b); |
| 3083 | luaL_addlstring(&b, blk1, len1); |
| 3084 | len = len1; |
| 3085 | if (unlikely(ret == 2)) { |
| 3086 | luaL_addlstring(&b, blk2, len2); |
| 3087 | len += len2; |
| 3088 | } |
| 3089 | luaL_pushresult(&b); |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3090 | 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] | 3091 | return 1; |
| 3092 | } |
| 3093 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3094 | /* Check arguments for the function "hlua_channel_getline_yield". */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3095 | __LJMP static int hlua_channel_getline(lua_State *L) |
| 3096 | { |
| 3097 | MAY_LJMP(check_args(L, 1, "getline")); |
| 3098 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3099 | return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0)); |
| 3100 | } |
| 3101 | |
Christopher Faulet | 3b7bc3e | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3102 | /* This function takes a string as argument, and append it at the input side of |
| 3103 | * channel. If data cannot be copied, because there is not enough space to do so |
| 3104 | * or because the channel is closed, it returns -1. Otherwise, it returns the |
| 3105 | * amount of data written (the string length). This function does not yield. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3106 | */ |
Christopher Faulet | 3b7bc3e | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3107 | __LJMP static int hlua_channel_append(lua_State *L) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3108 | { |
Christopher Faulet | 3b7bc3e | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3109 | struct channel *chn; |
| 3110 | const char *str; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3111 | size_t len; |
Christopher Faulet | 3b7bc3e | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3112 | |
| 3113 | MAY_LJMP(check_args(L, 2, "append")); |
| 3114 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3115 | str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3116 | |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 3117 | if (IS_HTX_STRM(chn_strm(chn))) { |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3118 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3119 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3120 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3121 | |
Christopher Faulet | 3b7bc3e | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3122 | if (len > c_room(chn) || ci_putblk(chn, str, len) < 0) |
| 3123 | lua_pushinteger(L, -1); |
| 3124 | else |
| 3125 | lua_pushinteger(L, len); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3126 | return 1; |
| 3127 | } |
| 3128 | |
Christopher Faulet | 3b7bc3e | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3129 | /* The function replaces input data from the channel by the string passed as |
| 3130 | * argument. Before clearing the buffer, we take care the copy will be |
| 3131 | * possible. It returns the amount of data written (the string length) on |
| 3132 | * success or -1 if the copy is not performed. This function does not yield. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3133 | */ |
| 3134 | __LJMP static int hlua_channel_set(lua_State *L) |
| 3135 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3136 | struct channel *chn; |
Christopher Faulet | 3b7bc3e | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3137 | const char *str; |
| 3138 | size_t len; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3139 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3140 | MAY_LJMP(check_args(L, 2, "set")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3141 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 3b7bc3e | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3142 | str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3143 | |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 3144 | if (IS_HTX_STRM(chn_strm(chn))) { |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3145 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3146 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3147 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3148 | |
Christopher Faulet | 3b7bc3e | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3149 | /* Be sure we can copied the string once input data will be removed. */ |
| 3150 | if (len > ci_data(chn) || channel_input_closed(chn)) |
| 3151 | lua_pushinteger(L, -1); |
| 3152 | else { |
| 3153 | b_set_data(&chn->buf, co_data(chn)); |
| 3154 | if (ci_putblk(chn, str, len) < 0) |
| 3155 | lua_pushinteger(L, -1); |
| 3156 | else |
| 3157 | lua_pushinteger(L, len); |
| 3158 | } |
| 3159 | return -1; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3160 | } |
| 3161 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3162 | /* Append data in the output side of the buffer. This data is immediately |
| 3163 | * sent. The function returns the amount of data written. If the buffer |
| 3164 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3165 | * if the channel is closed. |
| 3166 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3167 | __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] | 3168 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3169 | struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3170 | size_t len; |
| 3171 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3172 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3173 | int max; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 3174 | struct hlua *hlua; |
| 3175 | |
| 3176 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 3177 | hlua = hlua_gethlua(L); |
| 3178 | if (!hlua) { |
| 3179 | lua_pushnil(L); |
| 3180 | return 1; |
| 3181 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3182 | |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 3183 | if (IS_HTX_STRM(chn_strm(chn))) { |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3184 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3185 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3186 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3187 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3188 | if (unlikely(channel_output_closed(chn))) { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3189 | lua_pushinteger(L, -1); |
| 3190 | return 1; |
| 3191 | } |
| 3192 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3193 | /* Check if the buffer is available because HAProxy doesn't allocate |
Thierry FOURNIER | 3e3a608 | 2015-03-05 17:06:12 +0100 | [diff] [blame] | 3194 | * the request buffer if its not required. |
| 3195 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3196 | if (chn->buf.size == 0) { |
Christopher Faulet | 055310f | 2021-08-05 11:58:37 +0200 | [diff] [blame] | 3197 | if (HLUA_CANT_YIELD(hlua_gethlua(L))) |
| 3198 | return 1; |
Willy Tarreau | 4b962a4 | 2018-11-15 11:03:21 +0100 | [diff] [blame] | 3199 | si_rx_buff_blk(chn_prod(chn)); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3200 | 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] | 3201 | } |
| 3202 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3203 | /* The written data will be immediately sent, so we can check |
| 3204 | * the available space without taking in account the reserve. |
| 3205 | * The reserve is guaranteed for the processing of incoming |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3206 | * data, because the buffer will be flushed. |
| 3207 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3208 | max = b_room(&chn->buf); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3209 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3210 | /* If there is no space available, and the output buffer is empty. |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3211 | * in this case, we cannot add more data, so we cannot yield, |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3212 | * we return the amount of copied data. |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3213 | */ |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3214 | if (max == 0 && co_data(chn) == 0) |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3215 | return 1; |
| 3216 | |
| 3217 | /* Adjust the real required length. */ |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3218 | if (max > len - l) |
| 3219 | max = len - l; |
| 3220 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3221 | /* The buffer available size may be not contiguous. This test |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3222 | * detects a non contiguous buffer and realign it. |
| 3223 | */ |
Willy Tarreau | 3f67999 | 2018-06-15 15:06:42 +0200 | [diff] [blame] | 3224 | if (ci_space_for_replace(chn) < max) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3225 | channel_slow_realign(chn, trash.area); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3226 | |
| 3227 | /* Copy input data in the buffer. */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3228 | 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] | 3229 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3230 | /* buffer replace considers that the input part is filled. |
| 3231 | * so, I must forward these new data in the output part. |
| 3232 | */ |
Willy Tarreau | bcbd393 | 2018-06-06 07:13:22 +0200 | [diff] [blame] | 3233 | c_adv(chn, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3234 | |
| 3235 | l += max; |
| 3236 | lua_pop(L, 1); |
| 3237 | lua_pushinteger(L, l); |
| 3238 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3239 | if (l < len) { |
Christopher Faulet | 055310f | 2021-08-05 11:58:37 +0200 | [diff] [blame] | 3240 | /* If there is no space available, and the output buffer is empty. |
| 3241 | * in this case, we cannot add more data, so we cannot yield, |
| 3242 | * we return the amount of copied data. |
| 3243 | */ |
| 3244 | max = b_room(&chn->buf); |
| 3245 | if ((max == 0 && co_data(chn) == 0) || HLUA_CANT_YIELD(hlua_gethlua(L))) |
| 3246 | return 1; |
| 3247 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3248 | /* If we are waiting for space in the response buffer, we |
| 3249 | * must set the flag WAKERESWR. This flag required the task |
| 3250 | * wake up if any activity is detected on the response buffer. |
| 3251 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3252 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3253 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3254 | else |
| 3255 | HLUA_SET_WAKEREQWR(hlua); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3256 | 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] | 3257 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3258 | |
| 3259 | return 1; |
| 3260 | } |
| 3261 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3262 | /* Just a wrapper of "_hlua_channel_send". This wrapper permits |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3263 | * yield the LUA process, and resume it without checking the |
| 3264 | * input arguments. |
| 3265 | */ |
| 3266 | __LJMP static int hlua_channel_send(lua_State *L) |
| 3267 | { |
| 3268 | MAY_LJMP(check_args(L, 2, "send")); |
| 3269 | lua_pushinteger(L, 0); |
| 3270 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3271 | return MAY_LJMP(hlua_channel_send_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3272 | } |
| 3273 | |
| 3274 | /* This function forward and amount of butes. The data pass from |
| 3275 | * the input side of the buffer to the output side, and can be |
| 3276 | * forwarded. This function never fails. |
| 3277 | * |
| 3278 | * The Lua function takes an amount of bytes to be forwarded in |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3279 | * input. It returns the number of bytes forwarded. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3280 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3281 | __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] | 3282 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3283 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3284 | int len; |
| 3285 | int l; |
| 3286 | int max; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 3287 | struct hlua *hlua; |
| 3288 | |
| 3289 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 3290 | hlua = hlua_gethlua(L); |
| 3291 | if (!hlua) |
| 3292 | return 1; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3293 | |
| 3294 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3295 | |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 3296 | if (IS_HTX_STRM(chn_strm(chn))) { |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3297 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3298 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3299 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3300 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3301 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3302 | l = MAY_LJMP(luaL_checkinteger(L, -1)); |
| 3303 | |
| 3304 | max = len - l; |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3305 | if (max > ci_data(chn)) |
| 3306 | max = ci_data(chn); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3307 | channel_forward(chn, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3308 | l += max; |
| 3309 | |
| 3310 | lua_pop(L, 1); |
| 3311 | lua_pushinteger(L, l); |
| 3312 | |
| 3313 | /* Check if it miss bytes to forward. */ |
| 3314 | if (l < len) { |
| 3315 | /* The the input channel or the output channel are closed, we |
| 3316 | * must return the amount of data forwarded. |
| 3317 | */ |
Christopher Faulet | 055310f | 2021-08-05 11:58:37 +0200 | [diff] [blame] | 3318 | if (channel_input_closed(chn) || channel_output_closed(chn) || HLUA_CANT_YIELD(hlua_gethlua(L))) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3319 | return 1; |
| 3320 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3321 | /* If we are waiting for space data in the response buffer, we |
| 3322 | * must set the flag WAKERESWR. This flag required the task |
| 3323 | * wake up if any activity is detected on the response buffer. |
| 3324 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3325 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3326 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3327 | else |
| 3328 | HLUA_SET_WAKEREQWR(hlua); |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3329 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3330 | /* Otherwise, we can yield waiting for new data in the inpout side. */ |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3331 | 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] | 3332 | } |
| 3333 | |
| 3334 | return 1; |
| 3335 | } |
| 3336 | |
| 3337 | /* Just check the input and prepare the stack for the previous |
| 3338 | * function "hlua_channel_forward_yield" |
| 3339 | */ |
| 3340 | __LJMP static int hlua_channel_forward(lua_State *L) |
| 3341 | { |
| 3342 | MAY_LJMP(check_args(L, 2, "forward")); |
| 3343 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3344 | MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3345 | |
| 3346 | lua_pushinteger(L, 0); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3347 | return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3348 | } |
| 3349 | |
| 3350 | /* Just returns the number of bytes available in the input |
| 3351 | * side of the buffer. This function never fails. |
| 3352 | */ |
| 3353 | __LJMP static int hlua_channel_get_in_len(lua_State *L) |
| 3354 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3355 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3356 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3357 | MAY_LJMP(check_args(L, 1, "get_in_len")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3358 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a3ceac1 | 2018-12-14 13:39:09 +0100 | [diff] [blame] | 3359 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3360 | struct htx *htx = htxbuf(&chn->buf); |
| 3361 | lua_pushinteger(L, htx->data - co_data(chn)); |
| 3362 | } |
| 3363 | else |
| 3364 | lua_pushinteger(L, ci_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3365 | return 1; |
| 3366 | } |
| 3367 | |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3368 | /* Returns true if the channel is full. */ |
| 3369 | __LJMP static int hlua_channel_is_full(lua_State *L) |
| 3370 | { |
| 3371 | struct channel *chn; |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3372 | |
| 3373 | MAY_LJMP(check_args(L, 1, "is_full")); |
| 3374 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 0ec740e | 2020-02-26 11:59:19 +0100 | [diff] [blame] | 3375 | /* ignore the reserve, we are not on a producer side (ie in an |
| 3376 | * applet). |
| 3377 | */ |
| 3378 | lua_pushboolean(L, channel_full(chn, 0)); |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3379 | return 1; |
| 3380 | } |
| 3381 | |
Christopher Faulet | 2ac9ba2 | 2020-02-25 10:15:50 +0100 | [diff] [blame] | 3382 | /* Returns true if the channel is the response channel. */ |
| 3383 | __LJMP static int hlua_channel_is_resp(lua_State *L) |
| 3384 | { |
| 3385 | struct channel *chn; |
| 3386 | |
| 3387 | MAY_LJMP(check_args(L, 1, "is_resp")); |
| 3388 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3389 | |
| 3390 | lua_pushboolean(L, !!(chn->flags & CF_ISRESP)); |
| 3391 | return 1; |
| 3392 | } |
| 3393 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3394 | /* Just returns the number of bytes available in the output |
| 3395 | * side of the buffer. This function never fails. |
| 3396 | */ |
| 3397 | __LJMP static int hlua_channel_get_out_len(lua_State *L) |
| 3398 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3399 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3400 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3401 | MAY_LJMP(check_args(L, 1, "get_out_len")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3402 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3403 | lua_pushinteger(L, co_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3404 | return 1; |
| 3405 | } |
| 3406 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3407 | /* |
| 3408 | * |
| 3409 | * |
| 3410 | * Class Fetches |
| 3411 | * |
| 3412 | * |
| 3413 | */ |
| 3414 | |
| 3415 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3416 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3417 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3418 | __LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud) |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3419 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3420 | return MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3421 | } |
| 3422 | |
| 3423 | /* This function creates and push in the stack a fetch object according |
| 3424 | * with a current TXN. |
| 3425 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3426 | 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] | 3427 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3428 | struct hlua_smp *hsmp; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3429 | |
| 3430 | /* Check stack size. */ |
| 3431 | if (!lua_checkstack(L, 3)) |
| 3432 | return 0; |
| 3433 | |
| 3434 | /* Create the object: obj[0] = userdata. |
| 3435 | * Note that the base of the Fetches object is the |
| 3436 | * transaction object. |
| 3437 | */ |
| 3438 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3439 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3440 | lua_rawseti(L, -2, 0); |
| 3441 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3442 | hsmp->s = txn->s; |
| 3443 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 3444 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3445 | hsmp->flags = flags; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3446 | |
| 3447 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 3448 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref); |
| 3449 | lua_setmetatable(L, -2); |
| 3450 | |
| 3451 | return 1; |
| 3452 | } |
| 3453 | |
| 3454 | /* This function is an LUA binding. It is called with each sample-fetch. |
| 3455 | * It uses closure argument to store the associated sample-fetch. It |
| 3456 | * returns only one argument or throws an error. An error is thrown |
| 3457 | * only if an error is encountered during the argument parsing. If |
| 3458 | * the "sample-fetch" function fails, nil is returned. |
| 3459 | */ |
| 3460 | __LJMP static int hlua_run_sample_fetch(lua_State *L) |
| 3461 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3462 | struct hlua_smp *hsmp; |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 3463 | struct sample_fetch *f; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 3464 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3465 | int i; |
| 3466 | struct sample smp; |
| 3467 | |
| 3468 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3469 | f = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3470 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3471 | /* Get traditional arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3472 | hsmp = MAY_LJMP(hlua_checkfetches(L, 1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3473 | |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 3474 | /* Check execution authorization. */ |
| 3475 | if (f->use & SMP_USE_HTTP_ANY && |
| 3476 | !(hsmp->flags & HLUA_F_MAY_USE_HTTP)) { |
| 3477 | lua_pushfstring(L, "the sample-fetch '%s' needs an HTTP parser which " |
| 3478 | "is not available in Lua services", f->kw); |
| 3479 | WILL_LJMP(lua_error(L)); |
| 3480 | } |
| 3481 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3482 | /* Get extra arguments. */ |
| 3483 | for (i = 0; i < lua_gettop(L) - 1; i++) { |
| 3484 | if (i >= ARGM_NBARGS) |
| 3485 | break; |
| 3486 | hlua_lua2arg(L, i + 2, &args[i]); |
| 3487 | } |
| 3488 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3489 | args[i].data.str.area = NULL; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3490 | |
| 3491 | /* Check arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3492 | 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] | 3493 | |
| 3494 | /* Run the special args checker. */ |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 3495 | if (f->val_args && !f->val_args(args, NULL)) { |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3496 | lua_pushfstring(L, "error in arguments"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3497 | goto error; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3498 | } |
| 3499 | |
| 3500 | /* Initialise the sample. */ |
| 3501 | memset(&smp, 0, sizeof(smp)); |
| 3502 | |
| 3503 | /* Run the sample fetch process. */ |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 3504 | 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] | 3505 | if (!f->process(args, &smp, f->kw, f->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3506 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3507 | lua_pushstring(L, ""); |
| 3508 | else |
| 3509 | lua_pushnil(L); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3510 | goto end; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3511 | } |
| 3512 | |
| 3513 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3514 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3515 | hlua_smp2lua_str(L, &smp); |
| 3516 | else |
| 3517 | hlua_smp2lua(L, &smp); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3518 | |
| 3519 | end: |
| 3520 | for (i = 0; args[i].type != ARGT_STOP; i++) { |
| 3521 | if (args[i].type == ARGT_STR) |
| 3522 | chunk_destroy(&args[i].data.str); |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 3523 | else if (args[i].type == ARGT_REG) |
| 3524 | regex_free(args[i].data.reg); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3525 | } |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3526 | return 1; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3527 | |
| 3528 | error: |
| 3529 | for (i = 0; args[i].type != ARGT_STOP; i++) { |
| 3530 | if (args[i].type == ARGT_STR) |
| 3531 | chunk_destroy(&args[i].data.str); |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 3532 | else if (args[i].type == ARGT_REG) |
| 3533 | regex_free(args[i].data.reg); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3534 | } |
| 3535 | WILL_LJMP(lua_error(L)); |
| 3536 | return 0; /* Never reached */ |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3537 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3538 | |
| 3539 | /* |
| 3540 | * |
| 3541 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3542 | * Class Converters |
| 3543 | * |
| 3544 | * |
| 3545 | */ |
| 3546 | |
| 3547 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3548 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3549 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3550 | __LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3551 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3552 | return MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3553 | } |
| 3554 | |
| 3555 | /* This function creates and push in the stack a Converters object |
| 3556 | * according with a current TXN. |
| 3557 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3558 | 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] | 3559 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3560 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3561 | |
| 3562 | /* Check stack size. */ |
| 3563 | if (!lua_checkstack(L, 3)) |
| 3564 | return 0; |
| 3565 | |
| 3566 | /* Create the object: obj[0] = userdata. |
| 3567 | * Note that the base of the Converters object is the |
| 3568 | * same than the TXN object. |
| 3569 | */ |
| 3570 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3571 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3572 | lua_rawseti(L, -2, 0); |
| 3573 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3574 | hsmp->s = txn->s; |
| 3575 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 3576 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3577 | hsmp->flags = flags; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3578 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3579 | /* Pop a class stream metatable and affect it to the table. */ |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3580 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref); |
| 3581 | lua_setmetatable(L, -2); |
| 3582 | |
| 3583 | return 1; |
| 3584 | } |
| 3585 | |
| 3586 | /* This function is an LUA binding. It is called with each converter. |
| 3587 | * It uses closure argument to store the associated converter. It |
| 3588 | * returns only one argument or throws an error. An error is thrown |
| 3589 | * only if an error is encountered during the argument parsing. If |
| 3590 | * the converter function function fails, nil is returned. |
| 3591 | */ |
| 3592 | __LJMP static int hlua_run_sample_conv(lua_State *L) |
| 3593 | { |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3594 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3595 | struct sample_conv *conv; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 3596 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3597 | int i; |
| 3598 | struct sample smp; |
| 3599 | |
| 3600 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3601 | conv = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3602 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3603 | /* Get traditional arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3604 | hsmp = MAY_LJMP(hlua_checkconverters(L, 1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3605 | |
| 3606 | /* Get extra arguments. */ |
| 3607 | for (i = 0; i < lua_gettop(L) - 2; i++) { |
| 3608 | if (i >= ARGM_NBARGS) |
| 3609 | break; |
| 3610 | hlua_lua2arg(L, i + 3, &args[i]); |
| 3611 | } |
| 3612 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3613 | args[i].data.str.area = NULL; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3614 | |
| 3615 | /* Check arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3616 | 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] | 3617 | |
| 3618 | /* Run the special args checker. */ |
| 3619 | if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) { |
| 3620 | hlua_pusherror(L, "error in arguments"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3621 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3622 | } |
| 3623 | |
| 3624 | /* Initialise the sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 3625 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3626 | if (!hlua_lua2smp(L, 2, &smp)) { |
| 3627 | hlua_pusherror(L, "error in the input argument"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3628 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3629 | } |
| 3630 | |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 3631 | smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR); |
| 3632 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3633 | /* Apply expected cast. */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3634 | if (!sample_casts[smp.data.type][conv->in_type]) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3635 | hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'", |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3636 | smp_to_type[smp.data.type], smp_to_type[conv->in_type]); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3637 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3638 | } |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3639 | if (sample_casts[smp.data.type][conv->in_type] != c_none && |
| 3640 | !sample_casts[smp.data.type][conv->in_type](&smp)) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3641 | hlua_pusherror(L, "error during the input argument casting"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3642 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3643 | } |
| 3644 | |
| 3645 | /* Run the sample conversion process. */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 3646 | if (!conv->process(args, &smp, conv->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3647 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3648 | lua_pushstring(L, ""); |
| 3649 | else |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 3650 | lua_pushnil(L); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3651 | goto end; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3652 | } |
| 3653 | |
| 3654 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3655 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3656 | hlua_smp2lua_str(L, &smp); |
| 3657 | else |
| 3658 | hlua_smp2lua(L, &smp); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3659 | end: |
| 3660 | for (i = 0; args[i].type != ARGT_STOP; i++) { |
| 3661 | if (args[i].type == ARGT_STR) |
| 3662 | chunk_destroy(&args[i].data.str); |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 3663 | else if (args[i].type == ARGT_REG) |
| 3664 | regex_free(args[i].data.reg); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3665 | } |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 3666 | return 1; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3667 | |
| 3668 | error: |
| 3669 | for (i = 0; args[i].type != ARGT_STOP; i++) { |
| 3670 | if (args[i].type == ARGT_STR) |
| 3671 | chunk_destroy(&args[i].data.str); |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 3672 | else if (args[i].type == ARGT_REG) |
| 3673 | regex_free(args[i].data.reg); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3674 | } |
| 3675 | WILL_LJMP(lua_error(L)); |
| 3676 | return 0; /* Never reached */ |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3677 | } |
| 3678 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3679 | /* |
| 3680 | * |
| 3681 | * |
| 3682 | * Class AppletTCP |
| 3683 | * |
| 3684 | * |
| 3685 | */ |
| 3686 | |
| 3687 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 3688 | * a class stream, otherwise it throws an error. |
| 3689 | */ |
| 3690 | __LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud) |
| 3691 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3692 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3693 | } |
| 3694 | |
| 3695 | /* This function creates and push in the stack an Applet object |
| 3696 | * according with a current TXN. |
| 3697 | */ |
| 3698 | static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx) |
| 3699 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3700 | struct hlua_appctx *luactx; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3701 | struct stream_interface *si = ctx->owner; |
| 3702 | struct stream *s = si_strm(si); |
| 3703 | struct proxy *p = s->be; |
| 3704 | |
| 3705 | /* Check stack size. */ |
| 3706 | if (!lua_checkstack(L, 3)) |
| 3707 | return 0; |
| 3708 | |
| 3709 | /* Create the object: obj[0] = userdata. |
| 3710 | * Note that the base of the Converters object is the |
| 3711 | * same than the TXN object. |
| 3712 | */ |
| 3713 | lua_newtable(L); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3714 | luactx = lua_newuserdata(L, sizeof(*luactx)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3715 | lua_rawseti(L, -2, 0); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3716 | luactx->appctx = ctx; |
| 3717 | luactx->htxn.s = s; |
| 3718 | luactx->htxn.p = p; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3719 | |
| 3720 | /* Create the "f" field that contains a list of fetches. */ |
| 3721 | lua_pushstring(L, "f"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3722 | if (!hlua_fetches_new(L, &luactx->htxn, 0)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3723 | return 0; |
| 3724 | lua_settable(L, -3); |
| 3725 | |
| 3726 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 3727 | lua_pushstring(L, "sf"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3728 | if (!hlua_fetches_new(L, &luactx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3729 | return 0; |
| 3730 | lua_settable(L, -3); |
| 3731 | |
| 3732 | /* Create the "c" field that contains a list of converters. */ |
| 3733 | lua_pushstring(L, "c"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3734 | if (!hlua_converters_new(L, &luactx->htxn, 0)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3735 | return 0; |
| 3736 | lua_settable(L, -3); |
| 3737 | |
| 3738 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 3739 | lua_pushstring(L, "sc"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3740 | if (!hlua_converters_new(L, &luactx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3741 | return 0; |
| 3742 | lua_settable(L, -3); |
| 3743 | |
| 3744 | /* Pop a class stream metatable and affect it to the table. */ |
| 3745 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref); |
| 3746 | lua_setmetatable(L, -2); |
| 3747 | |
| 3748 | return 1; |
| 3749 | } |
| 3750 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3751 | __LJMP static int hlua_applet_tcp_set_var(lua_State *L) |
| 3752 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3753 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3754 | struct stream *s; |
| 3755 | const char *name; |
| 3756 | size_t len; |
| 3757 | struct sample smp; |
| 3758 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 3759 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 3760 | 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] | 3761 | |
| 3762 | /* It is useles to retrieve the stream, but this function |
| 3763 | * runs only in a stream context. |
| 3764 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3765 | luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3766 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3767 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3768 | |
| 3769 | /* Converts the third argument in a sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 3770 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3771 | hlua_lua2smp(L, 3, &smp); |
| 3772 | |
| 3773 | /* Store the sample in a variable. */ |
| 3774 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 3775 | |
| 3776 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 3777 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 3778 | else |
| 3779 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 3780 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 3781 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3782 | } |
| 3783 | |
| 3784 | __LJMP static int hlua_applet_tcp_unset_var(lua_State *L) |
| 3785 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3786 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3787 | struct stream *s; |
| 3788 | const char *name; |
| 3789 | size_t len; |
| 3790 | struct sample smp; |
| 3791 | |
| 3792 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 3793 | |
| 3794 | /* It is useles to retrieve the stream, but this function |
| 3795 | * runs only in a stream context. |
| 3796 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3797 | luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3798 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3799 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3800 | |
| 3801 | /* Unset the variable. */ |
| 3802 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 3803 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 3804 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3805 | } |
| 3806 | |
| 3807 | __LJMP static int hlua_applet_tcp_get_var(lua_State *L) |
| 3808 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3809 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3810 | struct stream *s; |
| 3811 | const char *name; |
| 3812 | size_t len; |
| 3813 | struct sample smp; |
| 3814 | |
| 3815 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 3816 | |
| 3817 | /* It is useles to retrieve the stream, but this function |
| 3818 | * runs only in a stream context. |
| 3819 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3820 | luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3821 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3822 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3823 | |
| 3824 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 3825 | if (!vars_get_by_name(name, len, &smp)) { |
| 3826 | lua_pushnil(L); |
| 3827 | return 1; |
| 3828 | } |
| 3829 | |
| 3830 | return hlua_smp2lua(L, &smp); |
| 3831 | } |
| 3832 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3833 | __LJMP static int hlua_applet_tcp_set_priv(lua_State *L) |
| 3834 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3835 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3836 | struct stream *s = luactx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 3837 | struct hlua *hlua; |
| 3838 | |
| 3839 | /* 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] | 3840 | if (!s->hlua) |
| 3841 | return 0; |
| 3842 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3843 | |
| 3844 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 3845 | |
| 3846 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 3847 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3848 | |
| 3849 | /* Get and store new value. */ |
| 3850 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 3851 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 3852 | |
| 3853 | return 0; |
| 3854 | } |
| 3855 | |
| 3856 | __LJMP static int hlua_applet_tcp_get_priv(lua_State *L) |
| 3857 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3858 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3859 | struct stream *s = luactx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 3860 | struct hlua *hlua; |
| 3861 | |
| 3862 | /* 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] | 3863 | if (!s->hlua) { |
| 3864 | lua_pushnil(L); |
| 3865 | return 1; |
| 3866 | } |
| 3867 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3868 | |
| 3869 | /* Push configuration index in the stack. */ |
| 3870 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 3871 | |
| 3872 | return 1; |
| 3873 | } |
| 3874 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3875 | /* If expected data not yet available, it returns a yield. This function |
| 3876 | * consumes the data in the buffer. It returns a string containing the |
| 3877 | * data. This string can be empty. |
| 3878 | */ |
| 3879 | __LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx) |
| 3880 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3881 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3882 | struct stream_interface *si = luactx->appctx->owner; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3883 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3884 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3885 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3886 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3887 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3888 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3889 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3890 | ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3891 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3892 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3893 | if (ret == 0) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3894 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3895 | 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] | 3896 | } |
| 3897 | |
| 3898 | /* End of data: commit the total strings and return. */ |
| 3899 | if (ret < 0) { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3900 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3901 | return 1; |
| 3902 | } |
| 3903 | |
| 3904 | /* Ensure that the block 2 length is usable. */ |
| 3905 | if (ret == 1) |
| 3906 | len2 = 0; |
| 3907 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 3908 | /* don't check the max length read and don't check. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3909 | luaL_addlstring(&luactx->b, blk1, len1); |
| 3910 | luaL_addlstring(&luactx->b, blk2, len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3911 | |
| 3912 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3913 | co_skip(si_oc(si), len1 + len2); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3914 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3915 | return 1; |
| 3916 | } |
| 3917 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3918 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3919 | __LJMP static int hlua_applet_tcp_getline(lua_State *L) |
| 3920 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3921 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3922 | |
| 3923 | /* Initialise the string catenation. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3924 | luaL_buffinit(L, &luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3925 | |
| 3926 | return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0)); |
| 3927 | } |
| 3928 | |
| 3929 | /* If expected data not yet available, it returns a yield. This function |
| 3930 | * consumes the data in the buffer. It returns a string containing the |
| 3931 | * data. This string can be empty. |
| 3932 | */ |
| 3933 | __LJMP static int hlua_applet_tcp_recv_yield(lua_State *L, int status, lua_KContext ctx) |
| 3934 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3935 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3936 | struct stream_interface *si = luactx->appctx->owner; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3937 | size_t len = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3938 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3939 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3940 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3941 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3942 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3943 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3944 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3945 | ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3946 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3947 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3948 | if (ret == 0) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3949 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3950 | 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] | 3951 | } |
| 3952 | |
| 3953 | /* End of data: commit the total strings and return. */ |
| 3954 | if (ret < 0) { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3955 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3956 | return 1; |
| 3957 | } |
| 3958 | |
| 3959 | /* Ensure that the block 2 length is usable. */ |
| 3960 | if (ret == 1) |
| 3961 | len2 = 0; |
| 3962 | |
| 3963 | if (len == -1) { |
| 3964 | |
| 3965 | /* If len == -1, catenate all the data avalaile and |
| 3966 | * yield because we want to get all the data until |
| 3967 | * the end of data stream. |
| 3968 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3969 | luaL_addlstring(&luactx->b, blk1, len1); |
| 3970 | luaL_addlstring(&luactx->b, blk2, len2); |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3971 | co_skip(si_oc(si), len1 + len2); |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3972 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3973 | 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] | 3974 | |
| 3975 | } else { |
| 3976 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3977 | /* Copy the first block caping to the length required. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3978 | if (len1 > len) |
| 3979 | len1 = len; |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3980 | luaL_addlstring(&luactx->b, blk1, len1); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3981 | len -= len1; |
| 3982 | |
| 3983 | /* Copy the second block. */ |
| 3984 | if (len2 > len) |
| 3985 | len2 = len; |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3986 | luaL_addlstring(&luactx->b, blk2, len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3987 | len -= len2; |
| 3988 | |
| 3989 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3990 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3991 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3992 | /* If there is no other data available, yield waiting for new data. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3993 | if (len > 0) { |
| 3994 | lua_pushinteger(L, len); |
| 3995 | lua_replace(L, 2); |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3996 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3997 | 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] | 3998 | } |
| 3999 | |
| 4000 | /* return the result. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4001 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4002 | return 1; |
| 4003 | } |
| 4004 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4005 | /* we never execute this */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4006 | hlua_pusherror(L, "Lua: internal error"); |
| 4007 | WILL_LJMP(lua_error(L)); |
| 4008 | return 0; |
| 4009 | } |
| 4010 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4011 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4012 | __LJMP static int hlua_applet_tcp_recv(lua_State *L) |
| 4013 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4014 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4015 | int len = -1; |
| 4016 | |
| 4017 | if (lua_gettop(L) > 2) |
| 4018 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 4019 | if (lua_gettop(L) >= 2) { |
| 4020 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4021 | lua_pop(L, 1); |
| 4022 | } |
| 4023 | |
| 4024 | /* Confirm or set the required length */ |
| 4025 | lua_pushinteger(L, len); |
| 4026 | |
| 4027 | /* Initialise the string catenation. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4028 | luaL_buffinit(L, &luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4029 | |
| 4030 | return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0)); |
| 4031 | } |
| 4032 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4033 | /* Append data in the output side of the buffer. This data is immediately |
| 4034 | * sent. The function returns the amount of data written. If the buffer |
| 4035 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4036 | * if the channel is closed. |
| 4037 | */ |
| 4038 | __LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx) |
| 4039 | { |
| 4040 | size_t len; |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4041 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4042 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4043 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4044 | struct stream_interface *si = luactx->appctx->owner; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4045 | struct channel *chn = si_ic(si); |
| 4046 | int max; |
| 4047 | |
| 4048 | /* Get the max amount of data which can write as input in the channel. */ |
| 4049 | max = channel_recv_max(chn); |
| 4050 | if (max > (len - l)) |
| 4051 | max = len - l; |
| 4052 | |
| 4053 | /* Copy data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4054 | ci_putblk(chn, str + l, max); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4055 | |
| 4056 | /* update counters. */ |
| 4057 | l += max; |
| 4058 | lua_pop(L, 1); |
| 4059 | lua_pushinteger(L, l); |
| 4060 | |
| 4061 | /* If some data is not send, declares the situation to the |
| 4062 | * applet, and returns a yield. |
| 4063 | */ |
| 4064 | if (l < len) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 4065 | si_rx_room_blk(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4066 | 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] | 4067 | } |
| 4068 | |
| 4069 | return 1; |
| 4070 | } |
| 4071 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4072 | /* Just a wrapper of "hlua_applet_tcp_send_yield". This wrapper permits |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4073 | * yield the LUA process, and resume it without checking the |
| 4074 | * input arguments. |
| 4075 | */ |
| 4076 | __LJMP static int hlua_applet_tcp_send(lua_State *L) |
| 4077 | { |
| 4078 | MAY_LJMP(check_args(L, 2, "send")); |
| 4079 | lua_pushinteger(L, 0); |
| 4080 | |
| 4081 | return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0)); |
| 4082 | } |
| 4083 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4084 | /* |
| 4085 | * |
| 4086 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4087 | * Class AppletHTTP |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4088 | * |
| 4089 | * |
| 4090 | */ |
| 4091 | |
| 4092 | /* Returns a struct hlua_txn if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4093 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4094 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4095 | __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] | 4096 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4097 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_http_ref)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4098 | } |
| 4099 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4100 | /* This function creates and push in the stack an Applet object |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4101 | * according with a current TXN. |
| 4102 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4103 | static int hlua_applet_http_new(lua_State *L, struct appctx *ctx) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4104 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4105 | struct hlua_appctx *luactx; |
Thierry FOURNIER | 841475e | 2015-12-11 17:10:09 +0100 | [diff] [blame] | 4106 | struct hlua_txn htxn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4107 | struct stream_interface *si = ctx->owner; |
| 4108 | struct stream *s = si_strm(si); |
| 4109 | struct proxy *px = s->be; |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4110 | struct htx *htx; |
| 4111 | struct htx_blk *blk; |
| 4112 | struct htx_sl *sl; |
| 4113 | struct ist path; |
| 4114 | unsigned long long len = 0; |
| 4115 | int32_t pos; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4116 | |
| 4117 | /* Check stack size. */ |
| 4118 | if (!lua_checkstack(L, 3)) |
| 4119 | return 0; |
| 4120 | |
| 4121 | /* Create the object: obj[0] = userdata. |
| 4122 | * Note that the base of the Converters object is the |
| 4123 | * same than the TXN object. |
| 4124 | */ |
| 4125 | lua_newtable(L); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4126 | luactx = lua_newuserdata(L, sizeof(*luactx)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4127 | lua_rawseti(L, -2, 0); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4128 | luactx->appctx = ctx; |
| 4129 | luactx->appctx->ctx.hlua_apphttp.status = 200; /* Default status code returned. */ |
| 4130 | luactx->appctx->ctx.hlua_apphttp.reason = NULL; /* Use default reason based on status */ |
| 4131 | luactx->htxn.s = s; |
| 4132 | luactx->htxn.p = px; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4133 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4134 | /* Create the "f" field that contains a list of fetches. */ |
| 4135 | lua_pushstring(L, "f"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4136 | if (!hlua_fetches_new(L, &luactx->htxn, 0)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4137 | return 0; |
| 4138 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4139 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4140 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 4141 | lua_pushstring(L, "sf"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4142 | if (!hlua_fetches_new(L, &luactx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4143 | return 0; |
| 4144 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4145 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4146 | /* Create the "c" field that contains a list of converters. */ |
| 4147 | lua_pushstring(L, "c"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4148 | if (!hlua_converters_new(L, &luactx->htxn, 0)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4149 | return 0; |
| 4150 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4151 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4152 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 4153 | lua_pushstring(L, "sc"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4154 | if (!hlua_converters_new(L, &luactx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4155 | return 0; |
| 4156 | lua_settable(L, -3); |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4157 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4158 | htx = htxbuf(&s->req.buf); |
| 4159 | blk = htx_get_first_blk(htx); |
Christopher Faulet | ea00973 | 2019-11-18 15:50:25 +0100 | [diff] [blame] | 4160 | BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4161 | sl = htx_get_blk_ptr(htx, blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4162 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4163 | /* Stores the request method. */ |
| 4164 | lua_pushstring(L, "method"); |
| 4165 | lua_pushlstring(L, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); |
| 4166 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4167 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4168 | /* Stores the http version. */ |
| 4169 | lua_pushstring(L, "version"); |
| 4170 | lua_pushlstring(L, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); |
| 4171 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4172 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4173 | /* creates an array of headers. hlua_http_get_headers() crates and push |
| 4174 | * the array on the top of the stack. |
| 4175 | */ |
| 4176 | lua_pushstring(L, "headers"); |
| 4177 | htxn.s = s; |
| 4178 | htxn.p = px; |
| 4179 | htxn.dir = SMP_OPT_DIR_REQ; |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 4180 | if (!hlua_http_get_headers(L, &htxn.s->txn->req)) |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4181 | return 0; |
| 4182 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4183 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4184 | path = http_get_path(htx_sl_req_uri(sl)); |
Tim Duesterhus | ed52637 | 2020-03-05 17:56:33 +0100 | [diff] [blame] | 4185 | if (isttest(path)) { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4186 | char *p, *q, *end; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4187 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4188 | p = path.ptr; |
| 4189 | end = path.ptr + path.len; |
| 4190 | q = p; |
| 4191 | while (q < end && *q != '?') |
| 4192 | q++; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4193 | |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 4194 | /* Stores the request path. */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4195 | lua_pushstring(L, "path"); |
| 4196 | lua_pushlstring(L, p, q - p); |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 4197 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4198 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4199 | /* Stores the query string. */ |
| 4200 | lua_pushstring(L, "qs"); |
| 4201 | if (*q == '?') |
| 4202 | q++; |
| 4203 | lua_pushlstring(L, q, end - q); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4204 | lua_settable(L, -3); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4205 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4206 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4207 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 4208 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 4209 | enum htx_blk_type type = htx_get_blk_type(blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4210 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 4211 | if (type == HTX_BLK_TLR || type == HTX_BLK_EOT) |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4212 | break; |
| 4213 | if (type == HTX_BLK_DATA) |
| 4214 | len += htx_get_blksz(blk); |
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 | if (htx->extra != ULLONG_MAX) |
| 4217 | len += htx->extra; |
| 4218 | |
| 4219 | /* Stores the request path. */ |
| 4220 | lua_pushstring(L, "length"); |
| 4221 | lua_pushinteger(L, len); |
| 4222 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4223 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4224 | /* Create an empty array of HTTP request headers. */ |
| 4225 | lua_pushstring(L, "response"); |
| 4226 | lua_newtable(L); |
| 4227 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4228 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4229 | /* Pop a class stream metatable and affect it to the table. */ |
| 4230 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref); |
| 4231 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4232 | |
| 4233 | return 1; |
| 4234 | } |
| 4235 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4236 | __LJMP static int hlua_applet_http_set_var(lua_State *L) |
| 4237 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4238 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4239 | struct stream *s; |
| 4240 | const char *name; |
| 4241 | size_t len; |
| 4242 | struct sample smp; |
| 4243 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 4244 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 4245 | 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] | 4246 | |
| 4247 | /* It is useles to retrieve the stream, but this function |
| 4248 | * runs only in a stream context. |
| 4249 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4250 | luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4251 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4252 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4253 | |
| 4254 | /* Converts the third argument in a sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 4255 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4256 | hlua_lua2smp(L, 3, &smp); |
| 4257 | |
| 4258 | /* Store the sample in a variable. */ |
| 4259 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 4260 | |
| 4261 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 4262 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 4263 | else |
| 4264 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 4265 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 4266 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4267 | } |
| 4268 | |
| 4269 | __LJMP static int hlua_applet_http_unset_var(lua_State *L) |
| 4270 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4271 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4272 | struct stream *s; |
| 4273 | const char *name; |
| 4274 | size_t len; |
| 4275 | struct sample smp; |
| 4276 | |
| 4277 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 4278 | |
| 4279 | /* It is useles to retrieve the stream, but this function |
| 4280 | * runs only in a stream context. |
| 4281 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4282 | luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4283 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4284 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4285 | |
| 4286 | /* Unset the variable. */ |
| 4287 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 4288 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 4289 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4290 | } |
| 4291 | |
| 4292 | __LJMP static int hlua_applet_http_get_var(lua_State *L) |
| 4293 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4294 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4295 | struct stream *s; |
| 4296 | const char *name; |
| 4297 | size_t len; |
| 4298 | struct sample smp; |
| 4299 | |
| 4300 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 4301 | |
| 4302 | /* It is useles to retrieve the stream, but this function |
| 4303 | * runs only in a stream context. |
| 4304 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4305 | luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4306 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4307 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4308 | |
| 4309 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 4310 | if (!vars_get_by_name(name, len, &smp)) { |
| 4311 | lua_pushnil(L); |
| 4312 | return 1; |
| 4313 | } |
| 4314 | |
| 4315 | return hlua_smp2lua(L, &smp); |
| 4316 | } |
| 4317 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4318 | __LJMP static int hlua_applet_http_set_priv(lua_State *L) |
| 4319 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4320 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4321 | struct stream *s = luactx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4322 | struct hlua *hlua; |
| 4323 | |
| 4324 | /* 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] | 4325 | if (!s->hlua) |
| 4326 | return 0; |
| 4327 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4328 | |
| 4329 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 4330 | |
| 4331 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 4332 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4333 | |
| 4334 | /* Get and store new value. */ |
| 4335 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 4336 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 4337 | |
| 4338 | return 0; |
| 4339 | } |
| 4340 | |
| 4341 | __LJMP static int hlua_applet_http_get_priv(lua_State *L) |
| 4342 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4343 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4344 | struct stream *s = luactx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4345 | struct hlua *hlua; |
| 4346 | |
| 4347 | /* 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] | 4348 | if (!s->hlua) { |
| 4349 | lua_pushnil(L); |
| 4350 | return 1; |
| 4351 | } |
| 4352 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4353 | |
| 4354 | /* Push configuration index in the stack. */ |
| 4355 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 4356 | |
| 4357 | return 1; |
| 4358 | } |
| 4359 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4360 | /* If expected data not yet available, it returns a yield. This function |
| 4361 | * consumes the data in the buffer. It returns a string containing the |
| 4362 | * data. This string can be empty. |
| 4363 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4364 | __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] | 4365 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4366 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4367 | struct stream_interface *si = luactx->appctx->owner; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4368 | struct channel *req = si_oc(si); |
| 4369 | struct htx *htx; |
| 4370 | struct htx_blk *blk; |
| 4371 | size_t count; |
| 4372 | int stop = 0; |
| 4373 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4374 | htx = htx_from_buf(&req->buf); |
| 4375 | count = co_data(req); |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 4376 | blk = htx_get_first_blk(htx); |
Christopher Faulet | cc26b13 | 2018-12-18 21:20:57 +0100 | [diff] [blame] | 4377 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4378 | while (count && !stop && blk) { |
| 4379 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4380 | uint32_t sz = htx_get_blksz(blk); |
| 4381 | struct ist v; |
| 4382 | uint32_t vlen; |
| 4383 | char *nl; |
| 4384 | |
| 4385 | vlen = sz; |
| 4386 | if (vlen > count) { |
| 4387 | if (type != HTX_BLK_DATA) |
| 4388 | break; |
| 4389 | vlen = count; |
| 4390 | } |
| 4391 | |
| 4392 | switch (type) { |
| 4393 | case HTX_BLK_UNUSED: |
| 4394 | break; |
| 4395 | |
| 4396 | case HTX_BLK_DATA: |
| 4397 | v = htx_get_blk_value(htx, blk); |
| 4398 | v.len = vlen; |
| 4399 | nl = istchr(v, '\n'); |
| 4400 | if (nl != NULL) { |
| 4401 | stop = 1; |
| 4402 | vlen = nl - v.ptr + 1; |
| 4403 | } |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4404 | luaL_addlstring(&luactx->b, v.ptr, vlen); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4405 | break; |
| 4406 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4407 | case HTX_BLK_TLR: |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 4408 | case HTX_BLK_EOT: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4409 | stop = 1; |
| 4410 | break; |
| 4411 | |
| 4412 | default: |
| 4413 | break; |
| 4414 | } |
| 4415 | |
| 4416 | co_set_data(req, co_data(req) - vlen); |
| 4417 | count -= vlen; |
| 4418 | if (sz == vlen) |
| 4419 | blk = htx_remove_blk(htx, blk); |
| 4420 | else { |
| 4421 | htx_cut_data_blk(htx, blk, vlen); |
| 4422 | break; |
| 4423 | } |
| 4424 | } |
| 4425 | |
Christopher Faulet | eccb31c | 2021-04-02 14:24:56 +0200 | [diff] [blame] | 4426 | /* The message was fully consumed and no more data are expected |
| 4427 | * (EOM flag set). |
| 4428 | */ |
Christopher Faulet | a5a25b1 | 2022-08-29 15:37:16 +0200 | [diff] [blame] | 4429 | if (htx_is_empty(htx) && (req->flags & CF_EOI)) |
Christopher Faulet | eccb31c | 2021-04-02 14:24:56 +0200 | [diff] [blame] | 4430 | stop = 1; |
| 4431 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4432 | htx_to_buf(htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4433 | if (!stop) { |
| 4434 | si_cant_get(si); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4435 | 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] | 4436 | } |
| 4437 | |
| 4438 | /* return the result. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4439 | luaL_pushresult(&luactx->b); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4440 | return 1; |
| 4441 | } |
| 4442 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4443 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4444 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4445 | __LJMP static int hlua_applet_http_getline(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4446 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4447 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4448 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4449 | /* Initialise the string catenation. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4450 | luaL_buffinit(L, &luactx->b); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4451 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4452 | return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4453 | } |
| 4454 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4455 | /* If expected data not yet available, it returns a yield. This function |
| 4456 | * consumes the data in the buffer. It returns a string containing the |
| 4457 | * data. This string can be empty. |
| 4458 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4459 | __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] | 4460 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4461 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4462 | struct stream_interface *si = luactx->appctx->owner; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4463 | struct channel *req = si_oc(si); |
| 4464 | struct htx *htx; |
| 4465 | struct htx_blk *blk; |
| 4466 | size_t count; |
| 4467 | int len; |
| 4468 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4469 | htx = htx_from_buf(&req->buf); |
| 4470 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4471 | count = co_data(req); |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 4472 | blk = htx_get_head_blk(htx); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4473 | while (count && len && blk) { |
| 4474 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4475 | uint32_t sz = htx_get_blksz(blk); |
| 4476 | struct ist v; |
| 4477 | uint32_t vlen; |
| 4478 | |
| 4479 | vlen = sz; |
| 4480 | if (len > 0 && vlen > len) |
| 4481 | vlen = len; |
| 4482 | if (vlen > count) { |
| 4483 | if (type != HTX_BLK_DATA) |
| 4484 | break; |
| 4485 | vlen = count; |
| 4486 | } |
| 4487 | |
| 4488 | switch (type) { |
| 4489 | case HTX_BLK_UNUSED: |
| 4490 | break; |
| 4491 | |
| 4492 | case HTX_BLK_DATA: |
| 4493 | v = htx_get_blk_value(htx, blk); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4494 | luaL_addlstring(&luactx->b, v.ptr, vlen); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4495 | break; |
| 4496 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4497 | case HTX_BLK_TLR: |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 4498 | case HTX_BLK_EOT: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4499 | len = 0; |
| 4500 | break; |
| 4501 | |
| 4502 | default: |
| 4503 | break; |
| 4504 | } |
| 4505 | |
| 4506 | co_set_data(req, co_data(req) - vlen); |
| 4507 | count -= vlen; |
| 4508 | if (len > 0) |
| 4509 | len -= vlen; |
| 4510 | if (sz == vlen) |
| 4511 | blk = htx_remove_blk(htx, blk); |
| 4512 | else { |
| 4513 | htx_cut_data_blk(htx, blk, vlen); |
| 4514 | break; |
| 4515 | } |
| 4516 | } |
| 4517 | |
Christopher Faulet | eccb31c | 2021-04-02 14:24:56 +0200 | [diff] [blame] | 4518 | /* The message was fully consumed and no more data are expected |
| 4519 | * (EOM flag set). |
| 4520 | */ |
Christopher Faulet | a5a25b1 | 2022-08-29 15:37:16 +0200 | [diff] [blame] | 4521 | if (htx_is_empty(htx) && (req->flags & CF_EOI)) |
Christopher Faulet | eccb31c | 2021-04-02 14:24:56 +0200 | [diff] [blame] | 4522 | len = 0; |
| 4523 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4524 | htx_to_buf(htx, &req->buf); |
| 4525 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4526 | /* If we are no other data available, yield waiting for new data. */ |
| 4527 | if (len) { |
| 4528 | if (len > 0) { |
| 4529 | lua_pushinteger(L, len); |
| 4530 | lua_replace(L, 2); |
| 4531 | } |
| 4532 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4533 | 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] | 4534 | } |
| 4535 | |
| 4536 | /* return the result. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4537 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4538 | return 1; |
| 4539 | } |
| 4540 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4541 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4542 | __LJMP static int hlua_applet_http_recv(lua_State *L) |
| 4543 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4544 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4545 | int len = -1; |
| 4546 | |
| 4547 | /* Check arguments. */ |
| 4548 | if (lua_gettop(L) > 2) |
| 4549 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 4550 | if (lua_gettop(L) >= 2) { |
| 4551 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4552 | lua_pop(L, 1); |
| 4553 | } |
| 4554 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4555 | lua_pushinteger(L, len); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4556 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4557 | /* Initialise the string catenation. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4558 | luaL_buffinit(L, &luactx->b); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4559 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4560 | return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4561 | } |
| 4562 | |
| 4563 | /* Append data in the output side of the buffer. This data is immediately |
| 4564 | * sent. The function returns the amount of data written. If the buffer |
| 4565 | * cannot contain the data, the function yields. The function returns -1 |
| 4566 | * if the channel is closed. |
| 4567 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4568 | __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] | 4569 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4570 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4571 | struct stream_interface *si = luactx->appctx->owner; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4572 | struct channel *res = si_ic(si); |
| 4573 | struct htx *htx = htx_from_buf(&res->buf); |
| 4574 | const char *data; |
| 4575 | size_t len; |
| 4576 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 4577 | int max; |
| 4578 | |
Christopher Faulet | 9060fc0 | 2019-07-03 11:39:30 +0200 | [diff] [blame] | 4579 | max = htx_get_max_blksz(htx, channel_htx_recv_max(res, htx)); |
Christopher Faulet | 4b0e9b2 | 2019-01-09 12:16:58 +0100 | [diff] [blame] | 4580 | if (!max) |
| 4581 | goto snd_yield; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4582 | |
| 4583 | data = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4584 | |
| 4585 | /* Get the max amount of data which can write as input in the channel. */ |
| 4586 | if (max > (len - l)) |
| 4587 | max = len - l; |
| 4588 | |
| 4589 | /* Copy data. */ |
Willy Tarreau | 0a7ef02 | 2019-05-28 10:30:11 +0200 | [diff] [blame] | 4590 | max = htx_add_data(htx, ist2(data + l, max)); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 4591 | channel_add_input(res, max); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4592 | |
| 4593 | /* update counters. */ |
| 4594 | l += max; |
| 4595 | lua_pop(L, 1); |
| 4596 | lua_pushinteger(L, l); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4597 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4598 | /* If some data is not send, declares the situation to the |
| 4599 | * applet, and returns a yield. |
| 4600 | */ |
| 4601 | if (l < len) { |
Christopher Faulet | 4b0e9b2 | 2019-01-09 12:16:58 +0100 | [diff] [blame] | 4602 | snd_yield: |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4603 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4604 | si_rx_room_blk(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4605 | 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] | 4606 | } |
| 4607 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4608 | htx_to_buf(htx, &res->buf); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4609 | return 1; |
| 4610 | } |
| 4611 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4612 | /* Just a wrapper of "hlua_applet_send_yield". This wrapper permits |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4613 | * yield the LUA process, and resume it without checking the |
| 4614 | * input arguments. |
| 4615 | */ |
| 4616 | __LJMP static int hlua_applet_http_send(lua_State *L) |
| 4617 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4618 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4619 | |
| 4620 | /* We want to send some data. Headers must be sent. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4621 | if (!(luactx->appctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4622 | hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data."); |
| 4623 | WILL_LJMP(lua_error(L)); |
| 4624 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4625 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4626 | /* This integer is used for followinf the amount of data sent. */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4627 | lua_pushinteger(L, 0); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4628 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4629 | return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4630 | } |
| 4631 | |
| 4632 | __LJMP static int hlua_applet_http_addheader(lua_State *L) |
| 4633 | { |
| 4634 | const char *name; |
| 4635 | int ret; |
| 4636 | |
| 4637 | MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4638 | name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 4639 | MAY_LJMP(luaL_checkstring(L, 3)); |
| 4640 | |
| 4641 | /* Push in the stack the "response" entry. */ |
| 4642 | ret = lua_getfield(L, 1, "response"); |
| 4643 | if (ret != LUA_TTABLE) { |
| 4644 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] " |
| 4645 | "is expected as an array. %s found", lua_typename(L, ret)); |
| 4646 | WILL_LJMP(lua_error(L)); |
| 4647 | } |
| 4648 | |
| 4649 | /* check if the header is already registered if it is not |
| 4650 | * the case, register it. |
| 4651 | */ |
| 4652 | ret = lua_getfield(L, -1, name); |
| 4653 | if (ret == LUA_TNIL) { |
| 4654 | |
| 4655 | /* Entry not found. */ |
| 4656 | lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */ |
| 4657 | |
| 4658 | /* Insert the new header name in the array in the top of the stack. |
| 4659 | * It left the new array in the top of the stack. |
| 4660 | */ |
| 4661 | lua_newtable(L); |
| 4662 | lua_pushvalue(L, 2); |
| 4663 | lua_pushvalue(L, -2); |
| 4664 | lua_settable(L, -4); |
| 4665 | |
| 4666 | } else if (ret != LUA_TTABLE) { |
| 4667 | |
| 4668 | /* corruption error. */ |
| 4669 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] " |
| 4670 | "is expected as an array. %s found", name, lua_typename(L, ret)); |
| 4671 | WILL_LJMP(lua_error(L)); |
| 4672 | } |
| 4673 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 4674 | /* Now the top of thestack is an array of values. We push |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4675 | * the header value as new entry. |
| 4676 | */ |
| 4677 | lua_pushvalue(L, 3); |
| 4678 | ret = lua_rawlen(L, -2); |
| 4679 | lua_rawseti(L, -2, ret + 1); |
| 4680 | lua_pushboolean(L, 1); |
| 4681 | return 1; |
| 4682 | } |
| 4683 | |
| 4684 | __LJMP static int hlua_applet_http_status(lua_State *L) |
| 4685 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4686 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4687 | int status = MAY_LJMP(luaL_checkinteger(L, 2)); |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4688 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4689 | |
| 4690 | if (status < 100 || status > 599) { |
| 4691 | lua_pushboolean(L, 0); |
| 4692 | return 1; |
| 4693 | } |
| 4694 | |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4695 | luactx->appctx->ctx.hlua_apphttp.status = status; |
| 4696 | luactx->appctx->ctx.hlua_apphttp.reason = reason; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4697 | lua_pushboolean(L, 1); |
| 4698 | return 1; |
| 4699 | } |
| 4700 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4701 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4702 | __LJMP static int hlua_applet_http_send_response(lua_State *L) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4703 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4704 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4705 | struct stream_interface *si = luactx->appctx->owner; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4706 | struct channel *res = si_ic(si); |
| 4707 | struct htx *htx; |
| 4708 | struct htx_sl *sl; |
| 4709 | struct h1m h1m; |
| 4710 | const char *status, *reason; |
| 4711 | const char *name, *value; |
| 4712 | size_t nlen, vlen; |
| 4713 | unsigned int flags; |
| 4714 | |
| 4715 | /* Send the message at once. */ |
| 4716 | htx = htx_from_buf(&res->buf); |
| 4717 | h1m_init_res(&h1m); |
| 4718 | |
| 4719 | /* Use the same http version than the request. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4720 | status = ultoa_r(luactx->appctx->ctx.hlua_apphttp.status, trash.area, trash.size); |
| 4721 | reason = luactx->appctx->ctx.hlua_apphttp.reason; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4722 | if (reason == NULL) |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4723 | reason = http_get_reason(luactx->appctx->ctx.hlua_apphttp.status); |
| 4724 | if (luactx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4725 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11); |
| 4726 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist(status), ist(reason)); |
| 4727 | } |
| 4728 | else { |
| 4729 | flags = HTX_SL_F_IS_RESP; |
| 4730 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), ist(status), ist(reason)); |
| 4731 | } |
| 4732 | if (!sl) { |
| 4733 | hlua_pusherror(L, "Lua applet http '%s': Failed to create response.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4734 | luactx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4735 | WILL_LJMP(lua_error(L)); |
| 4736 | } |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4737 | sl->info.res.status = luactx->appctx->ctx.hlua_apphttp.status; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4738 | |
| 4739 | /* Get the array associated to the field "response" in the object AppletHTTP. */ |
| 4740 | lua_pushvalue(L, 0); |
| 4741 | if (lua_getfield(L, 1, "response") != LUA_TTABLE) { |
| 4742 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4743 | luactx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4744 | WILL_LJMP(lua_error(L)); |
| 4745 | } |
| 4746 | |
| 4747 | /* Browse the list of headers. */ |
| 4748 | lua_pushnil(L); |
| 4749 | while(lua_next(L, -2) != 0) { |
| 4750 | /* We expect a string as -2. */ |
| 4751 | if (lua_type(L, -2) != LUA_TSTRING) { |
| 4752 | 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] | 4753 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4754 | lua_typename(L, lua_type(L, -2))); |
| 4755 | WILL_LJMP(lua_error(L)); |
| 4756 | } |
| 4757 | name = lua_tolstring(L, -2, &nlen); |
| 4758 | |
| 4759 | /* We expect an array as -1. */ |
| 4760 | if (lua_type(L, -1) != LUA_TTABLE) { |
| 4761 | 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] | 4762 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4763 | name, |
| 4764 | lua_typename(L, lua_type(L, -1))); |
| 4765 | WILL_LJMP(lua_error(L)); |
| 4766 | } |
| 4767 | |
| 4768 | /* Browse the table who is on the top of the stack. */ |
| 4769 | lua_pushnil(L); |
| 4770 | while(lua_next(L, -2) != 0) { |
| 4771 | int id; |
| 4772 | |
| 4773 | /* We expect a number as -2. */ |
| 4774 | if (lua_type(L, -2) != LUA_TNUMBER) { |
| 4775 | 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] | 4776 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4777 | name, |
| 4778 | lua_typename(L, lua_type(L, -2))); |
| 4779 | WILL_LJMP(lua_error(L)); |
| 4780 | } |
| 4781 | id = lua_tointeger(L, -2); |
| 4782 | |
| 4783 | /* We expect a string as -2. */ |
| 4784 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 4785 | 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] | 4786 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4787 | name, id, |
| 4788 | lua_typename(L, lua_type(L, -1))); |
| 4789 | WILL_LJMP(lua_error(L)); |
| 4790 | } |
| 4791 | value = lua_tolstring(L, -1, &vlen); |
| 4792 | |
| 4793 | /* Simple Protocol checks. */ |
| 4794 | if (isteqi(ist2(name, nlen), ist("transfer-encoding"))) |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 4795 | h1_parse_xfer_enc_header(&h1m, ist2(value, vlen)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4796 | else if (isteqi(ist2(name, nlen), ist("content-length"))) { |
| 4797 | struct ist v = ist2(value, vlen); |
| 4798 | int ret; |
| 4799 | |
| 4800 | ret = h1_parse_cont_len_header(&h1m, &v); |
| 4801 | if (ret < 0) { |
| 4802 | hlua_pusherror(L, "Lua applet http '%s': Invalid '%s' header.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4803 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4804 | name); |
| 4805 | WILL_LJMP(lua_error(L)); |
| 4806 | } |
| 4807 | else if (ret == 0) |
| 4808 | goto next; /* Skip it */ |
| 4809 | } |
| 4810 | |
| 4811 | /* Add a new header */ |
| 4812 | if (!htx_add_header(htx, ist2(name, nlen), ist2(value, vlen))) { |
| 4813 | 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] | 4814 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4815 | name); |
| 4816 | WILL_LJMP(lua_error(L)); |
| 4817 | } |
| 4818 | next: |
| 4819 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 4820 | lua_pop(L, 1); |
| 4821 | } |
| 4822 | |
| 4823 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 4824 | lua_pop(L, 1); |
| 4825 | } |
| 4826 | |
| 4827 | if (h1m.flags & H1_MF_CHNK) |
| 4828 | h1m.flags &= ~H1_MF_CLEN; |
| 4829 | if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK)) |
| 4830 | h1m.flags |= H1_MF_XFER_LEN; |
| 4831 | |
| 4832 | /* Uset HTX start-line flags */ |
| 4833 | if (h1m.flags & H1_MF_XFER_ENC) |
| 4834 | flags |= HTX_SL_F_XFER_ENC; |
| 4835 | if (h1m.flags & H1_MF_XFER_LEN) { |
| 4836 | flags |= HTX_SL_F_XFER_LEN; |
| 4837 | if (h1m.flags & H1_MF_CHNK) |
| 4838 | flags |= HTX_SL_F_CHNK; |
| 4839 | else if (h1m.flags & H1_MF_CLEN) |
| 4840 | flags |= HTX_SL_F_CLEN; |
| 4841 | if (h1m.body_len == 0) |
| 4842 | flags |= HTX_SL_F_BODYLESS; |
| 4843 | } |
| 4844 | sl->flags |= flags; |
| 4845 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 4846 | /* 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] | 4847 | * and the status code implies the presence of a message body, we must |
| 4848 | * announce a transfer encoding chunked. This is required by haproxy |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4849 | * for the keepalive compliance. If the applet announces a transfer-encoding |
| 4850 | * chunked itself, don't do anything. |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4851 | */ |
| 4852 | 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] | 4853 | luactx->appctx->ctx.hlua_apphttp.status >= 200 && |
| 4854 | luactx->appctx->ctx.hlua_apphttp.status != 204 && |
| 4855 | luactx->appctx->ctx.hlua_apphttp.status != 304) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4856 | /* Add a new header */ |
| 4857 | sl->flags |= (HTX_SL_F_XFER_ENC|H1_MF_CHNK|H1_MF_XFER_LEN); |
| 4858 | if (!htx_add_header(htx, ist("transfer-encoding"), ist("chunked"))) { |
| 4859 | 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] | 4860 | luactx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4861 | WILL_LJMP(lua_error(L)); |
| 4862 | } |
| 4863 | } |
| 4864 | |
| 4865 | /* Finalize headers. */ |
| 4866 | if (!htx_add_endof(htx, HTX_BLK_EOH)) { |
| 4867 | hlua_pusherror(L, "Lua applet http '%s': Failed create the response.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4868 | luactx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4869 | WILL_LJMP(lua_error(L)); |
| 4870 | } |
| 4871 | |
| 4872 | if (htx_used_space(htx) > b_size(&res->buf) - global.tune.maxrewrite) { |
| 4873 | b_reset(&res->buf); |
| 4874 | hlua_pusherror(L, "Lua: 'start_response': response header block too big"); |
| 4875 | WILL_LJMP(lua_error(L)); |
| 4876 | } |
| 4877 | |
| 4878 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 4879 | channel_add_input(res, htx->data); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4880 | |
| 4881 | /* Headers sent, set the flag. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4882 | luactx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4883 | return 0; |
| 4884 | |
| 4885 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4886 | /* We will build the status line and the headers of the HTTP response. |
| 4887 | * We will try send at once if its not possible, we give back the hand |
| 4888 | * waiting for more room. |
| 4889 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4890 | __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] | 4891 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4892 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4893 | struct stream_interface *si = luactx->appctx->owner; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4894 | struct channel *res = si_ic(si); |
| 4895 | |
| 4896 | if (co_data(res)) { |
| 4897 | si_rx_room_blk(si); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4898 | 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] | 4899 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4900 | return MAY_LJMP(hlua_applet_http_send_response(L)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4901 | } |
| 4902 | |
| 4903 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4904 | __LJMP static int hlua_applet_http_start_response(lua_State *L) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4905 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4906 | return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4907 | } |
| 4908 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4909 | /* |
| 4910 | * |
| 4911 | * |
| 4912 | * Class HTTP |
| 4913 | * |
| 4914 | * |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4915 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4916 | |
| 4917 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 4918 | * a class stream, otherwise it throws an error. |
| 4919 | */ |
| 4920 | __LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4921 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4922 | return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref)); |
| 4923 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4924 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4925 | /* This function creates and push in the stack a HTTP object |
| 4926 | * according with a current TXN. |
| 4927 | */ |
| 4928 | static int hlua_http_new(lua_State *L, struct hlua_txn *txn) |
| 4929 | { |
| 4930 | struct hlua_txn *htxn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4931 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4932 | /* Check stack size. */ |
| 4933 | if (!lua_checkstack(L, 3)) |
| 4934 | return 0; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4935 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4936 | /* Create the object: obj[0] = userdata. |
| 4937 | * Note that the base of the Converters object is the |
| 4938 | * same than the TXN object. |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4939 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4940 | lua_newtable(L); |
| 4941 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
| 4942 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4943 | |
| 4944 | htxn->s = txn->s; |
| 4945 | htxn->p = txn->p; |
Christopher Faulet | 256b69a | 2019-05-23 11:14:21 +0200 | [diff] [blame] | 4946 | htxn->dir = txn->dir; |
| 4947 | htxn->flags = txn->flags; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4948 | |
| 4949 | /* Pop a class stream metatable and affect it to the table. */ |
| 4950 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref); |
| 4951 | lua_setmetatable(L, -2); |
| 4952 | |
| 4953 | return 1; |
| 4954 | } |
| 4955 | |
| 4956 | /* This function creates ans returns an array of HTTP headers. |
| 4957 | * This function does not fails. It is used as wrapper with the |
| 4958 | * 2 following functions. |
| 4959 | */ |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 4960 | __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] | 4961 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4962 | struct htx *htx; |
| 4963 | int32_t pos; |
| 4964 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4965 | /* Create the table. */ |
| 4966 | lua_newtable(L); |
| 4967 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4968 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4969 | htx = htxbuf(&msg->chn->buf); |
| 4970 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 4971 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 4972 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4973 | struct ist n, v; |
| 4974 | int len; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 4975 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4976 | if (type == HTX_BLK_HDR) { |
| 4977 | n = htx_get_blk_name(htx,blk); |
| 4978 | v = htx_get_blk_value(htx, blk); |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 4979 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4980 | else if (type == HTX_BLK_EOH) |
| 4981 | break; |
| 4982 | else |
| 4983 | continue; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 4984 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4985 | /* Check for existing entry: |
| 4986 | * assume that the table is on the top of the stack, and |
| 4987 | * push the key in the stack, the function lua_gettable() |
| 4988 | * perform the lookup. |
| 4989 | */ |
| 4990 | lua_pushlstring(L, n.ptr, n.len); |
| 4991 | lua_gettable(L, -2); |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 4992 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4993 | switch (lua_type(L, -1)) { |
| 4994 | case LUA_TNIL: |
| 4995 | /* Table not found, create it. */ |
| 4996 | lua_pop(L, 1); /* remove the nil value. */ |
| 4997 | lua_pushlstring(L, n.ptr, n.len); /* push the header name as key. */ |
| 4998 | lua_newtable(L); /* create and push empty table. */ |
| 4999 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 5000 | lua_rawseti(L, -2, 0); /* index header value (pop it). */ |
| 5001 | 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] | 5002 | break; |
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 | case LUA_TTABLE: |
| 5005 | /* Entry found: push the value in the table. */ |
| 5006 | len = lua_rawlen(L, -1); |
| 5007 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 5008 | lua_rawseti(L, -2, len+1); /* index header value (pop it). */ |
| 5009 | lua_pop(L, 1); /* remove the table (it is stored in the main table). */ |
| 5010 | break; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5011 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5012 | default: |
| 5013 | /* Other cases are errors. */ |
| 5014 | hlua_pusherror(L, "internal error during the parsing of headers."); |
| 5015 | WILL_LJMP(lua_error(L)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5016 | } |
| 5017 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5018 | return 1; |
| 5019 | } |
| 5020 | |
| 5021 | __LJMP static int hlua_http_req_get_headers(lua_State *L) |
| 5022 | { |
| 5023 | struct hlua_txn *htxn; |
| 5024 | |
| 5025 | MAY_LJMP(check_args(L, 1, "req_get_headers")); |
| 5026 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5027 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5028 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5029 | WILL_LJMP(lua_error(L)); |
| 5030 | |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 5031 | return hlua_http_get_headers(L, &htxn->s->txn->req); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5032 | } |
| 5033 | |
| 5034 | __LJMP static int hlua_http_res_get_headers(lua_State *L) |
| 5035 | { |
| 5036 | struct hlua_txn *htxn; |
| 5037 | |
| 5038 | MAY_LJMP(check_args(L, 1, "res_get_headers")); |
| 5039 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5040 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5041 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5042 | WILL_LJMP(lua_error(L)); |
| 5043 | |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 5044 | return hlua_http_get_headers(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5045 | } |
| 5046 | |
| 5047 | /* This function replace full header, or just a value in |
| 5048 | * the request or in the response. It is a wrapper fir the |
| 5049 | * 4 following functions. |
| 5050 | */ |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5051 | __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] | 5052 | { |
| 5053 | size_t name_len; |
| 5054 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5055 | const char *reg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 5056 | const char *value = MAY_LJMP(luaL_checkstring(L, 4)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5057 | struct htx *htx; |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 5058 | struct my_regex *re; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5059 | |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 5060 | if (!(re = regex_comp(reg, 1, 1, NULL))) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5061 | WILL_LJMP(luaL_argerror(L, 3, "invalid regex")); |
| 5062 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5063 | htx = htxbuf(&msg->chn->buf); |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5064 | 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] | 5065 | regex_free(re); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5066 | return 0; |
| 5067 | } |
| 5068 | |
| 5069 | __LJMP static int hlua_http_req_rep_hdr(lua_State *L) |
| 5070 | { |
| 5071 | struct hlua_txn *htxn; |
| 5072 | |
| 5073 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 5074 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5075 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5076 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5077 | WILL_LJMP(lua_error(L)); |
| 5078 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5079 | 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] | 5080 | } |
| 5081 | |
| 5082 | __LJMP static int hlua_http_res_rep_hdr(lua_State *L) |
| 5083 | { |
| 5084 | struct hlua_txn *htxn; |
| 5085 | |
| 5086 | MAY_LJMP(check_args(L, 4, "res_rep_hdr")); |
| 5087 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5088 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5089 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5090 | WILL_LJMP(lua_error(L)); |
| 5091 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5092 | 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] | 5093 | } |
| 5094 | |
| 5095 | __LJMP static int hlua_http_req_rep_val(lua_State *L) |
| 5096 | { |
| 5097 | struct hlua_txn *htxn; |
| 5098 | |
| 5099 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 5100 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5101 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5102 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5103 | WILL_LJMP(lua_error(L)); |
| 5104 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5105 | 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] | 5106 | } |
| 5107 | |
| 5108 | __LJMP static int hlua_http_res_rep_val(lua_State *L) |
| 5109 | { |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5110 | struct hlua_txn *htxn; |
| 5111 | |
| 5112 | MAY_LJMP(check_args(L, 4, "res_rep_val")); |
| 5113 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5114 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5115 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5116 | WILL_LJMP(lua_error(L)); |
| 5117 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5118 | 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] | 5119 | } |
| 5120 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5121 | /* This function deletes all the occurrences of an header. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5122 | * It is a wrapper for the 2 following functions. |
| 5123 | */ |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5124 | __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] | 5125 | { |
| 5126 | size_t len; |
| 5127 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5128 | struct htx *htx = htxbuf(&msg->chn->buf); |
| 5129 | struct http_hdr_ctx ctx; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5130 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5131 | ctx.blk = NULL; |
| 5132 | while (http_find_header(htx, ist2(name, len), &ctx, 1)) |
| 5133 | http_remove_header(htx, &ctx); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5134 | return 0; |
| 5135 | } |
| 5136 | |
| 5137 | __LJMP static int hlua_http_req_del_hdr(lua_State *L) |
| 5138 | { |
| 5139 | struct hlua_txn *htxn; |
| 5140 | |
| 5141 | MAY_LJMP(check_args(L, 2, "req_del_hdr")); |
| 5142 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5143 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5144 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5145 | WILL_LJMP(lua_error(L)); |
| 5146 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5147 | return hlua_http_del_hdr(L, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5148 | } |
| 5149 | |
| 5150 | __LJMP static int hlua_http_res_del_hdr(lua_State *L) |
| 5151 | { |
| 5152 | struct hlua_txn *htxn; |
| 5153 | |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5154 | MAY_LJMP(check_args(L, 2, "res_del_hdr")); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5155 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5156 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5157 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5158 | WILL_LJMP(lua_error(L)); |
| 5159 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5160 | return hlua_http_del_hdr(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5161 | } |
| 5162 | |
| 5163 | /* This function adds an header. It is a wrapper used by |
| 5164 | * the 2 following functions. |
| 5165 | */ |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5166 | __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] | 5167 | { |
| 5168 | size_t name_len; |
| 5169 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5170 | size_t value_len; |
| 5171 | const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5172 | struct htx *htx = htxbuf(&msg->chn->buf); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5173 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5174 | lua_pushboolean(L, http_add_header(htx, ist2(name, name_len), |
| 5175 | ist2(value, value_len))); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5176 | return 0; |
| 5177 | } |
| 5178 | |
| 5179 | __LJMP static int hlua_http_req_add_hdr(lua_State *L) |
| 5180 | { |
| 5181 | struct hlua_txn *htxn; |
| 5182 | |
| 5183 | MAY_LJMP(check_args(L, 3, "req_add_hdr")); |
| 5184 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5185 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5186 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5187 | WILL_LJMP(lua_error(L)); |
| 5188 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5189 | return hlua_http_add_hdr(L, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5190 | } |
| 5191 | |
| 5192 | __LJMP static int hlua_http_res_add_hdr(lua_State *L) |
| 5193 | { |
| 5194 | struct hlua_txn *htxn; |
| 5195 | |
| 5196 | MAY_LJMP(check_args(L, 3, "res_add_hdr")); |
| 5197 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5198 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5199 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5200 | WILL_LJMP(lua_error(L)); |
| 5201 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5202 | return hlua_http_add_hdr(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5203 | } |
| 5204 | |
| 5205 | static int hlua_http_req_set_hdr(lua_State *L) |
| 5206 | { |
| 5207 | struct hlua_txn *htxn; |
| 5208 | |
| 5209 | MAY_LJMP(check_args(L, 3, "req_set_hdr")); |
| 5210 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5211 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5212 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5213 | WILL_LJMP(lua_error(L)); |
| 5214 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5215 | hlua_http_del_hdr(L, &htxn->s->txn->req); |
| 5216 | return hlua_http_add_hdr(L, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5217 | } |
| 5218 | |
| 5219 | static int hlua_http_res_set_hdr(lua_State *L) |
| 5220 | { |
| 5221 | struct hlua_txn *htxn; |
| 5222 | |
| 5223 | MAY_LJMP(check_args(L, 3, "res_set_hdr")); |
| 5224 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5225 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5226 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5227 | WILL_LJMP(lua_error(L)); |
| 5228 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5229 | hlua_http_del_hdr(L, &htxn->s->txn->rsp); |
| 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 | /* This function set the method. */ |
| 5234 | static int hlua_http_req_set_meth(lua_State *L) |
| 5235 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5236 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5237 | size_t name_len; |
| 5238 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 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 | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5243 | 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] | 5244 | return 1; |
| 5245 | } |
| 5246 | |
| 5247 | /* This function set the method. */ |
| 5248 | static int hlua_http_req_set_path(lua_State *L) |
| 5249 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5250 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5251 | size_t name_len; |
| 5252 | 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] | 5253 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5254 | if (htxn->dir != SMP_OPT_DIR_REQ || !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 | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5257 | 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] | 5258 | return 1; |
| 5259 | } |
| 5260 | |
| 5261 | /* This function set the query-string. */ |
| 5262 | static int hlua_http_req_set_query(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 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5271 | /* Check length. */ |
| 5272 | if (name_len > trash.size - 1) { |
| 5273 | lua_pushboolean(L, 0); |
| 5274 | return 1; |
| 5275 | } |
| 5276 | |
| 5277 | /* Add the mark question as prefix. */ |
| 5278 | chunk_reset(&trash); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5279 | trash.area[trash.data++] = '?'; |
| 5280 | memcpy(trash.area + trash.data, name, name_len); |
| 5281 | trash.data += name_len; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5282 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5283 | lua_pushboolean(L, |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5284 | 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] | 5285 | return 1; |
| 5286 | } |
| 5287 | |
| 5288 | /* This function set the uri. */ |
| 5289 | static int hlua_http_req_set_uri(lua_State *L) |
| 5290 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5291 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5292 | size_t name_len; |
| 5293 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5294 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5295 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5296 | WILL_LJMP(lua_error(L)); |
| 5297 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5298 | 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] | 5299 | return 1; |
| 5300 | } |
| 5301 | |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5302 | /* This function set the response code & optionally reason. */ |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5303 | static int hlua_http_res_set_status(lua_State *L) |
| 5304 | { |
| 5305 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5306 | unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2)); |
Christopher Faulet | 96bff76 | 2019-12-17 13:46:18 +0100 | [diff] [blame] | 5307 | const char *str = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
| 5308 | const struct ist reason = ist2(str, (str ? strlen(str) : 0)); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5309 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5310 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5311 | WILL_LJMP(lua_error(L)); |
| 5312 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5313 | http_res_set_status(code, reason, htxn->s); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5314 | return 0; |
| 5315 | } |
| 5316 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5317 | /* |
| 5318 | * |
| 5319 | * |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5320 | * Class TXN |
| 5321 | * |
| 5322 | * |
| 5323 | */ |
| 5324 | |
| 5325 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5326 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5327 | */ |
| 5328 | __LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud) |
| 5329 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 5330 | return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref)); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5331 | } |
| 5332 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5333 | __LJMP static int hlua_set_var(lua_State *L) |
| 5334 | { |
| 5335 | struct hlua_txn *htxn; |
| 5336 | const char *name; |
| 5337 | size_t len; |
| 5338 | struct sample smp; |
| 5339 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 5340 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 5341 | 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] | 5342 | |
| 5343 | /* It is useles to retrieve the stream, but this function |
| 5344 | * runs only in a stream context. |
| 5345 | */ |
| 5346 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5347 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5348 | |
| 5349 | /* Converts the third argument in a sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 5350 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5351 | hlua_lua2smp(L, 3, &smp); |
| 5352 | |
| 5353 | /* Store the sample in a variable. */ |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 5354 | 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] | 5355 | |
| 5356 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 5357 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 5358 | else |
| 5359 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 5360 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 5361 | return 1; |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5362 | } |
| 5363 | |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 5364 | __LJMP static int hlua_unset_var(lua_State *L) |
| 5365 | { |
| 5366 | struct hlua_txn *htxn; |
| 5367 | const char *name; |
| 5368 | size_t len; |
| 5369 | struct sample smp; |
| 5370 | |
| 5371 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 5372 | |
| 5373 | /* It is useles to retrieve the stream, but this function |
| 5374 | * runs only in a stream context. |
| 5375 | */ |
| 5376 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5377 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5378 | |
| 5379 | /* Unset the variable. */ |
| 5380 | 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] | 5381 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 5382 | return 1; |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 5383 | } |
| 5384 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5385 | __LJMP static int hlua_get_var(lua_State *L) |
| 5386 | { |
| 5387 | struct hlua_txn *htxn; |
| 5388 | const char *name; |
| 5389 | size_t len; |
| 5390 | struct sample smp; |
| 5391 | |
| 5392 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 5393 | |
| 5394 | /* It is useles to retrieve the stream, but this function |
| 5395 | * runs only in a stream context. |
| 5396 | */ |
| 5397 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5398 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5399 | |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 5400 | 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] | 5401 | if (!vars_get_by_name(name, len, &smp)) { |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5402 | lua_pushnil(L); |
| 5403 | return 1; |
| 5404 | } |
| 5405 | |
| 5406 | return hlua_smp2lua(L, &smp); |
| 5407 | } |
| 5408 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5409 | __LJMP static int hlua_set_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5410 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5411 | struct hlua *hlua; |
| 5412 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5413 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 5414 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5415 | /* It is useles to retrieve the stream, but this function |
| 5416 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5417 | */ |
| 5418 | MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 5419 | |
| 5420 | /* Get hlua struct, or NULL if we execute from main lua state */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5421 | hlua = hlua_gethlua(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 5422 | if (!hlua) |
| 5423 | return 0; |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5424 | |
| 5425 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 5426 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5427 | |
| 5428 | /* Get and store new value. */ |
| 5429 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 5430 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 5431 | |
| 5432 | return 0; |
| 5433 | } |
| 5434 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5435 | __LJMP static int hlua_get_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5436 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5437 | struct hlua *hlua; |
| 5438 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5439 | MAY_LJMP(check_args(L, 1, "get_priv")); |
| 5440 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5441 | /* It is useles to retrieve the stream, but this function |
| 5442 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5443 | */ |
| 5444 | MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 5445 | |
| 5446 | /* Get hlua struct, or NULL if we execute from main lua state */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5447 | hlua = hlua_gethlua(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 5448 | if (!hlua) { |
| 5449 | lua_pushnil(L); |
| 5450 | return 1; |
| 5451 | } |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5452 | |
| 5453 | /* Push configuration index in the stack. */ |
| 5454 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 5455 | |
| 5456 | return 1; |
| 5457 | } |
| 5458 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5459 | /* Create stack entry containing a class TXN. This function |
| 5460 | * return 0 if the stack does not contains free slots, |
| 5461 | * otherwise it returns 1. |
| 5462 | */ |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5463 | 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] | 5464 | { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5465 | struct hlua_txn *htxn; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5466 | |
| 5467 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5468 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5469 | return 0; |
| 5470 | |
| 5471 | /* NOTE: The allocation never fails. The failure |
| 5472 | * throw an error, and the function never returns. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5473 | * if the throw is not available, the process is aborted. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5474 | */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5475 | /* Create the object: obj[0] = userdata. */ |
| 5476 | lua_newtable(L); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5477 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5478 | lua_rawseti(L, -2, 0); |
| 5479 | |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5480 | htxn->s = s; |
| 5481 | htxn->p = p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 5482 | htxn->dir = dir; |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5483 | htxn->flags = flags; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5484 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 5485 | /* Create the "f" field that contains a list of fetches. */ |
| 5486 | lua_pushstring(L, "f"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 5487 | if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5488 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5489 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5490 | |
| 5491 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 5492 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 5493 | 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] | 5494 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5495 | lua_rawset(L, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 5496 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5497 | /* Create the "c" field that contains a list of converters. */ |
| 5498 | lua_pushstring(L, "c"); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5499 | if (!hlua_converters_new(L, htxn, 0)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5500 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5501 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5502 | |
| 5503 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 5504 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 5505 | if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5506 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5507 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5508 | |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5509 | /* Create the "req" field that contains the request channel object. */ |
| 5510 | lua_pushstring(L, "req"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 5511 | if (!hlua_channel_new(L, &s->req)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5512 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5513 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5514 | |
| 5515 | /* Create the "res" field that contains the response channel object. */ |
| 5516 | lua_pushstring(L, "res"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 5517 | if (!hlua_channel_new(L, &s->res)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5518 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5519 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5520 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5521 | /* Creates the HTTP object is the current proxy allows http. */ |
| 5522 | lua_pushstring(L, "http"); |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 5523 | if (IS_HTX_STRM(s)) { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5524 | if (!hlua_http_new(L, htxn)) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5525 | return 0; |
| 5526 | } |
| 5527 | else |
| 5528 | lua_pushnil(L); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5529 | lua_rawset(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5530 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5531 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 5532 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref); |
| 5533 | lua_setmetatable(L, -2); |
| 5534 | |
| 5535 | return 1; |
| 5536 | } |
| 5537 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 5538 | __LJMP static int hlua_txn_deflog(lua_State *L) |
| 5539 | { |
| 5540 | const char *msg; |
| 5541 | struct hlua_txn *htxn; |
| 5542 | |
| 5543 | MAY_LJMP(check_args(L, 2, "deflog")); |
| 5544 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5545 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5546 | |
| 5547 | hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg); |
| 5548 | return 0; |
| 5549 | } |
| 5550 | |
| 5551 | __LJMP static int hlua_txn_log(lua_State *L) |
| 5552 | { |
| 5553 | int level; |
| 5554 | const char *msg; |
| 5555 | struct hlua_txn *htxn; |
| 5556 | |
| 5557 | MAY_LJMP(check_args(L, 3, "log")); |
| 5558 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5559 | level = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5560 | msg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 5561 | |
| 5562 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 5563 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 5564 | |
| 5565 | hlua_sendlog(htxn->s->be, level, msg); |
| 5566 | return 0; |
| 5567 | } |
| 5568 | |
| 5569 | __LJMP static int hlua_txn_log_debug(lua_State *L) |
| 5570 | { |
| 5571 | const char *msg; |
| 5572 | struct hlua_txn *htxn; |
| 5573 | |
| 5574 | MAY_LJMP(check_args(L, 2, "Debug")); |
| 5575 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5576 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5577 | hlua_sendlog(htxn->s->be, LOG_DEBUG, msg); |
| 5578 | return 0; |
| 5579 | } |
| 5580 | |
| 5581 | __LJMP static int hlua_txn_log_info(lua_State *L) |
| 5582 | { |
| 5583 | const char *msg; |
| 5584 | struct hlua_txn *htxn; |
| 5585 | |
| 5586 | MAY_LJMP(check_args(L, 2, "Info")); |
| 5587 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5588 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5589 | hlua_sendlog(htxn->s->be, LOG_INFO, msg); |
| 5590 | return 0; |
| 5591 | } |
| 5592 | |
| 5593 | __LJMP static int hlua_txn_log_warning(lua_State *L) |
| 5594 | { |
| 5595 | const char *msg; |
| 5596 | struct hlua_txn *htxn; |
| 5597 | |
| 5598 | MAY_LJMP(check_args(L, 2, "Warning")); |
| 5599 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5600 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5601 | hlua_sendlog(htxn->s->be, LOG_WARNING, msg); |
| 5602 | return 0; |
| 5603 | } |
| 5604 | |
| 5605 | __LJMP static int hlua_txn_log_alert(lua_State *L) |
| 5606 | { |
| 5607 | const char *msg; |
| 5608 | struct hlua_txn *htxn; |
| 5609 | |
| 5610 | MAY_LJMP(check_args(L, 2, "Alert")); |
| 5611 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5612 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5613 | hlua_sendlog(htxn->s->be, LOG_ALERT, msg); |
| 5614 | return 0; |
| 5615 | } |
| 5616 | |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5617 | __LJMP static int hlua_txn_set_loglevel(lua_State *L) |
| 5618 | { |
| 5619 | struct hlua_txn *htxn; |
| 5620 | int ll; |
| 5621 | |
| 5622 | MAY_LJMP(check_args(L, 2, "set_loglevel")); |
| 5623 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5624 | ll = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5625 | |
| 5626 | if (ll < 0 || ll > 7) |
| 5627 | WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7")); |
| 5628 | |
| 5629 | htxn->s->logs.level = ll; |
| 5630 | return 0; |
| 5631 | } |
| 5632 | |
| 5633 | __LJMP static int hlua_txn_set_tos(lua_State *L) |
| 5634 | { |
| 5635 | struct hlua_txn *htxn; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5636 | int tos; |
| 5637 | |
| 5638 | MAY_LJMP(check_args(L, 2, "set_tos")); |
| 5639 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5640 | tos = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5641 | |
Willy Tarreau | 1a18b54 | 2018-12-11 16:37:42 +0100 | [diff] [blame] | 5642 | conn_set_tos(objt_conn(htxn->s->sess->origin), tos); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5643 | return 0; |
| 5644 | } |
| 5645 | |
| 5646 | __LJMP static int hlua_txn_set_mark(lua_State *L) |
| 5647 | { |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5648 | struct hlua_txn *htxn; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5649 | int mark; |
| 5650 | |
| 5651 | MAY_LJMP(check_args(L, 2, "set_mark")); |
| 5652 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5653 | mark = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5654 | |
Lukas Tribus | 579e3e3 | 2019-08-11 18:03:45 +0200 | [diff] [blame] | 5655 | conn_set_mark(objt_conn(htxn->s->sess->origin), mark); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5656 | return 0; |
| 5657 | } |
| 5658 | |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 5659 | __LJMP static int hlua_txn_set_priority_class(lua_State *L) |
| 5660 | { |
| 5661 | struct hlua_txn *htxn; |
| 5662 | |
| 5663 | MAY_LJMP(check_args(L, 2, "set_priority_class")); |
| 5664 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5665 | htxn->s->priority_class = queue_limit_class(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 5666 | return 0; |
| 5667 | } |
| 5668 | |
| 5669 | __LJMP static int hlua_txn_set_priority_offset(lua_State *L) |
| 5670 | { |
| 5671 | struct hlua_txn *htxn; |
| 5672 | |
| 5673 | MAY_LJMP(check_args(L, 2, "set_priority_offset")); |
| 5674 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5675 | htxn->s->priority_offset = queue_limit_offset(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 5676 | return 0; |
| 5677 | } |
| 5678 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5679 | /* 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] | 5680 | * 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] | 5681 | * message and terminate the transaction. It returns 1 on success and 0 on |
| 5682 | * error. The Reply must be on top of the stack. |
| 5683 | */ |
| 5684 | __LJMP static int hlua_txn_forward_reply(lua_State *L, struct stream *s) |
| 5685 | { |
| 5686 | struct htx *htx; |
| 5687 | struct htx_sl *sl; |
| 5688 | struct h1m h1m; |
| 5689 | const char *status, *reason, *body; |
| 5690 | size_t status_len, reason_len, body_len; |
| 5691 | int ret, code, flags; |
| 5692 | |
| 5693 | code = 200; |
| 5694 | status = "200"; |
| 5695 | status_len = 3; |
| 5696 | ret = lua_getfield(L, -1, "status"); |
| 5697 | if (ret == LUA_TNUMBER) { |
| 5698 | code = lua_tointeger(L, -1); |
| 5699 | status = lua_tolstring(L, -1, &status_len); |
| 5700 | } |
| 5701 | lua_pop(L, 1); |
| 5702 | |
| 5703 | reason = http_get_reason(code); |
| 5704 | reason_len = strlen(reason); |
| 5705 | ret = lua_getfield(L, -1, "reason"); |
| 5706 | if (ret == LUA_TSTRING) |
| 5707 | reason = lua_tolstring(L, -1, &reason_len); |
| 5708 | lua_pop(L, 1); |
| 5709 | |
| 5710 | body = NULL; |
| 5711 | body_len = 0; |
| 5712 | ret = lua_getfield(L, -1, "body"); |
| 5713 | if (ret == LUA_TSTRING) |
| 5714 | body = lua_tolstring(L, -1, &body_len); |
| 5715 | lua_pop(L, 1); |
| 5716 | |
| 5717 | /* Prepare the response before inserting the headers */ |
| 5718 | h1m_init_res(&h1m); |
| 5719 | htx = htx_from_buf(&s->res.buf); |
| 5720 | channel_htx_truncate(&s->res, htx); |
| 5721 | if (s->txn->req.flags & HTTP_MSGF_VER_11) { |
| 5722 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11); |
| 5723 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), |
| 5724 | ist2(status, status_len), ist2(reason, reason_len)); |
| 5725 | } |
| 5726 | else { |
| 5727 | flags = HTX_SL_F_IS_RESP; |
| 5728 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), |
| 5729 | ist2(status, status_len), ist2(reason, reason_len)); |
| 5730 | } |
| 5731 | if (!sl) |
| 5732 | goto fail; |
| 5733 | sl->info.res.status = code; |
| 5734 | |
| 5735 | /* Push in the stack the "headers" entry. */ |
| 5736 | ret = lua_getfield(L, -1, "headers"); |
| 5737 | if (ret != LUA_TTABLE) |
| 5738 | goto skip_headers; |
| 5739 | |
| 5740 | lua_pushnil(L); |
| 5741 | while (lua_next(L, -2) != 0) { |
| 5742 | struct ist name, value; |
| 5743 | const char *n, *v; |
| 5744 | size_t nlen, vlen; |
| 5745 | |
| 5746 | if (!lua_isstring(L, -2) || !lua_istable(L, -1)) { |
| 5747 | /* Skip element if the key is not a string or if the value is not a table */ |
| 5748 | goto next_hdr; |
| 5749 | } |
| 5750 | |
| 5751 | n = lua_tolstring(L, -2, &nlen); |
| 5752 | name = ist2(n, nlen); |
| 5753 | if (isteqi(name, ist("content-length"))) { |
| 5754 | /* Always skip content-length header. It will be added |
| 5755 | * later with the correct len |
| 5756 | */ |
| 5757 | goto next_hdr; |
| 5758 | } |
| 5759 | |
| 5760 | /* Loop on header's values */ |
| 5761 | lua_pushnil(L); |
| 5762 | while (lua_next(L, -2)) { |
| 5763 | if (!lua_isstring(L, -1)) { |
| 5764 | /* Skip the value if it is not a string */ |
| 5765 | goto next_value; |
| 5766 | } |
| 5767 | |
| 5768 | v = lua_tolstring(L, -1, &vlen); |
| 5769 | value = ist2(v, vlen); |
| 5770 | |
| 5771 | if (isteqi(name, ist("transfer-encoding"))) |
| 5772 | h1_parse_xfer_enc_header(&h1m, value); |
| 5773 | if (!htx_add_header(htx, ist2(n, nlen), ist2(v, vlen))) |
| 5774 | goto fail; |
| 5775 | |
| 5776 | next_value: |
| 5777 | lua_pop(L, 1); |
| 5778 | } |
| 5779 | |
| 5780 | next_hdr: |
| 5781 | lua_pop(L, 1); |
| 5782 | } |
| 5783 | skip_headers: |
| 5784 | lua_pop(L, 1); |
| 5785 | |
| 5786 | /* Update h1m flags: CLEN is set if CHNK is not present */ |
| 5787 | if (!(h1m.flags & H1_MF_CHNK)) { |
| 5788 | const char *clen = ultoa(body_len); |
| 5789 | |
| 5790 | h1m.flags |= H1_MF_CLEN; |
| 5791 | if (!htx_add_header(htx, ist("content-length"), ist(clen))) |
| 5792 | goto fail; |
| 5793 | } |
| 5794 | if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK)) |
| 5795 | h1m.flags |= H1_MF_XFER_LEN; |
| 5796 | |
| 5797 | /* Update HTX start-line flags */ |
| 5798 | if (h1m.flags & H1_MF_XFER_ENC) |
| 5799 | flags |= HTX_SL_F_XFER_ENC; |
| 5800 | if (h1m.flags & H1_MF_XFER_LEN) { |
| 5801 | flags |= HTX_SL_F_XFER_LEN; |
| 5802 | if (h1m.flags & H1_MF_CHNK) |
| 5803 | flags |= HTX_SL_F_CHNK; |
| 5804 | else if (h1m.flags & H1_MF_CLEN) |
| 5805 | flags |= HTX_SL_F_CLEN; |
| 5806 | if (h1m.body_len == 0) |
| 5807 | flags |= HTX_SL_F_BODYLESS; |
| 5808 | } |
| 5809 | sl->flags |= flags; |
| 5810 | |
| 5811 | |
| 5812 | if (!htx_add_endof(htx, HTX_BLK_EOH) || |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 5813 | (body_len && !htx_add_data_atonce(htx, ist2(body, body_len)))) |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5814 | goto fail; |
| 5815 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 5816 | htx->flags |= HTX_FL_EOM; |
| 5817 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5818 | /* Now, forward the response and terminate the transaction */ |
| 5819 | s->txn->status = code; |
| 5820 | htx_to_buf(htx, &s->res.buf); |
| 5821 | if (!http_forward_proxy_resp(s, 1)) |
| 5822 | goto fail; |
| 5823 | |
| 5824 | return 1; |
| 5825 | |
| 5826 | fail: |
| 5827 | channel_htx_truncate(&s->res, htx); |
| 5828 | return 0; |
| 5829 | } |
| 5830 | |
| 5831 | /* Terminate a transaction if called from a lua action. For TCP streams, |
| 5832 | * processing is just aborted. Nothing is returned to the client and all |
| 5833 | * arguments are ignored. For HTTP streams, if a reply is passed as argument, it |
| 5834 | * is forwarded to the client before terminating the transaction. On success, |
| 5835 | * the function exits with ACT_RET_DONE code. If an error occurred, it exits |
| 5836 | * with ACT_RET_ERR code. If this function is not called from a lua action, it |
| 5837 | * just exits without any processing. |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5838 | */ |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 5839 | __LJMP static int hlua_txn_done(lua_State *L) |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5840 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 5841 | struct hlua_txn *htxn; |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5842 | struct stream *s; |
| 5843 | int finst; |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5844 | |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 5845 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5846 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5847 | /* If the flags NOTERM is set, we cannot terminate the session, so we |
| 5848 | * just end the execution of the current lua code. */ |
| 5849 | if (htxn->flags & HLUA_TXN_NOTERM) |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5850 | WILL_LJMP(hlua_done(L)); |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5851 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5852 | s = htxn->s; |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5853 | if (!IS_HTX_STRM(htxn->s)) { |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5854 | struct channel *req = &s->req; |
| 5855 | struct channel *res = &s->res; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 5856 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5857 | channel_auto_read(req); |
| 5858 | channel_abort(req); |
| 5859 | channel_auto_close(req); |
| 5860 | channel_erase(req); |
| 5861 | |
| 5862 | res->wex = tick_add_ifset(now_ms, res->wto); |
| 5863 | channel_auto_read(res); |
| 5864 | channel_auto_close(res); |
| 5865 | channel_shutr_now(res); |
| 5866 | |
| 5867 | finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_D); |
| 5868 | goto done; |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 5869 | } |
Thierry FOURNIER | 10ec214 | 2015-08-24 17:23:45 +0200 | [diff] [blame] | 5870 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5871 | if (lua_gettop(L) == 1 || !lua_istable(L, 2)) { |
| 5872 | /* No reply or invalid reply */ |
| 5873 | s->txn->status = 0; |
| 5874 | http_reply_and_close(s, 0, NULL); |
| 5875 | } |
| 5876 | else { |
| 5877 | /* Remove extra args to have the reply on top of the stack */ |
| 5878 | if (lua_gettop(L) > 2) |
| 5879 | lua_pop(L, lua_gettop(L) - 2); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5880 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5881 | if (!hlua_txn_forward_reply(L, s)) { |
| 5882 | if (!(s->flags & SF_ERR_MASK)) |
| 5883 | s->flags |= SF_ERR_PRXCOND; |
| 5884 | lua_pushinteger(L, ACT_RET_ERR); |
| 5885 | WILL_LJMP(hlua_done(L)); |
| 5886 | return 0; /* Never reached */ |
| 5887 | } |
Christopher Faulet | 4d0e263 | 2019-07-16 10:52:40 +0200 | [diff] [blame] | 5888 | } |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 5889 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5890 | finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_H); |
| 5891 | if (htxn->dir == SMP_OPT_DIR_REQ) { |
| 5892 | /* let's log the request time */ |
| 5893 | s->logs.tv_request = now; |
| 5894 | 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] | 5895 | _HA_ATOMIC_INC(&s->sess->fe->fe_counters.intercepted_req); |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5896 | } |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 5897 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5898 | done: |
| 5899 | if (!(s->flags & SF_ERR_MASK)) |
| 5900 | s->flags |= SF_ERR_LOCAL; |
| 5901 | if (!(s->flags & SF_FINST_MASK)) |
| 5902 | s->flags |= finst; |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 5903 | |
Christopher Faulet | 4ad7310 | 2020-03-05 11:07:31 +0100 | [diff] [blame] | 5904 | lua_pushinteger(L, ACT_RET_ABRT); |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 5905 | WILL_LJMP(hlua_done(L)); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 5906 | return 0; |
| 5907 | } |
| 5908 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5909 | /* |
| 5910 | * |
| 5911 | * |
| 5912 | * Class REPLY |
| 5913 | * |
| 5914 | * |
| 5915 | */ |
| 5916 | |
| 5917 | /* Pushes the TXN reply onto the top of the stack. If the stask does not have a |
| 5918 | * free slots, the function fails and returns 0; |
| 5919 | */ |
| 5920 | static int hlua_txn_reply_new(lua_State *L) |
| 5921 | { |
| 5922 | struct hlua_txn *htxn; |
| 5923 | const char *reason, *body = NULL; |
| 5924 | int ret, status; |
| 5925 | |
| 5926 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5927 | if (!IS_HTX_STRM(htxn->s)) { |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5928 | hlua_pusherror(L, "txn object is not an HTTP transaction."); |
| 5929 | WILL_LJMP(lua_error(L)); |
| 5930 | } |
| 5931 | |
| 5932 | /* Default value */ |
| 5933 | status = 200; |
| 5934 | reason = http_get_reason(status); |
| 5935 | |
| 5936 | if (lua_istable(L, 2)) { |
| 5937 | /* load status and reason from the table argument at index 2 */ |
| 5938 | ret = lua_getfield(L, 2, "status"); |
| 5939 | if (ret == LUA_TNIL) |
| 5940 | goto reason; |
| 5941 | else if (ret != LUA_TNUMBER) { |
| 5942 | /* invalid status: ignore the reason */ |
| 5943 | goto body; |
| 5944 | } |
| 5945 | status = lua_tointeger(L, -1); |
| 5946 | |
| 5947 | reason: |
| 5948 | lua_pop(L, 1); /* restore the stack: remove status */ |
| 5949 | ret = lua_getfield(L, 2, "reason"); |
| 5950 | if (ret == LUA_TSTRING) |
| 5951 | reason = lua_tostring(L, -1); |
| 5952 | |
| 5953 | body: |
| 5954 | lua_pop(L, 1); /* restore the stack: remove invalid status or reason */ |
| 5955 | ret = lua_getfield(L, 2, "body"); |
| 5956 | if (ret == LUA_TSTRING) |
| 5957 | body = lua_tostring(L, -1); |
| 5958 | lua_pop(L, 1); /* restore the stack: remove body */ |
| 5959 | } |
| 5960 | |
| 5961 | /* Create the Reply table */ |
| 5962 | lua_newtable(L); |
| 5963 | |
| 5964 | /* Add status element */ |
| 5965 | lua_pushstring(L, "status"); |
| 5966 | lua_pushinteger(L, status); |
| 5967 | lua_settable(L, -3); |
| 5968 | |
| 5969 | /* Add reason element */ |
| 5970 | reason = http_get_reason(status); |
| 5971 | lua_pushstring(L, "reason"); |
| 5972 | lua_pushstring(L, reason); |
| 5973 | lua_settable(L, -3); |
| 5974 | |
| 5975 | /* Add body element, nil if undefined */ |
| 5976 | lua_pushstring(L, "body"); |
| 5977 | if (body) |
| 5978 | lua_pushstring(L, body); |
| 5979 | else |
| 5980 | lua_pushnil(L); |
| 5981 | lua_settable(L, -3); |
| 5982 | |
| 5983 | /* Add headers element */ |
| 5984 | lua_pushstring(L, "headers"); |
| 5985 | lua_newtable(L); |
| 5986 | |
| 5987 | /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */ |
| 5988 | if (lua_istable(L, 2)) { |
| 5989 | /* load headers from the table argument at index 2. If it is a table, copy it. */ |
| 5990 | ret = lua_getfield(L, 2, "headers"); |
| 5991 | if (ret == LUA_TTABLE) { |
| 5992 | /* stack: [ ... <headers:table>, <table> ] */ |
| 5993 | lua_pushnil(L); |
| 5994 | while (lua_next(L, -2) != 0) { |
| 5995 | /* stack: [ ... <headers:table>, <table>, k, v] */ |
| 5996 | if (!lua_isstring(L, -1) && !lua_istable(L, -1)) { |
| 5997 | /* invalid value type, skip it */ |
| 5998 | lua_pop(L, 1); |
| 5999 | continue; |
| 6000 | } |
| 6001 | |
| 6002 | |
| 6003 | /* Duplicate the key and swap it with the value. */ |
| 6004 | lua_pushvalue(L, -2); |
| 6005 | lua_insert(L, -2); |
| 6006 | /* stack: [ ... <headers:table>, <table>, k, k, v ] */ |
| 6007 | |
| 6008 | lua_newtable(L); |
| 6009 | lua_insert(L, -2); |
| 6010 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, v ] */ |
| 6011 | |
| 6012 | if (lua_isstring(L, -1)) { |
| 6013 | /* push the value in the inner table */ |
| 6014 | lua_rawseti(L, -2, 1); |
| 6015 | } |
| 6016 | else { /* table */ |
| 6017 | lua_pushnil(L); |
| 6018 | while (lua_next(L, -2) != 0) { |
| 6019 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2, v2 ] */ |
| 6020 | if (!lua_isstring(L, -1)) { |
| 6021 | /* invalid value type, skip it*/ |
| 6022 | lua_pop(L, 1); |
| 6023 | continue; |
| 6024 | } |
| 6025 | /* push the value in the inner table */ |
| 6026 | lua_rawseti(L, -4, lua_rawlen(L, -4) + 1); |
| 6027 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2 ] */ |
| 6028 | } |
| 6029 | lua_pop(L, 1); |
| 6030 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table> ] */ |
| 6031 | } |
| 6032 | |
| 6033 | /* push (k,v) on the stack in the headers table: |
| 6034 | * stack: [ ... <headers:table>, <table>, k, k, v ] |
| 6035 | */ |
| 6036 | lua_settable(L, -5); |
| 6037 | /* stack: [ ... <headers:table>, <table>, k ] */ |
| 6038 | } |
| 6039 | } |
| 6040 | lua_pop(L, 1); |
| 6041 | } |
| 6042 | /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */ |
| 6043 | lua_settable(L, -3); |
| 6044 | /* stack: [ txn, <Arg:table>, <Reply:table> ] */ |
| 6045 | |
| 6046 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 6047 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_reply_ref); |
| 6048 | lua_setmetatable(L, -2); |
| 6049 | return 1; |
| 6050 | } |
| 6051 | |
| 6052 | /* Set the reply status code, and optionally the reason. If no reason is |
| 6053 | * provided, the default one corresponding to the status code is used. |
| 6054 | */ |
| 6055 | __LJMP static int hlua_txn_reply_set_status(lua_State *L) |
| 6056 | { |
| 6057 | int status = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 6058 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
| 6059 | |
| 6060 | /* First argument (self) must be a table */ |
| 6061 | luaL_checktype(L, 1, LUA_TTABLE); |
| 6062 | |
| 6063 | if (status < 100 || status > 599) { |
| 6064 | lua_pushboolean(L, 0); |
| 6065 | return 1; |
| 6066 | } |
| 6067 | if (!reason) |
| 6068 | reason = http_get_reason(status); |
| 6069 | |
| 6070 | lua_pushinteger(L, status); |
| 6071 | lua_setfield(L, 1, "status"); |
| 6072 | |
| 6073 | lua_pushstring(L, reason); |
| 6074 | lua_setfield(L, 1, "reason"); |
| 6075 | |
| 6076 | lua_pushboolean(L, 1); |
| 6077 | return 1; |
| 6078 | } |
| 6079 | |
| 6080 | /* Add a header into the reply object. Each header name is associated to an |
| 6081 | * array of values in the "headers" table. If the header name is not found, a |
| 6082 | * new entry is created. |
| 6083 | */ |
| 6084 | __LJMP static int hlua_txn_reply_add_header(lua_State *L) |
| 6085 | { |
| 6086 | const char *name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 6087 | const char *value = MAY_LJMP(luaL_checkstring(L, 3)); |
| 6088 | int ret; |
| 6089 | |
| 6090 | /* First argument (self) must be a table */ |
| 6091 | luaL_checktype(L, 1, LUA_TTABLE); |
| 6092 | |
| 6093 | /* Push in the stack the "headers" entry. */ |
| 6094 | ret = lua_getfield(L, 1, "headers"); |
| 6095 | if (ret != LUA_TTABLE) { |
| 6096 | hlua_pusherror(L, "Reply['headers'] is expected to a an array. %s found", lua_typename(L, ret)); |
| 6097 | WILL_LJMP(lua_error(L)); |
| 6098 | } |
| 6099 | |
| 6100 | /* check if the header is already registered. If not, register it. */ |
| 6101 | ret = lua_getfield(L, -1, name); |
| 6102 | if (ret == LUA_TNIL) { |
| 6103 | /* Entry not found. */ |
| 6104 | lua_pop(L, 1); /* remove the nil. The "headers" table is the top of the stack. */ |
| 6105 | |
| 6106 | /* Insert the new header name in the array in the top of the stack. |
| 6107 | * It left the new array in the top of the stack. |
| 6108 | */ |
| 6109 | lua_newtable(L); |
| 6110 | lua_pushstring(L, name); |
| 6111 | lua_pushvalue(L, -2); |
| 6112 | lua_settable(L, -4); |
| 6113 | } |
| 6114 | else if (ret != LUA_TTABLE) { |
| 6115 | hlua_pusherror(L, "Reply['headers']['%s'] is expected to be an array. %s found", name, lua_typename(L, ret)); |
| 6116 | WILL_LJMP(lua_error(L)); |
| 6117 | } |
| 6118 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 6119 | /* Now the top of thestack is an array of values. We push |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 6120 | * the header value as new entry. |
| 6121 | */ |
| 6122 | lua_pushstring(L, value); |
| 6123 | ret = lua_rawlen(L, -2); |
| 6124 | lua_rawseti(L, -2, ret + 1); |
| 6125 | |
| 6126 | lua_pushboolean(L, 1); |
| 6127 | return 1; |
| 6128 | } |
| 6129 | |
| 6130 | /* Remove all occurrences of a given header name. */ |
| 6131 | __LJMP static int hlua_txn_reply_del_header(lua_State *L) |
| 6132 | { |
| 6133 | const char *name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 6134 | int ret; |
| 6135 | |
| 6136 | /* First argument (self) must be a table */ |
| 6137 | luaL_checktype(L, 1, LUA_TTABLE); |
| 6138 | |
| 6139 | /* Push in the stack the "headers" entry. */ |
| 6140 | ret = lua_getfield(L, 1, "headers"); |
| 6141 | if (ret != LUA_TTABLE) { |
| 6142 | hlua_pusherror(L, "Reply['headers'] is expected to be an array. %s found", lua_typename(L, ret)); |
| 6143 | WILL_LJMP(lua_error(L)); |
| 6144 | } |
| 6145 | |
| 6146 | lua_pushstring(L, name); |
| 6147 | lua_pushnil(L); |
| 6148 | lua_settable(L, -3); |
| 6149 | |
| 6150 | lua_pushboolean(L, 1); |
| 6151 | return 1; |
| 6152 | } |
| 6153 | |
| 6154 | /* Set the reply's body. Overwrite any existing entry. */ |
| 6155 | __LJMP static int hlua_txn_reply_set_body(lua_State *L) |
| 6156 | { |
| 6157 | const char *payload = MAY_LJMP(luaL_checkstring(L, 2)); |
| 6158 | |
| 6159 | /* First argument (self) must be a table */ |
| 6160 | luaL_checktype(L, 1, LUA_TTABLE); |
| 6161 | |
| 6162 | lua_pushstring(L, payload); |
| 6163 | lua_setfield(L, 1, "body"); |
| 6164 | |
| 6165 | lua_pushboolean(L, 1); |
| 6166 | return 1; |
| 6167 | } |
| 6168 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 6169 | __LJMP static int hlua_log(lua_State *L) |
| 6170 | { |
| 6171 | int level; |
| 6172 | const char *msg; |
| 6173 | |
| 6174 | MAY_LJMP(check_args(L, 2, "log")); |
| 6175 | level = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 6176 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 6177 | |
| 6178 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 6179 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 6180 | |
| 6181 | hlua_sendlog(NULL, level, msg); |
| 6182 | return 0; |
| 6183 | } |
| 6184 | |
| 6185 | __LJMP static int hlua_log_debug(lua_State *L) |
| 6186 | { |
| 6187 | const char *msg; |
| 6188 | |
| 6189 | MAY_LJMP(check_args(L, 1, "debug")); |
| 6190 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6191 | hlua_sendlog(NULL, LOG_DEBUG, msg); |
| 6192 | return 0; |
| 6193 | } |
| 6194 | |
| 6195 | __LJMP static int hlua_log_info(lua_State *L) |
| 6196 | { |
| 6197 | const char *msg; |
| 6198 | |
| 6199 | MAY_LJMP(check_args(L, 1, "info")); |
| 6200 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6201 | hlua_sendlog(NULL, LOG_INFO, msg); |
| 6202 | return 0; |
| 6203 | } |
| 6204 | |
| 6205 | __LJMP static int hlua_log_warning(lua_State *L) |
| 6206 | { |
| 6207 | const char *msg; |
| 6208 | |
| 6209 | MAY_LJMP(check_args(L, 1, "warning")); |
| 6210 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6211 | hlua_sendlog(NULL, LOG_WARNING, msg); |
| 6212 | return 0; |
| 6213 | } |
| 6214 | |
| 6215 | __LJMP static int hlua_log_alert(lua_State *L) |
| 6216 | { |
| 6217 | const char *msg; |
| 6218 | |
| 6219 | MAY_LJMP(check_args(L, 1, "alert")); |
| 6220 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6221 | hlua_sendlog(NULL, LOG_ALERT, msg); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 6222 | return 0; |
| 6223 | } |
| 6224 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 6225 | __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] | 6226 | { |
| 6227 | int wakeup_ms = lua_tointeger(L, -1); |
Willy Tarreau | 9cc2e4c | 2021-09-30 16:12:31 +0200 | [diff] [blame] | 6228 | if (!tick_is_expired(wakeup_ms, now_ms)) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 6229 | 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] | 6230 | return 0; |
| 6231 | } |
| 6232 | |
| 6233 | __LJMP static int hlua_sleep(lua_State *L) |
| 6234 | { |
| 6235 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6236 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6237 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6238 | MAY_LJMP(check_args(L, 1, "sleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6239 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 6240 | delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6241 | wakeup_ms = tick_add(now_ms, delay); |
| 6242 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6243 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 6244 | 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] | 6245 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6246 | } |
| 6247 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6248 | __LJMP static int hlua_msleep(lua_State *L) |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6249 | { |
| 6250 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6251 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6252 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6253 | MAY_LJMP(check_args(L, 1, "msleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6254 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 6255 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6256 | wakeup_ms = tick_add(now_ms, delay); |
| 6257 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6258 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 6259 | 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] | 6260 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6261 | } |
| 6262 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 6263 | /* This functionis an LUA binding. it permits to give back |
| 6264 | * the hand at the HAProxy scheduler. It is used when the |
| 6265 | * LUA processing consumes a lot of time. |
| 6266 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 6267 | __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] | 6268 | { |
| 6269 | return 0; |
| 6270 | } |
| 6271 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 6272 | __LJMP static int hlua_yield(lua_State *L) |
| 6273 | { |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 6274 | 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] | 6275 | return 0; |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 6276 | } |
| 6277 | |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6278 | /* This function change the nice of the currently executed |
| 6279 | * task. It is used set low or high priority at the current |
| 6280 | * task. |
| 6281 | */ |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 6282 | __LJMP static int hlua_set_nice(lua_State *L) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6283 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6284 | struct hlua *hlua; |
| 6285 | int nice; |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6286 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6287 | MAY_LJMP(check_args(L, 1, "set_nice")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6288 | nice = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6289 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 6290 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 6291 | hlua = hlua_gethlua(L); |
| 6292 | |
| 6293 | /* If the task is not set, I'm in a start mode. */ |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6294 | if (!hlua || !hlua->task) |
| 6295 | return 0; |
| 6296 | |
| 6297 | if (nice < -1024) |
| 6298 | nice = -1024; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6299 | else if (nice > 1024) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6300 | nice = 1024; |
| 6301 | |
| 6302 | hlua->task->nice = nice; |
| 6303 | return 0; |
| 6304 | } |
| 6305 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6306 | /* 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] | 6307 | * HAProxy task subsystem when the task is awaked. The LUA runtime can |
| 6308 | * return an E_AGAIN signal, the emmiter of this signal must set a |
| 6309 | * signal to wake the task. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6310 | * |
| 6311 | * Task wrapper are longjmp safe because the only one Lua code |
| 6312 | * executed is the safe hlua_ctx_resume(); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6313 | */ |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 6314 | 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] | 6315 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 6316 | struct hlua *hlua = context; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6317 | enum hlua_exec status; |
| 6318 | |
Christopher Faulet | 5bc9972 | 2018-04-25 10:34:45 +0200 | [diff] [blame] | 6319 | if (task->thread_mask == MAX_THREADS_MASK) |
| 6320 | task_set_affinity(task, tid_bit); |
| 6321 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6322 | /* If it is the first call to the task, we must initialize the |
| 6323 | * execution timeouts. |
| 6324 | */ |
| 6325 | if (!HLUA_IS_RUNNING(hlua)) |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 6326 | hlua->max_time = hlua_timeout_task; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6327 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6328 | /* Execute the Lua code. */ |
| 6329 | status = hlua_ctx_resume(hlua, 1); |
| 6330 | |
| 6331 | switch (status) { |
| 6332 | /* finished or yield */ |
| 6333 | case HLUA_E_OK: |
| 6334 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 6335 | task_destroy(task); |
Tim Duesterhus | cd235c6 | 2018-04-24 13:56:01 +0200 | [diff] [blame] | 6336 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6337 | break; |
| 6338 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6339 | case HLUA_E_AGAIN: /* co process or timeout wake me later. */ |
Thierry FOURNIER | cb14688 | 2017-12-10 17:10:57 +0100 | [diff] [blame] | 6340 | notification_gc(&hlua->com); |
PiBa-NL | fe971b3 | 2018-05-02 22:27:14 +0200 | [diff] [blame] | 6341 | task->expire = hlua->wake_time; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6342 | break; |
| 6343 | |
| 6344 | /* finished with error. */ |
| 6345 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6346 | SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1)); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6347 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 6348 | task_destroy(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 6349 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6350 | break; |
| 6351 | |
| 6352 | case HLUA_E_ERR: |
| 6353 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6354 | SEND_ERR(NULL, "Lua task: unknown error.\n"); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6355 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 6356 | task_destroy(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 6357 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6358 | break; |
| 6359 | } |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 6360 | return task; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6361 | } |
| 6362 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6363 | /* This function is an LUA binding that register LUA function to be |
| 6364 | * executed after the HAProxy configuration parsing and before the |
| 6365 | * HAProxy scheduler starts. This function expect only one LUA |
| 6366 | * argument that is a function. This function returns nothing, but |
| 6367 | * throws if an error is encountered. |
| 6368 | */ |
| 6369 | __LJMP static int hlua_register_init(lua_State *L) |
| 6370 | { |
| 6371 | struct hlua_init_function *init; |
| 6372 | int ref; |
| 6373 | |
| 6374 | MAY_LJMP(check_args(L, 1, "register_init")); |
| 6375 | |
| 6376 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 6377 | |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6378 | init = calloc(1, sizeof(*init)); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6379 | if (!init) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6380 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6381 | |
| 6382 | init->function_ref = ref; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 6383 | LIST_APPEND(&hlua_init_functions[hlua_state_id], &init->l); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6384 | return 0; |
| 6385 | } |
| 6386 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6387 | /* This functio is an LUA binding. It permits to register a task |
| 6388 | * executed in parallel of the main HAroxy activity. The task is |
| 6389 | * created and it is set in the HAProxy scheduler. It can be called |
| 6390 | * from the "init" section, "post init" or during the runtime. |
| 6391 | * |
| 6392 | * Lua prototype: |
| 6393 | * |
| 6394 | * <none> core.register_task(<function>) |
| 6395 | */ |
| 6396 | static int hlua_register_task(lua_State *L) |
| 6397 | { |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 6398 | struct hlua *hlua = NULL; |
| 6399 | struct task *task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6400 | int ref; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 6401 | int state_id; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6402 | |
| 6403 | MAY_LJMP(check_args(L, 1, "register_task")); |
| 6404 | |
| 6405 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 6406 | |
Thierry Fournier | 75fc029 | 2020-11-28 13:18:56 +0100 | [diff] [blame] | 6407 | /* Get the reference state. If the reference is NULL, L is the master |
| 6408 | * state, otherwise hlua->T is. |
| 6409 | */ |
| 6410 | hlua = hlua_gethlua(L); |
| 6411 | if (hlua) |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 6412 | /* we are in runtime processing */ |
| 6413 | state_id = hlua->state_id; |
Thierry Fournier | 75fc029 | 2020-11-28 13:18:56 +0100 | [diff] [blame] | 6414 | else |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 6415 | /* we are in initialization mode */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 6416 | state_id = hlua_state_id; |
Thierry Fournier | 75fc029 | 2020-11-28 13:18:56 +0100 | [diff] [blame] | 6417 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6418 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6419 | if (!hlua) |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 6420 | goto alloc_error; |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 6421 | HLUA_INIT(hlua); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6422 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 6423 | /* We are in the common lua state, execute the task anywhere, |
| 6424 | * otherwise, inherit the current thread identifier |
| 6425 | */ |
| 6426 | if (state_id == 0) |
| 6427 | task = task_new(MAX_THREADS_MASK); |
| 6428 | else |
| 6429 | task = task_new(tid_bit); |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 6430 | if (!task) |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 6431 | goto alloc_error; |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 6432 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6433 | task->context = hlua; |
| 6434 | task->process = hlua_process_task; |
| 6435 | |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 6436 | if (!hlua_ctx_init(hlua, state_id, task, 1)) |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 6437 | goto alloc_error; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6438 | |
| 6439 | /* Restore the function in the stack. */ |
| 6440 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref); |
| 6441 | hlua->nargs = 0; |
| 6442 | |
| 6443 | /* Schedule task. */ |
Willy Tarreau | 790810f | 2021-09-30 16:17:37 +0200 | [diff] [blame] | 6444 | task_wakeup(task, TASK_WOKEN_INIT); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6445 | |
| 6446 | return 0; |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 6447 | |
| 6448 | alloc_error: |
| 6449 | task_destroy(task); |
| 6450 | hlua_ctx_destroy(hlua); |
| 6451 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 6452 | return 0; /* Never reached */ |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6453 | } |
| 6454 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6455 | /* Wrapper called by HAProxy to execute an LUA converter. This wrapper |
| 6456 | * doesn't allow "yield" functions because the HAProxy engine cannot |
| 6457 | * resume converters. |
| 6458 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6459 | 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] | 6460 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 6461 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6462 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6463 | const char *error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6464 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6465 | if (!stream) |
| 6466 | return 0; |
| 6467 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6468 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6469 | * Lua context can be not initialized. This behavior |
| 6470 | * permits to save performances because a systematic |
| 6471 | * Lua initialization cause 5% performances loss. |
| 6472 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6473 | if (!stream->hlua) { |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 6474 | struct hlua *hlua; |
| 6475 | |
| 6476 | hlua = pool_alloc(pool_head_hlua); |
| 6477 | if (!hlua) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6478 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 6479 | return 0; |
| 6480 | } |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 6481 | HLUA_INIT(hlua); |
| 6482 | stream->hlua = hlua; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 6483 | 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] | 6484 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 6485 | return 0; |
| 6486 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6487 | } |
| 6488 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6489 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6490 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6491 | |
| 6492 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 6493 | if (!SET_SAFE_LJMP(stream->hlua)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6494 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 6495 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6496 | else |
| 6497 | error = "critical error"; |
| 6498 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6499 | return 0; |
| 6500 | } |
| 6501 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6502 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6503 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6504 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 6505 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6506 | return 0; |
| 6507 | } |
| 6508 | |
| 6509 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 6510 | 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] | 6511 | |
| 6512 | /* convert input sample and pust-it in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6513 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6514 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 6515 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6516 | return 0; |
| 6517 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6518 | hlua_smp2lua(stream->hlua->T, smp); |
| 6519 | stream->hlua->nargs = 1; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6520 | |
| 6521 | /* push keywords in the stack. */ |
| 6522 | if (arg_p) { |
| 6523 | for (; arg_p->type != ARGT_STOP; arg_p++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6524 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6525 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 6526 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6527 | return 0; |
| 6528 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6529 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 6530 | stream->hlua->nargs++; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6531 | } |
| 6532 | } |
| 6533 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6534 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6535 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6536 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6537 | /* At this point the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 6538 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6539 | } |
| 6540 | |
| 6541 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6542 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6543 | /* finished. */ |
| 6544 | case HLUA_E_OK: |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 6545 | /* If the stack is empty, the function fails. */ |
| 6546 | if (lua_gettop(stream->hlua->T) <= 0) |
| 6547 | return 0; |
| 6548 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6549 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6550 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 6551 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6552 | return 1; |
| 6553 | |
| 6554 | /* yield. */ |
| 6555 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6556 | 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] | 6557 | return 0; |
| 6558 | |
| 6559 | /* finished with error. */ |
| 6560 | case HLUA_E_ERRMSG: |
| 6561 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6562 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6563 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 6564 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6565 | return 0; |
| 6566 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6567 | case HLUA_E_ETMOUT: |
| 6568 | SEND_ERR(stream->be, "Lua converter '%s': execution timeout.\n", fcn->name); |
| 6569 | return 0; |
| 6570 | |
| 6571 | case HLUA_E_NOMEM: |
| 6572 | SEND_ERR(stream->be, "Lua converter '%s': out of memory error.\n", fcn->name); |
| 6573 | return 0; |
| 6574 | |
| 6575 | case HLUA_E_YIELD: |
| 6576 | SEND_ERR(stream->be, "Lua converter '%s': yield functions like core.tcp() or core.sleep() are not allowed.\n", fcn->name); |
| 6577 | return 0; |
| 6578 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6579 | case HLUA_E_ERR: |
| 6580 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6581 | 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] | 6582 | /* fall through */ |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6583 | |
| 6584 | default: |
| 6585 | return 0; |
| 6586 | } |
| 6587 | } |
| 6588 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6589 | /* Wrapper called by HAProxy to execute a sample-fetch. this wrapper |
| 6590 | * doesn't allow "yield" functions because the HAProxy engine cannot |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6591 | * resume sample-fetches. This function will be called by the sample |
| 6592 | * fetch engine to call lua-based fetch operations. |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6593 | */ |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6594 | static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp, |
| 6595 | const char *kw, void *private) |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6596 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 6597 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6598 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6599 | const char *error; |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6600 | unsigned int hflags = HLUA_TXN_NOTERM; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6601 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6602 | if (!stream) |
| 6603 | return 0; |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 6604 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6605 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6606 | * Lua context can be not initialized. This behavior |
| 6607 | * permits to save performances because a systematic |
| 6608 | * Lua initialization cause 5% performances loss. |
| 6609 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6610 | if (!stream->hlua) { |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 6611 | struct hlua *hlua; |
| 6612 | |
| 6613 | hlua = pool_alloc(pool_head_hlua); |
| 6614 | if (!hlua) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6615 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 6616 | return 0; |
| 6617 | } |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 6618 | hlua->T = NULL; |
| 6619 | stream->hlua = hlua; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 6620 | 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] | 6621 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 6622 | return 0; |
| 6623 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6624 | } |
| 6625 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6626 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6627 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6628 | |
| 6629 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 6630 | if (!SET_SAFE_LJMP(stream->hlua)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6631 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 6632 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6633 | else |
| 6634 | error = "critical error"; |
| 6635 | 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] | 6636 | return 0; |
| 6637 | } |
| 6638 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6639 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6640 | if (!lua_checkstack(stream->hlua->T, 2)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6641 | 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] | 6642 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6643 | return 0; |
| 6644 | } |
| 6645 | |
| 6646 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 6647 | 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] | 6648 | |
| 6649 | /* push arguments in the stack. */ |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6650 | 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] | 6651 | 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] | 6652 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6653 | return 0; |
| 6654 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6655 | stream->hlua->nargs = 1; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6656 | |
| 6657 | /* push keywords in the stack. */ |
| 6658 | for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) { |
| 6659 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6660 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6661 | 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] | 6662 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6663 | return 0; |
| 6664 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6665 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 6666 | stream->hlua->nargs++; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6667 | } |
| 6668 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6669 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6670 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6671 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6672 | /* At this point the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 6673 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6674 | } |
| 6675 | |
| 6676 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6677 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6678 | /* finished. */ |
| 6679 | case HLUA_E_OK: |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 6680 | /* If the stack is empty, the function fails. */ |
| 6681 | if (lua_gettop(stream->hlua->T) <= 0) |
| 6682 | return 0; |
| 6683 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6684 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6685 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 6686 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6687 | |
| 6688 | /* Set the end of execution flag. */ |
| 6689 | smp->flags &= ~SMP_F_MAY_CHANGE; |
| 6690 | return 1; |
| 6691 | |
| 6692 | /* yield. */ |
| 6693 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6694 | 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] | 6695 | return 0; |
| 6696 | |
| 6697 | /* finished with error. */ |
| 6698 | case HLUA_E_ERRMSG: |
| 6699 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6700 | SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6701 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 6702 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6703 | return 0; |
| 6704 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6705 | case HLUA_E_ETMOUT: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6706 | SEND_ERR(smp->px, "Lua sample-fetch '%s': execution timeout.\n", fcn->name); |
| 6707 | return 0; |
| 6708 | |
| 6709 | case HLUA_E_NOMEM: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6710 | SEND_ERR(smp->px, "Lua sample-fetch '%s': out of memory error.\n", fcn->name); |
| 6711 | return 0; |
| 6712 | |
| 6713 | case HLUA_E_YIELD: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6714 | SEND_ERR(smp->px, "Lua sample-fetch '%s': yield not allowed.\n", fcn->name); |
| 6715 | return 0; |
| 6716 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6717 | case HLUA_E_ERR: |
| 6718 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6719 | 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] | 6720 | /* fall through */ |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6721 | |
| 6722 | default: |
| 6723 | return 0; |
| 6724 | } |
| 6725 | } |
| 6726 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6727 | /* This function is an LUA binding used for registering |
| 6728 | * "sample-conv" functions. It expects a converter name used |
| 6729 | * in the haproxy configuration file, and an LUA function. |
| 6730 | */ |
| 6731 | __LJMP static int hlua_register_converters(lua_State *L) |
| 6732 | { |
| 6733 | struct sample_conv_kw_list *sck; |
| 6734 | const char *name; |
| 6735 | int ref; |
| 6736 | int len; |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 6737 | struct hlua_function *fcn = NULL; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 6738 | struct sample_conv *sc; |
| 6739 | struct buffer *trash; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6740 | |
| 6741 | MAY_LJMP(check_args(L, 2, "register_converters")); |
| 6742 | |
| 6743 | /* First argument : converter name. */ |
| 6744 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6745 | |
| 6746 | /* Second argument : lua function. */ |
| 6747 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 6748 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 6749 | /* Check if the converter is already registered */ |
| 6750 | trash = get_trash_chunk(); |
| 6751 | chunk_printf(trash, "lua.%s", name); |
| 6752 | sc = find_sample_conv(trash->area, trash->data); |
| 6753 | if (sc != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 6754 | fcn = sc->private; |
| 6755 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 6756 | ha_warning("Trying to register converter 'lua.%s' more than once. " |
| 6757 | "This will become a hard error in version 2.5.\n", name); |
| 6758 | } |
| 6759 | fcn->function_ref[hlua_state_id] = ref; |
| 6760 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 6761 | } |
| 6762 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6763 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6764 | sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6765 | if (!sck) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 6766 | goto alloc_error; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 6767 | fcn = new_hlua_function(); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6768 | if (!fcn) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 6769 | goto alloc_error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6770 | |
| 6771 | /* Fill fcn. */ |
| 6772 | fcn->name = strdup(name); |
| 6773 | if (!fcn->name) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 6774 | goto alloc_error; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 6775 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6776 | |
| 6777 | /* List head */ |
| 6778 | sck->list.n = sck->list.p = NULL; |
| 6779 | |
| 6780 | /* converter keyword. */ |
| 6781 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6782 | sck->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6783 | if (!sck->kw[0].kw) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 6784 | goto alloc_error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6785 | |
| 6786 | snprintf((char *)sck->kw[0].kw, len, "lua.%s", name); |
| 6787 | sck->kw[0].process = hlua_sample_conv_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 6788 | 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] | 6789 | sck->kw[0].val_args = NULL; |
| 6790 | sck->kw[0].in_type = SMP_T_STR; |
| 6791 | sck->kw[0].out_type = SMP_T_STR; |
| 6792 | sck->kw[0].private = fcn; |
| 6793 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6794 | /* Register this new converter */ |
| 6795 | sample_register_convs(sck); |
| 6796 | |
| 6797 | return 0; |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 6798 | |
| 6799 | alloc_error: |
| 6800 | release_hlua_function(fcn); |
| 6801 | ha_free(&sck); |
| 6802 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 6803 | return 0; /* Never reached */ |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6804 | } |
| 6805 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6806 | /* This function is an LUA binding used for registering |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6807 | * "sample-fetch" functions. It expects a converter name used |
| 6808 | * in the haproxy configuration file, and an LUA function. |
| 6809 | */ |
| 6810 | __LJMP static int hlua_register_fetches(lua_State *L) |
| 6811 | { |
| 6812 | const char *name; |
| 6813 | int ref; |
| 6814 | int len; |
| 6815 | struct sample_fetch_kw_list *sfk; |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 6816 | struct hlua_function *fcn = NULL; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 6817 | struct sample_fetch *sf; |
| 6818 | struct buffer *trash; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6819 | |
| 6820 | MAY_LJMP(check_args(L, 2, "register_fetches")); |
| 6821 | |
| 6822 | /* First argument : sample-fetch name. */ |
| 6823 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6824 | |
| 6825 | /* Second argument : lua function. */ |
| 6826 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 6827 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 6828 | /* Check if the sample-fetch is already registered */ |
| 6829 | trash = get_trash_chunk(); |
| 6830 | chunk_printf(trash, "lua.%s", name); |
| 6831 | sf = find_sample_fetch(trash->area, trash->data); |
| 6832 | if (sf != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 6833 | fcn = sf->private; |
| 6834 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 6835 | ha_warning("Trying to register sample-fetch 'lua.%s' more than once. " |
| 6836 | "This will become a hard error in version 2.5.\n", name); |
| 6837 | } |
| 6838 | fcn->function_ref[hlua_state_id] = ref; |
| 6839 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 6840 | } |
| 6841 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6842 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6843 | sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6844 | if (!sfk) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 6845 | goto alloc_error; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 6846 | fcn = new_hlua_function(); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6847 | if (!fcn) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 6848 | goto alloc_error; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6849 | |
| 6850 | /* Fill fcn. */ |
| 6851 | fcn->name = strdup(name); |
| 6852 | if (!fcn->name) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 6853 | goto alloc_error; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 6854 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6855 | |
| 6856 | /* List head */ |
| 6857 | sfk->list.n = sfk->list.p = NULL; |
| 6858 | |
| 6859 | /* sample-fetch keyword. */ |
| 6860 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6861 | sfk->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6862 | if (!sfk->kw[0].kw) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 6863 | goto alloc_error; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6864 | |
| 6865 | snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name); |
| 6866 | sfk->kw[0].process = hlua_sample_fetch_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 6867 | 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] | 6868 | sfk->kw[0].val_args = NULL; |
| 6869 | sfk->kw[0].out_type = SMP_T_STR; |
| 6870 | sfk->kw[0].use = SMP_USE_HTTP_ANY; |
| 6871 | sfk->kw[0].val = 0; |
| 6872 | sfk->kw[0].private = fcn; |
| 6873 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6874 | /* Register this new fetch. */ |
| 6875 | sample_register_fetches(sfk); |
| 6876 | |
| 6877 | return 0; |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 6878 | |
| 6879 | alloc_error: |
| 6880 | release_hlua_function(fcn); |
| 6881 | ha_free(&sfk); |
| 6882 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 6883 | return 0; /* Never reached */ |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6884 | } |
| 6885 | |
Christopher Faulet | 501465d | 2020-02-26 14:54:16 +0100 | [diff] [blame] | 6886 | /* This function is a lua binding to set the wake_time. |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 6887 | */ |
Christopher Faulet | 501465d | 2020-02-26 14:54:16 +0100 | [diff] [blame] | 6888 | __LJMP static int hlua_set_wake_time(lua_State *L) |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 6889 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 6890 | struct hlua *hlua; |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 6891 | unsigned int delay; |
| 6892 | unsigned int wakeup_ms; |
| 6893 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 6894 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 6895 | hlua = hlua_gethlua(L); |
| 6896 | if (!hlua) { |
| 6897 | return 0; |
| 6898 | } |
| 6899 | |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 6900 | MAY_LJMP(check_args(L, 1, "wake_time")); |
| 6901 | |
| 6902 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 6903 | wakeup_ms = tick_add(now_ms, delay); |
| 6904 | hlua->wake_time = wakeup_ms; |
| 6905 | return 0; |
| 6906 | } |
| 6907 | |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6908 | /* This function is a wrapper to execute each LUA function declared as an action |
| 6909 | * wrapper during the initialisation period. This function may return any |
| 6910 | * ACT_RET_* value. On error ACT_RET_CONT is returned and the action is |
| 6911 | * ignored. If the lua action yields, ACT_RET_YIELD is returned. On success, the |
| 6912 | * return value is the first element on the stack. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6913 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6914 | 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] | 6915 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6916 | { |
| 6917 | char **arg; |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6918 | unsigned int hflags = 0; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6919 | int dir, act_ret = ACT_RET_CONT; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6920 | const char *error; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6921 | |
| 6922 | switch (rule->from) { |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 6923 | case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break; |
| 6924 | case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break; |
| 6925 | case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break; |
| 6926 | case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6927 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6928 | SEND_ERR(px, "Lua: internal error while execute action.\n"); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6929 | goto end; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6930 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6931 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6932 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6933 | * Lua context can be not initialized. This behavior |
| 6934 | * permits to save performances because a systematic |
| 6935 | * Lua initialization cause 5% performances loss. |
| 6936 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6937 | if (!s->hlua) { |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 6938 | struct hlua *hlua; |
| 6939 | |
| 6940 | hlua = pool_alloc(pool_head_hlua); |
| 6941 | if (!hlua) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6942 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 6943 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6944 | goto end; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6945 | } |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 6946 | HLUA_INIT(hlua); |
| 6947 | s->hlua = hlua; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 6948 | 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] | 6949 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 6950 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6951 | goto end; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6952 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6953 | } |
| 6954 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6955 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6956 | if (!HLUA_IS_RUNNING(s->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6957 | |
| 6958 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 6959 | if (!SET_SAFE_LJMP(s->hlua)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6960 | if (lua_type(s->hlua->T, -1) == LUA_TSTRING) |
| 6961 | error = lua_tostring(s->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6962 | else |
| 6963 | error = "critical error"; |
| 6964 | SEND_ERR(px, "Lua function '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 6965 | rule->arg.hlua_rule->fcn->name, error); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6966 | goto end; |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6967 | } |
| 6968 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6969 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6970 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6971 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 6972 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 6973 | RESET_SAFE_LJMP(s->hlua); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6974 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6975 | } |
| 6976 | |
| 6977 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 6978 | 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] | 6979 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6980 | /* Create and and push object stream in the stack. */ |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6981 | if (!hlua_txn_new(s->hlua->T, s, px, dir, hflags)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6982 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 6983 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 6984 | RESET_SAFE_LJMP(s->hlua); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6985 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6986 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6987 | s->hlua->nargs = 1; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6988 | |
| 6989 | /* push keywords in the stack. */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6990 | for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6991 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6992 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 6993 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 6994 | RESET_SAFE_LJMP(s->hlua); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6995 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6996 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6997 | lua_pushstring(s->hlua->T, *arg); |
| 6998 | s->hlua->nargs++; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6999 | } |
| 7000 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7001 | /* Now the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7002 | RESET_SAFE_LJMP(s->hlua); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7003 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 7004 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 7005 | s->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7006 | } |
| 7007 | |
| 7008 | /* Execute the function. */ |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 7009 | switch (hlua_ctx_resume(s->hlua, !(flags & ACT_OPT_FINAL))) { |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7010 | /* finished. */ |
| 7011 | case HLUA_E_OK: |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7012 | /* Catch the return value */ |
| 7013 | if (lua_gettop(s->hlua->T) > 0) |
| 7014 | act_ret = lua_tointeger(s->hlua->T, -1); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7015 | |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 7016 | /* Set timeout in the required channel. */ |
Christopher Faulet | 498c483 | 2020-07-28 11:59:58 +0200 | [diff] [blame] | 7017 | if (act_ret == ACT_RET_YIELD) { |
| 7018 | if (flags & ACT_OPT_FINAL) |
| 7019 | goto err_yield; |
| 7020 | |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 7021 | if (dir == SMP_OPT_DIR_REQ) |
| 7022 | s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp), |
| 7023 | s->hlua->wake_time); |
| 7024 | else |
| 7025 | s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp), |
| 7026 | s->hlua->wake_time); |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 7027 | } |
| 7028 | goto end; |
| 7029 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7030 | /* yield. */ |
| 7031 | case HLUA_E_AGAIN: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 7032 | /* Set timeout in the required channel. */ |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 7033 | if (dir == SMP_OPT_DIR_REQ) |
| 7034 | s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp), |
| 7035 | s->hlua->wake_time); |
| 7036 | else |
| 7037 | s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp), |
| 7038 | s->hlua->wake_time); |
| 7039 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7040 | /* Some actions can be wake up when a "write" event |
| 7041 | * is detected on a response channel. This is useful |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7042 | * only for actions targeted on the requests. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7043 | */ |
Christopher Faulet | 51fa358 | 2019-07-26 14:54:52 +0200 | [diff] [blame] | 7044 | if (HLUA_IS_WAKERESWR(s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 7045 | s->res.flags |= CF_WAKE_WRITE; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 7046 | if (HLUA_IS_WAKEREQWR(s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 7047 | s->req.flags |= CF_WAKE_WRITE; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7048 | act_ret = ACT_RET_YIELD; |
| 7049 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7050 | |
| 7051 | /* finished with error. */ |
| 7052 | case HLUA_E_ERRMSG: |
| 7053 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 7054 | SEND_ERR(px, "Lua function '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7055 | rule->arg.hlua_rule->fcn->name, lua_tostring(s->hlua->T, -1)); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 7056 | lua_pop(s->hlua->T, 1); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7057 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7058 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7059 | case HLUA_E_ETMOUT: |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7060 | 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] | 7061 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7062 | |
| 7063 | case HLUA_E_NOMEM: |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7064 | 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] | 7065 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7066 | |
| 7067 | case HLUA_E_YIELD: |
Christopher Faulet | 498c483 | 2020-07-28 11:59:58 +0200 | [diff] [blame] | 7068 | err_yield: |
| 7069 | act_ret = ACT_RET_CONT; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7070 | 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] | 7071 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7072 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7073 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7074 | case HLUA_E_ERR: |
| 7075 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 7076 | SEND_ERR(px, "Lua function '%s' return an unknown error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7077 | rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7078 | |
| 7079 | default: |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7080 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7081 | } |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7082 | |
| 7083 | end: |
Christopher Faulet | 2361fd9 | 2020-07-30 10:40:58 +0200 | [diff] [blame] | 7084 | if (act_ret != ACT_RET_YIELD && s->hlua) |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 7085 | s->hlua->wake_time = TICK_ETERNITY; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7086 | return act_ret; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7087 | } |
| 7088 | |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 7089 | 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] | 7090 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 7091 | struct appctx *ctx = context; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7092 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7093 | appctx_wakeup(ctx); |
Willy Tarreau | d958741 | 2017-08-23 16:07:33 +0200 | [diff] [blame] | 7094 | t->expire = TICK_ETERNITY; |
Willy Tarreau | d1aa41f | 2017-07-21 16:41:56 +0200 | [diff] [blame] | 7095 | return t; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7096 | } |
| 7097 | |
| 7098 | static int hlua_applet_tcp_init(struct appctx *ctx, struct proxy *px, struct stream *strm) |
| 7099 | { |
| 7100 | struct stream_interface *si = ctx->owner; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7101 | struct hlua *hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7102 | struct task *task; |
| 7103 | char **arg; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7104 | const char *error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7105 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7106 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7107 | if (!hlua) { |
| 7108 | SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7109 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7110 | return 0; |
| 7111 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7112 | HLUA_INIT(hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7113 | ctx->ctx.hlua_apptcp.hlua = hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7114 | ctx->ctx.hlua_apptcp.flags = 0; |
| 7115 | |
| 7116 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 7117 | task = task_new(tid_bit); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7118 | if (!task) { |
| 7119 | SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7120 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7121 | return 0; |
| 7122 | } |
| 7123 | task->nice = 0; |
| 7124 | task->context = ctx; |
| 7125 | task->process = hlua_applet_wakeup; |
| 7126 | ctx->ctx.hlua_apptcp.task = task; |
| 7127 | |
| 7128 | /* In the execution wrappers linked with a stream, the |
| 7129 | * Lua context can be not initialized. This behavior |
| 7130 | * permits to save performances because a systematic |
| 7131 | * Lua initialization cause 5% performances loss. |
| 7132 | */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 7133 | 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] | 7134 | 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] | 7135 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7136 | return 0; |
| 7137 | } |
| 7138 | |
| 7139 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 7140 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7141 | |
| 7142 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7143 | if (!SET_SAFE_LJMP(hlua)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7144 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 7145 | error = lua_tostring(hlua->T, -1); |
| 7146 | else |
| 7147 | error = "critical error"; |
| 7148 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7149 | ctx->rule->arg.hlua_rule->fcn->name, error); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7150 | return 0; |
| 7151 | } |
| 7152 | |
| 7153 | /* Check stack available size. */ |
| 7154 | if (!lua_checkstack(hlua->T, 1)) { |
| 7155 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7156 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7157 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7158 | return 0; |
| 7159 | } |
| 7160 | |
| 7161 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 7162 | 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] | 7163 | |
| 7164 | /* Create and and push object stream in the stack. */ |
| 7165 | if (!hlua_applet_tcp_new(hlua->T, ctx)) { |
| 7166 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7167 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7168 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7169 | return 0; |
| 7170 | } |
| 7171 | hlua->nargs = 1; |
| 7172 | |
| 7173 | /* push keywords in the stack. */ |
| 7174 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 7175 | if (!lua_checkstack(hlua->T, 1)) { |
| 7176 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7177 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7178 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7179 | return 0; |
| 7180 | } |
| 7181 | lua_pushstring(hlua->T, *arg); |
| 7182 | hlua->nargs++; |
| 7183 | } |
| 7184 | |
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 | |
| 7187 | /* Wakeup the applet ASAP. */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 7188 | si_cant_get(si); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 7189 | si_rx_endp_more(si); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7190 | |
| 7191 | return 1; |
| 7192 | } |
| 7193 | |
Willy Tarreau | 60409db | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 7194 | void hlua_applet_tcp_fct(struct appctx *ctx) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7195 | { |
| 7196 | struct stream_interface *si = ctx->owner; |
| 7197 | struct stream *strm = si_strm(si); |
| 7198 | struct channel *res = si_ic(si); |
| 7199 | struct act_rule *rule = ctx->rule; |
| 7200 | struct proxy *px = strm->be; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7201 | struct hlua *hlua = ctx->ctx.hlua_apptcp.hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7202 | |
| 7203 | /* The applet execution is already done. */ |
Olivier Houchard | 594c8c5 | 2018-08-28 14:41:31 +0200 | [diff] [blame] | 7204 | if (ctx->ctx.hlua_apptcp.flags & APPLET_DONE) { |
| 7205 | /* eat the whole request */ |
| 7206 | co_skip(si_oc(si), co_data(si_oc(si))); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7207 | return; |
Olivier Houchard | 594c8c5 | 2018-08-28 14:41:31 +0200 | [diff] [blame] | 7208 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7209 | |
| 7210 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 7211 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 7212 | return; |
| 7213 | |
| 7214 | /* Execute the function. */ |
| 7215 | switch (hlua_ctx_resume(hlua, 1)) { |
| 7216 | /* finished. */ |
| 7217 | case HLUA_E_OK: |
| 7218 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 7219 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7220 | /* eat the whole request */ |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 7221 | co_skip(si_oc(si), co_data(si_oc(si))); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7222 | res->flags |= CF_READ_NULL; |
| 7223 | si_shutr(si); |
| 7224 | return; |
| 7225 | |
| 7226 | /* yield. */ |
| 7227 | case HLUA_E_AGAIN: |
Thierry Fournier | 0164f20 | 2016-02-20 17:47:43 +0100 | [diff] [blame] | 7228 | if (hlua->wake_time != TICK_ETERNITY) |
| 7229 | task_schedule(ctx->ctx.hlua_apptcp.task, hlua->wake_time); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7230 | return; |
| 7231 | |
| 7232 | /* finished with error. */ |
| 7233 | case HLUA_E_ERRMSG: |
| 7234 | /* Display log. */ |
| 7235 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7236 | rule->arg.hlua_rule->fcn->name, lua_tostring(hlua->T, -1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7237 | lua_pop(hlua->T, 1); |
| 7238 | goto error; |
| 7239 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7240 | case HLUA_E_ETMOUT: |
| 7241 | SEND_ERR(px, "Lua applet tcp '%s': execution timeout.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7242 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7243 | goto error; |
| 7244 | |
| 7245 | case HLUA_E_NOMEM: |
| 7246 | SEND_ERR(px, "Lua applet tcp '%s': out of memory error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7247 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7248 | goto error; |
| 7249 | |
| 7250 | case HLUA_E_YIELD: /* unexpected */ |
| 7251 | SEND_ERR(px, "Lua applet tcp '%s': yield not allowed.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7252 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7253 | goto error; |
| 7254 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7255 | case HLUA_E_ERR: |
| 7256 | /* Display log. */ |
| 7257 | SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7258 | rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7259 | goto error; |
| 7260 | |
| 7261 | default: |
| 7262 | goto error; |
| 7263 | } |
| 7264 | |
| 7265 | error: |
| 7266 | |
| 7267 | /* For all other cases, just close the stream. */ |
| 7268 | si_shutw(si); |
| 7269 | si_shutr(si); |
| 7270 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 7271 | } |
| 7272 | |
| 7273 | static void hlua_applet_tcp_release(struct appctx *ctx) |
| 7274 | { |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 7275 | task_destroy(ctx->ctx.hlua_apptcp.task); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7276 | ctx->ctx.hlua_apptcp.task = NULL; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7277 | hlua_ctx_destroy(ctx->ctx.hlua_apptcp.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7278 | ctx->ctx.hlua_apptcp.hlua = NULL; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7279 | } |
| 7280 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7281 | /* The function returns 1 if the initialisation is complete, 0 if |
| 7282 | * an errors occurs and -1 if more data are required for initializing |
| 7283 | * the applet. |
| 7284 | */ |
| 7285 | static int hlua_applet_http_init(struct appctx *ctx, struct proxy *px, struct stream *strm) |
| 7286 | { |
| 7287 | struct stream_interface *si = ctx->owner; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7288 | struct http_txn *txn; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7289 | struct hlua *hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7290 | char **arg; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7291 | struct task *task; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7292 | const char *error; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7293 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7294 | txn = strm->txn; |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7295 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7296 | if (!hlua) { |
| 7297 | SEND_ERR(px, "Lua applet http '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7298 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7299 | return 0; |
| 7300 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7301 | HLUA_INIT(hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7302 | ctx->ctx.hlua_apphttp.hlua = hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7303 | ctx->ctx.hlua_apphttp.left_bytes = -1; |
| 7304 | ctx->ctx.hlua_apphttp.flags = 0; |
| 7305 | |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 7306 | if (txn->req.flags & HTTP_MSGF_VER_11) |
| 7307 | ctx->ctx.hlua_apphttp.flags |= APPLET_HTTP11; |
| 7308 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7309 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 7310 | task = task_new(tid_bit); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7311 | if (!task) { |
| 7312 | SEND_ERR(px, "Lua applet http '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7313 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7314 | return 0; |
| 7315 | } |
| 7316 | task->nice = 0; |
| 7317 | task->context = ctx; |
| 7318 | task->process = hlua_applet_wakeup; |
| 7319 | ctx->ctx.hlua_apphttp.task = task; |
| 7320 | |
| 7321 | /* In the execution wrappers linked with a stream, the |
| 7322 | * Lua context can be not initialized. This behavior |
| 7323 | * permits to save performances because a systematic |
| 7324 | * Lua initialization cause 5% performances loss. |
| 7325 | */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 7326 | 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] | 7327 | 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] | 7328 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7329 | return 0; |
| 7330 | } |
| 7331 | |
| 7332 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 7333 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7334 | |
| 7335 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7336 | if (!SET_SAFE_LJMP(hlua)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7337 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 7338 | error = lua_tostring(hlua->T, -1); |
| 7339 | else |
| 7340 | error = "critical error"; |
| 7341 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7342 | ctx->rule->arg.hlua_rule->fcn->name, error); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7343 | return 0; |
| 7344 | } |
| 7345 | |
| 7346 | /* Check stack available size. */ |
| 7347 | if (!lua_checkstack(hlua->T, 1)) { |
| 7348 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7349 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7350 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7351 | return 0; |
| 7352 | } |
| 7353 | |
| 7354 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 7355 | 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] | 7356 | |
| 7357 | /* Create and and push object stream in the stack. */ |
| 7358 | if (!hlua_applet_http_new(hlua->T, ctx)) { |
| 7359 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7360 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7361 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7362 | return 0; |
| 7363 | } |
| 7364 | hlua->nargs = 1; |
| 7365 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7366 | /* push keywords in the stack. */ |
| 7367 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 7368 | if (!lua_checkstack(hlua->T, 1)) { |
| 7369 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7370 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7371 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7372 | return 0; |
| 7373 | } |
| 7374 | lua_pushstring(hlua->T, *arg); |
| 7375 | hlua->nargs++; |
| 7376 | } |
| 7377 | |
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 | |
| 7380 | /* Wakeup the applet when data is ready for read. */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 7381 | si_cant_get(si); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7382 | |
| 7383 | return 1; |
| 7384 | } |
| 7385 | |
Willy Tarreau | 60409db | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 7386 | void hlua_applet_http_fct(struct appctx *ctx) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7387 | { |
| 7388 | struct stream_interface *si = ctx->owner; |
| 7389 | struct stream *strm = si_strm(si); |
| 7390 | struct channel *req = si_oc(si); |
| 7391 | struct channel *res = si_ic(si); |
| 7392 | struct act_rule *rule = ctx->rule; |
| 7393 | struct proxy *px = strm->be; |
| 7394 | struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua; |
| 7395 | struct htx *req_htx, *res_htx; |
| 7396 | |
| 7397 | res_htx = htx_from_buf(&res->buf); |
| 7398 | |
| 7399 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 7400 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 7401 | goto out; |
| 7402 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 7403 | /* Check if the input buffer is available. */ |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7404 | if (!b_size(&res->buf)) { |
| 7405 | si_rx_room_blk(si); |
| 7406 | goto out; |
| 7407 | } |
| 7408 | /* check that the output is not closed */ |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7409 | if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR)) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7410 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 7411 | |
| 7412 | /* Set the currently running flag. */ |
| 7413 | if (!HLUA_IS_RUNNING(hlua) && |
| 7414 | !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
Christopher Faulet | bd878d2 | 2021-04-28 10:50:21 +0200 | [diff] [blame] | 7415 | if (!co_data(req)) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7416 | si_cant_get(si); |
| 7417 | goto out; |
| 7418 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7419 | } |
| 7420 | |
| 7421 | /* Executes The applet if it is not done. */ |
| 7422 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
| 7423 | |
| 7424 | /* Execute the function. */ |
| 7425 | switch (hlua_ctx_resume(hlua, 1)) { |
| 7426 | /* finished. */ |
| 7427 | case HLUA_E_OK: |
| 7428 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 7429 | break; |
| 7430 | |
| 7431 | /* yield. */ |
| 7432 | case HLUA_E_AGAIN: |
| 7433 | if (hlua->wake_time != TICK_ETERNITY) |
| 7434 | task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time); |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7435 | goto out; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7436 | |
| 7437 | /* finished with error. */ |
| 7438 | case HLUA_E_ERRMSG: |
| 7439 | /* Display log. */ |
| 7440 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7441 | rule->arg.hlua_rule->fcn->name, lua_tostring(hlua->T, -1)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7442 | lua_pop(hlua->T, 1); |
| 7443 | goto error; |
| 7444 | |
| 7445 | case HLUA_E_ETMOUT: |
| 7446 | SEND_ERR(px, "Lua applet http '%s': execution timeout.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7447 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7448 | goto error; |
| 7449 | |
| 7450 | case HLUA_E_NOMEM: |
| 7451 | SEND_ERR(px, "Lua applet http '%s': out of memory error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7452 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7453 | goto error; |
| 7454 | |
| 7455 | case HLUA_E_YIELD: /* unexpected */ |
| 7456 | SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7457 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7458 | goto error; |
| 7459 | |
| 7460 | case HLUA_E_ERR: |
| 7461 | /* Display log. */ |
| 7462 | SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7463 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7464 | goto error; |
| 7465 | |
| 7466 | default: |
| 7467 | goto error; |
| 7468 | } |
| 7469 | } |
| 7470 | |
| 7471 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7472 | if (ctx->ctx.hlua_apphttp.flags & APPLET_RSP_SENT) |
| 7473 | goto done; |
| 7474 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7475 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) |
| 7476 | goto error; |
| 7477 | |
Christopher Faulet | 868b3b1 | 2022-04-07 10:07:18 +0200 | [diff] [blame] | 7478 | /* no more data are expected. If the response buffer is empty |
| 7479 | * for a chunked message, be sure to add something (EOT block in |
| 7480 | * this case) to have something to send. It is important to be |
| 7481 | * sure the EOM flags will be handled by the endpoint. |
| 7482 | */ |
| 7483 | if (htx_is_empty(res_htx) && (strm->txn->rsp.flags & (HTTP_MSGF_XFER_LEN|HTTP_MSGF_CNT_LEN)) == HTTP_MSGF_XFER_LEN) { |
| 7484 | if (!htx_add_endof(res_htx, HTX_BLK_EOT)) { |
| 7485 | si_rx_room_blk(si); |
| 7486 | goto out; |
| 7487 | } |
| 7488 | channel_add_input(res, 1); |
| 7489 | } |
| 7490 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 7491 | res_htx->flags |= HTX_FL_EOM; |
Christopher Faulet | 79c0fc7 | 2022-03-07 15:50:54 +0100 | [diff] [blame] | 7492 | res->flags |= CF_EOI; |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7493 | strm->txn->status = ctx->ctx.hlua_apphttp.status; |
| 7494 | ctx->ctx.hlua_apphttp.flags |= APPLET_RSP_SENT; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7495 | } |
| 7496 | |
| 7497 | done: |
| 7498 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7499 | if (!(res->flags & CF_SHUTR)) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7500 | res->flags |= CF_READ_NULL; |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7501 | si_shutr(si); |
| 7502 | } |
| 7503 | |
| 7504 | /* eat the whole request */ |
| 7505 | if (co_data(req)) { |
| 7506 | req_htx = htx_from_buf(&req->buf); |
| 7507 | co_htx_skip(req, req_htx, co_data(req)); |
| 7508 | htx_to_buf(req_htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7509 | } |
| 7510 | } |
| 7511 | |
| 7512 | out: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7513 | htx_to_buf(res_htx, &res->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7514 | return; |
| 7515 | |
| 7516 | error: |
| 7517 | |
| 7518 | /* If we are in HTTP mode, and we are not send any |
| 7519 | * data, return a 500 server error in best effort: |
| 7520 | * if there is no room available in the buffer, |
| 7521 | * just close the connection. |
| 7522 | */ |
| 7523 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) { |
Christopher Faulet | f734638 | 2019-07-17 22:02:08 +0200 | [diff] [blame] | 7524 | struct buffer *err = &http_err_chunks[HTTP_ERR_500]; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7525 | |
| 7526 | channel_erase(res); |
| 7527 | res->buf.data = b_data(err); |
| 7528 | memcpy(res->buf.area, b_head(err), b_data(err)); |
| 7529 | res_htx = htx_from_buf(&res->buf); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 7530 | channel_add_input(res, res_htx->data); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7531 | } |
| 7532 | if (!(strm->flags & SF_ERR_MASK)) |
| 7533 | strm->flags |= SF_ERR_RESOURCE; |
| 7534 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 7535 | goto done; |
| 7536 | } |
| 7537 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7538 | static void hlua_applet_http_release(struct appctx *ctx) |
| 7539 | { |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 7540 | task_destroy(ctx->ctx.hlua_apphttp.task); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7541 | ctx->ctx.hlua_apphttp.task = NULL; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7542 | hlua_ctx_destroy(ctx->ctx.hlua_apphttp.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7543 | ctx->ctx.hlua_apphttp.hlua = NULL; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7544 | } |
| 7545 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7546 | /* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 7547 | * success case, else return ACT_RET_PRS_ERR. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7548 | * |
| 7549 | * This function can fail with an abort() due to an Lua critical error. |
| 7550 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 7551 | * tolerated. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7552 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7553 | static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px, |
| 7554 | struct act_rule *rule, char **err) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7555 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7556 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7557 | int i; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7558 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7559 | /* Memory for the rule. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7560 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7561 | if (!rule->arg.hlua_rule) { |
| 7562 | memprintf(err, "out of memory error"); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 7563 | goto error; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7564 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7565 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7566 | /* Memory for arguments. */ |
Tim Duesterhus | e52b6e5 | 2020-09-12 20:26:43 +0200 | [diff] [blame] | 7567 | rule->arg.hlua_rule->args = calloc(fcn->nargs + 1, |
| 7568 | sizeof(*rule->arg.hlua_rule->args)); |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7569 | if (!rule->arg.hlua_rule->args) { |
| 7570 | memprintf(err, "out of memory error"); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 7571 | goto error; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7572 | } |
| 7573 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7574 | /* Reference the Lua function and store the reference. */ |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7575 | rule->arg.hlua_rule->fcn = fcn; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7576 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7577 | /* Expect some arguments */ |
| 7578 | for (i = 0; i < fcn->nargs; i++) { |
Thierry FOURNIER | 1725c2e | 2019-01-06 19:38:49 +0100 | [diff] [blame] | 7579 | if (*args[*cur_arg] == '\0') { |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7580 | memprintf(err, "expect %d arguments", fcn->nargs); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 7581 | goto error; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7582 | } |
Thierry FOURNIER | 1725c2e | 2019-01-06 19:38:49 +0100 | [diff] [blame] | 7583 | rule->arg.hlua_rule->args[i] = strdup(args[*cur_arg]); |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7584 | if (!rule->arg.hlua_rule->args[i]) { |
| 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 | (*cur_arg)++; |
| 7589 | } |
| 7590 | rule->arg.hlua_rule->args[i] = NULL; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7591 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 7592 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7593 | rule->action_ptr = hlua_action; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 7594 | return ACT_RET_PRS_OK; |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 7595 | |
| 7596 | error: |
| 7597 | if (rule->arg.hlua_rule) { |
| 7598 | if (rule->arg.hlua_rule->args) { |
| 7599 | for (i = 0; i < fcn->nargs; i++) |
| 7600 | ha_free(&rule->arg.hlua_rule->args[i]); |
| 7601 | ha_free(&rule->arg.hlua_rule->args); |
| 7602 | } |
| 7603 | ha_free(&rule->arg.hlua_rule); |
| 7604 | } |
| 7605 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7606 | } |
| 7607 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7608 | static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px, |
| 7609 | struct act_rule *rule, char **err) |
| 7610 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7611 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7612 | |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 7613 | /* HTTP applets are forbidden in tcp-request rules. |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 7614 | * HTTP applet request requires everything initialized by |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 7615 | * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER). |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 7616 | * The applet will be immediately initialized, but its before |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 7617 | * the call of this analyzer. |
| 7618 | */ |
| 7619 | if (rule->from != ACT_F_HTTP_REQ) { |
| 7620 | memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets"); |
| 7621 | return ACT_RET_PRS_ERR; |
| 7622 | } |
| 7623 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7624 | /* Memory for the rule. */ |
| 7625 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 7626 | if (!rule->arg.hlua_rule) { |
| 7627 | memprintf(err, "out of memory error"); |
| 7628 | return ACT_RET_PRS_ERR; |
| 7629 | } |
| 7630 | |
| 7631 | /* Reference the Lua function and store the reference. */ |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7632 | rule->arg.hlua_rule->fcn = fcn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7633 | |
| 7634 | /* TODO: later accept arguments. */ |
| 7635 | rule->arg.hlua_rule->args = NULL; |
| 7636 | |
| 7637 | /* Add applet pointer in the rule. */ |
| 7638 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 7639 | rule->applet.name = fcn->name; |
| 7640 | rule->applet.init = hlua_applet_http_init; |
| 7641 | rule->applet.fct = hlua_applet_http_fct; |
| 7642 | rule->applet.release = hlua_applet_http_release; |
| 7643 | rule->applet.timeout = hlua_timeout_applet; |
| 7644 | |
| 7645 | return ACT_RET_PRS_OK; |
| 7646 | } |
| 7647 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7648 | /* This function is an LUA binding used for registering |
| 7649 | * "sample-conv" functions. It expects a converter name used |
| 7650 | * in the haproxy configuration file, and an LUA function. |
| 7651 | */ |
| 7652 | __LJMP static int hlua_register_action(lua_State *L) |
| 7653 | { |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 7654 | struct action_kw_list *akl = NULL; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7655 | const char *name; |
| 7656 | int ref; |
| 7657 | int len; |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 7658 | struct hlua_function *fcn = NULL; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7659 | int nargs; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 7660 | struct buffer *trash; |
| 7661 | struct action_kw *akw; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7662 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7663 | /* Initialise the number of expected arguments at 0. */ |
| 7664 | nargs = 0; |
| 7665 | |
| 7666 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 7667 | 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] | 7668 | |
| 7669 | /* First argument : converter name. */ |
| 7670 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 7671 | |
| 7672 | /* Second argument : environment. */ |
| 7673 | if (lua_type(L, 2) != LUA_TTABLE) |
| 7674 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 7675 | |
| 7676 | /* Third argument : lua function. */ |
| 7677 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 7678 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7679 | /* 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] | 7680 | if (lua_gettop(L) >= 4) |
| 7681 | nargs = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 7682 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7683 | /* browse the second argument as an array. */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7684 | lua_pushnil(L); |
| 7685 | while (lua_next(L, 2) != 0) { |
| 7686 | if (lua_type(L, -1) != LUA_TSTRING) |
| 7687 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 7688 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 7689 | /* Check if action exists */ |
| 7690 | trash = get_trash_chunk(); |
| 7691 | chunk_printf(trash, "lua.%s", name); |
| 7692 | if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) { |
| 7693 | akw = tcp_req_cont_action(trash->area); |
| 7694 | } else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) { |
| 7695 | akw = tcp_res_cont_action(trash->area); |
| 7696 | } else if (strcmp(lua_tostring(L, -1), "http-req") == 0) { |
| 7697 | akw = action_http_req_custom(trash->area); |
| 7698 | } else if (strcmp(lua_tostring(L, -1), "http-res") == 0) { |
| 7699 | akw = action_http_res_custom(trash->area); |
| 7700 | } else { |
| 7701 | akw = NULL; |
| 7702 | } |
| 7703 | if (akw != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 7704 | fcn = akw->private; |
| 7705 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 7706 | ha_warning("Trying to register action 'lua.%s' more than once. " |
| 7707 | "This will become a hard error in version 2.5.\n", name); |
| 7708 | } |
| 7709 | fcn->function_ref[hlua_state_id] = ref; |
| 7710 | |
| 7711 | /* pop the environment string. */ |
| 7712 | lua_pop(L, 1); |
| 7713 | continue; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 7714 | } |
| 7715 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7716 | /* Check required environment. Only accepted "http" or "tcp". */ |
| 7717 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7718 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7719 | if (!akl) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 7720 | goto alloc_error;; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 7721 | fcn = new_hlua_function(); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7722 | if (!fcn) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 7723 | goto alloc_error; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7724 | |
| 7725 | /* Fill fcn. */ |
| 7726 | fcn->name = strdup(name); |
| 7727 | if (!fcn->name) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 7728 | goto alloc_error; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 7729 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7730 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 7731 | /* Set the expected number of arguments. */ |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7732 | fcn->nargs = nargs; |
| 7733 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7734 | /* List head */ |
| 7735 | akl->list.n = akl->list.p = NULL; |
| 7736 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7737 | /* action keyword. */ |
| 7738 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7739 | akl->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7740 | if (!akl->kw[0].kw) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 7741 | goto alloc_error; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7742 | |
| 7743 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 7744 | |
Amaury Denoyelle | e4a617c | 2021-05-06 15:33:09 +0200 | [diff] [blame] | 7745 | akl->kw[0].flags = 0; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7746 | akl->kw[0].private = fcn; |
| 7747 | akl->kw[0].parse = action_register_lua; |
| 7748 | |
| 7749 | /* select the action registering point. */ |
| 7750 | if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) |
| 7751 | tcp_req_cont_keywords_register(akl); |
| 7752 | else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) |
| 7753 | tcp_res_cont_keywords_register(akl); |
| 7754 | else if (strcmp(lua_tostring(L, -1), "http-req") == 0) |
| 7755 | http_req_keywords_register(akl); |
| 7756 | else if (strcmp(lua_tostring(L, -1), "http-res") == 0) |
| 7757 | http_res_keywords_register(akl); |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 7758 | else { |
| 7759 | release_hlua_function(fcn); |
| 7760 | if (akl) |
| 7761 | ha_free((char **)&(akl->kw[0].kw)); |
| 7762 | ha_free(&akl); |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7763 | WILL_LJMP(luaL_error(L, "Lua action environment '%s' is unknown. " |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7764 | "'tcp-req', 'tcp-res', 'http-req' or 'http-res' " |
| 7765 | "are expected.", lua_tostring(L, -1))); |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 7766 | } |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7767 | |
| 7768 | /* pop the environment string. */ |
| 7769 | lua_pop(L, 1); |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 7770 | |
| 7771 | /* reset for next loop */ |
| 7772 | akl = NULL; |
| 7773 | fcn = NULL; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7774 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7775 | return ACT_RET_PRS_OK; |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 7776 | |
| 7777 | alloc_error: |
| 7778 | release_hlua_function(fcn); |
| 7779 | ha_free(&akl); |
| 7780 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 7781 | return 0; /* Never reached */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7782 | } |
| 7783 | |
| 7784 | static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px, |
| 7785 | struct act_rule *rule, char **err) |
| 7786 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7787 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7788 | |
Christopher Faulet | 280f85b | 2019-07-15 15:02:04 +0200 | [diff] [blame] | 7789 | if (px->mode == PR_MODE_HTTP) { |
| 7790 | memprintf(err, "Lua TCP services cannot be used on HTTP proxies"); |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 7791 | return ACT_RET_PRS_ERR; |
| 7792 | } |
| 7793 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7794 | /* Memory for the rule. */ |
| 7795 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 7796 | if (!rule->arg.hlua_rule) { |
| 7797 | memprintf(err, "out of memory error"); |
| 7798 | return ACT_RET_PRS_ERR; |
| 7799 | } |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7800 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7801 | /* Reference the Lua function and store the reference. */ |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7802 | rule->arg.hlua_rule->fcn = fcn; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7803 | |
| 7804 | /* TODO: later accept arguments. */ |
| 7805 | rule->arg.hlua_rule->args = NULL; |
| 7806 | |
| 7807 | /* Add applet pointer in the rule. */ |
| 7808 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 7809 | rule->applet.name = fcn->name; |
| 7810 | rule->applet.init = hlua_applet_tcp_init; |
| 7811 | rule->applet.fct = hlua_applet_tcp_fct; |
| 7812 | rule->applet.release = hlua_applet_tcp_release; |
| 7813 | rule->applet.timeout = hlua_timeout_applet; |
| 7814 | |
| 7815 | return 0; |
| 7816 | } |
| 7817 | |
| 7818 | /* This function is an LUA binding used for registering |
| 7819 | * "sample-conv" functions. It expects a converter name used |
| 7820 | * in the haproxy configuration file, and an LUA function. |
| 7821 | */ |
| 7822 | __LJMP static int hlua_register_service(lua_State *L) |
| 7823 | { |
| 7824 | struct action_kw_list *akl; |
| 7825 | const char *name; |
| 7826 | const char *env; |
| 7827 | int ref; |
| 7828 | int len; |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 7829 | struct hlua_function *fcn = NULL; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 7830 | struct buffer *trash; |
| 7831 | struct action_kw *akw; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7832 | |
| 7833 | MAY_LJMP(check_args(L, 3, "register_service")); |
| 7834 | |
| 7835 | /* First argument : converter name. */ |
| 7836 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 7837 | |
| 7838 | /* Second argument : environment. */ |
| 7839 | env = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7840 | |
| 7841 | /* Third argument : lua function. */ |
| 7842 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 7843 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 7844 | /* Check for service already registered */ |
| 7845 | trash = get_trash_chunk(); |
| 7846 | chunk_printf(trash, "lua.%s", name); |
| 7847 | akw = service_find(trash->area); |
| 7848 | if (akw != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 7849 | fcn = akw->private; |
| 7850 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 7851 | ha_warning("Trying to register service 'lua.%s' more than once. " |
| 7852 | "This will become a hard error in version 2.5.\n", name); |
| 7853 | } |
| 7854 | fcn->function_ref[hlua_state_id] = ref; |
| 7855 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 7856 | } |
| 7857 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7858 | /* Allocate and fill the sample fetch keyword struct. */ |
| 7859 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
| 7860 | if (!akl) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 7861 | goto alloc_error; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 7862 | fcn = new_hlua_function(); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7863 | if (!fcn) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 7864 | goto alloc_error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7865 | |
| 7866 | /* Fill fcn. */ |
| 7867 | len = strlen("<lua.>") + strlen(name) + 1; |
| 7868 | fcn->name = calloc(1, len); |
| 7869 | if (!fcn->name) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 7870 | goto alloc_error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7871 | snprintf((char *)fcn->name, len, "<lua.%s>", name); |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 7872 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7873 | |
| 7874 | /* List head */ |
| 7875 | akl->list.n = akl->list.p = NULL; |
| 7876 | |
| 7877 | /* converter keyword. */ |
| 7878 | len = strlen("lua.") + strlen(name) + 1; |
| 7879 | akl->kw[0].kw = calloc(1, len); |
| 7880 | if (!akl->kw[0].kw) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 7881 | goto alloc_error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7882 | |
| 7883 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 7884 | |
Thierry FOURNIER / OZON.IO | 02564fd | 2016-11-12 11:07:05 +0100 | [diff] [blame] | 7885 | /* Check required environment. Only accepted "http" or "tcp". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7886 | if (strcmp(env, "tcp") == 0) |
| 7887 | akl->kw[0].parse = action_register_service_tcp; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7888 | else if (strcmp(env, "http") == 0) |
| 7889 | akl->kw[0].parse = action_register_service_http; |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 7890 | else { |
| 7891 | release_hlua_function(fcn); |
| 7892 | if (akl) |
| 7893 | ha_free((char **)&(akl->kw[0].kw)); |
| 7894 | ha_free(&akl); |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7895 | WILL_LJMP(luaL_error(L, "Lua service environment '%s' is unknown. " |
Eric Salama | fe7456f | 2017-12-21 14:30:07 +0100 | [diff] [blame] | 7896 | "'tcp' or 'http' are expected.", env)); |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 7897 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7898 | |
Amaury Denoyelle | e4a617c | 2021-05-06 15:33:09 +0200 | [diff] [blame] | 7899 | akl->kw[0].flags = 0; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7900 | akl->kw[0].private = fcn; |
| 7901 | |
| 7902 | /* End of array. */ |
| 7903 | memset(&akl->kw[1], 0, sizeof(*akl->kw)); |
| 7904 | |
| 7905 | /* Register this new converter */ |
| 7906 | service_keywords_register(akl); |
| 7907 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7908 | return 0; |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 7909 | |
| 7910 | alloc_error: |
| 7911 | release_hlua_function(fcn); |
| 7912 | ha_free(&akl); |
| 7913 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 7914 | return 0; /* Never reached */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7915 | } |
| 7916 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7917 | /* This function initialises Lua cli handler. It copies the |
| 7918 | * arguments in the Lua stack and create channel IO objects. |
| 7919 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 7920 | 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] | 7921 | { |
| 7922 | struct hlua *hlua; |
| 7923 | struct hlua_function *fcn; |
| 7924 | int i; |
| 7925 | const char *error; |
| 7926 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7927 | fcn = private; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7928 | appctx->ctx.hlua_cli.fcn = private; |
| 7929 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7930 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7931 | if (!hlua) { |
| 7932 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 7933 | return 1; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7934 | } |
| 7935 | HLUA_INIT(hlua); |
| 7936 | appctx->ctx.hlua_cli.hlua = hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7937 | |
| 7938 | /* Create task used by signal to wakeup applets. |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 7939 | * 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] | 7940 | * applet_http. It is absolutely compatible. |
| 7941 | */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 7942 | appctx->ctx.hlua_cli.task = task_new(tid_bit); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7943 | if (!appctx->ctx.hlua_cli.task) { |
Thierry FOURNIER | ffbf569 | 2016-12-16 11:14:06 +0100 | [diff] [blame] | 7944 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 7945 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7946 | } |
| 7947 | appctx->ctx.hlua_cli.task->nice = 0; |
| 7948 | appctx->ctx.hlua_cli.task->context = appctx; |
| 7949 | appctx->ctx.hlua_cli.task->process = hlua_applet_wakeup; |
| 7950 | |
| 7951 | /* Initialises the Lua context */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 7952 | 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] | 7953 | 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] | 7954 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7955 | } |
| 7956 | |
| 7957 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7958 | if (!SET_SAFE_LJMP(hlua)) { |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7959 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 7960 | error = lua_tostring(hlua->T, -1); |
| 7961 | else |
| 7962 | error = "critical error"; |
| 7963 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error); |
| 7964 | goto error; |
| 7965 | } |
| 7966 | |
| 7967 | /* Check stack available size. */ |
| 7968 | if (!lua_checkstack(hlua->T, 2)) { |
| 7969 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 7970 | goto error; |
| 7971 | } |
| 7972 | |
| 7973 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 7974 | 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] | 7975 | |
| 7976 | /* Once the arguments parsed, the CLI is like an AppletTCP, |
| 7977 | * so push AppletTCP in the stack. |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7978 | */ |
| 7979 | if (!hlua_applet_tcp_new(hlua->T, appctx)) { |
| 7980 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 7981 | goto error; |
| 7982 | } |
| 7983 | hlua->nargs = 1; |
| 7984 | |
| 7985 | /* push keywords in the stack. */ |
| 7986 | for (i = 0; *args[i]; i++) { |
| 7987 | /* Check stack available size. */ |
| 7988 | if (!lua_checkstack(hlua->T, 1)) { |
| 7989 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 7990 | goto error; |
| 7991 | } |
| 7992 | lua_pushstring(hlua->T, args[i]); |
| 7993 | hlua->nargs++; |
| 7994 | } |
| 7995 | |
| 7996 | /* We must initialize the execution timeouts. */ |
| 7997 | hlua->max_time = hlua_timeout_session; |
| 7998 | |
| 7999 | /* At this point the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8000 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8001 | |
| 8002 | /* It's ok */ |
| 8003 | return 0; |
| 8004 | |
| 8005 | /* It's not ok. */ |
| 8006 | error: |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8007 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8008 | hlua_ctx_destroy(hlua); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 8009 | appctx->ctx.hlua_cli.hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8010 | return 1; |
| 8011 | } |
| 8012 | |
| 8013 | static int hlua_cli_io_handler_fct(struct appctx *appctx) |
| 8014 | { |
| 8015 | struct hlua *hlua; |
| 8016 | struct stream_interface *si; |
| 8017 | struct hlua_function *fcn; |
| 8018 | |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 8019 | hlua = appctx->ctx.hlua_cli.hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8020 | si = appctx->owner; |
Willy Tarreau | 8ae4f75 | 2016-12-14 15:41:45 +0100 | [diff] [blame] | 8021 | fcn = appctx->ctx.hlua_cli.fcn; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8022 | |
| 8023 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 8024 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 8025 | return 1; |
| 8026 | |
| 8027 | /* Execute the function. */ |
| 8028 | switch (hlua_ctx_resume(hlua, 1)) { |
| 8029 | |
| 8030 | /* finished. */ |
| 8031 | case HLUA_E_OK: |
| 8032 | return 1; |
| 8033 | |
| 8034 | /* yield. */ |
| 8035 | case HLUA_E_AGAIN: |
| 8036 | /* We want write. */ |
| 8037 | if (HLUA_IS_WAKERESWR(hlua)) |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 8038 | si_rx_room_blk(si); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8039 | /* Set the timeout. */ |
| 8040 | if (hlua->wake_time != TICK_ETERNITY) |
| 8041 | task_schedule(hlua->task, hlua->wake_time); |
| 8042 | return 0; |
| 8043 | |
| 8044 | /* finished with error. */ |
| 8045 | case HLUA_E_ERRMSG: |
| 8046 | /* Display log. */ |
| 8047 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", |
| 8048 | fcn->name, lua_tostring(hlua->T, -1)); |
| 8049 | lua_pop(hlua->T, 1); |
| 8050 | return 1; |
| 8051 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 8052 | case HLUA_E_ETMOUT: |
| 8053 | SEND_ERR(NULL, "Lua converter '%s': execution timeout.\n", |
| 8054 | fcn->name); |
| 8055 | return 1; |
| 8056 | |
| 8057 | case HLUA_E_NOMEM: |
| 8058 | SEND_ERR(NULL, "Lua converter '%s': out of memory error.\n", |
| 8059 | fcn->name); |
| 8060 | return 1; |
| 8061 | |
| 8062 | case HLUA_E_YIELD: /* unexpected */ |
| 8063 | SEND_ERR(NULL, "Lua converter '%s': yield not allowed.\n", |
| 8064 | fcn->name); |
| 8065 | return 1; |
| 8066 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8067 | case HLUA_E_ERR: |
| 8068 | /* Display log. */ |
| 8069 | SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n", |
| 8070 | fcn->name); |
| 8071 | return 1; |
| 8072 | |
| 8073 | default: |
| 8074 | return 1; |
| 8075 | } |
| 8076 | |
| 8077 | return 1; |
| 8078 | } |
| 8079 | |
| 8080 | static void hlua_cli_io_release_fct(struct appctx *appctx) |
| 8081 | { |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 8082 | hlua_ctx_destroy(appctx->ctx.hlua_cli.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 8083 | appctx->ctx.hlua_cli.hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8084 | } |
| 8085 | |
| 8086 | /* This function is an LUA binding used for registering |
| 8087 | * new keywords in the cli. It expects a list of keywords |
| 8088 | * which are the "path". It is limited to 5 keywords. A |
| 8089 | * description of the command, a function to be executed |
| 8090 | * for the parsing and a function for io handlers. |
| 8091 | */ |
| 8092 | __LJMP static int hlua_register_cli(lua_State *L) |
| 8093 | { |
| 8094 | struct cli_kw_list *cli_kws; |
| 8095 | const char *message; |
| 8096 | int ref_io; |
| 8097 | int len; |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 8098 | struct hlua_function *fcn = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8099 | int index; |
| 8100 | int i; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8101 | struct buffer *trash; |
| 8102 | const char *kw[5]; |
| 8103 | struct cli_kw *cli_kw; |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 8104 | const char *errmsg; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8105 | |
| 8106 | MAY_LJMP(check_args(L, 3, "register_cli")); |
| 8107 | |
| 8108 | /* First argument : an array of maximum 5 keywords. */ |
| 8109 | if (!lua_istable(L, 1)) |
| 8110 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table")); |
| 8111 | |
| 8112 | /* Second argument : string with contextual message. */ |
| 8113 | message = MAY_LJMP(luaL_checkstring(L, 2)); |
| 8114 | |
| 8115 | /* Third and fourth argument : lua function. */ |
| 8116 | ref_io = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 8117 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8118 | /* Check for CLI service already registered */ |
| 8119 | trash = get_trash_chunk(); |
| 8120 | index = 0; |
| 8121 | lua_pushnil(L); |
| 8122 | memset(kw, 0, sizeof(kw)); |
| 8123 | while (lua_next(L, 1) != 0) { |
| 8124 | if (index >= CLI_PREFIX_KW_NB) |
| 8125 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries")); |
| 8126 | if (lua_type(L, -1) != LUA_TSTRING) |
| 8127 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings")); |
| 8128 | kw[index] = lua_tostring(L, -1); |
| 8129 | if (index == 0) |
| 8130 | chunk_printf(trash, "%s", kw[index]); |
| 8131 | else |
| 8132 | chunk_appendf(trash, " %s", kw[index]); |
| 8133 | index++; |
| 8134 | lua_pop(L, 1); |
| 8135 | } |
| 8136 | cli_kw = cli_find_kw_exact((char **)kw); |
| 8137 | if (cli_kw != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8138 | fcn = cli_kw->private; |
| 8139 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 8140 | ha_warning("Trying to register CLI keyword 'lua.%s' more than once. " |
| 8141 | "This will become a hard error in version 2.5.\n", trash->area); |
| 8142 | } |
| 8143 | fcn->function_ref[hlua_state_id] = ref_io; |
| 8144 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8145 | } |
| 8146 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8147 | /* Allocate and fill the sample fetch keyword struct. */ |
| 8148 | cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 8149 | if (!cli_kws) { |
| 8150 | errmsg = "Lua out of memory error."; |
| 8151 | goto error; |
| 8152 | } |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 8153 | fcn = new_hlua_function(); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 8154 | if (!fcn) { |
| 8155 | errmsg = "Lua out of memory error."; |
| 8156 | goto error; |
| 8157 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8158 | |
| 8159 | /* Fill path. */ |
| 8160 | index = 0; |
| 8161 | lua_pushnil(L); |
| 8162 | while(lua_next(L, 1) != 0) { |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 8163 | if (index >= 5) { |
| 8164 | errmsg = "1st argument must be a table with a maximum of 5 entries"; |
| 8165 | goto error; |
| 8166 | } |
| 8167 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 8168 | errmsg = "1st argument must be a table filled with strings"; |
| 8169 | goto error; |
| 8170 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8171 | cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1)); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 8172 | if (!cli_kws->kw[0].str_kw[index]) { |
| 8173 | errmsg = "Lua out of memory error."; |
| 8174 | goto error; |
| 8175 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8176 | index++; |
| 8177 | lua_pop(L, 1); |
| 8178 | } |
| 8179 | |
| 8180 | /* Copy help message. */ |
| 8181 | cli_kws->kw[0].usage = strdup(message); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 8182 | if (!cli_kws->kw[0].usage) { |
| 8183 | errmsg = "Lua out of memory error."; |
| 8184 | goto error; |
| 8185 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8186 | |
| 8187 | /* Fill fcn io handler. */ |
| 8188 | len = strlen("<lua.cli>") + 1; |
| 8189 | for (i = 0; i < index; i++) |
| 8190 | len += strlen(cli_kws->kw[0].str_kw[i]) + 1; |
| 8191 | fcn->name = calloc(1, len); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 8192 | if (!fcn->name) { |
| 8193 | errmsg = "Lua out of memory error."; |
| 8194 | goto error; |
| 8195 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8196 | strncat((char *)fcn->name, "<lua.cli", len); |
| 8197 | for (i = 0; i < index; i++) { |
| 8198 | strncat((char *)fcn->name, ".", len); |
| 8199 | strncat((char *)fcn->name, cli_kws->kw[0].str_kw[i], len); |
| 8200 | } |
| 8201 | strncat((char *)fcn->name, ">", len); |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8202 | fcn->function_ref[hlua_state_id] = ref_io; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8203 | |
| 8204 | /* Fill last entries. */ |
| 8205 | cli_kws->kw[0].private = fcn; |
| 8206 | cli_kws->kw[0].parse = hlua_cli_parse_fct; |
| 8207 | cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct; |
| 8208 | cli_kws->kw[0].io_release = hlua_cli_io_release_fct; |
| 8209 | |
| 8210 | /* Register this new converter */ |
| 8211 | cli_register_kw(cli_kws); |
| 8212 | |
| 8213 | return 0; |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 8214 | |
| 8215 | error: |
| 8216 | release_hlua_function(fcn); |
| 8217 | if (cli_kws) { |
| 8218 | for (i = 0; i < index; i++) |
| 8219 | ha_free((char **)&(cli_kws->kw[0].str_kw[i])); |
| 8220 | ha_free((char **)&(cli_kws->kw[0].usage)); |
| 8221 | } |
| 8222 | ha_free(&cli_kws); |
| 8223 | WILL_LJMP(luaL_error(L, errmsg)); |
| 8224 | return 0; /* Never reached */ |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8225 | } |
| 8226 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8227 | 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] | 8228 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8229 | char **err, unsigned int *timeout) |
| 8230 | { |
| 8231 | const char *error; |
| 8232 | |
| 8233 | error = parse_time_err(args[1], timeout, TIME_UNIT_MS); |
Willy Tarreau | 9faebe3 | 2019-06-07 19:00:37 +0200 | [diff] [blame] | 8234 | if (error == PARSE_TIME_OVER) { |
| 8235 | memprintf(err, "timer overflow in argument <%s> to <%s> (maximum value is 2147483647 ms or ~24.8 days)", |
| 8236 | args[1], args[0]); |
| 8237 | return -1; |
| 8238 | } |
| 8239 | else if (error == PARSE_TIME_UNDER) { |
| 8240 | memprintf(err, "timer underflow in argument <%s> to <%s> (minimum non-null value is 1 ms)", |
| 8241 | args[1], args[0]); |
| 8242 | return -1; |
| 8243 | } |
| 8244 | else if (error) { |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8245 | memprintf(err, "%s: invalid timeout", args[0]); |
| 8246 | return -1; |
| 8247 | } |
| 8248 | return 0; |
| 8249 | } |
| 8250 | |
| 8251 | 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] | 8252 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8253 | char **err) |
| 8254 | { |
| 8255 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 8256 | file, line, err, &hlua_timeout_session); |
| 8257 | } |
| 8258 | |
| 8259 | 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] | 8260 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8261 | char **err) |
| 8262 | { |
| 8263 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 8264 | file, line, err, &hlua_timeout_task); |
| 8265 | } |
| 8266 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8267 | 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] | 8268 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8269 | char **err) |
| 8270 | { |
| 8271 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 8272 | file, line, err, &hlua_timeout_applet); |
| 8273 | } |
| 8274 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 8275 | 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] | 8276 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 8277 | char **err) |
| 8278 | { |
| 8279 | char *error; |
| 8280 | |
| 8281 | hlua_nb_instruction = strtoll(args[1], &error, 10); |
| 8282 | if (*error != '\0') { |
| 8283 | memprintf(err, "%s: invalid number", args[0]); |
| 8284 | return -1; |
| 8285 | } |
| 8286 | return 0; |
| 8287 | } |
| 8288 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8289 | 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] | 8290 | const struct proxy *defpx, const char *file, int line, |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8291 | char **err) |
| 8292 | { |
| 8293 | char *error; |
| 8294 | |
| 8295 | if (*(args[1]) == 0) { |
| 8296 | memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]); |
| 8297 | return -1; |
| 8298 | } |
| 8299 | hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L; |
| 8300 | if (*error != '\0') { |
| 8301 | memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error); |
| 8302 | return -1; |
| 8303 | } |
| 8304 | return 0; |
| 8305 | } |
| 8306 | |
| 8307 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8308 | /* This function is called by the main configuration key "lua-load". It loads and |
| 8309 | * execute an lua file during the parsing of the HAProxy configuration file. It is |
| 8310 | * the main lua entry point. |
| 8311 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 8312 | * This function runs with the HAProxy keywords API. It returns -1 if an error |
| 8313 | * occurs, otherwise it returns 0. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8314 | * |
| 8315 | * In some error case, LUA set an error message in top of the stack. This function |
| 8316 | * 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] | 8317 | * |
| 8318 | * This function can fail with an abort() due to an Lua critical error. |
| 8319 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 8320 | * tolerated. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8321 | */ |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8322 | static int hlua_load_state(char *filename, lua_State *L, char **err) |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8323 | { |
| 8324 | int error; |
| 8325 | |
| 8326 | /* Just load and compile the file. */ |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8327 | error = luaL_loadfile(L, filename); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8328 | if (error) { |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8329 | memprintf(err, "error in Lua file '%s': %s", filename, lua_tostring(L, -1)); |
| 8330 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8331 | return -1; |
| 8332 | } |
| 8333 | |
| 8334 | /* If no syntax error where detected, execute the code. */ |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8335 | error = lua_pcall(L, 0, LUA_MULTRET, 0); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8336 | switch (error) { |
| 8337 | case LUA_OK: |
| 8338 | break; |
| 8339 | case LUA_ERRRUN: |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8340 | memprintf(err, "Lua runtime error: %s\n", lua_tostring(L, -1)); |
| 8341 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8342 | return -1; |
| 8343 | case LUA_ERRMEM: |
Thierry Fournier | de6145f | 2020-11-29 00:55:53 +0100 | [diff] [blame] | 8344 | memprintf(err, "Lua out of memory error\n"); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8345 | return -1; |
| 8346 | case LUA_ERRERR: |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8347 | memprintf(err, "Lua message handler error: %s\n", lua_tostring(L, -1)); |
| 8348 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8349 | return -1; |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 8350 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM <= 503 |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8351 | case LUA_ERRGCMM: |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8352 | memprintf(err, "Lua garbage collector error: %s\n", lua_tostring(L, -1)); |
| 8353 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8354 | return -1; |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 8355 | #endif |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8356 | default: |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8357 | memprintf(err, "Lua unknown error: %s\n", lua_tostring(L, -1)); |
| 8358 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8359 | return -1; |
| 8360 | } |
| 8361 | |
| 8362 | return 0; |
| 8363 | } |
| 8364 | |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8365 | static int hlua_load(char **args, int section_type, struct proxy *curpx, |
Willy Tarreau | 0182516 | 2021-03-09 09:53:46 +0100 | [diff] [blame] | 8366 | const struct proxy *defpx, const char *file, int line, |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8367 | char **err) |
| 8368 | { |
| 8369 | if (*(args[1]) == 0) { |
| 8370 | memprintf(err, "'%s' expects a file name as parameter.\n", args[0]); |
| 8371 | return -1; |
| 8372 | } |
| 8373 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8374 | /* loading for global state */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8375 | hlua_state_id = 0; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8376 | ha_set_tid(0); |
Thierry Fournier | afc63e2 | 2020-11-28 17:06:51 +0100 | [diff] [blame] | 8377 | return hlua_load_state(args[1], hlua_states[0], err); |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8378 | } |
| 8379 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8380 | 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] | 8381 | const struct proxy *defpx, const char *file, int line, |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8382 | char **err) |
| 8383 | { |
| 8384 | int len; |
| 8385 | |
| 8386 | if (*(args[1]) == 0) { |
| 8387 | memprintf(err, "'%s' expects a file as parameter.\n", args[0]); |
| 8388 | return -1; |
| 8389 | } |
| 8390 | |
| 8391 | if (per_thread_load == NULL) { |
| 8392 | /* allocate the first entry large enough to store the final NULL */ |
| 8393 | per_thread_load = calloc(1, sizeof(*per_thread_load)); |
| 8394 | if (per_thread_load == NULL) { |
| 8395 | memprintf(err, "out of memory error"); |
| 8396 | return -1; |
| 8397 | } |
| 8398 | } |
| 8399 | |
| 8400 | /* count used entries */ |
| 8401 | for (len = 0; per_thread_load[len] != NULL; len++) |
| 8402 | ; |
| 8403 | |
| 8404 | per_thread_load = realloc(per_thread_load, (len + 2) * sizeof(*per_thread_load)); |
| 8405 | if (per_thread_load == NULL) { |
| 8406 | memprintf(err, "out of memory error"); |
| 8407 | return -1; |
| 8408 | } |
| 8409 | |
| 8410 | per_thread_load[len] = strdup(args[1]); |
| 8411 | per_thread_load[len + 1] = NULL; |
| 8412 | |
| 8413 | if (per_thread_load[len] == NULL) { |
| 8414 | memprintf(err, "out of memory error"); |
| 8415 | return -1; |
| 8416 | } |
| 8417 | |
| 8418 | /* loading for thread 1 only */ |
| 8419 | hlua_state_id = 1; |
| 8420 | ha_set_tid(0); |
| 8421 | return hlua_load_state(args[1], hlua_states[1], err); |
| 8422 | } |
| 8423 | |
Tim Duesterhus | c9fc9f2 | 2020-01-12 13:55:39 +0100 | [diff] [blame] | 8424 | /* Prepend the given <path> followed by a semicolon to the `package.<type>` variable |
| 8425 | * in the given <ctx>. |
| 8426 | */ |
Thierry Fournier | 3fb9e51 | 2020-11-28 10:13:12 +0100 | [diff] [blame] | 8427 | static int hlua_prepend_path(lua_State *L, char *type, char *path) |
Tim Duesterhus | c9fc9f2 | 2020-01-12 13:55:39 +0100 | [diff] [blame] | 8428 | { |
Thierry Fournier | 3fb9e51 | 2020-11-28 10:13:12 +0100 | [diff] [blame] | 8429 | lua_getglobal(L, "package"); /* push package variable */ |
| 8430 | lua_pushstring(L, path); /* push given path */ |
| 8431 | lua_pushstring(L, ";"); /* push semicolon */ |
| 8432 | lua_getfield(L, -3, type); /* push old path */ |
| 8433 | lua_concat(L, 3); /* concatenate to new path */ |
| 8434 | lua_setfield(L, -2, type); /* store new path */ |
| 8435 | lua_pop(L, 1); /* pop package variable */ |
Tim Duesterhus | c9fc9f2 | 2020-01-12 13:55:39 +0100 | [diff] [blame] | 8436 | |
| 8437 | return 0; |
| 8438 | } |
| 8439 | |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8440 | 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] | 8441 | const struct proxy *defpx, const char *file, int line, |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8442 | char **err) |
| 8443 | { |
| 8444 | char *path; |
| 8445 | char *type = "path"; |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 8446 | struct prepend_path *p = NULL; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8447 | |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8448 | if (too_many_args(2, args, err, NULL)) { |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 8449 | goto err; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8450 | } |
| 8451 | |
| 8452 | if (!(*args[1])) { |
| 8453 | memprintf(err, "'%s' expects to receive a <path> as argument", args[0]); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 8454 | goto err; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8455 | } |
| 8456 | path = args[1]; |
| 8457 | |
| 8458 | if (*args[2]) { |
| 8459 | if (strcmp(args[2], "path") != 0 && strcmp(args[2], "cpath") != 0) { |
| 8460 | 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] | 8461 | goto err; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8462 | } |
| 8463 | type = args[2]; |
| 8464 | } |
| 8465 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8466 | p = calloc(1, sizeof(*p)); |
| 8467 | if (p == NULL) { |
Tim Duesterhus | f89d43a | 2021-01-03 20:04:37 +0100 | [diff] [blame] | 8468 | memprintf(err, "memory allocation failed"); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 8469 | goto err; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8470 | } |
| 8471 | p->path = strdup(path); |
| 8472 | if (p->path == NULL) { |
Tim Duesterhus | f89d43a | 2021-01-03 20:04:37 +0100 | [diff] [blame] | 8473 | memprintf(err, "memory allocation failed"); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 8474 | goto err2; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8475 | } |
| 8476 | p->type = strdup(type); |
| 8477 | if (p->type == NULL) { |
Tim Duesterhus | f89d43a | 2021-01-03 20:04:37 +0100 | [diff] [blame] | 8478 | memprintf(err, "memory allocation failed"); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 8479 | goto err2; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8480 | } |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 8481 | LIST_APPEND(&prepend_path_list, &p->l); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8482 | |
| 8483 | hlua_prepend_path(hlua_states[0], type, path); |
| 8484 | hlua_prepend_path(hlua_states[1], type, path); |
| 8485 | return 0; |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 8486 | |
| 8487 | err2: |
| 8488 | free(p->type); |
| 8489 | free(p->path); |
| 8490 | err: |
| 8491 | free(p); |
| 8492 | return -1; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8493 | } |
| 8494 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8495 | /* configuration keywords declaration */ |
| 8496 | static struct cfg_kw_list cfg_kws = {{ },{ |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8497 | { CFG_GLOBAL, "lua-prepend-path", hlua_config_prepend_path }, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8498 | { CFG_GLOBAL, "lua-load", hlua_load }, |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8499 | { CFG_GLOBAL, "lua-load-per-thread", hlua_load_per_thread }, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8500 | { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout }, |
| 8501 | { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout }, |
Thierry FOURNIER | 56da101 | 2015-10-01 08:42:31 +0200 | [diff] [blame] | 8502 | { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout }, |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 8503 | { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield }, |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8504 | { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem }, |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8505 | { 0, NULL, NULL }, |
| 8506 | }}; |
| 8507 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 8508 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |
| 8509 | |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 8510 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8511 | /* This function can fail with an abort() due to an Lua critical error. |
| 8512 | * We are in the initialisation process of HAProxy, this abort() is |
| 8513 | * tolerated. |
| 8514 | */ |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8515 | int hlua_post_init_state(lua_State *L) |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8516 | { |
| 8517 | struct hlua_init_function *init; |
| 8518 | const char *msg; |
| 8519 | enum hlua_exec ret; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8520 | const char *error; |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8521 | const char *kind; |
| 8522 | const char *trace; |
| 8523 | int return_status = 1; |
| 8524 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
| 8525 | int nres; |
| 8526 | #endif |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8527 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 8528 | /* disable memory limit checks if limit is not set */ |
| 8529 | if (!hlua_global_allocator.limit) |
| 8530 | hlua_global_allocator.limit = ~hlua_global_allocator.limit; |
| 8531 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 8532 | /* Call post initialisation function in safe environment. */ |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 8533 | if (setjmp(safe_ljmp_env) != 0) { |
| 8534 | lua_atpanic(L, hlua_panic_safe); |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8535 | if (lua_type(L, -1) == LUA_TSTRING) |
| 8536 | error = lua_tostring(L, -1); |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 8537 | else |
| 8538 | error = "critical error"; |
| 8539 | fprintf(stderr, "Lua post-init: %s.\n", error); |
| 8540 | exit(1); |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 8541 | } else { |
| 8542 | lua_atpanic(L, hlua_panic_ljmp); |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 8543 | } |
Frédéric Lécaille | 54f2bcf | 2018-08-29 13:46:24 +0200 | [diff] [blame] | 8544 | |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8545 | hlua_fcn_post_init(L); |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 8546 | |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8547 | list_for_each_entry(init, &hlua_init_functions[hlua_state_id], l) { |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8548 | lua_rawgeti(L, LUA_REGISTRYINDEX, init->function_ref); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8549 | |
| 8550 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8551 | ret = lua_resume(L, L, 0, &nres); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8552 | #else |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8553 | ret = lua_resume(L, L, 0); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8554 | #endif |
| 8555 | kind = NULL; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8556 | switch (ret) { |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8557 | |
| 8558 | case LUA_OK: |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8559 | lua_pop(L, -1); |
Thierry Fournier | 13d08b7 | 2020-11-28 11:02:58 +0100 | [diff] [blame] | 8560 | break; |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8561 | |
| 8562 | case LUA_ERRERR: |
| 8563 | kind = "message handler error"; |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 8564 | /* Fall through */ |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8565 | case LUA_ERRRUN: |
| 8566 | if (!kind) |
| 8567 | kind = "runtime error"; |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8568 | msg = lua_tostring(L, -1); |
| 8569 | lua_settop(L, 0); /* Empty the stack. */ |
| 8570 | lua_pop(L, 1); |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 8571 | trace = hlua_traceback(L, ", "); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8572 | if (msg) |
| 8573 | ha_alert("Lua init: %s: '%s' from %s\n", kind, msg, trace); |
| 8574 | else |
| 8575 | ha_alert("Lua init: unknown %s from %s\n", kind, trace); |
| 8576 | return_status = 0; |
| 8577 | break; |
| 8578 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8579 | default: |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8580 | /* Unknown error */ |
| 8581 | kind = "Unknown error"; |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 8582 | /* Fall through */ |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8583 | case LUA_YIELD: |
| 8584 | /* yield is not configured at this step, this state doesn't happen */ |
| 8585 | if (!kind) |
| 8586 | kind = "yield not allowed"; |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 8587 | /* Fall through */ |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8588 | case LUA_ERRMEM: |
| 8589 | if (!kind) |
| 8590 | kind = "out of memory error"; |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8591 | lua_settop(L, 0); |
| 8592 | lua_pop(L, 1); |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 8593 | trace = hlua_traceback(L, ", "); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8594 | ha_alert("Lua init: %s: %s\n", kind, trace); |
| 8595 | return_status = 0; |
| 8596 | break; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8597 | } |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8598 | if (!return_status) |
| 8599 | break; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8600 | } |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 8601 | |
| 8602 | lua_atpanic(L, hlua_panic_safe); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8603 | return return_status; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8604 | } |
| 8605 | |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8606 | int hlua_post_init() |
| 8607 | { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8608 | int ret; |
| 8609 | int i; |
| 8610 | int errors; |
| 8611 | char *err = NULL; |
| 8612 | struct hlua_function *fcn; |
| 8613 | |
Willy Tarreau | 42afc59 | 2021-08-30 09:35:18 +0200 | [diff] [blame] | 8614 | #if defined(USE_OPENSSL) |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8615 | /* Initialize SSL server. */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 8616 | if (socket_ssl->xprt->prepare_srv) { |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8617 | int saved_used_backed = global.ssl_used_backend; |
| 8618 | // don't affect maxconn automatic computation |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 8619 | socket_ssl->xprt->prepare_srv(socket_ssl); |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8620 | global.ssl_used_backend = saved_used_backed; |
| 8621 | } |
| 8622 | #endif |
| 8623 | |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8624 | /* Perform post init of common thread */ |
| 8625 | hlua_state_id = 0; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8626 | ha_set_tid(0); |
| 8627 | ret = hlua_post_init_state(hlua_states[hlua_state_id]); |
| 8628 | if (ret == 0) |
| 8629 | return 0; |
| 8630 | |
| 8631 | /* init remaining lua states and load files */ |
| 8632 | for (hlua_state_id = 2; hlua_state_id < global.nbthread + 1; hlua_state_id++) { |
| 8633 | |
| 8634 | /* set thread context */ |
| 8635 | ha_set_tid(hlua_state_id - 1); |
| 8636 | |
| 8637 | /* Init lua state */ |
| 8638 | hlua_states[hlua_state_id] = hlua_init_state(hlua_state_id); |
| 8639 | |
| 8640 | /* Load lua files */ |
| 8641 | for (i = 0; per_thread_load && per_thread_load[i]; i++) { |
| 8642 | ret = hlua_load_state(per_thread_load[i], hlua_states[hlua_state_id], &err); |
| 8643 | if (ret != 0) { |
| 8644 | ha_alert("Lua init: %s\n", err); |
| 8645 | return 0; |
| 8646 | } |
| 8647 | } |
| 8648 | } |
| 8649 | |
| 8650 | /* Reset thread context */ |
| 8651 | ha_set_tid(0); |
| 8652 | |
| 8653 | /* Execute post init for all states */ |
| 8654 | for (hlua_state_id = 1; hlua_state_id < global.nbthread + 1; hlua_state_id++) { |
| 8655 | |
| 8656 | /* set thread context */ |
| 8657 | ha_set_tid(hlua_state_id - 1); |
| 8658 | |
| 8659 | /* run post init */ |
| 8660 | ret = hlua_post_init_state(hlua_states[hlua_state_id]); |
| 8661 | if (ret == 0) |
| 8662 | return 0; |
| 8663 | } |
| 8664 | |
| 8665 | /* Reset thread context */ |
| 8666 | ha_set_tid(0); |
| 8667 | |
| 8668 | /* control functions registering. Each function must have: |
| 8669 | * - only the function_ref[0] set positive and all other to -1 |
| 8670 | * - only the function_ref[0] set to -1 and all other positive |
| 8671 | * This ensure a same reference is not used both in shared |
| 8672 | * lua state and thread dedicated lua state. Note: is the case |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 8673 | * reach, the shared state is priority, but the bug will be |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8674 | * complicated to found for the end user. |
| 8675 | */ |
| 8676 | errors = 0; |
| 8677 | list_for_each_entry(fcn, &referenced_functions, l) { |
| 8678 | ret = 0; |
| 8679 | for (i = 1; i < global.nbthread + 1; i++) { |
| 8680 | if (fcn->function_ref[i] == -1) |
| 8681 | ret--; |
| 8682 | else |
| 8683 | ret++; |
| 8684 | } |
| 8685 | if (abs(ret) != global.nbthread) { |
| 8686 | ha_alert("Lua function '%s' is not referenced in all thread. " |
| 8687 | "Expect function in all thread or in none thread.\n", fcn->name); |
| 8688 | errors++; |
| 8689 | continue; |
| 8690 | } |
| 8691 | |
| 8692 | if ((fcn->function_ref[0] == -1) == (ret < 0)) { |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 8693 | ha_alert("Lua function '%s' is referenced both ins shared Lua context (through lua-load) " |
| 8694 | "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] | 8695 | "exclusive.\n", fcn->name); |
| 8696 | errors++; |
| 8697 | } |
| 8698 | } |
| 8699 | |
| 8700 | if (errors > 0) |
| 8701 | return 0; |
| 8702 | |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 8703 | /* 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] | 8704 | * -1 in order to have probably a segfault if someone use it |
| 8705 | */ |
| 8706 | hlua_state_id = -1; |
| 8707 | |
| 8708 | return 1; |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8709 | } |
| 8710 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8711 | /* The memory allocator used by the Lua stack. <ud> is a pointer to the |
| 8712 | * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize> |
| 8713 | * 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] | 8714 | * allocation. <nsize> is the requested new size. A new allocation is |
| 8715 | * indicated by <ptr> being NULL. A free is indicated by <nsize> being |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 8716 | * zero. This one verifies that the limits are respected but is optimized |
| 8717 | * for the fast case where limits are not used, hence stats are not updated. |
Willy Tarreau | e00ad05 | 2021-10-22 16:00:02 +0200 | [diff] [blame] | 8718 | * |
| 8719 | * Warning: while this API ressembles glibc's realloc() a lot, glibc surpasses |
| 8720 | * POSIX by making realloc(ptr,0) an effective free(), but others do not do |
| 8721 | * that and will simply allocate zero as if it were the result of malloc(0), |
| 8722 | * so mapping this onto realloc() will lead to memory leaks on non-glibc |
| 8723 | * systems. |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8724 | */ |
| 8725 | static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize) |
| 8726 | { |
| 8727 | struct hlua_mem_allocator *zone = ud; |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 8728 | size_t limit, old, new; |
| 8729 | |
Tim Duesterhus | 2258652 | 2021-01-08 10:35:33 +0100 | [diff] [blame] | 8730 | if (unlikely(!ptr && !nsize)) |
| 8731 | return NULL; |
| 8732 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 8733 | /* a limit of ~0 means unlimited and boot complete, so there's no need |
| 8734 | * for accounting anymore. |
| 8735 | */ |
Willy Tarreau | e00ad05 | 2021-10-22 16:00:02 +0200 | [diff] [blame] | 8736 | if (likely(~zone->limit == 0)) { |
| 8737 | if (!nsize) |
| 8738 | ha_free(&ptr); |
| 8739 | else |
| 8740 | ptr = realloc(ptr, nsize); |
| 8741 | return ptr; |
| 8742 | } |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8743 | |
Willy Tarreau | d36c7fa | 2020-12-02 12:26:29 +0100 | [diff] [blame] | 8744 | if (!ptr) |
| 8745 | osize = 0; |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8746 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 8747 | /* enforce strict limits across all threads */ |
| 8748 | limit = zone->limit; |
| 8749 | old = _HA_ATOMIC_LOAD(&zone->allocated); |
| 8750 | do { |
| 8751 | new = old + nsize - osize; |
| 8752 | if (unlikely(nsize && limit && new > limit)) |
| 8753 | return NULL; |
| 8754 | } while (!_HA_ATOMIC_CAS(&zone->allocated, &old, new)); |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8755 | |
Willy Tarreau | e00ad05 | 2021-10-22 16:00:02 +0200 | [diff] [blame] | 8756 | if (!nsize) |
| 8757 | ha_free(&ptr); |
| 8758 | else |
| 8759 | ptr = realloc(ptr, nsize); |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 8760 | |
| 8761 | if (unlikely(!ptr && nsize)) // failed |
| 8762 | _HA_ATOMIC_SUB(&zone->allocated, nsize - osize); |
| 8763 | |
| 8764 | __ha_barrier_atomic_store(); |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8765 | return ptr; |
| 8766 | } |
| 8767 | |
Thierry Fournier | ecb83c2 | 2020-11-28 15:49:44 +0100 | [diff] [blame] | 8768 | /* 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] | 8769 | * We are in the initialisation process of HAProxy, this abort() is |
| 8770 | * tolerated. |
| 8771 | */ |
Thierry Fournier | ecb83c2 | 2020-11-28 15:49:44 +0100 | [diff] [blame] | 8772 | lua_State *hlua_init_state(int thread_num) |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 8773 | { |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8774 | int i; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8775 | int idx; |
| 8776 | struct sample_fetch *sf; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8777 | struct sample_conv *sc; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8778 | char *p; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8779 | const char *error_msg; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 8780 | void **context; |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8781 | lua_State *L; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8782 | struct prepend_path *pp; |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8783 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 8784 | /* Init main lua stack. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8785 | L = lua_newstate(hlua_alloc, &hlua_global_allocator); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 8786 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 8787 | /* Initialise Lua context to NULL */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8788 | context = lua_getextraspace(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 8789 | *context = NULL; |
| 8790 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 8791 | /* From this point, until the end of the initialisation function, |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8792 | * the Lua function can fail with an abort. We are in the initialisation |
| 8793 | * process of HAProxy, this abort() is tolerated. |
| 8794 | */ |
| 8795 | |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 8796 | /* Call post initialisation function in safe environment. */ |
| 8797 | if (setjmp(safe_ljmp_env) != 0) { |
| 8798 | lua_atpanic(L, hlua_panic_safe); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8799 | if (lua_type(L, -1) == LUA_TSTRING) |
| 8800 | error_msg = lua_tostring(L, -1); |
Thierry Fournier | 2f05cc6 | 2020-11-28 16:08:02 +0100 | [diff] [blame] | 8801 | else |
| 8802 | error_msg = "critical error"; |
| 8803 | fprintf(stderr, "Lua init: %s.\n", error_msg); |
| 8804 | exit(1); |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 8805 | } else { |
| 8806 | lua_atpanic(L, hlua_panic_ljmp); |
Thierry Fournier | 2f05cc6 | 2020-11-28 16:08:02 +0100 | [diff] [blame] | 8807 | } |
| 8808 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 8809 | /* Initialise lua. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8810 | luaL_openlibs(L); |
Tim Duesterhus | 541fe1e | 2020-01-12 13:55:41 +0100 | [diff] [blame] | 8811 | #define HLUA_PREPEND_PATH_TOSTRING1(x) #x |
| 8812 | #define HLUA_PREPEND_PATH_TOSTRING(x) HLUA_PREPEND_PATH_TOSTRING1(x) |
| 8813 | #ifdef HLUA_PREPEND_PATH |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8814 | hlua_prepend_path(L, "path", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_PATH)); |
Tim Duesterhus | 541fe1e | 2020-01-12 13:55:41 +0100 | [diff] [blame] | 8815 | #endif |
| 8816 | #ifdef HLUA_PREPEND_CPATH |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8817 | hlua_prepend_path(L, "cpath", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_CPATH)); |
Tim Duesterhus | 541fe1e | 2020-01-12 13:55:41 +0100 | [diff] [blame] | 8818 | #endif |
| 8819 | #undef HLUA_PREPEND_PATH_TOSTRING |
| 8820 | #undef HLUA_PREPEND_PATH_TOSTRING1 |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8821 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8822 | /* Apply configured prepend path */ |
| 8823 | list_for_each_entry(pp, &prepend_path_list, l) |
| 8824 | hlua_prepend_path(L, pp->type, pp->path); |
| 8825 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8826 | /* |
| 8827 | * |
| 8828 | * Create "core" object. |
| 8829 | * |
| 8830 | */ |
| 8831 | |
Thierry FOURNIER | a2d8c65 | 2015-03-11 17:29:39 +0100 | [diff] [blame] | 8832 | /* This table entry is the object "core" base. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8833 | lua_newtable(L); |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8834 | |
Thierry Fournier | ecb83c2 | 2020-11-28 15:49:44 +0100 | [diff] [blame] | 8835 | /* set the thread id */ |
| 8836 | hlua_class_const_int(L, "thread", thread_num); |
| 8837 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8838 | /* Push the loglevel constants. */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8839 | for (i = 0; i < NB_LOG_LEVELS; i++) |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8840 | hlua_class_const_int(L, log_levels[i], i); |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8841 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8842 | /* Register special functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8843 | hlua_class_function(L, "register_init", hlua_register_init); |
| 8844 | hlua_class_function(L, "register_task", hlua_register_task); |
| 8845 | hlua_class_function(L, "register_fetches", hlua_register_fetches); |
| 8846 | hlua_class_function(L, "register_converters", hlua_register_converters); |
| 8847 | hlua_class_function(L, "register_action", hlua_register_action); |
| 8848 | hlua_class_function(L, "register_service", hlua_register_service); |
| 8849 | hlua_class_function(L, "register_cli", hlua_register_cli); |
| 8850 | hlua_class_function(L, "yield", hlua_yield); |
| 8851 | hlua_class_function(L, "set_nice", hlua_set_nice); |
| 8852 | hlua_class_function(L, "sleep", hlua_sleep); |
| 8853 | hlua_class_function(L, "msleep", hlua_msleep); |
| 8854 | hlua_class_function(L, "add_acl", hlua_add_acl); |
| 8855 | hlua_class_function(L, "del_acl", hlua_del_acl); |
| 8856 | hlua_class_function(L, "set_map", hlua_set_map); |
| 8857 | hlua_class_function(L, "del_map", hlua_del_map); |
| 8858 | hlua_class_function(L, "tcp", hlua_socket_new); |
| 8859 | hlua_class_function(L, "log", hlua_log); |
| 8860 | hlua_class_function(L, "Debug", hlua_log_debug); |
| 8861 | hlua_class_function(L, "Info", hlua_log_info); |
| 8862 | hlua_class_function(L, "Warning", hlua_log_warning); |
| 8863 | hlua_class_function(L, "Alert", hlua_log_alert); |
| 8864 | hlua_class_function(L, "done", hlua_done); |
| 8865 | hlua_fcn_reg_core_fcn(L); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8866 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8867 | lua_setglobal(L, "core"); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 8868 | |
| 8869 | /* |
| 8870 | * |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 8871 | * Create "act" object. |
| 8872 | * |
| 8873 | */ |
| 8874 | |
| 8875 | /* This table entry is the object "act" base. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8876 | lua_newtable(L); |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 8877 | |
| 8878 | /* push action return constants */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8879 | hlua_class_const_int(L, "CONTINUE", ACT_RET_CONT); |
| 8880 | hlua_class_const_int(L, "STOP", ACT_RET_STOP); |
| 8881 | hlua_class_const_int(L, "YIELD", ACT_RET_YIELD); |
| 8882 | hlua_class_const_int(L, "ERROR", ACT_RET_ERR); |
| 8883 | hlua_class_const_int(L, "DONE", ACT_RET_DONE); |
| 8884 | hlua_class_const_int(L, "DENY", ACT_RET_DENY); |
| 8885 | hlua_class_const_int(L, "ABORT", ACT_RET_ABRT); |
| 8886 | hlua_class_const_int(L, "INVALID", ACT_RET_INV); |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 8887 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8888 | hlua_class_function(L, "wake_time", hlua_set_wake_time); |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 8889 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8890 | lua_setglobal(L, "act"); |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 8891 | |
| 8892 | /* |
| 8893 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8894 | * Register class Map |
| 8895 | * |
| 8896 | */ |
| 8897 | |
| 8898 | /* This table entry is the object "Map" base. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8899 | lua_newtable(L); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8900 | |
| 8901 | /* register pattern types. */ |
| 8902 | for (i=0; i<PAT_MATCH_NUM; i++) |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8903 | hlua_class_const_int(L, pat_match_names[i], i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 8904 | for (i=0; i<PAT_MATCH_NUM; i++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8905 | snprintf(trash.area, trash.size, "_%s", pat_match_names[i]); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8906 | hlua_class_const_int(L, trash.area, i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 8907 | } |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8908 | |
| 8909 | /* register constructor. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8910 | hlua_class_function(L, "new", hlua_map_new); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8911 | |
| 8912 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8913 | lua_newtable(L); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8914 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8915 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8916 | lua_pushstring(L, "__index"); |
| 8917 | lua_newtable(L); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8918 | |
| 8919 | /* Register . */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8920 | hlua_class_function(L, "lookup", hlua_map_lookup); |
| 8921 | hlua_class_function(L, "slookup", hlua_map_slookup); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8922 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8923 | lua_rawset(L, -3); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8924 | |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8925 | /* Register previous table in the registry with reference and named entry. |
| 8926 | * The function hlua_register_metatable() pops the stack, so we |
| 8927 | * previously create a copy of the table. |
| 8928 | */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8929 | lua_pushvalue(L, -1); /* Copy the -1 entry and push it on the stack. */ |
| 8930 | class_map_ref = hlua_register_metatable(L, CLASS_MAP); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8931 | |
| 8932 | /* Assign the metatable to the mai Map object. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8933 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8934 | |
| 8935 | /* Set a name to the table. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8936 | lua_setglobal(L, "Map"); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8937 | |
| 8938 | /* |
| 8939 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8940 | * Register class Channel |
| 8941 | * |
| 8942 | */ |
| 8943 | |
| 8944 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8945 | lua_newtable(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8946 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8947 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8948 | lua_pushstring(L, "__index"); |
| 8949 | lua_newtable(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8950 | |
| 8951 | /* Register . */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8952 | hlua_class_function(L, "get", hlua_channel_get); |
| 8953 | hlua_class_function(L, "dup", hlua_channel_dup); |
| 8954 | hlua_class_function(L, "getline", hlua_channel_getline); |
| 8955 | hlua_class_function(L, "set", hlua_channel_set); |
| 8956 | hlua_class_function(L, "append", hlua_channel_append); |
| 8957 | hlua_class_function(L, "send", hlua_channel_send); |
| 8958 | hlua_class_function(L, "forward", hlua_channel_forward); |
| 8959 | hlua_class_function(L, "get_in_len", hlua_channel_get_in_len); |
| 8960 | hlua_class_function(L, "get_out_len", hlua_channel_get_out_len); |
| 8961 | hlua_class_function(L, "is_full", hlua_channel_is_full); |
| 8962 | hlua_class_function(L, "is_resp", hlua_channel_is_resp); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8963 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8964 | lua_rawset(L, -3); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8965 | |
| 8966 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8967 | class_channel_ref = hlua_register_metatable(L, CLASS_CHANNEL); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8968 | |
| 8969 | /* |
| 8970 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8971 | * Register class Fetches |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 8972 | * |
| 8973 | */ |
| 8974 | |
| 8975 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8976 | lua_newtable(L); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 8977 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8978 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8979 | lua_pushstring(L, "__index"); |
| 8980 | lua_newtable(L); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 8981 | |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8982 | /* Browse existing fetches and create the associated |
| 8983 | * object method. |
| 8984 | */ |
| 8985 | sf = NULL; |
| 8986 | while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) { |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8987 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 8988 | * by an underscore. |
| 8989 | */ |
Willy Tarreau | fa92f36 | 2021-08-26 16:48:53 +0200 | [diff] [blame] | 8990 | strlcpy2(trash.area, sf->kw, trash.size); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8991 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8992 | if (*p == '.' || *p == '-' || *p == '+') |
| 8993 | *p = '_'; |
| 8994 | |
| 8995 | /* Register the function. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8996 | lua_pushstring(L, trash.area); |
| 8997 | lua_pushlightuserdata(L, sf); |
| 8998 | lua_pushcclosure(L, hlua_run_sample_fetch, 1); |
| 8999 | lua_rawset(L, -3); |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 9000 | } |
| 9001 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9002 | lua_rawset(L, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 9003 | |
| 9004 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9005 | class_fetches_ref = hlua_register_metatable(L, CLASS_FETCHES); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 9006 | |
| 9007 | /* |
| 9008 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 9009 | * Register class Converters |
| 9010 | * |
| 9011 | */ |
| 9012 | |
| 9013 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9014 | lua_newtable(L); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 9015 | |
| 9016 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9017 | lua_pushstring(L, "__index"); |
| 9018 | lua_newtable(L); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 9019 | |
| 9020 | /* Browse existing converters and create the associated |
| 9021 | * object method. |
| 9022 | */ |
| 9023 | sc = NULL; |
| 9024 | while ((sc = sample_conv_getnext(sc, &idx)) != NULL) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 9025 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 9026 | * by an underscore. |
| 9027 | */ |
Willy Tarreau | fa92f36 | 2021-08-26 16:48:53 +0200 | [diff] [blame] | 9028 | strlcpy2(trash.area, sc->kw, trash.size); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 9029 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 9030 | if (*p == '.' || *p == '-' || *p == '+') |
| 9031 | *p = '_'; |
| 9032 | |
| 9033 | /* Register the function. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9034 | lua_pushstring(L, trash.area); |
| 9035 | lua_pushlightuserdata(L, sc); |
| 9036 | lua_pushcclosure(L, hlua_run_sample_conv, 1); |
| 9037 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 9038 | } |
| 9039 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9040 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 9041 | |
| 9042 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9043 | class_converters_ref = hlua_register_metatable(L, CLASS_CONVERTERS); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 9044 | |
| 9045 | /* |
| 9046 | * |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 9047 | * Register class HTTP |
| 9048 | * |
| 9049 | */ |
| 9050 | |
| 9051 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9052 | lua_newtable(L); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 9053 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 9054 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9055 | lua_pushstring(L, "__index"); |
| 9056 | lua_newtable(L); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 9057 | |
| 9058 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9059 | hlua_class_function(L, "req_get_headers",hlua_http_req_get_headers); |
| 9060 | hlua_class_function(L, "req_del_header", hlua_http_req_del_hdr); |
| 9061 | hlua_class_function(L, "req_rep_header", hlua_http_req_rep_hdr); |
| 9062 | hlua_class_function(L, "req_rep_value", hlua_http_req_rep_val); |
| 9063 | hlua_class_function(L, "req_add_header", hlua_http_req_add_hdr); |
| 9064 | hlua_class_function(L, "req_set_header", hlua_http_req_set_hdr); |
| 9065 | hlua_class_function(L, "req_set_method", hlua_http_req_set_meth); |
| 9066 | hlua_class_function(L, "req_set_path", hlua_http_req_set_path); |
| 9067 | hlua_class_function(L, "req_set_query", hlua_http_req_set_query); |
| 9068 | hlua_class_function(L, "req_set_uri", hlua_http_req_set_uri); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 9069 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9070 | hlua_class_function(L, "res_get_headers",hlua_http_res_get_headers); |
| 9071 | hlua_class_function(L, "res_del_header", hlua_http_res_del_hdr); |
| 9072 | hlua_class_function(L, "res_rep_header", hlua_http_res_rep_hdr); |
| 9073 | hlua_class_function(L, "res_rep_value", hlua_http_res_rep_val); |
| 9074 | hlua_class_function(L, "res_add_header", hlua_http_res_add_hdr); |
| 9075 | hlua_class_function(L, "res_set_header", hlua_http_res_set_hdr); |
| 9076 | hlua_class_function(L, "res_set_status", hlua_http_res_set_status); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 9077 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9078 | lua_rawset(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 9079 | |
| 9080 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9081 | class_http_ref = hlua_register_metatable(L, CLASS_HTTP); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 9082 | |
| 9083 | /* |
| 9084 | * |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9085 | * Register class AppletTCP |
| 9086 | * |
| 9087 | */ |
| 9088 | |
| 9089 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9090 | lua_newtable(L); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9091 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 9092 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9093 | lua_pushstring(L, "__index"); |
| 9094 | lua_newtable(L); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9095 | |
| 9096 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9097 | hlua_class_function(L, "getline", hlua_applet_tcp_getline); |
| 9098 | hlua_class_function(L, "receive", hlua_applet_tcp_recv); |
| 9099 | hlua_class_function(L, "send", hlua_applet_tcp_send); |
| 9100 | hlua_class_function(L, "set_priv", hlua_applet_tcp_set_priv); |
| 9101 | hlua_class_function(L, "get_priv", hlua_applet_tcp_get_priv); |
| 9102 | hlua_class_function(L, "set_var", hlua_applet_tcp_set_var); |
| 9103 | hlua_class_function(L, "unset_var", hlua_applet_tcp_unset_var); |
| 9104 | hlua_class_function(L, "get_var", hlua_applet_tcp_get_var); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9105 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9106 | lua_settable(L, -3); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9107 | |
| 9108 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9109 | class_applet_tcp_ref = hlua_register_metatable(L, CLASS_APPLET_TCP); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9110 | |
| 9111 | /* |
| 9112 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9113 | * Register class AppletHTTP |
| 9114 | * |
| 9115 | */ |
| 9116 | |
| 9117 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9118 | lua_newtable(L); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9119 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 9120 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9121 | lua_pushstring(L, "__index"); |
| 9122 | lua_newtable(L); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9123 | |
| 9124 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9125 | hlua_class_function(L, "set_priv", hlua_applet_http_set_priv); |
| 9126 | hlua_class_function(L, "get_priv", hlua_applet_http_get_priv); |
| 9127 | hlua_class_function(L, "set_var", hlua_applet_http_set_var); |
| 9128 | hlua_class_function(L, "unset_var", hlua_applet_http_unset_var); |
| 9129 | hlua_class_function(L, "get_var", hlua_applet_http_get_var); |
| 9130 | hlua_class_function(L, "getline", hlua_applet_http_getline); |
| 9131 | hlua_class_function(L, "receive", hlua_applet_http_recv); |
| 9132 | hlua_class_function(L, "send", hlua_applet_http_send); |
| 9133 | hlua_class_function(L, "add_header", hlua_applet_http_addheader); |
| 9134 | hlua_class_function(L, "set_status", hlua_applet_http_status); |
| 9135 | hlua_class_function(L, "start_response", hlua_applet_http_start_response); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9136 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9137 | lua_settable(L, -3); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9138 | |
| 9139 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9140 | class_applet_http_ref = hlua_register_metatable(L, CLASS_APPLET_HTTP); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9141 | |
| 9142 | /* |
| 9143 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 9144 | * Register class TXN |
| 9145 | * |
| 9146 | */ |
| 9147 | |
| 9148 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9149 | lua_newtable(L); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 9150 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 9151 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9152 | lua_pushstring(L, "__index"); |
| 9153 | lua_newtable(L); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 9154 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 9155 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9156 | hlua_class_function(L, "set_priv", hlua_set_priv); |
| 9157 | hlua_class_function(L, "get_priv", hlua_get_priv); |
| 9158 | hlua_class_function(L, "set_var", hlua_set_var); |
| 9159 | hlua_class_function(L, "unset_var", hlua_unset_var); |
| 9160 | hlua_class_function(L, "get_var", hlua_get_var); |
| 9161 | hlua_class_function(L, "done", hlua_txn_done); |
| 9162 | hlua_class_function(L, "reply", hlua_txn_reply_new); |
| 9163 | hlua_class_function(L, "set_loglevel", hlua_txn_set_loglevel); |
| 9164 | hlua_class_function(L, "set_tos", hlua_txn_set_tos); |
| 9165 | hlua_class_function(L, "set_mark", hlua_txn_set_mark); |
| 9166 | hlua_class_function(L, "set_priority_class", hlua_txn_set_priority_class); |
| 9167 | hlua_class_function(L, "set_priority_offset", hlua_txn_set_priority_offset); |
| 9168 | hlua_class_function(L, "deflog", hlua_txn_deflog); |
| 9169 | hlua_class_function(L, "log", hlua_txn_log); |
| 9170 | hlua_class_function(L, "Debug", hlua_txn_log_debug); |
| 9171 | hlua_class_function(L, "Info", hlua_txn_log_info); |
| 9172 | hlua_class_function(L, "Warning", hlua_txn_log_warning); |
| 9173 | hlua_class_function(L, "Alert", hlua_txn_log_alert); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 9174 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9175 | lua_rawset(L, -3); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 9176 | |
| 9177 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9178 | class_txn_ref = hlua_register_metatable(L, CLASS_TXN); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9179 | |
| 9180 | /* |
| 9181 | * |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 9182 | * Register class reply |
| 9183 | * |
| 9184 | */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9185 | lua_newtable(L); |
| 9186 | lua_pushstring(L, "__index"); |
| 9187 | lua_newtable(L); |
| 9188 | hlua_class_function(L, "set_status", hlua_txn_reply_set_status); |
| 9189 | hlua_class_function(L, "add_header", hlua_txn_reply_add_header); |
| 9190 | hlua_class_function(L, "del_header", hlua_txn_reply_del_header); |
| 9191 | hlua_class_function(L, "set_body", hlua_txn_reply_set_body); |
| 9192 | lua_settable(L, -3); /* Sets the __index entry. */ |
| 9193 | class_txn_reply_ref = luaL_ref(L, LUA_REGISTRYINDEX); |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 9194 | |
| 9195 | |
| 9196 | /* |
| 9197 | * |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9198 | * Register class Socket |
| 9199 | * |
| 9200 | */ |
| 9201 | |
| 9202 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9203 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9204 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 9205 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9206 | lua_pushstring(L, "__index"); |
| 9207 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9208 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 9209 | #ifdef USE_OPENSSL |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9210 | hlua_class_function(L, "connect_ssl", hlua_socket_connect_ssl); |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 9211 | #endif |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9212 | hlua_class_function(L, "connect", hlua_socket_connect); |
| 9213 | hlua_class_function(L, "send", hlua_socket_send); |
| 9214 | hlua_class_function(L, "receive", hlua_socket_receive); |
| 9215 | hlua_class_function(L, "close", hlua_socket_close); |
| 9216 | hlua_class_function(L, "getpeername", hlua_socket_getpeername); |
| 9217 | hlua_class_function(L, "getsockname", hlua_socket_getsockname); |
| 9218 | hlua_class_function(L, "setoption", hlua_socket_setoption); |
| 9219 | hlua_class_function(L, "settimeout", hlua_socket_settimeout); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9220 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9221 | 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] | 9222 | |
| 9223 | /* Register the garbage collector entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9224 | lua_pushstring(L, "__gc"); |
| 9225 | lua_pushcclosure(L, hlua_socket_gc, 0); |
| 9226 | 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] | 9227 | |
| 9228 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9229 | class_socket_ref = hlua_register_metatable(L, CLASS_SOCKET); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9230 | |
Thierry Fournier | aafc777 | 2020-12-04 11:47:47 +0100 | [diff] [blame] | 9231 | lua_atpanic(L, hlua_panic_safe); |
| 9232 | |
| 9233 | return L; |
| 9234 | } |
| 9235 | |
| 9236 | void hlua_init(void) { |
| 9237 | int i; |
Amaury Denoyelle | 239fdbf | 2021-03-24 10:22:03 +0100 | [diff] [blame] | 9238 | char *errmsg; |
Thierry Fournier | aafc777 | 2020-12-04 11:47:47 +0100 | [diff] [blame] | 9239 | #ifdef USE_OPENSSL |
| 9240 | struct srv_kw *kw; |
| 9241 | int tmp_error; |
| 9242 | char *error; |
| 9243 | char *args[] = { /* SSL client configuration. */ |
| 9244 | "ssl", |
| 9245 | "verify", |
| 9246 | "none", |
| 9247 | NULL |
| 9248 | }; |
| 9249 | #endif |
| 9250 | |
| 9251 | /* Init post init function list head */ |
| 9252 | for (i = 0; i < MAX_THREADS + 1; i++) |
| 9253 | LIST_INIT(&hlua_init_functions[i]); |
| 9254 | |
| 9255 | /* Init state for common/shared lua parts */ |
| 9256 | hlua_state_id = 0; |
| 9257 | ha_set_tid(0); |
| 9258 | hlua_states[0] = hlua_init_state(0); |
| 9259 | |
| 9260 | /* Init state 1 for thread 0. We have at least one thread. */ |
| 9261 | hlua_state_id = 1; |
| 9262 | ha_set_tid(0); |
| 9263 | hlua_states[1] = hlua_init_state(1); |
| 9264 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9265 | /* Proxy and server configuration initialisation. */ |
Amaury Denoyelle | 239fdbf | 2021-03-24 10:22:03 +0100 | [diff] [blame] | 9266 | socket_proxy = alloc_new_proxy("LUA-SOCKET", PR_CAP_FE|PR_CAP_BE|PR_CAP_LUA, &errmsg); |
| 9267 | if (!socket_proxy) { |
| 9268 | fprintf(stderr, "Lua init: %s\n", errmsg); |
| 9269 | exit(1); |
| 9270 | } |
| 9271 | proxy_preset_defaults(socket_proxy); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9272 | |
| 9273 | /* Init TCP server: unchanged parameters */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 9274 | socket_tcp = new_server(socket_proxy); |
| 9275 | if (!socket_tcp) { |
| 9276 | fprintf(stderr, "Lua init: failed to allocate tcp server socket\n"); |
| 9277 | exit(1); |
| 9278 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9279 | |
| 9280 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9281 | /* Init TCP server: unchanged parameters */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 9282 | socket_ssl = new_server(socket_proxy); |
| 9283 | if (!socket_ssl) { |
| 9284 | fprintf(stderr, "Lua init: failed to allocate ssl server socket\n"); |
| 9285 | exit(1); |
| 9286 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9287 | |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 9288 | socket_ssl->use_ssl = 1; |
| 9289 | socket_ssl->xprt = xprt_get(XPRT_SSL); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9290 | |
Bertrand Jacquin | 80839ff | 2021-01-21 19:14:46 +0000 | [diff] [blame] | 9291 | for (i = 0; args[i] != NULL; i++) { |
| 9292 | 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] | 9293 | /* |
| 9294 | * |
| 9295 | * If the keyword is not known, we can search in the registered |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 9296 | * server keywords. This is useful to configure special SSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9297 | * features like client certificates and ssl_verify. |
| 9298 | * |
| 9299 | */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 9300 | tmp_error = kw->parse(args, &i, socket_proxy, socket_ssl, &error); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9301 | if (tmp_error != 0) { |
| 9302 | fprintf(stderr, "INTERNAL ERROR: %s\n", error); |
| 9303 | abort(); /* This must be never arrives because the command line |
| 9304 | not editable by the user. */ |
| 9305 | } |
Bertrand Jacquin | 80839ff | 2021-01-21 19:14:46 +0000 | [diff] [blame] | 9306 | i += kw->skip; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9307 | } |
| 9308 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9309 | #endif |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 9310 | |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 9311 | } |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 9312 | |
Tim Duesterhus | d0c0ca2 | 2020-07-04 11:53:26 +0200 | [diff] [blame] | 9313 | static void hlua_deinit() |
| 9314 | { |
Willy Tarreau | 186f376 | 2020-12-04 11:48:12 +0100 | [diff] [blame] | 9315 | int thr; |
| 9316 | |
| 9317 | for (thr = 0; thr < MAX_THREADS+1; thr++) { |
| 9318 | if (hlua_states[thr]) |
| 9319 | lua_close(hlua_states[thr]); |
| 9320 | } |
Willy Tarreau | 430bf4a | 2021-03-04 09:45:32 +0100 | [diff] [blame] | 9321 | |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 9322 | free_server(socket_tcp); |
Willy Tarreau | 430bf4a | 2021-03-04 09:45:32 +0100 | [diff] [blame] | 9323 | |
Willy Tarreau | 0f143af | 2021-03-05 10:41:48 +0100 | [diff] [blame] | 9324 | #ifdef USE_OPENSSL |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 9325 | free_server(socket_ssl); |
Willy Tarreau | 0f143af | 2021-03-05 10:41:48 +0100 | [diff] [blame] | 9326 | #endif |
Amaury Denoyelle | 239fdbf | 2021-03-24 10:22:03 +0100 | [diff] [blame] | 9327 | |
| 9328 | free_proxy(socket_proxy); |
Tim Duesterhus | d0c0ca2 | 2020-07-04 11:53:26 +0200 | [diff] [blame] | 9329 | } |
| 9330 | |
| 9331 | REGISTER_POST_DEINIT(hlua_deinit); |
| 9332 | |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 9333 | static void hlua_register_build_options(void) |
| 9334 | { |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 9335 | char *ptr = NULL; |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 9336 | |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 9337 | memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE); |
| 9338 | hap_register_build_opts(ptr, 1); |
| 9339 | } |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 9340 | |
| 9341 | INITCALL0(STG_REGISTER, hlua_register_build_options); |