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 | 6a51090 | 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 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 146 | #define SET_SAFE_LJMP_L(__L, __HLUA) \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 147 | ({ \ |
| 148 | int ret; \ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 149 | if ((__HLUA)->state_id == 0) \ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 150 | HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock); \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 151 | if (setjmp(safe_ljmp_env) != 0) { \ |
| 152 | lua_atpanic(__L, hlua_panic_safe); \ |
| 153 | ret = 0; \ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 154 | if ((__HLUA)->state_id == 0) \ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 155 | HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 156 | } else { \ |
| 157 | lua_atpanic(__L, hlua_panic_ljmp); \ |
| 158 | ret = 1; \ |
| 159 | } \ |
| 160 | ret; \ |
| 161 | }) |
| 162 | |
| 163 | /* If we are the last function catching Lua errors, we |
| 164 | * must reset the panic function. |
| 165 | */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 166 | #define RESET_SAFE_LJMP_L(__L, __HLUA) \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 167 | do { \ |
| 168 | lua_atpanic(__L, hlua_panic_safe); \ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 169 | if ((__HLUA)->state_id == 0) \ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 170 | HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 171 | } while(0) |
| 172 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 173 | #define SET_SAFE_LJMP(__HLUA) \ |
| 174 | SET_SAFE_LJMP_L((__HLUA)->T, __HLUA) |
| 175 | |
| 176 | #define RESET_SAFE_LJMP(__HLUA) \ |
| 177 | RESET_SAFE_LJMP_L((__HLUA)->T, __HLUA) |
| 178 | |
| 179 | #define SET_SAFE_LJMP_PARENT(__HLUA) \ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 180 | SET_SAFE_LJMP_L(hlua_states[(__HLUA)->state_id], __HLUA) |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 181 | |
| 182 | #define RESET_SAFE_LJMP_PARENT(__HLUA) \ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 183 | RESET_SAFE_LJMP_L(hlua_states[(__HLUA)->state_id], __HLUA) |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 184 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 185 | /* Applet status flags */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 186 | #define APPLET_DONE 0x01 /* applet processing is done. */ |
Christopher Faulet | 18c2e8d | 2019-03-01 12:02:08 +0100 | [diff] [blame] | 187 | /* unused: 0x02 */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 188 | #define APPLET_HDR_SENT 0x04 /* Response header sent. */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 189 | /* unused: 0x08, 0x10 */ |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 190 | #define APPLET_HTTP11 0x20 /* Last chunk sent. */ |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 191 | #define APPLET_RSP_SENT 0x40 /* The response was fully sent */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 192 | |
Thierry Fournier | afc63e2 | 2020-11-28 17:06:51 +0100 | [diff] [blame] | 193 | /* The main Lua execution context. The 0 index is the |
| 194 | * common state shared by all threads. |
| 195 | */ |
Willy Tarreau | 186f376 | 2020-12-04 11:48:12 +0100 | [diff] [blame] | 196 | static lua_State *hlua_states[MAX_THREADS + 1]; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 197 | |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 198 | /* This is the memory pool containing struct lua for applets |
| 199 | * (including cli). |
| 200 | */ |
Willy Tarreau | 8ceae72 | 2018-11-26 11:58:30 +0100 | [diff] [blame] | 201 | DECLARE_STATIC_POOL(pool_head_hlua, "hlua", sizeof(struct hlua)); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 202 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 203 | /* Used for Socket connection. */ |
Amaury Denoyelle | 239fdbf | 2021-03-24 10:22:03 +0100 | [diff] [blame] | 204 | static struct proxy *socket_proxy; |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 205 | static struct server *socket_tcp; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 206 | #ifdef USE_OPENSSL |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 207 | static struct server *socket_ssl; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 208 | #endif |
| 209 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 210 | /* List head of the function called at the initialisation time. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 211 | struct list hlua_init_functions[MAX_THREADS + 1]; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 212 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 213 | /* The following variables contains the reference of the different |
| 214 | * Lua classes. These references are useful for identify metadata |
| 215 | * associated with an object. |
| 216 | */ |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 217 | static int class_txn_ref; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 218 | static int class_socket_ref; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 219 | static int class_channel_ref; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 220 | static int class_fetches_ref; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 221 | static int class_converters_ref; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 222 | static int class_http_ref; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 223 | static int class_map_ref; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 224 | static int class_applet_tcp_ref; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 225 | static int class_applet_http_ref; |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 226 | static int class_txn_reply_ref; |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 227 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 228 | /* Global Lua execution timeout. By default Lua, execution linked |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 229 | * with stream (actions, sample-fetches and converters) have a |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 230 | * short timeout. Lua linked with tasks doesn't have a timeout |
| 231 | * because a task may remain alive during all the haproxy execution. |
| 232 | */ |
| 233 | static unsigned int hlua_timeout_session = 4000; /* session timeout. */ |
| 234 | static unsigned int hlua_timeout_task = TICK_ETERNITY; /* task timeout. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 235 | static unsigned int hlua_timeout_applet = 4000; /* applet timeout. */ |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 236 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 237 | /* Interrupts the Lua processing each "hlua_nb_instruction" instructions. |
| 238 | * it is used for preventing infinite loops. |
| 239 | * |
| 240 | * I test the scheer with an infinite loop containing one incrementation |
| 241 | * and one test. I run this loop between 10 seconds, I raise a ceil of |
| 242 | * 710M loops from one interrupt each 9000 instructions, so I fix the value |
| 243 | * to one interrupt each 10 000 instructions. |
| 244 | * |
| 245 | * configured | Number of |
| 246 | * instructions | loops executed |
| 247 | * between two | in milions |
| 248 | * forced yields | |
| 249 | * ---------------+--------------- |
| 250 | * 10 | 160 |
| 251 | * 500 | 670 |
| 252 | * 1000 | 680 |
| 253 | * 5000 | 700 |
| 254 | * 7000 | 700 |
| 255 | * 8000 | 700 |
| 256 | * 9000 | 710 <- ceil |
| 257 | * 10000 | 710 |
| 258 | * 100000 | 710 |
| 259 | * 1000000 | 710 |
| 260 | * |
| 261 | */ |
| 262 | static unsigned int hlua_nb_instruction = 10000; |
| 263 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 264 | /* Descriptor for the memory allocation state. The limit is pre-initialised to |
| 265 | * 0 until it is replaced by "tune.lua.maxmem" during the config parsing, or it |
| 266 | * is replaced with ~0 during post_init after everything was loaded. This way |
| 267 | * it is guaranteed that if limit is ~0 the boot is complete and that if it's |
| 268 | * zero it's not yet limited and proper accounting is required. |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 269 | */ |
| 270 | struct hlua_mem_allocator { |
| 271 | size_t allocated; |
| 272 | size_t limit; |
| 273 | }; |
| 274 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 275 | static struct hlua_mem_allocator hlua_global_allocator THREAD_ALIGNED(64); |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 276 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 277 | /* These functions converts types between HAProxy internal args or |
| 278 | * sample and LUA types. Another function permits to check if the |
| 279 | * LUA stack contains arguments according with an required ARG_T |
| 280 | * format. |
| 281 | */ |
| 282 | static int hlua_arg2lua(lua_State *L, const struct arg *arg); |
| 283 | static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 284 | __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] | 285 | uint64_t mask, struct proxy *p); |
Willy Tarreau | 5eadada | 2015-03-10 17:28:54 +0100 | [diff] [blame] | 286 | static int hlua_smp2lua(lua_State *L, struct sample *smp); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 287 | static int hlua_smp2lua_str(lua_State *L, struct sample *smp); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 288 | static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp); |
| 289 | |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 290 | __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] | 291 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 292 | struct prepend_path { |
| 293 | struct list l; |
| 294 | char *type; |
| 295 | char *path; |
| 296 | }; |
| 297 | |
| 298 | static struct list prepend_path_list = LIST_HEAD_INIT(prepend_path_list); |
| 299 | |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 300 | #define SEND_ERR(__be, __fmt, __args...) \ |
| 301 | do { \ |
| 302 | send_log(__be, LOG_ERR, __fmt, ## __args); \ |
| 303 | if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) \ |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 304 | ha_alert(__fmt, ## __args); \ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 305 | } while (0) |
| 306 | |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 307 | static inline struct hlua_function *new_hlua_function() |
| 308 | { |
| 309 | struct hlua_function *fcn; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 310 | int i; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 311 | |
| 312 | fcn = calloc(1, sizeof(*fcn)); |
| 313 | if (!fcn) |
| 314 | return NULL; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 315 | LIST_APPEND(&referenced_functions, &fcn->l); |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 316 | for (i = 0; i < MAX_THREADS + 1; i++) |
| 317 | fcn->function_ref[i] = -1; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 318 | return fcn; |
| 319 | } |
| 320 | |
Christopher Faulet | dda4444 | 2021-04-12 14:05:43 +0200 | [diff] [blame] | 321 | static inline void release_hlua_function(struct hlua_function *fcn) |
| 322 | { |
| 323 | if (!fcn) |
| 324 | return; |
| 325 | if (fcn->name) |
| 326 | ha_free(&fcn->name); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 327 | LIST_DELETE(&fcn->l); |
Christopher Faulet | dda4444 | 2021-04-12 14:05:43 +0200 | [diff] [blame] | 328 | ha_free(&fcn); |
| 329 | } |
| 330 | |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 331 | /* If the common state is set, the stack id is 0, otherwise it is the tid + 1 */ |
| 332 | static inline int fcn_ref_to_stack_id(struct hlua_function *fcn) |
| 333 | { |
| 334 | if (fcn->function_ref[0] == -1) |
| 335 | return tid + 1; |
| 336 | return 0; |
| 337 | } |
| 338 | |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 339 | /* Used to check an Lua function type in the stack. It creates and |
| 340 | * returns a reference of the function. This function throws an |
| 341 | * error if the rgument is not a "function". |
| 342 | */ |
| 343 | __LJMP unsigned int hlua_checkfunction(lua_State *L, int argno) |
| 344 | { |
| 345 | if (!lua_isfunction(L, argno)) { |
Thierry FOURNIER | fd1e955 | 2018-02-23 18:41:18 +0100 | [diff] [blame] | 346 | 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] | 347 | WILL_LJMP(luaL_argerror(L, argno, msg)); |
| 348 | } |
| 349 | lua_pushvalue(L, argno); |
| 350 | return luaL_ref(L, LUA_REGISTRYINDEX); |
| 351 | } |
| 352 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 353 | /* Return the string that is of the top of the stack. */ |
| 354 | const char *hlua_get_top_error_string(lua_State *L) |
| 355 | { |
| 356 | if (lua_gettop(L) < 1) |
| 357 | return "unknown error"; |
| 358 | if (lua_type(L, -1) != LUA_TSTRING) |
| 359 | return "unknown error"; |
| 360 | return lua_tostring(L, -1); |
| 361 | } |
| 362 | |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 363 | __LJMP const char *hlua_traceback(lua_State *L, const char* sep) |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 364 | { |
| 365 | lua_Debug ar; |
| 366 | int level = 0; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 367 | struct buffer *msg = get_trash_chunk(); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 368 | |
| 369 | while (lua_getstack(L, level++, &ar)) { |
| 370 | |
| 371 | /* Add separator */ |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 372 | if (b_data(msg)) |
| 373 | chunk_appendf(msg, "%s", sep); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 374 | |
| 375 | /* Fill fields: |
| 376 | * 'S': fills in the fields source, short_src, linedefined, lastlinedefined, and what; |
| 377 | * 'l': fills in the field currentline; |
| 378 | * 'n': fills in the field name and namewhat; |
| 379 | * 't': fills in the field istailcall; |
| 380 | */ |
| 381 | lua_getinfo(L, "Slnt", &ar); |
| 382 | |
| 383 | /* Append code localisation */ |
| 384 | if (ar.currentline > 0) |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 385 | chunk_appendf(msg, "%s:%d: ", ar.short_src, ar.currentline); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 386 | else |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 387 | chunk_appendf(msg, "%s: ", ar.short_src); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 388 | |
| 389 | /* |
| 390 | * Get function name |
| 391 | * |
| 392 | * if namewhat is no empty, name is defined. |
| 393 | * what contains "Lua" for Lua function, "C" for C function, |
| 394 | * or "main" for main code. |
| 395 | */ |
| 396 | 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] | 397 | chunk_appendf(msg, "in %s '%s'", ar.namewhat, ar.name); /* use it */ |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 398 | |
| 399 | 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] | 400 | chunk_appendf(msg, "in main chunk"); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 401 | |
| 402 | else if (*ar.what != 'C') /* for Lua functions, use <file:line> */ |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 403 | chunk_appendf(msg, "in function line %d", ar.linedefined); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 404 | |
| 405 | else /* nothing left... */ |
| 406 | chunk_appendf(msg, "?"); |
| 407 | |
| 408 | |
| 409 | /* Display tailed call */ |
| 410 | if (ar.istailcall) |
| 411 | chunk_appendf(msg, " ..."); |
| 412 | } |
| 413 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 414 | return msg->area; |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 418 | /* This function check the number of arguments available in the |
| 419 | * stack. If the number of arguments available is not the same |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 420 | * then <nb> an error is thrown. |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 421 | */ |
| 422 | __LJMP static inline void check_args(lua_State *L, int nb, char *fcn) |
| 423 | { |
| 424 | if (lua_gettop(L) == nb) |
| 425 | return; |
| 426 | WILL_LJMP(luaL_error(L, "'%s' needs %d arguments", fcn, nb)); |
| 427 | } |
| 428 | |
Mark Lakes | 22154b4 | 2018-01-29 14:38:40 -0800 | [diff] [blame] | 429 | /* This function pushes an error string prefixed by the file name |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 430 | * and the line number where the error is encountered. |
| 431 | */ |
| 432 | static int hlua_pusherror(lua_State *L, const char *fmt, ...) |
| 433 | { |
| 434 | va_list argp; |
| 435 | va_start(argp, fmt); |
| 436 | luaL_where(L, 1); |
| 437 | lua_pushvfstring(L, fmt, argp); |
| 438 | va_end(argp); |
| 439 | lua_concat(L, 2); |
| 440 | return 1; |
| 441 | } |
| 442 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 443 | /* This functions is used with sample fetch and converters. It |
| 444 | * converts the HAProxy configuration argument in a lua stack |
| 445 | * values. |
| 446 | * |
| 447 | * It takes an array of "arg", and each entry of the array is |
| 448 | * converted and pushed in the LUA stack. |
| 449 | */ |
| 450 | static int hlua_arg2lua(lua_State *L, const struct arg *arg) |
| 451 | { |
| 452 | switch (arg->type) { |
| 453 | case ARGT_SINT: |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 454 | case ARGT_TIME: |
| 455 | case ARGT_SIZE: |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 456 | lua_pushinteger(L, arg->data.sint); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 457 | break; |
| 458 | |
| 459 | case ARGT_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 460 | lua_pushlstring(L, arg->data.str.area, arg->data.str.data); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 461 | break; |
| 462 | |
| 463 | case ARGT_IPV4: |
| 464 | case ARGT_IPV6: |
| 465 | case ARGT_MSK4: |
| 466 | case ARGT_MSK6: |
| 467 | case ARGT_FE: |
| 468 | case ARGT_BE: |
| 469 | case ARGT_TAB: |
| 470 | case ARGT_SRV: |
| 471 | case ARGT_USR: |
| 472 | case ARGT_MAP: |
| 473 | default: |
| 474 | lua_pushnil(L); |
| 475 | break; |
| 476 | } |
| 477 | return 1; |
| 478 | } |
| 479 | |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 480 | /* 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] | 481 | * 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] | 482 | * with sample fetch wrappers. The input arguments are given to the |
| 483 | * lua wrapper and converted as arg list by the function. |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 484 | */ |
| 485 | static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg) |
| 486 | { |
| 487 | switch (lua_type(L, ud)) { |
| 488 | |
| 489 | case LUA_TNUMBER: |
| 490 | case LUA_TBOOLEAN: |
| 491 | arg->type = ARGT_SINT; |
| 492 | arg->data.sint = lua_tointeger(L, ud); |
| 493 | break; |
| 494 | |
| 495 | case LUA_TSTRING: |
| 496 | arg->type = ARGT_STR; |
Tim Duesterhus | 2e89dec | 2019-09-29 23:03:08 +0200 | [diff] [blame] | 497 | 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] | 498 | /* 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] | 499 | 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] | 500 | arg->data.str.head = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 501 | break; |
| 502 | |
| 503 | case LUA_TUSERDATA: |
| 504 | case LUA_TNIL: |
| 505 | case LUA_TTABLE: |
| 506 | case LUA_TFUNCTION: |
| 507 | case LUA_TTHREAD: |
| 508 | case LUA_TLIGHTUSERDATA: |
| 509 | arg->type = ARGT_SINT; |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 510 | arg->data.sint = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 511 | break; |
| 512 | } |
| 513 | return 1; |
| 514 | } |
| 515 | |
| 516 | /* the following functions are used to convert a struct sample |
| 517 | * in Lua type. This useful to convert the return of the |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 518 | * fetches or converters. |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 519 | */ |
Willy Tarreau | 5eadada | 2015-03-10 17:28:54 +0100 | [diff] [blame] | 520 | static int hlua_smp2lua(lua_State *L, struct sample *smp) |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 521 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 522 | switch (smp->data.type) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 523 | case SMP_T_SINT: |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 524 | case SMP_T_BOOL: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 525 | lua_pushinteger(L, smp->data.u.sint); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 526 | break; |
| 527 | |
| 528 | case SMP_T_BIN: |
| 529 | case SMP_T_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 530 | 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] | 531 | break; |
| 532 | |
| 533 | case SMP_T_METH: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 534 | switch (smp->data.u.meth.meth) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 535 | case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break; |
| 536 | case HTTP_METH_GET: lua_pushstring(L, "GET"); break; |
| 537 | case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break; |
| 538 | case HTTP_METH_POST: lua_pushstring(L, "POST"); break; |
| 539 | case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break; |
| 540 | case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break; |
| 541 | case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break; |
| 542 | case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break; |
| 543 | case HTTP_METH_OTHER: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 544 | 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] | 545 | break; |
| 546 | default: |
| 547 | lua_pushnil(L); |
| 548 | break; |
| 549 | } |
| 550 | break; |
| 551 | |
| 552 | case SMP_T_IPV4: |
| 553 | case SMP_T_IPV6: |
| 554 | 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] | 555 | if (sample_casts[smp->data.type][SMP_T_STR] && |
| 556 | sample_casts[smp->data.type][SMP_T_STR](smp)) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 557 | 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] | 558 | else |
| 559 | lua_pushnil(L); |
| 560 | break; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 561 | default: |
| 562 | lua_pushnil(L); |
| 563 | break; |
| 564 | } |
| 565 | return 1; |
| 566 | } |
| 567 | |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 568 | /* the following functions are used to convert a struct sample |
| 569 | * in Lua strings. This is useful to convert the return of the |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 570 | * fetches or converters. |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 571 | */ |
| 572 | static int hlua_smp2lua_str(lua_State *L, struct sample *smp) |
| 573 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 574 | switch (smp->data.type) { |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 575 | |
| 576 | case SMP_T_BIN: |
| 577 | case SMP_T_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 578 | 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] | 579 | break; |
| 580 | |
| 581 | case SMP_T_METH: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 582 | switch (smp->data.u.meth.meth) { |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 583 | case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break; |
| 584 | case HTTP_METH_GET: lua_pushstring(L, "GET"); break; |
| 585 | case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break; |
| 586 | case HTTP_METH_POST: lua_pushstring(L, "POST"); break; |
| 587 | case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break; |
| 588 | case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break; |
| 589 | case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break; |
| 590 | case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break; |
| 591 | case HTTP_METH_OTHER: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 592 | 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] | 593 | break; |
| 594 | default: |
| 595 | lua_pushstring(L, ""); |
| 596 | break; |
| 597 | } |
| 598 | break; |
| 599 | |
| 600 | case SMP_T_SINT: |
| 601 | case SMP_T_BOOL: |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 602 | case SMP_T_IPV4: |
| 603 | case SMP_T_IPV6: |
| 604 | 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] | 605 | if (sample_casts[smp->data.type][SMP_T_STR] && |
| 606 | sample_casts[smp->data.type][SMP_T_STR](smp)) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 607 | 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] | 608 | else |
| 609 | lua_pushstring(L, ""); |
| 610 | break; |
| 611 | default: |
| 612 | lua_pushstring(L, ""); |
| 613 | break; |
| 614 | } |
| 615 | return 1; |
| 616 | } |
| 617 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 618 | /* the following functions are used to convert an Lua type in a |
| 619 | * struct sample. This is useful to provide data from a converter |
| 620 | * to the LUA code. |
| 621 | */ |
| 622 | static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp) |
| 623 | { |
| 624 | switch (lua_type(L, ud)) { |
| 625 | |
| 626 | case LUA_TNUMBER: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 627 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 628 | smp->data.u.sint = lua_tointeger(L, ud); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 629 | break; |
| 630 | |
| 631 | |
| 632 | case LUA_TBOOLEAN: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 633 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 634 | smp->data.u.sint = lua_toboolean(L, ud); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 635 | break; |
| 636 | |
| 637 | case LUA_TSTRING: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 638 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 639 | smp->flags |= SMP_F_CONST; |
Tim Duesterhus | 2e89dec | 2019-09-29 23:03:08 +0200 | [diff] [blame] | 640 | 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] | 641 | /* 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] | 642 | 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] | 643 | smp->data.u.str.head = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 644 | break; |
| 645 | |
| 646 | case LUA_TUSERDATA: |
| 647 | case LUA_TNIL: |
| 648 | case LUA_TTABLE: |
| 649 | case LUA_TFUNCTION: |
| 650 | case LUA_TTHREAD: |
| 651 | case LUA_TLIGHTUSERDATA: |
Thierry FOURNIER | 93405e1 | 2015-08-26 14:19:03 +0200 | [diff] [blame] | 652 | case LUA_TNONE: |
| 653 | default: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 654 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 655 | smp->data.u.sint = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 656 | break; |
| 657 | } |
| 658 | return 1; |
| 659 | } |
| 660 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 661 | /* This function check the "argp" built by another conversion function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 662 | * 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] | 663 | * 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] | 664 | * |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 665 | * This function assumes that the argp argument contains ARGM_NBARGS + 1 |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 666 | * entries and that there is at least one stop at the last position. |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 667 | */ |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 668 | __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] | 669 | uint64_t mask, struct proxy *p) |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 670 | { |
| 671 | int min_arg; |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 672 | int idx; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 673 | struct proxy *px; |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 674 | struct userlist *ul; |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 675 | struct my_regex *reg; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 676 | const char *msg = NULL; |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 677 | char *sname, *pname, *err = NULL; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 678 | |
| 679 | idx = 0; |
| 680 | min_arg = ARGM(mask); |
| 681 | mask >>= ARGM_BITS; |
| 682 | |
| 683 | while (1) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 684 | struct buffer tmp = BUF_NULL; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 685 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 686 | /* Check for mandatory arguments. */ |
| 687 | if (argp[idx].type == ARGT_STOP) { |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 688 | if (idx < min_arg) { |
| 689 | |
| 690 | /* If miss other argument than the first one, we return an error. */ |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 691 | if (idx > 0) { |
| 692 | msg = "Mandatory argument expected"; |
| 693 | goto error; |
| 694 | } |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 695 | |
| 696 | /* If first argument have a certain type, some default values |
| 697 | * may be used. See the function smp_resolve_args(). |
| 698 | */ |
| 699 | switch (mask & ARGT_MASK) { |
| 700 | |
| 701 | case ARGT_FE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 702 | if (!(p->cap & PR_CAP_FE)) { |
| 703 | msg = "Mandatory argument expected"; |
| 704 | goto error; |
| 705 | } |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 706 | argp[idx].data.prx = p; |
| 707 | argp[idx].type = ARGT_FE; |
| 708 | argp[idx+1].type = ARGT_STOP; |
| 709 | break; |
| 710 | |
| 711 | case ARGT_BE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 712 | if (!(p->cap & PR_CAP_BE)) { |
| 713 | msg = "Mandatory argument expected"; |
| 714 | goto error; |
| 715 | } |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 716 | argp[idx].data.prx = p; |
| 717 | argp[idx].type = ARGT_BE; |
| 718 | argp[idx+1].type = ARGT_STOP; |
| 719 | break; |
| 720 | |
| 721 | case ARGT_TAB: |
| 722 | argp[idx].data.prx = p; |
| 723 | argp[idx].type = ARGT_TAB; |
| 724 | argp[idx+1].type = ARGT_STOP; |
| 725 | break; |
| 726 | |
| 727 | default: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 728 | msg = "Mandatory argument expected"; |
| 729 | goto error; |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 730 | break; |
| 731 | } |
| 732 | } |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 733 | break; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 734 | } |
| 735 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 736 | /* Check for exceed the number of required argument. */ |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 737 | if ((mask & ARGT_MASK) == ARGT_STOP && |
| 738 | argp[idx].type != ARGT_STOP) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 739 | msg = "Last argument expected"; |
| 740 | goto error; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 741 | } |
| 742 | |
| 743 | if ((mask & ARGT_MASK) == ARGT_STOP && |
| 744 | argp[idx].type == ARGT_STOP) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 745 | break; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 746 | } |
| 747 | |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 748 | /* Convert some argument types. All string in argp[] are for not |
| 749 | * duplicated yet. |
| 750 | */ |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 751 | switch (mask & ARGT_MASK) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 752 | case ARGT_SINT: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 753 | if (argp[idx].type != ARGT_SINT) { |
| 754 | msg = "integer expected"; |
| 755 | goto error; |
| 756 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 757 | argp[idx].type = ARGT_SINT; |
| 758 | break; |
| 759 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 760 | case ARGT_TIME: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 761 | if (argp[idx].type != ARGT_SINT) { |
| 762 | msg = "integer expected"; |
| 763 | goto error; |
| 764 | } |
Thierry FOURNIER | 29176f3 | 2015-07-07 00:41:29 +0200 | [diff] [blame] | 765 | argp[idx].type = ARGT_TIME; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 766 | break; |
| 767 | |
| 768 | case ARGT_SIZE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 769 | if (argp[idx].type != ARGT_SINT) { |
| 770 | msg = "integer expected"; |
| 771 | goto error; |
| 772 | } |
Thierry FOURNIER | 29176f3 | 2015-07-07 00:41:29 +0200 | [diff] [blame] | 773 | argp[idx].type = ARGT_SIZE; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 774 | break; |
| 775 | |
| 776 | case ARGT_FE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 777 | if (argp[idx].type != ARGT_STR) { |
| 778 | msg = "string expected"; |
| 779 | goto error; |
| 780 | } |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 781 | 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] | 782 | if (!argp[idx].data.prx) { |
| 783 | msg = "frontend doesn't exist"; |
| 784 | goto error; |
| 785 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 786 | argp[idx].type = ARGT_FE; |
| 787 | break; |
| 788 | |
| 789 | case ARGT_BE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 790 | if (argp[idx].type != ARGT_STR) { |
| 791 | msg = "string expected"; |
| 792 | goto error; |
| 793 | } |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 794 | 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] | 795 | if (!argp[idx].data.prx) { |
| 796 | msg = "backend doesn't exist"; |
| 797 | goto error; |
| 798 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 799 | argp[idx].type = ARGT_BE; |
| 800 | break; |
| 801 | |
| 802 | case ARGT_TAB: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 803 | if (argp[idx].type != ARGT_STR) { |
| 804 | msg = "string expected"; |
| 805 | goto error; |
| 806 | } |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 807 | 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] | 808 | if (!argp[idx].data.t) { |
| 809 | msg = "table doesn't exist"; |
| 810 | goto error; |
| 811 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 812 | argp[idx].type = ARGT_TAB; |
| 813 | break; |
| 814 | |
| 815 | case ARGT_SRV: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 816 | if (argp[idx].type != ARGT_STR) { |
| 817 | msg = "string expected"; |
| 818 | goto error; |
| 819 | } |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 820 | sname = strrchr(argp[idx].data.str.area, '/'); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 821 | if (sname) { |
| 822 | *sname++ = '\0'; |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 823 | pname = argp[idx].data.str.area; |
Willy Tarreau | 9e0bb10 | 2015-05-26 11:24:42 +0200 | [diff] [blame] | 824 | px = proxy_be_by_name(pname); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 825 | if (!px) { |
| 826 | msg = "backend doesn't exist"; |
| 827 | goto error; |
| 828 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 829 | } |
| 830 | else { |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 831 | sname = argp[idx].data.str.area; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 832 | px = p; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 833 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 834 | argp[idx].data.srv = findserver(px, sname); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 835 | if (!argp[idx].data.srv) { |
| 836 | msg = "server doesn't exist"; |
| 837 | goto error; |
| 838 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 839 | argp[idx].type = ARGT_SRV; |
| 840 | break; |
| 841 | |
| 842 | case ARGT_IPV4: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 843 | if (argp[idx].type != ARGT_STR) { |
| 844 | msg = "string expected"; |
| 845 | goto error; |
| 846 | } |
| 847 | if (inet_pton(AF_INET, argp[idx].data.str.area, &argp[idx].data.ipv4)) { |
| 848 | msg = "invalid IPv4 address"; |
| 849 | goto error; |
| 850 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 851 | argp[idx].type = ARGT_IPV4; |
| 852 | break; |
| 853 | |
| 854 | case ARGT_MSK4: |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 855 | if (argp[idx].type == ARGT_SINT) |
| 856 | len2mask4(argp[idx].data.sint, &argp[idx].data.ipv4); |
| 857 | else if (argp[idx].type == ARGT_STR) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 858 | if (!str2mask(argp[idx].data.str.area, &argp[idx].data.ipv4)) { |
| 859 | msg = "invalid IPv4 mask"; |
| 860 | goto error; |
| 861 | } |
| 862 | } |
| 863 | else { |
| 864 | msg = "integer or string expected"; |
| 865 | goto error; |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 866 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 867 | argp[idx].type = ARGT_MSK4; |
| 868 | break; |
| 869 | |
| 870 | case ARGT_IPV6: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 871 | if (argp[idx].type != ARGT_STR) { |
| 872 | msg = "string expected"; |
| 873 | goto error; |
| 874 | } |
| 875 | if (inet_pton(AF_INET6, argp[idx].data.str.area, &argp[idx].data.ipv6)) { |
| 876 | msg = "invalid IPv6 address"; |
| 877 | goto error; |
| 878 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 879 | argp[idx].type = ARGT_IPV6; |
| 880 | break; |
| 881 | |
| 882 | case ARGT_MSK6: |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 883 | if (argp[idx].type == ARGT_SINT) |
| 884 | len2mask6(argp[idx].data.sint, &argp[idx].data.ipv6); |
| 885 | else if (argp[idx].type == ARGT_STR) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 886 | if (!str2mask6(argp[idx].data.str.area, &argp[idx].data.ipv6)) { |
| 887 | msg = "invalid IPv6 mask"; |
| 888 | goto error; |
| 889 | } |
| 890 | } |
| 891 | else { |
| 892 | msg = "integer or string expected"; |
| 893 | goto error; |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 894 | } |
Tim Duesterhus | b814da6 | 2018-01-25 16:24:50 +0100 | [diff] [blame] | 895 | argp[idx].type = ARGT_MSK6; |
| 896 | break; |
| 897 | |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 898 | case ARGT_REG: |
| 899 | if (argp[idx].type != ARGT_STR) { |
| 900 | msg = "string expected"; |
| 901 | goto error; |
| 902 | } |
| 903 | reg = regex_comp(argp[idx].data.str.area, !(argp[idx].type_flags & ARGF_REG_ICASE), 1, &err); |
| 904 | if (!reg) { |
| 905 | msg = lua_pushfstring(L, "error compiling regex '%s' : '%s'", |
| 906 | argp[idx].data.str.area, err); |
| 907 | free(err); |
| 908 | goto error; |
| 909 | } |
| 910 | argp[idx].type = ARGT_REG; |
| 911 | argp[idx].data.reg = reg; |
| 912 | break; |
| 913 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 914 | case ARGT_USR: |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 915 | if (argp[idx].type != ARGT_STR) { |
| 916 | msg = "string expected"; |
| 917 | goto error; |
| 918 | } |
| 919 | if (p->uri_auth && p->uri_auth->userlist && |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 920 | strcmp(p->uri_auth->userlist->name, argp[idx].data.str.area) == 0) |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 921 | ul = p->uri_auth->userlist; |
| 922 | else |
| 923 | ul = auth_find_userlist(argp[idx].data.str.area); |
| 924 | |
| 925 | if (!ul) { |
| 926 | msg = lua_pushfstring(L, "unable to find userlist '%s'", argp[idx].data.str.area); |
| 927 | goto error; |
| 928 | } |
| 929 | argp[idx].type = ARGT_USR; |
| 930 | argp[idx].data.usr = ul; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 931 | break; |
| 932 | |
| 933 | case ARGT_STR: |
| 934 | if (!chunk_dup(&tmp, &argp[idx].data.str)) { |
| 935 | msg = "unable to duplicate string arg"; |
| 936 | goto error; |
| 937 | } |
| 938 | argp[idx].data.str = tmp; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 939 | break; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 940 | |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 941 | case ARGT_MAP: |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 942 | msg = "type not yet supported"; |
| 943 | goto error; |
| 944 | break; |
| 945 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 946 | } |
| 947 | |
| 948 | /* Check for type of argument. */ |
| 949 | if ((mask & ARGT_MASK) != argp[idx].type) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 950 | msg = lua_pushfstring(L, "'%s' expected, got '%s'", |
| 951 | arg_type_names[(mask & ARGT_MASK)], |
| 952 | arg_type_names[argp[idx].type & ARGT_MASK]); |
| 953 | goto error; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 954 | } |
| 955 | |
| 956 | /* Next argument. */ |
| 957 | mask >>= ARGT_BITS; |
| 958 | idx++; |
| 959 | } |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 960 | return 0; |
| 961 | |
| 962 | error: |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 963 | free_args(argp); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 964 | WILL_LJMP(luaL_argerror(L, first + idx, msg)); |
| 965 | return 0; /* Never reached */ |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 966 | } |
| 967 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 968 | /* |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 969 | * The following functions are used to make correspondence between the the |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 970 | * executed lua pointer and the "struct hlua *" that contain the context. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 971 | * |
| 972 | * - hlua_gethlua : return the hlua context associated with an lua_State. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 973 | * - hlua_sethlua : create the association between hlua context and lua_state. |
| 974 | */ |
| 975 | static inline struct hlua *hlua_gethlua(lua_State *L) |
| 976 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 977 | struct hlua **hlua = lua_getextraspace(L); |
| 978 | return *hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 979 | } |
| 980 | static inline void hlua_sethlua(struct hlua *hlua) |
| 981 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 982 | struct hlua **hlua_store = lua_getextraspace(hlua->T); |
| 983 | *hlua_store = hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 984 | } |
| 985 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 986 | /* This function is used to send logs. It try to send on screen (stderr) |
| 987 | * and on the default syslog server. |
| 988 | */ |
| 989 | static inline void hlua_sendlog(struct proxy *px, int level, const char *msg) |
| 990 | { |
| 991 | struct tm tm; |
| 992 | char *p; |
| 993 | |
| 994 | /* Cleanup the log message. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 995 | p = trash.area; |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 996 | for (; *msg != '\0'; msg++, p++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 997 | if (p >= trash.area + trash.size - 1) { |
Thierry FOURNIER | ccf0063 | 2015-09-16 12:47:03 +0200 | [diff] [blame] | 998 | /* Break the message if exceed the buffer size. */ |
| 999 | *(p-4) = ' '; |
| 1000 | *(p-3) = '.'; |
| 1001 | *(p-2) = '.'; |
| 1002 | *(p-1) = '.'; |
| 1003 | break; |
| 1004 | } |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 1005 | if (isprint((unsigned char)*msg)) |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1006 | *p = *msg; |
| 1007 | else |
| 1008 | *p = '.'; |
| 1009 | } |
| 1010 | *p = '\0'; |
| 1011 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1012 | send_log(px, level, "%s\n", trash.area); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1013 | if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) { |
Christopher Faulet | f98d821 | 2020-10-02 18:13:52 +0200 | [diff] [blame] | 1014 | if (level == LOG_DEBUG && !(global.mode & MODE_DEBUG)) |
| 1015 | return; |
| 1016 | |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 1017 | get_localtime(date.tv_sec, &tm); |
| 1018 | fprintf(stderr, "[%s] %03d/%02d%02d%02d (%d) : %s\n", |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1019 | 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] | 1020 | (int)getpid(), trash.area); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1021 | fflush(stderr); |
| 1022 | } |
| 1023 | } |
| 1024 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1025 | /* This function just ensure that the yield will be always |
| 1026 | * returned with a timeout and permit to set some flags |
| 1027 | */ |
| 1028 | __LJMP void hlua_yieldk(lua_State *L, int nresults, int ctx, |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1029 | lua_KFunction k, int timeout, unsigned int flags) |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1030 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 1031 | struct hlua *hlua; |
| 1032 | |
| 1033 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 1034 | hlua = hlua_gethlua(L); |
| 1035 | if (!hlua) { |
| 1036 | return; |
| 1037 | } |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1038 | |
| 1039 | /* Set the wake timeout. If timeout is required, we set |
| 1040 | * the expiration time. |
| 1041 | */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1042 | hlua->wake_time = timeout; |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1043 | |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 1044 | hlua->flags |= flags; |
| 1045 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1046 | /* Process the yield. */ |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 1047 | MAY_LJMP(lua_yieldk(L, nresults, ctx, k)); |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1048 | } |
| 1049 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1050 | /* This function initialises the Lua environment stored in the stream. |
| 1051 | * 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] | 1052 | * 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] | 1053 | * |
| 1054 | * This function is particular. it initialises a new Lua thread. If the |
| 1055 | * initialisation fails (example: out of memory error), the lua function |
| 1056 | * throws an error (longjmp). |
| 1057 | * |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1058 | * 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] | 1059 | * environment, so we must not initialise it. While the support of |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1060 | * threads appear, the safe environment set a lock to ensure only one |
| 1061 | * Lua execution at a time. If we initialize safe environment in another |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1062 | * safe environment, we have a dead lock. |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1063 | * |
| 1064 | * set "already_safe" true if the context is initialized form safe |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1065 | * Lua function. |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1066 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1067 | * This function manipulates two Lua stacks: the main and the thread. Only |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 1068 | * the main stack can fail. The thread is not manipulated. This function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1069 | * MUST NOT manipulate the created thread stack state, because it is not |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1070 | * protected against errors thrown by the thread stack. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1071 | */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1072 | 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] | 1073 | { |
| 1074 | lua->Mref = LUA_REFNIL; |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1075 | lua->flags = 0; |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 1076 | lua->gc_count = 0; |
Christopher Faulet | bc275a9 | 2020-02-26 14:55:16 +0100 | [diff] [blame] | 1077 | lua->wake_time = TICK_ETERNITY; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1078 | lua->state_id = state_id; |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 1079 | LIST_INIT(&lua->com); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1080 | if (!already_safe) { |
| 1081 | if (!SET_SAFE_LJMP_PARENT(lua)) { |
| 1082 | lua->Tref = LUA_REFNIL; |
| 1083 | return 0; |
| 1084 | } |
| 1085 | } |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1086 | lua->T = lua_newthread(hlua_states[state_id]); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1087 | if (!lua->T) { |
| 1088 | lua->Tref = LUA_REFNIL; |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1089 | if (!already_safe) |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1090 | RESET_SAFE_LJMP_PARENT(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1091 | return 0; |
| 1092 | } |
| 1093 | hlua_sethlua(lua); |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1094 | lua->Tref = luaL_ref(hlua_states[state_id], LUA_REGISTRYINDEX); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1095 | lua->task = task; |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1096 | if (!already_safe) |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1097 | RESET_SAFE_LJMP_PARENT(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1098 | return 1; |
| 1099 | } |
| 1100 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1101 | /* Used to destroy the Lua coroutine when the attached stream or task |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1102 | * is destroyed. The destroy also the memory context. The struct "lua" |
| 1103 | * is not freed. |
| 1104 | */ |
| 1105 | void hlua_ctx_destroy(struct hlua *lua) |
| 1106 | { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 1107 | if (!lua) |
Thierry FOURNIER | a718b29 | 2015-03-04 16:48:34 +0100 | [diff] [blame] | 1108 | return; |
| 1109 | |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 1110 | if (!lua->T) |
| 1111 | goto end; |
| 1112 | |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 1113 | /* Purge all the pending signals. */ |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1114 | notification_purge(&lua->com); |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 1115 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1116 | if (!SET_SAFE_LJMP(lua)) |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 1117 | return; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1118 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1119 | RESET_SAFE_LJMP(lua); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 1120 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1121 | if (!SET_SAFE_LJMP_PARENT(lua)) |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 1122 | return; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1123 | luaL_unref(hlua_states[lua->state_id], LUA_REGISTRYINDEX, lua->Tref); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1124 | RESET_SAFE_LJMP_PARENT(lua); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 1125 | /* Forces a garbage collecting process. If the Lua program is finished |
| 1126 | * without error, we run the GC on the thread pointer. Its freed all |
| 1127 | * the unused memory. |
| 1128 | * If the thread is finnish with an error or is currently yielded, |
| 1129 | * it seems that the GC applied on the thread doesn't clean anything, |
| 1130 | * so e run the GC on the main thread. |
| 1131 | * NOTE: maybe this action locks all the Lua threads untiml the en of |
| 1132 | * the garbage collection. |
| 1133 | */ |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 1134 | if (lua->gc_count) { |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1135 | if (!SET_SAFE_LJMP_PARENT(lua)) |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 1136 | return; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1137 | lua_gc(hlua_states[lua->state_id], LUA_GCCOLLECT, 0); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1138 | RESET_SAFE_LJMP_PARENT(lua); |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 1139 | } |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 1140 | |
Thierry FOURNIER | a7b536b | 2015-09-21 22:50:24 +0200 | [diff] [blame] | 1141 | lua->T = NULL; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 1142 | |
| 1143 | end: |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 1144 | pool_free(pool_head_hlua, lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1145 | } |
| 1146 | |
| 1147 | /* This function is used to restore the Lua context when a coroutine |
| 1148 | * fails. This function copy the common memory between old coroutine |
| 1149 | * and the new coroutine. The old coroutine is destroyed, and its |
| 1150 | * replaced by the new coroutine. |
| 1151 | * If the flag "keep_msg" is set, the last entry of the old is assumed |
| 1152 | * as string error message and it is copied in the new stack. |
| 1153 | */ |
| 1154 | static int hlua_ctx_renew(struct hlua *lua, int keep_msg) |
| 1155 | { |
| 1156 | lua_State *T; |
| 1157 | int new_ref; |
| 1158 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1159 | /* New Lua coroutine. */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1160 | T = lua_newthread(hlua_states[lua->state_id]); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1161 | if (!T) |
| 1162 | return 0; |
| 1163 | |
| 1164 | /* Copy last error message. */ |
| 1165 | if (keep_msg) |
| 1166 | lua_xmove(lua->T, T, 1); |
| 1167 | |
| 1168 | /* Copy data between the coroutines. */ |
| 1169 | lua_rawgeti(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 1170 | lua_xmove(lua->T, T, 1); |
Ilya Shipitsin | 46a030c | 2020-07-05 16:36:08 +0500 | [diff] [blame] | 1171 | new_ref = luaL_ref(T, LUA_REGISTRYINDEX); /* Value popped. */ |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1172 | |
| 1173 | /* Destroy old data. */ |
| 1174 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 1175 | |
| 1176 | /* The thread is garbage collected by Lua. */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1177 | luaL_unref(hlua_states[lua->state_id], LUA_REGISTRYINDEX, lua->Tref); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1178 | |
| 1179 | /* Fill the struct with the new coroutine values. */ |
| 1180 | lua->Mref = new_ref; |
| 1181 | lua->T = T; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1182 | lua->Tref = luaL_ref(hlua_states[lua->state_id], LUA_REGISTRYINDEX); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1183 | |
| 1184 | /* Set context. */ |
| 1185 | hlua_sethlua(lua); |
| 1186 | |
| 1187 | return 1; |
| 1188 | } |
| 1189 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1190 | void hlua_hook(lua_State *L, lua_Debug *ar) |
| 1191 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 1192 | struct hlua *hlua; |
| 1193 | |
| 1194 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 1195 | hlua = hlua_gethlua(L); |
| 1196 | if (!hlua) |
| 1197 | return; |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1198 | |
| 1199 | /* Lua cannot yield when its returning from a function, |
| 1200 | * so, we can fix the interrupt hook to 1 instruction, |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1201 | * expecting that the function is finished. |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1202 | */ |
| 1203 | if (lua_gethookmask(L) & LUA_MASKRET) { |
| 1204 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, 1); |
| 1205 | return; |
| 1206 | } |
| 1207 | |
| 1208 | /* restore the interrupt condition. */ |
| 1209 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 1210 | |
| 1211 | /* If we interrupt the Lua processing in yieldable state, we yield. |
| 1212 | * If the state is not yieldable, trying yield causes an error. |
| 1213 | */ |
| 1214 | if (lua_isyieldable(L)) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 1215 | MAY_LJMP(hlua_yieldk(L, 0, 0, NULL, TICK_ETERNITY, HLUA_CTRLYIELD)); |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1216 | |
Thierry FOURNIER | a85cfb1 | 2015-03-13 14:50:06 +0100 | [diff] [blame] | 1217 | /* If we cannot yield, update the clock and check the timeout. */ |
| 1218 | tv_update_date(0, 1); |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1219 | hlua->run_time += now_ms - hlua->start_time; |
| 1220 | if (hlua->max_time && hlua->run_time >= hlua->max_time) { |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1221 | lua_pushfstring(L, "execution timeout"); |
| 1222 | WILL_LJMP(lua_error(L)); |
| 1223 | } |
| 1224 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1225 | /* Update the start time. */ |
| 1226 | hlua->start_time = now_ms; |
| 1227 | |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1228 | /* Try to interrupt the process at the end of the current |
| 1229 | * unyieldable function. |
| 1230 | */ |
| 1231 | 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] | 1232 | } |
| 1233 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1234 | /* This function start or resumes the Lua stack execution. If the flag |
| 1235 | * "yield_allowed" if no set and the LUA stack execution returns a yield |
| 1236 | * The function return an error. |
| 1237 | * |
| 1238 | * The function can returns 4 values: |
| 1239 | * - HLUA_E_OK : The execution is terminated without any errors. |
| 1240 | * - HLUA_E_AGAIN : The execution must continue at the next associated |
| 1241 | * task wakeup. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1242 | * - 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] | 1243 | * the top of the stack. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1244 | * - HLUA_E_ERR : An error has occurred without error message. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1245 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1246 | * 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] | 1247 | * LUA code. |
| 1248 | */ |
| 1249 | static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed) |
| 1250 | { |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1251 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
| 1252 | int nres; |
| 1253 | #endif |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1254 | int ret; |
| 1255 | const char *msg; |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1256 | const char *trace; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1257 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1258 | /* Initialise run time counter. */ |
| 1259 | if (!HLUA_IS_RUNNING(lua)) |
| 1260 | lua->run_time = 0; |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1261 | |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1262 | /* Lock the whole Lua execution. This lock must be before the |
| 1263 | * label "resume_execution". |
| 1264 | */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1265 | if (lua->state_id == 0) |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1266 | HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1267 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1268 | resume_execution: |
| 1269 | |
| 1270 | /* This hook interrupts the Lua processing each 'hlua_nb_instruction' |
| 1271 | * instructions. it is used for preventing infinite loops. |
| 1272 | */ |
| 1273 | lua_sethook(lua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 1274 | |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1275 | /* Remove all flags except the running flags. */ |
Thierry FOURNIER | 2f3867f | 2015-09-28 01:02:01 +0200 | [diff] [blame] | 1276 | HLUA_SET_RUN(lua); |
| 1277 | HLUA_CLR_CTRLYIELD(lua); |
| 1278 | HLUA_CLR_WAKERESWR(lua); |
| 1279 | HLUA_CLR_WAKEREQWR(lua); |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1280 | |
Christopher Faulet | bc275a9 | 2020-02-26 14:55:16 +0100 | [diff] [blame] | 1281 | /* Update the start time and reset wake_time. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1282 | lua->start_time = now_ms; |
Christopher Faulet | bc275a9 | 2020-02-26 14:55:16 +0100 | [diff] [blame] | 1283 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1284 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1285 | /* Call the function. */ |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1286 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1287 | 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] | 1288 | #else |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1289 | ret = lua_resume(lua->T, hlua_states[lua->state_id], lua->nargs); |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1290 | #endif |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1291 | switch (ret) { |
| 1292 | |
| 1293 | case LUA_OK: |
| 1294 | ret = HLUA_E_OK; |
| 1295 | break; |
| 1296 | |
| 1297 | case LUA_YIELD: |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1298 | /* Check if the execution timeout is expired. It it is the case, we |
| 1299 | * break the Lua execution. |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1300 | */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1301 | tv_update_date(0, 1); |
| 1302 | lua->run_time += now_ms - lua->start_time; |
| 1303 | if (lua->max_time && lua->run_time > lua->max_time) { |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1304 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1305 | ret = HLUA_E_ETMOUT; |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1306 | break; |
| 1307 | } |
| 1308 | /* Process the forced yield. if the general yield is not allowed or |
| 1309 | * if no task were associated this the current Lua execution |
| 1310 | * coroutine, we resume the execution. Else we want to return in the |
| 1311 | * scheduler and we want to be waked up again, to continue the |
| 1312 | * current Lua execution. So we schedule our own task. |
| 1313 | */ |
| 1314 | if (HLUA_IS_CTRLYIELDING(lua)) { |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1315 | if (!yield_allowed || !lua->task) |
| 1316 | goto resume_execution; |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1317 | task_wakeup(lua->task, TASK_WOKEN_MSG); |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1318 | } |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1319 | if (!yield_allowed) { |
| 1320 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1321 | ret = HLUA_E_YIELD; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1322 | break; |
| 1323 | } |
| 1324 | ret = HLUA_E_AGAIN; |
| 1325 | break; |
| 1326 | |
| 1327 | case LUA_ERRRUN: |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1328 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1329 | /* Special exit case. The traditional exit is returned as an error |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1330 | * because the errors ares the only one mean to return immediately |
| 1331 | * from and lua execution. |
| 1332 | */ |
| 1333 | if (lua->flags & HLUA_EXIT) { |
| 1334 | ret = HLUA_E_OK; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 1335 | hlua_ctx_renew(lua, 1); |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1336 | break; |
| 1337 | } |
| 1338 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1339 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1340 | if (!lua_checkstack(lua->T, 1)) { |
| 1341 | ret = HLUA_E_ERR; |
| 1342 | break; |
| 1343 | } |
| 1344 | msg = lua_tostring(lua->T, -1); |
| 1345 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1346 | lua_pop(lua->T, 1); |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 1347 | trace = hlua_traceback(lua->T, ", "); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1348 | if (msg) |
Thierry Fournier | 46278ff | 2020-11-29 11:48:12 +0100 | [diff] [blame] | 1349 | 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] | 1350 | else |
Thierry Fournier | 46278ff | 2020-11-29 11:48:12 +0100 | [diff] [blame] | 1351 | 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] | 1352 | ret = HLUA_E_ERRMSG; |
| 1353 | break; |
| 1354 | |
| 1355 | case LUA_ERRMEM: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1356 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1357 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1358 | ret = HLUA_E_NOMEM; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1359 | break; |
| 1360 | |
| 1361 | case LUA_ERRERR: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1362 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1363 | if (!lua_checkstack(lua->T, 1)) { |
| 1364 | ret = HLUA_E_ERR; |
| 1365 | break; |
| 1366 | } |
| 1367 | msg = lua_tostring(lua->T, -1); |
| 1368 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1369 | lua_pop(lua->T, 1); |
| 1370 | if (msg) |
Thierry Fournier | 46278ff | 2020-11-29 11:48:12 +0100 | [diff] [blame] | 1371 | 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] | 1372 | else |
Thierry Fournier | 46278ff | 2020-11-29 11:48:12 +0100 | [diff] [blame] | 1373 | 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] | 1374 | ret = HLUA_E_ERRMSG; |
| 1375 | break; |
| 1376 | |
| 1377 | default: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1378 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1379 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1380 | ret = HLUA_E_ERR; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1381 | break; |
| 1382 | } |
| 1383 | |
| 1384 | switch (ret) { |
| 1385 | case HLUA_E_AGAIN: |
| 1386 | break; |
| 1387 | |
| 1388 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1389 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1390 | hlua_ctx_renew(lua, 1); |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1391 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1392 | break; |
| 1393 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1394 | case HLUA_E_ETMOUT: |
| 1395 | case HLUA_E_NOMEM: |
| 1396 | case HLUA_E_YIELD: |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1397 | case HLUA_E_ERR: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1398 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1399 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1400 | hlua_ctx_renew(lua, 0); |
| 1401 | break; |
| 1402 | |
| 1403 | case HLUA_E_OK: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1404 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1405 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1406 | break; |
| 1407 | } |
| 1408 | |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1409 | /* This is the main exit point, remove the Lua lock. */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1410 | if (lua->state_id == 0) |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1411 | HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1412 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1413 | return ret; |
| 1414 | } |
| 1415 | |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1416 | /* This function exit the current code. */ |
| 1417 | __LJMP static int hlua_done(lua_State *L) |
| 1418 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 1419 | struct hlua *hlua; |
| 1420 | |
| 1421 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 1422 | hlua = hlua_gethlua(L); |
| 1423 | if (!hlua) |
| 1424 | return 0; |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1425 | |
| 1426 | hlua->flags |= HLUA_EXIT; |
| 1427 | WILL_LJMP(lua_error(L)); |
| 1428 | |
| 1429 | return 0; |
| 1430 | } |
| 1431 | |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1432 | /* This function is an LUA binding. It provides a function |
| 1433 | * for deleting ACL from a referenced ACL file. |
| 1434 | */ |
| 1435 | __LJMP static int hlua_del_acl(lua_State *L) |
| 1436 | { |
| 1437 | const char *name; |
| 1438 | const char *key; |
| 1439 | struct pat_ref *ref; |
| 1440 | |
| 1441 | MAY_LJMP(check_args(L, 2, "del_acl")); |
| 1442 | |
| 1443 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1444 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1445 | |
| 1446 | ref = pat_ref_lookup(name); |
| 1447 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1448 | WILL_LJMP(luaL_error(L, "'del_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1449 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1450 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1451 | pat_ref_delete(ref, key); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1452 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1453 | return 0; |
| 1454 | } |
| 1455 | |
| 1456 | /* This function is an LUA binding. It provides a function |
| 1457 | * for deleting map entry from a referenced map file. |
| 1458 | */ |
| 1459 | static int hlua_del_map(lua_State *L) |
| 1460 | { |
| 1461 | const char *name; |
| 1462 | const char *key; |
| 1463 | struct pat_ref *ref; |
| 1464 | |
| 1465 | MAY_LJMP(check_args(L, 2, "del_map")); |
| 1466 | |
| 1467 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1468 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1469 | |
| 1470 | ref = pat_ref_lookup(name); |
| 1471 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1472 | WILL_LJMP(luaL_error(L, "'del_map': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1473 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1474 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1475 | pat_ref_delete(ref, key); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1476 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1477 | return 0; |
| 1478 | } |
| 1479 | |
| 1480 | /* This function is an LUA binding. It provides a function |
| 1481 | * for adding ACL pattern from a referenced ACL file. |
| 1482 | */ |
| 1483 | static int hlua_add_acl(lua_State *L) |
| 1484 | { |
| 1485 | const char *name; |
| 1486 | const char *key; |
| 1487 | struct pat_ref *ref; |
| 1488 | |
| 1489 | MAY_LJMP(check_args(L, 2, "add_acl")); |
| 1490 | |
| 1491 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1492 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1493 | |
| 1494 | ref = pat_ref_lookup(name); |
| 1495 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1496 | WILL_LJMP(luaL_error(L, "'add_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1497 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1498 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1499 | if (pat_ref_find_elt(ref, key) == NULL) |
| 1500 | pat_ref_add(ref, key, NULL, NULL); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1501 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1502 | return 0; |
| 1503 | } |
| 1504 | |
| 1505 | /* This function is an LUA binding. It provides a function |
| 1506 | * for setting map pattern and sample from a referenced map |
| 1507 | * file. |
| 1508 | */ |
| 1509 | static int hlua_set_map(lua_State *L) |
| 1510 | { |
| 1511 | const char *name; |
| 1512 | const char *key; |
| 1513 | const char *value; |
| 1514 | struct pat_ref *ref; |
| 1515 | |
| 1516 | MAY_LJMP(check_args(L, 3, "set_map")); |
| 1517 | |
| 1518 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1519 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1520 | value = MAY_LJMP(luaL_checkstring(L, 3)); |
| 1521 | |
| 1522 | ref = pat_ref_lookup(name); |
| 1523 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1524 | WILL_LJMP(luaL_error(L, "'set_map': unknown map file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1525 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1526 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1527 | if (pat_ref_find_elt(ref, key) != NULL) |
| 1528 | pat_ref_set(ref, key, value, NULL); |
| 1529 | else |
| 1530 | pat_ref_add(ref, key, value, NULL); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1531 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1532 | return 0; |
| 1533 | } |
| 1534 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1535 | /* A class is a lot of memory that contain data. This data can be a table, |
| 1536 | * 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] | 1537 | * metatable have an original version registered in the global context with |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1538 | * the name of the object (_G[<name>] = <metable> ). |
| 1539 | * |
| 1540 | * A metable is a table that modify the standard behavior of a standard |
| 1541 | * access to the associated data. The entries of this new metatable are |
| 1542 | * defined as is: |
| 1543 | * |
| 1544 | * http://lua-users.org/wiki/MetatableEvents |
| 1545 | * |
| 1546 | * __index |
| 1547 | * |
| 1548 | * we access an absent field in a table, the result is nil. This is |
| 1549 | * true, but it is not the whole truth. Actually, such access triggers |
| 1550 | * the interpreter to look for an __index metamethod: If there is no |
| 1551 | * such method, as usually happens, then the access results in nil; |
| 1552 | * otherwise, the metamethod will provide the result. |
| 1553 | * |
| 1554 | * Control 'prototype' inheritance. When accessing "myTable[key]" and |
| 1555 | * the key does not appear in the table, but the metatable has an __index |
| 1556 | * property: |
| 1557 | * |
| 1558 | * - if the value is a function, the function is called, passing in the |
| 1559 | * table and the key; the return value of that function is returned as |
| 1560 | * the result. |
| 1561 | * |
| 1562 | * - if the value is another table, the value of the key in that table is |
| 1563 | * asked for and returned (and if it doesn't exist in that table, but that |
| 1564 | * table's metatable has an __index property, then it continues on up) |
| 1565 | * |
| 1566 | * - Use "rawget(myTable,key)" to skip this metamethod. |
| 1567 | * |
| 1568 | * http://www.lua.org/pil/13.4.1.html |
| 1569 | * |
| 1570 | * __newindex |
| 1571 | * |
| 1572 | * Like __index, but control property assignment. |
| 1573 | * |
| 1574 | * __mode - Control weak references. A string value with one or both |
| 1575 | * of the characters 'k' and 'v' which specifies that the the |
| 1576 | * keys and/or values in the table are weak references. |
| 1577 | * |
| 1578 | * __call - Treat a table like a function. When a table is followed by |
| 1579 | * parenthesis such as "myTable( 'foo' )" and the metatable has |
| 1580 | * a __call key pointing to a function, that function is invoked |
| 1581 | * (passing any specified arguments) and the return value is |
| 1582 | * returned. |
| 1583 | * |
| 1584 | * __metatable - Hide the metatable. When "getmetatable( myTable )" is |
| 1585 | * called, if the metatable for myTable has a __metatable |
| 1586 | * key, the value of that key is returned instead of the |
| 1587 | * actual metatable. |
| 1588 | * |
| 1589 | * __tostring - Control string representation. When the builtin |
| 1590 | * "tostring( myTable )" function is called, if the metatable |
| 1591 | * for myTable has a __tostring property set to a function, |
| 1592 | * that function is invoked (passing myTable to it) and the |
| 1593 | * return value is used as the string representation. |
| 1594 | * |
| 1595 | * __len - Control table length. When the table length is requested using |
| 1596 | * the length operator ( '#' ), if the metatable for myTable has |
| 1597 | * a __len key pointing to a function, that function is invoked |
| 1598 | * (passing myTable to it) and the return value used as the value |
| 1599 | * of "#myTable". |
| 1600 | * |
| 1601 | * __gc - Userdata finalizer code. When userdata is set to be garbage |
| 1602 | * collected, if the metatable has a __gc field pointing to a |
| 1603 | * function, that function is first invoked, passing the userdata |
| 1604 | * to it. The __gc metamethod is not called for tables. |
| 1605 | * (See http://lua-users.org/lists/lua-l/2006-11/msg00508.html) |
| 1606 | * |
| 1607 | * Special metamethods for redefining standard operators: |
| 1608 | * http://www.lua.org/pil/13.1.html |
| 1609 | * |
| 1610 | * __add "+" |
| 1611 | * __sub "-" |
| 1612 | * __mul "*" |
| 1613 | * __div "/" |
| 1614 | * __unm "!" |
| 1615 | * __pow "^" |
| 1616 | * __concat ".." |
| 1617 | * |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1618 | * Special methods for redefining standard relations |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1619 | * http://www.lua.org/pil/13.2.html |
| 1620 | * |
| 1621 | * __eq "==" |
| 1622 | * __lt "<" |
| 1623 | * __le "<=" |
| 1624 | */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1625 | |
| 1626 | /* |
| 1627 | * |
| 1628 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1629 | * Class Map |
| 1630 | * |
| 1631 | * |
| 1632 | */ |
| 1633 | |
| 1634 | /* Returns a struct hlua_map if the stack entry "ud" is |
| 1635 | * a class session, otherwise it throws an error. |
| 1636 | */ |
| 1637 | __LJMP static struct map_descriptor *hlua_checkmap(lua_State *L, int ud) |
| 1638 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1639 | return MAY_LJMP(hlua_checkudata(L, ud, class_map_ref)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1640 | } |
| 1641 | |
| 1642 | /* This function is the map constructor. It don't need |
| 1643 | * the class Map object. It creates and return a new Map |
| 1644 | * object. It must be called only during "body" or "init" |
| 1645 | * context because it process some filesystem accesses. |
| 1646 | */ |
| 1647 | __LJMP static int hlua_map_new(struct lua_State *L) |
| 1648 | { |
| 1649 | const char *fn; |
| 1650 | int match = PAT_MATCH_STR; |
| 1651 | struct sample_conv conv; |
| 1652 | const char *file = ""; |
| 1653 | int line = 0; |
| 1654 | lua_Debug ar; |
| 1655 | char *err = NULL; |
| 1656 | struct arg args[2]; |
| 1657 | |
| 1658 | if (lua_gettop(L) < 1 || lua_gettop(L) > 2) |
| 1659 | WILL_LJMP(luaL_error(L, "'new' needs at least 1 argument.")); |
| 1660 | |
| 1661 | fn = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1662 | |
| 1663 | if (lua_gettop(L) >= 2) { |
| 1664 | match = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 1665 | if (match < 0 || match >= PAT_MATCH_NUM) |
| 1666 | WILL_LJMP(luaL_error(L, "'new' needs a valid match method.")); |
| 1667 | } |
| 1668 | |
| 1669 | /* Get Lua filename and line number. */ |
| 1670 | if (lua_getstack(L, 1, &ar)) { /* check function at level */ |
| 1671 | lua_getinfo(L, "Sl", &ar); /* get info about it */ |
| 1672 | if (ar.currentline > 0) { /* is there info? */ |
| 1673 | file = ar.short_src; |
| 1674 | line = ar.currentline; |
| 1675 | } |
| 1676 | } |
| 1677 | |
| 1678 | /* fill fake sample_conv struct. */ |
| 1679 | conv.kw = ""; /* unused. */ |
| 1680 | conv.process = NULL; /* unused. */ |
| 1681 | conv.arg_mask = 0; /* unused. */ |
| 1682 | conv.val_args = NULL; /* unused. */ |
| 1683 | conv.out_type = SMP_T_STR; |
| 1684 | conv.private = (void *)(long)match; |
| 1685 | switch (match) { |
| 1686 | case PAT_MATCH_STR: conv.in_type = SMP_T_STR; break; |
| 1687 | case PAT_MATCH_BEG: conv.in_type = SMP_T_STR; break; |
| 1688 | case PAT_MATCH_SUB: conv.in_type = SMP_T_STR; break; |
| 1689 | case PAT_MATCH_DIR: conv.in_type = SMP_T_STR; break; |
| 1690 | case PAT_MATCH_DOM: conv.in_type = SMP_T_STR; break; |
| 1691 | case PAT_MATCH_END: conv.in_type = SMP_T_STR; break; |
| 1692 | case PAT_MATCH_REG: conv.in_type = SMP_T_STR; break; |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1693 | case PAT_MATCH_INT: conv.in_type = SMP_T_SINT; break; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1694 | case PAT_MATCH_IP: conv.in_type = SMP_T_ADDR; break; |
| 1695 | default: |
| 1696 | WILL_LJMP(luaL_error(L, "'new' doesn't support this match mode.")); |
| 1697 | } |
| 1698 | |
| 1699 | /* fill fake args. */ |
| 1700 | args[0].type = ARGT_STR; |
Christopher Faulet | 73292e9 | 2020-08-06 08:40:09 +0200 | [diff] [blame] | 1701 | args[0].data.str.area = strdup(fn); |
| 1702 | args[0].data.str.data = strlen(fn); |
| 1703 | args[0].data.str.size = args[0].data.str.data+1; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1704 | args[1].type = ARGT_STOP; |
| 1705 | |
| 1706 | /* load the map. */ |
| 1707 | if (!sample_load_map(args, &conv, file, line, &err)) { |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 1708 | /* error case: we can't use luaL_error because we must |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1709 | * free the err variable. |
| 1710 | */ |
| 1711 | luaL_where(L, 1); |
| 1712 | lua_pushfstring(L, "'new': %s.", err); |
| 1713 | lua_concat(L, 2); |
| 1714 | free(err); |
Christopher Faulet | 6ad7df4 | 2020-08-07 11:45:18 +0200 | [diff] [blame] | 1715 | chunk_destroy(&args[0].data.str); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1716 | WILL_LJMP(lua_error(L)); |
| 1717 | } |
| 1718 | |
| 1719 | /* create the lua object. */ |
| 1720 | lua_newtable(L); |
| 1721 | lua_pushlightuserdata(L, args[0].data.map); |
| 1722 | lua_rawseti(L, -2, 0); |
| 1723 | |
| 1724 | /* Pop a class Map metatable and affect it to the userdata. */ |
| 1725 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_map_ref); |
| 1726 | lua_setmetatable(L, -2); |
| 1727 | |
| 1728 | |
| 1729 | return 1; |
| 1730 | } |
| 1731 | |
| 1732 | __LJMP static inline int _hlua_map_lookup(struct lua_State *L, int str) |
| 1733 | { |
| 1734 | struct map_descriptor *desc; |
| 1735 | struct pattern *pat; |
| 1736 | struct sample smp; |
| 1737 | |
| 1738 | MAY_LJMP(check_args(L, 2, "lookup")); |
| 1739 | desc = MAY_LJMP(hlua_checkmap(L, 1)); |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1740 | if (desc->pat.expect_type == SMP_T_SINT) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1741 | smp.data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1742 | smp.data.u.sint = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1743 | } |
| 1744 | else { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1745 | smp.data.type = SMP_T_STR; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1746 | smp.flags = SMP_F_CONST; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1747 | 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] | 1748 | smp.data.u.str.size = smp.data.u.str.data + 1; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1749 | } |
| 1750 | |
| 1751 | pat = pattern_exec_match(&desc->pat, &smp, 1); |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1752 | if (!pat || !pat->data) { |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1753 | if (str) |
| 1754 | lua_pushstring(L, ""); |
| 1755 | else |
| 1756 | lua_pushnil(L); |
| 1757 | return 1; |
| 1758 | } |
| 1759 | |
| 1760 | /* 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] | 1761 | 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] | 1762 | return 1; |
| 1763 | } |
| 1764 | |
| 1765 | __LJMP static int hlua_map_lookup(struct lua_State *L) |
| 1766 | { |
| 1767 | return _hlua_map_lookup(L, 0); |
| 1768 | } |
| 1769 | |
| 1770 | __LJMP static int hlua_map_slookup(struct lua_State *L) |
| 1771 | { |
| 1772 | return _hlua_map_lookup(L, 1); |
| 1773 | } |
| 1774 | |
| 1775 | /* |
| 1776 | * |
| 1777 | * |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1778 | * Class Socket |
| 1779 | * |
| 1780 | * |
| 1781 | */ |
| 1782 | |
| 1783 | __LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud) |
| 1784 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1785 | return MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1786 | } |
| 1787 | |
| 1788 | /* 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] | 1789 | * connection. It is used for notify space available to send or data |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1790 | * received. |
| 1791 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1792 | static void hlua_socket_handler(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1793 | { |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1794 | struct stream_interface *si = appctx->owner; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1795 | |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1796 | if (appctx->ctx.hlua_cosocket.die) { |
| 1797 | si_shutw(si); |
| 1798 | si_shutr(si); |
| 1799 | si_ic(si)->flags |= CF_READ_NULL; |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1800 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1801 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1802 | stream_shutdown(si_strm(si), SF_ERR_KILLED); |
| 1803 | } |
| 1804 | |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 1805 | /* If we can't write, wakeup the pending write signals. */ |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1806 | if (channel_output_closed(si_ic(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1807 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1808 | |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 1809 | /* If we can't read, wakeup the pending read signals. */ |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1810 | if (channel_input_closed(si_oc(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1811 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1812 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 1813 | /* if the connection is not established, inform the stream that we want |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 1814 | * to be notified whenever the connection completes. |
| 1815 | */ |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 1816 | if (si_opposite(si)->state < SI_ST_EST) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 1817 | si_cant_get(si); |
Willy Tarreau | 12c2423 | 2018-12-06 15:29:50 +0100 | [diff] [blame] | 1818 | si_rx_conn_blk(si); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1819 | si_rx_endp_more(si); |
Willy Tarreau | d4da196 | 2015-04-20 01:31:23 +0200 | [diff] [blame] | 1820 | return; |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 1821 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1822 | |
| 1823 | /* This function is called after the connect. */ |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 1824 | appctx->ctx.hlua_cosocket.connected = 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1825 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1826 | /* 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] | 1827 | if (channel_may_recv(si_ic(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1828 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1829 | |
| 1830 | /* Wake the tasks which wants to read if the buffer contains data. */ |
Thierry FOURNIER | eba6f64 | 2015-09-26 22:01:07 +0200 | [diff] [blame] | 1831 | if (!channel_is_empty(si_oc(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1832 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1833 | |
| 1834 | /* Some data were injected in the buffer, notify the stream |
| 1835 | * interface. |
| 1836 | */ |
| 1837 | if (!channel_is_empty(si_ic(si))) |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 1838 | si_update(si); |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1839 | |
| 1840 | /* If write notifications are registered, we considers we want |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1841 | * to write, so we clear the blocking flag. |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1842 | */ |
| 1843 | if (notification_registered(&appctx->ctx.hlua_cosocket.wake_on_write)) |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1844 | si_rx_endp_more(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1845 | } |
| 1846 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1847 | /* This function is called when the "struct stream" is destroyed. |
| 1848 | * Remove the link from the object to this stream. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1849 | * Wake all the pending signals. |
| 1850 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1851 | static void hlua_socket_release(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1852 | { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1853 | struct xref *peer; |
| 1854 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1855 | /* Remove my link in the original object. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1856 | peer = xref_get_peer_and_lock(&appctx->ctx.hlua_cosocket.xref); |
| 1857 | if (peer) |
| 1858 | xref_disconnect(&appctx->ctx.hlua_cosocket.xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1859 | |
| 1860 | /* Wake all the task waiting for me. */ |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1861 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1862 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1863 | } |
| 1864 | |
| 1865 | /* If the garbage collectio of the object is launch, nobody |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1866 | * uses this object. If the stream does not exists, just quit. |
| 1867 | * Send the shutdown signal to the stream. In some cases, |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1868 | * pending signal can rest in the read and write lists. destroy |
| 1869 | * it. |
| 1870 | */ |
| 1871 | __LJMP static int hlua_socket_gc(lua_State *L) |
| 1872 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1873 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1874 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1875 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1876 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1877 | MAY_LJMP(check_args(L, 1, "__gc")); |
| 1878 | |
| 1879 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1880 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1881 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1882 | return 0; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1883 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1884 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1885 | /* Set the flag which destroy the session. */ |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1886 | appctx->ctx.hlua_cosocket.die = 1; |
| 1887 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1888 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1889 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1890 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1891 | return 0; |
| 1892 | } |
| 1893 | |
| 1894 | /* The close function send shutdown signal and break the |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1895 | * links between the stream and the object. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1896 | */ |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 1897 | __LJMP static int hlua_socket_close_helper(lua_State *L) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1898 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1899 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1900 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1901 | struct xref *peer; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 1902 | struct hlua *hlua; |
| 1903 | |
| 1904 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 1905 | hlua = hlua_gethlua(L); |
| 1906 | if (!hlua) |
| 1907 | return 0; |
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 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 1910 | |
| 1911 | /* Check if we run on the same thread than the xreator thread. |
| 1912 | * We cannot access to the socket if the thread is different. |
| 1913 | */ |
| 1914 | if (socket->tid != tid) |
| 1915 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 1916 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1917 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1918 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1919 | return 0; |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 1920 | |
| 1921 | hlua->gc_count--; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1922 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1923 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1924 | /* Set the flag which destroy the session. */ |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1925 | appctx->ctx.hlua_cosocket.die = 1; |
| 1926 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1927 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1928 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1929 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1930 | return 0; |
| 1931 | } |
| 1932 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 1933 | /* The close function calls close_helper. |
| 1934 | */ |
| 1935 | __LJMP static int hlua_socket_close(lua_State *L) |
| 1936 | { |
| 1937 | MAY_LJMP(check_args(L, 1, "close")); |
| 1938 | return hlua_socket_close_helper(L); |
| 1939 | } |
| 1940 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1941 | /* This Lua function assumes that the stack contain three parameters. |
| 1942 | * 1 - USERDATA containing a struct socket |
| 1943 | * 2 - INTEGER with values of the macro defined below |
| 1944 | * If the integer is -1, we must read at most one line. |
| 1945 | * If the integer is -2, we ust read all the data until the |
| 1946 | * end of the stream. |
| 1947 | * If the integer is positive value, we must read a number of |
| 1948 | * bytes corresponding to this value. |
| 1949 | */ |
| 1950 | #define HLSR_READ_LINE (-1) |
| 1951 | #define HLSR_READ_ALL (-2) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1952 | __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] | 1953 | { |
| 1954 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 1955 | int wanted = lua_tointeger(L, 2); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 1956 | struct hlua *hlua; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1957 | struct appctx *appctx; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 1958 | size_t len; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1959 | int nblk; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 1960 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 1961 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 1962 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 1963 | size_t len2; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 1964 | int skip_at_end = 0; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 1965 | struct channel *oc; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1966 | struct stream_interface *si; |
| 1967 | struct stream *s; |
| 1968 | struct xref *peer; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1969 | int missing_bytes; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1970 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 1971 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 1972 | hlua = hlua_gethlua(L); |
| 1973 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1974 | /* Check if this lua stack is schedulable. */ |
| 1975 | if (!hlua || !hlua->task) |
| 1976 | WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in " |
| 1977 | "'frontend', 'backend' or 'task'")); |
| 1978 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 1979 | /* Check if we run on the same thread than the xreator thread. |
| 1980 | * We cannot access to the socket if the thread is different. |
| 1981 | */ |
| 1982 | if (socket->tid != tid) |
| 1983 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 1984 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1985 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1986 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1987 | if (!peer) |
| 1988 | goto no_peer; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1989 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 1990 | si = appctx->owner; |
| 1991 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1992 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1993 | oc = &s->res; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1994 | if (wanted == HLSR_READ_LINE) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1995 | /* Read line. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1996 | nblk = co_getline_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1997 | if (nblk < 0) /* Connection close. */ |
| 1998 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1999 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2000 | goto connection_empty; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 2001 | |
| 2002 | /* remove final \r\n. */ |
| 2003 | if (nblk == 1) { |
| 2004 | if (blk1[len1-1] == '\n') { |
| 2005 | len1--; |
| 2006 | skip_at_end++; |
| 2007 | if (blk1[len1-1] == '\r') { |
| 2008 | len1--; |
| 2009 | skip_at_end++; |
| 2010 | } |
| 2011 | } |
| 2012 | } |
| 2013 | else { |
| 2014 | if (blk2[len2-1] == '\n') { |
| 2015 | len2--; |
| 2016 | skip_at_end++; |
| 2017 | if (blk2[len2-1] == '\r') { |
| 2018 | len2--; |
| 2019 | skip_at_end++; |
| 2020 | } |
| 2021 | } |
| 2022 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2023 | } |
| 2024 | |
| 2025 | else if (wanted == HLSR_READ_ALL) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2026 | /* Read all the available data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2027 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2028 | if (nblk < 0) /* Connection close. */ |
| 2029 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2030 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2031 | goto connection_empty; |
| 2032 | } |
| 2033 | |
| 2034 | else { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2035 | /* Read a block of data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2036 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2037 | if (nblk < 0) /* Connection close. */ |
| 2038 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2039 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2040 | goto connection_empty; |
| 2041 | |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 2042 | missing_bytes = wanted - socket->b.n; |
| 2043 | if (len1 > missing_bytes) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2044 | nblk = 1; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 2045 | len1 = missing_bytes; |
| 2046 | } if (nblk == 2 && len1 + len2 > missing_bytes) |
| 2047 | len2 = missing_bytes - len1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2048 | } |
| 2049 | |
| 2050 | len = len1; |
| 2051 | |
| 2052 | luaL_addlstring(&socket->b, blk1, len1); |
| 2053 | if (nblk == 2) { |
| 2054 | len += len2; |
| 2055 | luaL_addlstring(&socket->b, blk2, len2); |
| 2056 | } |
| 2057 | |
| 2058 | /* Consume data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2059 | co_skip(oc, len + skip_at_end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2060 | |
| 2061 | /* Don't wait anything. */ |
Thierry FOURNIER | 7e4ee47 | 2018-05-25 15:03:50 +0200 | [diff] [blame] | 2062 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2063 | |
| 2064 | /* If the pattern reclaim to read all the data |
| 2065 | * in the connection, got out. |
| 2066 | */ |
| 2067 | if (wanted == HLSR_READ_ALL) |
| 2068 | goto connection_empty; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 2069 | else if (wanted >= 0 && socket->b.n < wanted) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2070 | goto connection_empty; |
| 2071 | |
| 2072 | /* Return result. */ |
| 2073 | luaL_pushresult(&socket->b); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2074 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2075 | return 1; |
| 2076 | |
| 2077 | connection_closed: |
| 2078 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2079 | xref_unlock(&socket->xref, peer); |
| 2080 | |
| 2081 | no_peer: |
| 2082 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2083 | /* If the buffer containds data. */ |
| 2084 | if (socket->b.n > 0) { |
| 2085 | luaL_pushresult(&socket->b); |
| 2086 | return 1; |
| 2087 | } |
| 2088 | lua_pushnil(L); |
| 2089 | lua_pushstring(L, "connection closed."); |
| 2090 | return 2; |
| 2091 | |
| 2092 | connection_empty: |
| 2093 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2094 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_read, hlua->task)) { |
| 2095 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2096 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2097 | } |
| 2098 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2099 | 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] | 2100 | return 0; |
| 2101 | } |
| 2102 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2103 | /* This Lua function gets two parameters. The first one can be string |
| 2104 | * or a number. If the string is "*l", the user requires one line. If |
| 2105 | * 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] | 2106 | * If the value is a number, the user require a number of bytes equal |
| 2107 | * to the value. The default value is "*l" (a line). |
| 2108 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2109 | * This parameter with a variable type is converted in integer. This |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2110 | * integer takes this values: |
| 2111 | * -1 : read a line |
| 2112 | * -2 : read all the stream |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2113 | * >0 : amount of bytes. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2114 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2115 | * The second parameter is optional. It contains a string that must be |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2116 | * concatenated with the read data. |
| 2117 | */ |
| 2118 | __LJMP static int hlua_socket_receive(struct lua_State *L) |
| 2119 | { |
| 2120 | int wanted = HLSR_READ_LINE; |
| 2121 | const char *pattern; |
Christopher Faulet | c31b200 | 2021-05-03 10:11:13 +0200 | [diff] [blame] | 2122 | int lastarg, type; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2123 | char *error; |
| 2124 | size_t len; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2125 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2126 | |
| 2127 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 2128 | WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments.")); |
| 2129 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2130 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2131 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2132 | /* Check if we run on the same thread than the xreator thread. |
| 2133 | * We cannot access to the socket if the thread is different. |
| 2134 | */ |
| 2135 | if (socket->tid != tid) |
| 2136 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2137 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2138 | /* check for pattern. */ |
| 2139 | if (lua_gettop(L) >= 2) { |
| 2140 | type = lua_type(L, 2); |
| 2141 | if (type == LUA_TSTRING) { |
| 2142 | pattern = lua_tostring(L, 2); |
| 2143 | if (strcmp(pattern, "*a") == 0) |
| 2144 | wanted = HLSR_READ_ALL; |
| 2145 | else if (strcmp(pattern, "*l") == 0) |
| 2146 | wanted = HLSR_READ_LINE; |
| 2147 | else { |
| 2148 | wanted = strtoll(pattern, &error, 10); |
| 2149 | if (*error != '\0') |
| 2150 | WILL_LJMP(luaL_error(L, "Unsupported pattern.")); |
| 2151 | } |
| 2152 | } |
| 2153 | else if (type == LUA_TNUMBER) { |
| 2154 | wanted = lua_tointeger(L, 2); |
| 2155 | if (wanted < 0) |
| 2156 | WILL_LJMP(luaL_error(L, "Unsupported size.")); |
| 2157 | } |
| 2158 | } |
| 2159 | |
| 2160 | /* Set pattern. */ |
| 2161 | lua_pushinteger(L, wanted); |
Tim Duesterhus | c6e377e | 2018-01-04 19:32:13 +0100 | [diff] [blame] | 2162 | |
| 2163 | /* Check if we would replace the top by itself. */ |
| 2164 | if (lua_gettop(L) != 2) |
| 2165 | lua_replace(L, 2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2166 | |
Christopher Faulet | c31b200 | 2021-05-03 10:11:13 +0200 | [diff] [blame] | 2167 | /* Save index of the top of the stack because since buffers are used, it |
| 2168 | * may change |
| 2169 | */ |
| 2170 | lastarg = lua_gettop(L); |
| 2171 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2172 | /* init buffer, and fill it with prefix. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2173 | luaL_buffinit(L, &socket->b); |
| 2174 | |
| 2175 | /* Check prefix. */ |
Christopher Faulet | c31b200 | 2021-05-03 10:11:13 +0200 | [diff] [blame] | 2176 | if (lastarg >= 3) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2177 | if (lua_type(L, 3) != LUA_TSTRING) |
| 2178 | WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix")); |
| 2179 | pattern = lua_tolstring(L, 3, &len); |
| 2180 | luaL_addlstring(&socket->b, pattern, len); |
| 2181 | } |
| 2182 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2183 | return __LJMP(hlua_socket_receive_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2184 | } |
| 2185 | |
| 2186 | /* Write the Lua input string in the output buffer. |
Mark Lakes | 22154b4 | 2018-01-29 14:38:40 -0800 | [diff] [blame] | 2187 | * This function returns a yield if no space is available. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2188 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2189 | 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] | 2190 | { |
| 2191 | struct hlua_socket *socket; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2192 | struct hlua *hlua; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2193 | struct appctx *appctx; |
| 2194 | size_t buf_len; |
| 2195 | const char *buf; |
| 2196 | int len; |
| 2197 | int send_len; |
| 2198 | int sent; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2199 | struct xref *peer; |
| 2200 | struct stream_interface *si; |
| 2201 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2202 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2203 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 2204 | hlua = hlua_gethlua(L); |
| 2205 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2206 | /* Check if this lua stack is schedulable. */ |
| 2207 | if (!hlua || !hlua->task) |
| 2208 | WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in " |
| 2209 | "'frontend', 'backend' or 'task'")); |
| 2210 | |
| 2211 | /* Get object */ |
| 2212 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2213 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2214 | sent = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2215 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2216 | /* Check if we run on the same thread than the xreator thread. |
| 2217 | * We cannot access to the socket if the thread is different. |
| 2218 | */ |
| 2219 | if (socket->tid != tid) |
| 2220 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2221 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2222 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2223 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2224 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2225 | lua_pushinteger(L, -1); |
| 2226 | return 1; |
| 2227 | } |
| 2228 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2229 | si = appctx->owner; |
| 2230 | s = si_strm(si); |
| 2231 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2232 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2233 | if (channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2234 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2235 | lua_pushinteger(L, -1); |
| 2236 | return 1; |
| 2237 | } |
| 2238 | |
| 2239 | /* Update the input buffer data. */ |
| 2240 | buf += sent; |
| 2241 | send_len = buf_len - sent; |
| 2242 | |
| 2243 | /* All the data are sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2244 | if (sent >= buf_len) { |
| 2245 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2246 | return 1; /* Implicitly return the length sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2247 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2248 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2249 | /* Check if the buffer is available because HAProxy doesn't allocate |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 2250 | * the request buffer if its not required. |
| 2251 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2252 | if (s->req.buf.size == 0) { |
Willy Tarreau | 581abd3 | 2018-10-25 10:21:41 +0200 | [diff] [blame] | 2253 | if (!si_alloc_ibuf(si, &appctx->buffer_wait)) |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2254 | goto hlua_socket_write_yield_return; |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 2255 | } |
| 2256 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2257 | /* Check for available space. */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2258 | len = b_room(&s->req.buf); |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2259 | if (len <= 0) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2260 | goto hlua_socket_write_yield_return; |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2261 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2262 | |
| 2263 | /* send data */ |
| 2264 | if (len < send_len) |
| 2265 | send_len = len; |
Thierry FOURNIER | 66b8919 | 2018-05-27 01:14:47 +0200 | [diff] [blame] | 2266 | len = ci_putblk(&s->req, buf, send_len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2267 | |
| 2268 | /* "Not enough space" (-1), "Buffer too little to contain |
| 2269 | * the data" (-2) are not expected because the available length |
| 2270 | * is tested. |
| 2271 | * Other unknown error are also not expected. |
| 2272 | */ |
| 2273 | if (len <= 0) { |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2274 | if (len == -1) |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2275 | s->req.flags |= CF_WAKE_WRITE; |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2276 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 2277 | MAY_LJMP(hlua_socket_close_helper(L)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2278 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2279 | lua_pushinteger(L, -1); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2280 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2281 | return 1; |
| 2282 | } |
| 2283 | |
| 2284 | /* update buffers. */ |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 2285 | appctx_wakeup(appctx); |
Willy Tarreau | de70fa1 | 2015-09-26 11:25:05 +0200 | [diff] [blame] | 2286 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2287 | s->req.rex = TICK_ETERNITY; |
| 2288 | s->res.wex = TICK_ETERNITY; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2289 | |
| 2290 | /* Update length sent. */ |
| 2291 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2292 | lua_pushinteger(L, sent + len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2293 | |
| 2294 | /* All the data buffer is sent ? */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2295 | if (sent + len >= buf_len) { |
| 2296 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2297 | return 1; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2298 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2299 | |
| 2300 | hlua_socket_write_yield_return: |
Thierry FOURNIER | ba42fcd | 2018-05-27 00:59:48 +0200 | [diff] [blame] | 2301 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2302 | xref_unlock(&socket->xref, peer); |
| 2303 | WILL_LJMP(luaL_error(L, "out of memory")); |
| 2304 | } |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2305 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2306 | 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] | 2307 | return 0; |
| 2308 | } |
| 2309 | |
| 2310 | /* This function initiate the send of data. It just check the input |
| 2311 | * parameters and push an integer in the Lua stack that contain the |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2312 | * 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] | 2313 | * "hlua_socket_write_yield" that can yield. |
| 2314 | * |
| 2315 | * The Lua function gets between 3 and 4 parameters. The first one is |
| 2316 | * the associated object. The second is a string buffer. The third is |
| 2317 | * a facultative integer that represents where is the buffer position |
| 2318 | * of the start of the data that can send. The first byte is the |
| 2319 | * position "1". The default value is "1". The fourth argument is a |
| 2320 | * facultative integer that represents where is the buffer position |
| 2321 | * of the end of the data that can send. The default is the last byte. |
| 2322 | */ |
| 2323 | static int hlua_socket_send(struct lua_State *L) |
| 2324 | { |
| 2325 | int i; |
| 2326 | int j; |
| 2327 | const char *buf; |
| 2328 | size_t buf_len; |
| 2329 | |
| 2330 | /* Check number of arguments. */ |
| 2331 | if (lua_gettop(L) < 2 || lua_gettop(L) > 4) |
| 2332 | WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments")); |
| 2333 | |
| 2334 | /* Get the string. */ |
| 2335 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
| 2336 | |
| 2337 | /* Get and check j. */ |
| 2338 | if (lua_gettop(L) == 4) { |
| 2339 | j = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 2340 | if (j < 0) |
| 2341 | j = buf_len + j + 1; |
| 2342 | if (j > buf_len) |
| 2343 | j = buf_len + 1; |
| 2344 | lua_pop(L, 1); |
| 2345 | } |
| 2346 | else |
| 2347 | j = buf_len; |
| 2348 | |
| 2349 | /* Get and check i. */ |
| 2350 | if (lua_gettop(L) == 3) { |
| 2351 | i = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2352 | if (i < 0) |
| 2353 | i = buf_len + i + 1; |
| 2354 | if (i > buf_len) |
| 2355 | i = buf_len + 1; |
| 2356 | lua_pop(L, 1); |
| 2357 | } else |
| 2358 | i = 1; |
| 2359 | |
| 2360 | /* Check bth i and j. */ |
| 2361 | if (i > j) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2362 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2363 | return 1; |
| 2364 | } |
| 2365 | if (i == 0 && j == 0) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2366 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2367 | return 1; |
| 2368 | } |
| 2369 | if (i == 0) |
| 2370 | i = 1; |
| 2371 | if (j == 0) |
| 2372 | j = 1; |
| 2373 | |
| 2374 | /* Pop the string. */ |
| 2375 | lua_pop(L, 1); |
| 2376 | |
| 2377 | /* Update the buffer length. */ |
| 2378 | buf += i - 1; |
| 2379 | buf_len = j - i + 1; |
| 2380 | lua_pushlstring(L, buf, buf_len); |
| 2381 | |
| 2382 | /* This unsigned is used to remember the amount of sent data. */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2383 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2384 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2385 | return MAY_LJMP(hlua_socket_write_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2386 | } |
| 2387 | |
Willy Tarreau | 22b0a68 | 2015-06-17 19:43:49 +0200 | [diff] [blame] | 2388 | #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] | 2389 | __LJMP static inline int hlua_socket_info(struct lua_State *L, struct sockaddr_storage *addr) |
| 2390 | { |
| 2391 | static char buffer[SOCKET_INFO_MAX_LEN]; |
| 2392 | int ret; |
| 2393 | int len; |
| 2394 | char *p; |
| 2395 | |
| 2396 | ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1); |
| 2397 | if (ret <= 0) { |
| 2398 | lua_pushnil(L); |
| 2399 | return 1; |
| 2400 | } |
| 2401 | |
| 2402 | if (ret == AF_UNIX) { |
| 2403 | lua_pushstring(L, buffer+1); |
| 2404 | return 1; |
| 2405 | } |
| 2406 | else if (ret == AF_INET6) { |
| 2407 | buffer[0] = '['; |
| 2408 | len = strlen(buffer); |
| 2409 | buffer[len] = ']'; |
| 2410 | len++; |
| 2411 | buffer[len] = ':'; |
| 2412 | len++; |
| 2413 | p = buffer; |
| 2414 | } |
| 2415 | else if (ret == AF_INET) { |
| 2416 | p = buffer + 1; |
| 2417 | len = strlen(p); |
| 2418 | p[len] = ':'; |
| 2419 | len++; |
| 2420 | } |
| 2421 | else { |
| 2422 | lua_pushnil(L); |
| 2423 | return 1; |
| 2424 | } |
| 2425 | |
| 2426 | if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) { |
| 2427 | lua_pushnil(L); |
| 2428 | return 1; |
| 2429 | } |
| 2430 | |
| 2431 | lua_pushstring(L, p); |
| 2432 | return 1; |
| 2433 | } |
| 2434 | |
| 2435 | /* Returns information about the peer of the connection. */ |
| 2436 | __LJMP static int hlua_socket_getpeername(struct lua_State *L) |
| 2437 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2438 | struct hlua_socket *socket; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2439 | struct xref *peer; |
| 2440 | struct appctx *appctx; |
| 2441 | struct stream_interface *si; |
| 2442 | struct stream *s; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2443 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2444 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2445 | MAY_LJMP(check_args(L, 1, "getpeername")); |
| 2446 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2447 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2448 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2449 | /* Check if we run on the same thread than the xreator thread. |
| 2450 | * We cannot access to the socket if the thread is different. |
| 2451 | */ |
| 2452 | if (socket->tid != tid) |
| 2453 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2454 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2455 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2456 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2457 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2458 | lua_pushnil(L); |
| 2459 | return 1; |
| 2460 | } |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2461 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2462 | si = appctx->owner; |
| 2463 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2464 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2465 | if (!s->target_addr) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2466 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2467 | lua_pushnil(L); |
| 2468 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2469 | } |
| 2470 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2471 | ret = MAY_LJMP(hlua_socket_info(L, s->target_addr)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2472 | xref_unlock(&socket->xref, peer); |
| 2473 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2474 | } |
| 2475 | |
| 2476 | /* Returns information about my connection side. */ |
| 2477 | static int hlua_socket_getsockname(struct lua_State *L) |
| 2478 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2479 | struct hlua_socket *socket; |
| 2480 | struct connection *conn; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2481 | struct appctx *appctx; |
| 2482 | struct xref *peer; |
| 2483 | struct stream_interface *si; |
| 2484 | struct stream *s; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2485 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2486 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2487 | MAY_LJMP(check_args(L, 1, "getsockname")); |
| 2488 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2489 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2490 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2491 | /* Check if we run on the same thread than the xreator thread. |
| 2492 | * We cannot access to the socket if the thread is different. |
| 2493 | */ |
| 2494 | if (socket->tid != tid) |
| 2495 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2496 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2497 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2498 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2499 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2500 | lua_pushnil(L); |
| 2501 | return 1; |
| 2502 | } |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2503 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2504 | si = appctx->owner; |
| 2505 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2506 | |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 2507 | conn = cs_conn(objt_cs(s->si[1].end)); |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2508 | if (!conn || !conn_get_src(conn)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2509 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2510 | lua_pushnil(L); |
| 2511 | return 1; |
| 2512 | } |
| 2513 | |
Willy Tarreau | 9da9a6f | 2019-07-17 14:49:44 +0200 | [diff] [blame] | 2514 | ret = hlua_socket_info(L, conn->src); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2515 | xref_unlock(&socket->xref, peer); |
| 2516 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2517 | } |
| 2518 | |
| 2519 | /* This struct define the applet. */ |
Willy Tarreau | 3057645 | 2015-04-13 13:50:30 +0200 | [diff] [blame] | 2520 | static struct applet update_applet = { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2521 | .obj_type = OBJ_TYPE_APPLET, |
| 2522 | .name = "<LUA_TCP>", |
| 2523 | .fct = hlua_socket_handler, |
| 2524 | .release = hlua_socket_release, |
| 2525 | }; |
| 2526 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2527 | __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] | 2528 | { |
| 2529 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2530 | struct hlua *hlua; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2531 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2532 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2533 | struct stream_interface *si; |
| 2534 | struct stream *s; |
| 2535 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2536 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 2537 | hlua = hlua_gethlua(L); |
| 2538 | if (!hlua) |
| 2539 | return 0; |
| 2540 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2541 | /* Check if we run on the same thread than the xreator thread. |
| 2542 | * We cannot access to the socket if the thread is different. |
| 2543 | */ |
| 2544 | if (socket->tid != tid) |
| 2545 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2546 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2547 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2548 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2549 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2550 | lua_pushnil(L); |
| 2551 | lua_pushstring(L, "Can't connect"); |
| 2552 | return 2; |
| 2553 | } |
| 2554 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2555 | si = appctx->owner; |
| 2556 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2557 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2558 | /* Check if we run on the same thread than the xreator thread. |
| 2559 | * We cannot access to the socket if the thread is different. |
| 2560 | */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2561 | if (socket->tid != tid) { |
| 2562 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2563 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2564 | } |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2565 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2566 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2567 | if (!hlua || channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2568 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2569 | lua_pushnil(L); |
| 2570 | lua_pushstring(L, "Can't connect"); |
| 2571 | return 2; |
| 2572 | } |
| 2573 | |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 2574 | appctx = __objt_appctx(s->si[0].end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2575 | |
| 2576 | /* Check for connection established. */ |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2577 | if (appctx->ctx.hlua_cosocket.connected) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2578 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2579 | lua_pushinteger(L, 1); |
| 2580 | return 1; |
| 2581 | } |
| 2582 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2583 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2584 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2585 | WILL_LJMP(luaL_error(L, "out of memory error")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2586 | } |
| 2587 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2588 | 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] | 2589 | return 0; |
| 2590 | } |
| 2591 | |
| 2592 | /* This function fail or initite the connection. */ |
| 2593 | __LJMP static int hlua_socket_connect(struct lua_State *L) |
| 2594 | { |
| 2595 | struct hlua_socket *socket; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2596 | int port = -1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2597 | const char *ip; |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2598 | struct hlua *hlua; |
| 2599 | struct appctx *appctx; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2600 | int low, high; |
| 2601 | struct sockaddr_storage *addr; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2602 | struct xref *peer; |
| 2603 | struct stream_interface *si; |
| 2604 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2605 | |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2606 | if (lua_gettop(L) < 2) |
| 2607 | WILL_LJMP(luaL_error(L, "connect: need at least 2 arguments")); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2608 | |
| 2609 | /* Get args. */ |
| 2610 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2611 | |
| 2612 | /* Check if we run on the same thread than the xreator thread. |
| 2613 | * We cannot access to the socket if the thread is different. |
| 2614 | */ |
| 2615 | if (socket->tid != tid) |
| 2616 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2617 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2618 | ip = MAY_LJMP(luaL_checkstring(L, 2)); |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 2619 | if (lua_gettop(L) >= 3) { |
| 2620 | luaL_Buffer b; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2621 | port = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2622 | |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 2623 | /* Force the ip to end with a colon, to support IPv6 addresses |
| 2624 | * that are not enclosed within square brackets. |
| 2625 | */ |
| 2626 | if (port > 0) { |
| 2627 | luaL_buffinit(L, &b); |
| 2628 | luaL_addstring(&b, ip); |
| 2629 | luaL_addchar(&b, ':'); |
| 2630 | luaL_pushresult(&b); |
| 2631 | ip = lua_tolstring(L, lua_gettop(L), NULL); |
| 2632 | } |
| 2633 | } |
| 2634 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2635 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2636 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2637 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2638 | lua_pushnil(L); |
| 2639 | return 1; |
| 2640 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2641 | |
| 2642 | /* Parse ip address. */ |
Willy Tarreau | 5fc9328 | 2020-09-16 18:25:03 +0200 | [diff] [blame] | 2643 | 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] | 2644 | if (!addr) { |
| 2645 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2646 | WILL_LJMP(luaL_error(L, "connect: cannot parse destination address '%s'", ip)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2647 | } |
Willy Tarreau | 9da9a6f | 2019-07-17 14:49:44 +0200 | [diff] [blame] | 2648 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2649 | /* Set port. */ |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2650 | if (low == 0) { |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2651 | if (addr->ss_family == AF_INET) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2652 | if (port == -1) { |
| 2653 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2654 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2655 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2656 | ((struct sockaddr_in *)addr)->sin_port = htons(port); |
| 2657 | } else if (addr->ss_family == AF_INET6) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2658 | if (port == -1) { |
| 2659 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2660 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2661 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2662 | ((struct sockaddr_in6 *)addr)->sin6_port = htons(port); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2663 | } |
| 2664 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2665 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2666 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2667 | si = appctx->owner; |
| 2668 | s = si_strm(si); |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2669 | |
Willy Tarreau | 9b7587a | 2020-10-15 07:32:10 +0200 | [diff] [blame] | 2670 | if (!sockaddr_alloc(&s->target_addr, addr, sizeof(*addr))) { |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2671 | xref_unlock(&socket->xref, peer); |
| 2672 | WILL_LJMP(luaL_error(L, "connect: internal error")); |
| 2673 | } |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2674 | s->flags |= SF_ADDR_SET; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2675 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2676 | /* Get hlua struct, or NULL if we execute from main lua state */ |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2677 | hlua = hlua_gethlua(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2678 | if (!hlua) |
| 2679 | return 0; |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2680 | |
| 2681 | /* inform the stream that we want to be notified whenever the |
| 2682 | * connection completes. |
| 2683 | */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 2684 | si_cant_get(&s->si[0]); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 2685 | si_rx_endp_more(&s->si[0]); |
Thierry FOURNIER | 8c8fbbe | 2015-09-26 17:02:35 +0200 | [diff] [blame] | 2686 | appctx_wakeup(appctx); |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2687 | |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 2688 | hlua->gc_count++; |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 2689 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2690 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2691 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2692 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2693 | } |
| 2694 | xref_unlock(&socket->xref, peer); |
PiBa-NL | 706d5ee | 2018-05-05 23:51:42 +0200 | [diff] [blame] | 2695 | |
| 2696 | task_wakeup(s->task, TASK_WOKEN_INIT); |
| 2697 | /* Return yield waiting for connection. */ |
| 2698 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2699 | 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] | 2700 | |
| 2701 | return 0; |
| 2702 | } |
| 2703 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2704 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2705 | __LJMP static int hlua_socket_connect_ssl(struct lua_State *L) |
| 2706 | { |
| 2707 | struct hlua_socket *socket; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2708 | struct xref *peer; |
| 2709 | struct appctx *appctx; |
| 2710 | struct stream_interface *si; |
| 2711 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2712 | |
| 2713 | MAY_LJMP(check_args(L, 3, "connect_ssl")); |
| 2714 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2715 | |
| 2716 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2717 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2718 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2719 | lua_pushnil(L); |
| 2720 | return 1; |
| 2721 | } |
| 2722 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2723 | si = appctx->owner; |
| 2724 | s = si_strm(si); |
| 2725 | |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 2726 | s->target = &socket_ssl->obj_type; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2727 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2728 | return MAY_LJMP(hlua_socket_connect(L)); |
| 2729 | } |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2730 | #endif |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2731 | |
| 2732 | __LJMP static int hlua_socket_setoption(struct lua_State *L) |
| 2733 | { |
| 2734 | return 0; |
| 2735 | } |
| 2736 | |
| 2737 | __LJMP static int hlua_socket_settimeout(struct lua_State *L) |
| 2738 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2739 | struct hlua_socket *socket; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2740 | int tmout; |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2741 | double dtmout; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2742 | struct xref *peer; |
| 2743 | struct appctx *appctx; |
| 2744 | struct stream_interface *si; |
| 2745 | struct stream *s; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2746 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2747 | MAY_LJMP(check_args(L, 2, "settimeout")); |
| 2748 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2749 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2750 | |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2751 | /* convert the timeout to millis */ |
| 2752 | dtmout = MAY_LJMP(luaL_checknumber(L, 2)) * 1000; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2753 | |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 2754 | /* Check for negative values */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2755 | if (dtmout < 0) |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 2756 | WILL_LJMP(luaL_error(L, "settimeout: cannot set negatives values")); |
| 2757 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2758 | if (dtmout > INT_MAX) /* overflow check */ |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2759 | 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] | 2760 | |
| 2761 | tmout = MS_TO_TICKS((int)dtmout); |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2762 | if (tmout == 0) |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 2763 | tmout++; /* very small timeouts are adjusted to a minimum of 1ms */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2764 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2765 | /* Check if we run on the same thread than the xreator thread. |
| 2766 | * We cannot access to the socket if the thread is different. |
| 2767 | */ |
| 2768 | if (socket->tid != tid) |
| 2769 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2770 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2771 | /* check for connection break. If some data were read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2772 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2773 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2774 | hlua_pusherror(L, "socket: not yet initialised, you can't set timeouts."); |
| 2775 | WILL_LJMP(lua_error(L)); |
| 2776 | return 0; |
| 2777 | } |
| 2778 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2779 | si = appctx->owner; |
| 2780 | s = si_strm(si); |
| 2781 | |
Cyril Bonté | 7bb6345 | 2018-08-17 23:51:02 +0200 | [diff] [blame] | 2782 | s->sess->fe->timeout.connect = tmout; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2783 | s->req.rto = tmout; |
| 2784 | s->req.wto = tmout; |
| 2785 | s->res.rto = tmout; |
| 2786 | s->res.wto = tmout; |
Cyril Bonté | 7bb6345 | 2018-08-17 23:51:02 +0200 | [diff] [blame] | 2787 | s->req.rex = tick_add_ifset(now_ms, tmout); |
| 2788 | s->req.wex = tick_add_ifset(now_ms, tmout); |
| 2789 | s->res.rex = tick_add_ifset(now_ms, tmout); |
| 2790 | s->res.wex = tick_add_ifset(now_ms, tmout); |
| 2791 | |
| 2792 | s->task->expire = tick_add_ifset(now_ms, tmout); |
| 2793 | task_queue(s->task); |
| 2794 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2795 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2796 | |
Thierry Fournier | e9636f1 | 2018-03-08 09:54:32 +0100 | [diff] [blame] | 2797 | lua_pushinteger(L, 1); |
Tim Duesterhus | 119a5f1 | 2018-01-06 19:16:25 +0100 | [diff] [blame] | 2798 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2799 | } |
| 2800 | |
| 2801 | __LJMP static int hlua_socket_new(lua_State *L) |
| 2802 | { |
| 2803 | struct hlua_socket *socket; |
| 2804 | struct appctx *appctx; |
Willy Tarreau | 15b5e14 | 2015-04-04 14:38:25 +0200 | [diff] [blame] | 2805 | struct session *sess; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2806 | struct stream *strm; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2807 | |
| 2808 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2809 | if (!lua_checkstack(L, 3)) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2810 | hlua_pusherror(L, "socket: full stack"); |
| 2811 | goto out_fail_conf; |
| 2812 | } |
| 2813 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2814 | /* Create the object: obj[0] = userdata. */ |
| 2815 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2816 | socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket))); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2817 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2818 | memset(socket, 0, sizeof(*socket)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2819 | socket->tid = tid; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2820 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 2821 | /* Check if the various memory pools are initialized. */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 2822 | if (!pool_head_stream || !pool_head_buffer) { |
Thierry FOURNIER | 4a6170c | 2015-03-09 17:07:10 +0100 | [diff] [blame] | 2823 | hlua_pusherror(L, "socket: uninitialized pools."); |
| 2824 | goto out_fail_conf; |
| 2825 | } |
| 2826 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2827 | /* Pop a class stream metatable and affect it to the userdata. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2828 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref); |
| 2829 | lua_setmetatable(L, -2); |
| 2830 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2831 | /* Create the applet context */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 2832 | appctx = appctx_new(&update_applet, tid_bit); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2833 | if (!appctx) { |
| 2834 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | feb7640 | 2015-04-03 14:10:06 +0200 | [diff] [blame] | 2835 | goto out_fail_conf; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2836 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2837 | |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2838 | appctx->ctx.hlua_cosocket.connected = 0; |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 2839 | appctx->ctx.hlua_cosocket.die = 0; |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2840 | LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_write); |
| 2841 | LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_read); |
Willy Tarreau | b2bf833 | 2015-04-04 15:58:58 +0200 | [diff] [blame] | 2842 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2843 | /* Now create a session, task and stream for this applet */ |
Amaury Denoyelle | 239fdbf | 2021-03-24 10:22:03 +0100 | [diff] [blame] | 2844 | sess = session_new(socket_proxy, NULL, &appctx->obj_type); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2845 | if (!sess) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2846 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2847 | goto out_fail_sess; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2848 | } |
| 2849 | |
Christopher Faulet | 26256f8 | 2020-09-14 11:40:13 +0200 | [diff] [blame] | 2850 | strm = stream_new(sess, &appctx->obj_type, &BUF_NULL); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2851 | if (!strm) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2852 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2853 | goto out_fail_stream; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2854 | } |
| 2855 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2856 | /* Initialise cross reference between stream and Lua socket object. */ |
| 2857 | xref_create(&socket->xref, &appctx->ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2858 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2859 | /* Configure "right" stream interface. this "si" is used to connect |
| 2860 | * and retrieve data from the server. The connection is initialized |
| 2861 | * with the "struct server". |
| 2862 | */ |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2863 | si_set_state(&strm->si[1], SI_ST_ASS); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2864 | |
| 2865 | /* Force destination server. */ |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2866 | strm->flags |= SF_DIRECT | SF_ASSIGNED | SF_BE_ASSIGNED; |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 2867 | strm->target = &socket_tcp->obj_type; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2868 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2869 | return 1; |
| 2870 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2871 | out_fail_stream: |
Willy Tarreau | 11c3624 | 2015-04-04 15:54:03 +0200 | [diff] [blame] | 2872 | session_free(sess); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2873 | out_fail_sess: |
| 2874 | appctx_free(appctx); |
| 2875 | out_fail_conf: |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2876 | WILL_LJMP(lua_error(L)); |
| 2877 | return 0; |
| 2878 | } |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 2879 | |
| 2880 | /* |
| 2881 | * |
| 2882 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2883 | * Class Channel |
| 2884 | * |
| 2885 | * |
| 2886 | */ |
| 2887 | |
| 2888 | /* Returns the struct hlua_channel join to the class channel in the |
| 2889 | * stack entry "ud" or throws an argument error. |
| 2890 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2891 | __LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2892 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 2893 | return MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2894 | } |
| 2895 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2896 | /* Pushes the channel onto the top of the stack. If the stask does not have a |
| 2897 | * free slots, the function fails and returns 0; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2898 | */ |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 2899 | static int hlua_channel_new(lua_State *L, struct channel *channel) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2900 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2901 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2902 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2903 | return 0; |
| 2904 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2905 | lua_newtable(L); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2906 | lua_pushlightuserdata(L, channel); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2907 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2908 | |
| 2909 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 2910 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref); |
| 2911 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2912 | return 1; |
| 2913 | } |
| 2914 | |
| 2915 | /* Duplicate all the data present in the input channel and put it |
| 2916 | * in a string LUA variables. Returns -1 and push a nil value in |
| 2917 | * the stack if the channel is closed and all the data are consumed, |
| 2918 | * returns 0 if no data are available, otherwise it returns the length |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 2919 | * of the built string. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2920 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2921 | static inline int _hlua_channel_dup(struct channel *chn, lua_State *L) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2922 | { |
| 2923 | char *blk1; |
| 2924 | char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2925 | size_t len1; |
| 2926 | size_t len2; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2927 | int ret; |
| 2928 | luaL_Buffer b; |
| 2929 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2930 | ret = ci_getblk_nc(chn, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2931 | if (unlikely(ret == 0)) |
| 2932 | return 0; |
| 2933 | |
| 2934 | if (unlikely(ret < 0)) { |
| 2935 | lua_pushnil(L); |
| 2936 | return -1; |
| 2937 | } |
| 2938 | |
| 2939 | luaL_buffinit(L, &b); |
| 2940 | luaL_addlstring(&b, blk1, len1); |
| 2941 | if (unlikely(ret == 2)) |
| 2942 | luaL_addlstring(&b, blk2, len2); |
| 2943 | luaL_pushresult(&b); |
| 2944 | |
| 2945 | if (unlikely(ret == 2)) |
| 2946 | return len1 + len2; |
| 2947 | return len1; |
| 2948 | } |
| 2949 | |
| 2950 | /* "_hlua_channel_dup" wrapper. If no data are available, it returns |
| 2951 | * a yield. This function keep the data in the buffer. |
| 2952 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2953 | __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] | 2954 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2955 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2956 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2957 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2958 | |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 2959 | if (IS_HTX_STRM(chn_strm(chn))) { |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 2960 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2961 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 2962 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2963 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2964 | if (_hlua_channel_dup(chn, L) == 0) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2965 | 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] | 2966 | return 1; |
| 2967 | } |
| 2968 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2969 | /* Check arguments for the function "hlua_channel_dup_yield". */ |
| 2970 | __LJMP static int hlua_channel_dup(lua_State *L) |
| 2971 | { |
| 2972 | MAY_LJMP(check_args(L, 1, "dup")); |
| 2973 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2974 | return MAY_LJMP(hlua_channel_dup_yield(L, 0, 0)); |
| 2975 | } |
| 2976 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2977 | /* "_hlua_channel_dup" wrapper. If no data are available, it returns |
| 2978 | * a yield. This function consumes the data in the buffer. It returns |
| 2979 | * a string containing the data or a nil pointer if no data are available |
| 2980 | * and the channel is closed. |
| 2981 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2982 | __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] | 2983 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2984 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2985 | int ret; |
| 2986 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2987 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2988 | |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 2989 | if (IS_HTX_STRM(chn_strm(chn))) { |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 2990 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2991 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 2992 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2993 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2994 | ret = _hlua_channel_dup(chn, L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2995 | if (unlikely(ret == 0)) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2996 | 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] | 2997 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2998 | if (unlikely(ret == -1)) |
| 2999 | return 1; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3000 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3001 | b_sub(&chn->buf, ret); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3002 | return 1; |
| 3003 | } |
| 3004 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3005 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3006 | __LJMP static int hlua_channel_get(lua_State *L) |
| 3007 | { |
| 3008 | MAY_LJMP(check_args(L, 1, "get")); |
| 3009 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3010 | return MAY_LJMP(hlua_channel_get_yield(L, 0, 0)); |
| 3011 | } |
| 3012 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3013 | /* This functions consumes and returns one line. If the channel is closed, |
| 3014 | * 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] | 3015 | * 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] | 3016 | * value. |
| 3017 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3018 | __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] | 3019 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3020 | char *blk1; |
| 3021 | char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3022 | size_t len1; |
| 3023 | size_t len2; |
| 3024 | size_t len; |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3025 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3026 | int ret; |
| 3027 | luaL_Buffer b; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3028 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3029 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3030 | |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 3031 | if (IS_HTX_STRM(chn_strm(chn))) { |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3032 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3033 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3034 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3035 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3036 | ret = ci_getline_nc(chn, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3037 | if (ret == 0) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3038 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_getline_yield, TICK_ETERNITY, 0)); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3039 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3040 | if (ret == -1) { |
| 3041 | lua_pushnil(L); |
| 3042 | return 1; |
| 3043 | } |
| 3044 | |
| 3045 | luaL_buffinit(L, &b); |
| 3046 | luaL_addlstring(&b, blk1, len1); |
| 3047 | len = len1; |
| 3048 | if (unlikely(ret == 2)) { |
| 3049 | luaL_addlstring(&b, blk2, len2); |
| 3050 | len += len2; |
| 3051 | } |
| 3052 | luaL_pushresult(&b); |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3053 | 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] | 3054 | return 1; |
| 3055 | } |
| 3056 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3057 | /* Check arguments for the function "hlua_channel_getline_yield". */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3058 | __LJMP static int hlua_channel_getline(lua_State *L) |
| 3059 | { |
| 3060 | MAY_LJMP(check_args(L, 1, "getline")); |
| 3061 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3062 | return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0)); |
| 3063 | } |
| 3064 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3065 | /* This function takes a string as input, and append it at the |
| 3066 | * input side of channel. If the data is too big, but a space |
| 3067 | * is probably available after sending some data, the function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3068 | * yields. If the data is bigger than the buffer, or if the |
| 3069 | * channel is closed, it returns -1. Otherwise, it returns the |
| 3070 | * amount of data written. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3071 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3072 | __LJMP static int hlua_channel_append_yield(lua_State *L, int status, lua_KContext ctx) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3073 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3074 | struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3075 | size_t len; |
| 3076 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3077 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3078 | int ret; |
| 3079 | int max; |
| 3080 | |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 3081 | if (IS_HTX_STRM(chn_strm(chn))) { |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3082 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3083 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3084 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3085 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3086 | /* Check if the buffer is available because HAProxy doesn't allocate |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 3087 | * the request buffer if its not required. |
| 3088 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3089 | if (chn->buf.size == 0) { |
Willy Tarreau | 4b962a4 | 2018-11-15 11:03:21 +0100 | [diff] [blame] | 3090 | si_rx_buff_blk(chn_prod(chn)); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3091 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0)); |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 3092 | } |
| 3093 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3094 | max = channel_recv_limit(chn) - b_data(&chn->buf); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3095 | if (max > len - l) |
| 3096 | max = len - l; |
| 3097 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3098 | ret = ci_putblk(chn, str + l, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3099 | if (ret == -2 || ret == -3) { |
| 3100 | lua_pushinteger(L, -1); |
| 3101 | return 1; |
| 3102 | } |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 3103 | if (ret == -1) { |
| 3104 | chn->flags |= CF_WAKE_WRITE; |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3105 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0)); |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 3106 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3107 | l += ret; |
| 3108 | lua_pop(L, 1); |
| 3109 | lua_pushinteger(L, l); |
| 3110 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3111 | max = channel_recv_limit(chn) - b_data(&chn->buf); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3112 | if (max == 0 && co_data(chn) == 0) { |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3113 | /* There are no space available, and the output buffer is empty. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3114 | * in this case, we cannot add more data, so we cannot yield, |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3115 | * we return the amount of copied data. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3116 | */ |
| 3117 | return 1; |
| 3118 | } |
| 3119 | if (l < len) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3120 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3121 | return 1; |
| 3122 | } |
| 3123 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3124 | /* Just a wrapper of "hlua_channel_append_yield". It returns the length |
| 3125 | * of the written string, or -1 if the channel is closed or if the |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3126 | * buffer size is too little for the data. |
| 3127 | */ |
| 3128 | __LJMP static int hlua_channel_append(lua_State *L) |
| 3129 | { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3130 | size_t len; |
| 3131 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3132 | MAY_LJMP(check_args(L, 2, "append")); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3133 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3134 | MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3135 | MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3136 | lua_pushinteger(L, 0); |
| 3137 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3138 | return MAY_LJMP(hlua_channel_append_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3139 | } |
| 3140 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3141 | /* Just a wrapper of "hlua_channel_append_yield". This wrapper starts |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3142 | * his process by cleaning the buffer. The result is a replacement |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3143 | * of the current data. It returns the length of the written string, |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3144 | * or -1 if the channel is closed or if the buffer size is too |
| 3145 | * little for the data. |
| 3146 | */ |
| 3147 | __LJMP static int hlua_channel_set(lua_State *L) |
| 3148 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3149 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3150 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3151 | MAY_LJMP(check_args(L, 2, "set")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3152 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3153 | lua_pushinteger(L, 0); |
| 3154 | |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 3155 | if (IS_HTX_STRM(chn_strm(chn))) { |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3156 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3157 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3158 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3159 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3160 | b_set_data(&chn->buf, co_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3161 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3162 | return MAY_LJMP(hlua_channel_append_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3163 | } |
| 3164 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3165 | /* Append data in the output side of the buffer. This data is immediately |
| 3166 | * sent. The function returns the amount of data written. If the buffer |
| 3167 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3168 | * if the channel is closed. |
| 3169 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3170 | __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] | 3171 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3172 | struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3173 | size_t len; |
| 3174 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3175 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3176 | int max; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 3177 | struct hlua *hlua; |
| 3178 | |
| 3179 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 3180 | hlua = hlua_gethlua(L); |
| 3181 | if (!hlua) { |
| 3182 | lua_pushnil(L); |
| 3183 | return 1; |
| 3184 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3185 | |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 3186 | if (IS_HTX_STRM(chn_strm(chn))) { |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3187 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3188 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3189 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3190 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3191 | if (unlikely(channel_output_closed(chn))) { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3192 | lua_pushinteger(L, -1); |
| 3193 | return 1; |
| 3194 | } |
| 3195 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3196 | /* Check if the buffer is available because HAProxy doesn't allocate |
Thierry FOURNIER | 3e3a608 | 2015-03-05 17:06:12 +0100 | [diff] [blame] | 3197 | * the request buffer if its not required. |
| 3198 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3199 | if (chn->buf.size == 0) { |
Willy Tarreau | 4b962a4 | 2018-11-15 11:03:21 +0100 | [diff] [blame] | 3200 | si_rx_buff_blk(chn_prod(chn)); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3201 | 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] | 3202 | } |
| 3203 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3204 | /* The written data will be immediately sent, so we can check |
| 3205 | * the available space without taking in account the reserve. |
| 3206 | * The reserve is guaranteed for the processing of incoming |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3207 | * data, because the buffer will be flushed. |
| 3208 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3209 | max = b_room(&chn->buf); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3210 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3211 | /* If there is no space available, and the output buffer is empty. |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3212 | * in this case, we cannot add more data, so we cannot yield, |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3213 | * we return the amount of copied data. |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3214 | */ |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3215 | if (max == 0 && co_data(chn) == 0) |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3216 | return 1; |
| 3217 | |
| 3218 | /* Adjust the real required length. */ |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3219 | if (max > len - l) |
| 3220 | max = len - l; |
| 3221 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3222 | /* The buffer available size may be not contiguous. This test |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3223 | * detects a non contiguous buffer and realign it. |
| 3224 | */ |
Willy Tarreau | 3f67999 | 2018-06-15 15:06:42 +0200 | [diff] [blame] | 3225 | if (ci_space_for_replace(chn) < max) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3226 | channel_slow_realign(chn, trash.area); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3227 | |
| 3228 | /* Copy input data in the buffer. */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3229 | 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] | 3230 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3231 | /* buffer replace considers that the input part is filled. |
| 3232 | * so, I must forward these new data in the output part. |
| 3233 | */ |
Willy Tarreau | bcbd393 | 2018-06-06 07:13:22 +0200 | [diff] [blame] | 3234 | c_adv(chn, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3235 | |
| 3236 | l += max; |
| 3237 | lua_pop(L, 1); |
| 3238 | lua_pushinteger(L, l); |
| 3239 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3240 | /* If there is no space available, and the output buffer is empty. |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3241 | * in this case, we cannot add more data, so we cannot yield, |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3242 | * we return the amount of copied data. |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3243 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3244 | max = b_room(&chn->buf); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3245 | if (max == 0 && co_data(chn) == 0) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3246 | return 1; |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3247 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3248 | if (l < len) { |
| 3249 | /* If we are waiting for space in the response buffer, we |
| 3250 | * must set the flag WAKERESWR. This flag required the task |
| 3251 | * wake up if any activity is detected on the response buffer. |
| 3252 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3253 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3254 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3255 | else |
| 3256 | HLUA_SET_WAKEREQWR(hlua); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3257 | 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] | 3258 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3259 | |
| 3260 | return 1; |
| 3261 | } |
| 3262 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3263 | /* Just a wrapper of "_hlua_channel_send". This wrapper permits |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3264 | * yield the LUA process, and resume it without checking the |
| 3265 | * input arguments. |
| 3266 | */ |
| 3267 | __LJMP static int hlua_channel_send(lua_State *L) |
| 3268 | { |
| 3269 | MAY_LJMP(check_args(L, 2, "send")); |
| 3270 | lua_pushinteger(L, 0); |
| 3271 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3272 | return MAY_LJMP(hlua_channel_send_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3273 | } |
| 3274 | |
| 3275 | /* This function forward and amount of butes. The data pass from |
| 3276 | * the input side of the buffer to the output side, and can be |
| 3277 | * forwarded. This function never fails. |
| 3278 | * |
| 3279 | * The Lua function takes an amount of bytes to be forwarded in |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3280 | * input. It returns the number of bytes forwarded. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3281 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3282 | __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] | 3283 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3284 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3285 | int len; |
| 3286 | int l; |
| 3287 | int max; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 3288 | struct hlua *hlua; |
| 3289 | |
| 3290 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 3291 | hlua = hlua_gethlua(L); |
| 3292 | if (!hlua) |
| 3293 | return 1; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3294 | |
| 3295 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3296 | |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 3297 | if (IS_HTX_STRM(chn_strm(chn))) { |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3298 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3299 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3300 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3301 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3302 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3303 | l = MAY_LJMP(luaL_checkinteger(L, -1)); |
| 3304 | |
| 3305 | max = len - l; |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3306 | if (max > ci_data(chn)) |
| 3307 | max = ci_data(chn); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3308 | channel_forward(chn, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3309 | l += max; |
| 3310 | |
| 3311 | lua_pop(L, 1); |
| 3312 | lua_pushinteger(L, l); |
| 3313 | |
| 3314 | /* Check if it miss bytes to forward. */ |
| 3315 | if (l < len) { |
| 3316 | /* The the input channel or the output channel are closed, we |
| 3317 | * must return the amount of data forwarded. |
| 3318 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3319 | if (channel_input_closed(chn) || channel_output_closed(chn)) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3320 | return 1; |
| 3321 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3322 | /* If we are waiting for space data in the response buffer, we |
| 3323 | * must set the flag WAKERESWR. This flag required the task |
| 3324 | * wake up if any activity is detected on the response buffer. |
| 3325 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3326 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3327 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3328 | else |
| 3329 | HLUA_SET_WAKEREQWR(hlua); |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3330 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3331 | /* Otherwise, we can yield waiting for new data in the inpout side. */ |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3332 | 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] | 3333 | } |
| 3334 | |
| 3335 | return 1; |
| 3336 | } |
| 3337 | |
| 3338 | /* Just check the input and prepare the stack for the previous |
| 3339 | * function "hlua_channel_forward_yield" |
| 3340 | */ |
| 3341 | __LJMP static int hlua_channel_forward(lua_State *L) |
| 3342 | { |
| 3343 | MAY_LJMP(check_args(L, 2, "forward")); |
| 3344 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3345 | MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3346 | |
| 3347 | lua_pushinteger(L, 0); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3348 | return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3349 | } |
| 3350 | |
| 3351 | /* Just returns the number of bytes available in the input |
| 3352 | * side of the buffer. This function never fails. |
| 3353 | */ |
| 3354 | __LJMP static int hlua_channel_get_in_len(lua_State *L) |
| 3355 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3356 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3357 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3358 | MAY_LJMP(check_args(L, 1, "get_in_len")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3359 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a3ceac1 | 2018-12-14 13:39:09 +0100 | [diff] [blame] | 3360 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3361 | struct htx *htx = htxbuf(&chn->buf); |
| 3362 | lua_pushinteger(L, htx->data - co_data(chn)); |
| 3363 | } |
| 3364 | else |
| 3365 | lua_pushinteger(L, ci_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3366 | return 1; |
| 3367 | } |
| 3368 | |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3369 | /* Returns true if the channel is full. */ |
| 3370 | __LJMP static int hlua_channel_is_full(lua_State *L) |
| 3371 | { |
| 3372 | struct channel *chn; |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3373 | |
| 3374 | MAY_LJMP(check_args(L, 1, "is_full")); |
| 3375 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 0ec740e | 2020-02-26 11:59:19 +0100 | [diff] [blame] | 3376 | /* ignore the reserve, we are not on a producer side (ie in an |
| 3377 | * applet). |
| 3378 | */ |
| 3379 | lua_pushboolean(L, channel_full(chn, 0)); |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3380 | return 1; |
| 3381 | } |
| 3382 | |
Christopher Faulet | 2ac9ba2 | 2020-02-25 10:15:50 +0100 | [diff] [blame] | 3383 | /* Returns true if the channel is the response channel. */ |
| 3384 | __LJMP static int hlua_channel_is_resp(lua_State *L) |
| 3385 | { |
| 3386 | struct channel *chn; |
| 3387 | |
| 3388 | MAY_LJMP(check_args(L, 1, "is_resp")); |
| 3389 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3390 | |
| 3391 | lua_pushboolean(L, !!(chn->flags & CF_ISRESP)); |
| 3392 | return 1; |
| 3393 | } |
| 3394 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3395 | /* Just returns the number of bytes available in the output |
| 3396 | * side of the buffer. This function never fails. |
| 3397 | */ |
| 3398 | __LJMP static int hlua_channel_get_out_len(lua_State *L) |
| 3399 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3400 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3401 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3402 | MAY_LJMP(check_args(L, 1, "get_out_len")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3403 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3404 | lua_pushinteger(L, co_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3405 | return 1; |
| 3406 | } |
| 3407 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3408 | /* |
| 3409 | * |
| 3410 | * |
| 3411 | * Class Fetches |
| 3412 | * |
| 3413 | * |
| 3414 | */ |
| 3415 | |
| 3416 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3417 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3418 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3419 | __LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud) |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3420 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3421 | return MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3422 | } |
| 3423 | |
| 3424 | /* This function creates and push in the stack a fetch object according |
| 3425 | * with a current TXN. |
| 3426 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3427 | 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] | 3428 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3429 | struct hlua_smp *hsmp; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3430 | |
| 3431 | /* Check stack size. */ |
| 3432 | if (!lua_checkstack(L, 3)) |
| 3433 | return 0; |
| 3434 | |
| 3435 | /* Create the object: obj[0] = userdata. |
| 3436 | * Note that the base of the Fetches object is the |
| 3437 | * transaction object. |
| 3438 | */ |
| 3439 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3440 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3441 | lua_rawseti(L, -2, 0); |
| 3442 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3443 | hsmp->s = txn->s; |
| 3444 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 3445 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3446 | hsmp->flags = flags; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3447 | |
| 3448 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 3449 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref); |
| 3450 | lua_setmetatable(L, -2); |
| 3451 | |
| 3452 | return 1; |
| 3453 | } |
| 3454 | |
| 3455 | /* This function is an LUA binding. It is called with each sample-fetch. |
| 3456 | * It uses closure argument to store the associated sample-fetch. It |
| 3457 | * returns only one argument or throws an error. An error is thrown |
| 3458 | * only if an error is encountered during the argument parsing. If |
| 3459 | * the "sample-fetch" function fails, nil is returned. |
| 3460 | */ |
| 3461 | __LJMP static int hlua_run_sample_fetch(lua_State *L) |
| 3462 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3463 | struct hlua_smp *hsmp; |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 3464 | struct sample_fetch *f; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 3465 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3466 | int i; |
| 3467 | struct sample smp; |
| 3468 | |
| 3469 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3470 | f = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3471 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3472 | /* Get traditional arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3473 | hsmp = MAY_LJMP(hlua_checkfetches(L, 1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3474 | |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 3475 | /* Check execution authorization. */ |
| 3476 | if (f->use & SMP_USE_HTTP_ANY && |
| 3477 | !(hsmp->flags & HLUA_F_MAY_USE_HTTP)) { |
| 3478 | lua_pushfstring(L, "the sample-fetch '%s' needs an HTTP parser which " |
| 3479 | "is not available in Lua services", f->kw); |
| 3480 | WILL_LJMP(lua_error(L)); |
| 3481 | } |
| 3482 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3483 | /* Get extra arguments. */ |
| 3484 | for (i = 0; i < lua_gettop(L) - 1; i++) { |
| 3485 | if (i >= ARGM_NBARGS) |
| 3486 | break; |
| 3487 | hlua_lua2arg(L, i + 2, &args[i]); |
| 3488 | } |
| 3489 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3490 | args[i].data.str.area = NULL; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3491 | |
| 3492 | /* Check arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3493 | 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] | 3494 | |
| 3495 | /* Run the special args checker. */ |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 3496 | if (f->val_args && !f->val_args(args, NULL)) { |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3497 | lua_pushfstring(L, "error in arguments"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3498 | goto error; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3499 | } |
| 3500 | |
| 3501 | /* Initialise the sample. */ |
| 3502 | memset(&smp, 0, sizeof(smp)); |
| 3503 | |
| 3504 | /* Run the sample fetch process. */ |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 3505 | 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] | 3506 | if (!f->process(args, &smp, f->kw, f->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3507 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3508 | lua_pushstring(L, ""); |
| 3509 | else |
| 3510 | lua_pushnil(L); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3511 | goto end; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3512 | } |
| 3513 | |
| 3514 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3515 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3516 | hlua_smp2lua_str(L, &smp); |
| 3517 | else |
| 3518 | hlua_smp2lua(L, &smp); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3519 | |
| 3520 | end: |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 3521 | free_args(args); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3522 | return 1; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3523 | |
| 3524 | error: |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 3525 | free_args(args); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3526 | WILL_LJMP(lua_error(L)); |
| 3527 | return 0; /* Never reached */ |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3528 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3529 | |
| 3530 | /* |
| 3531 | * |
| 3532 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3533 | * Class Converters |
| 3534 | * |
| 3535 | * |
| 3536 | */ |
| 3537 | |
| 3538 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3539 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3540 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3541 | __LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3542 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3543 | return MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3544 | } |
| 3545 | |
| 3546 | /* This function creates and push in the stack a Converters object |
| 3547 | * according with a current TXN. |
| 3548 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3549 | 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] | 3550 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3551 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3552 | |
| 3553 | /* Check stack size. */ |
| 3554 | if (!lua_checkstack(L, 3)) |
| 3555 | return 0; |
| 3556 | |
| 3557 | /* Create the object: obj[0] = userdata. |
| 3558 | * Note that the base of the Converters object is the |
| 3559 | * same than the TXN object. |
| 3560 | */ |
| 3561 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3562 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3563 | lua_rawseti(L, -2, 0); |
| 3564 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3565 | hsmp->s = txn->s; |
| 3566 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 3567 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3568 | hsmp->flags = flags; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3569 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3570 | /* Pop a class stream metatable and affect it to the table. */ |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3571 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref); |
| 3572 | lua_setmetatable(L, -2); |
| 3573 | |
| 3574 | return 1; |
| 3575 | } |
| 3576 | |
| 3577 | /* This function is an LUA binding. It is called with each converter. |
| 3578 | * It uses closure argument to store the associated converter. It |
| 3579 | * returns only one argument or throws an error. An error is thrown |
| 3580 | * only if an error is encountered during the argument parsing. If |
| 3581 | * the converter function function fails, nil is returned. |
| 3582 | */ |
| 3583 | __LJMP static int hlua_run_sample_conv(lua_State *L) |
| 3584 | { |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3585 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3586 | struct sample_conv *conv; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 3587 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3588 | int i; |
| 3589 | struct sample smp; |
| 3590 | |
| 3591 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3592 | conv = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3593 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3594 | /* Get traditional arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3595 | hsmp = MAY_LJMP(hlua_checkconverters(L, 1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3596 | |
| 3597 | /* Get extra arguments. */ |
| 3598 | for (i = 0; i < lua_gettop(L) - 2; i++) { |
| 3599 | if (i >= ARGM_NBARGS) |
| 3600 | break; |
| 3601 | hlua_lua2arg(L, i + 3, &args[i]); |
| 3602 | } |
| 3603 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3604 | args[i].data.str.area = NULL; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3605 | |
| 3606 | /* Check arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3607 | 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] | 3608 | |
| 3609 | /* Run the special args checker. */ |
| 3610 | if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) { |
| 3611 | hlua_pusherror(L, "error in arguments"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3612 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3613 | } |
| 3614 | |
| 3615 | /* Initialise the sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 3616 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3617 | if (!hlua_lua2smp(L, 2, &smp)) { |
| 3618 | hlua_pusherror(L, "error in the input argument"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3619 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3620 | } |
| 3621 | |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 3622 | smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR); |
| 3623 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3624 | /* Apply expected cast. */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3625 | if (!sample_casts[smp.data.type][conv->in_type]) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3626 | hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'", |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3627 | smp_to_type[smp.data.type], smp_to_type[conv->in_type]); |
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 | } |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3630 | if (sample_casts[smp.data.type][conv->in_type] != c_none && |
| 3631 | !sample_casts[smp.data.type][conv->in_type](&smp)) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3632 | hlua_pusherror(L, "error during the input argument casting"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3633 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3634 | } |
| 3635 | |
| 3636 | /* Run the sample conversion process. */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 3637 | if (!conv->process(args, &smp, conv->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3638 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3639 | lua_pushstring(L, ""); |
| 3640 | else |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 3641 | lua_pushnil(L); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3642 | goto end; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3643 | } |
| 3644 | |
| 3645 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3646 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3647 | hlua_smp2lua_str(L, &smp); |
| 3648 | else |
| 3649 | hlua_smp2lua(L, &smp); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3650 | end: |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 3651 | free_args(args); |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 3652 | return 1; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3653 | |
| 3654 | error: |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 3655 | free_args(args); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3656 | WILL_LJMP(lua_error(L)); |
| 3657 | return 0; /* Never reached */ |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3658 | } |
| 3659 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3660 | /* |
| 3661 | * |
| 3662 | * |
| 3663 | * Class AppletTCP |
| 3664 | * |
| 3665 | * |
| 3666 | */ |
| 3667 | |
| 3668 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 3669 | * a class stream, otherwise it throws an error. |
| 3670 | */ |
| 3671 | __LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud) |
| 3672 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3673 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3674 | } |
| 3675 | |
| 3676 | /* This function creates and push in the stack an Applet object |
| 3677 | * according with a current TXN. |
| 3678 | */ |
| 3679 | static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx) |
| 3680 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3681 | struct hlua_appctx *luactx; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3682 | struct stream_interface *si = ctx->owner; |
| 3683 | struct stream *s = si_strm(si); |
| 3684 | struct proxy *p = s->be; |
| 3685 | |
| 3686 | /* Check stack size. */ |
| 3687 | if (!lua_checkstack(L, 3)) |
| 3688 | return 0; |
| 3689 | |
| 3690 | /* Create the object: obj[0] = userdata. |
| 3691 | * Note that the base of the Converters object is the |
| 3692 | * same than the TXN object. |
| 3693 | */ |
| 3694 | lua_newtable(L); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3695 | luactx = lua_newuserdata(L, sizeof(*luactx)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3696 | lua_rawseti(L, -2, 0); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3697 | luactx->appctx = ctx; |
| 3698 | luactx->htxn.s = s; |
| 3699 | luactx->htxn.p = p; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3700 | |
| 3701 | /* Create the "f" field that contains a list of fetches. */ |
| 3702 | lua_pushstring(L, "f"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3703 | if (!hlua_fetches_new(L, &luactx->htxn, 0)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3704 | return 0; |
| 3705 | lua_settable(L, -3); |
| 3706 | |
| 3707 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 3708 | lua_pushstring(L, "sf"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3709 | if (!hlua_fetches_new(L, &luactx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3710 | return 0; |
| 3711 | lua_settable(L, -3); |
| 3712 | |
| 3713 | /* Create the "c" field that contains a list of converters. */ |
| 3714 | lua_pushstring(L, "c"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3715 | if (!hlua_converters_new(L, &luactx->htxn, 0)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3716 | return 0; |
| 3717 | lua_settable(L, -3); |
| 3718 | |
| 3719 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 3720 | lua_pushstring(L, "sc"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3721 | if (!hlua_converters_new(L, &luactx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3722 | return 0; |
| 3723 | lua_settable(L, -3); |
| 3724 | |
| 3725 | /* Pop a class stream metatable and affect it to the table. */ |
| 3726 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref); |
| 3727 | lua_setmetatable(L, -2); |
| 3728 | |
| 3729 | return 1; |
| 3730 | } |
| 3731 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3732 | __LJMP static int hlua_applet_tcp_set_var(lua_State *L) |
| 3733 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3734 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3735 | struct stream *s; |
| 3736 | const char *name; |
| 3737 | size_t len; |
| 3738 | struct sample smp; |
| 3739 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 3740 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 3741 | 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] | 3742 | |
| 3743 | /* It is useles to retrieve the stream, but this function |
| 3744 | * runs only in a stream context. |
| 3745 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3746 | luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3747 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3748 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3749 | |
| 3750 | /* Converts the third argument in a sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 3751 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3752 | hlua_lua2smp(L, 3, &smp); |
| 3753 | |
| 3754 | /* Store the sample in a variable. */ |
| 3755 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 3756 | |
| 3757 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 3758 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 3759 | else |
| 3760 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 3761 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 3762 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3763 | } |
| 3764 | |
| 3765 | __LJMP static int hlua_applet_tcp_unset_var(lua_State *L) |
| 3766 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3767 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3768 | struct stream *s; |
| 3769 | const char *name; |
| 3770 | size_t len; |
| 3771 | struct sample smp; |
| 3772 | |
| 3773 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 3774 | |
| 3775 | /* It is useles to retrieve the stream, but this function |
| 3776 | * runs only in a stream context. |
| 3777 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3778 | luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3779 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3780 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3781 | |
| 3782 | /* Unset the variable. */ |
| 3783 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 3784 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 3785 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3786 | } |
| 3787 | |
| 3788 | __LJMP static int hlua_applet_tcp_get_var(lua_State *L) |
| 3789 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3790 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3791 | struct stream *s; |
| 3792 | const char *name; |
| 3793 | size_t len; |
| 3794 | struct sample smp; |
| 3795 | |
| 3796 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 3797 | |
| 3798 | /* It is useles to retrieve the stream, but this function |
| 3799 | * runs only in a stream context. |
| 3800 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3801 | luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3802 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3803 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3804 | |
| 3805 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 3806 | if (!vars_get_by_name(name, len, &smp)) { |
| 3807 | lua_pushnil(L); |
| 3808 | return 1; |
| 3809 | } |
| 3810 | |
| 3811 | return hlua_smp2lua(L, &smp); |
| 3812 | } |
| 3813 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3814 | __LJMP static int hlua_applet_tcp_set_priv(lua_State *L) |
| 3815 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3816 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3817 | struct stream *s = luactx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 3818 | struct hlua *hlua; |
| 3819 | |
| 3820 | /* 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] | 3821 | if (!s->hlua) |
| 3822 | return 0; |
| 3823 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3824 | |
| 3825 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 3826 | |
| 3827 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 3828 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3829 | |
| 3830 | /* Get and store new value. */ |
| 3831 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 3832 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 3833 | |
| 3834 | return 0; |
| 3835 | } |
| 3836 | |
| 3837 | __LJMP static int hlua_applet_tcp_get_priv(lua_State *L) |
| 3838 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3839 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3840 | struct stream *s = luactx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 3841 | struct hlua *hlua; |
| 3842 | |
| 3843 | /* 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] | 3844 | if (!s->hlua) { |
| 3845 | lua_pushnil(L); |
| 3846 | return 1; |
| 3847 | } |
| 3848 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3849 | |
| 3850 | /* Push configuration index in the stack. */ |
| 3851 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 3852 | |
| 3853 | return 1; |
| 3854 | } |
| 3855 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3856 | /* If expected data not yet available, it returns a yield. This function |
| 3857 | * consumes the data in the buffer. It returns a string containing the |
| 3858 | * data. This string can be empty. |
| 3859 | */ |
| 3860 | __LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx) |
| 3861 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3862 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3863 | struct stream_interface *si = luactx->appctx->owner; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3864 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3865 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3866 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3867 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3868 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3869 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3870 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3871 | ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3872 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3873 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3874 | if (ret == 0) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3875 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3876 | 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] | 3877 | } |
| 3878 | |
| 3879 | /* End of data: commit the total strings and return. */ |
| 3880 | if (ret < 0) { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3881 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3882 | return 1; |
| 3883 | } |
| 3884 | |
| 3885 | /* Ensure that the block 2 length is usable. */ |
| 3886 | if (ret == 1) |
| 3887 | len2 = 0; |
| 3888 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 3889 | /* don't check the max length read and don't check. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3890 | luaL_addlstring(&luactx->b, blk1, len1); |
| 3891 | luaL_addlstring(&luactx->b, blk2, len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3892 | |
| 3893 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3894 | co_skip(si_oc(si), len1 + len2); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3895 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3896 | return 1; |
| 3897 | } |
| 3898 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3899 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3900 | __LJMP static int hlua_applet_tcp_getline(lua_State *L) |
| 3901 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3902 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3903 | |
| 3904 | /* Initialise the string catenation. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3905 | luaL_buffinit(L, &luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3906 | |
| 3907 | return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0)); |
| 3908 | } |
| 3909 | |
| 3910 | /* If expected data not yet available, it returns a yield. This function |
| 3911 | * consumes the data in the buffer. It returns a string containing the |
| 3912 | * data. This string can be empty. |
| 3913 | */ |
| 3914 | __LJMP static int hlua_applet_tcp_recv_yield(lua_State *L, int status, lua_KContext ctx) |
| 3915 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3916 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3917 | struct stream_interface *si = luactx->appctx->owner; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3918 | size_t len = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3919 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3920 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3921 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3922 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3923 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3924 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3925 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3926 | ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3927 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3928 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3929 | if (ret == 0) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3930 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3931 | 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] | 3932 | } |
| 3933 | |
| 3934 | /* End of data: commit the total strings and return. */ |
| 3935 | if (ret < 0) { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3936 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3937 | return 1; |
| 3938 | } |
| 3939 | |
| 3940 | /* Ensure that the block 2 length is usable. */ |
| 3941 | if (ret == 1) |
| 3942 | len2 = 0; |
| 3943 | |
| 3944 | if (len == -1) { |
| 3945 | |
| 3946 | /* If len == -1, catenate all the data avalaile and |
| 3947 | * yield because we want to get all the data until |
| 3948 | * the end of data stream. |
| 3949 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3950 | luaL_addlstring(&luactx->b, blk1, len1); |
| 3951 | luaL_addlstring(&luactx->b, blk2, len2); |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3952 | co_skip(si_oc(si), len1 + len2); |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3953 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3954 | 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] | 3955 | |
| 3956 | } else { |
| 3957 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3958 | /* Copy the first block caping to the length required. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3959 | if (len1 > len) |
| 3960 | len1 = len; |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3961 | luaL_addlstring(&luactx->b, blk1, len1); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3962 | len -= len1; |
| 3963 | |
| 3964 | /* Copy the second block. */ |
| 3965 | if (len2 > len) |
| 3966 | len2 = len; |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3967 | luaL_addlstring(&luactx->b, blk2, len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3968 | len -= len2; |
| 3969 | |
| 3970 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3971 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3972 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3973 | /* If there is no other data available, yield waiting for new data. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3974 | if (len > 0) { |
| 3975 | lua_pushinteger(L, len); |
| 3976 | lua_replace(L, 2); |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3977 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3978 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3979 | } |
| 3980 | |
| 3981 | /* return the result. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3982 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3983 | return 1; |
| 3984 | } |
| 3985 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3986 | /* we never execute this */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3987 | hlua_pusherror(L, "Lua: internal error"); |
| 3988 | WILL_LJMP(lua_error(L)); |
| 3989 | return 0; |
| 3990 | } |
| 3991 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3992 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3993 | __LJMP static int hlua_applet_tcp_recv(lua_State *L) |
| 3994 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3995 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3996 | int len = -1; |
| 3997 | |
| 3998 | if (lua_gettop(L) > 2) |
| 3999 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 4000 | if (lua_gettop(L) >= 2) { |
| 4001 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4002 | lua_pop(L, 1); |
| 4003 | } |
| 4004 | |
| 4005 | /* Confirm or set the required length */ |
| 4006 | lua_pushinteger(L, len); |
| 4007 | |
| 4008 | /* Initialise the string catenation. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4009 | luaL_buffinit(L, &luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4010 | |
| 4011 | return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0)); |
| 4012 | } |
| 4013 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4014 | /* Append data in the output side of the buffer. This data is immediately |
| 4015 | * sent. The function returns the amount of data written. If the buffer |
| 4016 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4017 | * if the channel is closed. |
| 4018 | */ |
| 4019 | __LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx) |
| 4020 | { |
| 4021 | size_t len; |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4022 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4023 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4024 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4025 | struct stream_interface *si = luactx->appctx->owner; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4026 | struct channel *chn = si_ic(si); |
| 4027 | int max; |
| 4028 | |
| 4029 | /* Get the max amount of data which can write as input in the channel. */ |
| 4030 | max = channel_recv_max(chn); |
| 4031 | if (max > (len - l)) |
| 4032 | max = len - l; |
| 4033 | |
| 4034 | /* Copy data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4035 | ci_putblk(chn, str + l, max); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4036 | |
| 4037 | /* update counters. */ |
| 4038 | l += max; |
| 4039 | lua_pop(L, 1); |
| 4040 | lua_pushinteger(L, l); |
| 4041 | |
| 4042 | /* If some data is not send, declares the situation to the |
| 4043 | * applet, and returns a yield. |
| 4044 | */ |
| 4045 | if (l < len) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 4046 | si_rx_room_blk(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4047 | 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] | 4048 | } |
| 4049 | |
| 4050 | return 1; |
| 4051 | } |
| 4052 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4053 | /* Just a wrapper of "hlua_applet_tcp_send_yield". This wrapper permits |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4054 | * yield the LUA process, and resume it without checking the |
| 4055 | * input arguments. |
| 4056 | */ |
| 4057 | __LJMP static int hlua_applet_tcp_send(lua_State *L) |
| 4058 | { |
| 4059 | MAY_LJMP(check_args(L, 2, "send")); |
| 4060 | lua_pushinteger(L, 0); |
| 4061 | |
| 4062 | return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0)); |
| 4063 | } |
| 4064 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4065 | /* |
| 4066 | * |
| 4067 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4068 | * Class AppletHTTP |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4069 | * |
| 4070 | * |
| 4071 | */ |
| 4072 | |
| 4073 | /* Returns a struct hlua_txn if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4074 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4075 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4076 | __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] | 4077 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4078 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_http_ref)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4079 | } |
| 4080 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4081 | /* This function creates and push in the stack an Applet object |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4082 | * according with a current TXN. |
| 4083 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4084 | static int hlua_applet_http_new(lua_State *L, struct appctx *ctx) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4085 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4086 | struct hlua_appctx *luactx; |
Thierry FOURNIER | 841475e | 2015-12-11 17:10:09 +0100 | [diff] [blame] | 4087 | struct hlua_txn htxn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4088 | struct stream_interface *si = ctx->owner; |
| 4089 | struct stream *s = si_strm(si); |
| 4090 | struct proxy *px = s->be; |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4091 | struct htx *htx; |
| 4092 | struct htx_blk *blk; |
| 4093 | struct htx_sl *sl; |
| 4094 | struct ist path; |
| 4095 | unsigned long long len = 0; |
| 4096 | int32_t pos; |
Amaury Denoyelle | c453f95 | 2021-07-06 11:40:12 +0200 | [diff] [blame] | 4097 | struct http_uri_parser parser; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4098 | |
| 4099 | /* Check stack size. */ |
| 4100 | if (!lua_checkstack(L, 3)) |
| 4101 | return 0; |
| 4102 | |
| 4103 | /* Create the object: obj[0] = userdata. |
| 4104 | * Note that the base of the Converters object is the |
| 4105 | * same than the TXN object. |
| 4106 | */ |
| 4107 | lua_newtable(L); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4108 | luactx = lua_newuserdata(L, sizeof(*luactx)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4109 | lua_rawseti(L, -2, 0); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4110 | luactx->appctx = ctx; |
| 4111 | luactx->appctx->ctx.hlua_apphttp.status = 200; /* Default status code returned. */ |
| 4112 | luactx->appctx->ctx.hlua_apphttp.reason = NULL; /* Use default reason based on status */ |
| 4113 | luactx->htxn.s = s; |
| 4114 | luactx->htxn.p = px; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4115 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4116 | /* Create the "f" field that contains a list of fetches. */ |
| 4117 | lua_pushstring(L, "f"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4118 | if (!hlua_fetches_new(L, &luactx->htxn, 0)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4119 | return 0; |
| 4120 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4121 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4122 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 4123 | lua_pushstring(L, "sf"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4124 | if (!hlua_fetches_new(L, &luactx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4125 | return 0; |
| 4126 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4127 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4128 | /* Create the "c" field that contains a list of converters. */ |
| 4129 | lua_pushstring(L, "c"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4130 | if (!hlua_converters_new(L, &luactx->htxn, 0)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4131 | return 0; |
| 4132 | lua_settable(L, -3); |
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 "sc" field that contains a list of stringsafe converters. */ |
| 4135 | lua_pushstring(L, "sc"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4136 | if (!hlua_converters_new(L, &luactx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4137 | return 0; |
| 4138 | lua_settable(L, -3); |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4139 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4140 | htx = htxbuf(&s->req.buf); |
| 4141 | blk = htx_get_first_blk(htx); |
Christopher Faulet | ea00973 | 2019-11-18 15:50:25 +0100 | [diff] [blame] | 4142 | BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4143 | sl = htx_get_blk_ptr(htx, blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4144 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4145 | /* Stores the request method. */ |
| 4146 | lua_pushstring(L, "method"); |
| 4147 | lua_pushlstring(L, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); |
| 4148 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4149 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4150 | /* Stores the http version. */ |
| 4151 | lua_pushstring(L, "version"); |
| 4152 | lua_pushlstring(L, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); |
| 4153 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4154 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4155 | /* creates an array of headers. hlua_http_get_headers() crates and push |
| 4156 | * the array on the top of the stack. |
| 4157 | */ |
| 4158 | lua_pushstring(L, "headers"); |
| 4159 | htxn.s = s; |
| 4160 | htxn.p = px; |
| 4161 | htxn.dir = SMP_OPT_DIR_REQ; |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 4162 | if (!hlua_http_get_headers(L, &htxn.s->txn->req)) |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4163 | return 0; |
| 4164 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4165 | |
Amaury Denoyelle | c453f95 | 2021-07-06 11:40:12 +0200 | [diff] [blame] | 4166 | parser = http_uri_parser_init(htx_sl_req_uri(sl)); |
| 4167 | path = http_parse_path(&parser); |
Tim Duesterhus | ed52637 | 2020-03-05 17:56:33 +0100 | [diff] [blame] | 4168 | if (isttest(path)) { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4169 | char *p, *q, *end; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4170 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4171 | p = path.ptr; |
| 4172 | end = path.ptr + path.len; |
| 4173 | q = p; |
| 4174 | while (q < end && *q != '?') |
| 4175 | q++; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4176 | |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 4177 | /* Stores the request path. */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4178 | lua_pushstring(L, "path"); |
| 4179 | lua_pushlstring(L, p, q - p); |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 4180 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4181 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4182 | /* Stores the query string. */ |
| 4183 | lua_pushstring(L, "qs"); |
| 4184 | if (*q == '?') |
| 4185 | q++; |
| 4186 | lua_pushlstring(L, q, end - q); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4187 | lua_settable(L, -3); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4188 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4189 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4190 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 4191 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 4192 | enum htx_blk_type type = htx_get_blk_type(blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4193 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 4194 | if (type == HTX_BLK_TLR || type == HTX_BLK_EOT) |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4195 | break; |
| 4196 | if (type == HTX_BLK_DATA) |
| 4197 | len += htx_get_blksz(blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4198 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4199 | if (htx->extra != ULLONG_MAX) |
| 4200 | len += htx->extra; |
| 4201 | |
| 4202 | /* Stores the request path. */ |
| 4203 | lua_pushstring(L, "length"); |
| 4204 | lua_pushinteger(L, len); |
| 4205 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4206 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4207 | /* Create an empty array of HTTP request headers. */ |
| 4208 | lua_pushstring(L, "response"); |
| 4209 | lua_newtable(L); |
| 4210 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4211 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4212 | /* Pop a class stream metatable and affect it to the table. */ |
| 4213 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref); |
| 4214 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4215 | |
| 4216 | return 1; |
| 4217 | } |
| 4218 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4219 | __LJMP static int hlua_applet_http_set_var(lua_State *L) |
| 4220 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4221 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4222 | struct stream *s; |
| 4223 | const char *name; |
| 4224 | size_t len; |
| 4225 | struct sample smp; |
| 4226 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 4227 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 4228 | 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] | 4229 | |
| 4230 | /* It is useles to retrieve the stream, but this function |
| 4231 | * runs only in a stream context. |
| 4232 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4233 | luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4234 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4235 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4236 | |
| 4237 | /* Converts the third argument in a sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 4238 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4239 | hlua_lua2smp(L, 3, &smp); |
| 4240 | |
| 4241 | /* Store the sample in a variable. */ |
| 4242 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 4243 | |
| 4244 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 4245 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 4246 | else |
| 4247 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 4248 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 4249 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4250 | } |
| 4251 | |
| 4252 | __LJMP static int hlua_applet_http_unset_var(lua_State *L) |
| 4253 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4254 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4255 | struct stream *s; |
| 4256 | const char *name; |
| 4257 | size_t len; |
| 4258 | struct sample smp; |
| 4259 | |
| 4260 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 4261 | |
| 4262 | /* It is useles to retrieve the stream, but this function |
| 4263 | * runs only in a stream context. |
| 4264 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4265 | luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4266 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4267 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4268 | |
| 4269 | /* Unset the variable. */ |
| 4270 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 4271 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 4272 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4273 | } |
| 4274 | |
| 4275 | __LJMP static int hlua_applet_http_get_var(lua_State *L) |
| 4276 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4277 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4278 | struct stream *s; |
| 4279 | const char *name; |
| 4280 | size_t len; |
| 4281 | struct sample smp; |
| 4282 | |
| 4283 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 4284 | |
| 4285 | /* It is useles to retrieve the stream, but this function |
| 4286 | * runs only in a stream context. |
| 4287 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4288 | luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4289 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4290 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4291 | |
| 4292 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 4293 | if (!vars_get_by_name(name, len, &smp)) { |
| 4294 | lua_pushnil(L); |
| 4295 | return 1; |
| 4296 | } |
| 4297 | |
| 4298 | return hlua_smp2lua(L, &smp); |
| 4299 | } |
| 4300 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4301 | __LJMP static int hlua_applet_http_set_priv(lua_State *L) |
| 4302 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4303 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4304 | struct stream *s = luactx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4305 | struct hlua *hlua; |
| 4306 | |
| 4307 | /* 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] | 4308 | if (!s->hlua) |
| 4309 | return 0; |
| 4310 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4311 | |
| 4312 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 4313 | |
| 4314 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 4315 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4316 | |
| 4317 | /* Get and store new value. */ |
| 4318 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 4319 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 4320 | |
| 4321 | return 0; |
| 4322 | } |
| 4323 | |
| 4324 | __LJMP static int hlua_applet_http_get_priv(lua_State *L) |
| 4325 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4326 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4327 | struct stream *s = luactx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4328 | struct hlua *hlua; |
| 4329 | |
| 4330 | /* 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] | 4331 | if (!s->hlua) { |
| 4332 | lua_pushnil(L); |
| 4333 | return 1; |
| 4334 | } |
| 4335 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4336 | |
| 4337 | /* Push configuration index in the stack. */ |
| 4338 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 4339 | |
| 4340 | return 1; |
| 4341 | } |
| 4342 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4343 | /* If expected data not yet available, it returns a yield. This function |
| 4344 | * consumes the data in the buffer. It returns a string containing the |
| 4345 | * data. This string can be empty. |
| 4346 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4347 | __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] | 4348 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4349 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4350 | struct stream_interface *si = luactx->appctx->owner; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4351 | struct channel *req = si_oc(si); |
| 4352 | struct htx *htx; |
| 4353 | struct htx_blk *blk; |
| 4354 | size_t count; |
| 4355 | int stop = 0; |
| 4356 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4357 | htx = htx_from_buf(&req->buf); |
| 4358 | count = co_data(req); |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 4359 | blk = htx_get_first_blk(htx); |
Christopher Faulet | cc26b13 | 2018-12-18 21:20:57 +0100 | [diff] [blame] | 4360 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4361 | while (count && !stop && blk) { |
| 4362 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4363 | uint32_t sz = htx_get_blksz(blk); |
| 4364 | struct ist v; |
| 4365 | uint32_t vlen; |
| 4366 | char *nl; |
| 4367 | |
| 4368 | vlen = sz; |
| 4369 | if (vlen > count) { |
| 4370 | if (type != HTX_BLK_DATA) |
| 4371 | break; |
| 4372 | vlen = count; |
| 4373 | } |
| 4374 | |
| 4375 | switch (type) { |
| 4376 | case HTX_BLK_UNUSED: |
| 4377 | break; |
| 4378 | |
| 4379 | case HTX_BLK_DATA: |
| 4380 | v = htx_get_blk_value(htx, blk); |
| 4381 | v.len = vlen; |
| 4382 | nl = istchr(v, '\n'); |
| 4383 | if (nl != NULL) { |
| 4384 | stop = 1; |
| 4385 | vlen = nl - v.ptr + 1; |
| 4386 | } |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4387 | luaL_addlstring(&luactx->b, v.ptr, vlen); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4388 | break; |
| 4389 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4390 | case HTX_BLK_TLR: |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 4391 | case HTX_BLK_EOT: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4392 | stop = 1; |
| 4393 | break; |
| 4394 | |
| 4395 | default: |
| 4396 | break; |
| 4397 | } |
| 4398 | |
| 4399 | co_set_data(req, co_data(req) - vlen); |
| 4400 | count -= vlen; |
| 4401 | if (sz == vlen) |
| 4402 | blk = htx_remove_blk(htx, blk); |
| 4403 | else { |
| 4404 | htx_cut_data_blk(htx, blk, vlen); |
| 4405 | break; |
| 4406 | } |
| 4407 | } |
| 4408 | |
Christopher Faulet | eccb31c | 2021-04-02 14:24:56 +0200 | [diff] [blame] | 4409 | /* The message was fully consumed and no more data are expected |
| 4410 | * (EOM flag set). |
| 4411 | */ |
| 4412 | if (htx_is_empty(htx) && (htx->flags & HTX_FL_EOM)) |
| 4413 | stop = 1; |
| 4414 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4415 | htx_to_buf(htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4416 | if (!stop) { |
| 4417 | si_cant_get(si); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4418 | 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] | 4419 | } |
| 4420 | |
| 4421 | /* return the result. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4422 | luaL_pushresult(&luactx->b); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4423 | return 1; |
| 4424 | } |
| 4425 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4426 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4427 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4428 | __LJMP static int hlua_applet_http_getline(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4429 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4430 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4431 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4432 | /* Initialise the string catenation. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4433 | luaL_buffinit(L, &luactx->b); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4434 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4435 | return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4436 | } |
| 4437 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4438 | /* If expected data not yet available, it returns a yield. This function |
| 4439 | * consumes the data in the buffer. It returns a string containing the |
| 4440 | * data. This string can be empty. |
| 4441 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4442 | __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] | 4443 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4444 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4445 | struct stream_interface *si = luactx->appctx->owner; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4446 | struct channel *req = si_oc(si); |
| 4447 | struct htx *htx; |
| 4448 | struct htx_blk *blk; |
| 4449 | size_t count; |
| 4450 | int len; |
| 4451 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4452 | htx = htx_from_buf(&req->buf); |
| 4453 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4454 | count = co_data(req); |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 4455 | blk = htx_get_head_blk(htx); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4456 | while (count && len && blk) { |
| 4457 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4458 | uint32_t sz = htx_get_blksz(blk); |
| 4459 | struct ist v; |
| 4460 | uint32_t vlen; |
| 4461 | |
| 4462 | vlen = sz; |
| 4463 | if (len > 0 && vlen > len) |
| 4464 | vlen = len; |
| 4465 | if (vlen > count) { |
| 4466 | if (type != HTX_BLK_DATA) |
| 4467 | break; |
| 4468 | vlen = count; |
| 4469 | } |
| 4470 | |
| 4471 | switch (type) { |
| 4472 | case HTX_BLK_UNUSED: |
| 4473 | break; |
| 4474 | |
| 4475 | case HTX_BLK_DATA: |
| 4476 | v = htx_get_blk_value(htx, blk); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4477 | luaL_addlstring(&luactx->b, v.ptr, vlen); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4478 | break; |
| 4479 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4480 | case HTX_BLK_TLR: |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 4481 | case HTX_BLK_EOT: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4482 | len = 0; |
| 4483 | break; |
| 4484 | |
| 4485 | default: |
| 4486 | break; |
| 4487 | } |
| 4488 | |
| 4489 | co_set_data(req, co_data(req) - vlen); |
| 4490 | count -= vlen; |
| 4491 | if (len > 0) |
| 4492 | len -= vlen; |
| 4493 | if (sz == vlen) |
| 4494 | blk = htx_remove_blk(htx, blk); |
| 4495 | else { |
| 4496 | htx_cut_data_blk(htx, blk, vlen); |
| 4497 | break; |
| 4498 | } |
| 4499 | } |
| 4500 | |
Christopher Faulet | eccb31c | 2021-04-02 14:24:56 +0200 | [diff] [blame] | 4501 | /* The message was fully consumed and no more data are expected |
| 4502 | * (EOM flag set). |
| 4503 | */ |
| 4504 | if (htx_is_empty(htx) && (htx->flags & HTX_FL_EOM)) |
| 4505 | len = 0; |
| 4506 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4507 | htx_to_buf(htx, &req->buf); |
| 4508 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4509 | /* If we are no other data available, yield waiting for new data. */ |
| 4510 | if (len) { |
| 4511 | if (len > 0) { |
| 4512 | lua_pushinteger(L, len); |
| 4513 | lua_replace(L, 2); |
| 4514 | } |
| 4515 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4516 | 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] | 4517 | } |
| 4518 | |
| 4519 | /* return the result. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4520 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4521 | return 1; |
| 4522 | } |
| 4523 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4524 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4525 | __LJMP static int hlua_applet_http_recv(lua_State *L) |
| 4526 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4527 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4528 | int len = -1; |
| 4529 | |
| 4530 | /* Check arguments. */ |
| 4531 | if (lua_gettop(L) > 2) |
| 4532 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 4533 | if (lua_gettop(L) >= 2) { |
| 4534 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4535 | lua_pop(L, 1); |
| 4536 | } |
| 4537 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4538 | lua_pushinteger(L, len); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4539 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4540 | /* Initialise the string catenation. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4541 | luaL_buffinit(L, &luactx->b); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4542 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4543 | return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4544 | } |
| 4545 | |
| 4546 | /* Append data in the output side of the buffer. This data is immediately |
| 4547 | * sent. The function returns the amount of data written. If the buffer |
| 4548 | * cannot contain the data, the function yields. The function returns -1 |
| 4549 | * if the channel is closed. |
| 4550 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4551 | __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] | 4552 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4553 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4554 | struct stream_interface *si = luactx->appctx->owner; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4555 | struct channel *res = si_ic(si); |
| 4556 | struct htx *htx = htx_from_buf(&res->buf); |
| 4557 | const char *data; |
| 4558 | size_t len; |
| 4559 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 4560 | int max; |
| 4561 | |
Christopher Faulet | 9060fc0 | 2019-07-03 11:39:30 +0200 | [diff] [blame] | 4562 | max = htx_get_max_blksz(htx, channel_htx_recv_max(res, htx)); |
Christopher Faulet | 4b0e9b2 | 2019-01-09 12:16:58 +0100 | [diff] [blame] | 4563 | if (!max) |
| 4564 | goto snd_yield; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4565 | |
| 4566 | data = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4567 | |
| 4568 | /* Get the max amount of data which can write as input in the channel. */ |
| 4569 | if (max > (len - l)) |
| 4570 | max = len - l; |
| 4571 | |
| 4572 | /* Copy data. */ |
Willy Tarreau | 0a7ef02 | 2019-05-28 10:30:11 +0200 | [diff] [blame] | 4573 | max = htx_add_data(htx, ist2(data + l, max)); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 4574 | channel_add_input(res, max); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4575 | |
| 4576 | /* update counters. */ |
| 4577 | l += max; |
| 4578 | lua_pop(L, 1); |
| 4579 | lua_pushinteger(L, l); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4580 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4581 | /* If some data is not send, declares the situation to the |
| 4582 | * applet, and returns a yield. |
| 4583 | */ |
| 4584 | if (l < len) { |
Christopher Faulet | 4b0e9b2 | 2019-01-09 12:16:58 +0100 | [diff] [blame] | 4585 | snd_yield: |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4586 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4587 | si_rx_room_blk(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4588 | 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] | 4589 | } |
| 4590 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4591 | htx_to_buf(htx, &res->buf); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4592 | return 1; |
| 4593 | } |
| 4594 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4595 | /* Just a wrapper of "hlua_applet_send_yield". This wrapper permits |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4596 | * yield the LUA process, and resume it without checking the |
| 4597 | * input arguments. |
| 4598 | */ |
| 4599 | __LJMP static int hlua_applet_http_send(lua_State *L) |
| 4600 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4601 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4602 | |
| 4603 | /* We want to send some data. Headers must be sent. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4604 | if (!(luactx->appctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4605 | hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data."); |
| 4606 | WILL_LJMP(lua_error(L)); |
| 4607 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4608 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4609 | /* This integer is used for followinf the amount of data sent. */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4610 | lua_pushinteger(L, 0); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4611 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4612 | return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4613 | } |
| 4614 | |
| 4615 | __LJMP static int hlua_applet_http_addheader(lua_State *L) |
| 4616 | { |
| 4617 | const char *name; |
| 4618 | int ret; |
| 4619 | |
| 4620 | MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4621 | name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 4622 | MAY_LJMP(luaL_checkstring(L, 3)); |
| 4623 | |
| 4624 | /* Push in the stack the "response" entry. */ |
| 4625 | ret = lua_getfield(L, 1, "response"); |
| 4626 | if (ret != LUA_TTABLE) { |
| 4627 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] " |
| 4628 | "is expected as an array. %s found", lua_typename(L, ret)); |
| 4629 | WILL_LJMP(lua_error(L)); |
| 4630 | } |
| 4631 | |
| 4632 | /* check if the header is already registered if it is not |
| 4633 | * the case, register it. |
| 4634 | */ |
| 4635 | ret = lua_getfield(L, -1, name); |
| 4636 | if (ret == LUA_TNIL) { |
| 4637 | |
| 4638 | /* Entry not found. */ |
| 4639 | lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */ |
| 4640 | |
| 4641 | /* Insert the new header name in the array in the top of the stack. |
| 4642 | * It left the new array in the top of the stack. |
| 4643 | */ |
| 4644 | lua_newtable(L); |
| 4645 | lua_pushvalue(L, 2); |
| 4646 | lua_pushvalue(L, -2); |
| 4647 | lua_settable(L, -4); |
| 4648 | |
| 4649 | } else if (ret != LUA_TTABLE) { |
| 4650 | |
| 4651 | /* corruption error. */ |
| 4652 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] " |
| 4653 | "is expected as an array. %s found", name, lua_typename(L, ret)); |
| 4654 | WILL_LJMP(lua_error(L)); |
| 4655 | } |
| 4656 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 4657 | /* Now the top of thestack is an array of values. We push |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4658 | * the header value as new entry. |
| 4659 | */ |
| 4660 | lua_pushvalue(L, 3); |
| 4661 | ret = lua_rawlen(L, -2); |
| 4662 | lua_rawseti(L, -2, ret + 1); |
| 4663 | lua_pushboolean(L, 1); |
| 4664 | return 1; |
| 4665 | } |
| 4666 | |
| 4667 | __LJMP static int hlua_applet_http_status(lua_State *L) |
| 4668 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4669 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4670 | int status = MAY_LJMP(luaL_checkinteger(L, 2)); |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4671 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4672 | |
| 4673 | if (status < 100 || status > 599) { |
| 4674 | lua_pushboolean(L, 0); |
| 4675 | return 1; |
| 4676 | } |
| 4677 | |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4678 | luactx->appctx->ctx.hlua_apphttp.status = status; |
| 4679 | luactx->appctx->ctx.hlua_apphttp.reason = reason; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4680 | lua_pushboolean(L, 1); |
| 4681 | return 1; |
| 4682 | } |
| 4683 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4684 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4685 | __LJMP static int hlua_applet_http_send_response(lua_State *L) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4686 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4687 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4688 | struct stream_interface *si = luactx->appctx->owner; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4689 | struct channel *res = si_ic(si); |
| 4690 | struct htx *htx; |
| 4691 | struct htx_sl *sl; |
| 4692 | struct h1m h1m; |
| 4693 | const char *status, *reason; |
| 4694 | const char *name, *value; |
| 4695 | size_t nlen, vlen; |
| 4696 | unsigned int flags; |
| 4697 | |
| 4698 | /* Send the message at once. */ |
| 4699 | htx = htx_from_buf(&res->buf); |
| 4700 | h1m_init_res(&h1m); |
| 4701 | |
| 4702 | /* Use the same http version than the request. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4703 | status = ultoa_r(luactx->appctx->ctx.hlua_apphttp.status, trash.area, trash.size); |
| 4704 | reason = luactx->appctx->ctx.hlua_apphttp.reason; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4705 | if (reason == NULL) |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4706 | reason = http_get_reason(luactx->appctx->ctx.hlua_apphttp.status); |
| 4707 | if (luactx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4708 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11); |
| 4709 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist(status), ist(reason)); |
| 4710 | } |
| 4711 | else { |
| 4712 | flags = HTX_SL_F_IS_RESP; |
| 4713 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), ist(status), ist(reason)); |
| 4714 | } |
| 4715 | if (!sl) { |
| 4716 | hlua_pusherror(L, "Lua applet http '%s': Failed to create response.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4717 | luactx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4718 | WILL_LJMP(lua_error(L)); |
| 4719 | } |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4720 | sl->info.res.status = luactx->appctx->ctx.hlua_apphttp.status; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4721 | |
| 4722 | /* Get the array associated to the field "response" in the object AppletHTTP. */ |
| 4723 | lua_pushvalue(L, 0); |
| 4724 | if (lua_getfield(L, 1, "response") != LUA_TTABLE) { |
| 4725 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4726 | luactx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4727 | WILL_LJMP(lua_error(L)); |
| 4728 | } |
| 4729 | |
| 4730 | /* Browse the list of headers. */ |
| 4731 | lua_pushnil(L); |
| 4732 | while(lua_next(L, -2) != 0) { |
| 4733 | /* We expect a string as -2. */ |
| 4734 | if (lua_type(L, -2) != LUA_TSTRING) { |
| 4735 | 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] | 4736 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4737 | lua_typename(L, lua_type(L, -2))); |
| 4738 | WILL_LJMP(lua_error(L)); |
| 4739 | } |
| 4740 | name = lua_tolstring(L, -2, &nlen); |
| 4741 | |
| 4742 | /* We expect an array as -1. */ |
| 4743 | if (lua_type(L, -1) != LUA_TTABLE) { |
| 4744 | 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] | 4745 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4746 | name, |
| 4747 | lua_typename(L, lua_type(L, -1))); |
| 4748 | WILL_LJMP(lua_error(L)); |
| 4749 | } |
| 4750 | |
| 4751 | /* Browse the table who is on the top of the stack. */ |
| 4752 | lua_pushnil(L); |
| 4753 | while(lua_next(L, -2) != 0) { |
| 4754 | int id; |
| 4755 | |
| 4756 | /* We expect a number as -2. */ |
| 4757 | if (lua_type(L, -2) != LUA_TNUMBER) { |
| 4758 | 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] | 4759 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4760 | name, |
| 4761 | lua_typename(L, lua_type(L, -2))); |
| 4762 | WILL_LJMP(lua_error(L)); |
| 4763 | } |
| 4764 | id = lua_tointeger(L, -2); |
| 4765 | |
| 4766 | /* We expect a string as -2. */ |
| 4767 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 4768 | 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] | 4769 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4770 | name, id, |
| 4771 | lua_typename(L, lua_type(L, -1))); |
| 4772 | WILL_LJMP(lua_error(L)); |
| 4773 | } |
| 4774 | value = lua_tolstring(L, -1, &vlen); |
| 4775 | |
| 4776 | /* Simple Protocol checks. */ |
| 4777 | if (isteqi(ist2(name, nlen), ist("transfer-encoding"))) |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 4778 | h1_parse_xfer_enc_header(&h1m, ist2(value, vlen)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4779 | else if (isteqi(ist2(name, nlen), ist("content-length"))) { |
| 4780 | struct ist v = ist2(value, vlen); |
| 4781 | int ret; |
| 4782 | |
| 4783 | ret = h1_parse_cont_len_header(&h1m, &v); |
| 4784 | if (ret < 0) { |
| 4785 | hlua_pusherror(L, "Lua applet http '%s': Invalid '%s' header.\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); |
| 4788 | WILL_LJMP(lua_error(L)); |
| 4789 | } |
| 4790 | else if (ret == 0) |
| 4791 | goto next; /* Skip it */ |
| 4792 | } |
| 4793 | |
| 4794 | /* Add a new header */ |
| 4795 | if (!htx_add_header(htx, ist2(name, nlen), ist2(value, vlen))) { |
| 4796 | 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] | 4797 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4798 | name); |
| 4799 | WILL_LJMP(lua_error(L)); |
| 4800 | } |
| 4801 | next: |
| 4802 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 4803 | lua_pop(L, 1); |
| 4804 | } |
| 4805 | |
| 4806 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 4807 | lua_pop(L, 1); |
| 4808 | } |
| 4809 | |
| 4810 | if (h1m.flags & H1_MF_CHNK) |
| 4811 | h1m.flags &= ~H1_MF_CLEN; |
| 4812 | if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK)) |
| 4813 | h1m.flags |= H1_MF_XFER_LEN; |
| 4814 | |
| 4815 | /* Uset HTX start-line flags */ |
| 4816 | if (h1m.flags & H1_MF_XFER_ENC) |
| 4817 | flags |= HTX_SL_F_XFER_ENC; |
| 4818 | if (h1m.flags & H1_MF_XFER_LEN) { |
| 4819 | flags |= HTX_SL_F_XFER_LEN; |
| 4820 | if (h1m.flags & H1_MF_CHNK) |
| 4821 | flags |= HTX_SL_F_CHNK; |
| 4822 | else if (h1m.flags & H1_MF_CLEN) |
| 4823 | flags |= HTX_SL_F_CLEN; |
| 4824 | if (h1m.body_len == 0) |
| 4825 | flags |= HTX_SL_F_BODYLESS; |
| 4826 | } |
| 4827 | sl->flags |= flags; |
| 4828 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 4829 | /* 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] | 4830 | * and the status code implies the presence of a message body, we must |
| 4831 | * announce a transfer encoding chunked. This is required by haproxy |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4832 | * for the keepalive compliance. If the applet announces a transfer-encoding |
| 4833 | * chunked itself, don't do anything. |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4834 | */ |
| 4835 | 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] | 4836 | luactx->appctx->ctx.hlua_apphttp.status >= 200 && |
| 4837 | luactx->appctx->ctx.hlua_apphttp.status != 204 && |
| 4838 | luactx->appctx->ctx.hlua_apphttp.status != 304) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4839 | /* Add a new header */ |
| 4840 | sl->flags |= (HTX_SL_F_XFER_ENC|H1_MF_CHNK|H1_MF_XFER_LEN); |
| 4841 | if (!htx_add_header(htx, ist("transfer-encoding"), ist("chunked"))) { |
| 4842 | 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] | 4843 | luactx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4844 | WILL_LJMP(lua_error(L)); |
| 4845 | } |
| 4846 | } |
| 4847 | |
| 4848 | /* Finalize headers. */ |
| 4849 | if (!htx_add_endof(htx, HTX_BLK_EOH)) { |
| 4850 | hlua_pusherror(L, "Lua applet http '%s': Failed create the response.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4851 | luactx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4852 | WILL_LJMP(lua_error(L)); |
| 4853 | } |
| 4854 | |
| 4855 | if (htx_used_space(htx) > b_size(&res->buf) - global.tune.maxrewrite) { |
| 4856 | b_reset(&res->buf); |
| 4857 | hlua_pusherror(L, "Lua: 'start_response': response header block too big"); |
| 4858 | WILL_LJMP(lua_error(L)); |
| 4859 | } |
| 4860 | |
| 4861 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 4862 | channel_add_input(res, htx->data); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4863 | |
| 4864 | /* Headers sent, set the flag. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4865 | luactx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4866 | return 0; |
| 4867 | |
| 4868 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4869 | /* We will build the status line and the headers of the HTTP response. |
| 4870 | * We will try send at once if its not possible, we give back the hand |
| 4871 | * waiting for more room. |
| 4872 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4873 | __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] | 4874 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4875 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4876 | struct stream_interface *si = luactx->appctx->owner; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4877 | struct channel *res = si_ic(si); |
| 4878 | |
| 4879 | if (co_data(res)) { |
| 4880 | si_rx_room_blk(si); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4881 | 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] | 4882 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4883 | return MAY_LJMP(hlua_applet_http_send_response(L)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4884 | } |
| 4885 | |
| 4886 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4887 | __LJMP static int hlua_applet_http_start_response(lua_State *L) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4888 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4889 | return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4890 | } |
| 4891 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4892 | /* |
| 4893 | * |
| 4894 | * |
| 4895 | * Class HTTP |
| 4896 | * |
| 4897 | * |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4898 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4899 | |
| 4900 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 4901 | * a class stream, otherwise it throws an error. |
| 4902 | */ |
| 4903 | __LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4904 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4905 | return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref)); |
| 4906 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4907 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4908 | /* This function creates and push in the stack a HTTP object |
| 4909 | * according with a current TXN. |
| 4910 | */ |
| 4911 | static int hlua_http_new(lua_State *L, struct hlua_txn *txn) |
| 4912 | { |
| 4913 | struct hlua_txn *htxn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4914 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4915 | /* Check stack size. */ |
| 4916 | if (!lua_checkstack(L, 3)) |
| 4917 | return 0; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4918 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4919 | /* Create the object: obj[0] = userdata. |
| 4920 | * Note that the base of the Converters object is the |
| 4921 | * same than the TXN object. |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4922 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4923 | lua_newtable(L); |
| 4924 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
| 4925 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4926 | |
| 4927 | htxn->s = txn->s; |
| 4928 | htxn->p = txn->p; |
Christopher Faulet | 256b69a | 2019-05-23 11:14:21 +0200 | [diff] [blame] | 4929 | htxn->dir = txn->dir; |
| 4930 | htxn->flags = txn->flags; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4931 | |
| 4932 | /* Pop a class stream metatable and affect it to the table. */ |
| 4933 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref); |
| 4934 | lua_setmetatable(L, -2); |
| 4935 | |
| 4936 | return 1; |
| 4937 | } |
| 4938 | |
| 4939 | /* This function creates ans returns an array of HTTP headers. |
| 4940 | * This function does not fails. It is used as wrapper with the |
| 4941 | * 2 following functions. |
| 4942 | */ |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 4943 | __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] | 4944 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4945 | struct htx *htx; |
| 4946 | int32_t pos; |
| 4947 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4948 | /* Create the table. */ |
| 4949 | lua_newtable(L); |
| 4950 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4951 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4952 | htx = htxbuf(&msg->chn->buf); |
| 4953 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 4954 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 4955 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4956 | struct ist n, v; |
| 4957 | int len; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 4958 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4959 | if (type == HTX_BLK_HDR) { |
| 4960 | n = htx_get_blk_name(htx,blk); |
| 4961 | v = htx_get_blk_value(htx, blk); |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 4962 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4963 | else if (type == HTX_BLK_EOH) |
| 4964 | break; |
| 4965 | else |
| 4966 | continue; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 4967 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4968 | /* Check for existing entry: |
| 4969 | * assume that the table is on the top of the stack, and |
| 4970 | * push the key in the stack, the function lua_gettable() |
| 4971 | * perform the lookup. |
| 4972 | */ |
| 4973 | lua_pushlstring(L, n.ptr, n.len); |
| 4974 | lua_gettable(L, -2); |
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 | switch (lua_type(L, -1)) { |
| 4977 | case LUA_TNIL: |
| 4978 | /* Table not found, create it. */ |
| 4979 | lua_pop(L, 1); /* remove the nil value. */ |
| 4980 | lua_pushlstring(L, n.ptr, n.len); /* push the header name as key. */ |
| 4981 | lua_newtable(L); /* create and push empty table. */ |
| 4982 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 4983 | lua_rawseti(L, -2, 0); /* index header value (pop it). */ |
| 4984 | 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] | 4985 | break; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 4986 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4987 | case LUA_TTABLE: |
| 4988 | /* Entry found: push the value in the table. */ |
| 4989 | len = lua_rawlen(L, -1); |
| 4990 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 4991 | lua_rawseti(L, -2, len+1); /* index header value (pop it). */ |
| 4992 | lua_pop(L, 1); /* remove the table (it is stored in the main table). */ |
| 4993 | break; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4994 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4995 | default: |
| 4996 | /* Other cases are errors. */ |
| 4997 | hlua_pusherror(L, "internal error during the parsing of headers."); |
| 4998 | WILL_LJMP(lua_error(L)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4999 | } |
| 5000 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5001 | return 1; |
| 5002 | } |
| 5003 | |
| 5004 | __LJMP static int hlua_http_req_get_headers(lua_State *L) |
| 5005 | { |
| 5006 | struct hlua_txn *htxn; |
| 5007 | |
| 5008 | MAY_LJMP(check_args(L, 1, "req_get_headers")); |
| 5009 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5010 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5011 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5012 | WILL_LJMP(lua_error(L)); |
| 5013 | |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 5014 | return hlua_http_get_headers(L, &htxn->s->txn->req); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5015 | } |
| 5016 | |
| 5017 | __LJMP static int hlua_http_res_get_headers(lua_State *L) |
| 5018 | { |
| 5019 | struct hlua_txn *htxn; |
| 5020 | |
| 5021 | MAY_LJMP(check_args(L, 1, "res_get_headers")); |
| 5022 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5023 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5024 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5025 | WILL_LJMP(lua_error(L)); |
| 5026 | |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 5027 | return hlua_http_get_headers(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5028 | } |
| 5029 | |
| 5030 | /* This function replace full header, or just a value in |
| 5031 | * the request or in the response. It is a wrapper fir the |
| 5032 | * 4 following functions. |
| 5033 | */ |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5034 | __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] | 5035 | { |
| 5036 | size_t name_len; |
| 5037 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5038 | const char *reg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 5039 | const char *value = MAY_LJMP(luaL_checkstring(L, 4)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5040 | struct htx *htx; |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 5041 | struct my_regex *re; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5042 | |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 5043 | if (!(re = regex_comp(reg, 1, 1, NULL))) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5044 | WILL_LJMP(luaL_argerror(L, 3, "invalid regex")); |
| 5045 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5046 | htx = htxbuf(&msg->chn->buf); |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5047 | 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] | 5048 | regex_free(re); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5049 | return 0; |
| 5050 | } |
| 5051 | |
| 5052 | __LJMP static int hlua_http_req_rep_hdr(lua_State *L) |
| 5053 | { |
| 5054 | struct hlua_txn *htxn; |
| 5055 | |
| 5056 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 5057 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5058 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5059 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5060 | WILL_LJMP(lua_error(L)); |
| 5061 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5062 | 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] | 5063 | } |
| 5064 | |
| 5065 | __LJMP static int hlua_http_res_rep_hdr(lua_State *L) |
| 5066 | { |
| 5067 | struct hlua_txn *htxn; |
| 5068 | |
| 5069 | MAY_LJMP(check_args(L, 4, "res_rep_hdr")); |
| 5070 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5071 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5072 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5073 | WILL_LJMP(lua_error(L)); |
| 5074 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5075 | 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] | 5076 | } |
| 5077 | |
| 5078 | __LJMP static int hlua_http_req_rep_val(lua_State *L) |
| 5079 | { |
| 5080 | struct hlua_txn *htxn; |
| 5081 | |
| 5082 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 5083 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5084 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5085 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5086 | WILL_LJMP(lua_error(L)); |
| 5087 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5088 | 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] | 5089 | } |
| 5090 | |
| 5091 | __LJMP static int hlua_http_res_rep_val(lua_State *L) |
| 5092 | { |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5093 | struct hlua_txn *htxn; |
| 5094 | |
| 5095 | MAY_LJMP(check_args(L, 4, "res_rep_val")); |
| 5096 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5097 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5098 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5099 | WILL_LJMP(lua_error(L)); |
| 5100 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5101 | 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] | 5102 | } |
| 5103 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5104 | /* This function deletes all the occurrences of an header. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5105 | * It is a wrapper for the 2 following functions. |
| 5106 | */ |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5107 | __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] | 5108 | { |
| 5109 | size_t len; |
| 5110 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5111 | struct htx *htx = htxbuf(&msg->chn->buf); |
| 5112 | struct http_hdr_ctx ctx; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5113 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5114 | ctx.blk = NULL; |
| 5115 | while (http_find_header(htx, ist2(name, len), &ctx, 1)) |
| 5116 | http_remove_header(htx, &ctx); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5117 | return 0; |
| 5118 | } |
| 5119 | |
| 5120 | __LJMP static int hlua_http_req_del_hdr(lua_State *L) |
| 5121 | { |
| 5122 | struct hlua_txn *htxn; |
| 5123 | |
| 5124 | MAY_LJMP(check_args(L, 2, "req_del_hdr")); |
| 5125 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5126 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5127 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5128 | WILL_LJMP(lua_error(L)); |
| 5129 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5130 | return hlua_http_del_hdr(L, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5131 | } |
| 5132 | |
| 5133 | __LJMP static int hlua_http_res_del_hdr(lua_State *L) |
| 5134 | { |
| 5135 | struct hlua_txn *htxn; |
| 5136 | |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5137 | MAY_LJMP(check_args(L, 2, "res_del_hdr")); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5138 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5139 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5140 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5141 | WILL_LJMP(lua_error(L)); |
| 5142 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5143 | return hlua_http_del_hdr(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5144 | } |
| 5145 | |
| 5146 | /* This function adds an header. It is a wrapper used by |
| 5147 | * the 2 following functions. |
| 5148 | */ |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5149 | __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] | 5150 | { |
| 5151 | size_t name_len; |
| 5152 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5153 | size_t value_len; |
| 5154 | const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5155 | struct htx *htx = htxbuf(&msg->chn->buf); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5156 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5157 | lua_pushboolean(L, http_add_header(htx, ist2(name, name_len), |
| 5158 | ist2(value, value_len))); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5159 | return 0; |
| 5160 | } |
| 5161 | |
| 5162 | __LJMP static int hlua_http_req_add_hdr(lua_State *L) |
| 5163 | { |
| 5164 | struct hlua_txn *htxn; |
| 5165 | |
| 5166 | MAY_LJMP(check_args(L, 3, "req_add_hdr")); |
| 5167 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5168 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5169 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5170 | WILL_LJMP(lua_error(L)); |
| 5171 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5172 | return hlua_http_add_hdr(L, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5173 | } |
| 5174 | |
| 5175 | __LJMP static int hlua_http_res_add_hdr(lua_State *L) |
| 5176 | { |
| 5177 | struct hlua_txn *htxn; |
| 5178 | |
| 5179 | MAY_LJMP(check_args(L, 3, "res_add_hdr")); |
| 5180 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5181 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5182 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5183 | WILL_LJMP(lua_error(L)); |
| 5184 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5185 | return hlua_http_add_hdr(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5186 | } |
| 5187 | |
| 5188 | static int hlua_http_req_set_hdr(lua_State *L) |
| 5189 | { |
| 5190 | struct hlua_txn *htxn; |
| 5191 | |
| 5192 | MAY_LJMP(check_args(L, 3, "req_set_hdr")); |
| 5193 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5194 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5195 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5196 | WILL_LJMP(lua_error(L)); |
| 5197 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5198 | hlua_http_del_hdr(L, &htxn->s->txn->req); |
| 5199 | return hlua_http_add_hdr(L, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5200 | } |
| 5201 | |
| 5202 | static int hlua_http_res_set_hdr(lua_State *L) |
| 5203 | { |
| 5204 | struct hlua_txn *htxn; |
| 5205 | |
| 5206 | MAY_LJMP(check_args(L, 3, "res_set_hdr")); |
| 5207 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5208 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5209 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5210 | WILL_LJMP(lua_error(L)); |
| 5211 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5212 | hlua_http_del_hdr(L, &htxn->s->txn->rsp); |
| 5213 | return hlua_http_add_hdr(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5214 | } |
| 5215 | |
| 5216 | /* This function set the method. */ |
| 5217 | static int hlua_http_req_set_meth(lua_State *L) |
| 5218 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5219 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5220 | size_t name_len; |
| 5221 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5222 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5223 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5224 | WILL_LJMP(lua_error(L)); |
| 5225 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5226 | 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] | 5227 | return 1; |
| 5228 | } |
| 5229 | |
| 5230 | /* This function set the method. */ |
| 5231 | static int hlua_http_req_set_path(lua_State *L) |
| 5232 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5233 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5234 | size_t name_len; |
| 5235 | 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] | 5236 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5237 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5238 | WILL_LJMP(lua_error(L)); |
| 5239 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5240 | 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] | 5241 | return 1; |
| 5242 | } |
| 5243 | |
| 5244 | /* This function set the query-string. */ |
| 5245 | static int hlua_http_req_set_query(lua_State *L) |
| 5246 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5247 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5248 | size_t name_len; |
| 5249 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5250 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5251 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5252 | WILL_LJMP(lua_error(L)); |
| 5253 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5254 | /* Check length. */ |
| 5255 | if (name_len > trash.size - 1) { |
| 5256 | lua_pushboolean(L, 0); |
| 5257 | return 1; |
| 5258 | } |
| 5259 | |
| 5260 | /* Add the mark question as prefix. */ |
| 5261 | chunk_reset(&trash); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5262 | trash.area[trash.data++] = '?'; |
| 5263 | memcpy(trash.area + trash.data, name, name_len); |
| 5264 | trash.data += name_len; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5265 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5266 | lua_pushboolean(L, |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5267 | 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] | 5268 | return 1; |
| 5269 | } |
| 5270 | |
| 5271 | /* This function set the uri. */ |
| 5272 | static int hlua_http_req_set_uri(lua_State *L) |
| 5273 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5274 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5275 | size_t name_len; |
| 5276 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5277 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5278 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5279 | WILL_LJMP(lua_error(L)); |
| 5280 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5281 | 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] | 5282 | return 1; |
| 5283 | } |
| 5284 | |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5285 | /* This function set the response code & optionally reason. */ |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5286 | static int hlua_http_res_set_status(lua_State *L) |
| 5287 | { |
| 5288 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5289 | unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2)); |
Christopher Faulet | 96bff76 | 2019-12-17 13:46:18 +0100 | [diff] [blame] | 5290 | const char *str = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
| 5291 | const struct ist reason = ist2(str, (str ? strlen(str) : 0)); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5292 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5293 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5294 | WILL_LJMP(lua_error(L)); |
| 5295 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5296 | http_res_set_status(code, reason, htxn->s); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5297 | return 0; |
| 5298 | } |
| 5299 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5300 | /* |
| 5301 | * |
| 5302 | * |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5303 | * Class TXN |
| 5304 | * |
| 5305 | * |
| 5306 | */ |
| 5307 | |
| 5308 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5309 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5310 | */ |
| 5311 | __LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud) |
| 5312 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 5313 | return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref)); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5314 | } |
| 5315 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5316 | __LJMP static int hlua_set_var(lua_State *L) |
| 5317 | { |
| 5318 | struct hlua_txn *htxn; |
| 5319 | const char *name; |
| 5320 | size_t len; |
| 5321 | struct sample smp; |
| 5322 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 5323 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 5324 | 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] | 5325 | |
| 5326 | /* It is useles to retrieve the stream, but this function |
| 5327 | * runs only in a stream context. |
| 5328 | */ |
| 5329 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5330 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5331 | |
| 5332 | /* Converts the third argument in a sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 5333 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5334 | hlua_lua2smp(L, 3, &smp); |
| 5335 | |
| 5336 | /* Store the sample in a variable. */ |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 5337 | 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] | 5338 | |
| 5339 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 5340 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 5341 | else |
| 5342 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 5343 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 5344 | return 1; |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5345 | } |
| 5346 | |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 5347 | __LJMP static int hlua_unset_var(lua_State *L) |
| 5348 | { |
| 5349 | struct hlua_txn *htxn; |
| 5350 | const char *name; |
| 5351 | size_t len; |
| 5352 | struct sample smp; |
| 5353 | |
| 5354 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 5355 | |
| 5356 | /* It is useles to retrieve the stream, but this function |
| 5357 | * runs only in a stream context. |
| 5358 | */ |
| 5359 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5360 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5361 | |
| 5362 | /* Unset the variable. */ |
| 5363 | 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] | 5364 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 5365 | return 1; |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 5366 | } |
| 5367 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5368 | __LJMP static int hlua_get_var(lua_State *L) |
| 5369 | { |
| 5370 | struct hlua_txn *htxn; |
| 5371 | const char *name; |
| 5372 | size_t len; |
| 5373 | struct sample smp; |
| 5374 | |
| 5375 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 5376 | |
| 5377 | /* It is useles to retrieve the stream, but this function |
| 5378 | * runs only in a stream context. |
| 5379 | */ |
| 5380 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5381 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5382 | |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 5383 | 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] | 5384 | if (!vars_get_by_name(name, len, &smp)) { |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5385 | lua_pushnil(L); |
| 5386 | return 1; |
| 5387 | } |
| 5388 | |
| 5389 | return hlua_smp2lua(L, &smp); |
| 5390 | } |
| 5391 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5392 | __LJMP static int hlua_set_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5393 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5394 | struct hlua *hlua; |
| 5395 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5396 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 5397 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5398 | /* It is useles to retrieve the stream, but this function |
| 5399 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5400 | */ |
| 5401 | MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 5402 | |
| 5403 | /* Get hlua struct, or NULL if we execute from main lua state */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5404 | hlua = hlua_gethlua(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 5405 | if (!hlua) |
| 5406 | return 0; |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5407 | |
| 5408 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 5409 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5410 | |
| 5411 | /* Get and store new value. */ |
| 5412 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 5413 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 5414 | |
| 5415 | return 0; |
| 5416 | } |
| 5417 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5418 | __LJMP static int hlua_get_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5419 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5420 | struct hlua *hlua; |
| 5421 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5422 | MAY_LJMP(check_args(L, 1, "get_priv")); |
| 5423 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5424 | /* It is useles to retrieve the stream, but this function |
| 5425 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5426 | */ |
| 5427 | MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 5428 | |
| 5429 | /* Get hlua struct, or NULL if we execute from main lua state */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5430 | hlua = hlua_gethlua(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 5431 | if (!hlua) { |
| 5432 | lua_pushnil(L); |
| 5433 | return 1; |
| 5434 | } |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5435 | |
| 5436 | /* Push configuration index in the stack. */ |
| 5437 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 5438 | |
| 5439 | return 1; |
| 5440 | } |
| 5441 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5442 | /* Create stack entry containing a class TXN. This function |
| 5443 | * return 0 if the stack does not contains free slots, |
| 5444 | * otherwise it returns 1. |
| 5445 | */ |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5446 | 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] | 5447 | { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5448 | struct hlua_txn *htxn; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5449 | |
| 5450 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5451 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5452 | return 0; |
| 5453 | |
| 5454 | /* NOTE: The allocation never fails. The failure |
| 5455 | * throw an error, and the function never returns. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5456 | * if the throw is not available, the process is aborted. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5457 | */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5458 | /* Create the object: obj[0] = userdata. */ |
| 5459 | lua_newtable(L); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5460 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5461 | lua_rawseti(L, -2, 0); |
| 5462 | |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5463 | htxn->s = s; |
| 5464 | htxn->p = p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 5465 | htxn->dir = dir; |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5466 | htxn->flags = flags; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5467 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 5468 | /* Create the "f" field that contains a list of fetches. */ |
| 5469 | lua_pushstring(L, "f"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 5470 | if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5471 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5472 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5473 | |
| 5474 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 5475 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 5476 | 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] | 5477 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5478 | lua_rawset(L, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 5479 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5480 | /* Create the "c" field that contains a list of converters. */ |
| 5481 | lua_pushstring(L, "c"); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5482 | if (!hlua_converters_new(L, htxn, 0)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5483 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5484 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5485 | |
| 5486 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 5487 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 5488 | if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5489 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5490 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5491 | |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5492 | /* Create the "req" field that contains the request channel object. */ |
| 5493 | lua_pushstring(L, "req"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 5494 | if (!hlua_channel_new(L, &s->req)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5495 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5496 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5497 | |
| 5498 | /* Create the "res" field that contains the response channel object. */ |
| 5499 | lua_pushstring(L, "res"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 5500 | if (!hlua_channel_new(L, &s->res)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5501 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5502 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5503 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5504 | /* Creates the HTTP object is the current proxy allows http. */ |
| 5505 | lua_pushstring(L, "http"); |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 5506 | if (IS_HTX_STRM(s)) { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5507 | if (!hlua_http_new(L, htxn)) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5508 | return 0; |
| 5509 | } |
| 5510 | else |
| 5511 | lua_pushnil(L); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5512 | lua_rawset(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5513 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5514 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 5515 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref); |
| 5516 | lua_setmetatable(L, -2); |
| 5517 | |
| 5518 | return 1; |
| 5519 | } |
| 5520 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 5521 | __LJMP static int hlua_txn_deflog(lua_State *L) |
| 5522 | { |
| 5523 | const char *msg; |
| 5524 | struct hlua_txn *htxn; |
| 5525 | |
| 5526 | MAY_LJMP(check_args(L, 2, "deflog")); |
| 5527 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5528 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5529 | |
| 5530 | hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg); |
| 5531 | return 0; |
| 5532 | } |
| 5533 | |
| 5534 | __LJMP static int hlua_txn_log(lua_State *L) |
| 5535 | { |
| 5536 | int level; |
| 5537 | const char *msg; |
| 5538 | struct hlua_txn *htxn; |
| 5539 | |
| 5540 | MAY_LJMP(check_args(L, 3, "log")); |
| 5541 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5542 | level = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5543 | msg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 5544 | |
| 5545 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 5546 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 5547 | |
| 5548 | hlua_sendlog(htxn->s->be, level, msg); |
| 5549 | return 0; |
| 5550 | } |
| 5551 | |
| 5552 | __LJMP static int hlua_txn_log_debug(lua_State *L) |
| 5553 | { |
| 5554 | const char *msg; |
| 5555 | struct hlua_txn *htxn; |
| 5556 | |
| 5557 | MAY_LJMP(check_args(L, 2, "Debug")); |
| 5558 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5559 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5560 | hlua_sendlog(htxn->s->be, LOG_DEBUG, msg); |
| 5561 | return 0; |
| 5562 | } |
| 5563 | |
| 5564 | __LJMP static int hlua_txn_log_info(lua_State *L) |
| 5565 | { |
| 5566 | const char *msg; |
| 5567 | struct hlua_txn *htxn; |
| 5568 | |
| 5569 | MAY_LJMP(check_args(L, 2, "Info")); |
| 5570 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5571 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5572 | hlua_sendlog(htxn->s->be, LOG_INFO, msg); |
| 5573 | return 0; |
| 5574 | } |
| 5575 | |
| 5576 | __LJMP static int hlua_txn_log_warning(lua_State *L) |
| 5577 | { |
| 5578 | const char *msg; |
| 5579 | struct hlua_txn *htxn; |
| 5580 | |
| 5581 | MAY_LJMP(check_args(L, 2, "Warning")); |
| 5582 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5583 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5584 | hlua_sendlog(htxn->s->be, LOG_WARNING, msg); |
| 5585 | return 0; |
| 5586 | } |
| 5587 | |
| 5588 | __LJMP static int hlua_txn_log_alert(lua_State *L) |
| 5589 | { |
| 5590 | const char *msg; |
| 5591 | struct hlua_txn *htxn; |
| 5592 | |
| 5593 | MAY_LJMP(check_args(L, 2, "Alert")); |
| 5594 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5595 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5596 | hlua_sendlog(htxn->s->be, LOG_ALERT, msg); |
| 5597 | return 0; |
| 5598 | } |
| 5599 | |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5600 | __LJMP static int hlua_txn_set_loglevel(lua_State *L) |
| 5601 | { |
| 5602 | struct hlua_txn *htxn; |
| 5603 | int ll; |
| 5604 | |
| 5605 | MAY_LJMP(check_args(L, 2, "set_loglevel")); |
| 5606 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5607 | ll = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5608 | |
| 5609 | if (ll < 0 || ll > 7) |
| 5610 | WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7")); |
| 5611 | |
| 5612 | htxn->s->logs.level = ll; |
| 5613 | return 0; |
| 5614 | } |
| 5615 | |
| 5616 | __LJMP static int hlua_txn_set_tos(lua_State *L) |
| 5617 | { |
| 5618 | struct hlua_txn *htxn; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5619 | int tos; |
| 5620 | |
| 5621 | MAY_LJMP(check_args(L, 2, "set_tos")); |
| 5622 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5623 | tos = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5624 | |
Willy Tarreau | 1a18b54 | 2018-12-11 16:37:42 +0100 | [diff] [blame] | 5625 | conn_set_tos(objt_conn(htxn->s->sess->origin), tos); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5626 | return 0; |
| 5627 | } |
| 5628 | |
| 5629 | __LJMP static int hlua_txn_set_mark(lua_State *L) |
| 5630 | { |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5631 | struct hlua_txn *htxn; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5632 | int mark; |
| 5633 | |
| 5634 | MAY_LJMP(check_args(L, 2, "set_mark")); |
| 5635 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5636 | mark = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5637 | |
Lukas Tribus | 579e3e3 | 2019-08-11 18:03:45 +0200 | [diff] [blame] | 5638 | conn_set_mark(objt_conn(htxn->s->sess->origin), mark); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5639 | return 0; |
| 5640 | } |
| 5641 | |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 5642 | __LJMP static int hlua_txn_set_priority_class(lua_State *L) |
| 5643 | { |
| 5644 | struct hlua_txn *htxn; |
| 5645 | |
| 5646 | MAY_LJMP(check_args(L, 2, "set_priority_class")); |
| 5647 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5648 | htxn->s->priority_class = queue_limit_class(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 5649 | return 0; |
| 5650 | } |
| 5651 | |
| 5652 | __LJMP static int hlua_txn_set_priority_offset(lua_State *L) |
| 5653 | { |
| 5654 | struct hlua_txn *htxn; |
| 5655 | |
| 5656 | MAY_LJMP(check_args(L, 2, "set_priority_offset")); |
| 5657 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5658 | htxn->s->priority_offset = queue_limit_offset(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 5659 | return 0; |
| 5660 | } |
| 5661 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5662 | /* 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] | 5663 | * 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] | 5664 | * message and terminate the transaction. It returns 1 on success and 0 on |
| 5665 | * error. The Reply must be on top of the stack. |
| 5666 | */ |
| 5667 | __LJMP static int hlua_txn_forward_reply(lua_State *L, struct stream *s) |
| 5668 | { |
| 5669 | struct htx *htx; |
| 5670 | struct htx_sl *sl; |
| 5671 | struct h1m h1m; |
| 5672 | const char *status, *reason, *body; |
| 5673 | size_t status_len, reason_len, body_len; |
| 5674 | int ret, code, flags; |
| 5675 | |
| 5676 | code = 200; |
| 5677 | status = "200"; |
| 5678 | status_len = 3; |
| 5679 | ret = lua_getfield(L, -1, "status"); |
| 5680 | if (ret == LUA_TNUMBER) { |
| 5681 | code = lua_tointeger(L, -1); |
| 5682 | status = lua_tolstring(L, -1, &status_len); |
| 5683 | } |
| 5684 | lua_pop(L, 1); |
| 5685 | |
| 5686 | reason = http_get_reason(code); |
| 5687 | reason_len = strlen(reason); |
| 5688 | ret = lua_getfield(L, -1, "reason"); |
| 5689 | if (ret == LUA_TSTRING) |
| 5690 | reason = lua_tolstring(L, -1, &reason_len); |
| 5691 | lua_pop(L, 1); |
| 5692 | |
| 5693 | body = NULL; |
| 5694 | body_len = 0; |
| 5695 | ret = lua_getfield(L, -1, "body"); |
| 5696 | if (ret == LUA_TSTRING) |
| 5697 | body = lua_tolstring(L, -1, &body_len); |
| 5698 | lua_pop(L, 1); |
| 5699 | |
| 5700 | /* Prepare the response before inserting the headers */ |
| 5701 | h1m_init_res(&h1m); |
| 5702 | htx = htx_from_buf(&s->res.buf); |
| 5703 | channel_htx_truncate(&s->res, htx); |
| 5704 | if (s->txn->req.flags & HTTP_MSGF_VER_11) { |
| 5705 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11); |
| 5706 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), |
| 5707 | ist2(status, status_len), ist2(reason, reason_len)); |
| 5708 | } |
| 5709 | else { |
| 5710 | flags = HTX_SL_F_IS_RESP; |
| 5711 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), |
| 5712 | ist2(status, status_len), ist2(reason, reason_len)); |
| 5713 | } |
| 5714 | if (!sl) |
| 5715 | goto fail; |
| 5716 | sl->info.res.status = code; |
| 5717 | |
| 5718 | /* Push in the stack the "headers" entry. */ |
| 5719 | ret = lua_getfield(L, -1, "headers"); |
| 5720 | if (ret != LUA_TTABLE) |
| 5721 | goto skip_headers; |
| 5722 | |
| 5723 | lua_pushnil(L); |
| 5724 | while (lua_next(L, -2) != 0) { |
| 5725 | struct ist name, value; |
| 5726 | const char *n, *v; |
| 5727 | size_t nlen, vlen; |
| 5728 | |
| 5729 | if (!lua_isstring(L, -2) || !lua_istable(L, -1)) { |
| 5730 | /* Skip element if the key is not a string or if the value is not a table */ |
| 5731 | goto next_hdr; |
| 5732 | } |
| 5733 | |
| 5734 | n = lua_tolstring(L, -2, &nlen); |
| 5735 | name = ist2(n, nlen); |
| 5736 | if (isteqi(name, ist("content-length"))) { |
| 5737 | /* Always skip content-length header. It will be added |
| 5738 | * later with the correct len |
| 5739 | */ |
| 5740 | goto next_hdr; |
| 5741 | } |
| 5742 | |
| 5743 | /* Loop on header's values */ |
| 5744 | lua_pushnil(L); |
| 5745 | while (lua_next(L, -2)) { |
| 5746 | if (!lua_isstring(L, -1)) { |
| 5747 | /* Skip the value if it is not a string */ |
| 5748 | goto next_value; |
| 5749 | } |
| 5750 | |
| 5751 | v = lua_tolstring(L, -1, &vlen); |
| 5752 | value = ist2(v, vlen); |
| 5753 | |
| 5754 | if (isteqi(name, ist("transfer-encoding"))) |
| 5755 | h1_parse_xfer_enc_header(&h1m, value); |
| 5756 | if (!htx_add_header(htx, ist2(n, nlen), ist2(v, vlen))) |
| 5757 | goto fail; |
| 5758 | |
| 5759 | next_value: |
| 5760 | lua_pop(L, 1); |
| 5761 | } |
| 5762 | |
| 5763 | next_hdr: |
| 5764 | lua_pop(L, 1); |
| 5765 | } |
| 5766 | skip_headers: |
| 5767 | lua_pop(L, 1); |
| 5768 | |
| 5769 | /* Update h1m flags: CLEN is set if CHNK is not present */ |
| 5770 | if (!(h1m.flags & H1_MF_CHNK)) { |
| 5771 | const char *clen = ultoa(body_len); |
| 5772 | |
| 5773 | h1m.flags |= H1_MF_CLEN; |
| 5774 | if (!htx_add_header(htx, ist("content-length"), ist(clen))) |
| 5775 | goto fail; |
| 5776 | } |
| 5777 | if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK)) |
| 5778 | h1m.flags |= H1_MF_XFER_LEN; |
| 5779 | |
| 5780 | /* Update HTX start-line flags */ |
| 5781 | if (h1m.flags & H1_MF_XFER_ENC) |
| 5782 | flags |= HTX_SL_F_XFER_ENC; |
| 5783 | if (h1m.flags & H1_MF_XFER_LEN) { |
| 5784 | flags |= HTX_SL_F_XFER_LEN; |
| 5785 | if (h1m.flags & H1_MF_CHNK) |
| 5786 | flags |= HTX_SL_F_CHNK; |
| 5787 | else if (h1m.flags & H1_MF_CLEN) |
| 5788 | flags |= HTX_SL_F_CLEN; |
| 5789 | if (h1m.body_len == 0) |
| 5790 | flags |= HTX_SL_F_BODYLESS; |
| 5791 | } |
| 5792 | sl->flags |= flags; |
| 5793 | |
| 5794 | |
| 5795 | if (!htx_add_endof(htx, HTX_BLK_EOH) || |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 5796 | (body_len && !htx_add_data_atonce(htx, ist2(body, body_len)))) |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5797 | goto fail; |
| 5798 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 5799 | htx->flags |= HTX_FL_EOM; |
| 5800 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5801 | /* Now, forward the response and terminate the transaction */ |
| 5802 | s->txn->status = code; |
| 5803 | htx_to_buf(htx, &s->res.buf); |
| 5804 | if (!http_forward_proxy_resp(s, 1)) |
| 5805 | goto fail; |
| 5806 | |
| 5807 | return 1; |
| 5808 | |
| 5809 | fail: |
| 5810 | channel_htx_truncate(&s->res, htx); |
| 5811 | return 0; |
| 5812 | } |
| 5813 | |
| 5814 | /* Terminate a transaction if called from a lua action. For TCP streams, |
| 5815 | * processing is just aborted. Nothing is returned to the client and all |
| 5816 | * arguments are ignored. For HTTP streams, if a reply is passed as argument, it |
| 5817 | * is forwarded to the client before terminating the transaction. On success, |
| 5818 | * the function exits with ACT_RET_DONE code. If an error occurred, it exits |
| 5819 | * with ACT_RET_ERR code. If this function is not called from a lua action, it |
| 5820 | * just exits without any processing. |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5821 | */ |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 5822 | __LJMP static int hlua_txn_done(lua_State *L) |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5823 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 5824 | struct hlua_txn *htxn; |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5825 | struct stream *s; |
| 5826 | int finst; |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5827 | |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 5828 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5829 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5830 | /* If the flags NOTERM is set, we cannot terminate the session, so we |
| 5831 | * just end the execution of the current lua code. */ |
| 5832 | if (htxn->flags & HLUA_TXN_NOTERM) |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5833 | WILL_LJMP(hlua_done(L)); |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5834 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5835 | s = htxn->s; |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5836 | if (!IS_HTX_STRM(htxn->s)) { |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5837 | struct channel *req = &s->req; |
| 5838 | struct channel *res = &s->res; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 5839 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5840 | channel_auto_read(req); |
| 5841 | channel_abort(req); |
| 5842 | channel_auto_close(req); |
| 5843 | channel_erase(req); |
| 5844 | |
| 5845 | res->wex = tick_add_ifset(now_ms, res->wto); |
| 5846 | channel_auto_read(res); |
| 5847 | channel_auto_close(res); |
| 5848 | channel_shutr_now(res); |
| 5849 | |
| 5850 | finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_D); |
| 5851 | goto done; |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 5852 | } |
Thierry FOURNIER | 10ec214 | 2015-08-24 17:23:45 +0200 | [diff] [blame] | 5853 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5854 | if (lua_gettop(L) == 1 || !lua_istable(L, 2)) { |
| 5855 | /* No reply or invalid reply */ |
| 5856 | s->txn->status = 0; |
| 5857 | http_reply_and_close(s, 0, NULL); |
| 5858 | } |
| 5859 | else { |
| 5860 | /* Remove extra args to have the reply on top of the stack */ |
| 5861 | if (lua_gettop(L) > 2) |
| 5862 | lua_pop(L, lua_gettop(L) - 2); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5863 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5864 | if (!hlua_txn_forward_reply(L, s)) { |
| 5865 | if (!(s->flags & SF_ERR_MASK)) |
| 5866 | s->flags |= SF_ERR_PRXCOND; |
| 5867 | lua_pushinteger(L, ACT_RET_ERR); |
| 5868 | WILL_LJMP(hlua_done(L)); |
| 5869 | return 0; /* Never reached */ |
| 5870 | } |
Christopher Faulet | 4d0e263 | 2019-07-16 10:52:40 +0200 | [diff] [blame] | 5871 | } |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 5872 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5873 | finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_H); |
| 5874 | if (htxn->dir == SMP_OPT_DIR_REQ) { |
| 5875 | /* let's log the request time */ |
| 5876 | s->logs.tv_request = now; |
| 5877 | 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] | 5878 | _HA_ATOMIC_INC(&s->sess->fe->fe_counters.intercepted_req); |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5879 | } |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 5880 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5881 | done: |
| 5882 | if (!(s->flags & SF_ERR_MASK)) |
| 5883 | s->flags |= SF_ERR_LOCAL; |
| 5884 | if (!(s->flags & SF_FINST_MASK)) |
| 5885 | s->flags |= finst; |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 5886 | |
Christopher Faulet | 4ad7310 | 2020-03-05 11:07:31 +0100 | [diff] [blame] | 5887 | lua_pushinteger(L, ACT_RET_ABRT); |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 5888 | WILL_LJMP(hlua_done(L)); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 5889 | return 0; |
| 5890 | } |
| 5891 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5892 | /* |
| 5893 | * |
| 5894 | * |
| 5895 | * Class REPLY |
| 5896 | * |
| 5897 | * |
| 5898 | */ |
| 5899 | |
| 5900 | /* Pushes the TXN reply onto the top of the stack. If the stask does not have a |
| 5901 | * free slots, the function fails and returns 0; |
| 5902 | */ |
| 5903 | static int hlua_txn_reply_new(lua_State *L) |
| 5904 | { |
| 5905 | struct hlua_txn *htxn; |
| 5906 | const char *reason, *body = NULL; |
| 5907 | int ret, status; |
| 5908 | |
| 5909 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5910 | if (!IS_HTX_STRM(htxn->s)) { |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5911 | hlua_pusherror(L, "txn object is not an HTTP transaction."); |
| 5912 | WILL_LJMP(lua_error(L)); |
| 5913 | } |
| 5914 | |
| 5915 | /* Default value */ |
| 5916 | status = 200; |
| 5917 | reason = http_get_reason(status); |
| 5918 | |
| 5919 | if (lua_istable(L, 2)) { |
| 5920 | /* load status and reason from the table argument at index 2 */ |
| 5921 | ret = lua_getfield(L, 2, "status"); |
| 5922 | if (ret == LUA_TNIL) |
| 5923 | goto reason; |
| 5924 | else if (ret != LUA_TNUMBER) { |
| 5925 | /* invalid status: ignore the reason */ |
| 5926 | goto body; |
| 5927 | } |
| 5928 | status = lua_tointeger(L, -1); |
| 5929 | |
| 5930 | reason: |
| 5931 | lua_pop(L, 1); /* restore the stack: remove status */ |
| 5932 | ret = lua_getfield(L, 2, "reason"); |
| 5933 | if (ret == LUA_TSTRING) |
| 5934 | reason = lua_tostring(L, -1); |
| 5935 | |
| 5936 | body: |
| 5937 | lua_pop(L, 1); /* restore the stack: remove invalid status or reason */ |
| 5938 | ret = lua_getfield(L, 2, "body"); |
| 5939 | if (ret == LUA_TSTRING) |
| 5940 | body = lua_tostring(L, -1); |
| 5941 | lua_pop(L, 1); /* restore the stack: remove body */ |
| 5942 | } |
| 5943 | |
| 5944 | /* Create the Reply table */ |
| 5945 | lua_newtable(L); |
| 5946 | |
| 5947 | /* Add status element */ |
| 5948 | lua_pushstring(L, "status"); |
| 5949 | lua_pushinteger(L, status); |
| 5950 | lua_settable(L, -3); |
| 5951 | |
| 5952 | /* Add reason element */ |
| 5953 | reason = http_get_reason(status); |
| 5954 | lua_pushstring(L, "reason"); |
| 5955 | lua_pushstring(L, reason); |
| 5956 | lua_settable(L, -3); |
| 5957 | |
| 5958 | /* Add body element, nil if undefined */ |
| 5959 | lua_pushstring(L, "body"); |
| 5960 | if (body) |
| 5961 | lua_pushstring(L, body); |
| 5962 | else |
| 5963 | lua_pushnil(L); |
| 5964 | lua_settable(L, -3); |
| 5965 | |
| 5966 | /* Add headers element */ |
| 5967 | lua_pushstring(L, "headers"); |
| 5968 | lua_newtable(L); |
| 5969 | |
| 5970 | /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */ |
| 5971 | if (lua_istable(L, 2)) { |
| 5972 | /* load headers from the table argument at index 2. If it is a table, copy it. */ |
| 5973 | ret = lua_getfield(L, 2, "headers"); |
| 5974 | if (ret == LUA_TTABLE) { |
| 5975 | /* stack: [ ... <headers:table>, <table> ] */ |
| 5976 | lua_pushnil(L); |
| 5977 | while (lua_next(L, -2) != 0) { |
| 5978 | /* stack: [ ... <headers:table>, <table>, k, v] */ |
| 5979 | if (!lua_isstring(L, -1) && !lua_istable(L, -1)) { |
| 5980 | /* invalid value type, skip it */ |
| 5981 | lua_pop(L, 1); |
| 5982 | continue; |
| 5983 | } |
| 5984 | |
| 5985 | |
| 5986 | /* Duplicate the key and swap it with the value. */ |
| 5987 | lua_pushvalue(L, -2); |
| 5988 | lua_insert(L, -2); |
| 5989 | /* stack: [ ... <headers:table>, <table>, k, k, v ] */ |
| 5990 | |
| 5991 | lua_newtable(L); |
| 5992 | lua_insert(L, -2); |
| 5993 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, v ] */ |
| 5994 | |
| 5995 | if (lua_isstring(L, -1)) { |
| 5996 | /* push the value in the inner table */ |
| 5997 | lua_rawseti(L, -2, 1); |
| 5998 | } |
| 5999 | else { /* table */ |
| 6000 | lua_pushnil(L); |
| 6001 | while (lua_next(L, -2) != 0) { |
| 6002 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2, v2 ] */ |
| 6003 | if (!lua_isstring(L, -1)) { |
| 6004 | /* invalid value type, skip it*/ |
| 6005 | lua_pop(L, 1); |
| 6006 | continue; |
| 6007 | } |
| 6008 | /* push the value in the inner table */ |
| 6009 | lua_rawseti(L, -4, lua_rawlen(L, -4) + 1); |
| 6010 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2 ] */ |
| 6011 | } |
| 6012 | lua_pop(L, 1); |
| 6013 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table> ] */ |
| 6014 | } |
| 6015 | |
| 6016 | /* push (k,v) on the stack in the headers table: |
| 6017 | * stack: [ ... <headers:table>, <table>, k, k, v ] |
| 6018 | */ |
| 6019 | lua_settable(L, -5); |
| 6020 | /* stack: [ ... <headers:table>, <table>, k ] */ |
| 6021 | } |
| 6022 | } |
| 6023 | lua_pop(L, 1); |
| 6024 | } |
| 6025 | /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */ |
| 6026 | lua_settable(L, -3); |
| 6027 | /* stack: [ txn, <Arg:table>, <Reply:table> ] */ |
| 6028 | |
| 6029 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 6030 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_reply_ref); |
| 6031 | lua_setmetatable(L, -2); |
| 6032 | return 1; |
| 6033 | } |
| 6034 | |
| 6035 | /* Set the reply status code, and optionally the reason. If no reason is |
| 6036 | * provided, the default one corresponding to the status code is used. |
| 6037 | */ |
| 6038 | __LJMP static int hlua_txn_reply_set_status(lua_State *L) |
| 6039 | { |
| 6040 | int status = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 6041 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
| 6042 | |
| 6043 | /* First argument (self) must be a table */ |
| 6044 | luaL_checktype(L, 1, LUA_TTABLE); |
| 6045 | |
| 6046 | if (status < 100 || status > 599) { |
| 6047 | lua_pushboolean(L, 0); |
| 6048 | return 1; |
| 6049 | } |
| 6050 | if (!reason) |
| 6051 | reason = http_get_reason(status); |
| 6052 | |
| 6053 | lua_pushinteger(L, status); |
| 6054 | lua_setfield(L, 1, "status"); |
| 6055 | |
| 6056 | lua_pushstring(L, reason); |
| 6057 | lua_setfield(L, 1, "reason"); |
| 6058 | |
| 6059 | lua_pushboolean(L, 1); |
| 6060 | return 1; |
| 6061 | } |
| 6062 | |
| 6063 | /* Add a header into the reply object. Each header name is associated to an |
| 6064 | * array of values in the "headers" table. If the header name is not found, a |
| 6065 | * new entry is created. |
| 6066 | */ |
| 6067 | __LJMP static int hlua_txn_reply_add_header(lua_State *L) |
| 6068 | { |
| 6069 | const char *name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 6070 | const char *value = MAY_LJMP(luaL_checkstring(L, 3)); |
| 6071 | int ret; |
| 6072 | |
| 6073 | /* First argument (self) must be a table */ |
| 6074 | luaL_checktype(L, 1, LUA_TTABLE); |
| 6075 | |
| 6076 | /* Push in the stack the "headers" entry. */ |
| 6077 | ret = lua_getfield(L, 1, "headers"); |
| 6078 | if (ret != LUA_TTABLE) { |
| 6079 | hlua_pusherror(L, "Reply['headers'] is expected to a an array. %s found", lua_typename(L, ret)); |
| 6080 | WILL_LJMP(lua_error(L)); |
| 6081 | } |
| 6082 | |
| 6083 | /* check if the header is already registered. If not, register it. */ |
| 6084 | ret = lua_getfield(L, -1, name); |
| 6085 | if (ret == LUA_TNIL) { |
| 6086 | /* Entry not found. */ |
| 6087 | lua_pop(L, 1); /* remove the nil. The "headers" table is the top of the stack. */ |
| 6088 | |
| 6089 | /* Insert the new header name in the array in the top of the stack. |
| 6090 | * It left the new array in the top of the stack. |
| 6091 | */ |
| 6092 | lua_newtable(L); |
| 6093 | lua_pushstring(L, name); |
| 6094 | lua_pushvalue(L, -2); |
| 6095 | lua_settable(L, -4); |
| 6096 | } |
| 6097 | else if (ret != LUA_TTABLE) { |
| 6098 | hlua_pusherror(L, "Reply['headers']['%s'] is expected to be an array. %s found", name, lua_typename(L, ret)); |
| 6099 | WILL_LJMP(lua_error(L)); |
| 6100 | } |
| 6101 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 6102 | /* Now the top of thestack is an array of values. We push |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 6103 | * the header value as new entry. |
| 6104 | */ |
| 6105 | lua_pushstring(L, value); |
| 6106 | ret = lua_rawlen(L, -2); |
| 6107 | lua_rawseti(L, -2, ret + 1); |
| 6108 | |
| 6109 | lua_pushboolean(L, 1); |
| 6110 | return 1; |
| 6111 | } |
| 6112 | |
| 6113 | /* Remove all occurrences of a given header name. */ |
| 6114 | __LJMP static int hlua_txn_reply_del_header(lua_State *L) |
| 6115 | { |
| 6116 | const char *name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 6117 | int ret; |
| 6118 | |
| 6119 | /* First argument (self) must be a table */ |
| 6120 | luaL_checktype(L, 1, LUA_TTABLE); |
| 6121 | |
| 6122 | /* Push in the stack the "headers" entry. */ |
| 6123 | ret = lua_getfield(L, 1, "headers"); |
| 6124 | if (ret != LUA_TTABLE) { |
| 6125 | hlua_pusherror(L, "Reply['headers'] is expected to be an array. %s found", lua_typename(L, ret)); |
| 6126 | WILL_LJMP(lua_error(L)); |
| 6127 | } |
| 6128 | |
| 6129 | lua_pushstring(L, name); |
| 6130 | lua_pushnil(L); |
| 6131 | lua_settable(L, -3); |
| 6132 | |
| 6133 | lua_pushboolean(L, 1); |
| 6134 | return 1; |
| 6135 | } |
| 6136 | |
| 6137 | /* Set the reply's body. Overwrite any existing entry. */ |
| 6138 | __LJMP static int hlua_txn_reply_set_body(lua_State *L) |
| 6139 | { |
| 6140 | const char *payload = MAY_LJMP(luaL_checkstring(L, 2)); |
| 6141 | |
| 6142 | /* First argument (self) must be a table */ |
| 6143 | luaL_checktype(L, 1, LUA_TTABLE); |
| 6144 | |
| 6145 | lua_pushstring(L, payload); |
| 6146 | lua_setfield(L, 1, "body"); |
| 6147 | |
| 6148 | lua_pushboolean(L, 1); |
| 6149 | return 1; |
| 6150 | } |
| 6151 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 6152 | __LJMP static int hlua_log(lua_State *L) |
| 6153 | { |
| 6154 | int level; |
| 6155 | const char *msg; |
| 6156 | |
| 6157 | MAY_LJMP(check_args(L, 2, "log")); |
| 6158 | level = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 6159 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 6160 | |
| 6161 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 6162 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 6163 | |
| 6164 | hlua_sendlog(NULL, level, msg); |
| 6165 | return 0; |
| 6166 | } |
| 6167 | |
| 6168 | __LJMP static int hlua_log_debug(lua_State *L) |
| 6169 | { |
| 6170 | const char *msg; |
| 6171 | |
| 6172 | MAY_LJMP(check_args(L, 1, "debug")); |
| 6173 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6174 | hlua_sendlog(NULL, LOG_DEBUG, msg); |
| 6175 | return 0; |
| 6176 | } |
| 6177 | |
| 6178 | __LJMP static int hlua_log_info(lua_State *L) |
| 6179 | { |
| 6180 | const char *msg; |
| 6181 | |
| 6182 | MAY_LJMP(check_args(L, 1, "info")); |
| 6183 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6184 | hlua_sendlog(NULL, LOG_INFO, msg); |
| 6185 | return 0; |
| 6186 | } |
| 6187 | |
| 6188 | __LJMP static int hlua_log_warning(lua_State *L) |
| 6189 | { |
| 6190 | const char *msg; |
| 6191 | |
| 6192 | MAY_LJMP(check_args(L, 1, "warning")); |
| 6193 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6194 | hlua_sendlog(NULL, LOG_WARNING, msg); |
| 6195 | return 0; |
| 6196 | } |
| 6197 | |
| 6198 | __LJMP static int hlua_log_alert(lua_State *L) |
| 6199 | { |
| 6200 | const char *msg; |
| 6201 | |
| 6202 | MAY_LJMP(check_args(L, 1, "alert")); |
| 6203 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6204 | hlua_sendlog(NULL, LOG_ALERT, msg); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 6205 | return 0; |
| 6206 | } |
| 6207 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 6208 | __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] | 6209 | { |
| 6210 | int wakeup_ms = lua_tointeger(L, -1); |
| 6211 | if (now_ms < wakeup_ms) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 6212 | 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] | 6213 | return 0; |
| 6214 | } |
| 6215 | |
| 6216 | __LJMP static int hlua_sleep(lua_State *L) |
| 6217 | { |
| 6218 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6219 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6220 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6221 | MAY_LJMP(check_args(L, 1, "sleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6222 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 6223 | delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6224 | wakeup_ms = tick_add(now_ms, delay); |
| 6225 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6226 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 6227 | 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] | 6228 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6229 | } |
| 6230 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6231 | __LJMP static int hlua_msleep(lua_State *L) |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6232 | { |
| 6233 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6234 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6235 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6236 | MAY_LJMP(check_args(L, 1, "msleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6237 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 6238 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6239 | wakeup_ms = tick_add(now_ms, delay); |
| 6240 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6241 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 6242 | 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] | 6243 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6244 | } |
| 6245 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 6246 | /* This functionis an LUA binding. it permits to give back |
| 6247 | * the hand at the HAProxy scheduler. It is used when the |
| 6248 | * LUA processing consumes a lot of time. |
| 6249 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 6250 | __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] | 6251 | { |
| 6252 | return 0; |
| 6253 | } |
| 6254 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 6255 | __LJMP static int hlua_yield(lua_State *L) |
| 6256 | { |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 6257 | 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] | 6258 | return 0; |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 6259 | } |
| 6260 | |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6261 | /* This function change the nice of the currently executed |
| 6262 | * task. It is used set low or high priority at the current |
| 6263 | * task. |
| 6264 | */ |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 6265 | __LJMP static int hlua_set_nice(lua_State *L) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6266 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6267 | struct hlua *hlua; |
| 6268 | int nice; |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6269 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6270 | MAY_LJMP(check_args(L, 1, "set_nice")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6271 | nice = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6272 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 6273 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 6274 | hlua = hlua_gethlua(L); |
| 6275 | |
| 6276 | /* If the task is not set, I'm in a start mode. */ |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6277 | if (!hlua || !hlua->task) |
| 6278 | return 0; |
| 6279 | |
| 6280 | if (nice < -1024) |
| 6281 | nice = -1024; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6282 | else if (nice > 1024) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6283 | nice = 1024; |
| 6284 | |
| 6285 | hlua->task->nice = nice; |
| 6286 | return 0; |
| 6287 | } |
| 6288 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6289 | /* 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] | 6290 | * HAProxy task subsystem when the task is awaked. The LUA runtime can |
| 6291 | * return an E_AGAIN signal, the emmiter of this signal must set a |
| 6292 | * signal to wake the task. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6293 | * |
| 6294 | * Task wrapper are longjmp safe because the only one Lua code |
| 6295 | * executed is the safe hlua_ctx_resume(); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6296 | */ |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 6297 | 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] | 6298 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 6299 | struct hlua *hlua = context; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6300 | enum hlua_exec status; |
| 6301 | |
Christopher Faulet | 5bc9972 | 2018-04-25 10:34:45 +0200 | [diff] [blame] | 6302 | if (task->thread_mask == MAX_THREADS_MASK) |
| 6303 | task_set_affinity(task, tid_bit); |
| 6304 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6305 | /* If it is the first call to the task, we must initialize the |
| 6306 | * execution timeouts. |
| 6307 | */ |
| 6308 | if (!HLUA_IS_RUNNING(hlua)) |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 6309 | hlua->max_time = hlua_timeout_task; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6310 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6311 | /* Execute the Lua code. */ |
| 6312 | status = hlua_ctx_resume(hlua, 1); |
| 6313 | |
| 6314 | switch (status) { |
| 6315 | /* finished or yield */ |
| 6316 | case HLUA_E_OK: |
| 6317 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 6318 | task_destroy(task); |
Tim Duesterhus | cd235c6 | 2018-04-24 13:56:01 +0200 | [diff] [blame] | 6319 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6320 | break; |
| 6321 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6322 | case HLUA_E_AGAIN: /* co process or timeout wake me later. */ |
Thierry FOURNIER | cb14688 | 2017-12-10 17:10:57 +0100 | [diff] [blame] | 6323 | notification_gc(&hlua->com); |
PiBa-NL | fe971b3 | 2018-05-02 22:27:14 +0200 | [diff] [blame] | 6324 | task->expire = hlua->wake_time; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6325 | break; |
| 6326 | |
| 6327 | /* finished with error. */ |
| 6328 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6329 | SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1)); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6330 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 6331 | task_destroy(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 6332 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6333 | break; |
| 6334 | |
| 6335 | case HLUA_E_ERR: |
| 6336 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6337 | SEND_ERR(NULL, "Lua task: unknown error.\n"); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6338 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 6339 | task_destroy(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 6340 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6341 | break; |
| 6342 | } |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 6343 | return task; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6344 | } |
| 6345 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6346 | /* This function is an LUA binding that register LUA function to be |
| 6347 | * executed after the HAProxy configuration parsing and before the |
| 6348 | * HAProxy scheduler starts. This function expect only one LUA |
| 6349 | * argument that is a function. This function returns nothing, but |
| 6350 | * throws if an error is encountered. |
| 6351 | */ |
| 6352 | __LJMP static int hlua_register_init(lua_State *L) |
| 6353 | { |
| 6354 | struct hlua_init_function *init; |
| 6355 | int ref; |
| 6356 | |
| 6357 | MAY_LJMP(check_args(L, 1, "register_init")); |
| 6358 | |
| 6359 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 6360 | |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6361 | init = calloc(1, sizeof(*init)); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6362 | if (!init) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6363 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6364 | |
| 6365 | init->function_ref = ref; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 6366 | LIST_APPEND(&hlua_init_functions[hlua_state_id], &init->l); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6367 | return 0; |
| 6368 | } |
| 6369 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6370 | /* This functio is an LUA binding. It permits to register a task |
| 6371 | * executed in parallel of the main HAroxy activity. The task is |
| 6372 | * created and it is set in the HAProxy scheduler. It can be called |
| 6373 | * from the "init" section, "post init" or during the runtime. |
| 6374 | * |
| 6375 | * Lua prototype: |
| 6376 | * |
| 6377 | * <none> core.register_task(<function>) |
| 6378 | */ |
| 6379 | static int hlua_register_task(lua_State *L) |
| 6380 | { |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 6381 | struct hlua *hlua = NULL; |
| 6382 | struct task *task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6383 | int ref; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 6384 | int state_id; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6385 | |
| 6386 | MAY_LJMP(check_args(L, 1, "register_task")); |
| 6387 | |
| 6388 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 6389 | |
Thierry Fournier | 75fc029 | 2020-11-28 13:18:56 +0100 | [diff] [blame] | 6390 | /* Get the reference state. If the reference is NULL, L is the master |
| 6391 | * state, otherwise hlua->T is. |
| 6392 | */ |
| 6393 | hlua = hlua_gethlua(L); |
| 6394 | if (hlua) |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 6395 | /* we are in runtime processing */ |
| 6396 | state_id = hlua->state_id; |
Thierry Fournier | 75fc029 | 2020-11-28 13:18:56 +0100 | [diff] [blame] | 6397 | else |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 6398 | /* we are in initialization mode */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 6399 | state_id = hlua_state_id; |
Thierry Fournier | 75fc029 | 2020-11-28 13:18:56 +0100 | [diff] [blame] | 6400 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6401 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6402 | if (!hlua) |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 6403 | goto alloc_error; |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 6404 | HLUA_INIT(hlua); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6405 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 6406 | /* We are in the common lua state, execute the task anywhere, |
| 6407 | * otherwise, inherit the current thread identifier |
| 6408 | */ |
| 6409 | if (state_id == 0) |
| 6410 | task = task_new(MAX_THREADS_MASK); |
| 6411 | else |
| 6412 | task = task_new(tid_bit); |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 6413 | if (!task) |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 6414 | goto alloc_error; |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 6415 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6416 | task->context = hlua; |
| 6417 | task->process = hlua_process_task; |
| 6418 | |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 6419 | if (!hlua_ctx_init(hlua, state_id, task, 1)) |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 6420 | goto alloc_error; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6421 | |
| 6422 | /* Restore the function in the stack. */ |
| 6423 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref); |
| 6424 | hlua->nargs = 0; |
| 6425 | |
| 6426 | /* Schedule task. */ |
| 6427 | task_schedule(task, now_ms); |
| 6428 | |
| 6429 | return 0; |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 6430 | |
| 6431 | alloc_error: |
| 6432 | task_destroy(task); |
| 6433 | hlua_ctx_destroy(hlua); |
| 6434 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 6435 | return 0; /* Never reached */ |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6436 | } |
| 6437 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6438 | /* Wrapper called by HAProxy to execute an LUA converter. This wrapper |
| 6439 | * doesn't allow "yield" functions because the HAProxy engine cannot |
| 6440 | * resume converters. |
| 6441 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6442 | 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] | 6443 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 6444 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6445 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6446 | const char *error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6447 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6448 | if (!stream) |
| 6449 | return 0; |
| 6450 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6451 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6452 | * Lua context can be not initialized. This behavior |
| 6453 | * permits to save performances because a systematic |
| 6454 | * Lua initialization cause 5% performances loss. |
| 6455 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6456 | if (!stream->hlua) { |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 6457 | struct hlua *hlua; |
| 6458 | |
| 6459 | hlua = pool_alloc(pool_head_hlua); |
| 6460 | if (!hlua) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6461 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 6462 | return 0; |
| 6463 | } |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 6464 | HLUA_INIT(hlua); |
| 6465 | stream->hlua = hlua; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 6466 | 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] | 6467 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 6468 | return 0; |
| 6469 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6470 | } |
| 6471 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6472 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6473 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6474 | |
| 6475 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 6476 | if (!SET_SAFE_LJMP(stream->hlua)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6477 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 6478 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6479 | else |
| 6480 | error = "critical error"; |
| 6481 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6482 | return 0; |
| 6483 | } |
| 6484 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6485 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6486 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6487 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 6488 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6489 | return 0; |
| 6490 | } |
| 6491 | |
| 6492 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 6493 | 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] | 6494 | |
| 6495 | /* convert input sample and pust-it in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6496 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6497 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 6498 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6499 | return 0; |
| 6500 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6501 | hlua_smp2lua(stream->hlua->T, smp); |
| 6502 | stream->hlua->nargs = 1; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6503 | |
| 6504 | /* push keywords in the stack. */ |
| 6505 | if (arg_p) { |
| 6506 | for (; arg_p->type != ARGT_STOP; arg_p++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6507 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6508 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 6509 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6510 | return 0; |
| 6511 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6512 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 6513 | stream->hlua->nargs++; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6514 | } |
| 6515 | } |
| 6516 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6517 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6518 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6519 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6520 | /* At this point the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 6521 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6522 | } |
| 6523 | |
| 6524 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6525 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6526 | /* finished. */ |
| 6527 | case HLUA_E_OK: |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 6528 | /* If the stack is empty, the function fails. */ |
| 6529 | if (lua_gettop(stream->hlua->T) <= 0) |
| 6530 | return 0; |
| 6531 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6532 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6533 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 6534 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6535 | return 1; |
| 6536 | |
| 6537 | /* yield. */ |
| 6538 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6539 | 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] | 6540 | return 0; |
| 6541 | |
| 6542 | /* finished with error. */ |
| 6543 | case HLUA_E_ERRMSG: |
| 6544 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6545 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6546 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 6547 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6548 | return 0; |
| 6549 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6550 | case HLUA_E_ETMOUT: |
| 6551 | SEND_ERR(stream->be, "Lua converter '%s': execution timeout.\n", fcn->name); |
| 6552 | return 0; |
| 6553 | |
| 6554 | case HLUA_E_NOMEM: |
| 6555 | SEND_ERR(stream->be, "Lua converter '%s': out of memory error.\n", fcn->name); |
| 6556 | return 0; |
| 6557 | |
| 6558 | case HLUA_E_YIELD: |
| 6559 | SEND_ERR(stream->be, "Lua converter '%s': yield functions like core.tcp() or core.sleep() are not allowed.\n", fcn->name); |
| 6560 | return 0; |
| 6561 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6562 | case HLUA_E_ERR: |
| 6563 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6564 | 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] | 6565 | /* fall through */ |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6566 | |
| 6567 | default: |
| 6568 | return 0; |
| 6569 | } |
| 6570 | } |
| 6571 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6572 | /* Wrapper called by HAProxy to execute a sample-fetch. this wrapper |
| 6573 | * doesn't allow "yield" functions because the HAProxy engine cannot |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6574 | * resume sample-fetches. This function will be called by the sample |
| 6575 | * fetch engine to call lua-based fetch operations. |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6576 | */ |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6577 | static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp, |
| 6578 | const char *kw, void *private) |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6579 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 6580 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6581 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6582 | const char *error; |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6583 | unsigned int hflags = HLUA_TXN_NOTERM; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6584 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6585 | if (!stream) |
| 6586 | return 0; |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 6587 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6588 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6589 | * Lua context can be not initialized. This behavior |
| 6590 | * permits to save performances because a systematic |
| 6591 | * Lua initialization cause 5% performances loss. |
| 6592 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6593 | if (!stream->hlua) { |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 6594 | struct hlua *hlua; |
| 6595 | |
| 6596 | hlua = pool_alloc(pool_head_hlua); |
| 6597 | if (!hlua) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6598 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 6599 | return 0; |
| 6600 | } |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 6601 | hlua->T = NULL; |
| 6602 | stream->hlua = hlua; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 6603 | 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] | 6604 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 6605 | return 0; |
| 6606 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6607 | } |
| 6608 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6609 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6610 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6611 | |
| 6612 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 6613 | if (!SET_SAFE_LJMP(stream->hlua)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6614 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 6615 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6616 | else |
| 6617 | error = "critical error"; |
| 6618 | 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] | 6619 | return 0; |
| 6620 | } |
| 6621 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6622 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6623 | if (!lua_checkstack(stream->hlua->T, 2)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6624 | 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] | 6625 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6626 | return 0; |
| 6627 | } |
| 6628 | |
| 6629 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 6630 | 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] | 6631 | |
| 6632 | /* push arguments in the stack. */ |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6633 | 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] | 6634 | 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] | 6635 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6636 | return 0; |
| 6637 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6638 | stream->hlua->nargs = 1; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6639 | |
| 6640 | /* push keywords in the stack. */ |
| 6641 | for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) { |
| 6642 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6643 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6644 | 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] | 6645 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6646 | return 0; |
| 6647 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6648 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 6649 | stream->hlua->nargs++; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6650 | } |
| 6651 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6652 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6653 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6654 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6655 | /* At this point the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 6656 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6657 | } |
| 6658 | |
| 6659 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6660 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6661 | /* finished. */ |
| 6662 | case HLUA_E_OK: |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 6663 | /* If the stack is empty, the function fails. */ |
| 6664 | if (lua_gettop(stream->hlua->T) <= 0) |
| 6665 | return 0; |
| 6666 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6667 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6668 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 6669 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6670 | |
| 6671 | /* Set the end of execution flag. */ |
| 6672 | smp->flags &= ~SMP_F_MAY_CHANGE; |
| 6673 | return 1; |
| 6674 | |
| 6675 | /* yield. */ |
| 6676 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6677 | 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] | 6678 | return 0; |
| 6679 | |
| 6680 | /* finished with error. */ |
| 6681 | case HLUA_E_ERRMSG: |
| 6682 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6683 | SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6684 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 6685 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6686 | return 0; |
| 6687 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6688 | case HLUA_E_ETMOUT: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6689 | SEND_ERR(smp->px, "Lua sample-fetch '%s': execution timeout.\n", fcn->name); |
| 6690 | return 0; |
| 6691 | |
| 6692 | case HLUA_E_NOMEM: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6693 | SEND_ERR(smp->px, "Lua sample-fetch '%s': out of memory error.\n", fcn->name); |
| 6694 | return 0; |
| 6695 | |
| 6696 | case HLUA_E_YIELD: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6697 | SEND_ERR(smp->px, "Lua sample-fetch '%s': yield not allowed.\n", fcn->name); |
| 6698 | return 0; |
| 6699 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6700 | case HLUA_E_ERR: |
| 6701 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6702 | 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] | 6703 | /* fall through */ |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6704 | |
| 6705 | default: |
| 6706 | return 0; |
| 6707 | } |
| 6708 | } |
| 6709 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6710 | /* This function is an LUA binding used for registering |
| 6711 | * "sample-conv" functions. It expects a converter name used |
| 6712 | * in the haproxy configuration file, and an LUA function. |
| 6713 | */ |
| 6714 | __LJMP static int hlua_register_converters(lua_State *L) |
| 6715 | { |
| 6716 | struct sample_conv_kw_list *sck; |
| 6717 | const char *name; |
| 6718 | int ref; |
| 6719 | int len; |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 6720 | struct hlua_function *fcn = NULL; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 6721 | struct sample_conv *sc; |
| 6722 | struct buffer *trash; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6723 | |
| 6724 | MAY_LJMP(check_args(L, 2, "register_converters")); |
| 6725 | |
| 6726 | /* First argument : converter name. */ |
| 6727 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6728 | |
| 6729 | /* Second argument : lua function. */ |
| 6730 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 6731 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 6732 | /* Check if the converter is already registered */ |
| 6733 | trash = get_trash_chunk(); |
| 6734 | chunk_printf(trash, "lua.%s", name); |
| 6735 | sc = find_sample_conv(trash->area, trash->data); |
| 6736 | if (sc != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 6737 | fcn = sc->private; |
| 6738 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 6739 | ha_warning("Trying to register converter 'lua.%s' more than once. " |
| 6740 | "This will become a hard error in version 2.5.\n", name); |
| 6741 | } |
| 6742 | fcn->function_ref[hlua_state_id] = ref; |
| 6743 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 6744 | } |
| 6745 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6746 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6747 | sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6748 | if (!sck) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 6749 | goto alloc_error; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 6750 | fcn = new_hlua_function(); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6751 | if (!fcn) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 6752 | goto alloc_error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6753 | |
| 6754 | /* Fill fcn. */ |
| 6755 | fcn->name = strdup(name); |
| 6756 | if (!fcn->name) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 6757 | goto alloc_error; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 6758 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6759 | |
| 6760 | /* List head */ |
| 6761 | sck->list.n = sck->list.p = NULL; |
| 6762 | |
| 6763 | /* converter keyword. */ |
| 6764 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6765 | sck->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6766 | if (!sck->kw[0].kw) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 6767 | goto alloc_error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6768 | |
| 6769 | snprintf((char *)sck->kw[0].kw, len, "lua.%s", name); |
| 6770 | sck->kw[0].process = hlua_sample_conv_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 6771 | 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] | 6772 | sck->kw[0].val_args = NULL; |
| 6773 | sck->kw[0].in_type = SMP_T_STR; |
| 6774 | sck->kw[0].out_type = SMP_T_STR; |
| 6775 | sck->kw[0].private = fcn; |
| 6776 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6777 | /* Register this new converter */ |
| 6778 | sample_register_convs(sck); |
| 6779 | |
| 6780 | return 0; |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 6781 | |
| 6782 | alloc_error: |
| 6783 | release_hlua_function(fcn); |
| 6784 | ha_free(&sck); |
| 6785 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 6786 | return 0; /* Never reached */ |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6787 | } |
| 6788 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6789 | /* This function is an LUA binding used for registering |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6790 | * "sample-fetch" functions. It expects a converter name used |
| 6791 | * in the haproxy configuration file, and an LUA function. |
| 6792 | */ |
| 6793 | __LJMP static int hlua_register_fetches(lua_State *L) |
| 6794 | { |
| 6795 | const char *name; |
| 6796 | int ref; |
| 6797 | int len; |
| 6798 | struct sample_fetch_kw_list *sfk; |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 6799 | struct hlua_function *fcn = NULL; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 6800 | struct sample_fetch *sf; |
| 6801 | struct buffer *trash; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6802 | |
| 6803 | MAY_LJMP(check_args(L, 2, "register_fetches")); |
| 6804 | |
| 6805 | /* First argument : sample-fetch name. */ |
| 6806 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6807 | |
| 6808 | /* Second argument : lua function. */ |
| 6809 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 6810 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 6811 | /* Check if the sample-fetch is already registered */ |
| 6812 | trash = get_trash_chunk(); |
| 6813 | chunk_printf(trash, "lua.%s", name); |
| 6814 | sf = find_sample_fetch(trash->area, trash->data); |
| 6815 | if (sf != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 6816 | fcn = sf->private; |
| 6817 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 6818 | ha_warning("Trying to register sample-fetch 'lua.%s' more than once. " |
| 6819 | "This will become a hard error in version 2.5.\n", name); |
| 6820 | } |
| 6821 | fcn->function_ref[hlua_state_id] = ref; |
| 6822 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 6823 | } |
| 6824 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6825 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6826 | sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6827 | if (!sfk) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 6828 | goto alloc_error; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 6829 | fcn = new_hlua_function(); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6830 | if (!fcn) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 6831 | goto alloc_error; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6832 | |
| 6833 | /* Fill fcn. */ |
| 6834 | fcn->name = strdup(name); |
| 6835 | if (!fcn->name) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 6836 | goto alloc_error; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 6837 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6838 | |
| 6839 | /* List head */ |
| 6840 | sfk->list.n = sfk->list.p = NULL; |
| 6841 | |
| 6842 | /* sample-fetch keyword. */ |
| 6843 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6844 | sfk->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6845 | if (!sfk->kw[0].kw) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 6846 | goto alloc_error; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6847 | |
| 6848 | snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name); |
| 6849 | sfk->kw[0].process = hlua_sample_fetch_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 6850 | 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] | 6851 | sfk->kw[0].val_args = NULL; |
| 6852 | sfk->kw[0].out_type = SMP_T_STR; |
| 6853 | sfk->kw[0].use = SMP_USE_HTTP_ANY; |
| 6854 | sfk->kw[0].val = 0; |
| 6855 | sfk->kw[0].private = fcn; |
| 6856 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6857 | /* Register this new fetch. */ |
| 6858 | sample_register_fetches(sfk); |
| 6859 | |
| 6860 | return 0; |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 6861 | |
| 6862 | alloc_error: |
| 6863 | release_hlua_function(fcn); |
| 6864 | ha_free(&sfk); |
| 6865 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 6866 | return 0; /* Never reached */ |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6867 | } |
| 6868 | |
Christopher Faulet | 501465d | 2020-02-26 14:54:16 +0100 | [diff] [blame] | 6869 | /* This function is a lua binding to set the wake_time. |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 6870 | */ |
Christopher Faulet | 501465d | 2020-02-26 14:54:16 +0100 | [diff] [blame] | 6871 | __LJMP static int hlua_set_wake_time(lua_State *L) |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 6872 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 6873 | struct hlua *hlua; |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 6874 | unsigned int delay; |
| 6875 | unsigned int wakeup_ms; |
| 6876 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 6877 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 6878 | hlua = hlua_gethlua(L); |
| 6879 | if (!hlua) { |
| 6880 | return 0; |
| 6881 | } |
| 6882 | |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 6883 | MAY_LJMP(check_args(L, 1, "wake_time")); |
| 6884 | |
| 6885 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 6886 | wakeup_ms = tick_add(now_ms, delay); |
| 6887 | hlua->wake_time = wakeup_ms; |
| 6888 | return 0; |
| 6889 | } |
| 6890 | |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6891 | /* This function is a wrapper to execute each LUA function declared as an action |
| 6892 | * wrapper during the initialisation period. This function may return any |
| 6893 | * ACT_RET_* value. On error ACT_RET_CONT is returned and the action is |
| 6894 | * ignored. If the lua action yields, ACT_RET_YIELD is returned. On success, the |
| 6895 | * return value is the first element on the stack. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6896 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6897 | 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] | 6898 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6899 | { |
| 6900 | char **arg; |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6901 | unsigned int hflags = 0; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6902 | int dir, act_ret = ACT_RET_CONT; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6903 | const char *error; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6904 | |
| 6905 | switch (rule->from) { |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 6906 | case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break; |
| 6907 | case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break; |
| 6908 | case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break; |
| 6909 | case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6910 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6911 | SEND_ERR(px, "Lua: internal error while execute action.\n"); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6912 | goto end; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6913 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6914 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6915 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6916 | * Lua context can be not initialized. This behavior |
| 6917 | * permits to save performances because a systematic |
| 6918 | * Lua initialization cause 5% performances loss. |
| 6919 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6920 | if (!s->hlua) { |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 6921 | struct hlua *hlua; |
| 6922 | |
| 6923 | hlua = pool_alloc(pool_head_hlua); |
| 6924 | if (!hlua) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6925 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 6926 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6927 | goto end; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6928 | } |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 6929 | HLUA_INIT(hlua); |
| 6930 | s->hlua = hlua; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 6931 | 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] | 6932 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 6933 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6934 | goto end; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6935 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6936 | } |
| 6937 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6938 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6939 | if (!HLUA_IS_RUNNING(s->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6940 | |
| 6941 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 6942 | if (!SET_SAFE_LJMP(s->hlua)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6943 | if (lua_type(s->hlua->T, -1) == LUA_TSTRING) |
| 6944 | error = lua_tostring(s->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6945 | else |
| 6946 | error = "critical error"; |
| 6947 | SEND_ERR(px, "Lua function '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 6948 | rule->arg.hlua_rule->fcn->name, error); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6949 | goto end; |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6950 | } |
| 6951 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6952 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6953 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6954 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 6955 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 6956 | RESET_SAFE_LJMP(s->hlua); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6957 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6958 | } |
| 6959 | |
| 6960 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 6961 | 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] | 6962 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6963 | /* Create and and push object stream in the stack. */ |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6964 | if (!hlua_txn_new(s->hlua->T, s, px, dir, hflags)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6965 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 6966 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 6967 | RESET_SAFE_LJMP(s->hlua); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6968 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6969 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6970 | s->hlua->nargs = 1; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6971 | |
| 6972 | /* push keywords in the stack. */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6973 | for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6974 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6975 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 6976 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 6977 | RESET_SAFE_LJMP(s->hlua); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6978 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6979 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6980 | lua_pushstring(s->hlua->T, *arg); |
| 6981 | s->hlua->nargs++; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6982 | } |
| 6983 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6984 | /* Now the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 6985 | RESET_SAFE_LJMP(s->hlua); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6986 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6987 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6988 | s->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6989 | } |
| 6990 | |
| 6991 | /* Execute the function. */ |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 6992 | switch (hlua_ctx_resume(s->hlua, !(flags & ACT_OPT_FINAL))) { |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6993 | /* finished. */ |
| 6994 | case HLUA_E_OK: |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6995 | /* Catch the return value */ |
| 6996 | if (lua_gettop(s->hlua->T) > 0) |
| 6997 | act_ret = lua_tointeger(s->hlua->T, -1); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6998 | |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 6999 | /* Set timeout in the required channel. */ |
Christopher Faulet | 498c483 | 2020-07-28 11:59:58 +0200 | [diff] [blame] | 7000 | if (act_ret == ACT_RET_YIELD) { |
| 7001 | if (flags & ACT_OPT_FINAL) |
| 7002 | goto err_yield; |
| 7003 | |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 7004 | if (dir == SMP_OPT_DIR_REQ) |
| 7005 | s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp), |
| 7006 | s->hlua->wake_time); |
| 7007 | else |
| 7008 | s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp), |
| 7009 | s->hlua->wake_time); |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 7010 | } |
| 7011 | goto end; |
| 7012 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7013 | /* yield. */ |
| 7014 | case HLUA_E_AGAIN: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 7015 | /* Set timeout in the required channel. */ |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 7016 | if (dir == SMP_OPT_DIR_REQ) |
| 7017 | s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp), |
| 7018 | s->hlua->wake_time); |
| 7019 | else |
| 7020 | s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp), |
| 7021 | s->hlua->wake_time); |
| 7022 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7023 | /* Some actions can be wake up when a "write" event |
| 7024 | * is detected on a response channel. This is useful |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7025 | * only for actions targeted on the requests. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7026 | */ |
Christopher Faulet | 51fa358 | 2019-07-26 14:54:52 +0200 | [diff] [blame] | 7027 | if (HLUA_IS_WAKERESWR(s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 7028 | s->res.flags |= CF_WAKE_WRITE; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 7029 | if (HLUA_IS_WAKEREQWR(s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 7030 | s->req.flags |= CF_WAKE_WRITE; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7031 | act_ret = ACT_RET_YIELD; |
| 7032 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7033 | |
| 7034 | /* finished with error. */ |
| 7035 | case HLUA_E_ERRMSG: |
| 7036 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 7037 | SEND_ERR(px, "Lua function '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7038 | rule->arg.hlua_rule->fcn->name, lua_tostring(s->hlua->T, -1)); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 7039 | lua_pop(s->hlua->T, 1); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7040 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7041 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7042 | case HLUA_E_ETMOUT: |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7043 | 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] | 7044 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7045 | |
| 7046 | case HLUA_E_NOMEM: |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7047 | 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] | 7048 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7049 | |
| 7050 | case HLUA_E_YIELD: |
Christopher Faulet | 498c483 | 2020-07-28 11:59:58 +0200 | [diff] [blame] | 7051 | err_yield: |
| 7052 | act_ret = ACT_RET_CONT; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7053 | 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] | 7054 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7055 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7056 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7057 | case HLUA_E_ERR: |
| 7058 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 7059 | SEND_ERR(px, "Lua function '%s' return an unknown error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7060 | rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7061 | |
| 7062 | default: |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7063 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7064 | } |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7065 | |
| 7066 | end: |
Christopher Faulet | 2361fd9 | 2020-07-30 10:40:58 +0200 | [diff] [blame] | 7067 | if (act_ret != ACT_RET_YIELD && s->hlua) |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 7068 | s->hlua->wake_time = TICK_ETERNITY; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7069 | return act_ret; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7070 | } |
| 7071 | |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 7072 | 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] | 7073 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 7074 | struct appctx *ctx = context; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7075 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7076 | appctx_wakeup(ctx); |
Willy Tarreau | d958741 | 2017-08-23 16:07:33 +0200 | [diff] [blame] | 7077 | t->expire = TICK_ETERNITY; |
Willy Tarreau | d1aa41f | 2017-07-21 16:41:56 +0200 | [diff] [blame] | 7078 | return t; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7079 | } |
| 7080 | |
| 7081 | static int hlua_applet_tcp_init(struct appctx *ctx, struct proxy *px, struct stream *strm) |
| 7082 | { |
| 7083 | struct stream_interface *si = ctx->owner; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7084 | struct hlua *hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7085 | struct task *task; |
| 7086 | char **arg; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7087 | const char *error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7088 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7089 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7090 | if (!hlua) { |
| 7091 | SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7092 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7093 | return 0; |
| 7094 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7095 | HLUA_INIT(hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7096 | ctx->ctx.hlua_apptcp.hlua = hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7097 | ctx->ctx.hlua_apptcp.flags = 0; |
| 7098 | |
| 7099 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 7100 | task = task_new(tid_bit); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7101 | if (!task) { |
| 7102 | SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7103 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7104 | return 0; |
| 7105 | } |
| 7106 | task->nice = 0; |
| 7107 | task->context = ctx; |
| 7108 | task->process = hlua_applet_wakeup; |
| 7109 | ctx->ctx.hlua_apptcp.task = task; |
| 7110 | |
| 7111 | /* In the execution wrappers linked with a stream, the |
| 7112 | * Lua context can be not initialized. This behavior |
| 7113 | * permits to save performances because a systematic |
| 7114 | * Lua initialization cause 5% performances loss. |
| 7115 | */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 7116 | 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] | 7117 | 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] | 7118 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7119 | return 0; |
| 7120 | } |
| 7121 | |
| 7122 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 7123 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7124 | |
| 7125 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7126 | if (!SET_SAFE_LJMP(hlua)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7127 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 7128 | error = lua_tostring(hlua->T, -1); |
| 7129 | else |
| 7130 | error = "critical error"; |
| 7131 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7132 | ctx->rule->arg.hlua_rule->fcn->name, error); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7133 | return 0; |
| 7134 | } |
| 7135 | |
| 7136 | /* Check stack available size. */ |
| 7137 | if (!lua_checkstack(hlua->T, 1)) { |
| 7138 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7139 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7140 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7141 | return 0; |
| 7142 | } |
| 7143 | |
| 7144 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 7145 | 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] | 7146 | |
| 7147 | /* Create and and push object stream in the stack. */ |
| 7148 | if (!hlua_applet_tcp_new(hlua->T, ctx)) { |
| 7149 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7150 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7151 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7152 | return 0; |
| 7153 | } |
| 7154 | hlua->nargs = 1; |
| 7155 | |
| 7156 | /* push keywords in the stack. */ |
| 7157 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 7158 | if (!lua_checkstack(hlua->T, 1)) { |
| 7159 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7160 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7161 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7162 | return 0; |
| 7163 | } |
| 7164 | lua_pushstring(hlua->T, *arg); |
| 7165 | hlua->nargs++; |
| 7166 | } |
| 7167 | |
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 | |
| 7170 | /* Wakeup the applet ASAP. */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 7171 | si_cant_get(si); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 7172 | si_rx_endp_more(si); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7173 | |
| 7174 | return 1; |
| 7175 | } |
| 7176 | |
Willy Tarreau | 60409db | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 7177 | void hlua_applet_tcp_fct(struct appctx *ctx) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7178 | { |
| 7179 | struct stream_interface *si = ctx->owner; |
| 7180 | struct stream *strm = si_strm(si); |
| 7181 | struct channel *res = si_ic(si); |
| 7182 | struct act_rule *rule = ctx->rule; |
| 7183 | struct proxy *px = strm->be; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7184 | struct hlua *hlua = ctx->ctx.hlua_apptcp.hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7185 | |
| 7186 | /* The applet execution is already done. */ |
Olivier Houchard | 594c8c5 | 2018-08-28 14:41:31 +0200 | [diff] [blame] | 7187 | if (ctx->ctx.hlua_apptcp.flags & APPLET_DONE) { |
| 7188 | /* eat the whole request */ |
| 7189 | co_skip(si_oc(si), co_data(si_oc(si))); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7190 | return; |
Olivier Houchard | 594c8c5 | 2018-08-28 14:41:31 +0200 | [diff] [blame] | 7191 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7192 | |
| 7193 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 7194 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 7195 | return; |
| 7196 | |
| 7197 | /* Execute the function. */ |
| 7198 | switch (hlua_ctx_resume(hlua, 1)) { |
| 7199 | /* finished. */ |
| 7200 | case HLUA_E_OK: |
| 7201 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 7202 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7203 | /* eat the whole request */ |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 7204 | co_skip(si_oc(si), co_data(si_oc(si))); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7205 | res->flags |= CF_READ_NULL; |
| 7206 | si_shutr(si); |
| 7207 | return; |
| 7208 | |
| 7209 | /* yield. */ |
| 7210 | case HLUA_E_AGAIN: |
Thierry Fournier | 0164f20 | 2016-02-20 17:47:43 +0100 | [diff] [blame] | 7211 | if (hlua->wake_time != TICK_ETERNITY) |
| 7212 | task_schedule(ctx->ctx.hlua_apptcp.task, hlua->wake_time); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7213 | return; |
| 7214 | |
| 7215 | /* finished with error. */ |
| 7216 | case HLUA_E_ERRMSG: |
| 7217 | /* Display log. */ |
| 7218 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7219 | rule->arg.hlua_rule->fcn->name, lua_tostring(hlua->T, -1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7220 | lua_pop(hlua->T, 1); |
| 7221 | goto error; |
| 7222 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7223 | case HLUA_E_ETMOUT: |
| 7224 | SEND_ERR(px, "Lua applet tcp '%s': execution timeout.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7225 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7226 | goto error; |
| 7227 | |
| 7228 | case HLUA_E_NOMEM: |
| 7229 | SEND_ERR(px, "Lua applet tcp '%s': out of memory error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7230 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7231 | goto error; |
| 7232 | |
| 7233 | case HLUA_E_YIELD: /* unexpected */ |
| 7234 | SEND_ERR(px, "Lua applet tcp '%s': yield not allowed.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7235 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7236 | goto error; |
| 7237 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7238 | case HLUA_E_ERR: |
| 7239 | /* Display log. */ |
| 7240 | SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7241 | rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7242 | goto error; |
| 7243 | |
| 7244 | default: |
| 7245 | goto error; |
| 7246 | } |
| 7247 | |
| 7248 | error: |
| 7249 | |
| 7250 | /* For all other cases, just close the stream. */ |
| 7251 | si_shutw(si); |
| 7252 | si_shutr(si); |
| 7253 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 7254 | } |
| 7255 | |
| 7256 | static void hlua_applet_tcp_release(struct appctx *ctx) |
| 7257 | { |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 7258 | task_destroy(ctx->ctx.hlua_apptcp.task); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7259 | ctx->ctx.hlua_apptcp.task = NULL; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7260 | hlua_ctx_destroy(ctx->ctx.hlua_apptcp.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7261 | ctx->ctx.hlua_apptcp.hlua = NULL; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7262 | } |
| 7263 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7264 | /* The function returns 1 if the initialisation is complete, 0 if |
| 7265 | * an errors occurs and -1 if more data are required for initializing |
| 7266 | * the applet. |
| 7267 | */ |
| 7268 | static int hlua_applet_http_init(struct appctx *ctx, struct proxy *px, struct stream *strm) |
| 7269 | { |
| 7270 | struct stream_interface *si = ctx->owner; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7271 | struct http_txn *txn; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7272 | struct hlua *hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7273 | char **arg; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7274 | struct task *task; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7275 | const char *error; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7276 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7277 | txn = strm->txn; |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7278 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7279 | if (!hlua) { |
| 7280 | SEND_ERR(px, "Lua applet http '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7281 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7282 | return 0; |
| 7283 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7284 | HLUA_INIT(hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7285 | ctx->ctx.hlua_apphttp.hlua = hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7286 | ctx->ctx.hlua_apphttp.left_bytes = -1; |
| 7287 | ctx->ctx.hlua_apphttp.flags = 0; |
| 7288 | |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 7289 | if (txn->req.flags & HTTP_MSGF_VER_11) |
| 7290 | ctx->ctx.hlua_apphttp.flags |= APPLET_HTTP11; |
| 7291 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7292 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 7293 | task = task_new(tid_bit); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7294 | if (!task) { |
| 7295 | SEND_ERR(px, "Lua applet http '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7296 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7297 | return 0; |
| 7298 | } |
| 7299 | task->nice = 0; |
| 7300 | task->context = ctx; |
| 7301 | task->process = hlua_applet_wakeup; |
| 7302 | ctx->ctx.hlua_apphttp.task = task; |
| 7303 | |
| 7304 | /* In the execution wrappers linked with a stream, the |
| 7305 | * Lua context can be not initialized. This behavior |
| 7306 | * permits to save performances because a systematic |
| 7307 | * Lua initialization cause 5% performances loss. |
| 7308 | */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 7309 | 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] | 7310 | 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] | 7311 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7312 | return 0; |
| 7313 | } |
| 7314 | |
| 7315 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 7316 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7317 | |
| 7318 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7319 | if (!SET_SAFE_LJMP(hlua)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7320 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 7321 | error = lua_tostring(hlua->T, -1); |
| 7322 | else |
| 7323 | error = "critical error"; |
| 7324 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7325 | ctx->rule->arg.hlua_rule->fcn->name, error); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7326 | return 0; |
| 7327 | } |
| 7328 | |
| 7329 | /* Check stack available size. */ |
| 7330 | if (!lua_checkstack(hlua->T, 1)) { |
| 7331 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7332 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7333 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7334 | return 0; |
| 7335 | } |
| 7336 | |
| 7337 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 7338 | 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] | 7339 | |
| 7340 | /* Create and and push object stream in the stack. */ |
| 7341 | if (!hlua_applet_http_new(hlua->T, ctx)) { |
| 7342 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7343 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7344 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7345 | return 0; |
| 7346 | } |
| 7347 | hlua->nargs = 1; |
| 7348 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7349 | /* push keywords in the stack. */ |
| 7350 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 7351 | if (!lua_checkstack(hlua->T, 1)) { |
| 7352 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7353 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7354 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7355 | return 0; |
| 7356 | } |
| 7357 | lua_pushstring(hlua->T, *arg); |
| 7358 | hlua->nargs++; |
| 7359 | } |
| 7360 | |
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 | |
| 7363 | /* Wakeup the applet when data is ready for read. */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 7364 | si_cant_get(si); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7365 | |
| 7366 | return 1; |
| 7367 | } |
| 7368 | |
Willy Tarreau | 60409db | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 7369 | void hlua_applet_http_fct(struct appctx *ctx) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7370 | { |
| 7371 | struct stream_interface *si = ctx->owner; |
| 7372 | struct stream *strm = si_strm(si); |
| 7373 | struct channel *req = si_oc(si); |
| 7374 | struct channel *res = si_ic(si); |
| 7375 | struct act_rule *rule = ctx->rule; |
| 7376 | struct proxy *px = strm->be; |
| 7377 | struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua; |
| 7378 | struct htx *req_htx, *res_htx; |
| 7379 | |
| 7380 | res_htx = htx_from_buf(&res->buf); |
| 7381 | |
| 7382 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 7383 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 7384 | goto out; |
| 7385 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 7386 | /* Check if the input buffer is available. */ |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7387 | if (!b_size(&res->buf)) { |
| 7388 | si_rx_room_blk(si); |
| 7389 | goto out; |
| 7390 | } |
| 7391 | /* check that the output is not closed */ |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7392 | if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR)) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7393 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 7394 | |
| 7395 | /* Set the currently running flag. */ |
| 7396 | if (!HLUA_IS_RUNNING(hlua) && |
| 7397 | !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
Christopher Faulet | bd878d2 | 2021-04-28 10:50:21 +0200 | [diff] [blame] | 7398 | if (!co_data(req)) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7399 | si_cant_get(si); |
| 7400 | goto out; |
| 7401 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7402 | } |
| 7403 | |
| 7404 | /* Executes The applet if it is not done. */ |
| 7405 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
| 7406 | |
| 7407 | /* Execute the function. */ |
| 7408 | switch (hlua_ctx_resume(hlua, 1)) { |
| 7409 | /* finished. */ |
| 7410 | case HLUA_E_OK: |
| 7411 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 7412 | break; |
| 7413 | |
| 7414 | /* yield. */ |
| 7415 | case HLUA_E_AGAIN: |
| 7416 | if (hlua->wake_time != TICK_ETERNITY) |
| 7417 | task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time); |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7418 | goto out; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7419 | |
| 7420 | /* finished with error. */ |
| 7421 | case HLUA_E_ERRMSG: |
| 7422 | /* Display log. */ |
| 7423 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7424 | rule->arg.hlua_rule->fcn->name, lua_tostring(hlua->T, -1)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7425 | lua_pop(hlua->T, 1); |
| 7426 | goto error; |
| 7427 | |
| 7428 | case HLUA_E_ETMOUT: |
| 7429 | SEND_ERR(px, "Lua applet http '%s': execution timeout.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7430 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7431 | goto error; |
| 7432 | |
| 7433 | case HLUA_E_NOMEM: |
| 7434 | SEND_ERR(px, "Lua applet http '%s': out of memory error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7435 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7436 | goto error; |
| 7437 | |
| 7438 | case HLUA_E_YIELD: /* unexpected */ |
| 7439 | SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7440 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7441 | goto error; |
| 7442 | |
| 7443 | case HLUA_E_ERR: |
| 7444 | /* Display log. */ |
| 7445 | SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7446 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7447 | goto error; |
| 7448 | |
| 7449 | default: |
| 7450 | goto error; |
| 7451 | } |
| 7452 | } |
| 7453 | |
| 7454 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7455 | if (ctx->ctx.hlua_apphttp.flags & APPLET_RSP_SENT) |
| 7456 | goto done; |
| 7457 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7458 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) |
| 7459 | goto error; |
| 7460 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 7461 | /* no more data are expected. Don't add TLR because mux-h1 will take care of it */ |
| 7462 | res_htx->flags |= HTX_FL_EOM; |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7463 | strm->txn->status = ctx->ctx.hlua_apphttp.status; |
| 7464 | ctx->ctx.hlua_apphttp.flags |= APPLET_RSP_SENT; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7465 | } |
| 7466 | |
| 7467 | done: |
| 7468 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7469 | if (!(res->flags & CF_SHUTR)) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7470 | res->flags |= CF_READ_NULL; |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7471 | si_shutr(si); |
| 7472 | } |
| 7473 | |
| 7474 | /* eat the whole request */ |
| 7475 | if (co_data(req)) { |
| 7476 | req_htx = htx_from_buf(&req->buf); |
| 7477 | co_htx_skip(req, req_htx, co_data(req)); |
| 7478 | htx_to_buf(req_htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7479 | } |
| 7480 | } |
| 7481 | |
| 7482 | out: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7483 | htx_to_buf(res_htx, &res->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7484 | return; |
| 7485 | |
| 7486 | error: |
| 7487 | |
| 7488 | /* If we are in HTTP mode, and we are not send any |
| 7489 | * data, return a 500 server error in best effort: |
| 7490 | * if there is no room available in the buffer, |
| 7491 | * just close the connection. |
| 7492 | */ |
| 7493 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) { |
Christopher Faulet | f734638 | 2019-07-17 22:02:08 +0200 | [diff] [blame] | 7494 | struct buffer *err = &http_err_chunks[HTTP_ERR_500]; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7495 | |
| 7496 | channel_erase(res); |
| 7497 | res->buf.data = b_data(err); |
| 7498 | memcpy(res->buf.area, b_head(err), b_data(err)); |
| 7499 | res_htx = htx_from_buf(&res->buf); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 7500 | channel_add_input(res, res_htx->data); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7501 | } |
| 7502 | if (!(strm->flags & SF_ERR_MASK)) |
| 7503 | strm->flags |= SF_ERR_RESOURCE; |
| 7504 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 7505 | goto done; |
| 7506 | } |
| 7507 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7508 | static void hlua_applet_http_release(struct appctx *ctx) |
| 7509 | { |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 7510 | task_destroy(ctx->ctx.hlua_apphttp.task); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7511 | ctx->ctx.hlua_apphttp.task = NULL; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7512 | hlua_ctx_destroy(ctx->ctx.hlua_apphttp.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7513 | ctx->ctx.hlua_apphttp.hlua = NULL; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7514 | } |
| 7515 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7516 | /* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 7517 | * success case, else return ACT_RET_PRS_ERR. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7518 | * |
| 7519 | * This function can fail with an abort() due to an Lua critical error. |
| 7520 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 7521 | * tolerated. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7522 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7523 | static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px, |
| 7524 | struct act_rule *rule, char **err) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7525 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7526 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7527 | int i; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7528 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7529 | /* Memory for the rule. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7530 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7531 | if (!rule->arg.hlua_rule) { |
| 7532 | memprintf(err, "out of memory error"); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 7533 | goto error; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7534 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7535 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7536 | /* Memory for arguments. */ |
Tim Duesterhus | e52b6e5 | 2020-09-12 20:26:43 +0200 | [diff] [blame] | 7537 | rule->arg.hlua_rule->args = calloc(fcn->nargs + 1, |
| 7538 | sizeof(*rule->arg.hlua_rule->args)); |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7539 | if (!rule->arg.hlua_rule->args) { |
| 7540 | memprintf(err, "out of memory error"); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 7541 | goto error; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7542 | } |
| 7543 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7544 | /* Reference the Lua function and store the reference. */ |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7545 | rule->arg.hlua_rule->fcn = fcn; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7546 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7547 | /* Expect some arguments */ |
| 7548 | for (i = 0; i < fcn->nargs; i++) { |
Thierry FOURNIER | 1725c2e | 2019-01-06 19:38:49 +0100 | [diff] [blame] | 7549 | if (*args[*cur_arg] == '\0') { |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7550 | memprintf(err, "expect %d arguments", fcn->nargs); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 7551 | goto error; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7552 | } |
Thierry FOURNIER | 1725c2e | 2019-01-06 19:38:49 +0100 | [diff] [blame] | 7553 | rule->arg.hlua_rule->args[i] = strdup(args[*cur_arg]); |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7554 | if (!rule->arg.hlua_rule->args[i]) { |
| 7555 | memprintf(err, "out of memory error"); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 7556 | goto error; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7557 | } |
| 7558 | (*cur_arg)++; |
| 7559 | } |
| 7560 | rule->arg.hlua_rule->args[i] = NULL; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7561 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 7562 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7563 | rule->action_ptr = hlua_action; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 7564 | return ACT_RET_PRS_OK; |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 7565 | |
| 7566 | error: |
| 7567 | if (rule->arg.hlua_rule) { |
| 7568 | if (rule->arg.hlua_rule->args) { |
| 7569 | for (i = 0; i < fcn->nargs; i++) |
| 7570 | ha_free(&rule->arg.hlua_rule->args[i]); |
| 7571 | ha_free(&rule->arg.hlua_rule->args); |
| 7572 | } |
| 7573 | ha_free(&rule->arg.hlua_rule); |
| 7574 | } |
| 7575 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7576 | } |
| 7577 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7578 | static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px, |
| 7579 | struct act_rule *rule, char **err) |
| 7580 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7581 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7582 | |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 7583 | /* HTTP applets are forbidden in tcp-request rules. |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 7584 | * HTTP applet request requires everything initialized by |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 7585 | * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER). |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 7586 | * The applet will be immediately initialized, but its before |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 7587 | * the call of this analyzer. |
| 7588 | */ |
| 7589 | if (rule->from != ACT_F_HTTP_REQ) { |
| 7590 | memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets"); |
| 7591 | return ACT_RET_PRS_ERR; |
| 7592 | } |
| 7593 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7594 | /* Memory for the rule. */ |
| 7595 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 7596 | if (!rule->arg.hlua_rule) { |
| 7597 | memprintf(err, "out of memory error"); |
| 7598 | return ACT_RET_PRS_ERR; |
| 7599 | } |
| 7600 | |
| 7601 | /* Reference the Lua function and store the reference. */ |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7602 | rule->arg.hlua_rule->fcn = fcn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7603 | |
| 7604 | /* TODO: later accept arguments. */ |
| 7605 | rule->arg.hlua_rule->args = NULL; |
| 7606 | |
| 7607 | /* Add applet pointer in the rule. */ |
| 7608 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 7609 | rule->applet.name = fcn->name; |
| 7610 | rule->applet.init = hlua_applet_http_init; |
| 7611 | rule->applet.fct = hlua_applet_http_fct; |
| 7612 | rule->applet.release = hlua_applet_http_release; |
| 7613 | rule->applet.timeout = hlua_timeout_applet; |
| 7614 | |
| 7615 | return ACT_RET_PRS_OK; |
| 7616 | } |
| 7617 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7618 | /* This function is an LUA binding used for registering |
| 7619 | * "sample-conv" functions. It expects a converter name used |
| 7620 | * in the haproxy configuration file, and an LUA function. |
| 7621 | */ |
| 7622 | __LJMP static int hlua_register_action(lua_State *L) |
| 7623 | { |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 7624 | struct action_kw_list *akl = NULL; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7625 | const char *name; |
| 7626 | int ref; |
| 7627 | int len; |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 7628 | struct hlua_function *fcn = NULL; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7629 | int nargs; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 7630 | struct buffer *trash; |
| 7631 | struct action_kw *akw; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7632 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7633 | /* Initialise the number of expected arguments at 0. */ |
| 7634 | nargs = 0; |
| 7635 | |
| 7636 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 7637 | 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] | 7638 | |
| 7639 | /* First argument : converter name. */ |
| 7640 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 7641 | |
| 7642 | /* Second argument : environment. */ |
| 7643 | if (lua_type(L, 2) != LUA_TTABLE) |
| 7644 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 7645 | |
| 7646 | /* Third argument : lua function. */ |
| 7647 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 7648 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7649 | /* 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] | 7650 | if (lua_gettop(L) >= 4) |
| 7651 | nargs = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 7652 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7653 | /* browse the second argument as an array. */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7654 | lua_pushnil(L); |
| 7655 | while (lua_next(L, 2) != 0) { |
| 7656 | if (lua_type(L, -1) != LUA_TSTRING) |
| 7657 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 7658 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 7659 | /* Check if action exists */ |
| 7660 | trash = get_trash_chunk(); |
| 7661 | chunk_printf(trash, "lua.%s", name); |
| 7662 | if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) { |
| 7663 | akw = tcp_req_cont_action(trash->area); |
| 7664 | } else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) { |
| 7665 | akw = tcp_res_cont_action(trash->area); |
| 7666 | } else if (strcmp(lua_tostring(L, -1), "http-req") == 0) { |
| 7667 | akw = action_http_req_custom(trash->area); |
| 7668 | } else if (strcmp(lua_tostring(L, -1), "http-res") == 0) { |
| 7669 | akw = action_http_res_custom(trash->area); |
| 7670 | } else { |
| 7671 | akw = NULL; |
| 7672 | } |
| 7673 | if (akw != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 7674 | fcn = akw->private; |
| 7675 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 7676 | ha_warning("Trying to register action 'lua.%s' more than once. " |
| 7677 | "This will become a hard error in version 2.5.\n", name); |
| 7678 | } |
| 7679 | fcn->function_ref[hlua_state_id] = ref; |
| 7680 | |
| 7681 | /* pop the environment string. */ |
| 7682 | lua_pop(L, 1); |
| 7683 | continue; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 7684 | } |
| 7685 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7686 | /* Check required environment. Only accepted "http" or "tcp". */ |
| 7687 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7688 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7689 | if (!akl) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 7690 | goto alloc_error;; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 7691 | fcn = new_hlua_function(); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7692 | if (!fcn) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 7693 | goto alloc_error; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7694 | |
| 7695 | /* Fill fcn. */ |
| 7696 | fcn->name = strdup(name); |
| 7697 | if (!fcn->name) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 7698 | goto alloc_error; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 7699 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7700 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 7701 | /* Set the expected number of arguments. */ |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7702 | fcn->nargs = nargs; |
| 7703 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7704 | /* List head */ |
| 7705 | akl->list.n = akl->list.p = NULL; |
| 7706 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7707 | /* action keyword. */ |
| 7708 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7709 | akl->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7710 | if (!akl->kw[0].kw) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 7711 | goto alloc_error; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7712 | |
| 7713 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 7714 | |
Amaury Denoyelle | e4a617c | 2021-05-06 15:33:09 +0200 | [diff] [blame] | 7715 | akl->kw[0].flags = 0; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7716 | akl->kw[0].private = fcn; |
| 7717 | akl->kw[0].parse = action_register_lua; |
| 7718 | |
| 7719 | /* select the action registering point. */ |
| 7720 | if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) |
| 7721 | tcp_req_cont_keywords_register(akl); |
| 7722 | else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) |
| 7723 | tcp_res_cont_keywords_register(akl); |
| 7724 | else if (strcmp(lua_tostring(L, -1), "http-req") == 0) |
| 7725 | http_req_keywords_register(akl); |
| 7726 | else if (strcmp(lua_tostring(L, -1), "http-res") == 0) |
| 7727 | http_res_keywords_register(akl); |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 7728 | else { |
| 7729 | release_hlua_function(fcn); |
| 7730 | if (akl) |
| 7731 | ha_free((char **)&(akl->kw[0].kw)); |
| 7732 | ha_free(&akl); |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7733 | WILL_LJMP(luaL_error(L, "Lua action environment '%s' is unknown. " |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7734 | "'tcp-req', 'tcp-res', 'http-req' or 'http-res' " |
| 7735 | "are expected.", lua_tostring(L, -1))); |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 7736 | } |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7737 | |
| 7738 | /* pop the environment string. */ |
| 7739 | lua_pop(L, 1); |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 7740 | |
| 7741 | /* reset for next loop */ |
| 7742 | akl = NULL; |
| 7743 | fcn = NULL; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7744 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7745 | return ACT_RET_PRS_OK; |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 7746 | |
| 7747 | alloc_error: |
| 7748 | release_hlua_function(fcn); |
| 7749 | ha_free(&akl); |
| 7750 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 7751 | return 0; /* Never reached */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7752 | } |
| 7753 | |
| 7754 | static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px, |
| 7755 | struct act_rule *rule, char **err) |
| 7756 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7757 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7758 | |
Christopher Faulet | 280f85b | 2019-07-15 15:02:04 +0200 | [diff] [blame] | 7759 | if (px->mode == PR_MODE_HTTP) { |
| 7760 | memprintf(err, "Lua TCP services cannot be used on HTTP proxies"); |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 7761 | return ACT_RET_PRS_ERR; |
| 7762 | } |
| 7763 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7764 | /* Memory for the rule. */ |
| 7765 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 7766 | if (!rule->arg.hlua_rule) { |
| 7767 | memprintf(err, "out of memory error"); |
| 7768 | return ACT_RET_PRS_ERR; |
| 7769 | } |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7770 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7771 | /* Reference the Lua function and store the reference. */ |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7772 | rule->arg.hlua_rule->fcn = fcn; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7773 | |
| 7774 | /* TODO: later accept arguments. */ |
| 7775 | rule->arg.hlua_rule->args = NULL; |
| 7776 | |
| 7777 | /* Add applet pointer in the rule. */ |
| 7778 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 7779 | rule->applet.name = fcn->name; |
| 7780 | rule->applet.init = hlua_applet_tcp_init; |
| 7781 | rule->applet.fct = hlua_applet_tcp_fct; |
| 7782 | rule->applet.release = hlua_applet_tcp_release; |
| 7783 | rule->applet.timeout = hlua_timeout_applet; |
| 7784 | |
| 7785 | return 0; |
| 7786 | } |
| 7787 | |
| 7788 | /* This function is an LUA binding used for registering |
| 7789 | * "sample-conv" functions. It expects a converter name used |
| 7790 | * in the haproxy configuration file, and an LUA function. |
| 7791 | */ |
| 7792 | __LJMP static int hlua_register_service(lua_State *L) |
| 7793 | { |
| 7794 | struct action_kw_list *akl; |
| 7795 | const char *name; |
| 7796 | const char *env; |
| 7797 | int ref; |
| 7798 | int len; |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 7799 | struct hlua_function *fcn = NULL; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 7800 | struct buffer *trash; |
| 7801 | struct action_kw *akw; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7802 | |
| 7803 | MAY_LJMP(check_args(L, 3, "register_service")); |
| 7804 | |
| 7805 | /* First argument : converter name. */ |
| 7806 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 7807 | |
| 7808 | /* Second argument : environment. */ |
| 7809 | env = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7810 | |
| 7811 | /* Third argument : lua function. */ |
| 7812 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 7813 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 7814 | /* Check for service already registered */ |
| 7815 | trash = get_trash_chunk(); |
| 7816 | chunk_printf(trash, "lua.%s", name); |
| 7817 | akw = service_find(trash->area); |
| 7818 | if (akw != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 7819 | fcn = akw->private; |
| 7820 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 7821 | ha_warning("Trying to register service 'lua.%s' more than once. " |
| 7822 | "This will become a hard error in version 2.5.\n", name); |
| 7823 | } |
| 7824 | fcn->function_ref[hlua_state_id] = ref; |
| 7825 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 7826 | } |
| 7827 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7828 | /* Allocate and fill the sample fetch keyword struct. */ |
| 7829 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
| 7830 | if (!akl) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 7831 | goto alloc_error; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 7832 | fcn = new_hlua_function(); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7833 | if (!fcn) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 7834 | goto alloc_error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7835 | |
| 7836 | /* Fill fcn. */ |
| 7837 | len = strlen("<lua.>") + strlen(name) + 1; |
| 7838 | fcn->name = calloc(1, len); |
| 7839 | if (!fcn->name) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 7840 | goto alloc_error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7841 | snprintf((char *)fcn->name, len, "<lua.%s>", name); |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 7842 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7843 | |
| 7844 | /* List head */ |
| 7845 | akl->list.n = akl->list.p = NULL; |
| 7846 | |
| 7847 | /* converter keyword. */ |
| 7848 | len = strlen("lua.") + strlen(name) + 1; |
| 7849 | akl->kw[0].kw = calloc(1, len); |
| 7850 | if (!akl->kw[0].kw) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 7851 | goto alloc_error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7852 | |
| 7853 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 7854 | |
Thierry FOURNIER / OZON.IO | 02564fd | 2016-11-12 11:07:05 +0100 | [diff] [blame] | 7855 | /* Check required environment. Only accepted "http" or "tcp". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7856 | if (strcmp(env, "tcp") == 0) |
| 7857 | akl->kw[0].parse = action_register_service_tcp; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7858 | else if (strcmp(env, "http") == 0) |
| 7859 | akl->kw[0].parse = action_register_service_http; |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 7860 | else { |
| 7861 | release_hlua_function(fcn); |
| 7862 | if (akl) |
| 7863 | ha_free((char **)&(akl->kw[0].kw)); |
| 7864 | ha_free(&akl); |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7865 | WILL_LJMP(luaL_error(L, "Lua service environment '%s' is unknown. " |
Eric Salama | fe7456f | 2017-12-21 14:30:07 +0100 | [diff] [blame] | 7866 | "'tcp' or 'http' are expected.", env)); |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 7867 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7868 | |
Amaury Denoyelle | e4a617c | 2021-05-06 15:33:09 +0200 | [diff] [blame] | 7869 | akl->kw[0].flags = 0; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7870 | akl->kw[0].private = fcn; |
| 7871 | |
| 7872 | /* End of array. */ |
| 7873 | memset(&akl->kw[1], 0, sizeof(*akl->kw)); |
| 7874 | |
| 7875 | /* Register this new converter */ |
| 7876 | service_keywords_register(akl); |
| 7877 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7878 | return 0; |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 7879 | |
| 7880 | alloc_error: |
| 7881 | release_hlua_function(fcn); |
| 7882 | ha_free(&akl); |
| 7883 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 7884 | return 0; /* Never reached */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7885 | } |
| 7886 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7887 | /* This function initialises Lua cli handler. It copies the |
| 7888 | * arguments in the Lua stack and create channel IO objects. |
| 7889 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 7890 | 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] | 7891 | { |
| 7892 | struct hlua *hlua; |
| 7893 | struct hlua_function *fcn; |
| 7894 | int i; |
| 7895 | const char *error; |
| 7896 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7897 | fcn = private; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7898 | appctx->ctx.hlua_cli.fcn = private; |
| 7899 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7900 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7901 | if (!hlua) { |
| 7902 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 7903 | return 1; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7904 | } |
| 7905 | HLUA_INIT(hlua); |
| 7906 | appctx->ctx.hlua_cli.hlua = hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7907 | |
| 7908 | /* Create task used by signal to wakeup applets. |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 7909 | * 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] | 7910 | * applet_http. It is absolutely compatible. |
| 7911 | */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 7912 | appctx->ctx.hlua_cli.task = task_new(tid_bit); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7913 | if (!appctx->ctx.hlua_cli.task) { |
Thierry FOURNIER | ffbf569 | 2016-12-16 11:14:06 +0100 | [diff] [blame] | 7914 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 7915 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7916 | } |
| 7917 | appctx->ctx.hlua_cli.task->nice = 0; |
| 7918 | appctx->ctx.hlua_cli.task->context = appctx; |
| 7919 | appctx->ctx.hlua_cli.task->process = hlua_applet_wakeup; |
| 7920 | |
| 7921 | /* Initialises the Lua context */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 7922 | 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] | 7923 | 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] | 7924 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7925 | } |
| 7926 | |
| 7927 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7928 | if (!SET_SAFE_LJMP(hlua)) { |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7929 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 7930 | error = lua_tostring(hlua->T, -1); |
| 7931 | else |
| 7932 | error = "critical error"; |
| 7933 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error); |
| 7934 | goto error; |
| 7935 | } |
| 7936 | |
| 7937 | /* Check stack available size. */ |
| 7938 | if (!lua_checkstack(hlua->T, 2)) { |
| 7939 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 7940 | goto error; |
| 7941 | } |
| 7942 | |
| 7943 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 7944 | 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] | 7945 | |
| 7946 | /* Once the arguments parsed, the CLI is like an AppletTCP, |
| 7947 | * so push AppletTCP in the stack. |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7948 | */ |
| 7949 | if (!hlua_applet_tcp_new(hlua->T, appctx)) { |
| 7950 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 7951 | goto error; |
| 7952 | } |
| 7953 | hlua->nargs = 1; |
| 7954 | |
| 7955 | /* push keywords in the stack. */ |
| 7956 | for (i = 0; *args[i]; i++) { |
| 7957 | /* Check stack available size. */ |
| 7958 | if (!lua_checkstack(hlua->T, 1)) { |
| 7959 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 7960 | goto error; |
| 7961 | } |
| 7962 | lua_pushstring(hlua->T, args[i]); |
| 7963 | hlua->nargs++; |
| 7964 | } |
| 7965 | |
| 7966 | /* We must initialize the execution timeouts. */ |
| 7967 | hlua->max_time = hlua_timeout_session; |
| 7968 | |
| 7969 | /* At this point the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7970 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7971 | |
| 7972 | /* It's ok */ |
| 7973 | return 0; |
| 7974 | |
| 7975 | /* It's not ok. */ |
| 7976 | error: |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7977 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7978 | hlua_ctx_destroy(hlua); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 7979 | appctx->ctx.hlua_cli.hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7980 | return 1; |
| 7981 | } |
| 7982 | |
| 7983 | static int hlua_cli_io_handler_fct(struct appctx *appctx) |
| 7984 | { |
| 7985 | struct hlua *hlua; |
| 7986 | struct stream_interface *si; |
| 7987 | struct hlua_function *fcn; |
| 7988 | |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7989 | hlua = appctx->ctx.hlua_cli.hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7990 | si = appctx->owner; |
Willy Tarreau | 8ae4f75 | 2016-12-14 15:41:45 +0100 | [diff] [blame] | 7991 | fcn = appctx->ctx.hlua_cli.fcn; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7992 | |
| 7993 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 7994 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 7995 | return 1; |
| 7996 | |
| 7997 | /* Execute the function. */ |
| 7998 | switch (hlua_ctx_resume(hlua, 1)) { |
| 7999 | |
| 8000 | /* finished. */ |
| 8001 | case HLUA_E_OK: |
| 8002 | return 1; |
| 8003 | |
| 8004 | /* yield. */ |
| 8005 | case HLUA_E_AGAIN: |
| 8006 | /* We want write. */ |
| 8007 | if (HLUA_IS_WAKERESWR(hlua)) |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 8008 | si_rx_room_blk(si); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8009 | /* Set the timeout. */ |
| 8010 | if (hlua->wake_time != TICK_ETERNITY) |
| 8011 | task_schedule(hlua->task, hlua->wake_time); |
| 8012 | return 0; |
| 8013 | |
| 8014 | /* finished with error. */ |
| 8015 | case HLUA_E_ERRMSG: |
| 8016 | /* Display log. */ |
| 8017 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", |
| 8018 | fcn->name, lua_tostring(hlua->T, -1)); |
| 8019 | lua_pop(hlua->T, 1); |
| 8020 | return 1; |
| 8021 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 8022 | case HLUA_E_ETMOUT: |
| 8023 | SEND_ERR(NULL, "Lua converter '%s': execution timeout.\n", |
| 8024 | fcn->name); |
| 8025 | return 1; |
| 8026 | |
| 8027 | case HLUA_E_NOMEM: |
| 8028 | SEND_ERR(NULL, "Lua converter '%s': out of memory error.\n", |
| 8029 | fcn->name); |
| 8030 | return 1; |
| 8031 | |
| 8032 | case HLUA_E_YIELD: /* unexpected */ |
| 8033 | SEND_ERR(NULL, "Lua converter '%s': yield not allowed.\n", |
| 8034 | fcn->name); |
| 8035 | return 1; |
| 8036 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8037 | case HLUA_E_ERR: |
| 8038 | /* Display log. */ |
| 8039 | SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n", |
| 8040 | fcn->name); |
| 8041 | return 1; |
| 8042 | |
| 8043 | default: |
| 8044 | return 1; |
| 8045 | } |
| 8046 | |
| 8047 | return 1; |
| 8048 | } |
| 8049 | |
| 8050 | static void hlua_cli_io_release_fct(struct appctx *appctx) |
| 8051 | { |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 8052 | hlua_ctx_destroy(appctx->ctx.hlua_cli.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 8053 | appctx->ctx.hlua_cli.hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8054 | } |
| 8055 | |
| 8056 | /* This function is an LUA binding used for registering |
| 8057 | * new keywords in the cli. It expects a list of keywords |
| 8058 | * which are the "path". It is limited to 5 keywords. A |
| 8059 | * description of the command, a function to be executed |
| 8060 | * for the parsing and a function for io handlers. |
| 8061 | */ |
| 8062 | __LJMP static int hlua_register_cli(lua_State *L) |
| 8063 | { |
| 8064 | struct cli_kw_list *cli_kws; |
| 8065 | const char *message; |
| 8066 | int ref_io; |
| 8067 | int len; |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 8068 | struct hlua_function *fcn = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8069 | int index; |
| 8070 | int i; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8071 | struct buffer *trash; |
| 8072 | const char *kw[5]; |
| 8073 | struct cli_kw *cli_kw; |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 8074 | const char *errmsg; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8075 | |
| 8076 | MAY_LJMP(check_args(L, 3, "register_cli")); |
| 8077 | |
| 8078 | /* First argument : an array of maximum 5 keywords. */ |
| 8079 | if (!lua_istable(L, 1)) |
| 8080 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table")); |
| 8081 | |
| 8082 | /* Second argument : string with contextual message. */ |
| 8083 | message = MAY_LJMP(luaL_checkstring(L, 2)); |
| 8084 | |
| 8085 | /* Third and fourth argument : lua function. */ |
| 8086 | ref_io = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 8087 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8088 | /* Check for CLI service already registered */ |
| 8089 | trash = get_trash_chunk(); |
| 8090 | index = 0; |
| 8091 | lua_pushnil(L); |
| 8092 | memset(kw, 0, sizeof(kw)); |
| 8093 | while (lua_next(L, 1) != 0) { |
| 8094 | if (index >= CLI_PREFIX_KW_NB) |
| 8095 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries")); |
| 8096 | if (lua_type(L, -1) != LUA_TSTRING) |
| 8097 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings")); |
| 8098 | kw[index] = lua_tostring(L, -1); |
| 8099 | if (index == 0) |
| 8100 | chunk_printf(trash, "%s", kw[index]); |
| 8101 | else |
| 8102 | chunk_appendf(trash, " %s", kw[index]); |
| 8103 | index++; |
| 8104 | lua_pop(L, 1); |
| 8105 | } |
| 8106 | cli_kw = cli_find_kw_exact((char **)kw); |
| 8107 | if (cli_kw != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8108 | fcn = cli_kw->private; |
| 8109 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 8110 | ha_warning("Trying to register CLI keyword 'lua.%s' more than once. " |
| 8111 | "This will become a hard error in version 2.5.\n", trash->area); |
| 8112 | } |
| 8113 | fcn->function_ref[hlua_state_id] = ref_io; |
| 8114 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8115 | } |
| 8116 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8117 | /* Allocate and fill the sample fetch keyword struct. */ |
| 8118 | cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 8119 | if (!cli_kws) { |
| 8120 | errmsg = "Lua out of memory error."; |
| 8121 | goto error; |
| 8122 | } |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 8123 | fcn = new_hlua_function(); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 8124 | if (!fcn) { |
| 8125 | errmsg = "Lua out of memory error."; |
| 8126 | goto error; |
| 8127 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8128 | |
| 8129 | /* Fill path. */ |
| 8130 | index = 0; |
| 8131 | lua_pushnil(L); |
| 8132 | while(lua_next(L, 1) != 0) { |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 8133 | if (index >= 5) { |
| 8134 | errmsg = "1st argument must be a table with a maximum of 5 entries"; |
| 8135 | goto error; |
| 8136 | } |
| 8137 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 8138 | errmsg = "1st argument must be a table filled with strings"; |
| 8139 | goto error; |
| 8140 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8141 | cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1)); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 8142 | if (!cli_kws->kw[0].str_kw[index]) { |
| 8143 | errmsg = "Lua out of memory error."; |
| 8144 | goto error; |
| 8145 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8146 | index++; |
| 8147 | lua_pop(L, 1); |
| 8148 | } |
| 8149 | |
| 8150 | /* Copy help message. */ |
| 8151 | cli_kws->kw[0].usage = strdup(message); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 8152 | if (!cli_kws->kw[0].usage) { |
| 8153 | errmsg = "Lua out of memory error."; |
| 8154 | goto error; |
| 8155 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8156 | |
| 8157 | /* Fill fcn io handler. */ |
| 8158 | len = strlen("<lua.cli>") + 1; |
| 8159 | for (i = 0; i < index; i++) |
| 8160 | len += strlen(cli_kws->kw[0].str_kw[i]) + 1; |
| 8161 | fcn->name = calloc(1, len); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 8162 | if (!fcn->name) { |
| 8163 | errmsg = "Lua out of memory error."; |
| 8164 | goto error; |
| 8165 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8166 | strncat((char *)fcn->name, "<lua.cli", len); |
| 8167 | for (i = 0; i < index; i++) { |
| 8168 | strncat((char *)fcn->name, ".", len); |
| 8169 | strncat((char *)fcn->name, cli_kws->kw[0].str_kw[i], len); |
| 8170 | } |
| 8171 | strncat((char *)fcn->name, ">", len); |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8172 | fcn->function_ref[hlua_state_id] = ref_io; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8173 | |
| 8174 | /* Fill last entries. */ |
| 8175 | cli_kws->kw[0].private = fcn; |
| 8176 | cli_kws->kw[0].parse = hlua_cli_parse_fct; |
| 8177 | cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct; |
| 8178 | cli_kws->kw[0].io_release = hlua_cli_io_release_fct; |
| 8179 | |
| 8180 | /* Register this new converter */ |
| 8181 | cli_register_kw(cli_kws); |
| 8182 | |
| 8183 | return 0; |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 8184 | |
| 8185 | error: |
| 8186 | release_hlua_function(fcn); |
| 8187 | if (cli_kws) { |
| 8188 | for (i = 0; i < index; i++) |
| 8189 | ha_free((char **)&(cli_kws->kw[0].str_kw[i])); |
| 8190 | ha_free((char **)&(cli_kws->kw[0].usage)); |
| 8191 | } |
| 8192 | ha_free(&cli_kws); |
| 8193 | WILL_LJMP(luaL_error(L, errmsg)); |
| 8194 | return 0; /* Never reached */ |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8195 | } |
| 8196 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8197 | 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] | 8198 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8199 | char **err, unsigned int *timeout) |
| 8200 | { |
| 8201 | const char *error; |
| 8202 | |
| 8203 | error = parse_time_err(args[1], timeout, TIME_UNIT_MS); |
Willy Tarreau | 9faebe3 | 2019-06-07 19:00:37 +0200 | [diff] [blame] | 8204 | if (error == PARSE_TIME_OVER) { |
| 8205 | memprintf(err, "timer overflow in argument <%s> to <%s> (maximum value is 2147483647 ms or ~24.8 days)", |
| 8206 | args[1], args[0]); |
| 8207 | return -1; |
| 8208 | } |
| 8209 | else if (error == PARSE_TIME_UNDER) { |
| 8210 | memprintf(err, "timer underflow in argument <%s> to <%s> (minimum non-null value is 1 ms)", |
| 8211 | args[1], args[0]); |
| 8212 | return -1; |
| 8213 | } |
| 8214 | else if (error) { |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8215 | memprintf(err, "%s: invalid timeout", args[0]); |
| 8216 | return -1; |
| 8217 | } |
| 8218 | return 0; |
| 8219 | } |
| 8220 | |
| 8221 | 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] | 8222 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8223 | char **err) |
| 8224 | { |
| 8225 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 8226 | file, line, err, &hlua_timeout_session); |
| 8227 | } |
| 8228 | |
| 8229 | 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] | 8230 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8231 | char **err) |
| 8232 | { |
| 8233 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 8234 | file, line, err, &hlua_timeout_task); |
| 8235 | } |
| 8236 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8237 | 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] | 8238 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8239 | char **err) |
| 8240 | { |
| 8241 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 8242 | file, line, err, &hlua_timeout_applet); |
| 8243 | } |
| 8244 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 8245 | 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] | 8246 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 8247 | char **err) |
| 8248 | { |
| 8249 | char *error; |
| 8250 | |
| 8251 | hlua_nb_instruction = strtoll(args[1], &error, 10); |
| 8252 | if (*error != '\0') { |
| 8253 | memprintf(err, "%s: invalid number", args[0]); |
| 8254 | return -1; |
| 8255 | } |
| 8256 | return 0; |
| 8257 | } |
| 8258 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8259 | 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] | 8260 | const struct proxy *defpx, const char *file, int line, |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8261 | char **err) |
| 8262 | { |
| 8263 | char *error; |
| 8264 | |
| 8265 | if (*(args[1]) == 0) { |
| 8266 | memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]); |
| 8267 | return -1; |
| 8268 | } |
| 8269 | hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L; |
| 8270 | if (*error != '\0') { |
| 8271 | memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error); |
| 8272 | return -1; |
| 8273 | } |
| 8274 | return 0; |
| 8275 | } |
| 8276 | |
| 8277 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8278 | /* This function is called by the main configuration key "lua-load". It loads and |
| 8279 | * execute an lua file during the parsing of the HAProxy configuration file. It is |
| 8280 | * the main lua entry point. |
| 8281 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 8282 | * This function runs with the HAProxy keywords API. It returns -1 if an error |
| 8283 | * occurs, otherwise it returns 0. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8284 | * |
| 8285 | * In some error case, LUA set an error message in top of the stack. This function |
| 8286 | * 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] | 8287 | * |
| 8288 | * This function can fail with an abort() due to an Lua critical error. |
| 8289 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 8290 | * tolerated. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8291 | */ |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8292 | static int hlua_load_state(char *filename, lua_State *L, char **err) |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8293 | { |
| 8294 | int error; |
| 8295 | |
| 8296 | /* Just load and compile the file. */ |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8297 | error = luaL_loadfile(L, filename); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8298 | if (error) { |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8299 | memprintf(err, "error in Lua file '%s': %s", filename, lua_tostring(L, -1)); |
| 8300 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8301 | return -1; |
| 8302 | } |
| 8303 | |
| 8304 | /* If no syntax error where detected, execute the code. */ |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8305 | error = lua_pcall(L, 0, LUA_MULTRET, 0); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8306 | switch (error) { |
| 8307 | case LUA_OK: |
| 8308 | break; |
| 8309 | case LUA_ERRRUN: |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8310 | memprintf(err, "Lua runtime error: %s\n", lua_tostring(L, -1)); |
| 8311 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8312 | return -1; |
| 8313 | case LUA_ERRMEM: |
Thierry Fournier | de6145f | 2020-11-29 00:55:53 +0100 | [diff] [blame] | 8314 | memprintf(err, "Lua out of memory error\n"); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8315 | return -1; |
| 8316 | case LUA_ERRERR: |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8317 | memprintf(err, "Lua message handler error: %s\n", lua_tostring(L, -1)); |
| 8318 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8319 | return -1; |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 8320 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM <= 503 |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8321 | case LUA_ERRGCMM: |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8322 | memprintf(err, "Lua garbage collector error: %s\n", lua_tostring(L, -1)); |
| 8323 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8324 | return -1; |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 8325 | #endif |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8326 | default: |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8327 | memprintf(err, "Lua unknown error: %s\n", lua_tostring(L, -1)); |
| 8328 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8329 | return -1; |
| 8330 | } |
| 8331 | |
| 8332 | return 0; |
| 8333 | } |
| 8334 | |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8335 | static int hlua_load(char **args, int section_type, struct proxy *curpx, |
Willy Tarreau | 0182516 | 2021-03-09 09:53:46 +0100 | [diff] [blame] | 8336 | const struct proxy *defpx, const char *file, int line, |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8337 | char **err) |
| 8338 | { |
| 8339 | if (*(args[1]) == 0) { |
| 8340 | memprintf(err, "'%s' expects a file name as parameter.\n", args[0]); |
| 8341 | return -1; |
| 8342 | } |
| 8343 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8344 | /* loading for global state */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8345 | hlua_state_id = 0; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8346 | ha_set_tid(0); |
Thierry Fournier | afc63e2 | 2020-11-28 17:06:51 +0100 | [diff] [blame] | 8347 | return hlua_load_state(args[1], hlua_states[0], err); |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8348 | } |
| 8349 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8350 | 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] | 8351 | const struct proxy *defpx, const char *file, int line, |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8352 | char **err) |
| 8353 | { |
| 8354 | int len; |
| 8355 | |
| 8356 | if (*(args[1]) == 0) { |
| 8357 | memprintf(err, "'%s' expects a file as parameter.\n", args[0]); |
| 8358 | return -1; |
| 8359 | } |
| 8360 | |
| 8361 | if (per_thread_load == NULL) { |
| 8362 | /* allocate the first entry large enough to store the final NULL */ |
| 8363 | per_thread_load = calloc(1, sizeof(*per_thread_load)); |
| 8364 | if (per_thread_load == NULL) { |
| 8365 | memprintf(err, "out of memory error"); |
| 8366 | return -1; |
| 8367 | } |
| 8368 | } |
| 8369 | |
| 8370 | /* count used entries */ |
| 8371 | for (len = 0; per_thread_load[len] != NULL; len++) |
| 8372 | ; |
| 8373 | |
| 8374 | per_thread_load = realloc(per_thread_load, (len + 2) * sizeof(*per_thread_load)); |
| 8375 | if (per_thread_load == NULL) { |
| 8376 | memprintf(err, "out of memory error"); |
| 8377 | return -1; |
| 8378 | } |
| 8379 | |
| 8380 | per_thread_load[len] = strdup(args[1]); |
| 8381 | per_thread_load[len + 1] = NULL; |
| 8382 | |
| 8383 | if (per_thread_load[len] == NULL) { |
| 8384 | memprintf(err, "out of memory error"); |
| 8385 | return -1; |
| 8386 | } |
| 8387 | |
| 8388 | /* loading for thread 1 only */ |
| 8389 | hlua_state_id = 1; |
| 8390 | ha_set_tid(0); |
| 8391 | return hlua_load_state(args[1], hlua_states[1], err); |
| 8392 | } |
| 8393 | |
Tim Duesterhus | c9fc9f2 | 2020-01-12 13:55:39 +0100 | [diff] [blame] | 8394 | /* Prepend the given <path> followed by a semicolon to the `package.<type>` variable |
| 8395 | * in the given <ctx>. |
| 8396 | */ |
Thierry Fournier | 3fb9e51 | 2020-11-28 10:13:12 +0100 | [diff] [blame] | 8397 | static int hlua_prepend_path(lua_State *L, char *type, char *path) |
Tim Duesterhus | c9fc9f2 | 2020-01-12 13:55:39 +0100 | [diff] [blame] | 8398 | { |
Thierry Fournier | 3fb9e51 | 2020-11-28 10:13:12 +0100 | [diff] [blame] | 8399 | lua_getglobal(L, "package"); /* push package variable */ |
| 8400 | lua_pushstring(L, path); /* push given path */ |
| 8401 | lua_pushstring(L, ";"); /* push semicolon */ |
| 8402 | lua_getfield(L, -3, type); /* push old path */ |
| 8403 | lua_concat(L, 3); /* concatenate to new path */ |
| 8404 | lua_setfield(L, -2, type); /* store new path */ |
| 8405 | lua_pop(L, 1); /* pop package variable */ |
Tim Duesterhus | c9fc9f2 | 2020-01-12 13:55:39 +0100 | [diff] [blame] | 8406 | |
| 8407 | return 0; |
| 8408 | } |
| 8409 | |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8410 | 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] | 8411 | const struct proxy *defpx, const char *file, int line, |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8412 | char **err) |
| 8413 | { |
| 8414 | char *path; |
| 8415 | char *type = "path"; |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 8416 | struct prepend_path *p = NULL; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8417 | |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8418 | if (too_many_args(2, args, err, NULL)) { |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 8419 | goto err; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8420 | } |
| 8421 | |
| 8422 | if (!(*args[1])) { |
| 8423 | memprintf(err, "'%s' expects to receive a <path> as argument", args[0]); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 8424 | goto err; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8425 | } |
| 8426 | path = args[1]; |
| 8427 | |
| 8428 | if (*args[2]) { |
| 8429 | if (strcmp(args[2], "path") != 0 && strcmp(args[2], "cpath") != 0) { |
| 8430 | 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] | 8431 | goto err; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8432 | } |
| 8433 | type = args[2]; |
| 8434 | } |
| 8435 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8436 | p = calloc(1, sizeof(*p)); |
| 8437 | if (p == NULL) { |
Tim Duesterhus | f89d43a | 2021-01-03 20:04:37 +0100 | [diff] [blame] | 8438 | memprintf(err, "memory allocation failed"); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 8439 | goto err; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8440 | } |
| 8441 | p->path = strdup(path); |
| 8442 | if (p->path == NULL) { |
Tim Duesterhus | f89d43a | 2021-01-03 20:04:37 +0100 | [diff] [blame] | 8443 | memprintf(err, "memory allocation failed"); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 8444 | goto err2; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8445 | } |
| 8446 | p->type = strdup(type); |
| 8447 | if (p->type == NULL) { |
Tim Duesterhus | f89d43a | 2021-01-03 20:04:37 +0100 | [diff] [blame] | 8448 | memprintf(err, "memory allocation failed"); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 8449 | goto err2; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8450 | } |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 8451 | LIST_APPEND(&prepend_path_list, &p->l); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8452 | |
| 8453 | hlua_prepend_path(hlua_states[0], type, path); |
| 8454 | hlua_prepend_path(hlua_states[1], type, path); |
| 8455 | return 0; |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 8456 | |
| 8457 | err2: |
| 8458 | free(p->type); |
| 8459 | free(p->path); |
| 8460 | err: |
| 8461 | free(p); |
| 8462 | return -1; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8463 | } |
| 8464 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8465 | /* configuration keywords declaration */ |
| 8466 | static struct cfg_kw_list cfg_kws = {{ },{ |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8467 | { CFG_GLOBAL, "lua-prepend-path", hlua_config_prepend_path }, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8468 | { CFG_GLOBAL, "lua-load", hlua_load }, |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8469 | { CFG_GLOBAL, "lua-load-per-thread", hlua_load_per_thread }, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8470 | { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout }, |
| 8471 | { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout }, |
Thierry FOURNIER | 56da101 | 2015-10-01 08:42:31 +0200 | [diff] [blame] | 8472 | { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout }, |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 8473 | { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield }, |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8474 | { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem }, |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8475 | { 0, NULL, NULL }, |
| 8476 | }}; |
| 8477 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 8478 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |
| 8479 | |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 8480 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8481 | /* This function can fail with an abort() due to an Lua critical error. |
| 8482 | * We are in the initialisation process of HAProxy, this abort() is |
| 8483 | * tolerated. |
| 8484 | */ |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8485 | int hlua_post_init_state(lua_State *L) |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8486 | { |
| 8487 | struct hlua_init_function *init; |
| 8488 | const char *msg; |
| 8489 | enum hlua_exec ret; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8490 | const char *error; |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8491 | const char *kind; |
| 8492 | const char *trace; |
| 8493 | int return_status = 1; |
| 8494 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
| 8495 | int nres; |
| 8496 | #endif |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8497 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 8498 | /* disable memory limit checks if limit is not set */ |
| 8499 | if (!hlua_global_allocator.limit) |
| 8500 | hlua_global_allocator.limit = ~hlua_global_allocator.limit; |
| 8501 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 8502 | /* Call post initialisation function in safe environment. */ |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 8503 | if (setjmp(safe_ljmp_env) != 0) { |
| 8504 | lua_atpanic(L, hlua_panic_safe); |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8505 | if (lua_type(L, -1) == LUA_TSTRING) |
| 8506 | error = lua_tostring(L, -1); |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 8507 | else |
| 8508 | error = "critical error"; |
| 8509 | fprintf(stderr, "Lua post-init: %s.\n", error); |
| 8510 | exit(1); |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 8511 | } else { |
| 8512 | lua_atpanic(L, hlua_panic_ljmp); |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 8513 | } |
Frédéric Lécaille | 54f2bcf | 2018-08-29 13:46:24 +0200 | [diff] [blame] | 8514 | |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8515 | hlua_fcn_post_init(L); |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 8516 | |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8517 | list_for_each_entry(init, &hlua_init_functions[hlua_state_id], l) { |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8518 | lua_rawgeti(L, LUA_REGISTRYINDEX, init->function_ref); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8519 | |
| 8520 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8521 | ret = lua_resume(L, L, 0, &nres); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8522 | #else |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8523 | ret = lua_resume(L, L, 0); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8524 | #endif |
| 8525 | kind = NULL; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8526 | switch (ret) { |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8527 | |
| 8528 | case LUA_OK: |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8529 | lua_pop(L, -1); |
Thierry Fournier | 13d08b7 | 2020-11-28 11:02:58 +0100 | [diff] [blame] | 8530 | break; |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8531 | |
| 8532 | case LUA_ERRERR: |
| 8533 | kind = "message handler error"; |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 8534 | /* Fall through */ |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8535 | case LUA_ERRRUN: |
| 8536 | if (!kind) |
| 8537 | kind = "runtime error"; |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8538 | msg = lua_tostring(L, -1); |
| 8539 | lua_settop(L, 0); /* Empty the stack. */ |
| 8540 | lua_pop(L, 1); |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 8541 | trace = hlua_traceback(L, ", "); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8542 | if (msg) |
| 8543 | ha_alert("Lua init: %s: '%s' from %s\n", kind, msg, trace); |
| 8544 | else |
| 8545 | ha_alert("Lua init: unknown %s from %s\n", kind, trace); |
| 8546 | return_status = 0; |
| 8547 | break; |
| 8548 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8549 | default: |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8550 | /* Unknown error */ |
| 8551 | kind = "Unknown error"; |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 8552 | /* Fall through */ |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8553 | case LUA_YIELD: |
| 8554 | /* yield is not configured at this step, this state doesn't happen */ |
| 8555 | if (!kind) |
| 8556 | kind = "yield not allowed"; |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 8557 | /* Fall through */ |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8558 | case LUA_ERRMEM: |
| 8559 | if (!kind) |
| 8560 | kind = "out of memory error"; |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8561 | lua_settop(L, 0); |
| 8562 | lua_pop(L, 1); |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 8563 | trace = hlua_traceback(L, ", "); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8564 | ha_alert("Lua init: %s: %s\n", kind, trace); |
| 8565 | return_status = 0; |
| 8566 | break; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8567 | } |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8568 | if (!return_status) |
| 8569 | break; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8570 | } |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 8571 | |
| 8572 | lua_atpanic(L, hlua_panic_safe); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8573 | return return_status; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8574 | } |
| 8575 | |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8576 | int hlua_post_init() |
| 8577 | { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8578 | int ret; |
| 8579 | int i; |
| 8580 | int errors; |
| 8581 | char *err = NULL; |
| 8582 | struct hlua_function *fcn; |
| 8583 | |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8584 | #if USE_OPENSSL |
| 8585 | /* Initialize SSL server. */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 8586 | if (socket_ssl->xprt->prepare_srv) { |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8587 | int saved_used_backed = global.ssl_used_backend; |
| 8588 | // don't affect maxconn automatic computation |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 8589 | socket_ssl->xprt->prepare_srv(socket_ssl); |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8590 | global.ssl_used_backend = saved_used_backed; |
| 8591 | } |
| 8592 | #endif |
| 8593 | |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8594 | /* Perform post init of common thread */ |
| 8595 | hlua_state_id = 0; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8596 | ha_set_tid(0); |
| 8597 | ret = hlua_post_init_state(hlua_states[hlua_state_id]); |
| 8598 | if (ret == 0) |
| 8599 | return 0; |
| 8600 | |
| 8601 | /* init remaining lua states and load files */ |
| 8602 | for (hlua_state_id = 2; hlua_state_id < global.nbthread + 1; hlua_state_id++) { |
| 8603 | |
| 8604 | /* set thread context */ |
| 8605 | ha_set_tid(hlua_state_id - 1); |
| 8606 | |
| 8607 | /* Init lua state */ |
| 8608 | hlua_states[hlua_state_id] = hlua_init_state(hlua_state_id); |
| 8609 | |
| 8610 | /* Load lua files */ |
| 8611 | for (i = 0; per_thread_load && per_thread_load[i]; i++) { |
| 8612 | ret = hlua_load_state(per_thread_load[i], hlua_states[hlua_state_id], &err); |
| 8613 | if (ret != 0) { |
| 8614 | ha_alert("Lua init: %s\n", err); |
| 8615 | return 0; |
| 8616 | } |
| 8617 | } |
| 8618 | } |
| 8619 | |
| 8620 | /* Reset thread context */ |
| 8621 | ha_set_tid(0); |
| 8622 | |
| 8623 | /* Execute post init for all states */ |
| 8624 | for (hlua_state_id = 1; hlua_state_id < global.nbthread + 1; hlua_state_id++) { |
| 8625 | |
| 8626 | /* set thread context */ |
| 8627 | ha_set_tid(hlua_state_id - 1); |
| 8628 | |
| 8629 | /* run post init */ |
| 8630 | ret = hlua_post_init_state(hlua_states[hlua_state_id]); |
| 8631 | if (ret == 0) |
| 8632 | return 0; |
| 8633 | } |
| 8634 | |
| 8635 | /* Reset thread context */ |
| 8636 | ha_set_tid(0); |
| 8637 | |
| 8638 | /* control functions registering. Each function must have: |
| 8639 | * - only the function_ref[0] set positive and all other to -1 |
| 8640 | * - only the function_ref[0] set to -1 and all other positive |
| 8641 | * This ensure a same reference is not used both in shared |
| 8642 | * lua state and thread dedicated lua state. Note: is the case |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 8643 | * reach, the shared state is priority, but the bug will be |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8644 | * complicated to found for the end user. |
| 8645 | */ |
| 8646 | errors = 0; |
| 8647 | list_for_each_entry(fcn, &referenced_functions, l) { |
| 8648 | ret = 0; |
| 8649 | for (i = 1; i < global.nbthread + 1; i++) { |
| 8650 | if (fcn->function_ref[i] == -1) |
| 8651 | ret--; |
| 8652 | else |
| 8653 | ret++; |
| 8654 | } |
| 8655 | if (abs(ret) != global.nbthread) { |
| 8656 | ha_alert("Lua function '%s' is not referenced in all thread. " |
| 8657 | "Expect function in all thread or in none thread.\n", fcn->name); |
| 8658 | errors++; |
| 8659 | continue; |
| 8660 | } |
| 8661 | |
| 8662 | if ((fcn->function_ref[0] == -1) == (ret < 0)) { |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 8663 | ha_alert("Lua function '%s' is referenced both ins shared Lua context (through lua-load) " |
| 8664 | "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] | 8665 | "exclusive.\n", fcn->name); |
| 8666 | errors++; |
| 8667 | } |
| 8668 | } |
| 8669 | |
| 8670 | if (errors > 0) |
| 8671 | return 0; |
| 8672 | |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 8673 | /* 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] | 8674 | * -1 in order to have probably a segfault if someone use it |
| 8675 | */ |
| 8676 | hlua_state_id = -1; |
| 8677 | |
| 8678 | return 1; |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8679 | } |
| 8680 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8681 | /* The memory allocator used by the Lua stack. <ud> is a pointer to the |
| 8682 | * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize> |
| 8683 | * 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] | 8684 | * allocation. <nsize> is the requested new size. A new allocation is |
| 8685 | * indicated by <ptr> being NULL. A free is indicated by <nsize> being |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 8686 | * zero. This one verifies that the limits are respected but is optimized |
| 8687 | * for the fast case where limits are not used, hence stats are not updated. |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8688 | */ |
| 8689 | static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize) |
| 8690 | { |
| 8691 | struct hlua_mem_allocator *zone = ud; |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 8692 | size_t limit, old, new; |
| 8693 | |
Tim Duesterhus | 2258652 | 2021-01-08 10:35:33 +0100 | [diff] [blame] | 8694 | if (unlikely(!ptr && !nsize)) |
| 8695 | return NULL; |
| 8696 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 8697 | /* a limit of ~0 means unlimited and boot complete, so there's no need |
| 8698 | * for accounting anymore. |
| 8699 | */ |
Christopher Faulet | cc2c4f8 | 2021-03-24 14:52:24 +0100 | [diff] [blame] | 8700 | if (likely(~zone->limit == 0)) |
| 8701 | return realloc(ptr, nsize); |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8702 | |
Willy Tarreau | d36c7fa | 2020-12-02 12:26:29 +0100 | [diff] [blame] | 8703 | if (!ptr) |
| 8704 | osize = 0; |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8705 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 8706 | /* enforce strict limits across all threads */ |
| 8707 | limit = zone->limit; |
| 8708 | old = _HA_ATOMIC_LOAD(&zone->allocated); |
| 8709 | do { |
| 8710 | new = old + nsize - osize; |
| 8711 | if (unlikely(nsize && limit && new > limit)) |
| 8712 | return NULL; |
| 8713 | } while (!_HA_ATOMIC_CAS(&zone->allocated, &old, new)); |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8714 | |
| 8715 | ptr = realloc(ptr, nsize); |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 8716 | |
| 8717 | if (unlikely(!ptr && nsize)) // failed |
| 8718 | _HA_ATOMIC_SUB(&zone->allocated, nsize - osize); |
| 8719 | |
| 8720 | __ha_barrier_atomic_store(); |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8721 | return ptr; |
| 8722 | } |
| 8723 | |
Thierry Fournier | ecb83c2 | 2020-11-28 15:49:44 +0100 | [diff] [blame] | 8724 | /* 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] | 8725 | * We are in the initialisation process of HAProxy, this abort() is |
| 8726 | * tolerated. |
| 8727 | */ |
Thierry Fournier | ecb83c2 | 2020-11-28 15:49:44 +0100 | [diff] [blame] | 8728 | lua_State *hlua_init_state(int thread_num) |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 8729 | { |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8730 | int i; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8731 | int idx; |
| 8732 | struct sample_fetch *sf; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8733 | struct sample_conv *sc; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8734 | char *p; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8735 | const char *error_msg; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 8736 | void **context; |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8737 | lua_State *L; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8738 | struct prepend_path *pp; |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8739 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 8740 | /* Init main lua stack. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8741 | L = lua_newstate(hlua_alloc, &hlua_global_allocator); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 8742 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 8743 | /* Initialise Lua context to NULL */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8744 | context = lua_getextraspace(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 8745 | *context = NULL; |
| 8746 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 8747 | /* From this point, until the end of the initialisation function, |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8748 | * the Lua function can fail with an abort. We are in the initialisation |
| 8749 | * process of HAProxy, this abort() is tolerated. |
| 8750 | */ |
| 8751 | |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 8752 | /* Call post initialisation function in safe environment. */ |
| 8753 | if (setjmp(safe_ljmp_env) != 0) { |
| 8754 | lua_atpanic(L, hlua_panic_safe); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8755 | if (lua_type(L, -1) == LUA_TSTRING) |
| 8756 | error_msg = lua_tostring(L, -1); |
Thierry Fournier | 2f05cc6 | 2020-11-28 16:08:02 +0100 | [diff] [blame] | 8757 | else |
| 8758 | error_msg = "critical error"; |
| 8759 | fprintf(stderr, "Lua init: %s.\n", error_msg); |
| 8760 | exit(1); |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 8761 | } else { |
| 8762 | lua_atpanic(L, hlua_panic_ljmp); |
Thierry Fournier | 2f05cc6 | 2020-11-28 16:08:02 +0100 | [diff] [blame] | 8763 | } |
| 8764 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 8765 | /* Initialise lua. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8766 | luaL_openlibs(L); |
Tim Duesterhus | 541fe1e | 2020-01-12 13:55:41 +0100 | [diff] [blame] | 8767 | #define HLUA_PREPEND_PATH_TOSTRING1(x) #x |
| 8768 | #define HLUA_PREPEND_PATH_TOSTRING(x) HLUA_PREPEND_PATH_TOSTRING1(x) |
| 8769 | #ifdef HLUA_PREPEND_PATH |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8770 | hlua_prepend_path(L, "path", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_PATH)); |
Tim Duesterhus | 541fe1e | 2020-01-12 13:55:41 +0100 | [diff] [blame] | 8771 | #endif |
| 8772 | #ifdef HLUA_PREPEND_CPATH |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8773 | hlua_prepend_path(L, "cpath", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_CPATH)); |
Tim Duesterhus | 541fe1e | 2020-01-12 13:55:41 +0100 | [diff] [blame] | 8774 | #endif |
| 8775 | #undef HLUA_PREPEND_PATH_TOSTRING |
| 8776 | #undef HLUA_PREPEND_PATH_TOSTRING1 |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8777 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8778 | /* Apply configured prepend path */ |
| 8779 | list_for_each_entry(pp, &prepend_path_list, l) |
| 8780 | hlua_prepend_path(L, pp->type, pp->path); |
| 8781 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8782 | /* |
| 8783 | * |
| 8784 | * Create "core" object. |
| 8785 | * |
| 8786 | */ |
| 8787 | |
Thierry FOURNIER | a2d8c65 | 2015-03-11 17:29:39 +0100 | [diff] [blame] | 8788 | /* This table entry is the object "core" base. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8789 | lua_newtable(L); |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8790 | |
Thierry Fournier | ecb83c2 | 2020-11-28 15:49:44 +0100 | [diff] [blame] | 8791 | /* set the thread id */ |
| 8792 | hlua_class_const_int(L, "thread", thread_num); |
| 8793 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8794 | /* Push the loglevel constants. */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8795 | for (i = 0; i < NB_LOG_LEVELS; i++) |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8796 | hlua_class_const_int(L, log_levels[i], i); |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8797 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8798 | /* Register special functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8799 | hlua_class_function(L, "register_init", hlua_register_init); |
| 8800 | hlua_class_function(L, "register_task", hlua_register_task); |
| 8801 | hlua_class_function(L, "register_fetches", hlua_register_fetches); |
| 8802 | hlua_class_function(L, "register_converters", hlua_register_converters); |
| 8803 | hlua_class_function(L, "register_action", hlua_register_action); |
| 8804 | hlua_class_function(L, "register_service", hlua_register_service); |
| 8805 | hlua_class_function(L, "register_cli", hlua_register_cli); |
| 8806 | hlua_class_function(L, "yield", hlua_yield); |
| 8807 | hlua_class_function(L, "set_nice", hlua_set_nice); |
| 8808 | hlua_class_function(L, "sleep", hlua_sleep); |
| 8809 | hlua_class_function(L, "msleep", hlua_msleep); |
| 8810 | hlua_class_function(L, "add_acl", hlua_add_acl); |
| 8811 | hlua_class_function(L, "del_acl", hlua_del_acl); |
| 8812 | hlua_class_function(L, "set_map", hlua_set_map); |
| 8813 | hlua_class_function(L, "del_map", hlua_del_map); |
| 8814 | hlua_class_function(L, "tcp", hlua_socket_new); |
| 8815 | hlua_class_function(L, "log", hlua_log); |
| 8816 | hlua_class_function(L, "Debug", hlua_log_debug); |
| 8817 | hlua_class_function(L, "Info", hlua_log_info); |
| 8818 | hlua_class_function(L, "Warning", hlua_log_warning); |
| 8819 | hlua_class_function(L, "Alert", hlua_log_alert); |
| 8820 | hlua_class_function(L, "done", hlua_done); |
| 8821 | hlua_fcn_reg_core_fcn(L); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8822 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8823 | lua_setglobal(L, "core"); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 8824 | |
| 8825 | /* |
| 8826 | * |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 8827 | * Create "act" object. |
| 8828 | * |
| 8829 | */ |
| 8830 | |
| 8831 | /* This table entry is the object "act" base. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8832 | lua_newtable(L); |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 8833 | |
| 8834 | /* push action return constants */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8835 | hlua_class_const_int(L, "CONTINUE", ACT_RET_CONT); |
| 8836 | hlua_class_const_int(L, "STOP", ACT_RET_STOP); |
| 8837 | hlua_class_const_int(L, "YIELD", ACT_RET_YIELD); |
| 8838 | hlua_class_const_int(L, "ERROR", ACT_RET_ERR); |
| 8839 | hlua_class_const_int(L, "DONE", ACT_RET_DONE); |
| 8840 | hlua_class_const_int(L, "DENY", ACT_RET_DENY); |
| 8841 | hlua_class_const_int(L, "ABORT", ACT_RET_ABRT); |
| 8842 | hlua_class_const_int(L, "INVALID", ACT_RET_INV); |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 8843 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8844 | hlua_class_function(L, "wake_time", hlua_set_wake_time); |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 8845 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8846 | lua_setglobal(L, "act"); |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 8847 | |
| 8848 | /* |
| 8849 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8850 | * Register class Map |
| 8851 | * |
| 8852 | */ |
| 8853 | |
| 8854 | /* This table entry is the object "Map" base. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8855 | lua_newtable(L); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8856 | |
| 8857 | /* register pattern types. */ |
| 8858 | for (i=0; i<PAT_MATCH_NUM; i++) |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8859 | hlua_class_const_int(L, pat_match_names[i], i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 8860 | for (i=0; i<PAT_MATCH_NUM; i++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8861 | snprintf(trash.area, trash.size, "_%s", pat_match_names[i]); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8862 | hlua_class_const_int(L, trash.area, i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 8863 | } |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8864 | |
| 8865 | /* register constructor. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8866 | hlua_class_function(L, "new", hlua_map_new); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8867 | |
| 8868 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8869 | lua_newtable(L); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8870 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8871 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8872 | lua_pushstring(L, "__index"); |
| 8873 | lua_newtable(L); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8874 | |
| 8875 | /* Register . */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8876 | hlua_class_function(L, "lookup", hlua_map_lookup); |
| 8877 | hlua_class_function(L, "slookup", hlua_map_slookup); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8878 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8879 | lua_rawset(L, -3); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8880 | |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8881 | /* Register previous table in the registry with reference and named entry. |
| 8882 | * The function hlua_register_metatable() pops the stack, so we |
| 8883 | * previously create a copy of the table. |
| 8884 | */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8885 | lua_pushvalue(L, -1); /* Copy the -1 entry and push it on the stack. */ |
| 8886 | class_map_ref = hlua_register_metatable(L, CLASS_MAP); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8887 | |
| 8888 | /* Assign the metatable to the mai Map object. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8889 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8890 | |
| 8891 | /* Set a name to the table. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8892 | lua_setglobal(L, "Map"); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8893 | |
| 8894 | /* |
| 8895 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8896 | * Register class Channel |
| 8897 | * |
| 8898 | */ |
| 8899 | |
| 8900 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8901 | lua_newtable(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8902 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8903 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8904 | lua_pushstring(L, "__index"); |
| 8905 | lua_newtable(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8906 | |
| 8907 | /* Register . */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8908 | hlua_class_function(L, "get", hlua_channel_get); |
| 8909 | hlua_class_function(L, "dup", hlua_channel_dup); |
| 8910 | hlua_class_function(L, "getline", hlua_channel_getline); |
| 8911 | hlua_class_function(L, "set", hlua_channel_set); |
| 8912 | hlua_class_function(L, "append", hlua_channel_append); |
| 8913 | hlua_class_function(L, "send", hlua_channel_send); |
| 8914 | hlua_class_function(L, "forward", hlua_channel_forward); |
| 8915 | hlua_class_function(L, "get_in_len", hlua_channel_get_in_len); |
| 8916 | hlua_class_function(L, "get_out_len", hlua_channel_get_out_len); |
| 8917 | hlua_class_function(L, "is_full", hlua_channel_is_full); |
| 8918 | hlua_class_function(L, "is_resp", hlua_channel_is_resp); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8919 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8920 | lua_rawset(L, -3); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8921 | |
| 8922 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8923 | class_channel_ref = hlua_register_metatable(L, CLASS_CHANNEL); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8924 | |
| 8925 | /* |
| 8926 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8927 | * Register class Fetches |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 8928 | * |
| 8929 | */ |
| 8930 | |
| 8931 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8932 | lua_newtable(L); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 8933 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8934 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8935 | lua_pushstring(L, "__index"); |
| 8936 | lua_newtable(L); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 8937 | |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8938 | /* Browse existing fetches and create the associated |
| 8939 | * object method. |
| 8940 | */ |
| 8941 | sf = NULL; |
| 8942 | while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) { |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8943 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 8944 | * by an underscore. |
| 8945 | */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8946 | strncpy(trash.area, sf->kw, trash.size); |
| 8947 | trash.area[trash.size - 1] = '\0'; |
| 8948 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8949 | if (*p == '.' || *p == '-' || *p == '+') |
| 8950 | *p = '_'; |
| 8951 | |
| 8952 | /* Register the function. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8953 | lua_pushstring(L, trash.area); |
| 8954 | lua_pushlightuserdata(L, sf); |
| 8955 | lua_pushcclosure(L, hlua_run_sample_fetch, 1); |
| 8956 | lua_rawset(L, -3); |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8957 | } |
| 8958 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8959 | lua_rawset(L, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8960 | |
| 8961 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8962 | class_fetches_ref = hlua_register_metatable(L, CLASS_FETCHES); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8963 | |
| 8964 | /* |
| 8965 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8966 | * Register class Converters |
| 8967 | * |
| 8968 | */ |
| 8969 | |
| 8970 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8971 | lua_newtable(L); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8972 | |
| 8973 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8974 | lua_pushstring(L, "__index"); |
| 8975 | lua_newtable(L); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8976 | |
| 8977 | /* Browse existing converters and create the associated |
| 8978 | * object method. |
| 8979 | */ |
| 8980 | sc = NULL; |
| 8981 | while ((sc = sample_conv_getnext(sc, &idx)) != NULL) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8982 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 8983 | * by an underscore. |
| 8984 | */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8985 | strncpy(trash.area, sc->kw, trash.size); |
| 8986 | trash.area[trash.size - 1] = '\0'; |
| 8987 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8988 | if (*p == '.' || *p == '-' || *p == '+') |
| 8989 | *p = '_'; |
| 8990 | |
| 8991 | /* Register the function. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8992 | lua_pushstring(L, trash.area); |
| 8993 | lua_pushlightuserdata(L, sc); |
| 8994 | lua_pushcclosure(L, hlua_run_sample_conv, 1); |
| 8995 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8996 | } |
| 8997 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8998 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8999 | |
| 9000 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9001 | class_converters_ref = hlua_register_metatable(L, CLASS_CONVERTERS); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 9002 | |
| 9003 | /* |
| 9004 | * |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 9005 | * Register class HTTP |
| 9006 | * |
| 9007 | */ |
| 9008 | |
| 9009 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9010 | lua_newtable(L); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 9011 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 9012 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9013 | lua_pushstring(L, "__index"); |
| 9014 | lua_newtable(L); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 9015 | |
| 9016 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9017 | hlua_class_function(L, "req_get_headers",hlua_http_req_get_headers); |
| 9018 | hlua_class_function(L, "req_del_header", hlua_http_req_del_hdr); |
| 9019 | hlua_class_function(L, "req_rep_header", hlua_http_req_rep_hdr); |
| 9020 | hlua_class_function(L, "req_rep_value", hlua_http_req_rep_val); |
| 9021 | hlua_class_function(L, "req_add_header", hlua_http_req_add_hdr); |
| 9022 | hlua_class_function(L, "req_set_header", hlua_http_req_set_hdr); |
| 9023 | hlua_class_function(L, "req_set_method", hlua_http_req_set_meth); |
| 9024 | hlua_class_function(L, "req_set_path", hlua_http_req_set_path); |
| 9025 | hlua_class_function(L, "req_set_query", hlua_http_req_set_query); |
| 9026 | hlua_class_function(L, "req_set_uri", hlua_http_req_set_uri); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 9027 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9028 | hlua_class_function(L, "res_get_headers",hlua_http_res_get_headers); |
| 9029 | hlua_class_function(L, "res_del_header", hlua_http_res_del_hdr); |
| 9030 | hlua_class_function(L, "res_rep_header", hlua_http_res_rep_hdr); |
| 9031 | hlua_class_function(L, "res_rep_value", hlua_http_res_rep_val); |
| 9032 | hlua_class_function(L, "res_add_header", hlua_http_res_add_hdr); |
| 9033 | hlua_class_function(L, "res_set_header", hlua_http_res_set_hdr); |
| 9034 | hlua_class_function(L, "res_set_status", hlua_http_res_set_status); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 9035 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9036 | lua_rawset(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 9037 | |
| 9038 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9039 | class_http_ref = hlua_register_metatable(L, CLASS_HTTP); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 9040 | |
| 9041 | /* |
| 9042 | * |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9043 | * Register class AppletTCP |
| 9044 | * |
| 9045 | */ |
| 9046 | |
| 9047 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9048 | lua_newtable(L); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9049 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 9050 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9051 | lua_pushstring(L, "__index"); |
| 9052 | lua_newtable(L); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9053 | |
| 9054 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9055 | hlua_class_function(L, "getline", hlua_applet_tcp_getline); |
| 9056 | hlua_class_function(L, "receive", hlua_applet_tcp_recv); |
| 9057 | hlua_class_function(L, "send", hlua_applet_tcp_send); |
| 9058 | hlua_class_function(L, "set_priv", hlua_applet_tcp_set_priv); |
| 9059 | hlua_class_function(L, "get_priv", hlua_applet_tcp_get_priv); |
| 9060 | hlua_class_function(L, "set_var", hlua_applet_tcp_set_var); |
| 9061 | hlua_class_function(L, "unset_var", hlua_applet_tcp_unset_var); |
| 9062 | hlua_class_function(L, "get_var", hlua_applet_tcp_get_var); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9063 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9064 | lua_settable(L, -3); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9065 | |
| 9066 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9067 | class_applet_tcp_ref = hlua_register_metatable(L, CLASS_APPLET_TCP); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9068 | |
| 9069 | /* |
| 9070 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9071 | * Register class AppletHTTP |
| 9072 | * |
| 9073 | */ |
| 9074 | |
| 9075 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9076 | lua_newtable(L); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9077 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 9078 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9079 | lua_pushstring(L, "__index"); |
| 9080 | lua_newtable(L); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9081 | |
| 9082 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9083 | hlua_class_function(L, "set_priv", hlua_applet_http_set_priv); |
| 9084 | hlua_class_function(L, "get_priv", hlua_applet_http_get_priv); |
| 9085 | hlua_class_function(L, "set_var", hlua_applet_http_set_var); |
| 9086 | hlua_class_function(L, "unset_var", hlua_applet_http_unset_var); |
| 9087 | hlua_class_function(L, "get_var", hlua_applet_http_get_var); |
| 9088 | hlua_class_function(L, "getline", hlua_applet_http_getline); |
| 9089 | hlua_class_function(L, "receive", hlua_applet_http_recv); |
| 9090 | hlua_class_function(L, "send", hlua_applet_http_send); |
| 9091 | hlua_class_function(L, "add_header", hlua_applet_http_addheader); |
| 9092 | hlua_class_function(L, "set_status", hlua_applet_http_status); |
| 9093 | hlua_class_function(L, "start_response", hlua_applet_http_start_response); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9094 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9095 | lua_settable(L, -3); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9096 | |
| 9097 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9098 | class_applet_http_ref = hlua_register_metatable(L, CLASS_APPLET_HTTP); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9099 | |
| 9100 | /* |
| 9101 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 9102 | * Register class TXN |
| 9103 | * |
| 9104 | */ |
| 9105 | |
| 9106 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9107 | lua_newtable(L); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 9108 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 9109 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9110 | lua_pushstring(L, "__index"); |
| 9111 | lua_newtable(L); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 9112 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 9113 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9114 | hlua_class_function(L, "set_priv", hlua_set_priv); |
| 9115 | hlua_class_function(L, "get_priv", hlua_get_priv); |
| 9116 | hlua_class_function(L, "set_var", hlua_set_var); |
| 9117 | hlua_class_function(L, "unset_var", hlua_unset_var); |
| 9118 | hlua_class_function(L, "get_var", hlua_get_var); |
| 9119 | hlua_class_function(L, "done", hlua_txn_done); |
| 9120 | hlua_class_function(L, "reply", hlua_txn_reply_new); |
| 9121 | hlua_class_function(L, "set_loglevel", hlua_txn_set_loglevel); |
| 9122 | hlua_class_function(L, "set_tos", hlua_txn_set_tos); |
| 9123 | hlua_class_function(L, "set_mark", hlua_txn_set_mark); |
| 9124 | hlua_class_function(L, "set_priority_class", hlua_txn_set_priority_class); |
| 9125 | hlua_class_function(L, "set_priority_offset", hlua_txn_set_priority_offset); |
| 9126 | hlua_class_function(L, "deflog", hlua_txn_deflog); |
| 9127 | hlua_class_function(L, "log", hlua_txn_log); |
| 9128 | hlua_class_function(L, "Debug", hlua_txn_log_debug); |
| 9129 | hlua_class_function(L, "Info", hlua_txn_log_info); |
| 9130 | hlua_class_function(L, "Warning", hlua_txn_log_warning); |
| 9131 | hlua_class_function(L, "Alert", hlua_txn_log_alert); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 9132 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9133 | lua_rawset(L, -3); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 9134 | |
| 9135 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9136 | class_txn_ref = hlua_register_metatable(L, CLASS_TXN); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9137 | |
| 9138 | /* |
| 9139 | * |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 9140 | * Register class reply |
| 9141 | * |
| 9142 | */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9143 | lua_newtable(L); |
| 9144 | lua_pushstring(L, "__index"); |
| 9145 | lua_newtable(L); |
| 9146 | hlua_class_function(L, "set_status", hlua_txn_reply_set_status); |
| 9147 | hlua_class_function(L, "add_header", hlua_txn_reply_add_header); |
| 9148 | hlua_class_function(L, "del_header", hlua_txn_reply_del_header); |
| 9149 | hlua_class_function(L, "set_body", hlua_txn_reply_set_body); |
| 9150 | lua_settable(L, -3); /* Sets the __index entry. */ |
| 9151 | class_txn_reply_ref = luaL_ref(L, LUA_REGISTRYINDEX); |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 9152 | |
| 9153 | |
| 9154 | /* |
| 9155 | * |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9156 | * Register class Socket |
| 9157 | * |
| 9158 | */ |
| 9159 | |
| 9160 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9161 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9162 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 9163 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9164 | lua_pushstring(L, "__index"); |
| 9165 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9166 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 9167 | #ifdef USE_OPENSSL |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9168 | hlua_class_function(L, "connect_ssl", hlua_socket_connect_ssl); |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 9169 | #endif |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9170 | hlua_class_function(L, "connect", hlua_socket_connect); |
| 9171 | hlua_class_function(L, "send", hlua_socket_send); |
| 9172 | hlua_class_function(L, "receive", hlua_socket_receive); |
| 9173 | hlua_class_function(L, "close", hlua_socket_close); |
| 9174 | hlua_class_function(L, "getpeername", hlua_socket_getpeername); |
| 9175 | hlua_class_function(L, "getsockname", hlua_socket_getsockname); |
| 9176 | hlua_class_function(L, "setoption", hlua_socket_setoption); |
| 9177 | hlua_class_function(L, "settimeout", hlua_socket_settimeout); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9178 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9179 | 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] | 9180 | |
| 9181 | /* Register the garbage collector entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9182 | lua_pushstring(L, "__gc"); |
| 9183 | lua_pushcclosure(L, hlua_socket_gc, 0); |
| 9184 | 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] | 9185 | |
| 9186 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9187 | class_socket_ref = hlua_register_metatable(L, CLASS_SOCKET); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9188 | |
Thierry Fournier | aafc777 | 2020-12-04 11:47:47 +0100 | [diff] [blame] | 9189 | lua_atpanic(L, hlua_panic_safe); |
| 9190 | |
| 9191 | return L; |
| 9192 | } |
| 9193 | |
| 9194 | void hlua_init(void) { |
| 9195 | int i; |
Amaury Denoyelle | 239fdbf | 2021-03-24 10:22:03 +0100 | [diff] [blame] | 9196 | char *errmsg; |
Thierry Fournier | aafc777 | 2020-12-04 11:47:47 +0100 | [diff] [blame] | 9197 | #ifdef USE_OPENSSL |
| 9198 | struct srv_kw *kw; |
| 9199 | int tmp_error; |
| 9200 | char *error; |
| 9201 | char *args[] = { /* SSL client configuration. */ |
| 9202 | "ssl", |
| 9203 | "verify", |
| 9204 | "none", |
| 9205 | NULL |
| 9206 | }; |
| 9207 | #endif |
| 9208 | |
| 9209 | /* Init post init function list head */ |
| 9210 | for (i = 0; i < MAX_THREADS + 1; i++) |
| 9211 | LIST_INIT(&hlua_init_functions[i]); |
| 9212 | |
| 9213 | /* Init state for common/shared lua parts */ |
| 9214 | hlua_state_id = 0; |
| 9215 | ha_set_tid(0); |
| 9216 | hlua_states[0] = hlua_init_state(0); |
| 9217 | |
| 9218 | /* Init state 1 for thread 0. We have at least one thread. */ |
| 9219 | hlua_state_id = 1; |
| 9220 | ha_set_tid(0); |
| 9221 | hlua_states[1] = hlua_init_state(1); |
| 9222 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9223 | /* Proxy and server configuration initialisation. */ |
William Lallemand | 6bb77b9 | 2021-07-28 15:48:16 +0200 | [diff] [blame^] | 9224 | socket_proxy = alloc_new_proxy("LUA-SOCKET", PR_CAP_FE|PR_CAP_BE|PR_CAP_INT, &errmsg); |
Amaury Denoyelle | 239fdbf | 2021-03-24 10:22:03 +0100 | [diff] [blame] | 9225 | if (!socket_proxy) { |
| 9226 | fprintf(stderr, "Lua init: %s\n", errmsg); |
| 9227 | exit(1); |
| 9228 | } |
| 9229 | proxy_preset_defaults(socket_proxy); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9230 | |
| 9231 | /* Init TCP server: unchanged parameters */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 9232 | socket_tcp = new_server(socket_proxy); |
| 9233 | if (!socket_tcp) { |
| 9234 | fprintf(stderr, "Lua init: failed to allocate tcp server socket\n"); |
| 9235 | exit(1); |
| 9236 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9237 | |
| 9238 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9239 | /* Init TCP server: unchanged parameters */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 9240 | socket_ssl = new_server(socket_proxy); |
| 9241 | if (!socket_ssl) { |
| 9242 | fprintf(stderr, "Lua init: failed to allocate ssl server socket\n"); |
| 9243 | exit(1); |
| 9244 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9245 | |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 9246 | socket_ssl->use_ssl = 1; |
| 9247 | socket_ssl->xprt = xprt_get(XPRT_SSL); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9248 | |
Bertrand Jacquin | 80839ff | 2021-01-21 19:14:46 +0000 | [diff] [blame] | 9249 | for (i = 0; args[i] != NULL; i++) { |
| 9250 | 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] | 9251 | /* |
| 9252 | * |
| 9253 | * If the keyword is not known, we can search in the registered |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 9254 | * server keywords. This is useful to configure special SSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9255 | * features like client certificates and ssl_verify. |
| 9256 | * |
| 9257 | */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 9258 | tmp_error = kw->parse(args, &i, socket_proxy, socket_ssl, &error); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9259 | if (tmp_error != 0) { |
| 9260 | fprintf(stderr, "INTERNAL ERROR: %s\n", error); |
| 9261 | abort(); /* This must be never arrives because the command line |
| 9262 | not editable by the user. */ |
| 9263 | } |
Bertrand Jacquin | 80839ff | 2021-01-21 19:14:46 +0000 | [diff] [blame] | 9264 | i += kw->skip; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9265 | } |
| 9266 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9267 | #endif |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 9268 | |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 9269 | } |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 9270 | |
Tim Duesterhus | d0c0ca2 | 2020-07-04 11:53:26 +0200 | [diff] [blame] | 9271 | static void hlua_deinit() |
| 9272 | { |
Willy Tarreau | 186f376 | 2020-12-04 11:48:12 +0100 | [diff] [blame] | 9273 | int thr; |
| 9274 | |
| 9275 | for (thr = 0; thr < MAX_THREADS+1; thr++) { |
| 9276 | if (hlua_states[thr]) |
| 9277 | lua_close(hlua_states[thr]); |
| 9278 | } |
Willy Tarreau | 430bf4a | 2021-03-04 09:45:32 +0100 | [diff] [blame] | 9279 | |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 9280 | free_server(socket_tcp); |
Willy Tarreau | 430bf4a | 2021-03-04 09:45:32 +0100 | [diff] [blame] | 9281 | |
Willy Tarreau | 0f143af | 2021-03-05 10:41:48 +0100 | [diff] [blame] | 9282 | #ifdef USE_OPENSSL |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 9283 | free_server(socket_ssl); |
Willy Tarreau | 0f143af | 2021-03-05 10:41:48 +0100 | [diff] [blame] | 9284 | #endif |
Amaury Denoyelle | 239fdbf | 2021-03-24 10:22:03 +0100 | [diff] [blame] | 9285 | |
| 9286 | free_proxy(socket_proxy); |
Tim Duesterhus | d0c0ca2 | 2020-07-04 11:53:26 +0200 | [diff] [blame] | 9287 | } |
| 9288 | |
| 9289 | REGISTER_POST_DEINIT(hlua_deinit); |
| 9290 | |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 9291 | static void hlua_register_build_options(void) |
| 9292 | { |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 9293 | char *ptr = NULL; |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 9294 | |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 9295 | memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE); |
| 9296 | hap_register_build_opts(ptr, 1); |
| 9297 | } |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 9298 | |
| 9299 | INITCALL0(STG_REGISTER, hlua_register_build_options); |